Skip to content

Commit

Permalink
Enable related posts for AMP (#13028)
Browse files Browse the repository at this point in the history
* Enable related posts for AMP

* Don't enqueue JS for AMP

* Cautious extra check

* Improve names and comments

* Update modules/related-posts/jetpack-related-posts.php

Co-Authored-By: Jefferson Rabb <jefferson.rabb@automattic.com>

* Related Posts: PHPCS and Jetpack Codex-related changes
  • Loading branch information
gravityrail authored and zinigor committed Jul 29, 2019
1 parent 2c8fe44 commit 91c5676
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions modules/related-posts/jetpack-related-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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.
*
Expand All @@ -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 '';
}
Expand Down Expand Up @@ -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 '';
}
Expand Down Expand Up @@ -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'],
Expand All @@ -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(
Expand Down Expand Up @@ -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.
*
Expand All @@ -403,8 +432,9 @@ public function render_block( $attributes ) {
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
);
}
Expand Down Expand Up @@ -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.
*
Expand All @@ -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 );
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 91c5676

Please sign in to comment.