functions.opengraph.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * Open Graph Tags
  4. *
  5. * Add Open Graph tags so that Facebook (and any other service that supports them)
  6. * can crawl the site better and we provide a better sharing experience.
  7. *
  8. * @link http://ogp.me/
  9. * @link http://developers.facebook.com/docs/opengraph/
  10. */
  11. add_action( 'wp_head', 'jetpack_og_tags' );
  12. function jetpack_og_tags() {
  13. /**
  14. * Allow Jetpack to output Open Graph Meta Tags.
  15. *
  16. * @module sharedaddy, publicize
  17. *
  18. * @since 2.0.0
  19. * @deprecated 2.0.3 Duplicative filter. Use `jetpack_enable_open_graph`.
  20. *
  21. * @param bool true Should Jetpack's Open Graph Meta Tags be enabled. Default to true.
  22. */
  23. if ( false === apply_filters( 'jetpack_enable_opengraph', true ) ) {
  24. _deprecated_function( 'jetpack_enable_opengraph', '2.0.3', 'jetpack_enable_open_graph' );
  25. return;
  26. }
  27. // Disable the widont filter on WP.com to avoid stray &nbsps
  28. $disable_widont = remove_filter( 'the_title', 'widont' );
  29. $og_output = "\n<!-- Jetpack Open Graph Tags -->\n";
  30. $tags = array();
  31. /**
  32. * Filter the minimum width of the images used in Jetpack Open Graph Meta Tags.
  33. *
  34. * @module sharedaddy, publicize
  35. *
  36. * @since 2.0.0
  37. *
  38. * @param int 200 Minimum image width used in Jetpack Open Graph Meta Tags.
  39. */
  40. $image_width = absint( apply_filters( 'jetpack_open_graph_image_width', 200 ) );
  41. /**
  42. * Filter the minimum height of the images used in Jetpack Open Graph Meta Tags.
  43. *
  44. * @module sharedaddy, publicize
  45. *
  46. * @since 2.0.0
  47. *
  48. * @param int 200 Minimum image height used in Jetpack Open Graph Meta Tags.
  49. */
  50. $image_height = absint( apply_filters( 'jetpack_open_graph_image_height', 200 ) );
  51. $description_length = 197;
  52. if ( is_home() || is_front_page() ) {
  53. $site_type = Jetpack_Options::get_option_and_ensure_autoload( 'open_graph_protocol_site_type', '' );
  54. $tags['og:type'] = ! empty( $site_type ) ? $site_type : 'website';
  55. $tags['og:title'] = get_bloginfo( 'name' );
  56. $tags['og:description'] = get_bloginfo( 'description' );
  57. $front_page_id = get_option( 'page_for_posts' );
  58. if ( 'page' == get_option( 'show_on_front' ) && $front_page_id && is_home() )
  59. $tags['og:url'] = get_permalink( $front_page_id );
  60. else
  61. $tags['og:url'] = home_url( '/' );
  62. // Associate a blog's root path with one or more Facebook accounts
  63. $facebook_admins = Jetpack_Options::get_option_and_ensure_autoload( 'facebook_admins', array() );
  64. if ( ! empty( $facebook_admins ) )
  65. $tags['fb:admins'] = $facebook_admins;
  66. } else if ( is_author() ) {
  67. $tags['og:type'] = 'profile';
  68. $author = get_queried_object();
  69. $tags['og:title'] = $author->display_name;
  70. if ( ! empty( $author->user_url ) ) {
  71. $tags['og:url'] = $author->user_url;
  72. } else {
  73. $tags['og:url'] = get_author_posts_url( $author->ID );
  74. }
  75. $tags['og:description'] = $author->description;
  76. $tags['profile:first_name'] = get_the_author_meta( 'first_name', $author->ID );
  77. $tags['profile:last_name'] = get_the_author_meta( 'last_name', $author->ID );
  78. } else if ( is_singular() ) {
  79. global $post;
  80. $data = $post; // so that we don't accidentally explode the global
  81. $tags['og:type'] = 'article';
  82. if ( empty( $data->post_title ) ) {
  83. $tags['og:title'] = ' ';
  84. } else {
  85. /** This filter is documented in core/src/wp-includes/post-template.php */
  86. $tags['og:title'] = wp_kses( apply_filters( 'the_title', $data->post_title, $data->ID ), array() );
  87. }
  88. $tags['og:url'] = get_permalink( $data->ID );
  89. if ( ! post_password_required() ) {
  90. if ( ! empty( $data->post_excerpt ) ) {
  91. $tags['og:description'] = preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $data->post_excerpt, array() ) ) );
  92. } else {
  93. $exploded_content_on_more_tag = explode( '<!--more-->', $data->post_content );
  94. $tags['og:description'] = wp_trim_words( preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $exploded_content_on_more_tag[0], array() ) ) ) );
  95. }
  96. }
  97. if ( empty( $tags['og:description'] ) ) {
  98. /**
  99. * Filter the fallback `og:description` used when no excerpt information is provided.
  100. *
  101. * @module sharedaddy, publicize
  102. *
  103. * @since 3.9.0
  104. *
  105. * @param string $var Fallback og:description. Default is translated `Visit the post for more'.
  106. * @param object $data Post object for the current post.
  107. */
  108. $tags['og:description'] = apply_filters( 'jetpack_open_graph_fallback_description', __( 'Visit the post for more.', 'jetpack' ), $data );
  109. } else {
  110. // Intentionally not using a filter to prevent pollution. @see https://github.com/Automattic/jetpack/pull/2899#issuecomment-151957382
  111. $tags['og:description'] = wp_kses( trim( convert_chars( wptexturize( $tags['og:description'] ) ) ), array() );
  112. }
  113. $tags['article:published_time'] = date( 'c', strtotime( $data->post_date_gmt ) );
  114. $tags['article:modified_time'] = date( 'c', strtotime( $data->post_modified_gmt ) );
  115. if ( post_type_supports( get_post_type( $data ), 'author' ) && isset( $data->post_author ) ) {
  116. $publicize_facebook_user = get_post_meta( $data->ID, '_publicize_facebook_user', true );
  117. if ( ! empty( $publicize_facebook_user ) ) {
  118. $tags['article:author'] = esc_url( $publicize_facebook_user );
  119. }
  120. }
  121. }
  122. /**
  123. * Allow plugins to inject additional template-specific Open Graph tags.
  124. *
  125. * @module sharedaddy, publicize
  126. *
  127. * @since 3.0.0
  128. *
  129. * @param array $tags Array of Open Graph Meta tags.
  130. * @param array $args Array of image size parameters.
  131. */
  132. $tags = apply_filters( 'jetpack_open_graph_base_tags', $tags, compact( 'image_width', 'image_height' ) );
  133. // Re-enable widont if we had disabled it
  134. if ( $disable_widont )
  135. add_filter( 'the_title', 'widont' );
  136. /**
  137. * Do not return any Open Graph Meta tags if we don't have any info about a post.
  138. *
  139. * @module sharedaddy, publicize
  140. *
  141. * @since 3.0.0
  142. *
  143. * @param bool true Do not return any Open Graph Meta tags if we don't have any info about a post.
  144. */
  145. if ( empty( $tags ) && apply_filters( 'jetpack_open_graph_return_if_empty', true ) )
  146. return;
  147. $tags['og:site_name'] = get_bloginfo( 'name' );
  148. // Get image info and build tags
  149. if ( ! post_password_required() ) {
  150. $image_info = jetpack_og_get_image( $image_width, $image_height );
  151. $tags['og:image'] = $image_info['src'];
  152. if ( ! empty( $image_info['width'] ) ) {
  153. $tags['og:image:width'] = (int) $image_info['width'];
  154. }
  155. if ( ! empty( $image_info['height'] ) ) {
  156. $tags['og:image:height'] = (int) $image_info['height'];
  157. }
  158. }
  159. // Facebook whines if you give it an empty title
  160. if ( empty( $tags['og:title'] ) )
  161. $tags['og:title'] = __( '(no title)', 'jetpack' );
  162. // Shorten the description if it's too long
  163. if ( isset( $tags['og:description'] ) ) {
  164. $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '…' : $tags['og:description'];
  165. }
  166. // Try to add OG locale tag if the WP->FB data mapping exists
  167. if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
  168. require_once JETPACK__GLOTPRESS_LOCALES_PATH;
  169. $_locale = get_locale();
  170. // We have to account for w.org vs WP.com locale divergence
  171. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  172. $gp_locale = GP_Locales::by_field( 'slug', $_locale );
  173. } else {
  174. $gp_locale = GP_Locales::by_field( 'wp_locale', $_locale );
  175. }
  176. }
  177. if ( isset( $gp_locale->facebook_locale ) && ! empty( $gp_locale->facebook_locale ) ) {
  178. $tags['og:locale'] = $gp_locale->facebook_locale;
  179. }
  180. /**
  181. * Allow the addition of additional Open Graph Meta tags, or modify the existing tags.
  182. *
  183. * @module sharedaddy, publicize
  184. *
  185. * @since 2.0.0
  186. *
  187. * @param array $tags Array of Open Graph Meta tags.
  188. * @param array $args Array of image size parameters.
  189. */
  190. $tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) );
  191. // secure_urls need to go right after each og:image to work properly so we will abstract them here
  192. $secure = $tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url'];
  193. unset( $tags['og:image:secure_url'] );
  194. $secure_image_num = 0;
  195. foreach ( (array) $tags as $tag_property => $tag_content ) {
  196. // to accommodate multiple images
  197. $tag_content = (array) $tag_content;
  198. $tag_content = array_unique( $tag_content );
  199. foreach ( $tag_content as $tag_content_single ) {
  200. if ( empty( $tag_content_single ) )
  201. continue; // Don't ever output empty tags
  202. $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
  203. /**
  204. * Filter the HTML Output of each Open Graph Meta tag.
  205. *
  206. * @module sharedaddy, publicize
  207. *
  208. * @since 2.0.0
  209. *
  210. * @param string $og_tag HTML HTML Output of each Open Graph Meta tag.
  211. */
  212. $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
  213. $og_output .= "\n";
  214. if ( 'og:image' == $tag_property ) {
  215. if ( is_array( $secure ) && !empty( $secure[$secure_image_num] ) ) {
  216. $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) );
  217. /** This filter is documented in functions.opengraph.php */
  218. $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
  219. $og_output .= "\n";
  220. } else if ( !is_array( $secure ) && !empty( $secure ) ) {
  221. $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) );
  222. /** This filter is documented in functions.opengraph.php */
  223. $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
  224. $og_output .= "\n";
  225. }
  226. $secure_image_num++;
  227. }
  228. }
  229. }
  230. $og_output .= "\n<!-- End Jetpack Open Graph Tags -->\n";
  231. echo $og_output;
  232. }
  233. function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 200x200
  234. $image = array();
  235. if ( is_singular() && ! is_home() ) {
  236. // Grab obvious image if post is an attachment page for an image
  237. if ( is_attachment( get_the_ID() ) && 'image' == substr( get_post_mime_type(), 0, 5 ) ) {
  238. $image['src'] = wp_get_attachment_url( get_the_ID() );
  239. }
  240. // Attempt to find something good for this post using our generalized PostImages code
  241. if ( empty( $image ) && class_exists( 'Jetpack_PostImages' ) ) {
  242. $post_images = Jetpack_PostImages::get_images( get_the_ID(), array( 'width' => $width, 'height' => $height ) );
  243. if ( $post_images && ! is_wp_error( $post_images ) ) {
  244. foreach ( (array) $post_images as $post_image ) {
  245. $image['src'] = $post_image['src'];
  246. if ( isset( $post_image['src_width'], $post_image['src_height'] ) ) {
  247. $image['width'] = $post_image['src_width'];
  248. $image['height'] = $post_image['src_height'];
  249. }
  250. }
  251. }
  252. }
  253. } elseif ( is_author() ) {
  254. $author = get_queried_object();
  255. $image['src'] = get_avatar_url( $author->user_email, array(
  256. 'size' => $width,
  257. ) );
  258. }
  259. // First fall back, blavatar.
  260. if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) {
  261. $blavatar_domain = blavatar_domain( site_url() );
  262. if ( blavatar_exists( $blavatar_domain ) ) {
  263. $image['src'] = blavatar_url( $blavatar_domain, 'img', $width, false, true );
  264. $image['width'] = $width;
  265. $image['height'] = $height;
  266. }
  267. }
  268. // Second fall back, Site Logo.
  269. if ( empty( $image ) && ( function_exists( 'jetpack_has_site_logo' ) && jetpack_has_site_logo() ) ) {
  270. $image_id = jetpack_get_site_logo( 'id' );
  271. $logo = wp_get_attachment_image_src( $image_id, 'full' );
  272. if (
  273. isset( $logo[0], $logo[1], $logo[2] )
  274. && ( _jetpack_og_get_image_validate_size( $logo[1], $logo[2], $width, $height ) )
  275. ) {
  276. $image['src'] = $logo[0];
  277. $image['width'] = $logo[1];
  278. $image['height'] = $logo[2];
  279. }
  280. }
  281. // Third fall back, Core Site Icon, if valid in size. Added in WP 4.3.
  282. if ( empty( $image ) && ( function_exists( 'has_site_icon' ) && has_site_icon() ) ) {
  283. $image_id = get_option( 'site_icon' );
  284. $icon = wp_get_attachment_image_src( $image_id, 'full' );
  285. if (
  286. isset( $icon[0], $icon[1], $icon[2] )
  287. && ( _jetpack_og_get_image_validate_size( $icon[1], $icon[2], $width, $height ) )
  288. ) {
  289. $image['src'] = $icon[0];
  290. $image['width'] = $icon[1];
  291. $image['height'] = $icon[2];
  292. }
  293. }
  294. // Final fall back, blank image.
  295. if ( empty( $image ) ) {
  296. /**
  297. * Filter the default Open Graph Image tag, used when no Image can be found in a post.
  298. *
  299. * @since 3.0.0
  300. *
  301. * @param string $str Default Image URL.
  302. */
  303. $image['src'] = apply_filters( 'jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg' );
  304. }
  305. return $image;
  306. }
  307. /**
  308. * Validate the width and height against required width and height
  309. *
  310. * @param int $width Width of the image.
  311. * @param int $height Height of the image.
  312. * @param int $req_width Required width to pass validation.
  313. * @param int $req_height Required height to pass validation.
  314. *
  315. * @return bool - True if the image passed the required size validation
  316. */
  317. function _jetpack_og_get_image_validate_size( $width, $height, $req_width, $req_height ) {
  318. if ( ! $width || ! $height ) {
  319. return false;
  320. }
  321. $valid_width = ( $width >= $req_width );
  322. $valid_height = ( $height >= $req_height );
  323. $is_image_acceptable = $valid_width && $valid_height;
  324. return $is_image_acceptable;
  325. }
  326. /**
  327. * Gets a gravatar URL of the specified size.
  328. *
  329. * @param string $email E-mail address to get gravatar for.
  330. * @param int $width Size of returned gravatar.
  331. * @return array|bool|mixed|string
  332. */
  333. function jetpack_og_get_image_gravatar( $email, $width ) {
  334. return get_avatar_url( $email, array(
  335. 'size' => $width,
  336. ) );
  337. }