PluginProbe
Open Graph / 2.0.0
Open Graph v2.0.0
trunk 1.1 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.12.1 1.12.2 1.2 1.3 1.4 1.5 1.5.1 1.6 1.7.0 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 2.0.0 2.0.1 2.0.2
opengraph / opengraph.php

opengraph.php in Open Graph 2.0.0, at opengraph.php

962 lines 24.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Open Graph
4 * Plugin URI: https://wordpress.org/plugins/opengraph
5 * Description: Adds Open Graph metadata to your pages
6 * Author: Will Norris & Matthias Pfefferle
7 * Author URI: https://github.com/pfefferle/wordpress-opengraph
8 * Version: 2.0.0
9 * License: Apache License, Version 2.0
10 * License URI: http://www.apache.org/licenses/LICENSE-2.0.html
11 * Text Domain: opengraph
12 *
13 * @package opengraph
14 */
15
16 // If you have the opengraph plugin running alongside jetpack, we assume you'd
17 // rather use our opengraph support, so disable jetpack's opengraph functionality.
18 add_filter( 'jetpack_enable_opengraph', '__return_false' );
19 add_filter( 'jetpack_enable_open_graph', '__return_false' );
20
21
22 // Disable strict mode by default.
23 defined( 'OPENGRAPH_STRICT_MODE' ) || define( 'OPENGRAPH_STRICT_MODE', false );
24
25
26 /**
27 * Add Open Graph XML prefix to <html> element.
28 *
29 * @uses apply_filters calls 'opengraph_prefixes' filter on RDFa prefix array
30 *
31 * @param string $output The current list of prefixes.
32 *
33 * @return string The updated list of prefixes.
34 */
35 function opengraph_add_prefix( $output ) {
36 $prefixes = array(
37 'og' => 'http://ogp.me/ns#',
38 );
39 $prefixes = apply_filters( 'opengraph_prefixes', $prefixes );
40
41 $prefix_str = '';
42 foreach ( $prefixes as $k => $v ) {
43 $prefix_str .= $k . ': ' . $v . ' ';
44 }
45 $prefix_str = trim( $prefix_str );
46
47 if ( preg_match( '/(prefix\s*=\s*[\"|\'])/i', $output ) ) {
48 $output = preg_replace( '/(prefix\s*=\s*[\"|\'])/i', '${1}' . $prefix_str, $output );
49 } else {
50 $output .= ' prefix="' . esc_attr( $prefix_str ) . '"';
51 }
52
53 return $output;
54 }
55 add_filter( 'language_attributes', 'opengraph_add_prefix' );
56
57
58 /**
59 * Add additional prefix namespaces that are supported by the opengraph plugin.
60 *
61 * @param array $prefixes The current list of prefixes.
62 *
63 * @return array The updated list of prefixes.
64 */
65 function opengraph_additional_prefixes( $prefixes ) {
66 if ( is_author() ) {
67 $prefixes['profile'] = 'http://ogp.me/ns/profile#';
68 }
69 if ( is_singular() ) {
70 $prefixes['article'] = 'http://ogp.me/ns/article#';
71 }
72
73 return $prefixes;
74 }
75
76
77 /**
78 * Get the Open Graph metadata for the current page.
79 *
80 * @uses apply_filters() Calls 'opengraph_{$name}' for each property name
81 * @uses apply_filters() Calls 'twitter_{$name}' for each property name
82 * @uses apply_filters() Calls 'opengraph_metadata' before returning metadata array
83 */
84 function opengraph_metadata() {
85 $metadata = array();
86
87 // Default properties defined at http://ogp.me/.
88 $properties = array(
89 // Required properties.
90 'title' => '',
91 'type' => '',
92 'image' => array(),
93 'url' => '',
94
95 // Optional properties.
96 'audio' => array(),
97 'description' => '',
98 'determiner' => '',
99 'locale' => '',
100 'site_name' => '',
101 'video' => array(),
102 );
103
104 foreach ( $properties as $property => $default ) {
105 $filter = 'opengraph_' . $property;
106
107 /**
108 * Filter the Open Graph metadata.
109 *
110 * @param array $default The default value.
111 */
112 $metadata[ "og:$property" ] = apply_filters( $filter, $default );
113 }
114
115 $twitter_properties = array(
116 'card' => '',
117 'creator' => '',
118 );
119
120 foreach ( $twitter_properties as $property => $default ) {
121 $filter = 'twitter_' . $property;
122
123 /**
124 * Filter the Twitter Card metadata.
125 *
126 * @param array $default The default value.
127 */
128 $metadata[ "twitter:$property" ] = apply_filters( $filter, $default );
129 }
130
131 $fediverse_properties = array(
132 'creator' => array(),
133 );
134
135 foreach ( $fediverse_properties as $property => $default ) {
136 $filter = 'fediverse_' . $property;
137
138 /**
139 * Filter the Fediverse metadata.
140 *
141 * @param array $default The default value.
142 */
143 $metadata[ "fediverse:$property" ] = apply_filters( $filter, $default );
144 }
145
146 /**
147 * Filter the Open Graph metadata.
148 *
149 * @param array $metadata The metadata array.
150 */
151 return apply_filters( 'opengraph_metadata', $metadata );
152 }
153
154
155 /**
156 * Register filters for default Open Graph metadata.
157 */
158 function opengraph_default_metadata() {
159 // Core metadata attributes.
160 add_filter( 'opengraph_title', 'opengraph_default_title', 5 );
161 add_filter( 'opengraph_type', 'opengraph_default_type', 5 );
162 add_filter( 'opengraph_url', 'opengraph_default_url', 5 );
163
164 // Image metadata attributes with fallbacks.
165 add_filter( 'opengraph_image', 'opengraph_default_image', 5 );
166 add_filter( 'opengraph_image', 'opengraph_block_image', 15 );
167 add_filter( 'opengraph_image', 'opengraph_parsed_image', 25 );
168 add_filter( 'opengraph_image', 'opengraph_attached_image', 25 );
169 add_filter( 'opengraph_image', 'opengraph_fallback_image', 35 );
170
171 add_filter( 'opengraph_description', 'opengraph_default_description', 5 );
172 add_filter( 'opengraph_locale', 'opengraph_default_locale', 5 );
173 add_filter( 'opengraph_site_name', 'opengraph_default_sitename', 5 );
174 add_filter( 'opengraph_audio', 'opengraph_default_audio', 5 );
175 add_filter( 'opengraph_video', 'opengraph_default_video', 5 );
176
177 // Additional prefixes.
178 add_filter( 'opengraph_prefixes', 'opengraph_additional_prefixes' );
179
180 // Additional profile metadata.
181 add_filter( 'opengraph_metadata', 'opengraph_profile_metadata' );
182
183 // Additional article metadata.
184 add_filter( 'opengraph_metadata', 'opengraph_article_metadata' );
185
186 // twitter card metadata.
187 add_filter( 'twitter_card', 'twitter_default_card', 5 );
188 add_filter( 'twitter_creator', 'twitter_default_creator', 5 );
189
190 // fediverse creator metadata.
191 add_filter( 'fediverse_creator', 'fediverse_default_creator', 5 );
192 }
193 add_action( 'wp', 'opengraph_default_metadata' );
194
195
196 /**
197 * Default title property, using the page title.
198 *
199 * @param string $title The current title.
200 *
201 * @return string The title.
202 */
203 function opengraph_default_title( $title ) {
204 if ( $title ) {
205 return $title;
206 }
207
208 // Set default title, because twitter is requiring one.
209 $title = __( 'Untitled', 'opengraph' );
210
211 if ( is_home() || is_front_page() ) {
212 $title = get_bloginfo( 'name' );
213 } elseif ( is_singular() ) {
214 $title = get_the_title( get_queried_object_id() );
215 // Fall back to description.
216 if ( empty( $title ) ) {
217 $title = opengraph_default_description( null, 5 );
218 }
219 } elseif ( is_author() ) {
220 $author = get_queried_object();
221 $title = $author->display_name;
222 } elseif ( is_category() && single_cat_title( '', false ) ) {
223 $title = single_cat_title( '', false );
224 } elseif ( is_tag() && single_tag_title( '', false ) ) {
225 $title = single_tag_title( '', false );
226 } elseif ( is_archive() && get_post_format() ) {
227 $title = get_post_format_string( get_post_format() );
228 } elseif (
229 is_archive() &&
230 function_exists( 'get_the_archive_title' ) &&
231 get_the_archive_title()
232 ) { // New in version 4.1 to get all other archive titles.
233 $title = get_the_archive_title();
234 }
235
236 return wp_strip_all_tags( $title );
237 }
238
239
240 /**
241 * Default type property.
242 *
243 * @param string $type The current type.
244 *
245 * @return string The type.
246 */
247 function opengraph_default_type( $type = '' ) {
248 if ( empty( $type ) ) {
249 if ( is_singular( array( 'post', 'page' ) ) ) {
250 $type = 'article';
251 } elseif ( is_author() ) {
252 $type = 'profile';
253 } else {
254 $type = 'website';
255 }
256 }
257
258 return $type;
259 }
260
261
262 /**
263 * Default image property, using the post-thumbnail and any attached images.
264 *
265 * @param array $image The current list of images.
266 *
267 * @return array The list of images.
268 */
269 function opengraph_default_image( $image = array() ) {
270 // Show avatar on profile pages.
271 if ( is_author() ) {
272 return array( get_avatar_url( get_the_author_meta( 'ID' ), array( 'size' => 512 ) ) );
273 }
274
275 if ( count( $image ) >= opengraph_max_images() ) {
276 return $image;
277 }
278
279 if ( is_attachment() && wp_attachment_is_image() ) {
280 $id = get_queried_object_id();
281 $image[] = current( wp_get_attachment_image_src( $id, 'large' ) ?: array() ); // phpcs:ignore
282 } elseif ( is_singular() && ! is_attachment() ) {
283 $id = get_queried_object_id();
284
285 // List post thumbnail first if this post has one.
286 if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $id ) ) {
287 $thumbnail_id = get_post_thumbnail_id( $id );
288 $image[] = current( wp_get_attachment_image_src( $thumbnail_id, 'large' ) ?: array() ); // phpcs:ignore
289 }
290 }
291
292 return array_unique( $image );
293 }
294
295
296 /**
297 * Block image property, using the first image in the post content.
298 *
299 * @param array $image The current list of images.
300 *
301 * @return array The list of images.
302 */
303 function opengraph_block_image( $image = array() ) {
304 if (
305 ! opengraph_site_supports_blocks() ||
306 count( $image ) >= opengraph_max_images() ||
307 ! is_singular() ||
308 is_attachment()
309 ) {
310 return $image;
311 }
312
313 // Get the first image in the post content.
314 $blocks = parse_blocks( get_the_content( null, false ) );
315 foreach ( $blocks as $block ) {
316 if (
317 'core/image' === $block['blockName'] ||
318 'core/cover' === $block['blockName']
319 ) {
320 $id = $block['attrs']['id'];
321 $image[] = current( wp_get_attachment_image_src( $id, 'large' ) ?: array() ); // phpcs:ignore
322 }
323 }
324
325 return array_unique( $image );
326 }
327
328
329 /**
330 * Parse images in the HTML content.
331 *
332 * @param array $image The current list of images.
333 *
334 * @return array The list of images.
335 */
336 function opengraph_parsed_image( $image = array() ) {
337 // If someone calls that function directly, bail.
338 if (
339 ! \class_exists( 'WP_HTML_Tag_Processor' ) ||
340 ! opengraph_site_supports_blocks() ||
341 count( $image ) >= opengraph_max_images() ||
342 ! is_singular() ||
343 is_attachment()
344 ) {
345 return $image;
346 }
347
348 $post_id = get_queried_object_id();
349 $base = wp_upload_dir()['baseurl'];
350 $content = get_post_field( 'post_content', $post_id );
351 $tags = new WP_HTML_Tag_Processor( $content );
352
353 // This linter warning is a false positive - we have to re-count each time here as we modify $images.
354 // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
355 while ( $tags->next_tag( 'img' ) && ( count( $image ) <= opengraph_max_images() ) ) {
356 $src = $tags->get_attribute( 'src' );
357
358 /*
359 * If the img source is in our uploads dir, get the
360 * associated ID. Note: if there's a -500x500
361 * type suffix, we remove it, but we try the original
362 * first in case the original image is actually called
363 * that. Likewise, we try adding the -scaled suffix for
364 * the case that this is a small version of an image
365 * that was big enough to get scaled down on upload:
366 * https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/
367 */
368 if ( null === $src || ! str_starts_with( $src, $base ) ) {
369 continue;
370 }
371
372 $img_id = attachment_url_to_postid( $src );
373
374 if ( 0 === $img_id ) {
375 $count = 0;
376 $src = strtok( $src, '?' );
377 $img_id = attachment_url_to_postid( $src );
378 }
379
380 if ( 0 === $img_id ) {
381 $count = 0;
382 $src = preg_replace( '/-(?:\d+x\d+)(\.[a-zA-Z]+)$/', '$1', $src, 1, $count );
383 if ( $count > 0 ) {
384 $img_id = attachment_url_to_postid( $src );
385 }
386 }
387
388 if ( 0 === $img_id ) {
389 $src = preg_replace( '/(\.[a-zA-Z]+)$/', '-scaled$1', $src );
390 $img_id = attachment_url_to_postid( $src );
391 }
392
393 if ( 0 !== $img_id ) {
394 $image[] = current( wp_get_attachment_image_src( $img_id, 'large' ) ?: array() ); // phpcs:ignore
395 }
396 }
397
398 return array_unique( $image );
399 }
400
401
402 /**
403 * Attached images.
404 *
405 * @param array $image The current list of images.
406 *
407 * @return array The list of images.
408 */
409 function opengraph_attached_image( $image = array() ) {
410 $max_images = opengraph_max_images();
411
412 if (
413 count( $image ) >= $max_images ||
414 ! is_singular() || is_attachment()
415 ) {
416 return $image;
417 }
418
419 $id = get_queried_object_id();
420
421 $query = new WP_Query(
422 array(
423 'post_parent' => $id,
424 'post_status' => 'inherit',
425 'post_type' => 'attachment',
426 'post_mime_type' => 'image',
427 'order' => 'ASC',
428 'orderby' => 'menu_order ID',
429 'fields' => 'ids',
430 'posts_per_page' => $max_images,
431 )
432 );
433
434 $image_ids = $query->get_posts();
435
436 // Get URLs for each image.
437 foreach ( $image_ids as $id ) {
438 $thumbnail = wp_get_attachment_image_src( $id, 'large' );
439 if ( $thumbnail ) {
440 $image[] = $thumbnail[0];
441 }
442 }
443
444 return array_unique( $image );
445 }
446
447 /**
448 * Fallback image property, using the site icon, custom logo, or header images.
449 *
450 * @param array $image The current list of images.
451 *
452 * @return array The list of images.
453 */
454 function opengraph_fallback_image( $image = array() ) {
455 if ( $image ) {
456 return $image;
457 }
458
459 $max_images = opengraph_max_images();
460
461 // Try site icon.
462 if ( function_exists( 'get_site_icon_url' ) && has_site_icon() ) {
463 $image[] = get_site_icon_url( 512 );
464 }
465
466 // Try custom logo second.
467 if ( empty( $image ) ) {
468 $custom_logo = get_theme_mod( 'custom_logo' );
469 $image[] = wp_get_attachment_image_src( $custom_logo, 'large' );
470 }
471
472 // Try header images.
473 if ( empty( $image ) && function_exists( 'get_uploaded_header_images' ) ) {
474 if ( is_random_header_image() ) {
475 foreach ( get_uploaded_header_images() as $header_image ) {
476 $image[] = $header_image['url'];
477 if ( count( $image ) >= $max_images ) {
478 break;
479 }
480 }
481 } elseif ( get_header_image() ) {
482 $image[] = get_header_image();
483 }
484 }
485
486 return array_unique( $image );
487 }
488
489
490 /**
491 * Default audio property, using get_attached_media.
492 *
493 * @param array $audio The current list of audio files.
494 *
495 * @return array The list of audio files.
496 */
497 function opengraph_default_audio( $audio = array() ) {
498 $id = get_queried_object_id();
499 $attachments = get_attached_media( 'audio', $id );
500
501 if ( empty( $attachments ) ) {
502 return $audio;
503 }
504
505 foreach ( $attachments as $attachment ) {
506 $audio[] = wp_get_attachment_url( $attachment->ID );
507 }
508
509 return $audio;
510 }
511
512
513 /**
514 * Default video property, using get_attached_media.
515 *
516 * @param array $video The current list of video files.
517 *
518 * @return array The list of video files.
519 */
520 function opengraph_default_video( $video = array() ) {
521 $id = get_queried_object_id();
522 $attachments = get_attached_media( 'video', $id );
523
524 if ( empty( $attachments ) ) {
525 return $video;
526 }
527
528 foreach ( $attachments as $attachment ) {
529 $video[] = wp_get_attachment_url( $attachment->ID );
530 }
531
532 return $video;
533 }
534
535
536 /**
537 * Default url property, using the permalink for the page.
538 *
539 * @param string $url The current URL.
540 *
541 * @return string The URL.
542 */
543 function opengraph_default_url( $url = '' ) {
544 if ( empty( $url ) ) {
545 if ( is_singular() ) {
546 $url = get_permalink();
547 } elseif ( is_author() ) {
548 $url = get_author_posts_url( get_queried_object_id() );
549 }
550 }
551
552 return esc_url( $url );
553 }
554
555
556 /**
557 * Default site_name property, using the bloginfo name.
558 *
559 * @param string $name The current site name.
560 *
561 * @return string The site name.
562 */
563 function opengraph_default_sitename( $name = '' ) {
564 if ( empty( $name ) ) {
565 $name = get_bloginfo( 'name' );
566 }
567
568 return wp_strip_all_tags( $name );
569 }
570
571
572 /**
573 * Default description property, using the excerpt or content for posts, or the
574 * bloginfo description.
575 *
576 * @param string $description The current description.
577 * @param int $length The maximum length of the description.
578 *
579 * @return string The description.
580 */
581 function opengraph_default_description( $description = '', $length = 55 ) {
582 if ( $description ) {
583 return $description;
584 }
585
586 if ( is_singular() ) {
587 $post = get_queried_object();
588 if ( post_password_required( $post ) ) {
589 $description = __( 'This content is password protected.', 'opengraph' );
590 } elseif ( ! empty( $post->post_excerpt ) ) {
591 $description = $post->post_excerpt;
592 } else {
593 $description = $post->post_content;
594 }
595 } elseif ( is_author() ) {
596 $id = get_queried_object_id();
597 $description = get_user_meta( $id, 'description', true );
598 } elseif ( is_category() && category_description() ) {
599 $description = category_description();
600 } elseif ( is_tag() && tag_description() ) {
601 $description = tag_description();
602 } elseif (
603 is_archive() &&
604 function_exists( 'get_the_archive_description' ) &&
605 get_the_archive_description()
606 ) { // New in version 4.1 to get all other archive descriptions.
607 $description = get_the_archive_description();
608 } else {
609 $description = get_bloginfo( 'description' );
610 }
611
612 // strip description to first 55 words.
613 $description = wp_strip_all_tags( strip_shortcodes( $description ) );
614 $description = opengraph_trim_text( $description, $length );
615
616 return wp_strip_all_tags( $description );
617 }
618
619
620 /**
621 * Default locale property, using the WordPress locale.
622 *
623 * @param string $locale The current locale.
624 *
625 * @return string The locale.
626 */
627 function opengraph_default_locale( $locale = '' ) {
628 if ( empty( $locale ) ) {
629 $locale = get_locale();
630 }
631
632 return $locale;
633 }
634
635
636 /**
637 * Default twitter-card type.
638 *
639 * @param string $card The current card type.
640 *
641 * @return string The card type.
642 */
643 function twitter_default_card( $card = '' ) {
644 if ( $card ) {
645 return $card;
646 }
647
648 $card = 'summary';
649 $images = apply_filters( 'opengraph_image', array() );
650
651 // Show large image on...
652 if ( is_singular() ) {
653 if (
654 // Gallery and image posts.
655 in_array( get_post_format(), array( 'image', 'gallery' ), true ) ||
656 // Posts with more than one image.
657 ( is_array( $images ) && count( $images ) > 1 ) ||
658 // Posts with a post-thumbnail.
659 ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail() )
660 ) {
661 $card = 'summary_large_image';
662 }
663 }
664
665 return $card;
666 }
667
668
669 /**
670 * Default twitter-card creator.
671 *
672 * @see https://developer.twitter.com/en/docs/twitter-for-websites/cards/guides/getting-started
673 *
674 * @param string $creator The current creator.
675 *
676 * @return string The creator.
677 */
678 function twitter_default_creator( $creator = '' ) {
679 if ( $creator || ! is_singular() ) {
680 return $creator;
681 }
682
683 $post = get_queried_object();
684 $author = $post->post_author;
685 $twitter = get_the_author_meta( 'twitter', $author );
686
687 if ( ! $twitter ) {
688 return $creator;
689 }
690
691 // Check if twitter-account matches "http://twitter.com/username".
692 if ( preg_match( '/^http:\/\/twitter\.com\/(#!\/)?(\w+)/i', $twitter, $matches ) ) {
693 $creator = '@' . $matches[2];
694 } elseif ( preg_match( '/^@?(\w+)$/i', $twitter, $matches ) ) { // Check if twitter-account matches "(@)username".
695 $creator = '@' . $matches[1];
696 }
697
698 return $creator;
699 }
700
701
702 /**
703 * Default fediverse creator.
704 *
705 * @see https://github.com/mastodon/mastodon/pull/30398
706 *
707 * @param string $creator The current creator.
708 *
709 * @return string The creator.
710 */
711 function fediverse_default_creator( $creator = '' ) {
712 if ( ! is_singular() ) {
713 return $creator;
714 }
715
716 $post = get_queried_object();
717 $author = $post->post_author;
718 $webfinger = get_the_author_meta( 'fediverse', $author );
719
720 if ( ! $webfinger ) {
721 return $creator;
722 }
723
724 $webfinger = ltrim( $webfinger, '@' );
725 $webfinger = str_replace( 'acct:', '', $webfinger );
726
727 return $webfinger;
728 }
729
730
731 /**
732 * Output Open Graph <meta> tags in the page header.
733 */
734 function opengraph_meta_tags() {
735 $metadata = opengraph_metadata();
736 foreach ( $metadata as $key => $value ) {
737 if ( empty( $key ) || empty( $value ) ) {
738 continue;
739 }
740 $value = (array) $value;
741
742 foreach ( $value as $v ) {
743 // Skip empty values.
744 if ( empty( $v ) ) {
745 continue;
746 }
747
748 // Check if "strict mode" is enabled.
749 if ( OPENGRAPH_STRICT_MODE === true ) {
750 if ( // Use "name" attribute for Twitter Cards.
751 str_starts_with( $key, 'twitter:' ) ||
752 str_starts_with( $key, 'fediverse:' )
753 ) {
754 printf(
755 '<meta name="%1$s" content="%2$s" />' . PHP_EOL,
756 esc_attr( $key ),
757 esc_attr( $v )
758 );
759 } else { // Use "property" attribute for Open Graph.
760 printf(
761 '<meta property="%1$s" content="%2$s" />' . PHP_EOL,
762 esc_attr( $key ),
763 esc_attr( $v )
764 );
765 }
766 } else {
767 // Use the "property" and "name" attributes.
768 printf(
769 '<meta property="%1$s" name="%1$s" content="%2$s" />' . PHP_EOL,
770 esc_attr( $key ),
771 esc_attr( $v )
772 );
773 }
774 }
775 }
776 }
777 add_action( 'wp_head', 'opengraph_meta_tags' );
778
779
780 /**
781 * Include profile metadata for author pages.
782 *
783 * @link http://ogp.me/#type_profile
784 *
785 * @param array $metadata The current metadata.
786 *
787 * @return array The updated metadata.
788 */
789 function opengraph_profile_metadata( $metadata ) {
790 if ( is_author() ) {
791 $id = get_queried_object_id();
792
793 $metadata['profile:first_name'] = get_the_author_meta( 'first_name', $id );
794 $metadata['profile:last_name'] = get_the_author_meta( 'last_name', $id );
795 $metadata['profile:username'] = get_the_author_meta( 'nicename', $id );
796 }
797
798 return $metadata;
799 }
800
801
802 /**
803 * Include article metadata for posts and pages.
804 *
805 * @link http://ogp.me/#type_article
806 *
807 * @param array $metadata The current metadata.
808 *
809 * @return array The updated metadata.
810 */
811 function opengraph_article_metadata( $metadata ) {
812 if ( ! is_singular() ) {
813 return $metadata;
814 }
815
816 $post = get_queried_object();
817 $author = $post->post_author;
818
819 // Check if page/post has tags.
820 $tags = wp_get_object_terms( $post->ID, 'post_tag' );
821 if ( $tags && is_array( $tags ) ) {
822 foreach ( $tags as $tag ) {
823 $metadata['article:tag'][] = $tag->name;
824 }
825 }
826
827 // Check if page/post has categories.
828 $categories = wp_get_object_terms( $post->ID, 'category' );
829 if ( $categories && is_array( $categories ) ) {
830 $metadata['article:section'][] = current( $categories )->name;
831 }
832
833 $metadata['article:published_time'] = get_the_time( 'c', $post->ID );
834 $metadata['article:modified_time'] = get_the_modified_time( 'c', $post->ID );
835 $metadata['article:author'][] = get_author_posts_url( $author );
836
837 $facebook = get_the_author_meta( 'facebook', $author );
838
839 if ( ! empty( $facebook ) ) {
840 $metadata['article:author'][] = $facebook;
841 }
842
843 return $metadata;
844 }
845
846
847 /**
848 * Add "twitter" as a contact method
849 *
850 * @param array $user_contactmethods The current list of contact methods.
851 *
852 * @return array The updated list of contact methods.
853 */
854 function opengraph_user_contactmethods( $user_contactmethods = array() ) {
855 $user_contactmethods['twitter'] = __( 'Twitter', 'opengraph' );
856 $user_contactmethods['facebook'] = __( 'Facebook (Profile URL)', 'opengraph' );
857 $user_contactmethods['fediverse'] = __( 'Fediverse (username@host.tld)', 'opengraph' );
858
859 return $user_contactmethods;
860 }
861 add_filter( 'user_contactmethods', 'opengraph_user_contactmethods', 1 );
862
863
864 /**
865 * Add 512x512 icon size
866 *
867 * @param array $sizes Sizes available for the site icon.
868 *
869 * @return array updated list of icons.
870 */
871 function opengraph_site_icon_image_sizes( $sizes ) {
872 $sizes[] = 512;
873
874 return array_unique( $sizes );
875 }
876 add_filter( 'site_icon_image_sizes', 'opengraph_site_icon_image_sizes' );
877
878
879 /**
880 * Helper function to trim text using the same default values for length and
881 * 'more' text as wp_trim_excerpt.
882 *
883 * @param string $text The text to trim.
884 * @param int $length The maximum number of words to include.
885 *
886 * @return string The trimmed text.
887 */
888 function opengraph_trim_text( $text, $length = 55 ) {
889 $excerpt_length = apply_filters( 'excerpt_length', $length );
890 $excerpt_more = apply_filters( 'excerpt_more', ' [...]' );
891
892 return wp_trim_words( $text, $excerpt_length, $excerpt_more );
893 }
894
895 /**
896 * Get the maximum number of images to include in Open Graph metadata.
897 *
898 * @return int The maximum number of images to include.
899 */
900 function opengraph_max_images() {
901 /**
902 * Filter the maximum number of images to include in Open Graph metadata.
903 *
904 * As of July 2014, Facebook seems to only let you select from the first 3 images.
905 *
906 * @param int $max_images The maximum number of images to include.
907 */
908 $max_images = apply_filters( 'opengraph_max_images', 3 );
909
910 // Max images can't be negative or zero.
911 if ( $max_images <= 0 ) {
912 $max_images = 1;
913 }
914
915 return $max_images;
916 }
917
918 /**
919 * Check if a site supports the block editor.
920 *
921 * @return boolean True if the site supports the block editor, false otherwise.
922 */
923 function opengraph_site_supports_blocks() {
924 $return = true;
925
926 if ( version_compare( get_bloginfo( 'version' ), '5.9', '<' ) ) {
927 $return = false;
928 } elseif ( function_exists( 'classicpress_version' ) ) {
929 $return = false;
930 } elseif (
931 ! function_exists( 'register_block_type_from_metadata' ) ||
932 ! function_exists( 'do_blocks' )
933 ) {
934 $return = false;
935 }
936
937 /**
938 * Allow plugins to disable block editor support,
939 * thus disabling blocks registered by the OpenGraph plugin.
940 *
941 * @param boolean $supports_blocks True if the site supports the block editor, false otherwise.
942 */
943 return apply_filters( 'opengraph_site_supports_blocks', $return );
944 }
945
946
947 if ( ! function_exists( 'str_starts_with' ) ) {
948 /**
949 * `str_starts_with` function for PHP < 8.0.
950 *
951 * @see https://www.php.net/manual/en/function.str-starts-with
952 *
953 * @param string $haystack The string to search in.
954 * @param string $needle The string to search for.
955 *
956 * @return bool True if the string starts with the needle, false otherwise.
957 */
958 function str_starts_with( $haystack, $needle ) {
959 return 0 === strncmp( $haystack, $needle, \strlen( $needle ) );
960 }
961 }
962