diff --git a/modules/related-posts/jetpack-related-posts.php b/modules/related-posts/jetpack-related-posts.php
index bf99ff56c8166..94bbdeb8e403a 100644
--- a/modules/related-posts/jetpack-related-posts.php
+++ b/modules/related-posts/jetpack-related-posts.php
@@ -120,7 +120,7 @@ public function action_admin_init() {
*/
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;
@@ -166,7 +166,9 @@ public function get_headline() {
* Will skip adding the target if the post content contains a Related Posts block.
*
* @filter the_content
- * @param string $content
+ *
+ * @param string $content Post content.
+ *
* @returns string
*/
public function filter_add_target_to_dom( $content ) {
@@ -175,12 +177,37 @@ public function filter_add_target_to_dom( $content ) {
}
if ( ! $this->_found_shortcode ) {
- $content .= "\n" . $this->get_target_html();
+ 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.
*
@@ -201,7 +228,7 @@ public function test_for_shortcode( $content ) {
* @uses esc_html__, apply_filters
* @returns string
*/
- public function get_target_html() {
+ public function get_client_rendered_html() {
if ( Settings::is_syncing() ) {
return '';
}
@@ -235,7 +262,7 @@ public function get_target_html() {
*
* @returns string
*/
- public function get_target_html_unsupported() {
+ public function get_client_rendered_html_unsupported() {
if ( Settings::is_syncing() ) {
return '';
}
@@ -334,6 +361,7 @@ public function render_block_row( $posts, $block_attributes ) {
*/
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'],
@@ -342,6 +370,17 @@ public function render_block( $attributes ) {
);
$excludes = $this->parse_numeric_get_arg( 'relatedposts_origin' );
+
+ $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
+ );
+
$related_posts = $this->get_for_post_id(
get_the_ID(),
array(
@@ -376,16 +415,6 @@ public function render_block( $attributes ) {
$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.
*
@@ -403,8 +432,9 @@ public function render_block( $attributes ) {
add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
return sprintf(
- '',
+ '',
esc_attr( $block_attributes['layout'] ),
+ $block_attributes['headline'],
$rows_markup
);
}
@@ -1613,13 +1643,6 @@ protected function _enabled_for_request() {
&& ! 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.
*
@@ -1639,7 +1662,9 @@ class_exists( 'Jetpack_AMP_Support' )
* @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 );
@@ -1690,7 +1715,7 @@ protected function _enqueue_assets( $script, $style ) {
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() {