/wp-content/plugins/jetpack/modules/publicize/enhanced-open-graph.php

https://gitlab.com/remyvianne/krowkaramel · PHP · 172 lines · 104 code · 34 blank · 34 comment · 27 complexity · 91f99cfe205da99d179114328a068379 MD5 · raw file

  1. <?php
  2. /**
  3. * Enhanced Open Graph for Jetpack.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. if ( ! class_exists( 'Jetpack_Media_Summary' ) ) {
  8. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  9. include WP_CONTENT_DIR . '/lib/class.wpcom-media-summary.php';
  10. } else {
  11. jetpack_require_lib( 'class.media-summary' );
  12. }
  13. }
  14. /**
  15. * Better OG Image Tags for Image Post Formats
  16. *
  17. * @param array $tags Array of Open Graph tags.
  18. */
  19. function enhanced_og_image( $tags ) {
  20. if ( ! is_singular() || post_password_required() ) {
  21. return $tags;
  22. }
  23. global $post;
  24. // Bail if we do not have info about the post.
  25. if ( ! $post instanceof WP_Post ) {
  26. return $tags;
  27. }
  28. // Always favor featured images.
  29. if ( enhanced_og_has_featured_image( $post->ID ) ) {
  30. return $tags;
  31. }
  32. $summary = Jetpack_Media_Summary::get( $post->ID );
  33. if ( 'image' !== $summary['type'] ) {
  34. return $tags;
  35. }
  36. $tags['og:image'] = $summary['image'];
  37. $tags['og:image:secure_url'] = $summary['secure']['image'];
  38. return $tags;
  39. }
  40. add_filter( 'jetpack_open_graph_tags', 'enhanced_og_image' );
  41. /**
  42. * Better OG Image Tags for Gallery Post Formats
  43. *
  44. * @param array $tags Array of Open Graph tags.
  45. */
  46. function enhanced_og_gallery( $tags ) {
  47. if ( ! is_singular() || post_password_required() ) {
  48. return $tags;
  49. }
  50. global $post;
  51. // Bail if we do not have info about the post.
  52. if ( ! $post instanceof WP_Post ) {
  53. return $tags;
  54. }
  55. // Always favor featured images.
  56. if ( enhanced_og_has_featured_image( $post->ID ) ) {
  57. return $tags;
  58. }
  59. $summary = Jetpack_Media_Summary::get( $post->ID );
  60. if ( 'gallery' !== $summary['type'] ) {
  61. return $tags;
  62. }
  63. if ( ! isset( $summary['images'] ) || ! is_array( $summary['images'] ) || empty( $summary['images'] ) ) {
  64. return $tags;
  65. }
  66. $images = array();
  67. $secures = array();
  68. foreach ( $summary['images'] as $i => $image ) {
  69. $images[] = $image['url'];
  70. $secures[] = $summary['secure']['images'][ $i ]['url'];
  71. }
  72. $tags['og:image'] = $images;
  73. $tags['og:image:secure_url'] = $secures;
  74. return $tags;
  75. }
  76. add_filter( 'jetpack_open_graph_tags', 'enhanced_og_gallery' );
  77. /**
  78. * Allows VideoPress, YouTube, and Vimeo videos to play inline on Facebook
  79. *
  80. * @param array $tags Array of Open Graph tags.
  81. */
  82. function enhanced_og_video( $tags ) {
  83. if ( ! is_singular() || post_password_required() ) {
  84. return $tags;
  85. }
  86. global $post;
  87. // Bail if we do not have info about the post.
  88. if ( ! $post instanceof WP_Post ) {
  89. return $tags;
  90. }
  91. // Always favor featured images.
  92. if ( enhanced_og_has_featured_image( $post->ID ) ) {
  93. return $tags;
  94. }
  95. $summary = Jetpack_Media_Summary::get( $post->ID );
  96. if ( 'video' !== $summary['type'] ) {
  97. if ( $summary['count']['video'] > 0 && $summary['count']['image'] < 1 ) {
  98. $tags['og:image'] = $summary['image'];
  99. $tags['og:image:secure_url'] = $summary['secure']['image'];
  100. }
  101. return $tags;
  102. }
  103. $tags['og:image'] = $summary['image'];
  104. $tags['og:image:secure_url'] = $summary['secure']['image'];
  105. // This should be html by default for youtube/vimeo, since we're linking to HTML pages.
  106. $tags['og:video:type'] = isset( $summary['video_type'] ) ? $summary['video_type'] : 'text/html';
  107. $video_url = $summary['video'];
  108. $secure_video_url = $summary['secure']['video'];
  109. if ( preg_match( '/((youtube|vimeo)\.com|youtu.be)/', $video_url ) ) {
  110. if ( strstr( $video_url, 'youtube' ) ) {
  111. $id = jetpack_get_youtube_id( $video_url );
  112. $video_url = 'http://www.youtube.com/embed/' . $id;
  113. $secure_video_url = 'https://www.youtube.com/embed/' . $id;
  114. } elseif ( strstr( $video_url, 'vimeo' ) ) {
  115. preg_match( '|vimeo\.com/(\d+)/?$|i', $video_url, $match );
  116. $id = (int) $match[1];
  117. $video_url = 'http://vimeo.com/moogaloop.swf?clip_id=' . $id;
  118. $secure_video_url = 'https://vimeo.com/moogaloop.swf?clip_id=' . $id;
  119. }
  120. }
  121. $tags['og:video'] = $video_url;
  122. $tags['og:video:secure_url'] = $secure_video_url;
  123. if ( empty( $post->post_title ) ) {
  124. /* translators: %s is the name of the site */
  125. $tags['og:title'] = sprintf( __( 'Video on %s', 'jetpack' ), get_option( 'blogname' ) );
  126. }
  127. return $tags;
  128. }
  129. add_filter( 'jetpack_open_graph_tags', 'enhanced_og_video' );
  130. /**
  131. * Check if a post has a suitable featured image.
  132. *
  133. * @param int $post_id The post ID to check.
  134. * @return bool True if the post has a suitable featured image, false otherwise.
  135. */
  136. function enhanced_og_has_featured_image( $post_id ) {
  137. return ! empty( Jetpack_PostImages::from_thumbnail( $post_id ) );
  138. }