-
Notifications
You must be signed in to change notification settings - Fork 805
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
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.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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' ), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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' ); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
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.
modules/lazy-images/lazy-images.php
Outdated
@@ -50,6 +50,9 @@ private function __construct() { | |||
} | |||
|
|||
public function setup_filters() { | |||
if ( Jetpack_AMP_Support::is_amp_request() ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
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.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
'width' => $size, | ||
'height' => $size, | ||
); | ||
} else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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() ] ) ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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.
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.
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 duringinit
or earlier. This is because it requiresparse_query
to 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 build
inside the checkout.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:
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.