enhanced-open-graph.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. if ( ! class_exists( 'Jetpack_Media_Summary' ) ) {
  3. if ( defined('IS_WPCOM') && IS_WPCOM ) {
  4. include WP_CONTENT_DIR . '/lib/class.wpcom-media-summary.php';
  5. } else {
  6. jetpack_require_lib( 'class.media-summary' );
  7. }
  8. }
  9. /**
  10. * Better OG Image Tags for Image Post Formats
  11. */
  12. function enhanced_og_image( $tags ) {
  13. if ( !is_singular() || post_password_required() )
  14. return $tags;
  15. global $post;
  16. // Always favor featured images.
  17. if ( enhanced_og_has_featured_image( $post->ID ) )
  18. return $tags;
  19. $summary = Jetpack_Media_Summary::get( $post->ID );
  20. if ( 'image' != $summary['type'] )
  21. return $tags;
  22. $tags['og:image'] = $summary['image'];
  23. $tags['og:image:secure_url'] = $summary['secure']['image'];
  24. return $tags;
  25. }
  26. add_filter( 'jetpack_open_graph_tags', 'enhanced_og_image' );
  27. /**
  28. * Better OG Image Tags for Gallery Post Formats
  29. */
  30. function enhanced_og_gallery( $tags ) {
  31. if ( !is_singular() || post_password_required() )
  32. return $tags;
  33. global $post;
  34. // Always favor featured images.
  35. if ( enhanced_og_has_featured_image( $post->ID ) )
  36. return $tags;
  37. $summary = Jetpack_Media_Summary::get( $post->ID );
  38. if ( 'gallery' != $summary['type'] )
  39. return $tags;
  40. if( !isset( $summary['images'] ) || !is_array( $summary['images'] ) || empty( $summary['images'] ) )
  41. return $tags;
  42. $images = $secures = array();
  43. foreach ( $summary['images'] as $i => $image ) {
  44. $images[] = $image['url'];
  45. $secures[] = $summary['secure']['images'][$i]['url'];
  46. }
  47. $tags['og:image'] = $images;
  48. $tags['og:image:secure_url'] = $secures;
  49. return $tags;
  50. }
  51. add_filter( 'jetpack_open_graph_tags', 'enhanced_og_gallery' );
  52. /**
  53. * Allows VideoPress, YouTube, and Vimeo videos to play inline on Facebook
  54. */
  55. function enhanced_og_video( $tags ) {
  56. if ( !is_singular() || post_password_required() )
  57. return $tags;
  58. global $post;
  59. // Always favor featured images.
  60. if ( enhanced_og_has_featured_image( $post->ID ) )
  61. return $tags;
  62. $summary = Jetpack_Media_Summary::get( $post->ID );
  63. if ( 'video' != $summary['type'] ) {
  64. if ( $summary['count']['video'] > 0 && $summary['count']['image'] < 1 ) {
  65. $tags['og:image'] = $summary['image'];
  66. $tags['og:image:secure_url'] = $summary['secure']['image'];
  67. }
  68. return $tags;
  69. }
  70. $tags['og:image'] = $summary['image'];
  71. $tags['og:image:secure_url'] = $summary['secure']['image'];
  72. // This should be html by default for youtube/vimeo, since we're linking to HTML pages.
  73. $tags['og:video:type'] = isset( $summary['video_type'] ) ? $summary['video_type'] : 'text/html';
  74. $video_url = $summary['video'];
  75. $secure_video_url = $summary['secure']['video'];
  76. if ( preg_match( '/((youtube|vimeo)\.com|youtu.be)/', $video_url ) ) {
  77. if ( strstr( $video_url, 'youtube' ) ) {
  78. $id = jetpack_get_youtube_id( $video_url );
  79. $video_url = 'http://www.youtube.com/embed/' . $id;
  80. $secure_video_url = 'https://www.youtube.com/embed/' . $id;
  81. } else if ( strstr( $video_url, 'vimeo' ) ) {
  82. preg_match( '|vimeo\.com/(\d+)/?$|i', $video_url, $match );
  83. $id = (int) $match[1];
  84. $video_url = 'http://vimeo.com/moogaloop.swf?clip_id=' . $id;
  85. $secure_video_url = 'https://vimeo.com/moogaloop.swf?clip_id=' . $id;
  86. }
  87. }
  88. $tags['og:video'] = $video_url;
  89. $tags['og:video:secure_url'] = $secure_video_url;
  90. if ( empty( $post->post_title ) )
  91. $tags['og:title'] = sprintf( __( 'Video on %s', 'jetpack' ), get_option( 'blogname' ) );
  92. return $tags;
  93. }
  94. add_filter( 'jetpack_open_graph_tags', 'enhanced_og_video' );
  95. function enhanced_og_has_featured_image( $post_id ) {
  96. $featured = Jetpack_PostImages::from_thumbnail( $post_id, 200, 200 );
  97. if ( !empty( $featured ) && count( $featured ) > 0 )
  98. return true;
  99. return false;
  100. }