PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
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 +140 -89 13.3.3 → 16.3-a.5 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 '';
@@ -453,9 +456,9 @@
453 456 * @param WP_Block $block The block object.
454 457 * @return string
455 458 */
456 459 public function render_block( $attributes, $content, $block = null ) {
457 - if ( ! jetpack_is_frontend() ) {
460 + if ( ! Request::is_frontend() ) {
458 461 return $content;
459 462 }
460 463
461 464 $wrapper_attributes = array();
@@ -460,9 +463,9 @@
460 463
461 464 $wrapper_attributes = array();
462 465 $post_id = get_the_ID();
463 466 $block_attributes = array(
464 - 'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : null,
467 + 'headline' => $attributes['headline'] ?? null,
465 468 'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'],
466 469 'show_author' => isset( $attributes['displayAuthor'] ) ? (bool) $attributes['displayAuthor'] : false,
467 470 'show_headline' => isset( $attributes['displayHeadline'] ) ? (bool) $attributes['displayHeadline'] : false,
468 471 'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true,
@@ -484,8 +487,21 @@
484 487 if ( empty( $related_posts ) ) {
485 488 return '';
486 489 }
487 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 +
488 504 $list_markup = $this->render_post_list( $related_posts, $block_attributes );
489 505
490 506 if ( empty( $attributes['isServerRendered'] ) ) {
491 507 // The get_server_rendered_html() path won't register a block,
@@ -503,9 +519,9 @@
503 519 }
504 520 }
505 521 }
506 522
507 - if ( empty( $headline_markup ) && $block_attributes['show_headline'] === true ) {
523 + if ( empty( $headline_markup ) && $block_attributes['show_headline'] ) {
508 524 $headline = $block_attributes['headline'];
509 525 if ( strlen( trim( $headline ) ) !== 0 ) {
510 526 $headline_markup = sprintf(
511 527 '<h3 class="jp-relatedposts-headline">%1$s</h3>',
@@ -669,9 +685,9 @@
669 685 $current['show_thumbnails'] = ( isset( $input['show_thumbnails'] ) && '1' == $input['show_thumbnails'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
670 686 $current['show_date'] = ( isset( $input['show_date'] ) && '1' == $input['show_date'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
671 687 $current['show_context'] = ( isset( $input['show_context'] ) && '1' == $input['show_context'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
672 688 $current['layout'] = isset( $input['layout'] ) && in_array( $input['layout'], array( 'grid', 'list' ), true ) ? $input['layout'] : 'grid';
673 - $current['headline'] = isset( $input['headline'] ) ? $input['headline'] : esc_html__( 'Related', 'jetpack' );
689 + $current['headline'] = $input['headline'] ?? esc_html__( 'Related', 'jetpack' );
674 690 } else {
675 691 $current['enabled'] = false;
676 692 }
677 693
@@ -686,14 +702,13 @@
686 702 /**
687 703 * HTML for admin settings page.
688 704 *
689 705 * @uses self::get_options, checked, esc_html__
690 - * @returns null
691 706 */
692 707 public function print_setting_html() {
693 708 $options = $this->get_options();
694 709
695 - $ui_settings_template = <<<EOT
710 + $ui_settings_template = <<<'EOT'
696 711 <p class="description">%s</p>
697 712 <ul id="settings-reading-relatedposts-customize">
698 713 <li>
699 714 <label><input name="jetpack_relatedposts[show_headline]" type="checkbox" value="1" %s /> %s</label>
@@ -727,9 +742,9 @@
727 742 esc_html__( 'Preview:', 'jetpack' )
728 743 );
729 744
730 745 if ( ! $this->allow_feature_toggle() ) {
731 - $template = <<<EOT
746 + $template = <<<'EOT'
732 747 <input type="hidden" name="jetpack_relatedposts[enabled]" value="1" />
733 748 %s
734 749 EOT;
735 750 printf(
@@ -736,9 +751,9 @@
736 751 $template, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
737 752 $ui_settings // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- data is escaped when variable is set.
738 753 );
739 754 } else {
740 - $template = <<<EOT
755 + $template = <<<'EOT'
741 756 <ul id="settings-reading-relatedposts">
742 757 <li>
743 758 <label><input type="radio" name="jetpack_relatedposts[enabled]" value="0" class="tog" %s /> %s</label>
744 759 </li>
@@ -762,9 +777,9 @@
762 777 /**
763 778 * Head JS/CSS for admin settings page.
764 779 *
765 780 * @uses esc_html__
766 - * @returns null
781 + * @return null
767 782 */
768 783 public function print_setting_head() {
769 784
770 785 // only dislay the Related Posts JavaScript on the Reading Settings Admin Page.
@@ -930,9 +945,9 @@
930 945 $options['size'] = $args['size'];
931 946 }
932 947
933 948 if (
934 - ! $options['enabled']
949 + empty( $options['enabled'] )
935 950 || 0 === (int) $post_id
936 951 || empty( $options['size'] )
937 952 ) {
938 953 return array();
@@ -1289,9 +1304,9 @@
1289 1304 $related_posts[ $index ]['id'] = $real_post->ID;
1290 1305 $related_posts[ $index ]['url'] = esc_url( get_permalink( $real_post ) );
1291 1306 $related_posts[ $index ]['title'] = $this->to_utf8( $this->get_title( $real_post->post_title, $real_post->post_content, $real_post->ID ) );
1292 1307 $related_posts[ $index ]['date'] = get_the_date( '', $real_post );
1293 - $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' );
1294 1309 $related_posts[ $index ]['img'] = $this->generate_related_post_image_params( $real_post->ID );
1295 1310 $related_posts[ $index ]['context'] = $this->generate_related_post_context( $real_post->ID );
1296 1311 }
1297 1312 } else {
@@ -1304,23 +1319,22 @@
1304 1319 }
1305 1320
1306 1321 $response = array(
1307 1322 'version' => self::VERSION,
1308 - 'show_thumbnails' => (bool) $options['show_thumbnails'],
1309 - 'show_date' => (bool) $options['show_date'],
1310 - 'show_context' => (bool) $options['show_context'],
1311 - 'layout' => (string) $options['layout'],
1312 - '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'] ?? '' ),
1313 1328 'items' => array(),
1314 1329 );
1315 1330
1316 - if ( count( $related_posts ) === $options['size'] ) {
1331 + if ( ! empty( $options['size'] ) && count( $related_posts ) === $options['size'] ) {
1317 1332 $response['items'] = $related_posts;
1318 1333 }
1319 1334
1320 - echo wp_json_encode( $response );
1321 -
1322 - 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 );
1323 1337 }
1324 1338
1325 1339 /**
1326 1340 * Returns a UTF-8 encoded array of post information for the given post_id
@@ -1343,9 +1357,9 @@
1343 1357 'title' => $this->to_utf8( $this->get_title( $post->post_title, $post->post_content, $post->ID ) ),
1344 1358 'author' => $this->generate_related_post_display_author( $post->ID ),
1345 1359 'date' => get_the_date( '', $post->ID ),
1346 1360 'format' => get_post_format( $post->ID ),
1347 - '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' ),
1348 1362 /**
1349 1363 * Filters the rel attribute for the Related Posts' links.
1350 1364 *
1351 1365 * @module related-posts
@@ -1414,11 +1428,14 @@
1414 1428 apply_filters( 'the_title', $post_title, $post_id )
1415 1429 );
1416 1430 }
1417 1431
1418 - $post_title = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post_content ) ), 5, '…' );
1419 - if ( ! empty( $post_title ) ) {
1420 - 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 + }
1421 1438 }
1422 1439
1423 1440 return __( 'Untitled Post', 'jetpack' );
1424 1441 }
@@ -1427,16 +1444,20 @@
1427 1444 * Returns a plain text post excerpt for title attribute of links.
1428 1445 *
1429 1446 * @param string $post_excerpt - the post excerpt.
1430 1447 * @param string $post_content - the post content.
1448 + * @param int $post_id - the post ID.
1431 1449 * @uses strip_shortcodes, wp_strip_all_tags, wp_trim_words
1432 1450 * @return string
1433 1451 */
1434 - protected function get_excerpt( $post_excerpt, $post_content ) {
1435 - 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 {
1436 1459 $excerpt = $post_content;
1437 - } else {
1438 - $excerpt = $post_excerpt;
1439 1460 }
1440 1461
1441 1462 return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $excerpt ) ), 50, '…' );
1442 1463 }
@@ -1442,12 +1463,12 @@
1442 1463 }
1443 1464
1444 1465 /**
1445 1466 * Generates the thumbnail image to be used for the post. Uses the
1446 - * image as returned by Jetpack_PostImages::get_image()
1467 + * image as returned by Images::get_image()
1447 1468 *
1448 1469 * @param int $post_id - the post ID.
1449 - * @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
1450 1471 * @return string
1451 1472 */
1452 1473 protected function generate_related_post_image_params( $post_id ) {
1453 1474 $image_params = array(
@@ -1480,60 +1501,70 @@
1480 1501 );
1481 1502 }
1482 1503
1483 1504 // Try to get post image.
1484 - if ( class_exists( 'Jetpack_PostImages' ) ) {
1485 - $img_url = '';
1486 - $post_image = Jetpack_PostImages::get_image(
1487 - $post_id,
1488 - $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 + )
1489 1519 );
1520 + }
1521 + $post_image = Images::get_image( $post_id, $image_args );
1490 1522
1491 - if ( is_array( $post_image ) ) {
1492 - $img_url = $post_image['src'];
1493 - } elseif ( class_exists( 'Jetpack_Media_Summary' ) ) {
1494 - $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 );
1495 1527
1496 - if ( is_array( $media ) && ! empty( $media['image'] ) ) {
1497 - $img_url = $media['image'];
1498 - }
1528 + if ( is_array( $media ) && ! empty( $media['image'] ) ) {
1529 + $img_url = $media['image'];
1499 1530 }
1531 + }
1500 1532
1501 - if ( ! empty( $img_url ) ) {
1502 - if ( ! empty( $post_image['alt_text'] ) ) {
1503 - $image_params['alt_text'] = $post_image['alt_text'];
1504 - } else {
1505 - $image_params['alt_text'] = '';
1506 - }
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 + }
1507 1539
1508 - $thumbnail_width = 0;
1509 - $thumbnail_height = 0;
1540 + $thumbnail_width = 0;
1541 + $thumbnail_height = 0;
1510 1542
1511 - if ( ! empty( $thumbnail_size['width'] ) ) {
1512 - $thumbnail_width = $thumbnail_size['width'];
1513 - $image_params['width'] = $thumbnail_width;
1514 - }
1543 + if ( ! empty( $thumbnail_size['width'] ) ) {
1544 + $thumbnail_width = $thumbnail_size['width'];
1545 + $image_params['width'] = $thumbnail_width;
1546 + }
1515 1547
1516 - if ( ! empty( $thumbnail_size['height'] ) ) {
1517 - $thumbnail_height = $thumbnail_size['height'];
1518 - $image_params['height'] = $thumbnail_height;
1519 - }
1548 + if ( ! empty( $thumbnail_size['height'] ) ) {
1549 + $thumbnail_height = $thumbnail_size['height'];
1550 + $image_params['height'] = $thumbnail_height;
1551 + }
1520 1552
1521 - $image_params['src'] = Jetpack_PostImages::fit_image_url(
1522 - $img_url,
1523 - $thumbnail_width,
1524 - $thumbnail_height
1525 - );
1553 + $image_params['src'] = Images::fit_image_url(
1554 + $img_url,
1555 + $thumbnail_width,
1556 + $thumbnail_height
1557 + );
1526 1558
1527 - // Add a srcset to handle zoomed views and high-density screens.
1528 - $srcset = Jetpack_PostImages::generate_cropped_srcset(
1529 - $post_image,
1530 - $thumbnail_width,
1531 - $thumbnail_height
1532 - );
1533 - if ( ! empty( $srcset ) ) {
1534 - $image_params['srcset'] = $srcset;
1535 - }
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;
1536 1567 }
1537 1568 }
1538 1569
1539 1570 return $image_params;
@@ -1757,10 +1788,10 @@
1757 1788 protected function generate_related_post_context_block( $post_id ) {
1758 1789 $categories = get_the_category( $post_id );
1759 1790 if ( is_array( $categories ) ) {
1760 1791 foreach ( $categories as $category ) {
1761 - $cat_link = get_category_link( $category );
1762 - 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 );
1763 1794 return array(
1764 1795 'text' => trim( $category->name ),
1765 1796 'link' => $cat_link,
1766 1797 );
@@ -1769,10 +1800,10 @@
1769 1800 }
1770 1801 $tags = get_the_terms( $post_id, 'post_tag' );
1771 1802 if ( is_array( $tags ) ) {
1772 1803 foreach ( $tags as $tag ) {
1773 - $tag_link = get_tag_link( $tag );
1774 - if ( '' !== trim( $tag->name ) ) {
1804 + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1805 + $tag_link = get_tag_link( $tag );
1775 1806 return array(
1776 1807 'text' => trim( $tag->name ),
1777 1808 'link' => $tag_link,
1778 1809 );
@@ -1813,9 +1844,9 @@
1813 1844 protected function generate_related_post_context( $post_id ) {
1814 1845 $categories = get_the_category( $post_id );
1815 1846 if ( is_array( $categories ) ) {
1816 1847 foreach ( $categories as $category ) {
1817 - if ( 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1848 + if ( $category instanceof WP_Term && 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1818 1849 $post_cat_context = sprintf(
1819 1850 // Translators: The category or tag name.
1820 1851 esc_html_x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1821 1852 $category->name
@@ -1837,9 +1868,9 @@
1837 1868
1838 1869 $tags = get_the_terms( $post_id, 'post_tag' );
1839 1870 if ( is_array( $tags ) ) {
1840 1871 foreach ( $tags as $tag ) {
1841 - if ( '' !== trim( $tag->name ) ) {
1872 + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1842 1873 $post_tag_context = sprintf(
1843 1874 // Translators: the category or tag name.
1844 1875 _x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1845 1876 $tag->name
@@ -1883,13 +1914,22 @@
1883 1914
1884 1915 /**
1885 1916 * Determines if the current post is able to use related posts.
1886 1917 *
1887 - * @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
1888 1920 * @return bool
1889 1921 */
1890 1922 protected function enabled_for_request() {
1891 - $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
1892 1932 && ! is_attachment()
1893 1933 && ! is_admin()
1894 1934 && ! is_embed()
1895 1935 && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) );
@@ -1894,9 +1934,9 @@
1894 1934 && ! is_embed()
1895 1935 && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) );
1896 1936
1897 1937 /**
1898 - * 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.
1899 1939 *
1900 1940 * @module related-posts
1901 1941 *
1902 1942 * @since 3.3.0
@@ -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',
@@ -2061,10 +2111,11 @@
2061 2111 if ( empty( $options['enabled'] ) || ! $options['enabled'] ) {
2062 2112 return array();
2063 2113 }
2064 2114
2065 - // If the current post don't contain a Related Posts block, don't get the related posts.
2066 - 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() ) {
2067 2118 return array();
2068 2119 }
2069 2120
2070 2121 return $this->get_for_post_id( $object['id'], array( 'size' => 6 ) );