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 +163 -98 13.1.5 → 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.
@@ -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 '';
@@ -447,14 +450,15 @@
447 450
448 451 /**
449 452 * Render the related posts markup.
450 453 *
451 - * @param array $attributes Block attributes.
452 - * @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.
453 457 * @return string
454 458 */
455 - public function render_block( $attributes, $content ) {
456 - if ( ! jetpack_is_frontend() ) {
459 + public function render_block( $attributes, $content, $block = null ) {
460 + if ( ! Request::is_frontend() ) {
457 461 return $content;
458 462 }
459 463
460 464 $wrapper_attributes = array();
@@ -459,9 +463,9 @@
459 463
460 464 $wrapper_attributes = array();
461 465 $post_id = get_the_ID();
462 466 $block_attributes = array(
463 - 'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : null,
467 + 'headline' => $attributes['headline'] ?? null,
464 468 'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'],
465 469 'show_author' => isset( $attributes['displayAuthor'] ) ? (bool) $attributes['displayAuthor'] : false,
466 470 'show_headline' => isset( $attributes['displayHeadline'] ) ? (bool) $attributes['displayHeadline'] : false,
467 471 'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true,
@@ -483,8 +487,21 @@
483 487 if ( empty( $related_posts ) ) {
484 488 return '';
485 489 }
486 490
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 );
503 +
487 504 $list_markup = $this->render_post_list( $related_posts, $block_attributes );
488 505
489 506 if ( empty( $attributes['isServerRendered'] ) ) {
490 507 // The get_server_rendered_html() path won't register a block,
@@ -491,29 +508,37 @@
491 508 // so only apply block supports when not server rendered.
492 509 $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
493 510 }
494 511
495 - $headline_id = 'relatedposts-headline-' . uniqid();
496 512 $headline_markup = '';
497 513
498 - if ( $block_attributes['show_headline'] === true ) {
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'] ) {
499 524 $headline = $block_attributes['headline'];
500 525 if ( strlen( trim( $headline ) ) !== 0 ) {
501 526 $headline_markup = sprintf(
502 - '<h3 id="%2$s" class="jp-relatedposts-headline">%1$s</h3>',
503 - esc_html( $headline ),
504 - esc_attr( $headline_id )
527 + '<h3 class="jp-relatedposts-headline">%1$s</h3>',
528 + esc_html( $headline )
505 529 );
506 530 }
507 531 }
532 +
508 533 $display_markup = sprintf(
509 - '<nav class="jp-relatedposts-i2%1$s"%2$s data-layout="%3$s" %6$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>',
510 535 ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
511 536 ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
512 537 esc_attr( $block_attributes['layout'] ),
513 538 $headline_markup,
514 539 $list_markup,
515 - empty( $headline_markup ) ? 'aria-label="' . esc_attr( __( 'Related Posts', 'jetpack' ) ) . '"' : 'aria-labelledby="' . esc_attr( $headline_id ) . '"'
540 + empty( $headline_markup ) ? esc_attr__( 'Related Posts', 'jetpack' ) : esc_attr( wp_strip_all_tags( $headline_markup ) )
516 541 );
517 542
518 543 /**
519 544 * Filter the output HTML of Related Posts.
@@ -660,9 +685,9 @@
660 685 $current['show_thumbnails'] = ( isset( $input['show_thumbnails'] ) && '1' == $input['show_thumbnails'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
661 686 $current['show_date'] = ( isset( $input['show_date'] ) && '1' == $input['show_date'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
662 687 $current['show_context'] = ( isset( $input['show_context'] ) && '1' == $input['show_context'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
663 688 $current['layout'] = isset( $input['layout'] ) && in_array( $input['layout'], array( 'grid', 'list' ), true ) ? $input['layout'] : 'grid';
664 - $current['headline'] = isset( $input['headline'] ) ? $input['headline'] : esc_html__( 'Related', 'jetpack' );
689 + $current['headline'] = $input['headline'] ?? esc_html__( 'Related', 'jetpack' );
665 690 } else {
666 691 $current['enabled'] = false;
667 692 }
668 693
@@ -677,14 +702,13 @@
677 702 /**
678 703 * HTML for admin settings page.
679 704 *
680 705 * @uses self::get_options, checked, esc_html__
681 - * @returns null
682 706 */
683 707 public function print_setting_html() {
684 708 $options = $this->get_options();
685 709
686 - $ui_settings_template = <<<EOT
710 + $ui_settings_template = <<<'EOT'
687 711 <p class="description">%s</p>
688 712 <ul id="settings-reading-relatedposts-customize">
689 713 <li>
690 714 <label><input name="jetpack_relatedposts[show_headline]" type="checkbox" value="1" %s /> %s</label>
@@ -718,9 +742,9 @@
718 742 esc_html__( 'Preview:', 'jetpack' )
719 743 );
720 744
721 745 if ( ! $this->allow_feature_toggle() ) {
722 - $template = <<<EOT
746 + $template = <<<'EOT'
723 747 <input type="hidden" name="jetpack_relatedposts[enabled]" value="1" />
724 748 %s
725 749 EOT;
726 750 printf(
@@ -727,9 +751,9 @@
727 751 $template, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
728 752 $ui_settings // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- data is escaped when variable is set.
729 753 );
730 754 } else {
731 - $template = <<<EOT
755 + $template = <<<'EOT'
732 756 <ul id="settings-reading-relatedposts">
733 757 <li>
734 758 <label><input type="radio" name="jetpack_relatedposts[enabled]" value="0" class="tog" %s /> %s</label>
735 759 </li>
@@ -753,9 +777,9 @@
753 777 /**
754 778 * Head JS/CSS for admin settings page.
755 779 *
756 780 * @uses esc_html__
757 - * @returns null
781 + * @return null
758 782 */
759 783 public function print_setting_head() {
760 784
761 785 // only dislay the Related Posts JavaScript on the Reading Settings Admin Page.
@@ -921,9 +945,9 @@
921 945 $options['size'] = $args['size'];
922 946 }
923 947
924 948 if (
925 - ! $options['enabled']
949 + empty( $options['enabled'] )
926 950 || 0 === (int) $post_id
927 951 || empty( $options['size'] )
928 952 ) {
929 953 return array();
@@ -1152,8 +1176,9 @@
1152 1176 * NOTE: Calls exit() to end all further processing after payload has been outputed.
1153 1177 *
1154 1178 * @param array $excludes array of post_ids to exclude.
1155 1179 * @uses send_nosniff_header, self::get_for_post_id, get_the_ID
1180 + * @return never
1156 1181 */
1157 1182 protected function action_frontend_init_ajax( array $excludes ) {
1158 1183 define( 'DOING_AJAX', true );
1159 1184
@@ -1279,9 +1304,9 @@
1279 1304 $related_posts[ $index ]['id'] = $real_post->ID;
1280 1305 $related_posts[ $index ]['url'] = esc_url( get_permalink( $real_post ) );
1281 1306 $related_posts[ $index ]['title'] = $this->to_utf8( $this->get_title( $real_post->post_title, $real_post->post_content, $real_post->ID ) );
1282 1307 $related_posts[ $index ]['date'] = get_the_date( '', $real_post );
1283 - $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' );
1284 1309 $related_posts[ $index ]['img'] = $this->generate_related_post_image_params( $real_post->ID );
1285 1310 $related_posts[ $index ]['context'] = $this->generate_related_post_context( $real_post->ID );
1286 1311 }
1287 1312 } else {
@@ -1294,23 +1319,22 @@
1294 1319 }
1295 1320
1296 1321 $response = array(
1297 1322 'version' => self::VERSION,
1298 - 'show_thumbnails' => (bool) $options['show_thumbnails'],
1299 - 'show_date' => (bool) $options['show_date'],
1300 - 'show_context' => (bool) $options['show_context'],
1301 - 'layout' => (string) $options['layout'],
1302 - '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'] ?? '' ),
1303 1328 'items' => array(),
1304 1329 );
1305 1330
1306 - if ( count( $related_posts ) === $options['size'] ) {
1331 + if ( ! empty( $options['size'] ) && count( $related_posts ) === $options['size'] ) {
1307 1332 $response['items'] = $related_posts;
1308 1333 }
1309 1334
1310 - echo wp_json_encode( $response );
1311 -
1312 - 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 );
1313 1337 }
1314 1338
1315 1339 /**
1316 1340 * Returns a UTF-8 encoded array of post information for the given post_id
@@ -1333,9 +1357,9 @@
1333 1357 'title' => $this->to_utf8( $this->get_title( $post->post_title, $post->post_content, $post->ID ) ),
1334 1358 'author' => $this->generate_related_post_display_author( $post->ID ),
1335 1359 'date' => get_the_date( '', $post->ID ),
1336 1360 'format' => get_post_format( $post->ID ),
1337 - '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' ),
1338 1362 /**
1339 1363 * Filters the rel attribute for the Related Posts' links.
1340 1364 *
1341 1365 * @module related-posts
@@ -1404,11 +1428,14 @@
1404 1428 apply_filters( 'the_title', $post_title, $post_id )
1405 1429 );
1406 1430 }
1407 1431
1408 - $post_title = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post_content ) ), 5, '…' );
1409 - if ( ! empty( $post_title ) ) {
1410 - 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 + }
1411 1438 }
1412 1439
1413 1440 return __( 'Untitled Post', 'jetpack' );
1414 1441 }
@@ -1417,16 +1444,20 @@
1417 1444 * Returns a plain text post excerpt for title attribute of links.
1418 1445 *
1419 1446 * @param string $post_excerpt - the post excerpt.
1420 1447 * @param string $post_content - the post content.
1448 + * @param int $post_id - the post ID.
1421 1449 * @uses strip_shortcodes, wp_strip_all_tags, wp_trim_words
1422 1450 * @return string
1423 1451 */
1424 - protected function get_excerpt( $post_excerpt, $post_content ) {
1425 - 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 {
1426 1459 $excerpt = $post_content;
1427 - } else {
1428 - $excerpt = $post_excerpt;
1429 1460 }
1430 1461
1431 1462 return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $excerpt ) ), 50, '…' );
1432 1463 }
@@ -1432,12 +1463,12 @@
1432 1463 }
1433 1464
1434 1465 /**
1435 1466 * Generates the thumbnail image to be used for the post. Uses the
1436 - * image as returned by Jetpack_PostImages::get_image()
1467 + * image as returned by Images::get_image()
1437 1468 *
1438 1469 * @param int $post_id - the post ID.
1439 - * @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
1440 1471 * @return string
1441 1472 */
1442 1473 protected function generate_related_post_image_params( $post_id ) {
1443 1474 $image_params = array(
@@ -1470,60 +1501,70 @@
1470 1501 );
1471 1502 }
1472 1503
1473 1504 // Try to get post image.
1474 - if ( class_exists( 'Jetpack_PostImages' ) ) {
1475 - $img_url = '';
1476 - $post_image = Jetpack_PostImages::get_image(
1477 - $post_id,
1478 - $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 + )
1479 1519 );
1520 + }
1521 + $post_image = Images::get_image( $post_id, $image_args );
1480 1522
1481 - if ( is_array( $post_image ) ) {
1482 - $img_url = $post_image['src'];
1483 - } elseif ( class_exists( 'Jetpack_Media_Summary' ) ) {
1484 - $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 );
1485 1527
1486 - if ( is_array( $media ) && ! empty( $media['image'] ) ) {
1487 - $img_url = $media['image'];
1488 - }
1528 + if ( is_array( $media ) && ! empty( $media['image'] ) ) {
1529 + $img_url = $media['image'];
1489 1530 }
1531 + }
1490 1532
1491 - if ( ! empty( $img_url ) ) {
1492 - if ( ! empty( $post_image['alt_text'] ) ) {
1493 - $image_params['alt_text'] = $post_image['alt_text'];
1494 - } else {
1495 - $image_params['alt_text'] = '';
1496 - }
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 + }
1497 1539
1498 - $thumbnail_width = 0;
1499 - $thumbnail_height = 0;
1540 + $thumbnail_width = 0;
1541 + $thumbnail_height = 0;
1500 1542
1501 - if ( ! empty( $thumbnail_size['width'] ) ) {
1502 - $thumbnail_width = $thumbnail_size['width'];
1503 - $image_params['width'] = $thumbnail_width;
1504 - }
1543 + if ( ! empty( $thumbnail_size['width'] ) ) {
1544 + $thumbnail_width = $thumbnail_size['width'];
1545 + $image_params['width'] = $thumbnail_width;
1546 + }
1505 1547
1506 - if ( ! empty( $thumbnail_size['height'] ) ) {
1507 - $thumbnail_height = $thumbnail_size['height'];
1508 - $image_params['height'] = $thumbnail_height;
1509 - }
1548 + if ( ! empty( $thumbnail_size['height'] ) ) {
1549 + $thumbnail_height = $thumbnail_size['height'];
1550 + $image_params['height'] = $thumbnail_height;
1551 + }
1510 1552
1511 - $image_params['src'] = Jetpack_PostImages::fit_image_url(
1512 - $img_url,
1513 - $thumbnail_width,
1514 - $thumbnail_height
1515 - );
1553 + $image_params['src'] = Images::fit_image_url(
1554 + $img_url,
1555 + $thumbnail_width,
1556 + $thumbnail_height
1557 + );
1516 1558
1517 - // Add a srcset to handle zoomed views and high-density screens.
1518 - $srcset = Jetpack_PostImages::generate_cropped_srcset(
1519 - $post_image,
1520 - $thumbnail_width,
1521 - $thumbnail_height
1522 - );
1523 - if ( ! empty( $srcset ) ) {
1524 - $image_params['srcset'] = $srcset;
1525 - }
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;
1526 1567 }
1527 1568 }
1528 1569
1529 1570 return $image_params;
@@ -1747,10 +1788,10 @@
1747 1788 protected function generate_related_post_context_block( $post_id ) {
1748 1789 $categories = get_the_category( $post_id );
1749 1790 if ( is_array( $categories ) ) {
1750 1791 foreach ( $categories as $category ) {
1751 - $cat_link = get_category_link( $category );
1752 - 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 );
1753 1794 return array(
1754 1795 'text' => trim( $category->name ),
1755 1796 'link' => $cat_link,
1756 1797 );
@@ -1759,10 +1800,10 @@
1759 1800 }
1760 1801 $tags = get_the_terms( $post_id, 'post_tag' );
1761 1802 if ( is_array( $tags ) ) {
1762 1803 foreach ( $tags as $tag ) {
1763 - $tag_link = get_tag_link( $tag );
1764 - if ( '' !== trim( $tag->name ) ) {
1804 + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1805 + $tag_link = get_tag_link( $tag );
1765 1806 return array(
1766 1807 'text' => trim( $tag->name ),
1767 1808 'link' => $tag_link,
1768 1809 );
@@ -1803,9 +1844,9 @@
1803 1844 protected function generate_related_post_context( $post_id ) {
1804 1845 $categories = get_the_category( $post_id );
1805 1846 if ( is_array( $categories ) ) {
1806 1847 foreach ( $categories as $category ) {
1807 - if ( 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1848 + if ( $category instanceof WP_Term && 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1808 1849 $post_cat_context = sprintf(
1809 1850 // Translators: The category or tag name.
1810 1851 esc_html_x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1811 1852 $category->name
@@ -1827,9 +1868,9 @@
1827 1868
1828 1869 $tags = get_the_terms( $post_id, 'post_tag' );
1829 1870 if ( is_array( $tags ) ) {
1830 1871 foreach ( $tags as $tag ) {
1831 - if ( '' !== trim( $tag->name ) ) {
1872 + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1832 1873 $post_tag_context = sprintf(
1833 1874 // Translators: the category or tag name.
1834 1875 _x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1835 1876 $tag->name
@@ -1873,13 +1914,22 @@
1873 1914
1874 1915 /**
1875 1916 * Determines if the current post is able to use related posts.
1876 1917 *
1877 - * @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
1878 1920 * @return bool
1879 1921 */
1880 1922 protected function enabled_for_request() {
1881 - $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
1882 1932 && ! is_attachment()
1883 1933 && ! is_admin()
1884 1934 && ! is_embed()
1885 1935 && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) );
@@ -1884,9 +1934,9 @@
1884 1934 && ! is_embed()
1885 1935 && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) );
1886 1936
1887 1937 /**
1888 - * 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.
1889 1939 *
1890 1940 * @module related-posts
1891 1941 *
1892 1942 * @since 3.3.0
@@ -2015,8 +2065,18 @@
2015 2065 */
2016 2066 public function rest_register_related_posts() {
2017 2067 /** This filter is already documented in class.json-api-endpoints.php */
2018 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 +
2019 2079 foreach ( $post_types as $post_type ) {
2020 2080 register_rest_field(
2021 2081 $post_type,
2022 2082 'jetpack-related-posts',
@@ -2041,8 +2101,12 @@
2041 2101 *
2042 2102 * @return array
2043 2103 */
2044 2104 public function rest_get_related_posts( $object ) {
2105 + if ( ! isset( $object['id'] ) ) {
2106 + return array();
2107 + }
2108 +
2045 2109 // If the Related Posts option is turned off, don't get the related posts.
2046 2110 $options = \Jetpack_Options::get_option( 'relatedposts', array() );
2047 2111 if ( empty( $options['enabled'] ) || ! $options['enabled'] ) {
2048 2112 return array();
@@ -2047,10 +2111,11 @@
2047 2111 if ( empty( $options['enabled'] ) || ! $options['enabled'] ) {
2048 2112 return array();
2049 2113 }
2050 2114
2051 - // If the current post don't contain a Related Posts block, don't get the related posts.
2052 - if ( ! has_block( 'jetpack/related-posts' ) ) {
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() ) {
2053 2118 return array();
2054 2119 }
2055 2120
2056 2121 return $this->get_for_post_id( $object['id'], array( 'size' => 6 ) );