Brak opisu

functions.opengraph.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 https://ogp.me/
  9. * @link https://developers.facebook.com/docs/opengraph/
  10. *
  11. * @package automattic/jetpack
  12. */
  13. add_action( 'wp_head', 'jetpack_og_tags' );
  14. add_action( 'web_stories_story_head', 'jetpack_og_tags' );
  15. /**
  16. * Outputs Open Graph tags generated by Jetpack.
  17. */
  18. function jetpack_og_tags() {
  19. global $post;
  20. $data = $post; // so that we don't accidentally explode the global.
  21. /**
  22. * Allow Jetpack to output Open Graph Meta Tags.
  23. *
  24. * @module sharedaddy, publicize
  25. *
  26. * @since 2.0.0
  27. * @deprecated 2.0.3 Duplicative filter. Use `jetpack_enable_open_graph`.
  28. *
  29. * @param bool true Should Jetpack's Open Graph Meta Tags be enabled. Default to true.
  30. */
  31. if ( false === apply_filters( 'jetpack_enable_opengraph', true ) ) {
  32. _deprecated_function( 'jetpack_enable_opengraph', '2.0.3', 'jetpack_enable_open_graph' );
  33. return;
  34. }
  35. $is_amp_response = ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() );
  36. // Disable the widont filter on WP.com to avoid stray &nbsps.
  37. $disable_widont = remove_filter( 'the_title', 'widont' );
  38. $og_output = "\n";
  39. if ( ! $is_amp_response ) { // Because AMP optimizes the order or the nodes in the head.
  40. $og_output .= "<!-- Jetpack Open Graph Tags -->\n";
  41. }
  42. $tags = array();
  43. /**
  44. * Filter the minimum width of the images used in Jetpack Open Graph Meta Tags.
  45. *
  46. * @module sharedaddy, publicize
  47. *
  48. * @since 2.0.0
  49. *
  50. * @param int 200 Minimum image width used in Jetpack Open Graph Meta Tags.
  51. */
  52. $image_width = absint( apply_filters( 'jetpack_open_graph_image_width', 200 ) );
  53. /**
  54. * Filter the minimum height of the images used in Jetpack Open Graph Meta Tags.
  55. *
  56. * @module sharedaddy, publicize
  57. *
  58. * @since 2.0.0
  59. *
  60. * @param int 200 Minimum image height used in Jetpack Open Graph Meta Tags.
  61. */
  62. $image_height = absint( apply_filters( 'jetpack_open_graph_image_height', 200 ) );
  63. $description_length = 197;
  64. if ( is_home() || is_front_page() ) {
  65. $site_type = Jetpack_Options::get_option_and_ensure_autoload( 'open_graph_protocol_site_type', '' );
  66. $tags['og:type'] = ! empty( $site_type ) ? $site_type : 'website';
  67. $tags['og:title'] = get_bloginfo( 'name' );
  68. $tags['og:description'] = get_bloginfo( 'description' );
  69. $front_page_id = get_option( 'page_for_posts' );
  70. if ( 'page' === get_option( 'show_on_front' ) && $front_page_id && is_home() ) {
  71. $tags['og:url'] = get_permalink( $front_page_id );
  72. } else {
  73. $tags['og:url'] = home_url( '/' );
  74. }
  75. // Associate a blog's root path with one or more Facebook accounts.
  76. $facebook_admins = Jetpack_Options::get_option_and_ensure_autoload( 'facebook_admins', array() );
  77. if ( ! empty( $facebook_admins ) ) {
  78. $tags['fb:admins'] = $facebook_admins;
  79. }
  80. } elseif ( is_author() ) {
  81. $tags['og:type'] = 'profile';
  82. $author = get_queried_object();
  83. if ( is_a( $author, 'WP_User' ) ) {
  84. $tags['og:title'] = $author->display_name;
  85. if ( ! empty( $author->user_url ) ) {
  86. $tags['og:url'] = $author->user_url;
  87. } else {
  88. $tags['og:url'] = get_author_posts_url( $author->ID );
  89. }
  90. $tags['og:description'] = $author->description;
  91. $tags['profile:first_name'] = get_the_author_meta( 'first_name', $author->ID );
  92. $tags['profile:last_name'] = get_the_author_meta( 'last_name', $author->ID );
  93. }
  94. } elseif ( is_archive() ) {
  95. $tags['og:type'] = 'website';
  96. $tags['og:title'] = wp_get_document_title();
  97. $archive = get_queried_object();
  98. if ( ! empty( $archive ) ) {
  99. if ( is_category() || is_tag() || is_tax() ) {
  100. $tags['og:url'] = get_term_link( $archive->term_id, $archive->taxonomy );
  101. $tags['og:description'] = $archive->description;
  102. } elseif ( is_post_type_archive() ) {
  103. $tags['og:url'] = get_post_type_archive_link( $archive->name );
  104. $tags['og:description'] = $archive->description;
  105. }
  106. }
  107. } elseif ( is_singular() && is_a( $data, 'WP_Post' ) ) {
  108. $tags['og:type'] = 'article';
  109. if ( empty( $data->post_title ) ) {
  110. $tags['og:title'] = ' ';
  111. } else {
  112. /** This filter is documented in core/src/wp-includes/post-template.php */
  113. $tags['og:title'] = wp_kses( apply_filters( 'the_title', $data->post_title, $data->ID ), array() );
  114. }
  115. $tags['og:url'] = get_permalink( $data->ID );
  116. if ( ! post_password_required() ) {
  117. /*
  118. * If the post author set an excerpt, use that.
  119. * Otherwise, pick the post content that comes before the More tag if there is one.
  120. * Do not use the post content if it contains premium content.
  121. */
  122. if ( ! empty( $data->post_excerpt ) ) {
  123. $tags['og:description'] = jetpack_og_get_description( $data->post_excerpt );
  124. } elseif ( ! has_block( 'premium-content/container', $data->post_content ) ) {
  125. $excerpt = explode( '<!--more-->', $data->post_content )[0];
  126. $tags['og:description'] = jetpack_og_get_description( $excerpt );
  127. }
  128. }
  129. $tags['article:published_time'] = gmdate( 'c', strtotime( $data->post_date_gmt ) );
  130. $tags['article:modified_time'] = gmdate( 'c', strtotime( $data->post_modified_gmt ) );
  131. if ( post_type_supports( get_post_type( $data ), 'author' ) && isset( $data->post_author ) ) {
  132. $publicize_facebook_user = get_post_meta( $data->ID, '_publicize_facebook_user', true );
  133. if ( ! empty( $publicize_facebook_user ) ) {
  134. $tags['article:author'] = esc_url( $publicize_facebook_user );
  135. }
  136. }
  137. } elseif ( is_search() ) {
  138. if ( '' !== get_query_var( 's', '' ) ) {
  139. $tags['og:title'] = wp_get_document_title();
  140. }
  141. }
  142. /**
  143. * Allow plugins to inject additional template-specific Open Graph tags.
  144. *
  145. * @module sharedaddy, publicize
  146. *
  147. * @since 3.0.0
  148. *
  149. * @param array $tags Array of Open Graph Meta tags.
  150. * @param array $args Array of image size parameters.
  151. */
  152. $tags = apply_filters( 'jetpack_open_graph_base_tags', $tags, compact( 'image_width', 'image_height' ) );
  153. // Re-enable widont if we had disabled it.
  154. if ( $disable_widont ) {
  155. add_filter( 'the_title', 'widont' );
  156. }
  157. /**
  158. * Do not return any Open Graph Meta tags if we don't have any info about a post.
  159. *
  160. * @module sharedaddy, publicize
  161. *
  162. * @since 3.0.0
  163. *
  164. * @param bool true Do not return any Open Graph Meta tags if we don't have any info about a post.
  165. */
  166. if ( empty( $tags ) && apply_filters( 'jetpack_open_graph_return_if_empty', true ) ) {
  167. return;
  168. }
  169. $tags['og:site_name'] = get_bloginfo( 'name' );
  170. // Get image info and build tags.
  171. if ( ! post_password_required() ) {
  172. $image_info = jetpack_og_get_image( $image_width, $image_height );
  173. $tags['og:image'] = $image_info['src'];
  174. if ( ! empty( $image_info['width'] ) ) {
  175. $tags['og:image:width'] = (int) $image_info['width'];
  176. }
  177. if ( ! empty( $image_info['height'] ) ) {
  178. $tags['og:image:height'] = (int) $image_info['height'];
  179. }
  180. if ( ! empty( $image_info['alt_text'] ) ) {
  181. $tags['og:image:alt'] = esc_attr( $image_info['alt_text'] );
  182. }
  183. }
  184. // Facebook whines if you give it an empty title.
  185. if ( empty( $tags['og:title'] ) ) {
  186. $tags['og:title'] = __( '(no title)', 'jetpack' );
  187. }
  188. // Shorten the description if it's too long.
  189. if ( isset( $tags['og:description'] ) ) {
  190. $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '…' : $tags['og:description'];
  191. }
  192. // Try to add OG locale tag if the WP->FB data mapping exists.
  193. if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
  194. require_once JETPACK__GLOTPRESS_LOCALES_PATH;
  195. $_locale = get_locale();
  196. // We have to account for w.org vs WP.com locale divergence.
  197. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  198. $gp_locale = GP_Locales::by_field( 'slug', $_locale );
  199. } else {
  200. $gp_locale = GP_Locales::by_field( 'wp_locale', $_locale );
  201. }
  202. }
  203. if ( isset( $gp_locale->facebook_locale ) && ! empty( $gp_locale->facebook_locale ) ) {
  204. $tags['og:locale'] = $gp_locale->facebook_locale;
  205. }
  206. /**
  207. * Allow the addition of additional Open Graph Meta tags, or modify the existing tags.
  208. *
  209. * @module sharedaddy, publicize
  210. *
  211. * @since 2.0.0
  212. *
  213. * @param array $tags Array of Open Graph Meta tags.
  214. * @param array $args Array of image size parameters.
  215. */
  216. $tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) );
  217. // secure_urls need to go right after each og:image to work properly so we will abstract them here.
  218. $tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url'];
  219. $secure = $tags['og:image:secure_url'];
  220. unset( $tags['og:image:secure_url'] );
  221. $secure_image_num = 0;
  222. foreach ( (array) $tags as $tag_property => $tag_content ) {
  223. // to accommodate multiple images.
  224. $tag_content = (array) $tag_content;
  225. $tag_content = array_unique( $tag_content );
  226. foreach ( $tag_content as $tag_content_single ) {
  227. if ( empty( $tag_content_single ) ) {
  228. continue; // Don't ever output empty tags.
  229. }
  230. switch ( $tag_property ) {
  231. case 'og:url':
  232. case 'og:image':
  233. case 'og:image:url':
  234. case 'og:image:secure_url':
  235. case 'og:audio':
  236. case 'og:audio:url':
  237. case 'og:audio:secure_url':
  238. case 'og:video':
  239. case 'og:video:url':
  240. case 'og:video:secure_url':
  241. $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_url( $tag_content_single ) );
  242. break;
  243. default:
  244. $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
  245. }
  246. /**
  247. * Filter the HTML Output of each Open Graph Meta tag.
  248. *
  249. * @module sharedaddy, publicize
  250. *
  251. * @since 2.0.0
  252. *
  253. * @param string $og_tag HTML HTML Output of each Open Graph Meta tag.
  254. */
  255. $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
  256. $og_output .= "\n";
  257. if ( 'og:image' === $tag_property ) {
  258. if ( is_array( $secure ) && ! empty( $secure[ $secure_image_num ] ) ) {
  259. $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) );
  260. /** This filter is documented in functions.opengraph.php */
  261. $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
  262. $og_output .= "\n";
  263. } elseif ( ! is_array( $secure ) && ! empty( $secure ) ) {
  264. $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) );
  265. /** This filter is documented in functions.opengraph.php */
  266. $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
  267. $og_output .= "\n";
  268. }
  269. $secure_image_num++;
  270. }
  271. }
  272. }
  273. if ( ! $is_amp_response ) { // Because AMP optimizes the order or the nodes in the head.
  274. $og_output .= "\n<!-- End Jetpack Open Graph Tags -->";
  275. }
  276. $og_output .= "\n";
  277. // This is trusted output or added by a filter.
  278. echo $og_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  279. }
  280. /**
  281. * Returns an image used in social shares.
  282. *
  283. * @since 2.0.0
  284. *
  285. * @param int $width Minimum width for the image. Default is 200 based on Facebook's requirement.
  286. * @param int $height Minimum height for the image. Default is 200 based on Facebook's requirement.
  287. * @param null $deprecated Deprecated.
  288. *
  289. * @return array The source ('src'), 'width', and 'height' of the image.
  290. */
  291. function jetpack_og_get_image( $width = 200, $height = 200, $deprecated = null ) {
  292. if ( ! empty( $deprecated ) ) {
  293. _deprecated_argument( __FUNCTION__, '6.6.0' );
  294. }
  295. $image = array();
  296. if ( is_singular() && ! is_home() ) {
  297. // Grab obvious image if post is an attachment page for an image.
  298. if ( is_attachment( get_the_ID() ) && 'image' === substr( get_post_mime_type(), 0, 5 ) ) {
  299. $image['src'] = wp_get_attachment_url( get_the_ID() );
  300. }
  301. // Attempt to find something good for this post using our generalized PostImages code.
  302. if ( empty( $image ) && class_exists( 'Jetpack_PostImages' ) ) {
  303. $post_image = Jetpack_PostImages::get_image(
  304. get_the_ID(),
  305. array(
  306. 'width' => $width,
  307. 'height' => $height,
  308. )
  309. );
  310. if ( ! empty( $post_image ) && is_array( $post_image ) ) {
  311. $image['src'] = $post_image['src'];
  312. if ( isset( $post_image['src_width'], $post_image['src_height'] ) ) {
  313. $image['width'] = $post_image['src_width'];
  314. $image['height'] = $post_image['src_height'];
  315. }
  316. if ( ! empty( $post_image['alt_text'] ) ) {
  317. $image['alt_text'] = $post_image['alt_text'];
  318. }
  319. }
  320. }
  321. } elseif ( is_author() ) {
  322. $author = get_queried_object();
  323. if ( is_a( $author, 'WP_User' ) ) {
  324. $image['src'] = get_avatar_url(
  325. $author->user_email,
  326. array(
  327. 'size' => $width,
  328. )
  329. );
  330. }
  331. }
  332. // First fall back, blavatar.
  333. if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) {
  334. $blavatar_domain = blavatar_domain( site_url() );
  335. if ( blavatar_exists( $blavatar_domain ) ) {
  336. $image['src'] = blavatar_url( $blavatar_domain, 'img', $width, false, true );
  337. $image['width'] = $width;
  338. $image['height'] = $height;
  339. }
  340. }
  341. // Second fall back, Site Logo.
  342. if ( empty( $image ) && ( function_exists( 'jetpack_has_site_logo' ) && jetpack_has_site_logo() ) ) {
  343. $image_id = jetpack_get_site_logo( 'id' );
  344. $logo = wp_get_attachment_image_src( $image_id, 'full' );
  345. if (
  346. isset( $logo[0], $logo[1], $logo[2] )
  347. && ( _jetpack_og_get_image_validate_size( $logo[1], $logo[2], $width, $height ) )
  348. ) {
  349. $image['src'] = $logo[0];
  350. $image['width'] = $logo[1];
  351. $image['height'] = $logo[2];
  352. }
  353. }
  354. // Third fall back, Core Site Icon, if valid in size.
  355. if ( empty( $image ) && has_site_icon() ) {
  356. $image_id = get_option( 'site_icon' );
  357. $icon = wp_get_attachment_image_src( $image_id, 'full' );
  358. if (
  359. isset( $icon[0], $icon[1], $icon[2] )
  360. && ( _jetpack_og_get_image_validate_size( $icon[1], $icon[2], $width, $height ) )
  361. ) {
  362. $image['src'] = $icon[0];
  363. $image['width'] = $icon[1];
  364. $image['height'] = $icon[2];
  365. }
  366. }
  367. // Final fall back, blank image.
  368. if ( empty( $image ) ) {
  369. /**
  370. * Filter the default Open Graph Image tag, used when no Image can be found in a post.
  371. *
  372. * @since 3.0.0
  373. *
  374. * @param string $str Default Image URL.
  375. */
  376. $image['src'] = apply_filters( 'jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg' );
  377. }
  378. return $image;
  379. }
  380. /**
  381. * Validate the width and height against required width and height
  382. *
  383. * @param int $width Width of the image.
  384. * @param int $height Height of the image.
  385. * @param int $req_width Required width to pass validation.
  386. * @param int $req_height Required height to pass validation.
  387. *
  388. * @return bool - True if the image passed the required size validation
  389. */
  390. function _jetpack_og_get_image_validate_size( $width, $height, $req_width, $req_height ) {
  391. if ( ! $width || ! $height ) {
  392. return false;
  393. }
  394. $valid_width = ( $width >= $req_width );
  395. $valid_height = ( $height >= $req_height );
  396. $is_image_acceptable = $valid_width && $valid_height;
  397. return $is_image_acceptable;
  398. }
  399. /**
  400. * Gets a gravatar URL of the specified size.
  401. *
  402. * @param string $email E-mail address to get gravatar for.
  403. * @param int $width Size of returned gravatar.
  404. * @return array|bool|mixed|string
  405. */
  406. function jetpack_og_get_image_gravatar( $email, $width ) {
  407. return get_avatar_url(
  408. $email,
  409. array(
  410. 'size' => $width,
  411. )
  412. );
  413. }
  414. /**
  415. * Clean up text meant to be used as Description Open Graph tag.
  416. *
  417. * There should be:
  418. * - no links
  419. * - no shortcodes
  420. * - no html tags or their contents
  421. * - not too many words.
  422. *
  423. * @param string $description Text coming from WordPress (autogenerated or manually generated by author).
  424. * @param WP_Post|null $data Information about our post.
  425. *
  426. * @return string $description Cleaned up description string.
  427. */
  428. function jetpack_og_get_description( $description = '', $data = null ) {
  429. // Remove tags such as <style or <script.
  430. $description = wp_strip_all_tags( $description );
  431. /*
  432. * Clean up any plain text entities left into formatted entities.
  433. * Intentionally not using a filter to prevent pollution.
  434. * @see https://github.com/Automattic/jetpack/pull/2899#issuecomment-151957382
  435. */
  436. $description = wp_kses(
  437. trim(
  438. convert_chars(
  439. wptexturize( $description )
  440. )
  441. ),
  442. array()
  443. );
  444. // Remove shortcodes.
  445. $description = strip_shortcodes( $description );
  446. // Remove links.
  447. $description = preg_replace(
  448. '@https?://[\S]+@',
  449. '',
  450. $description
  451. );
  452. /*
  453. * Limit things to a small text blurb.
  454. * There isn't a hard limit set by Facebook, so let's rely on WP's own limit.
  455. * (55 words or the localized equivalent).
  456. * This limit can be customized with the wp_trim_words filter.
  457. */
  458. $description = wp_trim_words( $description );
  459. // Let's set a default if we have no text by now.
  460. if ( empty( $description ) ) {
  461. /**
  462. * Filter the fallback `og:description` used when no excerpt information is provided.
  463. *
  464. * @module sharedaddy, publicize
  465. *
  466. * @since 3.9.0
  467. *
  468. * @param string $var Fallback og:description. Default is translated `Visit the post for more'.
  469. * @param object $data Post object for the current post.
  470. */
  471. $description = apply_filters(
  472. 'jetpack_open_graph_fallback_description',
  473. __( 'Visit the post for more.', 'jetpack' ),
  474. $data
  475. );
  476. }
  477. return $description;
  478. }