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 | 153 |
1 files changed, 93 insertions, 60 deletions
diff --git a/plugins/jetpack/modules/related-posts/jetpack-related-posts.php b/plugins/jetpack/modules/related-posts/jetpack-related-posts.php index d1d4855f..aa9adefb 100644 --- a/plugins/jetpack/modules/related-posts/jetpack-related-posts.php +++ b/plugins/jetpack/modules/related-posts/jetpack-related-posts.php @@ -1,6 +1,10 @@ <?php + +use Automattic\Jetpack\Assets; +use Automattic\Jetpack\Sync\Settings; + class Jetpack_RelatedPosts { - const VERSION = '20190204'; + const VERSION = '20191011'; const SHORTCODE = 'jetpack-related-posts'; private static $instance = null; @@ -51,6 +55,7 @@ class Jetpack_RelatedPosts { * Constructor for Jetpack_RelatedPosts. * * @uses get_option, add_action, apply_filters + * * @return null */ public function __construct() { @@ -115,7 +120,7 @@ class Jetpack_RelatedPosts { */ public function action_frontend_init() { // Add a shortcode handler that outputs nothing, this gets overridden later if we can display related content - add_shortcode( self::SHORTCODE, array( $this, 'get_target_html_unsupported' ) ); + add_shortcode( self::SHORTCODE, array( $this, 'get_client_rendered_html_unsupported' ) ); if ( ! $this->_enabled_for_request() ) return; @@ -158,25 +163,53 @@ 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. + * 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. * * @filter the_content - * @param string $content + * + * @param string $content Post content. + * * @returns string */ public function filter_add_target_to_dom( $content ) { - if ( has_block( 'jetpack/related-posts', $content ) ) { + if ( has_block( 'jetpack/related-posts' ) ) { return $content; } - if ( ! $this->_found_shortcode ) { - $content .= "\n" . $this->get_target_html(); + if ( ! $this->_found_shortcode && ! doing_filter( 'get_the_excerpt' ) ) { + if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { + $content .= "\n" . $this->get_server_rendered_html(); + } else { + $content .= "\n" . $this->get_client_rendered_html(); + } } return $content; } /** + * Render static markup based on the Gutenberg block code + * + * @return string Rendered related posts HTML. + */ + public function get_server_rendered_html() { + $rp_settings = Jetpack_Options::get_option( 'relatedposts', array() ); + $block_rp_settings = array( + 'displayThumbnails' => $rp_settings['show_thumbnails'], + 'showHeadline' => $rp_settings['show_headline'], + 'displayDate' => isset( $rp_settings['show_date'] ) ? (bool) $rp_settings['show_date'] : true, + 'displayContext' => isset( $rp_settings['show_context'] ) && $rp_settings['show_context'], + 'postLayout' => isset( $rp_settings['layout'] ) ? $rp_settings['layout'] : 'grid', + 'postsToShow' => isset( $rp_settings['size'] ) ? $rp_settings['size'] : 3, + /** This filter is already documented in modules/related-posts/jetpack-related-posts.php */ + 'headline' => apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() ), + ); + + return $this->render_block( $block_rp_settings ); + } + + /** * Looks for our shortcode on the unfiltered content, this has to execute early. * * @filter the_content @@ -196,9 +229,8 @@ class Jetpack_RelatedPosts { * @uses esc_html__, apply_filters * @returns string */ - public function get_target_html() { - require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php'; - if ( Jetpack_Sync_Settings::is_syncing() ) { + public function get_client_rendered_html() { + if ( Settings::is_syncing() ) { return ''; } @@ -231,9 +263,8 @@ EOT; * * @returns string */ - public function get_target_html_unsupported() { - require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php'; - if ( Jetpack_Sync_Settings::is_syncing() ) { + public function get_client_rendered_html_unsupported() { + if ( Settings::is_syncing() ) { return ''; } return "\n\n<!-- Jetpack Related Posts is not supported in this context. -->\n\n"; @@ -262,18 +293,18 @@ EOT; ); $item_markup .= sprintf( - '<li class="jp-related-posts-i2__post-link"><a id="%1$s" href="%2$s" rel="%4$s">%3$s</a></li>', + '<li class="jp-related-posts-i2__post-link"><a id="%1$s" href="%2$s" %4$s>%3$s</a></li>', esc_attr( $label_id ), esc_url( $related_post['url'] ), esc_attr( $related_post['title'] ), - esc_attr( $related_post['rel'] ) + ( ! empty( $related_post['rel'] ) ? 'rel="' . esc_attr( $related_post['rel'] ) . '"' : '' ) ); 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" rel="%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" alt="%5$s" /></a></li>', esc_url( $related_post['url'] ), - esc_attr( $related_post['rel'] ), + ( ! 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']['alt_text'] ) @@ -331,6 +362,7 @@ EOT; */ public function render_block( $attributes ) { $block_attributes = array( + 'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : null, 'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'], 'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true, 'show_context' => isset( $attributes['displayContext'] ) && $attributes['displayContext'], @@ -338,7 +370,8 @@ EOT; 'size' => ! empty( $attributes['postsToShow'] ) ? absint( $attributes['postsToShow'] ) : 3, ); - $excludes = $this->parse_numeric_get_arg( 'relatedposts_origin' ); + $excludes = $this->parse_numeric_get_arg( 'relatedposts_origin' ); + $related_posts = $this->get_for_post_id( get_the_ID(), array( @@ -373,33 +406,26 @@ EOT; $rows_markup .= $this->render_block_row( $lower_row_posts, $block_attributes ); } - $target_to_dom_priority = has_filter( - 'the_content', - array( $this, 'filter_add_target_to_dom' ) - ); - remove_filter( - 'the_content', - array( $this, 'filter_add_target_to_dom' ), - $target_to_dom_priority - ); - /* - 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/attachment/ticket/45495/do_blocks.diff and - hopefully it makes to to the final RC of WP 5.1. - */ + * 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</nav>', + '<nav class="jp-relatedposts-i2" data-layout="%1$s">%2$s%3$s</nav>', esc_attr( $block_attributes['layout'] ), + $block_attributes['headline'], $rows_markup ); } @@ -417,7 +443,7 @@ EOT; * * @uses absint * - * @param string $arg Name of the GET variable + * @param string $arg Name of the GET variable. * @return array $result Parsed value(s) */ public function parse_numeric_get_arg( $arg ) { @@ -1187,11 +1213,12 @@ EOT; * @module related-posts * * @since 3.7.0 + * @since 7.9.0 - Change Default value to empty. * - * @param string nofollow Link rel attribute for Related Posts' link. Default is nofollow. - * @param int $post->ID Post ID. + * @param string $link_rel Link rel attribute for Related Posts' link. Default is empty. + * @param int $post->ID Post ID. */ - 'rel' => apply_filters( 'jetpack_relatedposts_filter_post_link_rel', 'nofollow', $post->ID ), + 'rel' => apply_filters( 'jetpack_relatedposts_filter_post_link_rel', '', $post->ID ), /** * Filter the context displayed below each Related Post. * @@ -1604,16 +1631,10 @@ EOT; */ protected function _enabled_for_request() { $enabled = is_single() + && ! is_attachment() && ! is_admin() && ( ! $this->_allow_feature_toggle() || $this->get_option( 'enabled' ) ); - if ( - class_exists( 'Jetpack_AMP_Support' ) - && Jetpack_AMP_Support::is_amp_request() - ) { - $enabled = false; - } - /** * Filter the Enabled value to allow related posts to be shown on pages as well. * @@ -1633,7 +1654,9 @@ EOT; * @return null */ protected function _action_frontend_init_page() { - $this->_enqueue_assets( true, true ); + + $enqueue_script = ! ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ); + $this->_enqueue_assets( $enqueue_script, true ); $this->_setup_shortcode(); add_filter( 'the_content', array( $this, 'filter_add_target_to_dom' ), 40 ); @@ -1650,7 +1673,7 @@ EOT; if ( $script ) { wp_enqueue_script( 'jetpack_related-posts', - Jetpack::get_file_url_for_environment( + Assets::get_file_url_for_environment( '_inc/build/related-posts/related-posts.min.js', 'modules/related-posts/related-posts.js' ), @@ -1672,9 +1695,14 @@ EOT; if ( $style ){ wp_enqueue_style( 'jetpack_related-posts', plugins_url( 'related-posts.css', __FILE__ ), array(), self::VERSION ); wp_style_add_data( 'jetpack_related-posts', 'rtl', 'replace' ); + add_action( 'amp_post_template_css', array( $this, 'render_amp_reader_mode_css' ) ); } } + public function render_amp_reader_mode_css() { + echo file_get_contents( plugin_dir_path( __FILE__ ) . 'related-posts.css' ); + } + /** * Sets up the shortcode processing. * @@ -1684,7 +1712,7 @@ EOT; protected function _setup_shortcode() { add_filter( 'the_content', array( $this, 'test_for_shortcode' ), 0 ); - add_shortcode( self::SHORTCODE, array( $this, 'get_target_html' ) ); + add_shortcode( self::SHORTCODE, array( $this, 'get_client_rendered_html' ) ); } protected function _allow_feature_toggle() { @@ -1719,14 +1747,19 @@ EOT; * @return null */ public function rest_register_related_posts() { - register_rest_field( 'post', - 'jetpack-related-posts', - array( - 'get_callback' => array( $this, 'rest_get_related_posts' ), - 'update_callback' => null, - 'schema' => null, - ) - ); + /** This filter is already documented in class.json-api-endpoints.php */ + $post_types = apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) ); + foreach ( $post_types as $post_type ) { + register_rest_field( + $post_type, + 'jetpack-related-posts', + array( + 'get_callback' => array( $this, 'rest_get_related_posts' ), + 'update_callback' => null, + 'schema' => null, + ) + ); + } } /** |