functions.opengraph.php
| 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\Jetpack\Status\Host; |
| 15 | |
| 16 | add_action( 'wp_head', 'jetpack_og_tags' ); |
| 17 | add_action( 'web_stories_story_head', 'jetpack_og_tags' ); |
| 18 | |
| 19 | // Add a Fediverse Open Graph Tag when an author has connected their Mastodon account. |
| 20 | add_filter( 'jetpack_open_graph_tags', 'jetpack_add_fediverse_creator_open_graph_tag', 10, 1 ); |
| 21 | add_filter( 'jetpack_open_graph_output', 'jetpack_filter_fediverse_cards_output', 10, 1 ); |
| 22 | |
| 23 | /** |
| 24 | * Outputs Open Graph tags generated by Jetpack. |
| 25 | */ |
| 26 | function jetpack_og_tags() { |
| 27 | global $post; |
| 28 | $data = $post; // so that we don't accidentally explode the global. |
| 29 | |
| 30 | $is_amp_response = ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ); |
| 31 | |
| 32 | // Disable the widont filter on WP.com to avoid stray  s. |
| 33 | $disable_widont = remove_filter( 'the_title', 'widont' ); |
| 34 | |
| 35 | $og_output = "\n"; |
| 36 | if ( ! $is_amp_response ) { // Because AMP optimizes the order or the nodes in the head. |
| 37 | $og_output .= "<!-- Jetpack Open Graph Tags -->\n"; |
| 38 | } |
| 39 | $tags = array(); |
| 40 | |
| 41 | /** |
| 42 | * Filter the minimum width 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 width used in Jetpack Open Graph Meta Tags. |
| 49 | */ |
| 50 | $image_width = absint( apply_filters( 'jetpack_open_graph_image_width', 200 ) ); |
| 51 | /** |
| 52 | * Filter the minimum height of the images used in Jetpack Open Graph Meta Tags. |
| 53 | * |
| 54 | * @module sharedaddy, publicize |
| 55 | * |
| 56 | * @since 2.0.0 |
| 57 | * |
| 58 | * @param int 200 Minimum image height used in Jetpack Open Graph Meta Tags. |
| 59 | */ |
| 60 | $image_height = absint( apply_filters( 'jetpack_open_graph_image_height', 200 ) ); |
| 61 | $description_length = 197; |
| 62 | |
| 63 | if ( is_home() || is_front_page() ) { |
| 64 | $site_type = Jetpack_Options::get_option_and_ensure_autoload( 'open_graph_protocol_site_type', '' ); |
| 65 | $tags['og:type'] = ! empty( $site_type ) ? $site_type : 'website'; |
| 66 | $tags['og:title'] = get_bloginfo( 'name' ); |
| 67 | $tags['og:description'] = get_bloginfo( 'description' ); |
| 68 | |
| 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 | |
| 76 | // Associate a blog's root path with one or more Facebook accounts. |
| 77 | $facebook_admins = Jetpack_Options::get_option_and_ensure_autoload( 'facebook_admins', array() ); |
| 78 | if ( ! empty( $facebook_admins ) ) { |
| 79 | $tags['fb:admins'] = $facebook_admins; |
| 80 | } |
| 81 | } elseif ( is_author() ) { |
| 82 | $tags['og:type'] = 'profile'; |
| 83 | |
| 84 | $author = get_queried_object(); |
| 85 | |
| 86 | if ( is_a( $author, 'WP_User' ) ) { |
| 87 | $tags['og:title'] = $author->display_name; |
| 88 | if ( ! empty( $author->user_url ) ) { |
| 89 | $tags['og:url'] = $author->user_url; |
| 90 | } else { |
| 91 | $tags['og:url'] = get_author_posts_url( $author->ID ); |
| 92 | } |
| 93 | $tags['og:description'] = $author->description; |
| 94 | $tags['profile:first_name'] = get_the_author_meta( 'first_name', $author->ID ); |
| 95 | $tags['profile:last_name'] = get_the_author_meta( 'last_name', $author->ID ); |
| 96 | } |
| 97 | } elseif ( is_archive() ) { |
| 98 | $tags['og:type'] = 'website'; |
| 99 | $tags['og:title'] = wp_get_document_title(); |
| 100 | |
| 101 | $archive = get_queried_object(); |
| 102 | if ( $archive instanceof WP_Term ) { |
| 103 | $tags['og:url'] = get_term_link( $archive->term_id, $archive->taxonomy ); |
| 104 | $tags['og:description'] = $archive->description; |
| 105 | } elseif ( ! empty( $archive ) && is_post_type_archive() ) { |
| 106 | $tags['og:url'] = get_post_type_archive_link( $archive->name ); |
| 107 | $tags['og:description'] = $archive->description; |
| 108 | } |
| 109 | } elseif ( is_singular() && is_a( $data, 'WP_Post' ) ) { |
| 110 | $tags['og:type'] = 'article'; |
| 111 | if ( empty( $data->post_title ) ) { |
| 112 | $tags['og:title'] = ' '; |
| 113 | } else { |
| 114 | /** This filter is documented in core/src/wp-includes/post-template.php */ |
| 115 | $tags['og:title'] = wp_kses( apply_filters( 'the_title', $data->post_title, $data->ID ), array() ); |
| 116 | } |
| 117 | |
| 118 | $tags['og:url'] = get_permalink( $data->ID ); |
| 119 | if ( ! post_password_required() ) { |
| 120 | /* |
| 121 | * If the post author set an excerpt, use that. |
| 122 | * Otherwise, pick the post content that comes before the More tag if there is one. |
| 123 | * Do not use the post content if it contains premium content. |
| 124 | */ |
| 125 | if ( ! empty( $data->post_excerpt ) ) { |
| 126 | $tags['og:description'] = jetpack_og_get_description( $data->post_excerpt ); |
| 127 | } elseif ( ! has_block( 'premium-content/container', $data->post_content ) ) { |
| 128 | $excerpt = explode( '<!--more-->', $data->post_content )[0]; |
| 129 | $tags['og:description'] = jetpack_og_get_description( $excerpt ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | $tags['article:published_time'] = gmdate( 'c', strtotime( $data->post_date_gmt ) ); |
| 134 | $tags['article:modified_time'] = gmdate( 'c', strtotime( $data->post_modified_gmt ) ); |
| 135 | if ( post_type_supports( get_post_type( $data ), 'author' ) && isset( $data->post_author ) ) { |
| 136 | $publicize_facebook_user = get_post_meta( $data->ID, '_publicize_facebook_user', true ); |
| 137 | if ( ! empty( $publicize_facebook_user ) ) { |
| 138 | $tags['article:author'] = esc_url( $publicize_facebook_user ); |
| 139 | } |
| 140 | } |
| 141 | } elseif ( is_search() ) { |
| 142 | if ( '' !== get_query_var( 's', '' ) ) { |
| 143 | $tags['og:title'] = wp_get_document_title(); |
| 144 | } |
| 145 | } |
| 146 | /** |
| 147 | * Allow plugins to inject additional template-specific Open Graph tags. |
| 148 | * |
| 149 | * @module sharedaddy, publicize |
| 150 | * |
| 151 | * @since 3.0.0 |
| 152 | * |
| 153 | * @param array $tags Array of Open Graph Meta tags. |
| 154 | * @param array $args Array of image size parameters. |
| 155 | */ |
| 156 | $tags = apply_filters( 'jetpack_open_graph_base_tags', $tags, compact( 'image_width', 'image_height' ) ); |
| 157 | |
| 158 | // Re-enable widont if we had disabled it. |
| 159 | if ( $disable_widont ) { |
| 160 | add_filter( 'the_title', 'widont' ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Do not return any Open Graph Meta tags if we don't have any info about a post. |
| 165 | * |
| 166 | * @module sharedaddy, publicize |
| 167 | * |
| 168 | * @since 3.0.0 |
| 169 | * |
| 170 | * @param bool true Do not return any Open Graph Meta tags if we don't have any info about a post. |
| 171 | */ |
| 172 | if ( empty( $tags ) && apply_filters( 'jetpack_open_graph_return_if_empty', true ) ) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | $tags['og:site_name'] = get_bloginfo( 'name' ); |
| 177 | |
| 178 | // Get image info and build tags. |
| 179 | if ( ! post_password_required() ) { |
| 180 | $image_info = jetpack_og_get_image( $image_width, $image_height ); |
| 181 | $tags['og:image'] = $image_info['src']; |
| 182 | |
| 183 | if ( ! empty( $image_info['width'] ) ) { |
| 184 | $tags['og:image:width'] = (int) $image_info['width']; |
| 185 | } |
| 186 | if ( ! empty( $image_info['height'] ) ) { |
| 187 | $tags['og:image:height'] = (int) $image_info['height']; |
| 188 | } |
| 189 | // If we have an image, add the alt text even if it's empty. |
| 190 | if ( ! empty( $image_info['src'] ) && isset( $image_info['alt_text'] ) ) { |
| 191 | $tags['og:image:alt'] = esc_attr( $image_info['alt_text'] ); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Facebook whines if you give it an empty title. |
| 196 | if ( empty( $tags['og:title'] ) ) { |
| 197 | $tags['og:title'] = __( '(no title)', 'jetpack' ); |
| 198 | } |
| 199 | |
| 200 | // Shorten the description if it's too long. |
| 201 | if ( isset( $tags['og:description'] ) ) { |
| 202 | $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '…' : $tags['og:description']; |
| 203 | } |
| 204 | |
| 205 | // Try to add OG locale tag if the WP->FB data mapping exists. |
| 206 | if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
| 207 | require_once JETPACK__GLOTPRESS_LOCALES_PATH; |
| 208 | $_locale = get_locale(); |
| 209 | |
| 210 | // We have to account for w.org vs WP.com locale divergence. |
| 211 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 212 | $gp_locale = GP_Locales::by_field( 'slug', $_locale ); |
| 213 | } else { |
| 214 | $gp_locale = GP_Locales::by_field( 'wp_locale', $_locale ); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if ( isset( $gp_locale->facebook_locale ) && ! empty( $gp_locale->facebook_locale ) ) { |
| 219 | $tags['og:locale'] = $gp_locale->facebook_locale; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Allow the addition of additional Open Graph Meta tags, or modify the existing tags. |
| 224 | * |
| 225 | * @module sharedaddy, publicize |
| 226 | * |
| 227 | * @since 2.0.0 |
| 228 | * |
| 229 | * @param array $tags Array of Open Graph Meta tags. |
| 230 | * @param array $args Array of image size parameters. |
| 231 | */ |
| 232 | $tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) ); |
| 233 | |
| 234 | // secure_urls need to go right after each og:image to work properly so we will abstract them here. |
| 235 | $tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url']; |
| 236 | $secure = $tags['og:image:secure_url']; |
| 237 | unset( $tags['og:image:secure_url'] ); |
| 238 | $secure_image_num = 0; |
| 239 | |
| 240 | $allowed_empty_tags = array( |
| 241 | 'og:image:alt', |
| 242 | ); |
| 243 | |
| 244 | foreach ( (array) $tags as $tag_property => $tag_content ) { |
| 245 | // to accommodate multiple images. |
| 246 | $tag_content = (array) $tag_content; |
| 247 | $tag_content = array_unique( array_filter( $tag_content, 'is_scalar' ) ); |
| 248 | |
| 249 | foreach ( $tag_content as $tag_content_single ) { |
| 250 | if ( empty( $tag_content_single ) && ! in_array( $tag_property, $allowed_empty_tags, true ) ) { |
| 251 | continue; // Only allow certain empty tags. |
| 252 | } |
| 253 | |
| 254 | switch ( $tag_property ) { |
| 255 | case 'og:url': |
| 256 | case 'og:image': |
| 257 | case 'og:image:url': |
| 258 | case 'og:image:secure_url': |
| 259 | case 'og:audio': |
| 260 | case 'og:audio:url': |
| 261 | case 'og:audio:secure_url': |
| 262 | case 'og:video': |
| 263 | case 'og:video:url': |
| 264 | case 'og:video:secure_url': |
| 265 | $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_url( $tag_content_single ) ); |
| 266 | break; |
| 267 | default: |
| 268 | $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) ); |
| 269 | } |
| 270 | /** |
| 271 | * Filter the HTML Output of each Open Graph Meta tag. |
| 272 | * |
| 273 | * @module sharedaddy, publicize |
| 274 | * |
| 275 | * @since 2.0.0 |
| 276 | * |
| 277 | * @param string $og_tag HTML HTML Output of each Open Graph Meta tag. |
| 278 | */ |
| 279 | $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag ); |
| 280 | $og_output .= "\n"; |
| 281 | |
| 282 | if ( 'og:image' === $tag_property ) { |
| 283 | if ( is_array( $secure ) && ! empty( $secure[ $secure_image_num ] ) ) { |
| 284 | $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) ); |
| 285 | /** This filter is documented in functions.opengraph.php */ |
| 286 | $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag ); |
| 287 | $og_output .= "\n"; |
| 288 | } elseif ( ! is_array( $secure ) && ! empty( $secure ) ) { |
| 289 | $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) ); |
| 290 | /** This filter is documented in functions.opengraph.php */ |
| 291 | $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag ); |
| 292 | $og_output .= "\n"; |
| 293 | } |
| 294 | ++$secure_image_num; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if ( ! $is_amp_response ) { // Because AMP optimizes the order or the nodes in the head. |
| 300 | $og_output .= "\n<!-- End Jetpack Open Graph Tags -->"; |
| 301 | } |
| 302 | $og_output .= "\n"; |
| 303 | // This is trusted output or added by a filter. |
| 304 | echo $og_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Returns an image used in social shares. |
| 309 | * |
| 310 | * @since 2.0.0 |
| 311 | * |
| 312 | * @param int $width Minimum width for the image. Default is 200 based on Facebook's requirement. |
| 313 | * @param int $height Minimum height for the image. Default is 200 based on Facebook's requirement. |
| 314 | * @param null $deprecated Deprecated. |
| 315 | * |
| 316 | * @return array The source ('src'), 'width', and 'height' of the image. |
| 317 | */ |
| 318 | function jetpack_og_get_image( $width = 200, $height = 200, $deprecated = null ) { |
| 319 | if ( ! empty( $deprecated ) ) { |
| 320 | _deprecated_argument( __FUNCTION__, 'jetpack-6.6.0' ); |
| 321 | } |
| 322 | $image = array(); |
| 323 | |
| 324 | if ( is_singular() && ! is_home() ) { |
| 325 | // Grab obvious image if post is an attachment page for an image. |
| 326 | if ( is_attachment( get_the_ID() ) && str_starts_with( get_post_mime_type(), 'image' ) ) { |
| 327 | $image['src'] = wp_get_attachment_url( get_the_ID() ); |
| 328 | $image['alt_text'] = Jetpack_PostImages::get_alt_text( get_the_ID() ); |
| 329 | } |
| 330 | |
| 331 | // Attempt to find something good for this post using our generalized PostImages code. |
| 332 | if ( empty( $image ) && class_exists( 'Jetpack_PostImages' ) ) { |
| 333 | $post_image = Jetpack_PostImages::get_image( |
| 334 | get_the_ID(), |
| 335 | array( |
| 336 | 'width' => $width, |
| 337 | 'height' => $height, |
| 338 | ) |
| 339 | ); |
| 340 | if ( ! empty( $post_image ) && is_array( $post_image ) ) { |
| 341 | $image['src'] = $post_image['src']; |
| 342 | if ( isset( $post_image['src_width'] ) && isset( $post_image['src_height'] ) ) { |
| 343 | $image['width'] = $post_image['src_width']; |
| 344 | $image['height'] = $post_image['src_height']; |
| 345 | } |
| 346 | if ( ! empty( $post_image['alt_text'] ) ) { |
| 347 | $image['alt_text'] = $post_image['alt_text']; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | } elseif ( is_author() ) { |
| 352 | $author = get_queried_object(); |
| 353 | if ( is_a( $author, 'WP_User' ) ) { |
| 354 | $image['src'] = get_avatar_url( |
| 355 | $author->user_email, |
| 356 | array( |
| 357 | 'size' => $width, |
| 358 | ) |
| 359 | ); |
| 360 | $image['alt_text'] = $author->display_name; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // First fall back, blavatar. |
| 365 | if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) { |
| 366 | $blavatar_domain = blavatar_domain( site_url() ); |
| 367 | if ( blavatar_exists( $blavatar_domain ) ) { |
| 368 | $image['src'] = blavatar_url( $blavatar_domain, 'img', $width, false, true ); |
| 369 | $image['width'] = $width; |
| 370 | $image['height'] = $height; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // Second fall back, Site Logo. |
| 375 | if ( empty( $image ) && ( function_exists( 'jetpack_has_site_logo' ) && jetpack_has_site_logo() ) ) { |
| 376 | $image_id = jetpack_get_site_logo( 'id' ); |
| 377 | $logo = wp_get_attachment_image_src( $image_id, 'full' ); |
| 378 | if ( |
| 379 | isset( $logo[0] ) && isset( $logo[1] ) && isset( $logo[2] ) |
| 380 | && ( _jetpack_og_get_image_validate_size( $logo[1], $logo[2], $width, $height ) ) |
| 381 | ) { |
| 382 | $image['src'] = $logo[0]; |
| 383 | $image['width'] = $logo[1]; |
| 384 | $image['height'] = $logo[2]; |
| 385 | $image['alt_text'] = Jetpack_PostImages::get_alt_text( $image_id ); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // Third fall back, Core Site Icon, if valid in size. |
| 390 | if ( empty( $image ) && has_site_icon() ) { |
| 391 | $image_id = get_option( 'site_icon' ); |
| 392 | $icon = wp_get_attachment_image_src( $image_id, 'full' ); |
| 393 | if ( |
| 394 | isset( $icon[0] ) && isset( $icon[1] ) && isset( $icon[2] ) |
| 395 | && ( _jetpack_og_get_image_validate_size( $icon[1], $icon[2], $width, $height ) ) |
| 396 | ) { |
| 397 | $image['src'] = $icon[0]; |
| 398 | $image['width'] = $icon[1]; |
| 399 | $image['height'] = $icon[2]; |
| 400 | $image['alt_text'] = Jetpack_PostImages::get_alt_text( $image_id ); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // Final fall back, blank image. |
| 405 | if ( empty( $image ) ) { |
| 406 | /** |
| 407 | * Filter the default Open Graph Image tag, used when no Image can be found in a post. |
| 408 | * |
| 409 | * @since 3.0.0 |
| 410 | * |
| 411 | * @param string $str Default Image URL. |
| 412 | */ |
| 413 | $image['src'] = apply_filters( 'jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg' ); |
| 414 | } |
| 415 | |
| 416 | // If we didn't get an explicit alt tag from the image, set a default. |
| 417 | if ( empty( $image['alt_text'] ) ) { |
| 418 | /** |
| 419 | * Filter the default Open Graph image alt text, used when the Open Graph image from the post does not have an alt text. |
| 420 | * |
| 421 | * @since 10.4 |
| 422 | * |
| 423 | * @param string $str Default Open Graph image alt text. |
| 424 | */ |
| 425 | $image['alt_text'] = apply_filters( 'jetpack_open_graph_image_default_alt_text', '' ); |
| 426 | } |
| 427 | |
| 428 | return $image; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Validate the width and height against required width and height |
| 433 | * |
| 434 | * @param int $width Width of the image. |
| 435 | * @param int $height Height of the image. |
| 436 | * @param int $req_width Required width to pass validation. |
| 437 | * @param int $req_height Required height to pass validation. |
| 438 | * |
| 439 | * @return bool - True if the image passed the required size validation |
| 440 | */ |
| 441 | function _jetpack_og_get_image_validate_size( $width, $height, $req_width, $req_height ) { |
| 442 | if ( ! $width || ! $height ) { |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | $valid_width = ( $width >= $req_width ); |
| 447 | $valid_height = ( $height >= $req_height ); |
| 448 | $is_image_acceptable = $valid_width && $valid_height; |
| 449 | |
| 450 | return $is_image_acceptable; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Gets a gravatar URL of the specified size. |
| 455 | * |
| 456 | * @param string $email E-mail address to get gravatar for. |
| 457 | * @param int $width Size of returned gravatar. |
| 458 | * @return array|bool|mixed|string |
| 459 | */ |
| 460 | function jetpack_og_get_image_gravatar( $email, $width ) { |
| 461 | return get_avatar_url( |
| 462 | $email, |
| 463 | array( |
| 464 | 'size' => $width, |
| 465 | ) |
| 466 | ); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Clean up text meant to be used as Description Open Graph tag. |
| 471 | * |
| 472 | * There should be: |
| 473 | * - no links |
| 474 | * - no shortcodes |
| 475 | * - no html tags or their contents |
| 476 | * - not too many words. |
| 477 | * |
| 478 | * @param string $description Text coming from WordPress (autogenerated or manually generated by author). |
| 479 | * @param WP_Post|null $data Information about our post. |
| 480 | * |
| 481 | * @return string $description Cleaned up description string. |
| 482 | */ |
| 483 | function jetpack_og_get_description( $description = '', $data = null ) { |
| 484 | // Remove tags such as <style or <script. |
| 485 | $description = wp_strip_all_tags( $description ); |
| 486 | |
| 487 | /* |
| 488 | * Clean up any plain text entities left into formatted entities. |
| 489 | * Intentionally not using a filter to prevent pollution. |
| 490 | * @see https://github.com/Automattic/jetpack/pull/2899#issuecomment-151957382 |
| 491 | */ |
| 492 | $description = wp_kses( |
| 493 | trim( |
| 494 | convert_chars( |
| 495 | wptexturize( $description ) |
| 496 | ) |
| 497 | ), |
| 498 | array() |
| 499 | ); |
| 500 | |
| 501 | // Remove shortcodes. |
| 502 | $description = strip_shortcodes( $description ); |
| 503 | |
| 504 | // Remove links. |
| 505 | $description = preg_replace( |
| 506 | '@https?://[\S]+@', |
| 507 | '', |
| 508 | $description |
| 509 | ); |
| 510 | |
| 511 | /* |
| 512 | * Limit things to a small text blurb. |
| 513 | * There isn't a hard limit set by Facebook, so let's rely on WP's own limit. |
| 514 | * (55 words or the localized equivalent). |
| 515 | * This limit can be customized with the wp_trim_words filter. |
| 516 | */ |
| 517 | $description = wp_trim_words( $description ); |
| 518 | |
| 519 | // Let's set a default if we have no text by now. |
| 520 | if ( empty( $description ) ) { |
| 521 | /** |
| 522 | * Filter the fallback `og:description` used when no excerpt information is provided. |
| 523 | * |
| 524 | * @module sharedaddy, publicize |
| 525 | * |
| 526 | * @since 3.9.0 |
| 527 | * |
| 528 | * @param string $var Fallback og:description. Default is translated `Visit the post for more'. |
| 529 | * @param object $data Post object for the current post. |
| 530 | */ |
| 531 | $description = apply_filters( |
| 532 | 'jetpack_open_graph_fallback_description', |
| 533 | __( 'Visit the post for more.', 'jetpack' ), |
| 534 | $data |
| 535 | ); |
| 536 | } |
| 537 | |
| 538 | return $description; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Display a Fediverse actor Open Graph tag when the post author has a Mastodon connection. |
| 543 | * |
| 544 | * @see https://blog.joinmastodon.org/2024/07/highlighting-journalism-on-mastodon/ |
| 545 | * |
| 546 | * @since 13.8 |
| 547 | * |
| 548 | * @param array $tags Current tags. |
| 549 | * |
| 550 | * @return array |
| 551 | */ |
| 552 | function jetpack_add_fediverse_creator_open_graph_tag( $tags ) { |
| 553 | /* |
| 554 | * Let's not do this on WordPress.com Simple for now, |
| 555 | * because of its performance impact. |
| 556 | * See p1723574138779019/1723572983.803009-slack-C01U2KGS2PQ |
| 557 | */ |
| 558 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 559 | return $tags; |
| 560 | } |
| 561 | |
| 562 | // Let's not add any tags when the ActivityPub plugin already adds its own. |
| 563 | $is_activitypub_opengraph_integration_active = get_option( 'activitypub_use_opengraph' ); |
| 564 | if ( $is_activitypub_opengraph_integration_active ) { |
| 565 | return $tags; |
| 566 | } |
| 567 | |
| 568 | // We pull the Mastodon connection data from Publicize. |
| 569 | if ( ! function_exists( 'publicize_init' ) ) { |
| 570 | return $tags; |
| 571 | } |
| 572 | $publicize = publicize_init(); |
| 573 | |
| 574 | global $post; |
| 575 | if ( |
| 576 | ! is_singular() |
| 577 | || ! $post instanceof WP_Post |
| 578 | || ! isset( $post->ID ) |
| 579 | || empty( $post->post_author ) |
| 580 | ) { |
| 581 | return $tags; |
| 582 | } |
| 583 | |
| 584 | $post_mastodon_connections = array(); |
| 585 | |
| 586 | // Loop through active connections. |
| 587 | foreach ( (array) $publicize->get_services( 'connected' ) as $service_name => $connections ) { |
| 588 | if ( 'mastodon' !== $service_name ) { |
| 589 | continue; |
| 590 | } |
| 591 | |
| 592 | // services can have multiple connections. Store them all in our array. |
| 593 | foreach ( $connections as $connection ) { |
| 594 | $connection_id = $publicize->get_connection_id( $connection ); |
| 595 | $connection_meta = $publicize->get_connection_meta( $connection ); |
| 596 | |
| 597 | $connection_data = $connection_meta['connection_data'] ?? array(); |
| 598 | $mastodon_handle = $connection_meta['external_display'] ?? ''; |
| 599 | $connection_user_id = $connection_data['user_id'] ?? 0; |
| 600 | |
| 601 | if ( empty( $mastodon_handle ) ) { |
| 602 | continue; |
| 603 | } |
| 604 | |
| 605 | // Did we skip this connection for this post? |
| 606 | if ( get_post_meta( $post->ID, $publicize->POST_SKIP_PUBLICIZE . $connection_id, true ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 607 | continue; |
| 608 | } |
| 609 | |
| 610 | $post_mastodon_connections[] = array( |
| 611 | 'user_id' => (int) $connection_user_id, |
| 612 | 'connection_id' => (int) $connection_id, |
| 613 | 'handle' => $mastodon_handle, |
| 614 | 'global' => 0 === (int) $connection_user_id, |
| 615 | ); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | // If we have no Mastodon connections, skip. |
| 620 | if ( empty( $post_mastodon_connections ) ) { |
| 621 | return $tags; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Select a single Mastodon connection to use. |
| 626 | * It should be either the first connection belonging to the post author, |
| 627 | * or the first global connection. |
| 628 | */ |
| 629 | foreach ( $post_mastodon_connections as $mastodon_connection ) { |
| 630 | if ( (int) $post->post_author === $mastodon_connection['user_id'] ) { |
| 631 | $tags['fediverse:creator'] = esc_attr( $mastodon_connection['handle'] ); |
| 632 | break; |
| 633 | } |
| 634 | |
| 635 | if ( $mastodon_connection['global'] ) { |
| 636 | $tags['fediverse:creator'] = esc_attr( $mastodon_connection['handle'] ); |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | return $tags; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Update the markup for the Open Graph tag to match the expected output for Mastodon |
| 646 | * (name instead of property). |
| 647 | * |
| 648 | * @since 13.8 |
| 649 | * |
| 650 | * @param string $og_tag A single OG tag. |
| 651 | * |
| 652 | * @return string Result of the OG tag. |
| 653 | */ |
| 654 | function jetpack_filter_fediverse_cards_output( $og_tag ) { |
| 655 | return ( str_contains( $og_tag, 'fediverse:' ) ) ? preg_replace( '/property="([^"]+)"/', 'name="\1"', $og_tag ) : $og_tag; |
| 656 | } |
| 657 |