PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | modules/related-posts/jetpack-related-posts.php +231 -165 12.7.316.3-a.1 View file →
@@ -6,8 +6,11 @@
6 6 */
7 7
8 8 use Automattic\Jetpack\Assets;
9 9 use Automattic\Jetpack\Blocks;
10 +use Automattic\Jetpack\Post_Media\Images;
11 +use Automattic\Jetpack\SEO\Content_Gate;
12 +use Automattic\Jetpack\Status\Request;
10 13 use Automattic\Jetpack\Sync\Settings;
11 14
12 15 /**
13 16 * The Jetpack_RelatedPosts class.
@@ -12,9 +15,9 @@
12 15 /**
13 16 * The Jetpack_RelatedPosts class.
14 17 */
15 18 class Jetpack_RelatedPosts {
16 - const VERSION = '20211209';
19 + const VERSION = '20240116';
17 20 const SHORTCODE = 'jetpack-related-posts';
18 21
19 22 /**
20 23 * Instance of the class.
@@ -127,9 +130,9 @@
127 130
128 131 /**
129 132 * Get the blog ID.
130 133 *
131 - * @return Object current blog id.
134 + * @return mixed current blog id.
132 135 */
133 136 protected function get_blog_id() {
134 137 return Jetpack_Options::get_option( 'id' );
135 138 }
@@ -196,9 +199,9 @@
196 199 */
197 200 public function get_headline() {
198 201 $options = $this->get_options();
199 202
200 - if ( $options['show_headline'] ) {
203 + if ( ! empty( $options['show_headline'] ) ) {
201 204 $headline = sprintf(
202 205 /** This filter is already documented in modules/sharedaddy/sharing-service.php */
203 206 apply_filters( 'jetpack_sharing_headline_html', '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', esc_html( $options['headline'] ), 'related-posts' ),
204 207 esc_html( $options['headline'] )
@@ -217,9 +220,9 @@
217 220 * @filter the_content
218 221 *
219 222 * @param string $content Post content.
220 223 *
221 - * @returns string
224 + * @return string
222 225 */
223 226 public function filter_add_target_to_dom( $content ) {
224 227 // Do not output related posts for ActivityPub requests.
225 228 if (
@@ -255,10 +258,10 @@
255 258 'displayThumbnails' => $rp_settings['show_thumbnails'],
256 259 'showHeadline' => $rp_settings['show_headline'],
257 260 'displayDate' => isset( $rp_settings['show_date'] ) ? (bool) $rp_settings['show_date'] : true,
258 261 'displayContext' => isset( $rp_settings['show_context'] ) && $rp_settings['show_context'],
259 - 'postLayout' => isset( $rp_settings['layout'] ) ? $rp_settings['layout'] : 'grid',
260 - 'postsToShow' => isset( $rp_settings['size'] ) ? $rp_settings['size'] : 3,
262 + 'postLayout' => $rp_settings['layout'] ?? 'grid',
263 + 'postsToShow' => $rp_settings['size'] ?? 3,
261 264 /** This filter is already documented in modules/related-posts/jetpack-related-posts.php */
262 265 'headline' => apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() ),
263 266 'isServerRendered' => true,
264 267 );
@@ -317,9 +320,9 @@
317 320
318 321 /**
319 322 * Returns the HTML for the related posts section if it's running in the loop or other instances where we don't support related posts.
320 323 *
321 - * @returns string
324 + * @return string
322 325 */
323 326 public function get_client_rendered_html_unsupported() {
324 327 if ( Settings::is_syncing() ) {
325 328 return '';
@@ -341,98 +344,108 @@
341 344 */
342 345 public function render_block_item( $related_post, $block_attributes ) {
343 346 $instance_id = 'related-posts-item-' . uniqid();
344 347 $label_id = $instance_id . '-label';
348 + $title = $related_post['title'];
349 + $url = $related_post['url'];
350 + $rel = $related_post['rel'];
351 + $img = '';
352 + $list = '';
345 353
346 354 $item_markup = sprintf(
347 - '<ul id="%1$s" aria-labelledby="%2$s" class="jp-related-posts-i2__post" role="menuitem">',
348 - esc_attr( $instance_id ),
349 - esc_attr( $label_id )
355 + '<li id="%1$s" class="jp-related-posts-i2__post">',
356 + esc_attr( $instance_id )
350 357 );
351 358
352 - $item_markup .= sprintf(
353 - '<li class="jp-related-posts-i2__post-link"><a id="%1$s" href="%2$s" %4$s>%3$s</a></li>',
354 - esc_attr( $label_id ),
355 - esc_url( $related_post['url'] ),
356 - esc_attr( $related_post['title'] ),
357 - ( ! empty( $related_post['rel'] ) ? 'rel="' . esc_attr( $related_post['rel'] ) . '"' : '' )
358 - );
359 -
359 + // Thumbnail
360 360 if ( ! empty( $block_attributes['show_thumbnails'] ) && ! empty( $related_post['img']['src'] ) ) {
361 - $img_link = sprintf(
362 - '<li class="jp-related-posts-i2__post-img-link"><a href="%1$s" %2$s><img loading="lazy" src="%3$s" width="%4$s" height="%5$s" alt="%6$s" %7$s/></a></li>',
363 - esc_url( $related_post['url'] ),
364 - ( ! empty( $related_post['rel'] ) ? 'rel="' . esc_attr( $related_post['rel'] ) . '"' : '' ),
361 + $img = sprintf(
362 + '<img loading="lazy" class="jp-related-posts-i2__post-img" src="%1$s" alt="%2$s" %3$s/>',
365 363 esc_url( $related_post['img']['src'] ),
366 - esc_attr( $related_post['img']['width'] ),
367 - esc_attr( $related_post['img']['height'] ),
368 364 esc_attr( $related_post['img']['alt_text'] ),
369 365 ( ! empty( $related_post['img']['srcset'] ) ? 'srcset="' . esc_attr( $related_post['img']['srcset'] ) . '"' : '' )
370 366 );
367 + }
371 368
372 - $item_markup .= $img_link;
373 - }
369 + // Link
370 + $item_markup .= sprintf(
371 + '<a id="%1$s" href="%2$s" class="jp-related-posts-i2__post-link" %3$s>%4$s%5$s</a>',
372 + esc_attr( $label_id ),
373 + esc_url( $url ),
374 + ( ! empty( $rel ) ? 'rel="' . esc_attr( $rel ) . '"' : '' ),
375 + esc_html( $title ),
376 + $img
377 + );
374 378
379 + // Date
375 380 if ( $block_attributes['show_date'] ) {
376 - $date_tag = sprintf(
377 - '<li class="jp-related-posts-i2__post-date">%1$s</li>',
378 - esc_html( $related_post['date'] )
379 - );
380 -
381 - $item_markup .= $date_tag;
381 + $list .= '<dt>' . __( 'Date', 'jetpack' ) . '</dt>';
382 + $list .= '<dd class="jp-related-posts-i2__post-date">';
383 + $list .= esc_html( $related_post['date'] );
384 + $list .= '</dd>';
382 385 }
383 386
387 + // Author
384 388 if ( $block_attributes['show_author'] ) {
385 - $author_tag = sprintf(
386 - '<li class="jp-related-posts-i2__post-author">%1$s</li>',
387 - esc_html( $related_post['author'] )
388 - );
389 -
390 - $item_markup .= $author_tag;
389 + $list .= '<dt>' . __( 'Author', 'jetpack' ) . '</dt>';
390 + $list .= '<dd class="jp-related-posts-i2__post-author">';
391 + $list .= esc_html( $related_post['author'] );
392 + $list .= '</dd>';
391 393 }
392 394
395 + // Context
393 396 if ( ( $block_attributes['show_context'] ) && ! empty( $related_post['block_context'] ) ) {
397 + // translators: this is followed by the reason why the item is related to the current post
398 + $list .= '<dt>' . __( 'In relation to', 'jetpack' ) . '</dt>';
399 + $list .= '<dd class="jp-related-posts-i2__post-context">';
400 +
394 401 // Note: The original 'context' value is not used when rendering the block.
395 402 // It is still generated and available for the legacy rendering code path though.
396 403 // See './related-posts.js' for that usage.
397 - $context_tag = '';
398 404 $block_context = $related_post['block_context'];
405 +
399 406 if ( ! empty( $block_context['link'] ) ) {
400 - $context_tag = sprintf(
401 - '<li class="jp-related-posts-i2__post-context"><a href="%1$s">%2$s</a></li>',
407 + $list .= sprintf(
408 + '<a href="%1$s">%2$s</a>',
402 409 esc_url( $block_context['link'] ),
403 410 esc_html( $block_context['text'] )
404 411 );
405 412 } else {
406 - $context_tag = sprintf(
407 - '<li class="jp-related-posts-i2__post-context">%1$s</li>',
408 - esc_html( $block_context['text'] )
409 - );
413 + $list .= esc_html( $block_context['text'] );
410 414 }
411 415
412 - $item_markup .= $context_tag;
416 + $list .= '</dd>';
413 417 }
414 418
415 - $item_markup .= '</ul>';
419 + // Metadata
420 + if ( ! empty( $list ) ) {
421 + $item_markup .= '<dl class="jp-related-posts-i2__post-defs">' . $list . '</dl>';
422 + }
416 423
424 + $item_markup .= '</li>';
425 +
417 426 return $item_markup;
418 427 }
419 428
420 429 /**
421 - * Render a related posts row.
430 + * Render the list of related posts.
422 431 *
423 - * @param array $posts The posts to render into the row.
432 + * @param array $posts The posts to render into the list.
424 433 * @param array $block_attributes Block attributes.
434 + * @return string
425 435 */
426 - public function render_block_row( $posts, $block_attributes ) {
427 - $rows_markup = '';
436 + public function render_post_list( $posts, $block_attributes ) {
437 + $markup = '';
438 +
428 439 foreach ( $posts as $post ) {
429 - $rows_markup .= $this->render_block_item( $post, $block_attributes );
440 + $markup .= $this->render_block_item( $post, $block_attributes );
430 441 }
442 +
431 443 return sprintf(
432 - '<div class="jp-related-posts-i2__row" data-post-count="%1$s">%2$s</div>',
444 + // role="list" is required for accessibility as VoiceOver ignores unstyled lists.
445 + '<ul class="jp-related-posts-i2__list" role="list" data-post-count="%1$s">%2$s</ul>',
433 446 count( $posts ),
434 - $rows_markup
447 + $markup
435 448 );
436 449 }
437 450
438 451 /**
@@ -437,14 +450,15 @@
437 450
438 451 /**
439 452 * Render the related posts markup.
440 453 *
441 - * @param array $attributes Block attributes.
442 - * @param string $content String containing the related Posts block content.
454 + * @param array $attributes Block attributes.
455 + * @param string $content String containing the related Posts block content.
456 + * @param WP_Block $block The block object.
443 457 * @return string
444 458 */
445 - public function render_block( $attributes, $content ) {
446 - if ( ! jetpack_is_frontend() ) {
459 + public function render_block( $attributes, $content, $block = null ) {
460 + if ( ! Request::is_frontend() ) {
447 461 return $content;
448 462 }
449 463
450 464 $wrapper_attributes = array();
@@ -449,9 +463,9 @@
449 463
450 464 $wrapper_attributes = array();
451 465 $post_id = get_the_ID();
452 466 $block_attributes = array(
453 - 'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : null,
467 + 'headline' => $attributes['headline'] ?? null,
454 468 'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'],
455 469 'show_author' => isset( $attributes['displayAuthor'] ) ? (bool) $attributes['displayAuthor'] : false,
456 470 'show_headline' => isset( $attributes['displayHeadline'] ) ? (bool) $attributes['displayHeadline'] : false,
457 471 'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true,
@@ -469,34 +483,27 @@
469 483 'exclude_post_ids' => $excludes,
470 484 )
471 485 );
472 486
473 - $display_lower_row = $block_attributes['size'] > 3;
474 -
475 487 if ( empty( $related_posts ) ) {
476 488 return '';
477 489 }
478 490
479 - switch ( count( $related_posts ) ) {
480 - case 2:
481 - case 4:
482 - case 5:
483 - $top_row_end = 2;
484 - break;
491 + /*
492 + * The block renders through its own block callback, independently of the
493 + * module's front-end asset gate (enabled_for_request()). That gate only
494 + * enqueues our assets on single posts in classic themes, so a block placed
495 + * on a page (or any view the gate skips) would render as unstyled HTML.
496 + * Enqueue the stylesheet here, whenever the block actually outputs markup,
497 + * to keep it styled everywhere it can be used. We intentionally do not widen
498 + * enabled_for_request() itself: that governs the automatic the_content
499 + * insertion and was deliberately scoped in #39784 to avoid showing related
500 + * posts on classic-theme pages.
501 + */
502 + $this->enqueue_assets( false, true );
485 503
486 - default:
487 - $top_row_end = 3;
488 - break;
489 - }
504 + $list_markup = $this->render_post_list( $related_posts, $block_attributes );
490 505
491 - $upper_row_posts = array_slice( $related_posts, 0, $top_row_end );
492 - $lower_row_posts = array_slice( $related_posts, $top_row_end );
493 -
494 - $rows_markup = $this->render_block_row( $upper_row_posts, $block_attributes );
495 - if ( $display_lower_row ) {
496 - $rows_markup .= $this->render_block_row( $lower_row_posts, $block_attributes );
497 - }
498 -
499 506 if ( empty( $attributes['isServerRendered'] ) ) {
500 507 // The get_server_rendered_html() path won't register a block,
501 508 // so only apply block supports when not server rendered.
502 509 $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
@@ -502,9 +509,19 @@
502 509 $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
503 510 }
504 511
505 512 $headline_markup = '';
506 - if ( $block_attributes['show_headline'] === true ) {
513 +
514 + if ( isset( $block ) ) {
515 + foreach ( $block->inner_blocks as $inner_block ) {
516 + if ( 'core/heading' === $inner_block->name && ! empty( wp_strip_all_tags( $inner_block->inner_html ) ) ) {
517 + $headline_markup = trim( $inner_block->inner_html );
518 + break;
519 + }
520 + }
521 + }
522 +
523 + if ( empty( $headline_markup ) && $block_attributes['show_headline'] ) {
507 524 $headline = $block_attributes['headline'];
508 525 if ( strlen( trim( $headline ) ) !== 0 ) {
509 526 $headline_markup = sprintf(
510 527 '<h3 class="jp-relatedposts-headline">%1$s</h3>',
@@ -511,15 +528,17 @@
511 528 esc_html( $headline )
512 529 );
513 530 }
514 531 }
532 +
515 533 $display_markup = sprintf(
516 - '<nav class="jp-relatedposts-i2%1$s"%2$s data-layout="%3$s">%4$s%5$s</nav>',
534 + '<nav class="jp-relatedposts-i2%1$s"%2$s data-layout="%3$s" aria-label="%6$s">%4$s%5$s</nav>',
517 535 ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
518 536 ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
519 537 esc_attr( $block_attributes['layout'] ),
520 538 $headline_markup,
521 - $rows_markup
539 + $list_markup,
540 + empty( $headline_markup ) ? esc_attr__( 'Related Posts', 'jetpack' ) : esc_attr( wp_strip_all_tags( $headline_markup ) )
522 541 );
523 542
524 543 /**
525 544 * Filter the output HTML of Related Posts.
@@ -666,9 +685,9 @@
666 685 $current['show_thumbnails'] = ( isset( $input['show_thumbnails'] ) && '1' == $input['show_thumbnails'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
667 686 $current['show_date'] = ( isset( $input['show_date'] ) && '1' == $input['show_date'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
668 687 $current['show_context'] = ( isset( $input['show_context'] ) && '1' == $input['show_context'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
669 688 $current['layout'] = isset( $input['layout'] ) && in_array( $input['layout'], array( 'grid', 'list' ), true ) ? $input['layout'] : 'grid';
670 - $current['headline'] = isset( $input['headline'] ) ? $input['headline'] : esc_html__( 'Related', 'jetpack' );
689 + $current['headline'] = $input['headline'] ?? esc_html__( 'Related', 'jetpack' );
671 690 } else {
672 691 $current['enabled'] = false;
673 692 }
674 693
@@ -683,14 +702,13 @@
683 702 /**
684 703 * HTML for admin settings page.
685 704 *
686 705 * @uses self::get_options, checked, esc_html__
687 - * @returns null
688 706 */
689 707 public function print_setting_html() {
690 708 $options = $this->get_options();
691 709
692 - $ui_settings_template = <<<EOT
710 + $ui_settings_template = <<<'EOT'
693 711 <p class="description">%s</p>
694 712 <ul id="settings-reading-relatedposts-customize">
695 713 <li>
696 714 <label><input name="jetpack_relatedposts[show_headline]" type="checkbox" value="1" %s /> %s</label>
@@ -724,9 +742,9 @@
724 742 esc_html__( 'Preview:', 'jetpack' )
725 743 );
726 744
727 745 if ( ! $this->allow_feature_toggle() ) {
728 - $template = <<<EOT
746 + $template = <<<'EOT'
729 747 <input type="hidden" name="jetpack_relatedposts[enabled]" value="1" />
730 748 %s
731 749 EOT;
732 750 printf(
@@ -733,9 +751,9 @@
733 751 $template, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
734 752 $ui_settings // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- data is escaped when variable is set.
735 753 );
736 754 } else {
737 - $template = <<<EOT
755 + $template = <<<'EOT'
738 756 <ul id="settings-reading-relatedposts">
739 757 <li>
740 758 <label><input type="radio" name="jetpack_relatedposts[enabled]" value="0" class="tog" %s /> %s</label>
741 759 </li>
@@ -759,9 +777,9 @@
759 777 /**
760 778 * Head JS/CSS for admin settings page.
761 779 *
762 780 * @uses esc_html__
763 - * @returns null
781 + * @return null
764 782 */
765 783 public function print_setting_head() {
766 784
767 785 // only dislay the Related Posts JavaScript on the Reading Settings Admin Page.
@@ -839,9 +857,8 @@
839 857 } else {
840 858 $extra_css = '';
841 859 }
842 860 // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped -- Escaped above where needed.
843 - // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- wpcom repo still uses WPCS v2, which uses a different code.
844 861 echo <<<EOT
845 862 <style type="text/css">
846 863 #settings-reading-relatedposts .disabled { opacity:.5; filter:Alpha(opacity=50); }
847 864 #settings-reading-relatedposts-preview .jp-relatedposts { background:#fff; padding:.5em; width:75%; }
@@ -909,9 +926,8 @@
909 926 .change( update_ui );
910 927 });
911 928 </script>
912 929 EOT;
913 - // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
914 930 // phpcs:enable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped
915 931 }
916 932
917 933 /**
@@ -929,9 +945,9 @@
929 945 $options['size'] = $args['size'];
930 946 }
931 947
932 948 if (
933 - ! $options['enabled']
949 + empty( $options['enabled'] )
934 950 || 0 === (int) $post_id
935 951 || empty( $options['size'] )
936 952 ) {
937 953 return array();
@@ -1160,8 +1176,9 @@
1160 1176 * NOTE: Calls exit() to end all further processing after payload has been outputed.
1161 1177 *
1162 1178 * @param array $excludes array of post_ids to exclude.
1163 1179 * @uses send_nosniff_header, self::get_for_post_id, get_the_ID
1180 + * @return never
1164 1181 */
1165 1182 protected function action_frontend_init_ajax( array $excludes ) {
1166 1183 define( 'DOING_AJAX', true );
1167 1184
@@ -1287,9 +1304,9 @@
1287 1304 $related_posts[ $index ]['id'] = $real_post->ID;
1288 1305 $related_posts[ $index ]['url'] = esc_url( get_permalink( $real_post ) );
1289 1306 $related_posts[ $index ]['title'] = $this->to_utf8( $this->get_title( $real_post->post_title, $real_post->post_content, $real_post->ID ) );
1290 1307 $related_posts[ $index ]['date'] = get_the_date( '', $real_post );
1291 - $related_posts[ $index ]['excerpt'] = html_entity_decode( $this->to_utf8( $this->get_excerpt( $real_post->post_excerpt, $real_post->post_content ) ), ENT_QUOTES, 'UTF-8' );
1308 + $related_posts[ $index ]['excerpt'] = html_entity_decode( $this->to_utf8( $this->get_excerpt( $real_post->post_excerpt, $real_post->post_content, $real_post->ID ) ), ENT_QUOTES, 'UTF-8' );
1292 1309 $related_posts[ $index ]['img'] = $this->generate_related_post_image_params( $real_post->ID );
1293 1310 $related_posts[ $index ]['context'] = $this->generate_related_post_context( $real_post->ID );
1294 1311 }
1295 1312 } else {
@@ -1302,23 +1319,22 @@
1302 1319 }
1303 1320
1304 1321 $response = array(
1305 1322 'version' => self::VERSION,
1306 - 'show_thumbnails' => (bool) $options['show_thumbnails'],
1307 - 'show_date' => (bool) $options['show_date'],
1308 - 'show_context' => (bool) $options['show_context'],
1309 - 'layout' => (string) $options['layout'],
1310 - 'headline' => (string) $options['headline'],
1323 + 'show_thumbnails' => (bool) ( $options['show_thumbnails'] ?? false ),
1324 + 'show_date' => (bool) ( $options['show_date'] ?? true ),
1325 + 'show_context' => (bool) ( $options['show_context'] ?? true ),
1326 + 'layout' => (string) ( $options['layout'] ?? 'grid' ),
1327 + 'headline' => (string) ( $options['headline'] ?? '' ),
1311 1328 'items' => array(),
1312 1329 );
1313 1330
1314 - if ( count( $related_posts ) === $options['size'] ) {
1331 + if ( ! empty( $options['size'] ) && count( $related_posts ) === $options['size'] ) {
1315 1332 $response['items'] = $related_posts;
1316 1333 }
1317 1334
1318 - echo wp_json_encode( $response );
1319 -
1320 - exit();
1335 + // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- It takes null, but its phpdoc only says int.
1336 + wp_send_json( $response, null, JSON_UNESCAPED_SLASHES );
1321 1337 }
1322 1338
1323 1339 /**
1324 1340 * Returns a UTF-8 encoded array of post information for the given post_id
@@ -1341,9 +1357,9 @@
1341 1357 'title' => $this->to_utf8( $this->get_title( $post->post_title, $post->post_content, $post->ID ) ),
1342 1358 'author' => $this->generate_related_post_display_author( $post->ID ),
1343 1359 'date' => get_the_date( '', $post->ID ),
1344 1360 'format' => get_post_format( $post->ID ),
1345 - 'excerpt' => html_entity_decode( $this->to_utf8( $this->get_excerpt( $post->post_excerpt, $post->post_content ) ), ENT_QUOTES, 'UTF-8' ),
1361 + 'excerpt' => html_entity_decode( $this->to_utf8( $this->get_excerpt( $post->post_excerpt, $post->post_content, $post->ID ) ), ENT_QUOTES, 'UTF-8' ),
1346 1362 /**
1347 1363 * Filters the rel attribute for the Related Posts' links.
1348 1364 *
1349 1365 * @module related-posts
@@ -1412,11 +1428,14 @@
1412 1428 apply_filters( 'the_title', $post_title, $post_id )
1413 1429 );
1414 1430 }
1415 1431
1416 - $post_title = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post_content ) ), 5, '…' );
1417 - if ( ! empty( $post_title ) ) {
1418 - return $post_title;
1432 + // Same gate as get_excerpt(): this fallback is five words of the raw body.
1433 + if ( ! Content_Gate::is_gated( $post_id ) ) {
1434 + $post_title = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post_content ) ), 5, '…' );
1435 + if ( ! empty( $post_title ) ) {
1436 + return $post_title;
1437 + }
1419 1438 }
1420 1439
1421 1440 return __( 'Untitled Post', 'jetpack' );
1422 1441 }
@@ -1425,16 +1444,20 @@
1425 1444 * Returns a plain text post excerpt for title attribute of links.
1426 1445 *
1427 1446 * @param string $post_excerpt - the post excerpt.
1428 1447 * @param string $post_content - the post content.
1448 + * @param int $post_id - the post ID.
1429 1449 * @uses strip_shortcodes, wp_strip_all_tags, wp_trim_words
1430 1450 * @return string
1431 1451 */
1432 - protected function get_excerpt( $post_excerpt, $post_content ) {
1433 - if ( empty( $post_excerpt ) ) {
1452 + protected function get_excerpt( $post_excerpt, $post_content, $post_id ) {
1453 + if ( ! empty( $post_excerpt ) ) {
1454 + $excerpt = $post_excerpt;
1455 + } elseif ( Content_Gate::is_gated( $post_id ) ) {
1456 + // The body fall-through never passes through the `the_content` paywall, so ask the gate directly.
1457 + return '';
1458 + } else {
1434 1459 $excerpt = $post_content;
1435 - } else {
1436 - $excerpt = $post_excerpt;
1437 1460 }
1438 1461
1439 1462 return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $excerpt ) ), 50, '…' );
1440 1463 }
@@ -1440,12 +1463,12 @@
1440 1463 }
1441 1464
1442 1465 /**
1443 1466 * Generates the thumbnail image to be used for the post. Uses the
1444 - * image as returned by Jetpack_PostImages::get_image()
1467 + * image as returned by Images::get_image()
1445 1468 *
1446 1469 * @param int $post_id - the post ID.
1447 - * @uses self::get_options, apply_filters, Jetpack_PostImages::get_image, Jetpack_PostImages::fit_image_url
1470 + * @uses self::get_options, apply_filters, Images::get_image, Images::fit_image_url
1448 1471 * @return string
1449 1472 */
1450 1473 protected function generate_related_post_image_params( $post_id ) {
1451 1474 $image_params = array(
@@ -1478,60 +1501,70 @@
1478 1501 );
1479 1502 }
1480 1503
1481 1504 // Try to get post image.
1482 - if ( class_exists( 'Jetpack_PostImages' ) ) {
1483 - $img_url = '';
1484 - $post_image = Jetpack_PostImages::get_image(
1485 - $post_id,
1486 - $thumbnail_size
1505 + $img_url = '';
1506 + $is_gated = Content_Gate::is_gated( $post_id );
1507 + $image_args = $thumbnail_size;
1508 + if ( $is_gated ) {
1509 + // Restrict to the featured image; every other source parses the body.
1510 + $image_args = array_merge(
1511 + $image_args,
1512 + array(
1513 + 'from_slideshow' => false,
1514 + 'from_gallery' => false,
1515 + 'from_attachment' => false,
1516 + 'from_blocks' => false,
1517 + 'from_html' => false,
1518 + )
1487 1519 );
1520 + }
1521 + $post_image = Images::get_image( $post_id, $image_args );
1488 1522
1489 - if ( is_array( $post_image ) ) {
1490 - $img_url = $post_image['src'];
1491 - } elseif ( class_exists( 'Jetpack_Media_Summary' ) ) {
1492 - $media = Jetpack_Media_Summary::get( $post_id );
1523 + if ( is_array( $post_image ) ) {
1524 + $img_url = $post_image['src'];
1525 + } elseif ( ! $is_gated && class_exists( 'Jetpack_Media_Summary' ) ) {
1526 + $media = Jetpack_Media_Summary::get( $post_id );
1493 1527
1494 - if ( is_array( $media ) && ! empty( $media['image'] ) ) {
1495 - $img_url = $media['image'];
1496 - }
1528 + if ( is_array( $media ) && ! empty( $media['image'] ) ) {
1529 + $img_url = $media['image'];
1497 1530 }
1531 + }
1498 1532
1499 - if ( ! empty( $img_url ) ) {
1500 - if ( ! empty( $post_image['alt_text'] ) ) {
1501 - $image_params['alt_text'] = $post_image['alt_text'];
1502 - } else {
1503 - $image_params['alt_text'] = '';
1504 - }
1533 + if ( ! empty( $img_url ) ) {
1534 + if ( ! empty( $post_image['alt_text'] ) ) {
1535 + $image_params['alt_text'] = $post_image['alt_text'];
1536 + } else {
1537 + $image_params['alt_text'] = '';
1538 + }
1505 1539
1506 - $thumbnail_width = 0;
1507 - $thumbnail_height = 0;
1540 + $thumbnail_width = 0;
1541 + $thumbnail_height = 0;
1508 1542
1509 - if ( ! empty( $thumbnail_size['width'] ) ) {
1510 - $thumbnail_width = $thumbnail_size['width'];
1511 - $image_params['width'] = $thumbnail_width;
1512 - }
1543 + if ( ! empty( $thumbnail_size['width'] ) ) {
1544 + $thumbnail_width = $thumbnail_size['width'];
1545 + $image_params['width'] = $thumbnail_width;
1546 + }
1513 1547
1514 - if ( ! empty( $thumbnail_size['height'] ) ) {
1515 - $thumbnail_height = $thumbnail_size['height'];
1516 - $image_params['height'] = $thumbnail_height;
1517 - }
1548 + if ( ! empty( $thumbnail_size['height'] ) ) {
1549 + $thumbnail_height = $thumbnail_size['height'];
1550 + $image_params['height'] = $thumbnail_height;
1551 + }
1518 1552
1519 - $image_params['src'] = Jetpack_PostImages::fit_image_url(
1520 - $img_url,
1521 - $thumbnail_width,
1522 - $thumbnail_height
1523 - );
1553 + $image_params['src'] = Images::fit_image_url(
1554 + $img_url,
1555 + $thumbnail_width,
1556 + $thumbnail_height
1557 + );
1524 1558
1525 - // Add a srcset to handle zoomed views and high-density screens.
1526 - $srcset = Jetpack_PostImages::generate_cropped_srcset(
1527 - $post_image,
1528 - $thumbnail_width,
1529 - $thumbnail_height
1530 - );
1531 - if ( ! empty( $srcset ) ) {
1532 - $image_params['srcset'] = $srcset;
1533 - }
1559 + // Add a srcset to handle zoomed views and high-density screens.
1560 + $srcset = Images::generate_cropped_srcset(
1561 + $post_image,
1562 + $thumbnail_width,
1563 + $thumbnail_height
1564 + );
1565 + if ( ! empty( $srcset ) ) {
1566 + $image_params['srcset'] = $srcset;
1534 1567 }
1535 1568 }
1536 1569
1537 1570 return $image_params;
@@ -1755,10 +1788,10 @@
1755 1788 protected function generate_related_post_context_block( $post_id ) {
1756 1789 $categories = get_the_category( $post_id );
1757 1790 if ( is_array( $categories ) ) {
1758 1791 foreach ( $categories as $category ) {
1759 - $cat_link = get_category_link( $category );
1760 - if ( 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1792 + if ( $category instanceof WP_Term && 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1793 + $cat_link = get_category_link( $category );
1761 1794 return array(
1762 1795 'text' => trim( $category->name ),
1763 1796 'link' => $cat_link,
1764 1797 );
@@ -1767,10 +1800,10 @@
1767 1800 }
1768 1801 $tags = get_the_terms( $post_id, 'post_tag' );
1769 1802 if ( is_array( $tags ) ) {
1770 1803 foreach ( $tags as $tag ) {
1771 - $tag_link = get_tag_link( $tag );
1772 - if ( '' !== trim( $tag->name ) ) {
1804 + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1805 + $tag_link = get_tag_link( $tag );
1773 1806 return array(
1774 1807 'text' => trim( $tag->name ),
1775 1808 'link' => $tag_link,
1776 1809 );
@@ -1811,9 +1844,9 @@
1811 1844 protected function generate_related_post_context( $post_id ) {
1812 1845 $categories = get_the_category( $post_id );
1813 1846 if ( is_array( $categories ) ) {
1814 1847 foreach ( $categories as $category ) {
1815 - if ( 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1848 + if ( $category instanceof WP_Term && 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1816 1849 $post_cat_context = sprintf(
1817 1850 // Translators: The category or tag name.
1818 1851 esc_html_x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1819 1852 $category->name
@@ -1835,9 +1868,9 @@
1835 1868
1836 1869 $tags = get_the_terms( $post_id, 'post_tag' );
1837 1870 if ( is_array( $tags ) ) {
1838 1871 foreach ( $tags as $tag ) {
1839 - if ( '' !== trim( $tag->name ) ) {
1872 + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1840 1873 $post_tag_context = sprintf(
1841 1874 // Translators: the category or tag name.
1842 1875 _x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1843 1876 $tag->name
@@ -1881,13 +1914,22 @@
1881 1914
1882 1915 /**
1883 1916 * Determines if the current post is able to use related posts.
1884 1917 *
1885 - * @uses self::get_options, is_admin, is_single, apply_filters
1918 + * @since 14.0 Checks for singular instead of single to allow usage on non-posts CPTs in block themes.
1919 + * @uses self::get_options, is_admin, is_singular, apply_filters
1886 1920 * @return bool
1887 1921 */
1888 1922 protected function enabled_for_request() {
1889 - $enabled = is_single()
1923 + /*
1924 + * On block themes, allow usage on any singular view (post, page, CPT).
1925 + * On classic themes, only allow usage on single posts by default.
1926 + */
1927 + $enabled_on_singular_views = wp_is_block_theme()
1928 + ? is_singular()
1929 + : is_single();
1930 +
1931 + $enabled = $enabled_on_singular_views
1890 1932 && ! is_attachment()
1891 1933 && ! is_admin()
1892 1934 && ! is_embed()
1893 1935 && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) );
@@ -1892,9 +1934,9 @@
1892 1934 && ! is_embed()
1893 1935 && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) );
1894 1936
1895 1937 /**
1896 - * Filter the Enabled value to allow related posts to be shown on pages as well.
1938 + * Filter the Enabled value to allow related posts to be selectively enabled/disabled.
1897 1939 *
1898 1940 * @module related-posts
1899 1941 *
1900 1942 * @since 3.3.0
@@ -1973,11 +2015,9 @@
1973 2015 /**
1974 2016 * Render AMP's reader mode CSS.
1975 2017 */
1976 2018 public function render_amp_reader_mode_css() {
1977 - // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- this is loading a CSS file.
1978 - echo file_get_contents( __DIR__ . '/related-posts.css' );
1979 - // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
2019 + echo file_get_contents( __DIR__ . '/related-posts.css' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- this is loading a CSS file.
1980 2020 }
1981 2021
1982 2022 /**
1983 2023 * Sets up the shortcode processing.
@@ -2025,8 +2065,18 @@
2025 2065 */
2026 2066 public function rest_register_related_posts() {
2027 2067 /** This filter is already documented in class.json-api-endpoints.php */
2028 2068 $post_types = apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
2069 +
2070 + /**
2071 + * Filter the post types that are allowed to have related posts.
2072 + *
2073 + * @since 15.3
2074 + *
2075 + * @param array $post_types The post types that are allowed to have related posts.
2076 + */
2077 + $post_types = apply_filters( 'jetpack_related_posts_rest_api_allowed_post_types', $post_types );
2078 +
2029 2079 foreach ( $post_types as $post_type ) {
2030 2080 register_rest_field(
2031 2081 $post_type,
2032 2082 'jetpack-related-posts',
@@ -2051,8 +2101,24 @@
2051 2101 *
2052 2102 * @return array
2053 2103 */
2054 2104 public function rest_get_related_posts( $object ) {
2105 + if ( ! isset( $object['id'] ) ) {
2106 + return array();
2107 + }
2108 +
2109 + // If the Related Posts option is turned off, don't get the related posts.
2110 + $options = \Jetpack_Options::get_option( 'relatedposts', array() );
2111 + if ( empty( $options['enabled'] ) || ! $options['enabled'] ) {
2112 + return array();
2113 + }
2114 +
2115 + // If the current post doesn't contain a Related Posts block, and we're also on an admin page, don't get the related posts.
2116 + // This will ensure that if the feature is enabled, we can still retrieve Related Posts via the REST API.
2117 + if ( ! has_block( 'jetpack/related-posts' ) && is_admin() ) {
2118 + return array();
2119 + }
2120 +
2055 2121 return $this->get_for_post_id( $object['id'], array( 'size' => 6 ) );
2056 2122 }
2057 2123 }
2058 2124