← All changes
|
modules/related-posts/jetpack-related-posts.php
+368
-169
12.0.3
→
16.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. |
| @@ -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 | } |
| @@ -200,9 +180,9 @@ | ||
| 200 | 180 | if ( isset( $_GET['relatedposts'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading and checking if we need to generate a list of excuded posts, does not update anything on the site. |
| 201 | 181 | $excludes = $this->parse_numeric_get_arg( 'relatedposts_exclude' ); |
| 202 | 182 | $this->action_frontend_init_ajax( $excludes ); |
| 203 | 183 | } else { |
| 204 | - if ( isset( $_GET['relatedposts_hit'], $_GET['relatedposts_origin'], $_GET['relatedposts_position'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- checking if fields are set to setup tracking, nothing is changing on the site. | |
| 184 | + if ( isset( $_GET['relatedposts_hit'] ) && isset( $_GET['relatedposts_origin'] ) && isset( $_GET['relatedposts_position'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- checking if fields are set to setup tracking, nothing is changing on the site. | |
| 205 | 185 | $this->previous_post_id = (int) $_GET['relatedposts_origin']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- fetching a previous post ID for tracking, nothing is changing on the site. |
| 206 | 186 | $this->log_click( $this->previous_post_id, get_the_ID(), sanitize_text_field( wp_unslash( $_GET['relatedposts_position'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- logging the click for tracking, nothing is changing on the site. |
| 207 | 187 | } |
| 208 | 188 | |
| @@ -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,11 +220,19 @@ | ||
| 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 ) { |
| 227 | + // Do not output related posts for ActivityPub requests. | |
| 228 | + if ( | |
| 229 | + function_exists( '\Activitypub\is_activitypub_request' ) | |
| 230 | + && \Activitypub\is_activitypub_request() | |
| 231 | + ) { | |
| 232 | + return $content; | |
| 233 | + } | |
| 234 | + | |
| 247 | 235 | if ( has_block( 'jetpack/related-posts' ) || Blocks::is_fse_theme() ) { |
| 248 | 236 | return $content; |
| 249 | 237 | } |
| 250 | 238 | |
| @@ -270,16 +258,16 @@ | ||
| 270 | 258 | 'displayThumbnails' => $rp_settings['show_thumbnails'], |
| 271 | 259 | 'showHeadline' => $rp_settings['show_headline'], |
| 272 | 260 | 'displayDate' => isset( $rp_settings['show_date'] ) ? (bool) $rp_settings['show_date'] : true, |
| 273 | 261 | 'displayContext' => isset( $rp_settings['show_context'] ) && $rp_settings['show_context'], |
| 274 | - 'postLayout' => isset( $rp_settings['layout'] ) ? $rp_settings['layout'] : 'grid', | |
| 275 | - 'postsToShow' => isset( $rp_settings['size'] ) ? $rp_settings['size'] : 3, | |
| 262 | + 'postLayout' => $rp_settings['layout'] ?? 'grid', | |
| 263 | + 'postsToShow' => $rp_settings['size'] ?? 3, | |
| 276 | 264 | /** This filter is already documented in modules/related-posts/jetpack-related-posts.php */ |
| 277 | 265 | 'headline' => apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() ), |
| 278 | 266 | 'isServerRendered' => true, |
| 279 | 267 | ); |
| 280 | 268 | |
| 281 | - return $this->render_block( $block_rp_settings ); | |
| 269 | + return $this->render_block( $block_rp_settings, '' ); | |
| 282 | 270 | } |
| 283 | 271 | |
| 284 | 272 | /** |
| 285 | 273 | * Looks for our shortcode on the unfiltered content, this has to execute early. |
| @@ -332,9 +320,9 @@ | ||
| 332 | 320 | |
| 333 | 321 | /** |
| 334 | 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. |
| 335 | 323 | * |
| 336 | - * @returns string | |
| 324 | + * @return string | |
| 337 | 325 | */ |
| 338 | 326 | public function get_client_rendered_html_unsupported() { |
| 339 | 327 | if ( Settings::is_syncing() ) { |
| 340 | 328 | return ''; |
| @@ -350,81 +338,114 @@ | ||
| 350 | 338 | |
| 351 | 339 | /** |
| 352 | 340 | * Echoes out items for the Gutenberg block |
| 353 | 341 | * |
| 354 | - * @param array $related_post The post oject. | |
| 342 | + * @param array $related_post The post object. | |
| 355 | 343 | * @param array $block_attributes The block attributes. |
| 356 | 344 | */ |
| 357 | 345 | public function render_block_item( $related_post, $block_attributes ) { |
| 358 | 346 | $instance_id = 'related-posts-item-' . uniqid(); |
| 359 | 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 = ''; | |
| 360 | 353 | |
| 361 | 354 | $item_markup = sprintf( |
| 362 | - '<ul id="%1$s" aria-labelledby="%2$s" class="jp-related-posts-i2__post" role="menuitem">', | |
| 363 | - esc_attr( $instance_id ), | |
| 364 | - esc_attr( $label_id ) | |
| 355 | + '<li id="%1$s" class="jp-related-posts-i2__post">', | |
| 356 | + esc_attr( $instance_id ) | |
| 365 | 357 | ); |
| 366 | 358 | |
| 359 | + // Thumbnail | |
| 360 | + if ( ! empty( $block_attributes['show_thumbnails'] ) && ! empty( $related_post['img']['src'] ) ) { | |
| 361 | + $img = sprintf( | |
| 362 | + '<img loading="lazy" class="jp-related-posts-i2__post-img" src="%1$s" alt="%2$s" %3$s/>', | |
| 363 | + esc_url( $related_post['img']['src'] ), | |
| 364 | + esc_attr( $related_post['img']['alt_text'] ), | |
| 365 | + ( ! empty( $related_post['img']['srcset'] ) ? 'srcset="' . esc_attr( $related_post['img']['srcset'] ) . '"' : '' ) | |
| 366 | + ); | |
| 367 | + } | |
| 368 | + | |
| 369 | + // Link | |
| 367 | 370 | $item_markup .= sprintf( |
| 368 | - '<li class="jp-related-posts-i2__post-link"><a id="%1$s" href="%2$s" %4$s>%3$s</a></li>', | |
| 371 | + '<a id="%1$s" href="%2$s" class="jp-related-posts-i2__post-link" %3$s>%4$s%5$s</a>', | |
| 369 | 372 | esc_attr( $label_id ), |
| 370 | - esc_url( $related_post['url'] ), | |
| 371 | - esc_attr( $related_post['title'] ), | |
| 372 | - ( ! empty( $related_post['rel'] ) ? 'rel="' . esc_attr( $related_post['rel'] ) . '"' : '' ) | |
| 373 | + esc_url( $url ), | |
| 374 | + ( ! empty( $rel ) ? 'rel="' . esc_attr( $rel ) . '"' : '' ), | |
| 375 | + esc_html( $title ), | |
| 376 | + $img | |
| 373 | 377 | ); |
| 374 | 378 | |
| 375 | - if ( ! empty( $block_attributes['show_thumbnails'] ) && ! empty( $related_post['img']['src'] ) ) { | |
| 376 | - $img_link = sprintf( | |
| 377 | - '<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>', | |
| 378 | - esc_url( $related_post['url'] ), | |
| 379 | - ( ! empty( $related_post['rel'] ) ? 'rel="' . esc_attr( $related_post['rel'] ) . '"' : '' ), | |
| 380 | - esc_url( $related_post['img']['src'] ), | |
| 381 | - esc_attr( $related_post['img']['width'] ), | |
| 382 | - esc_attr( $related_post['img']['height'] ), | |
| 383 | - esc_attr( $related_post['img']['alt_text'] ) | |
| 384 | - ); | |
| 379 | + // Date | |
| 380 | + if ( $block_attributes['show_date'] ) { | |
| 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>'; | |
| 385 | + } | |
| 385 | 386 | |
| 386 | - $item_markup .= $img_link; | |
| 387 | + // Author | |
| 388 | + if ( $block_attributes['show_author'] ) { | |
| 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>'; | |
| 387 | 393 | } |
| 388 | 394 | |
| 389 | - if ( $block_attributes['show_date'] ) { | |
| 390 | - $date_tag = sprintf( | |
| 391 | - '<li class="jp-related-posts-i2__post-date">%1$s</li>', | |
| 392 | - esc_html( $related_post['date'] ) | |
| 393 | - ); | |
| 395 | + // Context | |
| 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">'; | |
| 394 | 400 | |
| 395 | - $item_markup .= $date_tag; | |
| 401 | + // Note: The original 'context' value is not used when rendering the block. | |
| 402 | + // It is still generated and available for the legacy rendering code path though. | |
| 403 | + // See './related-posts.js' for that usage. | |
| 404 | + $block_context = $related_post['block_context']; | |
| 405 | + | |
| 406 | + if ( ! empty( $block_context['link'] ) ) { | |
| 407 | + $list .= sprintf( | |
| 408 | + '<a href="%1$s">%2$s</a>', | |
| 409 | + esc_url( $block_context['link'] ), | |
| 410 | + esc_html( $block_context['text'] ) | |
| 411 | + ); | |
| 412 | + } else { | |
| 413 | + $list .= esc_html( $block_context['text'] ); | |
| 414 | + } | |
| 415 | + | |
| 416 | + $list .= '</dd>'; | |
| 396 | 417 | } |
| 397 | 418 | |
| 398 | - if ( ( $block_attributes['show_context'] ) && ! empty( $related_post['context'] ) ) { | |
| 399 | - $context_tag = sprintf( | |
| 400 | - '<li class="jp-related-posts-i2__post-context">%1$s</li>', | |
| 401 | - esc_html( $related_post['context'] ) | |
| 402 | - ); | |
| 403 | - | |
| 404 | - $item_markup .= $context_tag; | |
| 419 | + // Metadata | |
| 420 | + if ( ! empty( $list ) ) { | |
| 421 | + $item_markup .= '<dl class="jp-related-posts-i2__post-defs">' . $list . '</dl>'; | |
| 405 | 422 | } |
| 406 | 423 | |
| 407 | - $item_markup .= '</ul>'; | |
| 424 | + $item_markup .= '</li>'; | |
| 408 | 425 | |
| 409 | 426 | return $item_markup; |
| 410 | 427 | } |
| 411 | 428 | |
| 412 | 429 | /** |
| 413 | - * Render a related posts row. | |
| 430 | + * Render the list of related posts. | |
| 414 | 431 | * |
| 415 | - * @param array $posts The posts to render into the row. | |
| 432 | + * @param array $posts The posts to render into the list. | |
| 416 | 433 | * @param array $block_attributes Block attributes. |
| 434 | + * @return string | |
| 417 | 435 | */ |
| 418 | - public function render_block_row( $posts, $block_attributes ) { | |
| 419 | - $rows_markup = ''; | |
| 436 | + public function render_post_list( $posts, $block_attributes ) { | |
| 437 | + $markup = ''; | |
| 438 | + | |
| 420 | 439 | foreach ( $posts as $post ) { |
| 421 | - $rows_markup .= $this->render_block_item( $post, $block_attributes ); | |
| 440 | + $markup .= $this->render_block_item( $post, $block_attributes ); | |
| 422 | 441 | } |
| 442 | + | |
| 423 | 443 | return sprintf( |
| 424 | - '<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>', | |
| 425 | 446 | count( $posts ), |
| 426 | - $rows_markup | |
| 447 | + $markup | |
| 427 | 448 | ); |
| 428 | 449 | } |
| 429 | 450 | |
| 430 | 451 | /** |
| @@ -429,16 +450,25 @@ | ||
| 429 | 450 | |
| 430 | 451 | /** |
| 431 | 452 | * Render the related posts markup. |
| 432 | 453 | * |
| 433 | - * @param array $attributes Block attributes. | |
| 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. | |
| 434 | 457 | * @return string |
| 435 | 458 | */ |
| 436 | - public function render_block( $attributes ) { | |
| 437 | - $post_id = get_the_ID(); | |
| 438 | - $block_attributes = array( | |
| 439 | - 'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : null, | |
| 459 | + public function render_block( $attributes, $content, $block = null ) { | |
| 460 | + if ( ! Request::is_frontend() ) { | |
| 461 | + return $content; | |
| 462 | + } | |
| 463 | + | |
| 464 | + $wrapper_attributes = array(); | |
| 465 | + $post_id = get_the_ID(); | |
| 466 | + $block_attributes = array( | |
| 467 | + 'headline' => $attributes['headline'] ?? null, | |
| 440 | 468 | 'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'], |
| 469 | + 'show_author' => isset( $attributes['displayAuthor'] ) ? (bool) $attributes['displayAuthor'] : false, | |
| 470 | + 'show_headline' => isset( $attributes['displayHeadline'] ) ? (bool) $attributes['displayHeadline'] : false, | |
| 441 | 471 | 'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true, |
| 442 | 472 | 'show_context' => isset( $attributes['displayContext'] ) && $attributes['displayContext'], |
| 443 | 473 | 'layout' => isset( $attributes['postLayout'] ) && 'list' === $attributes['postLayout'] ? $attributes['postLayout'] : 'grid', |
| 444 | 474 | 'size' => ! empty( $attributes['postsToShow'] ) ? absint( $attributes['postsToShow'] ) : 3, |
| @@ -453,47 +483,62 @@ | ||
| 453 | 483 | 'exclude_post_ids' => $excludes, |
| 454 | 484 | ) |
| 455 | 485 | ); |
| 456 | 486 | |
| 457 | - $display_lower_row = $block_attributes['size'] > 3; | |
| 458 | - | |
| 459 | 487 | if ( empty( $related_posts ) ) { |
| 460 | 488 | return ''; |
| 461 | 489 | } |
| 462 | 490 | |
| 463 | - switch ( count( $related_posts ) ) { | |
| 464 | - case 2: | |
| 465 | - case 4: | |
| 466 | - case 5: | |
| 467 | - $top_row_end = 2; | |
| 468 | - 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 ); | |
| 469 | 503 | |
| 470 | - default: | |
| 471 | - $top_row_end = 3; | |
| 472 | - break; | |
| 504 | + $list_markup = $this->render_post_list( $related_posts, $block_attributes ); | |
| 505 | + | |
| 506 | + if ( empty( $attributes['isServerRendered'] ) ) { | |
| 507 | + // The get_server_rendered_html() path won't register a block, | |
| 508 | + // so only apply block supports when not server rendered. | |
| 509 | + $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); | |
| 473 | 510 | } |
| 474 | 511 | |
| 475 | - $upper_row_posts = array_slice( $related_posts, 0, $top_row_end ); | |
| 476 | - $lower_row_posts = array_slice( $related_posts, $top_row_end ); | |
| 512 | + $headline_markup = ''; | |
| 477 | 513 | |
| 478 | - $rows_markup = $this->render_block_row( $upper_row_posts, $block_attributes ); | |
| 479 | - if ( $display_lower_row ) { | |
| 480 | - $rows_markup .= $this->render_block_row( $lower_row_posts, $block_attributes ); | |
| 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 | + } | |
| 481 | 521 | } |
| 482 | 522 | |
| 483 | - if ( empty( $attributes['isServerRendered'] ) ) { | |
| 484 | - // The get_server_rendered_html() path won't register a block, | |
| 485 | - // so only apply block supports when not server rendered. | |
| 486 | - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); | |
| 523 | + if ( empty( $headline_markup ) && $block_attributes['show_headline'] ) { | |
| 524 | + $headline = $block_attributes['headline']; | |
| 525 | + if ( strlen( trim( $headline ) ) !== 0 ) { | |
| 526 | + $headline_markup = sprintf( | |
| 527 | + '<h3 class="jp-relatedposts-headline">%1$s</h3>', | |
| 528 | + esc_html( $headline ) | |
| 529 | + ); | |
| 530 | + } | |
| 487 | 531 | } |
| 488 | 532 | |
| 489 | 533 | $display_markup = sprintf( |
| 490 | - '<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>', | |
| 491 | 535 | ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '', |
| 492 | 536 | ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '', |
| 493 | 537 | esc_attr( $block_attributes['layout'] ), |
| 494 | - $block_attributes['headline'], | |
| 495 | - $rows_markup | |
| 538 | + $headline_markup, | |
| 539 | + $list_markup, | |
| 540 | + empty( $headline_markup ) ? esc_attr__( 'Related Posts', 'jetpack' ) : esc_attr( wp_strip_all_tags( $headline_markup ) ) | |
| 496 | 541 | ); |
| 497 | 542 | |
| 498 | 543 | /** |
| 499 | 544 | * Filter the output HTML of Related Posts. |
| @@ -640,9 +685,9 @@ | ||
| 640 | 685 | $current['show_thumbnails'] = ( isset( $input['show_thumbnails'] ) && '1' == $input['show_thumbnails'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual |
| 641 | 686 | $current['show_date'] = ( isset( $input['show_date'] ) && '1' == $input['show_date'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual |
| 642 | 687 | $current['show_context'] = ( isset( $input['show_context'] ) && '1' == $input['show_context'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual |
| 643 | 688 | $current['layout'] = isset( $input['layout'] ) && in_array( $input['layout'], array( 'grid', 'list' ), true ) ? $input['layout'] : 'grid'; |
| 644 | - $current['headline'] = isset( $input['headline'] ) ? $input['headline'] : esc_html__( 'Related', 'jetpack' ); | |
| 689 | + $current['headline'] = $input['headline'] ?? esc_html__( 'Related', 'jetpack' ); | |
| 645 | 690 | } else { |
| 646 | 691 | $current['enabled'] = false; |
| 647 | 692 | } |
| 648 | 693 | |
| @@ -657,14 +702,13 @@ | ||
| 657 | 702 | /** |
| 658 | 703 | * HTML for admin settings page. |
| 659 | 704 | * |
| 660 | 705 | * @uses self::get_options, checked, esc_html__ |
| 661 | - * @returns null | |
| 662 | 706 | */ |
| 663 | 707 | public function print_setting_html() { |
| 664 | 708 | $options = $this->get_options(); |
| 665 | 709 | |
| 666 | - $ui_settings_template = <<<EOT | |
| 710 | + $ui_settings_template = <<<'EOT' | |
| 667 | 711 | <p class="description">%s</p> |
| 668 | 712 | <ul id="settings-reading-relatedposts-customize"> |
| 669 | 713 | <li> |
| 670 | 714 | <label><input name="jetpack_relatedposts[show_headline]" type="checkbox" value="1" %s /> %s</label> |
| @@ -698,9 +742,9 @@ | ||
| 698 | 742 | esc_html__( 'Preview:', 'jetpack' ) |
| 699 | 743 | ); |
| 700 | 744 | |
| 701 | 745 | if ( ! $this->allow_feature_toggle() ) { |
| 702 | - $template = <<<EOT | |
| 746 | + $template = <<<'EOT' | |
| 703 | 747 | <input type="hidden" name="jetpack_relatedposts[enabled]" value="1" /> |
| 704 | 748 | %s |
| 705 | 749 | EOT; |
| 706 | 750 | printf( |
| @@ -707,9 +751,9 @@ | ||
| 707 | 751 | $template, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 708 | 752 | $ui_settings // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- data is escaped when variable is set. |
| 709 | 753 | ); |
| 710 | 754 | } else { |
| 711 | - $template = <<<EOT | |
| 755 | + $template = <<<'EOT' | |
| 712 | 756 | <ul id="settings-reading-relatedposts"> |
| 713 | 757 | <li> |
| 714 | 758 | <label><input type="radio" name="jetpack_relatedposts[enabled]" value="0" class="tog" %s /> %s</label> |
| 715 | 759 | </li> |
| @@ -733,9 +777,9 @@ | ||
| 733 | 777 | /** |
| 734 | 778 | * Head JS/CSS for admin settings page. |
| 735 | 779 | * |
| 736 | 780 | * @uses esc_html__ |
| 737 | - * @returns null | |
| 781 | + * @return null | |
| 738 | 782 | */ |
| 739 | 783 | public function print_setting_head() { |
| 740 | 784 | |
| 741 | 785 | // only dislay the Related Posts JavaScript on the Reading Settings Admin Page. |
| @@ -812,9 +856,9 @@ | ||
| 812 | 856 | $extra_css = '#settings-reading-relatedposts-customize { padding-left:2em; margin-top:.5em; }'; |
| 813 | 857 | } else { |
| 814 | 858 | $extra_css = ''; |
| 815 | 859 | } |
| 816 | - // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 860 | + // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped -- Escaped above where needed. | |
| 817 | 861 | echo <<<EOT |
| 818 | 862 | <style type="text/css"> |
| 819 | 863 | #settings-reading-relatedposts .disabled { opacity:.5; filter:Alpha(opacity=50); } |
| 820 | 864 | #settings-reading-relatedposts-preview .jp-relatedposts { background:#fff; padding:.5em; width:75%; } |
| @@ -882,8 +926,9 @@ | ||
| 882 | 926 | .change( update_ui ); |
| 883 | 927 | }); |
| 884 | 928 | </script> |
| 885 | 929 | EOT; |
| 930 | + // phpcs:enable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped | |
| 886 | 931 | } |
| 887 | 932 | |
| 888 | 933 | /** |
| 889 | 934 | * Gets an array of related posts that match the given post_id. |
| @@ -900,9 +945,9 @@ | ||
| 900 | 945 | $options['size'] = $args['size']; |
| 901 | 946 | } |
| 902 | 947 | |
| 903 | 948 | if ( |
| 904 | - ! $options['enabled'] | |
| 949 | + empty( $options['enabled'] ) | |
| 905 | 950 | || 0 === (int) $post_id |
| 906 | 951 | || empty( $options['size'] ) |
| 907 | 952 | ) { |
| 908 | 953 | return array(); |
| @@ -1131,8 +1176,9 @@ | ||
| 1131 | 1176 | * NOTE: Calls exit() to end all further processing after payload has been outputed. |
| 1132 | 1177 | * |
| 1133 | 1178 | * @param array $excludes array of post_ids to exclude. |
| 1134 | 1179 | * @uses send_nosniff_header, self::get_for_post_id, get_the_ID |
| 1180 | + * @return never | |
| 1135 | 1181 | */ |
| 1136 | 1182 | protected function action_frontend_init_ajax( array $excludes ) { |
| 1137 | 1183 | define( 'DOING_AJAX', true ); |
| 1138 | 1184 | |
| @@ -1232,9 +1278,10 @@ | ||
| 1232 | 1278 | ) |
| 1233 | 1279 | ); |
| 1234 | 1280 | |
| 1235 | 1281 | // If we don't have enough, fetch posts without featured image. |
| 1236 | - $more = $options['size'] - count( $with_post_thumbnails ); | |
| 1282 | + $count_post_with_thumbnails = is_countable( $with_post_thumbnails ) ? count( $with_post_thumbnails ) : 0; | |
| 1283 | + $more = $options['size'] - $count_post_with_thumbnails; | |
| 1237 | 1284 | if ( 0 < $more ) { |
| 1238 | 1285 | $no_post_thumbnails = get_posts( |
| 1239 | 1286 | array( |
| 1240 | 1287 | 'posts_per_page' => $more, |
| @@ -1257,9 +1304,9 @@ | ||
| 1257 | 1304 | $related_posts[ $index ]['id'] = $real_post->ID; |
| 1258 | 1305 | $related_posts[ $index ]['url'] = esc_url( get_permalink( $real_post ) ); |
| 1259 | 1306 | $related_posts[ $index ]['title'] = $this->to_utf8( $this->get_title( $real_post->post_title, $real_post->post_content, $real_post->ID ) ); |
| 1260 | 1307 | $related_posts[ $index ]['date'] = get_the_date( '', $real_post ); |
| 1261 | - $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' ); | |
| 1262 | 1309 | $related_posts[ $index ]['img'] = $this->generate_related_post_image_params( $real_post->ID ); |
| 1263 | 1310 | $related_posts[ $index ]['context'] = $this->generate_related_post_context( $real_post->ID ); |
| 1264 | 1311 | } |
| 1265 | 1312 | } else { |
| @@ -1272,23 +1319,22 @@ | ||
| 1272 | 1319 | } |
| 1273 | 1320 | |
| 1274 | 1321 | $response = array( |
| 1275 | 1322 | 'version' => self::VERSION, |
| 1276 | - 'show_thumbnails' => (bool) $options['show_thumbnails'], | |
| 1277 | - 'show_date' => (bool) $options['show_date'], | |
| 1278 | - 'show_context' => (bool) $options['show_context'], | |
| 1279 | - 'layout' => (string) $options['layout'], | |
| 1280 | - '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'] ?? '' ), | |
| 1281 | 1328 | 'items' => array(), |
| 1282 | 1329 | ); |
| 1283 | 1330 | |
| 1284 | - if ( count( $related_posts ) === $options['size'] ) { | |
| 1331 | + if ( ! empty( $options['size'] ) && count( $related_posts ) === $options['size'] ) { | |
| 1285 | 1332 | $response['items'] = $related_posts; |
| 1286 | 1333 | } |
| 1287 | 1334 | |
| 1288 | - echo wp_json_encode( $response ); | |
| 1289 | - | |
| 1290 | - 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 ); | |
| 1291 | 1337 | } |
| 1292 | 1338 | |
| 1293 | 1339 | /** |
| 1294 | 1340 | * Returns a UTF-8 encoded array of post information for the given post_id |
| @@ -1301,18 +1347,19 @@ | ||
| 1301 | 1347 | */ |
| 1302 | 1348 | public function get_related_post_data_for_post( $post_id, $position, $origin ) { |
| 1303 | 1349 | $post = get_post( $post_id ); |
| 1304 | 1350 | return array( |
| 1305 | - 'id' => $post->ID, | |
| 1306 | - 'url' => get_permalink( $post->ID ), | |
| 1307 | - 'url_meta' => array( | |
| 1351 | + 'id' => $post->ID, | |
| 1352 | + 'url' => get_permalink( $post->ID ), | |
| 1353 | + 'url_meta' => array( | |
| 1308 | 1354 | 'origin' => $origin, |
| 1309 | 1355 | 'position' => $position, |
| 1310 | 1356 | ), |
| 1311 | - 'title' => $this->to_utf8( $this->get_title( $post->post_title, $post->post_content, $post->ID ) ), | |
| 1312 | - 'date' => get_the_date( '', $post->ID ), | |
| 1313 | - 'format' => get_post_format( $post->ID ), | |
| 1314 | - 'excerpt' => html_entity_decode( $this->to_utf8( $this->get_excerpt( $post->post_excerpt, $post->post_content ) ), ENT_QUOTES, 'UTF-8' ), | |
| 1357 | + 'title' => $this->to_utf8( $this->get_title( $post->post_title, $post->post_content, $post->ID ) ), | |
| 1358 | + 'author' => $this->generate_related_post_display_author( $post->ID ), | |
| 1359 | + 'date' => get_the_date( '', $post->ID ), | |
| 1360 | + 'format' => get_post_format( $post->ID ), | |
| 1361 | + 'excerpt' => html_entity_decode( $this->to_utf8( $this->get_excerpt( $post->post_excerpt, $post->post_content, $post->ID ) ), ENT_QUOTES, 'UTF-8' ), | |
| 1315 | 1362 | /** |
| 1316 | 1363 | * Filters the rel attribute for the Related Posts' links. |
| 1317 | 1364 | * |
| 1318 | 1365 | * @module related-posts |
| @@ -1322,12 +1369,15 @@ | ||
| 1322 | 1369 | * |
| 1323 | 1370 | * @param string $link_rel Link rel attribute for Related Posts' link. Default is empty. |
| 1324 | 1371 | * @param int $post->ID Post ID. |
| 1325 | 1372 | */ |
| 1326 | - 'rel' => apply_filters( 'jetpack_relatedposts_filter_post_link_rel', '', $post->ID ), | |
| 1373 | + 'rel' => apply_filters( 'jetpack_relatedposts_filter_post_link_rel', '', $post->ID ), | |
| 1327 | 1374 | /** |
| 1328 | 1375 | * Filter the context displayed below each Related Post. |
| 1329 | 1376 | * |
| 1377 | + * This context is used when rendering the legacy 'widget' version of Related Posts. | |
| 1378 | + * It is not used when rendering the block-based version. See 'block_context' below for that. | |
| 1379 | + * | |
| 1330 | 1380 | * @module related-posts |
| 1331 | 1381 | * |
| 1332 | 1382 | * @since 3.0.0 |
| 1333 | 1383 | * |
| @@ -1333,14 +1383,16 @@ | ||
| 1333 | 1383 | * |
| 1334 | 1384 | * @param string $this->to_utf8( $this->generate_related_post_context( $post->ID ) ) Context displayed below each related post. |
| 1335 | 1385 | * @param int $post_id Post ID of the post for which we are retrieving Related Posts. |
| 1336 | 1386 | */ |
| 1337 | - 'context' => apply_filters( | |
| 1387 | + 'context' => apply_filters( | |
| 1338 | 1388 | 'jetpack_relatedposts_filter_post_context', |
| 1339 | 1389 | $this->to_utf8( $this->generate_related_post_context( $post->ID ) ), |
| 1340 | 1390 | $post->ID |
| 1341 | 1391 | ), |
| 1342 | - 'img' => $this->generate_related_post_image_params( $post->ID ), | |
| 1392 | + // The context used when rendering as a Block. No filtering applied. | |
| 1393 | + 'block_context' => $this->generate_related_post_context_block( $post->ID ), | |
| 1394 | + 'img' => $this->generate_related_post_image_params( $post->ID ), | |
| 1343 | 1395 | /** |
| 1344 | 1396 | * Filter the post css classes added on HTML markup. |
| 1345 | 1397 | * |
| 1346 | 1398 | * @module related-posts |
| @@ -1349,9 +1401,9 @@ | ||
| 1349 | 1401 | * |
| 1350 | 1402 | * @param array array() CSS classes added on post HTML markup. |
| 1351 | 1403 | * @param string $post_id Post ID. |
| 1352 | 1404 | */ |
| 1353 | - 'classes' => apply_filters( | |
| 1405 | + 'classes' => apply_filters( | |
| 1354 | 1406 | 'jetpack_relatedposts_filter_post_css_classes', |
| 1355 | 1407 | array(), |
| 1356 | 1408 | $post->ID |
| 1357 | 1409 | ), |
| @@ -1376,11 +1428,14 @@ | ||
| 1376 | 1428 | apply_filters( 'the_title', $post_title, $post_id ) |
| 1377 | 1429 | ); |
| 1378 | 1430 | } |
| 1379 | 1431 | |
| 1380 | - $post_title = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post_content ) ), 5, '…' ); | |
| 1381 | - if ( ! empty( $post_title ) ) { | |
| 1382 | - 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 | + } | |
| 1383 | 1438 | } |
| 1384 | 1439 | |
| 1385 | 1440 | return __( 'Untitled Post', 'jetpack' ); |
| 1386 | 1441 | } |
| @@ -1389,16 +1444,20 @@ | ||
| 1389 | 1444 | * Returns a plain text post excerpt for title attribute of links. |
| 1390 | 1445 | * |
| 1391 | 1446 | * @param string $post_excerpt - the post excerpt. |
| 1392 | 1447 | * @param string $post_content - the post content. |
| 1448 | + * @param int $post_id - the post ID. | |
| 1393 | 1449 | * @uses strip_shortcodes, wp_strip_all_tags, wp_trim_words |
| 1394 | 1450 | * @return string |
| 1395 | 1451 | */ |
| 1396 | - protected function get_excerpt( $post_excerpt, $post_content ) { | |
| 1397 | - 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 { | |
| 1398 | 1459 | $excerpt = $post_content; |
| 1399 | - } else { | |
| 1400 | - $excerpt = $post_excerpt; | |
| 1401 | 1460 | } |
| 1402 | 1461 | |
| 1403 | 1462 | return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $excerpt ) ), 50, '…' ); |
| 1404 | 1463 | } |
| @@ -1404,12 +1463,12 @@ | ||
| 1404 | 1463 | } |
| 1405 | 1464 | |
| 1406 | 1465 | /** |
| 1407 | 1466 | * Generates the thumbnail image to be used for the post. Uses the |
| 1408 | - * image as returned by Jetpack_PostImages::get_image() | |
| 1467 | + * image as returned by Images::get_image() | |
| 1409 | 1468 | * |
| 1410 | 1469 | * @param int $post_id - the post ID. |
| 1411 | - * @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 | |
| 1412 | 1471 | * @return string |
| 1413 | 1472 | */ |
| 1414 | 1473 | protected function generate_related_post_image_params( $post_id ) { |
| 1415 | 1474 | $image_params = array( |
| @@ -1442,39 +1501,71 @@ | ||
| 1442 | 1501 | ); |
| 1443 | 1502 | } |
| 1444 | 1503 | |
| 1445 | 1504 | // Try to get post image. |
| 1446 | - if ( class_exists( 'Jetpack_PostImages' ) ) { | |
| 1447 | - $img_url = ''; | |
| 1448 | - $post_image = Jetpack_PostImages::get_image( | |
| 1449 | - $post_id, | |
| 1450 | - $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 | + ) | |
| 1451 | 1519 | ); |
| 1520 | + } | |
| 1521 | + $post_image = Images::get_image( $post_id, $image_args ); | |
| 1452 | 1522 | |
| 1453 | - if ( is_array( $post_image ) ) { | |
| 1454 | - $img_url = $post_image['src']; | |
| 1455 | - } elseif ( class_exists( 'Jetpack_Media_Summary' ) ) { | |
| 1456 | - $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 ); | |
| 1457 | 1527 | |
| 1458 | - if ( is_array( $media ) && ! empty( $media['image'] ) ) { | |
| 1459 | - $img_url = $media['image']; | |
| 1460 | - } | |
| 1528 | + if ( is_array( $media ) && ! empty( $media['image'] ) ) { | |
| 1529 | + $img_url = $media['image']; | |
| 1461 | 1530 | } |
| 1531 | + } | |
| 1462 | 1532 | |
| 1463 | - if ( ! empty( $img_url ) ) { | |
| 1464 | - if ( ! empty( $post_image['alt_text'] ) ) { | |
| 1465 | - $image_params['alt_text'] = $post_image['alt_text']; | |
| 1466 | - } else { | |
| 1467 | - $image_params['alt_text'] = ''; | |
| 1468 | - } | |
| 1469 | - $image_params['width'] = $thumbnail_size['width']; | |
| 1470 | - $image_params['height'] = $thumbnail_size['height']; | |
| 1471 | - $image_params['src'] = Jetpack_PostImages::fit_image_url( | |
| 1472 | - $img_url, | |
| 1473 | - $thumbnail_size['width'], | |
| 1474 | - $thumbnail_size['height'] | |
| 1475 | - ); | |
| 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'] = ''; | |
| 1476 | 1538 | } |
| 1539 | + | |
| 1540 | + $thumbnail_width = 0; | |
| 1541 | + $thumbnail_height = 0; | |
| 1542 | + | |
| 1543 | + if ( ! empty( $thumbnail_size['width'] ) ) { | |
| 1544 | + $thumbnail_width = $thumbnail_size['width']; | |
| 1545 | + $image_params['width'] = $thumbnail_width; | |
| 1546 | + } | |
| 1547 | + | |
| 1548 | + if ( ! empty( $thumbnail_size['height'] ) ) { | |
| 1549 | + $thumbnail_height = $thumbnail_size['height']; | |
| 1550 | + $image_params['height'] = $thumbnail_height; | |
| 1551 | + } | |
| 1552 | + | |
| 1553 | + $image_params['src'] = Images::fit_image_url( | |
| 1554 | + $img_url, | |
| 1555 | + $thumbnail_width, | |
| 1556 | + $thumbnail_height | |
| 1557 | + ); | |
| 1558 | + | |
| 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; | |
| 1567 | + } | |
| 1477 | 1568 | } |
| 1478 | 1569 | |
| 1479 | 1570 | return $image_params; |
| 1480 | 1571 | } |
| @@ -1545,8 +1636,9 @@ | ||
| 1545 | 1636 | * @uses wp_remote_post, is_wp_error, wp_remote_retrieve_body, get_post_meta, update_post_meta |
| 1546 | 1637 | * @return array |
| 1547 | 1638 | */ |
| 1548 | 1639 | protected function get_related_post_ids( $post_id, $size, array $filters ) { |
| 1640 | + $transient_name = null; | |
| 1549 | 1641 | $now_ts = time(); |
| 1550 | 1642 | $cache_meta_key = '_jetpack_related_posts_cache'; |
| 1551 | 1643 | |
| 1552 | 1644 | $body = array( |
| @@ -1666,8 +1758,24 @@ | ||
| 1666 | 1758 | return $filtered; |
| 1667 | 1759 | } |
| 1668 | 1760 | |
| 1669 | 1761 | /** |
| 1762 | + * Generates the author byline for the related post. | |
| 1763 | + * | |
| 1764 | + * @param int $post_id - the post ID. | |
| 1765 | + * @uses get_post_field, get_the_author_meta | |
| 1766 | + * @return string | |
| 1767 | + */ | |
| 1768 | + protected function generate_related_post_display_author( $post_id ) { | |
| 1769 | + $post_author = get_post_field( 'post_author', $post_id ); | |
| 1770 | + $author_display_name = get_the_author_meta( 'display_name', $post_author ); | |
| 1771 | + if ( ! empty( $author_display_name ) ) { | |
| 1772 | + return $author_display_name; | |
| 1773 | + } | |
| 1774 | + return ''; | |
| 1775 | + } | |
| 1776 | + | |
| 1777 | + /** | |
| 1670 | 1778 | * Generates a context for the related content (second line in related post output). |
| 1671 | 1779 | * Order of importance: |
| 1672 | 1780 | * - First category (Not 'Uncategorized') |
| 1673 | 1781 | * - First post tag |
| @@ -1676,13 +1784,69 @@ | ||
| 1676 | 1784 | * @param int $post_id - the post ID. |
| 1677 | 1785 | * @uses get_the_category, get_the_terms, get_comments_number, number_format_i18n, __, _n |
| 1678 | 1786 | * @return string |
| 1679 | 1787 | */ |
| 1788 | + protected function generate_related_post_context_block( $post_id ) { | |
| 1789 | + $categories = get_the_category( $post_id ); | |
| 1790 | + if ( is_array( $categories ) ) { | |
| 1791 | + foreach ( $categories as $category ) { | |
| 1792 | + if ( $category instanceof WP_Term && 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) { | |
| 1793 | + $cat_link = get_category_link( $category ); | |
| 1794 | + return array( | |
| 1795 | + 'text' => trim( $category->name ), | |
| 1796 | + 'link' => $cat_link, | |
| 1797 | + ); | |
| 1798 | + } | |
| 1799 | + } | |
| 1800 | + } | |
| 1801 | + $tags = get_the_terms( $post_id, 'post_tag' ); | |
| 1802 | + if ( is_array( $tags ) ) { | |
| 1803 | + foreach ( $tags as $tag ) { | |
| 1804 | + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) { | |
| 1805 | + $tag_link = get_tag_link( $tag ); | |
| 1806 | + return array( | |
| 1807 | + 'text' => trim( $tag->name ), | |
| 1808 | + 'link' => $tag_link, | |
| 1809 | + ); | |
| 1810 | + } | |
| 1811 | + } | |
| 1812 | + } | |
| 1813 | + $comment_count = get_comments_number( $post_id ); | |
| 1814 | + if ( $comment_count > 0 ) { | |
| 1815 | + $comments_string = sprintf( | |
| 1816 | + // Translators: amount of comments. | |
| 1817 | + _n( 'With %s comment', 'With %s comments', $comment_count, 'jetpack' ), | |
| 1818 | + number_format_i18n( $comment_count ) | |
| 1819 | + ); | |
| 1820 | + $comments_link = get_comments_link( $post_id ); | |
| 1821 | + return array( | |
| 1822 | + 'text' => $comments_string, | |
| 1823 | + 'link' => $comments_link, | |
| 1824 | + ); | |
| 1825 | + } | |
| 1826 | + $fallback_string = __( 'Similar post', 'jetpack' ); | |
| 1827 | + return array( | |
| 1828 | + 'text' => $fallback_string, | |
| 1829 | + 'link' => '', | |
| 1830 | + ); | |
| 1831 | + } | |
| 1832 | + | |
| 1833 | + /** | |
| 1834 | + * Generates a context for the related content (second line in related post output). | |
| 1835 | + * Order of importance: | |
| 1836 | + * - First category (Not 'Uncategorized') | |
| 1837 | + * - First post tag | |
| 1838 | + * - Number of comments | |
| 1839 | + * | |
| 1840 | + * @param int $post_id - the post ID. | |
| 1841 | + * @uses get_the_category, get_the_terms, get_comments_number, number_format_i18n, __, _n | |
| 1842 | + * @return string | |
| 1843 | + */ | |
| 1680 | 1844 | protected function generate_related_post_context( $post_id ) { |
| 1681 | 1845 | $categories = get_the_category( $post_id ); |
| 1682 | 1846 | if ( is_array( $categories ) ) { |
| 1683 | 1847 | foreach ( $categories as $category ) { |
| 1684 | - if ( 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) { | |
| 1848 | + if ( $category instanceof WP_Term && 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) { | |
| 1685 | 1849 | $post_cat_context = sprintf( |
| 1686 | 1850 | // Translators: The category or tag name. |
| 1687 | 1851 | esc_html_x( 'In "%s"', 'in {category/tag name}', 'jetpack' ), |
| 1688 | 1852 | $category->name |
| @@ -1704,9 +1868,9 @@ | ||
| 1704 | 1868 | |
| 1705 | 1869 | $tags = get_the_terms( $post_id, 'post_tag' ); |
| 1706 | 1870 | if ( is_array( $tags ) ) { |
| 1707 | 1871 | foreach ( $tags as $tag ) { |
| 1708 | - if ( '' !== trim( $tag->name ) ) { | |
| 1872 | + if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) { | |
| 1709 | 1873 | $post_tag_context = sprintf( |
| 1710 | 1874 | // Translators: the category or tag name. |
| 1711 | 1875 | _x( 'In "%s"', 'in {category/tag name}', 'jetpack' ), |
| 1712 | 1876 | $tag->name |
| @@ -1750,13 +1914,22 @@ | ||
| 1750 | 1914 | |
| 1751 | 1915 | /** |
| 1752 | 1916 | * Determines if the current post is able to use related posts. |
| 1753 | 1917 | * |
| 1754 | - * @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 | |
| 1755 | 1920 | * @return bool |
| 1756 | 1921 | */ |
| 1757 | 1922 | protected function enabled_for_request() { |
| 1758 | - $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 | |
| 1759 | 1932 | && ! is_attachment() |
| 1760 | 1933 | && ! is_admin() |
| 1761 | 1934 | && ! is_embed() |
| 1762 | 1935 | && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) ); |
| @@ -1761,9 +1934,9 @@ | ||
| 1761 | 1934 | && ! is_embed() |
| 1762 | 1935 | && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) ); |
| 1763 | 1936 | |
| 1764 | 1937 | /** |
| 1765 | - * 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. | |
| 1766 | 1939 | * |
| 1767 | 1940 | * @module related-posts |
| 1768 | 1941 | * |
| 1769 | 1942 | * @since 3.3.0 |
| @@ -1892,8 +2065,18 @@ | ||
| 1892 | 2065 | */ |
| 1893 | 2066 | public function rest_register_related_posts() { |
| 1894 | 2067 | /** This filter is already documented in class.json-api-endpoints.php */ |
| 1895 | 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 | + | |
| 1896 | 2079 | foreach ( $post_types as $post_type ) { |
| 1897 | 2080 | register_rest_field( |
| 1898 | 2081 | $post_type, |
| 1899 | 2082 | 'jetpack-related-posts', |
| @@ -1918,8 +2101,24 @@ | ||
| 1918 | 2101 | * |
| 1919 | 2102 | * @return array |
| 1920 | 2103 | */ |
| 1921 | 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 | + | |
| 1922 | 2121 | return $this->get_for_post_id( $object['id'], array( 'size' => 6 ) ); |
| 1923 | 2122 | } |
| 1924 | 2123 | } |
| 1925 | 2124 | |