Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable related posts for AMP #13028

Merged
merged 6 commits into from
Jul 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
gravityrail marked this conversation as resolved.
Show resolved Hide resolved
*
* @return string Rendered related posts HTML.
*/
public function get_server_rendered_html() {
$rp_settings = Jetpack_Options::get_option( 'relatedposts', array() );
jeherve marked this conversation as resolved.
Show resolved Hide resolved
$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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably out of scope of this PR, but is Related Posts supposed to appear in the loop? The doc comment implies not, but I'm seeing them on a test homepage (both on this branch and master).

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