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

Prevent Jetpack features from breaking AMP validation with amp-wp #9458

Merged
merged 12 commits into from
May 17, 2018

Conversation

gravityrail
Copy link
Contributor

@gravityrail gravityrail commented May 1, 2018

This patch does the minimum necessary to allow Jetpack features to work on AMP pages, and in some cases to prevent Jetpack features from rendering to the front end at all. This deprecates the Jetpack helper included in AMP, and some parts of the WPCOM helper - see this PR.

Q. Why don't we want some features on the front end at all?
A. Because in some cases features aren't possible, due to the limitations of AMP. In other cases, it would require a lot of work to make a feature AMP-compatible and in the meantime we want pages to validate correctly without broken-looking output.

This also includes WPCOM support where possible, fixing ampproject/amp-wp#1021

Future work should be done to ensure that these features can be used fully with AMP.

Some implementation notes:

Known issues:

  • Masterbar doesn't appear on AMP, probably needs to be rewritten.
  • Tiled galleries display as slideshow

Testing

Legacy AMP

Add ?amp=1 to any post URL, e.g. http://example.com/2018/03/23/a-post/?amp=1

All Jetpack features should work (or be hidden if broken) in this mode.

Canonical AMP

Put this in a plugin:

function add_amp_theme_support() {
	add_theme_support( 'amp' );
}

add_action( 'plugins_loaded', 'add_amp_theme_support' );

This will set the home page to use "canonical amp", which the the preferred way of using AMP after 1.0.

All Jetpack features should work (or be hidden if broken) in this mode.

Now try using various Jetpack features and make sure they don't break validation (or simply break).

Screenshots

Legacy AMP

Before After
goldsounds ngrok io_2018_03_23_another-amp-post__amp 1 goldsounds ngrok io_2018_03_23_another-amp-post__amp 1 1

Canonical AMP

Before After
goldsounds ngrok io_2018_03_23_another-amp-post_ ipad goldsounds ngrok io_2018_03_23_another-amp-post_ ipad 1

Changelog entry

AMP: Allow Jetpack features to work on AMP pages, and prevent Jetpack features from rendering to the front end at all.

@gravityrail gravityrail requested a review from a team as a code owner May 1, 2018 19:51
@gravityrail gravityrail changed the title AMP hackery Prevent Jetpack features from breaking AMP validation with amp-wp May 14, 2018
@gravityrail
Copy link
Contributor Author

cc @westonruter, whose work this is based on, for review and comment.

@gravityrail gravityrail self-assigned this May 15, 2018
@westonruter
Copy link
Contributor

Regarding tiled galleries rendering as carousel, see ampproject/amp-wp#1065

@gravityrail gravityrail requested a review from philipjohn May 15, 2018 18:23
@jeherve jeherve added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it General [Status] In Progress [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] In Progress labels May 15, 2018
@@ -0,0 +1,141 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this file go in the 3rd-party directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, will move there.

Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

Just a few questions about the current implementation.

static function add_fallback_image_to_metadata( $metadata ) {
$metadata['image'] = array(
'@type' => 'ImageObject',
'url' => self::staticize_subdomain( 'https://wordpress.com/i/blank.jpg' ),
Copy link
Member

Choose a reason for hiding this comment

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

Should we leverage the existing jetpack_open_graph_image_default filter here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -828,7 +830,7 @@ function sharing_display( $text = '', $echo = false ) {
*
* @param string $sharing_content Content markup of the Jetpack sharing links
Copy link
Member

Choose a reason for hiding this comment

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

Could you add the new parameter you've added here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
}

static function is_amp_request() {
Copy link
Member

Choose a reason for hiding this comment

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

Could we make this function filterable, so other AMP plugins like this one can leverage the work we've done in Jetpack?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Done.

return $widgets;
}

static function is_supported_widget( $widget_path ) {
Copy link
Member

Choose a reason for hiding this comment

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

What makes an AMP supported widget today? Are there any additional widgets we should add to that list, like the Twitter and Facebook widgets?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Almost certainly there are other compatibility issues; this PR aims only to port the fixes that have already been implemented by XWP in the amp-wp plugin. We should do a larger audit of other things that aren't working so well, but I didn't want this patch to get too big before we merge it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like slideshow is also not supported, but we can address that in a follow-up.

Copy link
Contributor

Choose a reason for hiding this comment

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

It appears gists don't work as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Really all of the core widgets work in AMP, except for the dropdown mode for Categories/Archives, which require a non-JS solution for navigating to the selected option. We should extend the widgets in Jetpack with the necessary checks for is_amp_endpoint() to output the necessary AMP components.

Copy link
Contributor

@ebinnion ebinnion left a comment

Choose a reason for hiding this comment

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

I left a few minor comments. I tested against your amp-wp patch and things seem to work well.

* @see https://github.com/Automattic/amp-wp
*/
class Jetpack_AMP_Support {
// static $modules_to_disable = array( 'likes', 'comment-likes', 'related-posts', 'carousel', 'photon', 'lazy-images', 'notes' );
Copy link
Contributor

Choose a reason for hiding this comment

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

We should remove this before merging.

// enforce freedom mode for videopress
add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );

// DONE
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we should track this in an issue instead of in the code?

// this is necessary since for better or worse Jetpack modules are loaded during plugins_loaded, which means we must
// take the opportunity to intercept initialisation before that point, either by adding explicit detection into the module,
// or preventing it from loading in the first place (better for performance)
add_action( 'plugins_loaded', array( 'Jetpack_AMP_Support', 'init_filter_jetpack_modules' ), 1 );
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be an empty newline at the end of this file.

@@ -50,6 +50,9 @@ private function __construct() {
}

public function setup_filters() {
if ( Jetpack_AMP_Support::is_amp_request() ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since lazy images loads on the wp hook, I think we could probably just hook like this:

add_filter( 'lazyload_is_enabled', '__return_false' );

This would allow us to bail as lazy images is initializing so that we don't have to place checks in multiple spots.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Done.

@@ -828,7 +830,7 @@ function sharing_display( $text = '', $echo = false ) {
*
* @param string $sharing_content Content markup of the Jetpack sharing links
Copy link
Contributor

Choose a reason for hiding this comment

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

We should update this docblock to reflect the new $enabled parameter and probably add a second @since. Perhaps something like:

@since 6.2.0 Started sending $enabled as a second parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@gravityrail
Copy link
Contributor Author

Thanks @enejb - I added your suggestion.

}

static function is_supported_widget( $widget_path ) {
return substr($widget_path, -14) !== '/milestone.php';
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor style nitpick: There should be spaces inside the parentheses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in new PR

/**
* Add publisher and image metadata.
*
* @since 0.3
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these methods be 6.2.0 instead? I imagine that they're brought over from amp-wp and were probably added in that version there.

Perhaps it could be something like:

@since 0.3 In amp-wp plugin
@since 6.2.0 Ported to Jetpack

I'm not sure. It's also pretty minor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in new PR

// this is necessary since for better or worse Jetpack modules and widget files are loaded during plugins_loaded, which means we must
// take the opportunity to intercept initialisation before that point, either by adding explicit detection into the module,
// or preventing it from loading in the first place (better for performance)
add_action( 'plugins_loaded', array( 'Jetpack_AMP_Support', 'init_filter_jetpack_widgets' ), 1 );
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick: There should an empty newline at the end of the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in new PR

@@ -584,6 +584,9 @@ private function __construct() {
Jetpack_Search_Performance_Logger::init();
}

// detect amp-wp and modify output as appropriate
require_once JETPACK__PLUGIN_DIR . '3rd-party/class.jetpack-amp-support.php';
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're just running this in the construct without a specific condition, should it go in the main 3rd-party file instead? That file should be loaded before the devicepx method in the Jetpack class is called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, fixed.

return $widgets;
}

static function is_supported_widget( $widget_path ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like slideshow is also not supported, but we can address that in a follow-up.

Copy link
Contributor

@ebinnion ebinnion left a comment

Choose a reason for hiding this comment

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

This looks good to me and tests well. I left some minor comments that are probably best addressed in a follow-up PR. I'll leave that up to you though.

@gravityrail gravityrail merged commit 1bd4eaa into master May 17, 2018
@gravityrail gravityrail deleted the add/amp-wp-support branch May 17, 2018 20:03
@gravityrail gravityrail mentioned this pull request May 17, 2018
@jeherve jeherve added [Status] Needs Testing We need to add this change to the testing call for this month's release and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels May 17, 2018
'width' => $size,
'height' => $size,
);
} else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that Custom Logo in core should be used if available. See ampproject/amp-wp#975

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this as an issue: #9587

&&
function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists
&&
( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Until ampproject/amp-wp#1148 is implemented, this should also check if the URL path ends in /amp/. Otherwise, AMP URLs on sites with amp theme support like https://make.xwp.co/2018/05/03/wordpress-amp-plugin-0-7-release/amp/ won't get recognized as an AMP request here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this as an issue: #9588

@kraftbj
Copy link
Contributor

kraftbj commented May 22, 2018

I'm trying to track an issue I'm seeing on my personal site where the stats pixel isn't loading on normal requests (but working on AMP requests). The timing of this merge lines up. Anyone else seeing anything similar?

_stq.push([ 'clickTrackerInit', '{$data['blog']}', '{$data['post']}' ]);
</script>

END;
Copy link
Contributor

Choose a reason for hiding this comment

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

We're missing a print here from moving everything around.

kraftbj added a commit that referenced this pull request May 22, 2018
After #9458, the pixel was not printed to the front-end.
oskosk pushed a commit that referenced this pull request May 24, 2018
After #9458, the pixel was not printed to the front-end.
@matticbot matticbot added [Status] Needs Changelog [Status] Needs Testing We need to add this change to the testing call for this month's release [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it AMP General Touches WP.com Files labels May 28, 2018
oskosk added a commit that referenced this pull request May 28, 2018
@jeherve jeherve removed the [Status] Needs Testing We need to add this change to the testing call for this month's release label Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AMP General Touches WP.com Files [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
8 participants