| @@ -22,8 +22,87 @@ | ||
| 22 | 22 | exit( 0 ); |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 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 ); | |
| 26 | 105 | add_action( 'web_stories_story_head', 'jetpack_og_tags' ); |
| 27 | 106 | |
| 28 | 107 | // Add a Fediverse Open Graph Tag when an author has connected their Mastodon account. |
| 29 | 108 | add_filter( 'jetpack_open_graph_tags', 'jetpack_add_fediverse_creator_open_graph_tag', 10, 1 ); |
| @@ -124,9 +203,9 @@ | ||
| 124 | 203 | $tags['og:title'] = wp_kses( apply_filters( 'the_title', $data->post_title, $data->ID ), array() ); |
| 125 | 204 | } |
| 126 | 205 | |
| 127 | 206 | $tags['og:url'] = get_permalink( $data->ID ); |
| 128 | - if ( ! post_password_required() ) { | |
| 207 | + if ( ! post_password_required( $data ) ) { | |
| 129 | 208 | /* |
| 130 | 209 | * If the post author set an excerpt, use that. |
| 131 | 210 | * Otherwise, pick the post content that comes before the More tag if there is one. |
| 132 | 211 | * Do not use the post content if it contains premium content. |
| @@ -132,11 +211,17 @@ | ||
| 132 | 211 | * Do not use the post content if it contains premium content. |
| 133 | 212 | */ |
| 134 | 213 | if ( ! empty( $data->post_excerpt ) ) { |
| 135 | 214 | $tags['og:description'] = jetpack_og_get_description( $data->post_excerpt ); |
| 136 | - } elseif ( ! has_block( 'premium-content/container', $data->post_content ) ) { | |
| 137 | - $excerpt = explode( '<!--more-->', $data->post_content )[0]; | |
| 138 | - $tags['og:description'] = jetpack_og_get_description( $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 | + } | |
| 139 | 224 | } |
| 140 | 225 | } |
| 141 | 226 | |
| 142 | 227 | $tags['article:published_time'] = gmdate( 'c', strtotime( $data->post_date_gmt ) ); |
| @@ -338,15 +423,23 @@ | ||
| 338 | 423 | } |
| 339 | 424 | |
| 340 | 425 | // Attempt to find something good for this post using our generalized PostImages code. |
| 341 | 426 | if ( empty( $image ) ) { |
| 342 | - $post_image = Images::get_image( | |
| 343 | - get_the_ID(), | |
| 344 | - array( | |
| 345 | - 'width' => $width, | |
| 346 | - 'height' => $height, | |
| 347 | - ) | |
| 427 | + $image_args = array( | |
| 428 | + 'width' => $width, | |
| 429 | + 'height' => $height, | |
| 348 | 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 ); | |
| 349 | 442 | if ( ! empty( $post_image ) && is_array( $post_image ) ) { |
| 350 | 443 | $image['src'] = $post_image['src']; |
| 351 | 444 | if ( isset( $post_image['src_width'] ) && isset( $post_image['src_height'] ) ) { |
| 352 | 445 | $image['width'] = $post_image['src_width']; |