PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 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 All 506 releases
← All changes | modules/related-posts/jetpack-related-posts.php +235 -186 12.4.2 → 16.3-a.7 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.
@@ -122,37 +125,14 @@
122 125 }
123 126
124 127 // Add Related Posts to the REST API Post response.
125 128 add_action( 'rest_api_init', array( $this, 'rest_register_related_posts' ) );
126 -
127 - Blocks::jetpack_register_block(
128 - 'jetpack/related-posts',
129 - array(
130 - 'render_callback' => array( $this, 'render_block' ),
131 - 'supports' => array(
132 - 'color' => array(
133 - 'gradients' => true,
134 - 'link' => true,
135 - ),
136 - 'spacing' => array(
137 - 'margin' => true,
138 - 'padding' => true,
139 - ),
140 - 'typography' => array(
141 - '__experimentalFontFamily' => true,
142 - 'fontSize' => true,
143 - 'lineHeight' => true,
144 - ),
145 - 'align' => array( 'wide', 'full' ),
146 - ),
147 - )
148 - );
149 129 }
150 130
151 131 /**
152 132 * Get the blog ID.
153 133 *
154 - * @return Object current blog id.
134 + * @return mixed current blog id.
155 135 */
156 136 protected function get_blog_id() {
157 137 return Jetpack_Options::get_option( 'id' );
158 138 }
@@ -219,9 +199,9 @@
219 199 */
220 200 public function get_headline() {
221 201 $options = $this->get_options();
222 202
223 - if ( $options['show_headline'] ) {
203 + if ( ! empty( $options['show_headline'] ) ) {
224 204 $headline = sprintf(
225 205 /** This filter is already documented in modules/sharedaddy/sharing-service.php */
226 206 apply_filters( 'jetpack_sharing_headline_html', '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', esc_html( $options['headline'] ), 'related-posts' ),
227 207 esc_html( $options['headline'] )
@@ -240,9 +220,9 @@
240 220 * @filter the_content
241 221 *
242 222 * @param string $content Post content.
243 223 *
244 - * @returns string
224 + * @return string
245 225 */
246 226 public function filter_add_target_to_dom( $content ) {
247 227 // Do not output related posts for ActivityPub requests.
248 228 if (
@@ -278,10 +258,10 @@
278 258 'displayThumbnails' => $rp_settings['show_thumbnails'],
279 259 'showHeadline' => $rp_settings['show_headline'],
280 260 'displayDate' => isset( $rp_settings['show_date'] ) ? (bool) $rp_settings['show_date'] : true,
281 261 'displayContext' => isset( $rp_settings['show_context'] ) && $rp_settings['show_context'],
282 - 'postLayout' => isset( $rp_settings['layout'] ) ? $rp_settings['layout'] : 'grid',
283 - 'postsToShow' => isset( $rp_settings['size'] ) ? $rp_settings['size'] : 3,
262 + 'postLayout' => $rp_settings['layout'] ?? 'grid',
263 + 'postsToShow' => $rp_settings['size'] ?? 3,
284 264 /** This filter is already documented in modules/related-posts/jetpack-related-posts.php */
285 265 'headline' => apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() ),
286 266 'isServerRendered' => true,
287 267 );
@@ -340,9 +320,9 @@
340 320
341 321 /**
342 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.
343 323 *
344 - * @returns string
324 + * @return string
345 325 */
346 326 public function get_client_rendered_html_unsupported() {
347 327 if ( Settings::is_syncing() ) {
348 328 return '';
@@ -358,103 +338,114 @@
358 338
359 339 /**
360 340 * Echoes out items for the Gutenberg block
361 341 *
362 - * @param array $related_post The post oject.
342 + * @param array $related_post The post object.
363 343 * @param array $block_attributes The block attributes.
364 344 */
365 345 public function render_block_item( $related_post, $block_attributes ) {
366 346 $instance_id = 'related-posts-item-' . uniqid();
367 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 = '';
368 353
369 354 $item_markup = sprintf(
370 - '<ul id="%1$s" aria-labelledby="%2$s" class="jp-related-posts-i2__post" role="menuitem">',
371 - esc_attr( $instance_id ),
372 - esc_attr( $label_id )
355 + '<li id="%1$s" class="jp-related-posts-i2__post">',
356 + esc_attr( $instance_id )
373 357 );
374 358
375 - $item_markup .= sprintf(
376 - '<li class="jp-related-posts-i2__post-link"><a id="%1$s" href="%2$s" %4$s>%3$s</a></li>',
377 - esc_attr( $label_id ),
378 - esc_url( $related_post['url'] ),
379 - esc_attr( $related_post['title'] ),
380 - ( ! empty( $related_post['rel'] ) ? 'rel="' . esc_attr( $related_post['rel'] ) . '"' : '' )
381 - );
382 -
359 + // Thumbnail
383 360 if ( ! empty( $block_attributes['show_thumbnails'] ) && ! empty( $related_post['img']['src'] ) ) {
384 - $img_link = sprintf(
385 - '<li class="jp-related-posts-i2__post-img-link"><a href="%1$s" %2$s><img src="%3$s" width="%4$s" height="%5$s" alt="%6$s" loading="lazy" /></a></li>',
386 - esc_url( $related_post['url'] ),
387 - ( ! 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/>',
388 363 esc_url( $related_post['img']['src'] ),
389 - esc_attr( $related_post['img']['width'] ),
390 - esc_attr( $related_post['img']['height'] ),
391 - esc_attr( $related_post['img']['alt_text'] )
364 + esc_attr( $related_post['img']['alt_text'] ),
365 + ( ! empty( $related_post['img']['srcset'] ) ? 'srcset="' . esc_attr( $related_post['img']['srcset'] ) . '"' : '' )
392 366 );
367 + }
393 368
394 - $item_markup .= $img_link;
395 - }
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 + );
396 378
379 + // Date
397 380 if ( $block_attributes['show_date'] ) {
398 - $date_tag = sprintf(
399 - '<li class="jp-related-posts-i2__post-date">%1$s</li>',
400 - esc_html( $related_post['date'] )
401 - );
402 -
403 - $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>';
404 385 }
405 386
387 + // Author
406 388 if ( $block_attributes['show_author'] ) {
407 - $author_tag = sprintf(
408 - '<li class="jp-related-posts-i2__post-author">%1$s</li>',
409 - esc_html( $related_post['author'] )
410 - );
411 -
412 - $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>';
413 393 }
414 394
395 + // Context
415 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 +
416 401 // Note: The original 'context' value is not used when rendering the block.
417 402 // It is still generated and available for the legacy rendering code path though.
418 403 // See './related-posts.js' for that usage.
419 - $context_tag = '';
420 404 $block_context = $related_post['block_context'];
405 +
421 406 if ( ! empty( $block_context['link'] ) ) {
422 - $context_tag = sprintf(
423 - '<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>',
424 409 esc_url( $block_context['link'] ),
425 410 esc_html( $block_context['text'] )
426 411 );
427 412 } else {
428 - $context_tag = sprintf(
429 - '<li class="jp-related-posts-i2__post-context">%1$s</li>',
430 - esc_html( $block_context['text'] )
431 - );
413 + $list .= esc_html( $block_context['text'] );
432 414 }
433 415
434 - $item_markup .= $context_tag;
416 + $list .= '</dd>';
435 417 }
436 418
437 - $item_markup .= '</ul>';
419 + // Metadata
420 + if ( ! empty( $list ) ) {
421 + $item_markup .= '<dl class="jp-related-posts-i2__post-defs">' . $list . '</dl>';
422 + }
438 423
424 + $item_markup .= '</li>';
425 +
439 426 return $item_markup;
440 427 }
441 428
442 429 /**
443 - * Render a related posts row.
430 + * Render the list of related posts.
444 431 *
445 - * @param array $posts The posts to render into the row.
432 + * @param array $posts The posts to render into the list.
446 433 * @param array $block_attributes Block attributes.
434 + * @return string
447 435 */
448 - public function render_block_row( $posts, $block_attributes ) {
449 - $rows_markup = '';
436 + public function render_post_list( $posts, $block_attributes ) {
437 + $markup = '';
438 +
450 439 foreach ( $posts as $post ) {
451 - $rows_markup .= $this->render_block_item( $post, $block_attributes );
440 + $markup .= $this->render_block_item( $post, $block_attributes );
452 441 }
442 +
453 443 return sprintf(
454 - '<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>',
455 446 count( $posts ),
456 - $rows_markup
447 + $markup
457 448 );
458 449 }
459 450
460 451 /**
@@ -459,14 +450,15 @@
459 450
460 451 /**
461 452 * Render the related posts markup.
462 453 *
463 - * @param array $attributes Block attributes.
464 - * @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.
465 457 * @return string
466 458 */
467 - public function render_block( $attributes, $content ) {
468 - if ( ! jetpack_is_frontend() ) {
459 + public function render_block( $attributes, $content, $block = null ) {
460 + if ( ! Request::is_frontend() ) {
469 461 return $content;
470 462 }
471 463
472 464 $wrapper_attributes = array();
@@ -471,9 +463,9 @@
471 463
472 464 $wrapper_attributes = array();
473 465 $post_id = get_the_ID();
474 466 $block_attributes = array(
475 - 'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : null,
467 + 'headline' => $attributes['headline'] ?? null,
476 468 'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'],
477 469 'show_author' => isset( $attributes['displayAuthor'] ) ? (bool) $attributes['displayAuthor'] : false,
478 470 'show_headline' => isset( $attributes['displayHeadline'] ) ? (bool) $attributes['displayHeadline'] : false,
479 471 'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true,
@@ -491,34 +483,27 @@
491 483 'exclude_post_ids' => $excludes,
492 484 )
493 485 );
494 486
495 - $display_lower_row = $block_attributes['size'] > 3;
496 -
497 487 if ( empty( $related_posts ) ) {
498 488 return '';
499 489 }
500 490
501 - switch ( count( $related_posts ) ) {
502 - case 2:
503 - case 4:
504 - case 5:
505 - $top_row_end = 2;
506 - 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 );
507 503
508 - default:
509 - $top_row_end = 3;
510 - break;
511 - }
504 + $list_markup = $this->render_post_list( $related_posts, $block_attributes );
512 505
513 - $upper_row_posts = array_slice( $related_posts, 0, $top_row_end );
514 - $lower_row_posts = array_slice( $related_posts, $top_row_end );
515 -
516 - $rows_markup = $this->render_block_row( $upper_row_posts, $block_attributes );
517 - if ( $display_lower_row ) {
518 - $rows_markup .= $this->render_block_row( $lower_row_posts, $block_attributes );
519 - }
520 -
521 506 if ( empty( $attributes['isServerRendered'] ) ) {
522 507 // The get_server_rendered_html() path won't register a block,
523 508 // so only apply block supports when not server rendered.
524 509 $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
@@ -524,9 +509,19 @@
524 509 $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
525 510 }
526 511
527 512 $headline_markup = '';
528 - 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'] ) {
529 524 $headline = $block_attributes['headline'];
530 525 if ( strlen( trim( $headline ) ) !== 0 ) {
531 526 $headline_markup = sprintf(
532 527 '<h3 class="jp-relatedposts-headline">%1$s</h3>',
@@ -533,15 +528,17 @@
533 528 esc_html( $headline )
534 529 );
535 530 }
536 531 }
532 +
537 533 $display_markup = sprintf(
538 - '<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>',
539 535 ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
540 536 ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
541 537 esc_attr( $block_attributes['layout'] ),
542 538 $headline_markup,
543 - $rows_markup
539 + $list_markup,
540 + empty( $headline_markup ) ? esc_attr__( 'Related Posts', 'jetpack' ) : esc_attr( wp_strip_all_tags( $headline_markup ) )
544 541 );
545 542
546 543 /**
547 544 * Filter the output HTML of Related Posts.
@@ -688,9 +685,9 @@
688 685 $current['show_thumbnails'] = ( isset( $input['show_thumbnails'] ) && '1' == $input['show_thumbnails'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
689 686 $current['show_date'] = ( isset( $input['show_date'] ) && '1' == $input['show_date'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
690 687 $current['show_context'] = ( isset( $input['show_context'] ) && '1' == $input['show_context'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
691 688 $current['layout'] = isset( $input['layout'] ) && in_array( $input['layout'], array( 'grid', 'list' ), true ) ? $input['layout'] : 'grid';
692 - $current['headline'] = isset( $input['headline'] ) ? $input['headline'] : esc_html__( 'Related', 'jetpack' );
689 + $current['headline'] = $input['headline'] ?? esc_html__( 'Related', 'jetpack' );
693 690 } else {
694 691 $current['enabled'] = false;
695 692 }
696 693
@@ -705,14 +702,13 @@
705 702 /**
706 703 * HTML for admin settings page.
707 704 *
708 705 * @uses self::get_options, checked, esc_html__
709 - * @returns null
710 706 */
711 707 public function print_setting_html() {
712 708 $options = $this->get_options();
713 709
714 - $ui_settings_template = <<<EOT
710 + $ui_settings_template = <<<'EOT'
715 711 <p class="description">%s</p>
716 712 <ul id="settings-reading-relatedposts-customize">
717 713 <li>
718 714 <label><input name="jetpack_relatedposts[show_headline]" type="checkbox" value="1" %s /> %s</label>
@@ -746,9 +742,9 @@
746 742 esc_html__( 'Preview:', 'jetpack' )
747 743 );
748 744
749 745 if ( ! $this->allow_feature_toggle() ) {
750 - $template = <<<EOT
746 + $template = <<<'EOT'
751 747 <input type="hidden" name="jetpack_relatedposts[enabled]" value="1" />
752 748 %s
753 749 EOT;
754 750 printf(
@@ -755,9 +751,9 @@
755 751 $template, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
756 752 $ui_settings // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- data is escaped when variable is set.
757 753 );
758 754 } else {
759 - $template = <<<EOT
755 + $template = <<<'EOT'
760 756 <ul id="settings-reading-relatedposts">
761 757 <li>
762 758 <label><input type="radio" name="jetpack_relatedposts[enabled]" value="0" class="tog" %s /> %s</label>
763 759 </li>
@@ -781,9 +777,9 @@
781 777 /**
782 778 * Head JS/CSS for admin settings page.
783 779 *
784 780 * @uses esc_html__
785 - * @returns null
781 + * @return null
786 782 */
787 783 public function print_setting_head() {
788 784
789 785 // only dislay the Related Posts JavaScript on the Reading Settings Admin Page.
@@ -860,9 +856,9 @@
860 856 $extra_css = '#settings-reading-relatedposts-customize { padding-left:2em; margin-top:.5em; }';
861 857 } else {
862 858 $extra_css = '';
863 859 }
864 - // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
860 + // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped -- Escaped above where needed.
865 861 echo <<<EOT
866 862 <style type="text/css">
867 863 #settings-reading-relatedposts .disabled { opacity:.5; filter:Alpha(opacity=50); }
868 864 #settings-reading-relatedposts-preview .jp-relatedposts { background:#fff; padding:.5em; width:75%; }
@@ -930,8 +926,9 @@
930 926 .change( update_ui );
931 927 });
932 928 </script>
933 929 EOT;
930 + // phpcs:enable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped
934 931 }
935 932
936 933 /**
937 934 * Gets an array of related posts that match the given post_id.
@@ -948,9 +945,9 @@
948 945 $options['size'] = $args['size'];
949 946 }
950 947
951 948 if (
952 - ! $options['enabled']
949 + empty( $options['enabled'] )
953 950 || 0 === (int) $post_id
954 951 || empty( $options['size'] )
955 952 ) {
956 953 return array();
@@ -1179,8 +1176,9 @@
1179 1176 * NOTE: Calls exit() to end all further processing after payload has been outputed.
1180 1177 *
1181 1178 * @param array $excludes array of post_ids to exclude.
1182 1179 * @uses send_nosniff_header, self::get_for_post_id, get_the_ID
1180 + * @return never
1183 1181 */
1184 1182 protected function action_frontend_init_ajax( array $excludes ) {
1185 1183 define( 'DOING_AJAX', true );
1186 1184
@@ -1306,9 +1304,9 @@
1306 1304 $related_posts[ $index ]['id'] = $real_post->ID;
1307 1305 $related_posts[ $index ]['url'] = esc_url( get_permalink( $real_post ) );
1308 1306 $related_posts[ $index ]['title'] = $this->to_utf8( $this->get_title( $real_post->post_title, $real_post->post_content, $real_post->ID ) );
1309 1307 $related_posts[ $index ]['date'] = get_the_date( '', $real_post );
1310 - $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' );
1311 1309 $related_posts[ $index ]['img'] = $this->generate_related_post_image_params( $real_post->ID );
1312 1310 $related_posts[ $index ]['context'] = $this->generate_related_post_context( $real_post->ID );
1313 1311 }
1314 1312 } else {
@@ -1321,23 +1319,22 @@
1321 1319 }
1322 1320
1323 1321 $response = array(
1324 1322 'version' => self::VERSION,
1325 - 'show_thumbnails' => (bool) $options['show_thumbnails'],
1326 - 'show_date' => (bool) $options['show_date'],
1327 - 'show_context' => (bool) $options['show_context'],
1328 - 'layout' => (string) $options['layout'],
1329 - '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'] ?? '' ),
1330 1328 'items' => array(),
1331 1329 );
1332 1330
1333 - if ( count( $related_posts ) === $options['size'] ) {
1331 + if ( ! empty( $options['size'] ) && count( $related_posts ) === $options['size'] ) {
1334 1332 $response['items'] = $related_posts;
1335 1333 }
1336 1334
1337 - echo wp_json_encode( $response );
1338 -
1339 - 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 );
1340 1337 }
1341 1338
1342 1339 /**
1343 1340 * Returns a UTF-8 encoded array of post information for the given post_id
@@ -1360,9 +1357,9 @@
1360 1357 'title' => $this->to_utf8( $this->get_title( $post->post_title, $post->post_content, $post->ID ) ),
1361 1358 'author' => $this->generate_related_post_display_author( $post->ID ),
1362 1359 'date' => get_the_date( '', $post->ID ),
1363 1360 'format' => get_post_format( $post->ID ),
1364 - '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' ),
1365 1362 /**
1366 1363 * Filters the rel attribute for the Related Posts' links.
1367 1364 *
1368 1365 * @module related-posts
@@ -1431,11 +1428,14 @@
1431 1428 apply_filters( 'the_title', $post_title, $post_id )
1432 1429 );
1433 1430 }
1434 1431
1435 - $post_title = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post_content ) ), 5, '…' );
1436 - if ( ! empty( $post_title ) ) {
1437 - 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 + }
1438 1438 }
1439 1439
1440 1440 return __( 'Untitled Post', 'jetpack' );
1441 1441 }
@@ -1444,16 +1444,20 @@
1444 1444 * Returns a plain text post excerpt for title attribute of links.
1445 1445 *
1446 1446 * @param string $post_excerpt - the post excerpt.
1447 1447 * @param string $post_content - the post content.
1448 + * @param int $post_id - the post ID.
1448 1449 * @uses strip_shortcodes, wp_strip_all_tags, wp_trim_words
1449 1450 * @return string
1450 1451 */
1451 - protected function get_excerpt( $post_excerpt, $post_content ) {
1452 - 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 {
1453 1459 $excerpt = $post_content;
1454 - } else {
1455 - $excerpt = $post_excerpt;
1456 1460 }
1457 1461
1458 1462 return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $excerpt ) ), 50, '…' );
1459 1463 }
@@ -1459,12 +1463,12 @@
1459 1463 }
1460 1464
1461 1465 /**
1462 1466 * Generates the thumbnail image to be used for the post. Uses the
1463 - * image as returned by Jetpack_PostImages::get_image()
1467 + * image as returned by Images::get_image()
1464 1468 *
1465 1469 * @param int $post_id - the post ID.
1466 - * @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
1467 1471 * @return string
1468 1472 */
1469 1473 protected function generate_related_post_image_params( $post_id ) {
1470 1474 $image_params = array(
@@ -1497,60 +1501,70 @@
1497 1501 );
1498 1502 }
1499 1503
1500 1504 // Try to get post image.
1501 - if ( class_exists( 'Jetpack_PostImages' ) ) {
1502 - $img_url = '';
1503 - $post_image = Jetpack_PostImages::get_image(
1504 - $post_id,
1505 - $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 + )
1506 1519 );
1520 + }
1521 + $post_image = Images::get_image( $post_id, $image_args );
1507 1522
1508 - if ( is_array( $post_image ) ) {
1509 - $img_url = $post_image['src'];
1510 - } elseif ( class_exists( 'Jetpack_Media_Summary' ) ) {
1511 - $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 );
1512 1527
1513 - if ( is_array( $media ) && ! empty( $media['image'] ) ) {
1514 - $img_url = $media['image'];
1515 - }
1528 + if ( is_array( $media ) && ! empty( $media['image'] ) ) {
1529 + $img_url = $media['image'];
1516 1530 }
1531 + }
1517 1532
1518 - if ( ! empty( $img_url ) ) {
1519 - if ( ! empty( $post_image['alt_text'] ) ) {
1520 - $image_params['alt_text'] = $post_image['alt_text'];
1521 - } else {
1522 - $image_params['alt_text'] = '';
1523 - }
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 + }
1524 1539
1525 - $thumbnail_width = 0;
1526 - $thumbnail_height = 0;
1540 + $thumbnail_width = 0;
1541 + $thumbnail_height = 0;
1527 1542
1528 - if ( ! empty( $thumbnail_size['width'] ) ) {
1529 - $thumbnail_width = $thumbnail_size['width'];
1530 - $image_params['width'] = $thumbnail_width;
1531 - }
1543 + if ( ! empty( $thumbnail_size['width'] ) ) {
1544 + $thumbnail_width = $thumbnail_size['width'];
1545 + $image_params['width'] = $thumbnail_width;
1546 + }
1532 1547
1533 - if ( ! empty( $thumbnail_size['height'] ) ) {
1534 - $thumbnail_height = $thumbnail_size['height'];
1535 - $image_params['height'] = $thumbnail_height;
1536 - }
1548 + if ( ! empty( $thumbnail_size['height'] ) ) {
1549 + $thumbnail_height = $thumbnail_size['height'];
1550 + $image_params['height'] = $thumbnail_height;
1551 + }
1537 1552
1538 - $image_params['src'] = Jetpack_PostImages::fit_image_url(
1539 - $img_url,
1540 - $thumbnail_width,
1541 - $thumbnail_height
1542 - );
1553 + $image_params['src'] = Images::fit_image_url(
1554 + $img_url,
1555 + $thumbnail_width,
1556 + $thumbnail_height
1557 + );
1543 1558
1544 - // Add a srcset to handle zoomed views and high-density screens.
1545 - $srcset = Jetpack_PostImages::generate_cropped_srcset(
1546 - $post_image,
1547 - $thumbnail_width,
1548 - $thumbnail_height
1549 - );
1550 - if ( ! empty( $srcset ) ) {
1551 - $image_params['srcset'] = $srcset;
1552 - }
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;
1553 1567 }
1554 1568 }
1555 1569
1556 1570 return $image_params;
@@ -1774,10 +1788,10 @@
1774 1788 protected function generate_related_post_context_block( $post_id ) {
1775 1789 $categories = get_the_category( $post_id );
1776 1790 if ( is_array( $categories ) ) {
1777 1791 foreach ( $categories as $category ) {
1778 - $cat_link = get_category_link( $category );
1779 - 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 );
1780 1794 return array(
1781 1795 'text' => trim( $category->name ),
1782 1796 'link' => $cat_link,
1783 1797 );
@@ -1786,10 +1800,10 @@
1786 1800 }
1787 1801 $tags = get_the_terms( $post_id, 'post_tag' );
1788 1802 if ( is_array( $tags ) ) {
1789 1803 foreach ( $tags as $tag ) {
1790 - $tag_link = get_tag_link( $tag );
1791 - if ( '' !== trim( $tag->name ) ) {
1804 + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1805 + $tag_link = get_tag_link( $tag );
1792 1806 return array(
1793 1807 'text' => trim( $tag->name ),
1794 1808 'link' => $tag_link,
1795 1809 );
@@ -1830,9 +1844,9 @@
1830 1844 protected function generate_related_post_context( $post_id ) {
1831 1845 $categories = get_the_category( $post_id );
1832 1846 if ( is_array( $categories ) ) {
1833 1847 foreach ( $categories as $category ) {
1834 - if ( 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1848 + if ( $category instanceof WP_Term && 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1835 1849 $post_cat_context = sprintf(
1836 1850 // Translators: The category or tag name.
1837 1851 esc_html_x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1838 1852 $category->name
@@ -1854,9 +1868,9 @@
1854 1868
1855 1869 $tags = get_the_terms( $post_id, 'post_tag' );
1856 1870 if ( is_array( $tags ) ) {
1857 1871 foreach ( $tags as $tag ) {
1858 - if ( '' !== trim( $tag->name ) ) {
1872 + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1859 1873 $post_tag_context = sprintf(
1860 1874 // Translators: the category or tag name.
1861 1875 _x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1862 1876 $tag->name
@@ -1900,13 +1914,22 @@
1900 1914
1901 1915 /**
1902 1916 * Determines if the current post is able to use related posts.
1903 1917 *
1904 - * @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
1905 1920 * @return bool
1906 1921 */
1907 1922 protected function enabled_for_request() {
1908 - $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
1909 1932 && ! is_attachment()
1910 1933 && ! is_admin()
1911 1934 && ! is_embed()
1912 1935 && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) );
@@ -1911,9 +1934,9 @@
1911 1934 && ! is_embed()
1912 1935 && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) );
1913 1936
1914 1937 /**
1915 - * 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.
1916 1939 *
1917 1940 * @module related-posts
1918 1941 *
1919 1942 * @since 3.3.0
@@ -2042,8 +2065,18 @@
2042 2065 */
2043 2066 public function rest_register_related_posts() {
2044 2067 /** This filter is already documented in class.json-api-endpoints.php */
2045 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 +
2046 2079 foreach ( $post_types as $post_type ) {
2047 2080 register_rest_field(
2048 2081 $post_type,
2049 2082 'jetpack-related-posts',
@@ -2068,8 +2101,24 @@
2068 2101 *
2069 2102 * @return array
2070 2103 */
2071 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 +
2072 2121 return $this->get_for_post_id( $object['id'], array( 'size' => 6 ) );
2073 2122 }
2074 2123 }
2075 2124