Prevent Jetpack features from breaking AMP validation with amp-wp#9458
Prevent Jetpack features from breaking AMP validation with amp-wp#9458gravityrail merged 12 commits intomasterfrom
Conversation
|
cc @westonruter, whose work this is based on, for review and comment. |
|
Regarding tiled galleries rendering as carousel, see ampproject/amp-wp#1065 |
| @@ -0,0 +1,141 @@ | |||
| <?php | |||
There was a problem hiding this comment.
Should this file go in the 3rd-party directory?
There was a problem hiding this comment.
Good call, will move there.
jeherve
left a comment
There was a problem hiding this comment.
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' ), |
There was a problem hiding this comment.
Should we leverage the existing jetpack_open_graph_image_default filter here?
| @@ -828,7 +830,7 @@ function sharing_display( $text = '', $echo = false ) { | |||
| * | |||
| * @param string $sharing_content Content markup of the Jetpack sharing links | |||
There was a problem hiding this comment.
Could you add the new parameter you've added here?
| return function_exists( 'amp_is_canonical' ) && amp_is_canonical(); | ||
| } | ||
|
|
||
| static function is_amp_request() { |
There was a problem hiding this comment.
Could we make this function filterable, so other AMP plugins like this one can leverage the work we've done in Jetpack?
There was a problem hiding this comment.
Good call. Done.
| return $widgets; | ||
| } | ||
|
|
||
| static function is_supported_widget( $widget_path ) { |
There was a problem hiding this comment.
What makes an AMP supported widget today? Are there any additional widgets we should add to that list, like the Twitter and Facebook widgets?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It looks like slideshow is also not supported, but we can address that in a follow-up.
There was a problem hiding this comment.
It appears gists don't work as well.
There was a problem hiding this comment.
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.
ebinnion
left a comment
There was a problem hiding this comment.
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' ); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 ); No newline at end of file |
There was a problem hiding this comment.
There should be an empty newline at the end of this file.
modules/lazy-images/lazy-images.php
Outdated
| } | ||
|
|
||
| public function setup_filters() { | ||
| if ( Jetpack_AMP_Support::is_amp_request() ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good call. Done.
| @@ -828,7 +830,7 @@ function sharing_display( $text = '', $echo = false ) { | |||
| * | |||
| * @param string $sharing_content Content markup of the Jetpack sharing links | |||
There was a problem hiding this comment.
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.
|
Thanks @enejb - I added your suggestion. |
| } | ||
|
|
||
| static function is_supported_widget( $widget_path ) { | ||
| return substr($widget_path, -14) !== '/milestone.php'; |
There was a problem hiding this comment.
Minor style nitpick: There should be spaces inside the parentheses.
There was a problem hiding this comment.
Fixed in new PR
| /** | ||
| * Add publisher and image metadata. | ||
| * | ||
| * @since 0.3 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ); No newline at end of file |
There was a problem hiding this comment.
Minor nitpick: There should an empty newline at the end of the file.
There was a problem hiding this comment.
Fixed in new PR
| } | ||
|
|
||
| // detect amp-wp and modify output as appropriate | ||
| require_once JETPACK__PLUGIN_DIR . '3rd-party/class.jetpack-amp-support.php'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good call, fixed.
| return $widgets; | ||
| } | ||
|
|
||
| static function is_supported_widget( $widget_path ) { |
There was a problem hiding this comment.
It looks like slideshow is also not supported, but we can address that in a follow-up.
ebinnion
left a comment
There was a problem hiding this comment.
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.
| 'width' => $size, | ||
| 'height' => $size, | ||
| ); | ||
| } else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) { |
There was a problem hiding this comment.
Note that Custom Logo in core should be used if available. See ampproject/amp-wp#975
| && | ||
| function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists | ||
| && | ||
| ( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) ); |
There was a problem hiding this comment.
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.
|
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; |
There was a problem hiding this comment.
We're missing a print here from moving everything around.
After #9458, the pixel was not printed to the front-end.
After #9458, the pixel was not printed to the front-end.
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.
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.
<amp-social-share><amp-pixel>for stats pixelSome implementation notes:
is_amp_endpoint()as it is not reliable when called duringinitor earlier. This is because it requiresparse_queryto have been run. Instead, we look for the amp query var or canonical amp support. We can go back to usingis_amp_endpoint()once Discontinue use of amp endpoint in favor of query var when amp theme support is present ampproject/amp-wp#1148 is resolved.Known issues:
Testing
npm run buildinside the checkout.Legacy AMP
Add
?amp=1to any post URL, e.g.http://example.com/2018/03/23/a-post/?amp=1All Jetpack features should work (or be hidden if broken) in this mode.
Canonical AMP
Put this in a plugin:
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
Canonical AMP
Changelog entry
AMP: Allow Jetpack features to work on AMP pages, and prevent Jetpack features from rendering to the front end at all.