| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
| 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 |
|
| 14 |
use Automattic\Block_Scanner; |
| 15 |
use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 16 |
use Automattic\Jetpack\Current_Plan; |
| 17 |
use Automattic\Jetpack\Post_Media\Images; |
| 18 |
use Automattic\Jetpack\Status; |
| 19 |
use Automattic\Jetpack\Status\Host; |
| 20 |
|
| 21 |
if ( ! defined( 'ABSPATH' ) ) { |
| 22 |
exit( 0 ); |
| 23 |
} |
| 24 |
|
| 25 |
add_action( 'wp_head', 'jetpack_og_tags' ); |
| 26 |
|
| 27 |
/** |
| 28 |
* Keep Twitter Cards from analyzing the body of a gated post. |
| 29 |
* |
| 30 |
* Twitter_Cards::twitter_cards_tags() runs at priority 11 and, when a card type |
| 31 |
* is already set, emits only the site default image instead of images and |
| 32 |
* videos parsed out of the body. |
| 33 |
* |
| 34 |
* @since 16.2 |
| 35 |
* |
| 36 |
* @param array $tags Open Graph tags. |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
function jetpack_og_gate_twitter_cards( $tags ) { |
| 40 |
/** This filter is documented in class.jetpack.php */ |
| 41 |
if ( apply_filters( 'jetpack_disable_twitter_cards', false ) ) { |
| 42 |
return $tags; |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! is_singular() ) { |
| 46 |
return $tags; |
| 47 |
} |
| 48 |
|
| 49 |
$post = get_post(); |
| 50 |
if ( ! $post instanceof WP_Post || ! \Automattic\Jetpack\SEO\Content_Gate::is_gated( $post ) ) { |
| 51 |
return $tags; |
| 52 |
} |
| 53 |
|
| 54 |
if ( empty( $tags['twitter:text:title'] ) ) { |
| 55 |
$tags['twitter:text:title'] = get_the_title( $post ); |
| 56 |
} |
| 57 |
|
| 58 |
// Twitter_Cards stops once a card type is set, so carry the featured image |
| 59 |
// across here; it is chosen separately and never parsed out of the body. |
| 60 |
$card_type = 'summary'; |
| 61 |
if ( empty( $tags['twitter:image'] ) ) { |
| 62 |
$image = Images::get_image( |
| 63 |
$post->ID, |
| 64 |
array( |
| 65 |
'width' => 144, |
| 66 |
'height' => 144, |
| 67 |
'from_slideshow' => false, |
| 68 |
'from_gallery' => false, |
| 69 |
'from_attachment' => false, |
| 70 |
'from_blocks' => false, |
| 71 |
'from_html' => false, |
| 72 |
) |
| 73 |
); |
| 74 |
|
| 75 |
// Twitter's limits: 4096px maximum, and 300x157 minimum for a large card. |
| 76 |
if ( |
| 77 |
! empty( $image['src'] ) |
| 78 |
&& isset( $image['src_width'] ) && isset( $image['src_height'] ) |
| 79 |
&& (int) $image['src_width'] <= 4096 |
| 80 |
&& (int) $image['src_height'] <= 4096 |
| 81 |
) { |
| 82 |
if ( (int) $image['src_width'] >= 300 && (int) $image['src_height'] >= 157 ) { |
| 83 |
$card_type = 'summary_large_image'; |
| 84 |
$tags['twitter:image'] = esc_url( add_query_arg( 'w', 640, $image['src'] ) ); |
| 85 |
} else { |
| 86 |
$tags['twitter:image'] = esc_url( add_query_arg( 'w', 144, $image['src'] ) ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! empty( $image['alt_text'] ) ) { |
| 90 |
$alt_length = 420; |
| 91 |
$tags['twitter:image:alt'] = strlen( $image['alt_text'] ) > $alt_length |
| 92 |
? esc_attr( mb_substr( $image['alt_text'], 0, $alt_length ) . '…' ) |
| 93 |
: esc_attr( $image['alt_text'] ); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if ( empty( $tags['twitter:card'] ) ) { |
| 99 |
$tags['twitter:card'] = $card_type; |
| 100 |
} |
| 101 |
|
| 102 |
return $tags; |
| 103 |
} |
| 104 |
add_filter( 'jetpack_open_graph_tags', 'jetpack_og_gate_twitter_cards', 10 ); |
| 105 |
add_action( 'web_stories_story_head', 'jetpack_og_tags' ); |
| 106 |
|
| 107 |
// Add a Fediverse Open Graph Tag when an author has connected their Mastodon account. |
| 108 |
add_filter( 'jetpack_open_graph_tags', 'jetpack_add_fediverse_creator_open_graph_tag', 10, 1 ); |
| 109 |
add_filter( 'jetpack_open_graph_output', 'jetpack_filter_fediverse_cards_output', 10, 1 ); |
| 110 |
|
| 111 |
/** |
| 112 |
* Outputs Open Graph tags generated by Jetpack. |
| 113 |
*/ |
| 114 |
function jetpack_og_tags() { |
| 115 |
global $post; |
| 116 |
$data = $post; // so that we don't accidentally explode the global. |
| 117 |
|
| 118 |
$is_amp_response = ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ); |
| 119 |
|
| 120 |
// Disable the widont filter on WP.com to avoid stray  s. |
| 121 |
$disable_widont = remove_filter( 'the_title', 'widont' ); |
| 122 |
|
| 123 |
$og_output = "\n"; |
| 124 |
if ( ! $is_amp_response ) { // Because AMP optimizes the order or the nodes in the head. |
| 125 |
$og_output .= "<!-- Jetpack Open Graph Tags -->\n"; |
| 126 |
} |
| 127 |
$tags = array(); |
| 128 |
|
| 129 |
/** |
| 130 |
* Filter the minimum width of the images used in Jetpack Open Graph Meta Tags. |
| 131 |
* |
| 132 |
* @module sharedaddy, publicize |
| 133 |
* |
| 134 |
* @since 2.0.0 |
| 135 |
* |
| 136 |
* @param int 200 Minimum image width used in Jetpack Open Graph Meta Tags. |
| 137 |
*/ |
| 138 |
$image_width = absint( apply_filters( 'jetpack_open_graph_image_width', 200 ) ); |
| 139 |
/** |
| 140 |
* Filter the minimum height of the images used in Jetpack Open Graph Meta Tags. |
| 141 |
* |
| 142 |
* @module sharedaddy, publicize |
| 143 |
* |
| 144 |
* @since 2.0.0 |
| 145 |
* |
| 146 |
* @param int 200 Minimum image height used in Jetpack Open Graph Meta Tags. |
| 147 |
*/ |
| 148 |
$image_height = absint( apply_filters( 'jetpack_open_graph_image_height', 200 ) ); |
| 149 |
$description_length = 197; |
| 150 |
|
| 151 |
if ( is_home() || is_front_page() ) { |
| 152 |
$site_type = Jetpack_Options::get_option_and_ensure_autoload( 'open_graph_protocol_site_type', '' ); |
| 153 |
$tags['og:type'] = ! empty( $site_type ) ? $site_type : 'website'; |
| 154 |
$tags['og:title'] = get_bloginfo( 'name' ); |
| 155 |
$tags['og:description'] = get_bloginfo( 'description' ); |
| 156 |
|
| 157 |
$front_page_id = get_option( 'page_for_posts' ); |
| 158 |
if ( 'page' === get_option( 'show_on_front' ) && $front_page_id && is_home() ) { |
| 159 |
$tags['og:url'] = get_permalink( $front_page_id ); |
| 160 |
} else { |
| 161 |
$tags['og:url'] = home_url( '/' ); |
| 162 |
} |
| 163 |
|
| 164 |
// Associate a blog's root path with one or more Facebook accounts. |
| 165 |
$facebook_admins = Jetpack_Options::get_option_and_ensure_autoload( 'facebook_admins', array() ); |
| 166 |
if ( ! empty( $facebook_admins ) ) { |
| 167 |
$tags['fb:admins'] = $facebook_admins; |
| 168 |
} |
| 169 |
} elseif ( is_author() ) { |
| 170 |
$tags['og:type'] = 'profile'; |
| 171 |
|
| 172 |
$author = get_queried_object(); |
| 173 |
|
| 174 |
if ( is_a( $author, 'WP_User' ) ) { |
| 175 |
$tags['og:title'] = $author->display_name; |
| 176 |
if ( ! empty( $author->user_url ) ) { |
| 177 |
$tags['og:url'] = $author->user_url; |
| 178 |
} else { |
| 179 |
$tags['og:url'] = get_author_posts_url( $author->ID ); |
| 180 |
} |
| 181 |
$tags['og:description'] = $author->description; |
| 182 |
$tags['profile:first_name'] = get_the_author_meta( 'first_name', $author->ID ); |
| 183 |
$tags['profile:last_name'] = get_the_author_meta( 'last_name', $author->ID ); |
| 184 |
} |
| 185 |
} elseif ( is_archive() ) { |
| 186 |
$tags['og:type'] = 'website'; |
| 187 |
$tags['og:title'] = wp_get_document_title(); |
| 188 |
|
| 189 |
$archive = get_queried_object(); |
| 190 |
if ( $archive instanceof WP_Term ) { |
| 191 |
$tags['og:url'] = get_term_link( $archive->term_id, $archive->taxonomy ); |
| 192 |
$tags['og:description'] = $archive->description; |
| 193 |
} elseif ( ! empty( $archive ) && is_post_type_archive() ) { |
| 194 |
$tags['og:url'] = get_post_type_archive_link( $archive->name ); |
| 195 |
$tags['og:description'] = $archive->description; |
| 196 |
} |
| 197 |
} elseif ( is_singular() && is_a( $data, 'WP_Post' ) ) { |
| 198 |
$tags['og:type'] = 'article'; |
| 199 |
if ( empty( $data->post_title ) ) { |
| 200 |
$tags['og:title'] = ' '; |
| 201 |
} else { |
| 202 |
/** This filter is documented in core/src/wp-includes/post-template.php */ |
| 203 |
$tags['og:title'] = wp_kses( apply_filters( 'the_title', $data->post_title, $data->ID ), array() ); |
| 204 |
} |
| 205 |
|
| 206 |
$tags['og:url'] = get_permalink( $data->ID ); |
| 207 |
if ( ! post_password_required( $data ) ) { |
| 208 |
/* |
| 209 |
* If the post author set an excerpt, use that. |
| 210 |
* Otherwise, pick the post content that comes before the More tag if there is one. |
| 211 |
* Do not use the post content if it contains premium content. |
| 212 |
*/ |
| 213 |
if ( ! empty( $data->post_excerpt ) ) { |
| 214 |
$tags['og:description'] = jetpack_og_get_description( $data->post_excerpt ); |
| 215 |
} else { |
| 216 |
$content = \Automattic\Jetpack\SEO\Content_Gate::is_gated( $data ) |
| 217 |
? \Automattic\Jetpack\SEO\Content_Gate::public_teaser( $data ) |
| 218 |
: $data->post_content; |
| 219 |
|
| 220 |
if ( '' !== $content ) { |
| 221 |
$excerpt = explode( '<!--more-->', $content )[0]; |
| 222 |
$tags['og:description'] = jetpack_og_get_description( $excerpt ); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
$tags['article:published_time'] = gmdate( 'c', strtotime( $data->post_date_gmt ) ); |
| 228 |
$tags['article:modified_time'] = gmdate( 'c', strtotime( $data->post_modified_gmt ) ); |
| 229 |
if ( post_type_supports( get_post_type( $data ), 'author' ) && isset( $data->post_author ) ) { |
| 230 |
$publicize_facebook_user = get_post_meta( $data->ID, '_publicize_facebook_user', true ); |
| 231 |
if ( ! empty( $publicize_facebook_user ) ) { |
| 232 |
$tags['article:author'] = esc_url( $publicize_facebook_user ); |
| 233 |
} |
| 234 |
} |
| 235 |
} elseif ( is_search() ) { |
| 236 |
if ( '' !== get_query_var( 's', '' ) ) { |
| 237 |
$tags['og:title'] = wp_get_document_title(); |
| 238 |
} |
| 239 |
} |
| 240 |
/** |
| 241 |
* Allow plugins to inject additional template-specific Open Graph tags. |
| 242 |
* |
| 243 |
* @module sharedaddy, publicize |
| 244 |
* |
| 245 |
* @since 3.0.0 |
| 246 |
* |
| 247 |
* @param array $tags Array of Open Graph Meta tags. |
| 248 |
* @param array $args Array of image size parameters. |
| 249 |
*/ |
| 250 |
$tags = apply_filters( 'jetpack_open_graph_base_tags', $tags, compact( 'image_width', 'image_height' ) ); |
| 251 |
|
| 252 |
// Re-enable widont if we had disabled it. |
| 253 |
if ( $disable_widont ) { |
| 254 |
add_filter( 'the_title', 'widont' ); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Do not return any Open Graph Meta tags if we don't have any info about a post. |
| 259 |
* |
| 260 |
* @module sharedaddy, publicize |
| 261 |
* |
| 262 |
* @since 3.0.0 |
| 263 |
* |
| 264 |
* @param bool true Do not return any Open Graph Meta tags if we don't have any info about a post. |
| 265 |
*/ |
| 266 |
if ( empty( $tags ) && apply_filters( 'jetpack_open_graph_return_if_empty', true ) ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
$tags['og:site_name'] = get_bloginfo( 'name' ); |
| 271 |
|
| 272 |
// Get image info and build tags. |
| 273 |
if ( ! post_password_required() ) { |
| 274 |
$image_info = jetpack_og_get_image( $image_width, $image_height ); |
| 275 |
$tags['og:image'] = $image_info['src']; |
| 276 |
|
| 277 |
if ( ! empty( $image_info['width'] ) ) { |
| 278 |
$tags['og:image:width'] = (int) $image_info['width']; |
| 279 |
} |
| 280 |
if ( ! empty( $image_info['height'] ) ) { |
| 281 |
$tags['og:image:height'] = (int) $image_info['height']; |
| 282 |
} |
| 283 |
// If we have an image, add the alt text even if it's empty. |
| 284 |
if ( ! empty( $image_info['src'] ) && isset( $image_info['alt_text'] ) ) { |
| 285 |
$tags['og:image:alt'] = esc_attr( $image_info['alt_text'] ); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
// Facebook whines if you give it an empty title. |
| 290 |
if ( empty( $tags['og:title'] ) ) { |
| 291 |
$tags['og:title'] = __( '(no title)', 'jetpack' ); |
| 292 |
} |
| 293 |
|
| 294 |
// Shorten the description if it's too long. |
| 295 |
if ( isset( $tags['og:description'] ) ) { |
| 296 |
$tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '…' : $tags['og:description']; |
| 297 |
} |
| 298 |
|
| 299 |
// Try to add OG locale tag if the WP->FB data mapping exists. |
| 300 |
if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
| 301 |
require_once JETPACK__GLOTPRESS_LOCALES_PATH; |
| 302 |
$_locale = get_locale(); |
| 303 |
|
| 304 |
// We have to account for w.org vs WP.com locale divergence. |
| 305 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 306 |
$gp_locale = GP_Locales::by_field( 'slug', $_locale ); |
| 307 |
} else { |
| 308 |
$gp_locale = GP_Locales::by_field( 'wp_locale', $_locale ); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
if ( isset( $gp_locale->facebook_locale ) && ! empty( $gp_locale->facebook_locale ) ) { |
| 313 |
$tags['og:locale'] = $gp_locale->facebook_locale; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Allow the addition of additional Open Graph Meta tags, or modify the existing tags. |
| 318 |
* |
| 319 |
* @module sharedaddy, publicize |
| 320 |
* |
| 321 |
* @since 2.0.0 |
| 322 |
* |
| 323 |
* @param array $tags Array of Open Graph Meta tags. |
| 324 |
* @param array $args Array of image size parameters. |
| 325 |
*/ |
| 326 |
$tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) ); |
| 327 |
|
| 328 |
// secure_urls need to go right after each og:image to work properly so we will abstract them here. |
| 329 |
$tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url']; |
| 330 |
$secure = $tags['og:image:secure_url']; |
| 331 |
unset( $tags['og:image:secure_url'] ); |
| 332 |
$secure_image_num = 0; |
| 333 |
|
| 334 |
$allowed_empty_tags = array( |
| 335 |
'og:image:alt', |
| 336 |
); |
| 337 |
|
| 338 |
foreach ( (array) $tags as $tag_property => $tag_content ) { |
| 339 |
// to accommodate multiple images. |
| 340 |
$tag_content = (array) $tag_content; |
| 341 |
$tag_content = array_unique( array_filter( $tag_content, 'is_scalar' ) ); |
| 342 |
|
| 343 |
foreach ( $tag_content as $tag_content_single ) { |
| 344 |
if ( empty( $tag_content_single ) && ! in_array( $tag_property, $allowed_empty_tags, true ) ) { |
| 345 |
continue; // Only allow certain empty tags. |
| 346 |
} |
| 347 |
|
| 348 |
switch ( $tag_property ) { |
| 349 |
case 'og:url': |
| 350 |
case 'og:image': |
| 351 |
case 'og:image:url': |
| 352 |
case 'og:image:secure_url': |
| 353 |
case 'og:audio': |
| 354 |
case 'og:audio:url': |
| 355 |
case 'og:audio:secure_url': |
| 356 |
case 'og:video': |
| 357 |
case 'og:video:url': |
| 358 |
case 'og:video:secure_url': |
| 359 |
$og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_url( $tag_content_single ) ); |
| 360 |
break; |
| 361 |
default: |
| 362 |
$og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) ); |
| 363 |
} |
| 364 |
/** |
| 365 |
* Filter the HTML Output of each Open Graph Meta tag. |
| 366 |
* |
| 367 |
* @module sharedaddy, publicize |
| 368 |
* |
| 369 |
* @since 2.0.0 |
| 370 |
* |
| 371 |
* @param string $og_tag HTML HTML Output of each Open Graph Meta tag. |
| 372 |
*/ |
| 373 |
$og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag ); |
| 374 |
$og_output .= "\n"; |
| 375 |
|
| 376 |
if ( 'og:image' === $tag_property ) { |
| 377 |
if ( is_array( $secure ) && ! empty( $secure[ $secure_image_num ] ) ) { |
| 378 |
$og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) ); |
| 379 |
/** This filter is documented in functions.opengraph.php */ |
| 380 |
$og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag ); |
| 381 |
$og_output .= "\n"; |
| 382 |
} elseif ( ! is_array( $secure ) && ! empty( $secure ) ) { |
| 383 |
$og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) ); |
| 384 |
/** This filter is documented in functions.opengraph.php */ |
| 385 |
$og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag ); |
| 386 |
$og_output .= "\n"; |
| 387 |
} |
| 388 |
++$secure_image_num; |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
if ( ! $is_amp_response ) { // Because AMP optimizes the order or the nodes in the head. |
| 394 |
$og_output .= "\n<!-- End Jetpack Open Graph Tags -->"; |
| 395 |
} |
| 396 |
$og_output .= "\n"; |
| 397 |
// This is trusted output or added by a filter. |
| 398 |
echo $og_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Returns an image used in social shares. |
| 403 |
* |
| 404 |
* @since 2.0.0 |
| 405 |
* |
| 406 |
* @param int $width Minimum width for the image. Default is 200 based on Facebook's requirement. |
| 407 |
* @param int $height Minimum height for the image. Default is 200 based on Facebook's requirement. |
| 408 |
* @param null $deprecated Deprecated. |
| 409 |
* |
| 410 |
* @return array The source ('src'), 'width', and 'height' of the image. |
| 411 |
*/ |
| 412 |
function jetpack_og_get_image( $width = 200, $height = 200, $deprecated = null ) { |
| 413 |
if ( ! empty( $deprecated ) ) { |
| 414 |
_deprecated_argument( __FUNCTION__, 'jetpack-6.6.0' ); |
| 415 |
} |
| 416 |
$image = array(); |
| 417 |
|
| 418 |
if ( is_singular() && ! is_home() ) { |
| 419 |
// Grab obvious image if post is an attachment page for an image. |
| 420 |
if ( is_attachment( get_the_ID() ) && str_starts_with( get_post_mime_type(), 'image' ) ) { |
| 421 |
$image['src'] = wp_get_attachment_url( get_the_ID() ); |
| 422 |
$image['alt_text'] = Images::get_alt_text( get_the_ID() ); |
| 423 |
} |
| 424 |
|
| 425 |
// Attempt to find something good for this post using our generalized PostImages code. |
| 426 |
if ( empty( $image ) ) { |
| 427 |
$image_args = array( |
| 428 |
'width' => $width, |
| 429 |
'height' => $height, |
| 430 |
); |
| 431 |
// Every source but the featured image is parsed out of the body. |
| 432 |
if ( \Automattic\Jetpack\SEO\Content_Gate::is_gated( get_post( get_the_ID() ) ) ) { |
| 433 |
$image_args += array( |
| 434 |
'from_slideshow' => false, |
| 435 |
'from_gallery' => false, |
| 436 |
'from_attachment' => false, |
| 437 |
'from_blocks' => false, |
| 438 |
'from_html' => false, |
| 439 |
); |
| 440 |
} |
| 441 |
$post_image = Images::get_image( get_the_ID(), $image_args ); |
| 442 |
if ( ! empty( $post_image ) && is_array( $post_image ) ) { |
| 443 |
$image['src'] = $post_image['src']; |
| 444 |
if ( isset( $post_image['src_width'] ) && isset( $post_image['src_height'] ) ) { |
| 445 |
$image['width'] = $post_image['src_width']; |
| 446 |
$image['height'] = $post_image['src_height']; |
| 447 |
} |
| 448 |
if ( ! empty( $post_image['alt_text'] ) ) { |
| 449 |
$image['alt_text'] = $post_image['alt_text']; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} elseif ( is_author() ) { |
| 454 |
$author = get_queried_object(); |
| 455 |
if ( is_a( $author, 'WP_User' ) ) { |
| 456 |
$image['src'] = get_avatar_url( |
| 457 |
$author->user_email, |
| 458 |
array( |
| 459 |
'size' => $width, |
| 460 |
) |
| 461 |
); |
| 462 |
$image['alt_text'] = $author->display_name; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/* |
| 467 |
* Generate a fallback social image, |
| 468 |
* dynamically generated based on the site name, |
| 469 |
* a representative image of the site, |
| 470 |
* and a custom template used by our Social Image Generator. |
| 471 |
*/ |
| 472 |
if ( empty( $image ) ) { |
| 473 |
$site_image = jetpack_og_get_fallback_social_image( $width, $height ); |
| 474 |
if ( ! empty( $site_image ) ) { |
| 475 |
$image['src'] = $site_image['src']; |
| 476 |
$image['width'] = $site_image['width']; |
| 477 |
$image['height'] = $site_image['height']; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
// If we didn't get an explicit alt tag from the image, set a default. |
| 482 |
if ( empty( $image['alt_text'] ) ) { |
| 483 |
/** |
| 484 |
* Filter the default Open Graph image alt text, used when the Open Graph image from the post does not have an alt text. |
| 485 |
* |
| 486 |
* @since 10.4 |
| 487 |
* |
| 488 |
* @param string $str Default Open Graph image alt text. |
| 489 |
*/ |
| 490 |
$image['alt_text'] = apply_filters( 'jetpack_open_graph_image_default_alt_text', '' ); |
| 491 |
} |
| 492 |
|
| 493 |
return $image; |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Get a fallback social image for the site. |
| 498 |
* |
| 499 |
* @since 14.9 |
| 500 |
* |
| 501 |
* @param int $width The width of the image. |
| 502 |
* @param int $height The height of the image. |
| 503 |
* |
| 504 |
* @return array The source ('src'), 'width', 'height', and source type of the image. |
| 505 |
*/ |
| 506 |
function jetpack_og_get_fallback_social_image( $width, $height ) { |
| 507 |
// Default template. |
| 508 |
$template = 'edge'; |
| 509 |
|
| 510 |
// Let's get the site's representative image. |
| 511 |
$site_image = jetpack_og_get_site_image( $width, $height ); |
| 512 |
|
| 513 |
/** |
| 514 |
* Define your own site's representative image, |
| 515 |
* to override any fallback image found by looking through site's logo, site icon, and blavatar. |
| 516 |
* This will allow you to overwrite the default fallback image generated dynamically. |
| 517 |
* |
| 518 |
* @since 15.0 |
| 519 |
* |
| 520 |
* @param array $site_image Your own site's representative image. |
| 521 |
* @param array $site_image The site's representative image picked by Jetpack. { |
| 522 |
* @type string $src The source of the image. |
| 523 |
* @type int $width The width of the image. |
| 524 |
* @type int $height The height of the image. |
| 525 |
* @type string $type The type of the image. |
| 526 |
* } |
| 527 |
*/ |
| 528 |
$custom_site_image = apply_filters( 'jetpack_og_default_site_image', array(), $site_image ); |
| 529 |
if ( ! empty( $custom_site_image['src'] ) ) { |
| 530 |
return $custom_site_image; |
| 531 |
} |
| 532 |
|
| 533 |
if ( empty( $site_image['src'] ) ) { |
| 534 |
// When using the default blank image, use a different template in Social Image Generator. |
| 535 |
$template = 'highway'; |
| 536 |
$site_image['src'] = jetpack_og_get_site_fallback_blank_image(); |
| 537 |
} |
| 538 |
|
| 539 |
// Return the site image if we are in offline mode instead of attempting to generate a dynamic one. |
| 540 |
if ( ( new Status() )->is_offline_mode() ) { |
| 541 |
return $site_image; |
| 542 |
} |
| 543 |
|
| 544 |
/* |
| 545 |
* Only attempt to generate a dynamic fallback image |
| 546 |
* if we have a healthy connection to WPCOM. |
| 547 |
* and if the site has the right plan. |
| 548 |
*/ |
| 549 |
if ( |
| 550 |
( ( new Host() )->is_wpcom_simple() |
| 551 |
|| ( new Connection_Manager() )->is_connected() |
| 552 |
) |
| 553 |
&& Current_Plan::supports( 'social-image-generator' ) |
| 554 |
) { |
| 555 |
/** |
| 556 |
* Allow filtering the template to use with Social Image Generator. |
| 557 |
* Available templates: highway, dois, fullscreen, edge. |
| 558 |
* |
| 559 |
* @since 14.9 |
| 560 |
* |
| 561 |
* @param string $template The template to use. |
| 562 |
*/ |
| 563 |
$template = apply_filters( 'jetpack_og_fallback_social_image_template', $template ); |
| 564 |
|
| 565 |
// Let's generate the image. |
| 566 |
$site_image = jetpack_og_generate_fallback_social_image( $site_image, $template ); |
| 567 |
} |
| 568 |
|
| 569 |
return $site_image; |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Get the site's representative image. |
| 574 |
* |
| 575 |
* @since 14.9 |
| 576 |
* |
| 577 |
* @param int $width The width of the image. |
| 578 |
* @param int $height The height of the image. |
| 579 |
* |
| 580 |
* @return array The source ('src'), 'width', 'height', and source type of the image. |
| 581 |
*/ |
| 582 |
function jetpack_og_get_site_image( $width, $height ) { |
| 583 |
// First fall back, blavatar. |
| 584 |
if ( function_exists( 'blavatar_domain' ) ) { |
| 585 |
$blavatar_domain = blavatar_domain( site_url() ); |
| 586 |
if ( blavatar_exists( $blavatar_domain ) ) { |
| 587 |
return array( |
| 588 |
'src' => blavatar_url( $blavatar_domain, 'img', $width, false, true ), |
| 589 |
'width' => $width, |
| 590 |
'height' => $height, |
| 591 |
'type' => 'blavatar', |
| 592 |
); |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
// Second fall back, Site Logo. |
| 597 |
if ( |
| 598 |
function_exists( 'jetpack_has_site_logo' ) |
| 599 |
&& jetpack_has_site_logo() |
| 600 |
) { |
| 601 |
$image_id = jetpack_get_site_logo( 'id' ); |
| 602 |
$logo = wp_get_attachment_image_src( $image_id, 'full' ); |
| 603 |
if ( |
| 604 |
isset( $logo[0] ) && isset( $logo[1] ) && isset( $logo[2] ) |
| 605 |
&& ( _jetpack_og_get_image_validate_size( $logo[1], $logo[2], $width, $height ) ) |
| 606 |
) { |
| 607 |
return array( |
| 608 |
'src' => $logo[0], |
| 609 |
'width' => $logo[1], |
| 610 |
'height' => $logo[2], |
| 611 |
'type' => 'site_logo', |
| 612 |
); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
// Third fall back, Core's site logo. |
| 617 |
if ( has_custom_logo() ) { |
| 618 |
$custom_logo_id = get_theme_mod( 'custom_logo' ); |
| 619 |
$sl_details = wp_get_attachment_image_src( |
| 620 |
$custom_logo_id, |
| 621 |
'full' |
| 622 |
); |
| 623 |
if ( |
| 624 |
isset( $sl_details[0] ) && isset( $sl_details[1] ) && isset( $sl_details[2] ) |
| 625 |
&& ( _jetpack_og_get_image_validate_size( $sl_details[1], $sl_details[2], $width, $height ) ) |
| 626 |
) { |
| 627 |
return array( |
| 628 |
'src' => $sl_details[0], |
| 629 |
'width' => $sl_details[1], |
| 630 |
'height' => $sl_details[2], |
| 631 |
'alt_text' => Images::get_alt_text( $custom_logo_id ), |
| 632 |
); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
// Fourth fall back, Core Site Icon, if valid in size. |
| 637 |
if ( has_site_icon() ) { |
| 638 |
$image_id = get_option( 'site_icon' ); |
| 639 |
$icon = wp_get_attachment_image_src( $image_id, 'full' ); |
| 640 |
if ( |
| 641 |
isset( $icon[0] ) && isset( $icon[1] ) && isset( $icon[2] ) |
| 642 |
&& ( _jetpack_og_get_image_validate_size( $icon[1], $icon[2], $width, $height ) ) |
| 643 |
) { |
| 644 |
return array( |
| 645 |
'src' => $icon[0], |
| 646 |
'width' => $icon[1], |
| 647 |
'height' => $icon[2], |
| 648 |
'type' => 'site_icon', |
| 649 |
); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
return array( |
| 654 |
'src' => '', |
| 655 |
'width' => $width, |
| 656 |
'height' => $height, |
| 657 |
'type' => 'blank', |
| 658 |
); |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
* Get the site's fallback image. |
| 663 |
* |
| 664 |
* @since 14.9 |
| 665 |
* |
| 666 |
* @return string |
| 667 |
*/ |
| 668 |
function jetpack_og_get_site_fallback_blank_image() { |
| 669 |
/** |
| 670 |
* Filter the default Open Graph Image tag, used when no Image can be found in a post. |
| 671 |
* |
| 672 |
* @since 3.0.0 |
| 673 |
* |
| 674 |
* @param string $str Default Image URL. |
| 675 |
*/ |
| 676 |
return apply_filters( 'jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg' ); |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Get available templates for Social Image Generator. |
| 681 |
* |
| 682 |
* @since 14.9 |
| 683 |
* |
| 684 |
* @return array The available templates. |
| 685 |
*/ |
| 686 |
function jetpack_og_get_available_templates() { |
| 687 |
if ( ! class_exists( '\Automattic\Jetpack\Publicize\Social_Image_Generator\Templates' ) ) { |
| 688 |
return array(); |
| 689 |
} |
| 690 |
|
| 691 |
return \Automattic\Jetpack\Publicize\Social_Image_Generator\Templates::TEMPLATES; |
| 692 |
} |
| 693 |
|
| 694 |
/** |
| 695 |
* Get a social image token from Social Image Generator. |
| 696 |
* |
| 697 |
* @since 14.9 |
| 698 |
* |
| 699 |
* @param string $site_title The site title. |
| 700 |
* @param string $image_url The image URL. |
| 701 |
* @param string $template The template to use. |
| 702 |
* |
| 703 |
* @return string|WP_Error The social image token, or a WP_Error if the token could not be generated. |
| 704 |
*/ |
| 705 |
function jetpack_og_get_social_image_token( $site_title, $image_url, $template ) { |
| 706 |
// Let's check if we have a cached token. |
| 707 |
$cache_key = wp_hash( $site_title . $image_url . $template ); |
| 708 |
$transient_name = 'jetpack_og_social_image_token_' . $cache_key; |
| 709 |
$cached_token = get_transient( $transient_name ); |
| 710 |
|
| 711 |
if ( ! empty( $cached_token ) ) { |
| 712 |
return $cached_token; |
| 713 |
} |
| 714 |
|
| 715 |
/** |
| 716 |
* Filter the social image token for testing purposes. |
| 717 |
* |
| 718 |
* @since 14.9 |
| 719 |
* |
| 720 |
* @param string|WP_Error|null $token The token to return, or null to use default behavior. |
| 721 |
*/ |
| 722 |
$token = apply_filters( 'jetpack_og_get_social_image_token', null ); |
| 723 |
if ( null !== $token ) { |
| 724 |
return $token; |
| 725 |
} |
| 726 |
|
| 727 |
if ( ! function_exists( '\Automattic\Jetpack\Publicize\Social_Image_Generator\fetch_token' ) ) { |
| 728 |
return new WP_Error( 'jetpack_og_get_social_image_token_error', __( 'Social Image Generator is not available.', 'jetpack' ) ); |
| 729 |
} |
| 730 |
|
| 731 |
$token = \Automattic\Jetpack\Publicize\Social_Image_Generator\fetch_token( $site_title, $image_url, $template ); |
| 732 |
|
| 733 |
/* |
| 734 |
* We want to cache 2 types of responses: |
| 735 |
* - Successful responses with a token. |
| 736 |
* - WP_Error responses that denote a WordPress.com connection issue. |
| 737 |
*/ |
| 738 |
if ( |
| 739 |
! is_wp_error( $token ) |
| 740 |
|| ( |
| 741 |
is_wp_error( $token ) |
| 742 |
&& 'invalid_user_permission_publicize' === $token->get_error_code() |
| 743 |
) |
| 744 |
) { |
| 745 |
set_transient( $transient_name, $token, DAY_IN_SECONDS ); |
| 746 |
} |
| 747 |
|
| 748 |
return $token; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Generate and create a fallback social image. |
| 753 |
* |
| 754 |
* @since 14.9 |
| 755 |
* |
| 756 |
* @param array $representative_image The representative image of the site. |
| 757 |
* @param string $template The template to use. |
| 758 |
* |
| 759 |
* @return array The source ('src'), 'width', and 'height' of the image. |
| 760 |
*/ |
| 761 |
function jetpack_og_generate_fallback_social_image( $representative_image, $template ) { |
| 762 |
$site_title = get_bloginfo( 'name' ); |
| 763 |
$fallback_image = array( |
| 764 |
'src' => $representative_image['src'], |
| 765 |
'width' => $representative_image['width'], |
| 766 |
'height' => $representative_image['height'], |
| 767 |
); |
| 768 |
|
| 769 |
// Ensure that we use a valid template. |
| 770 |
if ( |
| 771 |
! in_array( |
| 772 |
$template, |
| 773 |
jetpack_og_get_available_templates(), |
| 774 |
true |
| 775 |
) |
| 776 |
) { |
| 777 |
$template = 'edge'; |
| 778 |
} |
| 779 |
|
| 780 |
// Let's generate the token matching the image.. |
| 781 |
$token = jetpack_og_get_social_image_token( $site_title, $representative_image['src'], $template ); |
| 782 |
|
| 783 |
if ( is_wp_error( $token ) ) { |
| 784 |
return $fallback_image; |
| 785 |
} |
| 786 |
|
| 787 |
// Build the image URL and return it. |
| 788 |
return array( |
| 789 |
'src' => sprintf( |
| 790 |
'https://s0.wp.com/_si/?t=%s', |
| 791 |
$token |
| 792 |
), |
| 793 |
'width' => 1200, |
| 794 |
'height' => 630, |
| 795 |
); |
| 796 |
} |
| 797 |
|
| 798 |
/** |
| 799 |
* Validate the width and height against required width and height |
| 800 |
* |
| 801 |
* @param int $width Width of the image. |
| 802 |
* @param int $height Height of the image. |
| 803 |
* @param int $req_width Required width to pass validation. |
| 804 |
* @param int $req_height Required height to pass validation. |
| 805 |
* |
| 806 |
* @return bool - True if the image passed the required size validation |
| 807 |
*/ |
| 808 |
function _jetpack_og_get_image_validate_size( $width, $height, $req_width, $req_height ) { |
| 809 |
if ( ! $width || ! $height ) { |
| 810 |
return false; |
| 811 |
} |
| 812 |
|
| 813 |
$valid_width = ( $width >= $req_width ); |
| 814 |
$valid_height = ( $height >= $req_height ); |
| 815 |
$is_image_acceptable = $valid_width && $valid_height; |
| 816 |
|
| 817 |
return $is_image_acceptable; |
| 818 |
} |
| 819 |
|
| 820 |
/** |
| 821 |
* Gets a gravatar URL of the specified size. |
| 822 |
* |
| 823 |
* @param string $email E-mail address to get gravatar for. |
| 824 |
* @param int $width Size of returned gravatar. |
| 825 |
* @return array|bool|mixed|string |
| 826 |
*/ |
| 827 |
function jetpack_og_get_image_gravatar( $email, $width ) { |
| 828 |
return get_avatar_url( |
| 829 |
$email, |
| 830 |
array( |
| 831 |
'size' => $width, |
| 832 |
) |
| 833 |
); |
| 834 |
} |
| 835 |
|
| 836 |
/** |
| 837 |
* Clean up text meant to be used as Description Open Graph tag. |
| 838 |
* |
| 839 |
* There should be: |
| 840 |
* - no links |
| 841 |
* - no shortcodes |
| 842 |
* - no html tags or their contents |
| 843 |
* - no content within wp:query blocks |
| 844 |
* - not too many words. |
| 845 |
* |
| 846 |
* @param string $description Text coming from WordPress (autogenerated or manually generated by author). |
| 847 |
* @param WP_Post|null $data Information about our post. |
| 848 |
* |
| 849 |
* @return string $description Cleaned up description string. |
| 850 |
*/ |
| 851 |
function jetpack_og_get_description( $description = '', $data = null ) { |
| 852 |
// Remove content within wp:query blocks. |
| 853 |
$description = jetpack_og_remove_query_blocks( $description ); |
| 854 |
|
| 855 |
// Remove tags such as <style or <script. |
| 856 |
$description = wp_strip_all_tags( $description ); |
| 857 |
|
| 858 |
/* |
| 859 |
* Clean up any plain text entities left into formatted entities. |
| 860 |
* Intentionally not using a filter to prevent pollution. |
| 861 |
* @see https://github.com/Automattic/jetpack/pull/2899#issuecomment-151957382 |
| 862 |
*/ |
| 863 |
$description = wp_kses( |
| 864 |
trim( |
| 865 |
convert_chars( |
| 866 |
wptexturize( $description ) |
| 867 |
) |
| 868 |
), |
| 869 |
array() |
| 870 |
); |
| 871 |
|
| 872 |
// Remove shortcodes. |
| 873 |
$description = strip_shortcodes( $description ); |
| 874 |
|
| 875 |
// Remove links. |
| 876 |
$description = preg_replace( |
| 877 |
'@https?://[\S]+@', |
| 878 |
'', |
| 879 |
$description |
| 880 |
); |
| 881 |
|
| 882 |
/* |
| 883 |
* Limit things to a small text blurb. |
| 884 |
* There isn't a hard limit set by Facebook, so let's rely on WP's own limit. |
| 885 |
* (55 words or the localized equivalent). |
| 886 |
* This limit can be customized with the wp_trim_words filter. |
| 887 |
*/ |
| 888 |
$description = wp_trim_words( $description ); |
| 889 |
|
| 890 |
// Let's set a default if we have no text by now. |
| 891 |
if ( empty( $description ) ) { |
| 892 |
/** |
| 893 |
* Filter the fallback `og:description` used when no excerpt information is provided. |
| 894 |
* |
| 895 |
* @module sharedaddy, publicize |
| 896 |
* |
| 897 |
* @since 3.9.0 |
| 898 |
* |
| 899 |
* @param string $var Fallback og:description. Default is translated `Visit the post for more'. |
| 900 |
* @param object $data Post object for the current post. |
| 901 |
*/ |
| 902 |
$description = apply_filters( |
| 903 |
'jetpack_open_graph_fallback_description', |
| 904 |
__( 'Visit the post for more.', 'jetpack' ), |
| 905 |
$data |
| 906 |
); |
| 907 |
} |
| 908 |
|
| 909 |
return $description; |
| 910 |
} |
| 911 |
|
| 912 |
/** |
| 913 |
* Remove content within wp:query blocks from the description. |
| 914 |
* |
| 915 |
* @since 14.9 |
| 916 |
* |
| 917 |
* @param string $description The description text that may contain block markup. |
| 918 |
* @return string The description with wp:query blocks removed. |
| 919 |
*/ |
| 920 |
function jetpack_og_remove_query_blocks( $description ) { |
| 921 |
// Handle non-string input |
| 922 |
if ( ! is_string( $description ) ) { |
| 923 |
return ''; |
| 924 |
} |
| 925 |
|
| 926 |
$output = ''; |
| 927 |
$offset = 0; |
| 928 |
$depth = 0; |
| 929 |
$in_query_block = false; |
| 930 |
|
| 931 |
$scanner = Block_Scanner::create( $description ); |
| 932 |
if ( ! $scanner ) { |
| 933 |
return $description; |
| 934 |
} |
| 935 |
|
| 936 |
while ( $scanner->next_delimiter() ) { |
| 937 |
$span = $scanner->get_span(); |
| 938 |
$match_at = $span->start; |
| 939 |
$length = $span->length; |
| 940 |
|
| 941 |
// Check if this is a query block. |
| 942 |
if ( $scanner->is_block_type( 'query' ) ) { |
| 943 |
switch ( $scanner->get_delimiter_type() ) { |
| 944 |
case Block_Scanner::OPENER: |
| 945 |
if ( ! $in_query_block ) { |
| 946 |
// Copy content before the query block. |
| 947 |
$output .= substr( $description, $offset, $match_at - $offset ); |
| 948 |
$in_query_block = true; |
| 949 |
} |
| 950 |
++$depth; |
| 951 |
break; |
| 952 |
|
| 953 |
case Block_Scanner::CLOSER: |
| 954 |
--$depth; |
| 955 |
if ( $in_query_block && $depth === 0 ) { |
| 956 |
// We've exited the query block, continue from after it. |
| 957 |
$in_query_block = false; |
| 958 |
$offset = $match_at + $length; |
| 959 |
|
| 960 |
// Remove extra newline if present |
| 961 |
if ( |
| 962 |
str_starts_with( substr( $description, $offset ), "\n" ) |
| 963 |
&& str_ends_with( $output, "\n" ) |
| 964 |
) { |
| 965 |
++$offset; |
| 966 |
} |
| 967 |
} |
| 968 |
break; |
| 969 |
|
| 970 |
case Block_Scanner::VOID: |
| 971 |
// Void query blocks should be removed entirely. |
| 972 |
if ( ! $in_query_block ) { |
| 973 |
$output .= substr( $description, $offset, $match_at - $offset ); |
| 974 |
$offset = $match_at + $length; |
| 975 |
// Remove extra newline if present |
| 976 |
if ( |
| 977 |
str_starts_with( substr( $description, $offset ), "\n" ) |
| 978 |
&& str_ends_with( $output, "\n" ) |
| 979 |
) { |
| 980 |
++$offset; |
| 981 |
} |
| 982 |
} |
| 983 |
break; |
| 984 |
} |
| 985 |
} elseif ( ! $in_query_block ) { |
| 986 |
// Not a query block, copy content including the delimiter if we're not inside a query block. |
| 987 |
$output .= substr( $description, $offset, $match_at - $offset + $length ); |
| 988 |
$offset = $match_at + $length; |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
// Add any remaining content after the last delimiter. |
| 993 |
if ( ! $in_query_block ) { |
| 994 |
$output .= substr( $description, $offset ); |
| 995 |
} |
| 996 |
|
| 997 |
return $output; |
| 998 |
} |
| 999 |
|
| 1000 |
/** |
| 1001 |
* Display a Fediverse actor Open Graph tag when the post author has a Mastodon connection. |
| 1002 |
* |
| 1003 |
* @see https://blog.joinmastodon.org/2024/07/highlighting-journalism-on-mastodon/ |
| 1004 |
* |
| 1005 |
* @since 13.8 |
| 1006 |
* |
| 1007 |
* @param array $tags Current tags. |
| 1008 |
* |
| 1009 |
* @return array |
| 1010 |
*/ |
| 1011 |
function jetpack_add_fediverse_creator_open_graph_tag( $tags ) { |
| 1012 |
/* |
| 1013 |
* Let's not do this on WordPress.com Simple for now, |
| 1014 |
* because of its performance impact. |
| 1015 |
* See p1723574138779019/1723572983.803009-slack-C01U2KGS2PQ |
| 1016 |
*/ |
| 1017 |
if ( ( new Host() )->is_wpcom_simple() ) { |
| 1018 |
return $tags; |
| 1019 |
} |
| 1020 |
|
| 1021 |
// Let's not add any tags when the ActivityPub plugin already adds its own. |
| 1022 |
$is_activitypub_opengraph_integration_active = get_option( 'activitypub_use_opengraph' ); |
| 1023 |
if ( $is_activitypub_opengraph_integration_active ) { |
| 1024 |
return $tags; |
| 1025 |
} |
| 1026 |
|
| 1027 |
// We pull the Mastodon connection data from Publicize. |
| 1028 |
if ( ! function_exists( 'publicize_init' ) ) { |
| 1029 |
return $tags; |
| 1030 |
} |
| 1031 |
$publicize = publicize_init(); |
| 1032 |
|
| 1033 |
global $post; |
| 1034 |
if ( |
| 1035 |
! is_singular() |
| 1036 |
|| ! $post instanceof WP_Post |
| 1037 |
|| ! isset( $post->ID ) |
| 1038 |
|| empty( $post->post_author ) |
| 1039 |
) { |
| 1040 |
return $tags; |
| 1041 |
} |
| 1042 |
|
| 1043 |
$post_mastodon_connections = array(); |
| 1044 |
|
| 1045 |
// Loop through active connections. |
| 1046 |
foreach ( (array) $publicize->get_services( 'connected' ) as $service_name => $connections ) { |
| 1047 |
if ( 'mastodon' !== $service_name ) { |
| 1048 |
continue; |
| 1049 |
} |
| 1050 |
|
| 1051 |
// services can have multiple connections. Store them all in our array. |
| 1052 |
foreach ( $connections as $connection ) { |
| 1053 |
$connection_id = $publicize->get_connection_id( $connection ); |
| 1054 |
$connection_meta = $publicize->get_connection_meta( $connection ); |
| 1055 |
|
| 1056 |
$connection_data = $connection_meta['connection_data'] ?? array(); |
| 1057 |
$mastodon_handle = $connection_meta['external_display'] ?? ''; |
| 1058 |
$connection_user_id = $connection_data['user_id'] ?? 0; |
| 1059 |
|
| 1060 |
if ( empty( $mastodon_handle ) ) { |
| 1061 |
continue; |
| 1062 |
} |
| 1063 |
|
| 1064 |
// Did we skip this connection for this post? |
| 1065 |
if ( get_post_meta( $post->ID, $publicize->POST_SKIP_PUBLICIZE . $connection_id, true ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 1066 |
continue; |
| 1067 |
} |
| 1068 |
|
| 1069 |
$post_mastodon_connections[] = array( |
| 1070 |
'user_id' => (int) $connection_user_id, |
| 1071 |
'connection_id' => (int) $connection_id, |
| 1072 |
'handle' => $mastodon_handle, |
| 1073 |
'global' => 0 === (int) $connection_user_id, |
| 1074 |
); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
// If we have no Mastodon connections, skip. |
| 1079 |
if ( empty( $post_mastodon_connections ) ) { |
| 1080 |
return $tags; |
| 1081 |
} |
| 1082 |
|
| 1083 |
/* |
| 1084 |
* Select a single Mastodon connection to use. |
| 1085 |
* It should be either the first connection belonging to the post author, |
| 1086 |
* or the first global connection. |
| 1087 |
*/ |
| 1088 |
foreach ( $post_mastodon_connections as $mastodon_connection ) { |
| 1089 |
if ( (int) $post->post_author === $mastodon_connection['user_id'] ) { |
| 1090 |
$tags['fediverse:creator'] = esc_attr( $mastodon_connection['handle'] ); |
| 1091 |
break; |
| 1092 |
} |
| 1093 |
|
| 1094 |
if ( $mastodon_connection['global'] ) { |
| 1095 |
$tags['fediverse:creator'] = esc_attr( $mastodon_connection['handle'] ); |
| 1096 |
break; |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
return $tags; |
| 1101 |
} |
| 1102 |
|
| 1103 |
/** |
| 1104 |
* Update the markup for the Open Graph tag to match the expected output for Mastodon |
| 1105 |
* (name instead of property). |
| 1106 |
* |
| 1107 |
* @since 13.8 |
| 1108 |
* |
| 1109 |
* @param string $og_tag A single OG tag. |
| 1110 |
* |
| 1111 |
* @return string Result of the OG tag. |
| 1112 |
*/ |
| 1113 |
function jetpack_filter_fediverse_cards_output( $og_tag ) { |
| 1114 |
return ( str_contains( $og_tag, 'fediverse:' ) ) ? preg_replace( '/property="([^"]+)"/', 'name="\1"', $og_tag ) : $og_tag; |
| 1115 |
} |
| 1116 |
|