Keine Beschreibung

enhanced-open-graph.php 3.9KB

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