diff options
Diffstat (limited to 'plugins/jetpack/modules/related-posts/jetpack-related-posts.php')
-rw-r--r-- | plugins/jetpack/modules/related-posts/jetpack-related-posts.php | 92 |
1 files changed, 50 insertions, 42 deletions
diff --git a/plugins/jetpack/modules/related-posts/jetpack-related-posts.php b/plugins/jetpack/modules/related-posts/jetpack-related-posts.php index 125e2889..1927e21c 100644 --- a/plugins/jetpack/modules/related-posts/jetpack-related-posts.php +++ b/plugins/jetpack/modules/related-posts/jetpack-related-posts.php @@ -1,10 +1,11 @@ <?php use Automattic\Jetpack\Assets; +use Automattic\Jetpack\Blocks; use Automattic\Jetpack\Sync\Settings; class Jetpack_RelatedPosts { - const VERSION = '20191011'; + const VERSION = '20211209'; const SHORTCODE = 'jetpack-related-posts'; private static $instance = null; @@ -72,7 +73,7 @@ class Jetpack_RelatedPosts { // Add Related Posts to the REST API Post response. add_action( 'rest_api_init', array( $this, 'rest_register_related_posts' ) ); - jetpack_register_block( + Blocks::jetpack_register_block( 'jetpack/related-posts', array( 'render_callback' => array( $this, 'render_block' ), @@ -111,7 +112,7 @@ class Jetpack_RelatedPosts { } /** - * Load related posts assets if it's a elegiable front end page or execute search and return JSON if it's an endpoint request. + * Load related posts assets if it's an eligible front end page or execute search and return JSON if it's an endpoint request. * * @global $_GET * @action wp @@ -163,8 +164,8 @@ class Jetpack_RelatedPosts { /** * Adds a target to the post content to load related posts into if a shortcode for it did not already exist. - * Will skip adding the target if the post content contains a Related Posts block or if the 'get_the_excerpt' - * hook is in the current filter list. + * Will skip adding the target if the post content contains a Related Posts block, if the 'get_the_excerpt' + * hook is in the current filter list, or if the site is running an FSE/Site Editor theme. * * @filter the_content * @@ -173,7 +174,7 @@ class Jetpack_RelatedPosts { * @returns string */ public function filter_add_target_to_dom( $content ) { - if ( has_block( 'jetpack/related-posts' ) ) { + if ( has_block( 'jetpack/related-posts' ) || Blocks::is_fse_theme() ) { return $content; } @@ -302,11 +303,12 @@ EOT; if ( ! empty( $block_attributes['show_thumbnails'] ) && ! empty( $related_post['img']['src'] ) ) { $img_link = sprintf( - '<li class="jp-related-posts-i2__post-img-link"><a href="%1$s" %2$s><img src="%3$s" width="%4$s" alt="%5$s" /></a></li>', + '<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>', esc_url( $related_post['url'] ), ( ! empty( $related_post['rel'] ) ? 'rel="' . esc_attr( $related_post['rel'] ) . '"' : '' ), esc_url( $related_post['img']['src'] ), esc_attr( $related_post['img']['width'] ), + esc_attr( $related_post['img']['height'] ), esc_attr( $related_post['img']['alt_text'] ) ); @@ -406,22 +408,6 @@ EOT; $rows_markup .= $this->render_block_row( $lower_row_posts, $block_attributes ); } - /* - * Below is a hack to get the block content to render correctly. - * - * This functionality should be covered in /inc/blocks.php but due to an error, - * this has not been fixed as of this writing. - * - * Alda has submitted a patch to Core in order to have this issue fixed at - * https://core.trac.wordpress.org/ticket/45495 and - * made it into WordPress 5.2. - * - * @todo update when WP 5.2 is the minimum support version. - */ - $priority = has_filter( 'the_content', 'wpautop' ); - remove_filter( 'the_content', 'wpautop', $priority ); - add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 ); - return sprintf( '<nav class="jp-relatedposts-i2" data-layout="%1$s">%2$s%3$s</nav>', esc_attr( $block_attributes['layout'] ), @@ -1154,7 +1140,7 @@ EOT; foreach ( array_merge( $with_post_thumbnails, $no_post_thumbnails ) as $index => $real_post ) { $related_posts[ $index ]['id'] = $real_post->ID; $related_posts[ $index ]['url'] = esc_url( get_permalink( $real_post ) ); - $related_posts[ $index ]['title'] = $this->_to_utf8( $this->_get_title( $real_post->post_title, $real_post->post_content ) ); + $related_posts[ $index ]['title'] = $this->_to_utf8( $this->get_title( $real_post->post_title, $real_post->post_content, $real_post->ID ) ); $related_posts[ $index ]['date'] = get_the_date( '', $real_post ); $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' ); $related_posts[ $index ]['img'] = $this->_generate_related_post_image_params( $real_post->ID ); @@ -1200,13 +1186,16 @@ EOT; $post = get_post( $post_id ); return array( - 'id' => $post->ID, - 'url' => get_permalink( $post->ID ), - 'url_meta' => array( 'origin' => $origin, 'position' => $position ), - 'title' => $this->_to_utf8( $this->_get_title( $post->post_title, $post->post_content ) ), - 'date' => get_the_date( '', $post->ID ), - 'format' => get_post_format( $post->ID ), - 'excerpt' => html_entity_decode( $this->_to_utf8( $this->_get_excerpt( $post->post_excerpt, $post->post_content ) ), ENT_QUOTES, 'UTF-8' ), + 'id' => $post->ID, + 'url' => get_permalink( $post->ID ), + 'url_meta' => array( + 'origin' => $origin, + 'position' => $position, + ), + 'title' => $this->_to_utf8( $this->get_title( $post->post_title, $post->post_content, $post->ID ) ), + 'date' => get_the_date( '', $post->ID ), + 'format' => get_post_format( $post->ID ), + 'excerpt' => html_entity_decode( $this->_to_utf8( $this->_get_excerpt( $post->post_excerpt, $post->post_content ) ), ENT_QUOTES, 'UTF-8' ), /** * Filters the rel attribute for the Related Posts' links. * @@ -1256,14 +1245,20 @@ EOT; /** * Returns either the title or a small excerpt to use as title for post. * - * @param string $post_title - * @param string $post_content - * @uses strip_shortcodes, wp_trim_words, __ + * @uses strip_shortcodes, wp_trim_words, __, apply_filters + * + * @param string $post_title Post title. + * @param string $post_content Post content. + * @param int $post_id Post ID. + * * @return string */ - protected function _get_title( $post_title, $post_content ) { + protected function get_title( $post_title, $post_content, $post_id ) { if ( ! empty( $post_title ) ) { - return wp_strip_all_tags( $post_title ); + return wp_strip_all_tags( + /** This filter is documented in core/src/wp-includes/post-template.php */ + apply_filters( 'the_title', $post_title, $post_id ) + ); } $post_title = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post_content ) ), 5, '…' ); @@ -1633,6 +1628,7 @@ EOT; $enabled = is_single() && ! is_attachment() && ! is_admin() + && ! is_embed() && ( ! $this->_allow_feature_toggle() || $this->get_option( 'enabled' ) ); /** @@ -1648,29 +1644,41 @@ EOT; } /** - * Adds filters and enqueues assets. + * Adds filters. * * @uses self::_enqueue_assets, self::_setup_shortcode, add_filter * @return null */ protected function _action_frontend_init_page() { - - $enqueue_script = ! ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ); - $this->_enqueue_assets( $enqueue_script, true ); + $this->_enqueue_assets( true, true ); $this->_setup_shortcode(); add_filter( 'the_content', array( $this, 'filter_add_target_to_dom' ), 40 ); } /** + * Determines if the scripts need be enqueued. + * + * @return bool + */ + protected function requires_scripts() { + return ( + ! ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) && + ! has_block( 'jetpack/related-posts' ) && + ! Blocks::is_fse_theme() + ); + } + + /** * Enqueues assets needed to do async loading of related posts. * * @uses wp_enqueue_script, wp_enqueue_style, plugins_url * @return null */ protected function _enqueue_assets( $script, $style ) { - $dependencies = is_customize_preview() ? array( 'customize-base' ) : array( 'jquery' ); - if ( $script ) { + $dependencies = is_customize_preview() ? array( 'customize-base' ) : array(); + // Do not enqueue scripts unless they are required. + if ( $script && $this->requires_scripts() ) { wp_enqueue_script( 'jetpack_related-posts', Assets::get_file_url_for_environment( |