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

Fix checks for serving AMP response and improve AMP compatibility #10945

Merged
merged 8 commits into from
Jan 3, 2019

Conversation

gravityrail
Copy link
Contributor

@gravityrail gravityrail commented Dec 11, 2018

Fixes #9588
Fixes #9588
Fixes #10973

Based on #10918

Changes proposed in this Pull Request:

  • Make AMP checks later in boot sequence - AMP 1.0 requires that is_amp_endpoint() be called after parse_query.

This avoids messages like this being printed to the screen or debug.log:

[11-Dec-2018 18:10:52 UTC] PHP Notice:  is_amp_endpoint was called <strong>incorrectly</strong>. is_amp_endpoint() was called before the &#039;parse_query&#039; hook was called. This function will always return &#039;false&#039; before the &#039;parse_query&#039; hook is called. Please see <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information. (This message was added in version 0.4.2.) in /srv/www/wordpress-default/public_html/wp-includes/functions.php on line 4169

It also ensures that the stats pixel is loaded

Testing instructions:

  • Ensure WP_DEBUG and WP_DEBUG_DISPLAY are enabled
  • Ensure AMP 1.x is installed and active
  • Configure AMP for "Native" mode (should also try with "Paired" mode and "Classic" mode)
  • Enable Jetpack features and load pages with those features
  • Check that no errors are printed, and features are either working or hidden
  • Load pages with AMP disabled (or load regular pages with AMP not in "Native" mode) and ensure features still work correctly

Good features to check: Carousel, Likes, Gallery, social widgets

Proposed changelog entry for your changes:

  • Compatibility improvements with AMP plugin 1.0 (working stats pixel, remove warnings)

cc @westonruter

@matticbot
Copy link
Contributor

D22154-code. (newly created revision)

@gravityrail gravityrail added [Type] Bug When a feature is broken and / or not performing as intended [Status] Needs Review To request a review from Crew. Label will be renamed soon. labels Dec 11, 2018
@jetpackbot
Copy link

jetpackbot commented Dec 11, 2018

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

Scheduled Jetpack release: January 10, 2019.
Scheduled code freeze: January 3, 2019

Generated by 🚫 dangerJS against b4565df

@gravityrail gravityrail changed the title Westonruter fix/amp endpoint check Fix checks for serving AMP response and improve AMP compatibility Dec 11, 2018
@jeherve jeherve added AMP [Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. labels Dec 11, 2018
@jeherve jeherve added this to the 6.9 milestone Dec 11, 2018
westonruter
westonruter previously approved these changes Dec 11, 2018
Copy link
Collaborator

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

I deployed the changes to https://weston.ruter.net/ and I didn't notice any regressions or PHP log entries, though I don't have all modules active on my site.

@westonruter
Copy link
Collaborator

Note that this PR does not fix #9730 and #10314.

@jeherve jeherve added the [Status] Needs Testing We need to add this change to the testing call for this month's release label Dec 12, 2018
@gravityrail
Copy link
Contributor Author

Whoops! Thanks @westonruter

@westonruter
Copy link
Collaborator

Note that images on AMP pages are currently broken with the current version of Jetpack because lazy images aren't being disabled. This PR should be marked important, I believe.

@zinigor
Copy link
Member

zinigor commented Dec 13, 2018

I have tested this one a bit, seems to be working fine, but I have a question. Can we just filter out modules instead of introducing many checks into their code? We can have something like this for a predefined list of modules that shouldn't even be loaded if it's an AMP render:

	static function filter_modules( $modules ) {
		if ( ! self::is_amp_request() ) {
			return $modules;
		}

		return array_diff( $modules, array( 'lazy-images' ) );
	}

We can have this as a static method in class.jetpack-amp-support.php and hook it onto the jetpack_active_modules filter.

@westonruter
Copy link
Collaborator

I have tested this one a bit, seems to be working fine, but I have a question. Can we just filter out modules instead of introducing many checks into their code?

@zinigor I do not believe so because is_amp_endpoint() only works after the parse_query action. I assume that the filtering of the modules happens much earlier than that action, correct?

@zinigor
Copy link
Member

zinigor commented Dec 13, 2018

Oh, yes, you're right, it runs on plugins_loaded. OK then, multiple conditions work too.

@zinigor
Copy link
Member

zinigor commented Dec 13, 2018

I have found that with Masterbar enabled a logged in view seems to be way off, so we need to disable Masterbar as well. For that I had to change the way the module is initialized, @vindl can you please take a look and tell us if there are any caveats to doing this? The admin bar seems to work OK in non-AMP requests, but still, I'd love a second set of eyes.

@zinigor
Copy link
Member

zinigor commented Dec 13, 2018

Meanwhile I'm going to work on the wpcom build failure.

@gravityrail
Copy link
Contributor Author

@zinigor Just to confirm, yes, the reason I filter functionality rather than modules is because when Jetpack is loading its modules, we don't know yet whether the page is going to be rendered as AMP.

jeherve pushed a commit that referenced this pull request Dec 21, 2018
In order to synchronize code between Jetpack and WordPress.com better one of the little pieces we needed was the `is_module_active` method present in the Site Abstraction Layer. This change adds it as well as enforcing its existence in all extending classes.

#### Changes proposed in this Pull Request:
* Adds unit tests that will run on SAL classes.
* Adds the `is_module_active` method for SAL Sites.

#### Testing instructions:
* Functionally there's nothing to change, just run the tests.
* This should fail on wpcom check after being created, this is expected, because the method needs to be added on the other side as well.

#### Proposed changelog entry for your changes:
* Added base unit test suites for the SAL library.

This blocks #10945
@westonruter
Copy link
Collaborator

@dereksmart do you have stack traces for those?

@dereksmart
Copy link
Member

@westonruter

[03-Jan-2019 23:02:26 UTC] PHP Notice: Trying to get property 'post_type' of non-object in /home/dereksma/public_html/wp-content/plugins/amp/includes/class-amp-post-type-support.php on line 80
[03-Jan-2019 23:02:26 UTC] PHP Stack trace:
[03-Jan-2019 23:02:26 UTC] PHP 1. {main}() /home/dereksma/public_html/index.php:0
[03-Jan-2019 23:02:26 UTC] PHP 2. require() /home/dereksma/public_html/index.php:17
[03-Jan-2019 23:02:26 UTC] PHP 3. wp() /home/dereksma/public_html/wp-blog-header.php:16
[03-Jan-2019 23:02:26 UTC] PHP 4. WP->main() /home/dereksma/public_html/wp-includes/functions.php:1091
[03-Jan-2019 23:02:26 UTC] PHP 5. WP->query_posts() /home/dereksma/public_html/wp-includes/class-wp.php:739
[03-Jan-2019 23:02:26 UTC] PHP 6. WP_Query->query() /home/dereksma/public_html/wp-includes/class-wp.php:622
[03-Jan-2019 23:02:26 UTC] PHP 7. WP_Query->get_posts() /home/dereksma/public_html/wp-includes/class-wp-query.php:3365
[03-Jan-2019 23:02:26 UTC] PHP 8. WP_Query->parse_query() /home/dereksma/public_html/wp-includes/class-wp-query.php:1701
[03-Jan-2019 23:02:26 UTC] PHP 9. do_action_ref_array() /home/dereksma/public_html/wp-includes/class-wp-query.php:1050
[03-Jan-2019 23:02:26 UTC] PHP 10. WP_Hook->do_action() /home/dereksma/public_html/wp-includes/plugin.php:531
[03-Jan-2019 23:02:26 UTC] PHP 11. WP_Hook->apply_filters() /home/dereksma/public_html/wp-includes/class-wp-hook.php:310
[03-Jan-2019 23:02:26 UTC] PHP 12. jetpack_initialize_masterbar() /home/dereksma/public_html/wp-includes/class-wp-hook.php:286
[03-Jan-2019 23:02:26 UTC] PHP 13. Jetpack_AMP_Support::is_amp_request() /home/dereksma/public_html/wp-content/plugins/jetpack/modules/masterbar.php:23
[03-Jan-2019 23:02:26 UTC] PHP 14. is_amp_endpoint() /home/dereksma/public_html/wp-content/plugins/jetpack/3rd-party/class.jetpack-amp-support.php:55
[03-Jan-2019 23:02:26 UTC] PHP 15. AMP_Theme_Support::get_template_availability() /home/dereksma/public_html/wp-content/plugins/amp/includes/amp-helper-functions.php:286
[03-Jan-2019 23:02:26 UTC] PHP 16. AMP_Post_Type_Support::get_support_errors() /home/dereksma/public_html/wp-content/plugins/amp/includes/class-amp-theme-support.php:589
[03-Jan-2019 23:02:26 UTC] PHP Notice: Trying to get property 'ID' of non-object in /home/dereksma/public_html/wp-content/plugins/amp/includes/class-amp-post-type-support.php on line 101
[03-Jan-2019 23:02:26 UTC] PHP Stack trace:
[03-Jan-2019 23:02:26 UTC] PHP 1. {main}() /home/dereksma/public_html/index.php:0
[03-Jan-2019 23:02:26 UTC] PHP 2. require() /home/dereksma/public_html/index.php:17
[03-Jan-2019 23:02:26 UTC] PHP 3. wp() /home/dereksma/public_html/wp-blog-header.php:16
[03-Jan-2019 23:02:26 UTC] PHP 4. WP->main() /home/dereksma/public_html/wp-includes/functions.php:1091
[03-Jan-2019 23:02:26 UTC] PHP 5. WP->query_posts() /home/dereksma/public_html/wp-includes/class-wp.php:739
[03-Jan-2019 23:02:26 UTC] PHP 6. WP_Query->query() /home/dereksma/public_html/wp-includes/class-wp.php:622
[03-Jan-2019 23:02:26 UTC] PHP 7. WP_Query->get_posts() /home/dereksma/public_html/wp-includes/class-wp-query.php:3365
[03-Jan-2019 23:02:26 UTC] PHP 8. WP_Query->parse_query() /home/dereksma/public_html/wp-includes/class-wp-query.php:1701
[03-Jan-2019 23:02:26 UTC] PHP 9. do_action_ref_array() /home/dereksma/public_html/wp-includes/class-wp-query.php:1050
[03-Jan-2019 23:02:26 UTC] PHP 10. WP_Hook->do_action() /home/dereksma/public_html/wp-includes/plugin.php:531
[03-Jan-2019 23:02:26 UTC] PHP 11. WP_Hook->apply_filters() /home/dereksma/public_html/wp-includes/class-wp-hook.php:310
[03-Jan-2019 23:02:26 UTC] PHP 12. jetpack_initialize_masterbar() /home/dereksma/public_html/wp-includes/class-wp-hook.php:286
[03-Jan-2019 23:02:26 UTC] PHP 13. Jetpack_AMP_Support::is_amp_request() /home/dereksma/public_html/wp-content/plugins/jetpack/modules/masterbar.php:23
[03-Jan-2019 23:02:26 UTC] PHP 14. is_amp_endpoint() /home/dereksma/public_html/wp-content/plugins/jetpack/3rd-party/class.jetpack-amp-support.php:55
[03-Jan-2019 23:02:26 UTC] PHP 15. AMP_Theme_Support::get_template_availability() /home/dereksma/public_html/wp-content/plugins/amp/includes/amp-helper-functions.php:286
[03-Jan-2019 23:02:26 UTC] PHP 16. AMP_Post_Type_Support::get_support_errors() /home/dereksma/public_html/wp-content/plugins/amp/includes/class-amp-theme-support.php:589

@jeherve jeherve added [Status] Has Changelog and removed [Status] Needs Changelog [Status] Needs Testing We need to add this change to the testing call for this month's release labels Jan 3, 2019
jeherve added a commit that referenced this pull request Jan 3, 2019
@westonruter
Copy link
Collaborator

@dereksmart Thanks. It seems Jetpack is doing the right thing:

// In order to be able to tell if it's an AMP request or not we have to hook into parse_query at a later priority.
add_action( 'parse_query', 'jetpack_initialize_masterbar', 99 );
/**
* Initializes the Masterbar in case the request is not AMP.
*/
function jetpack_initialize_masterbar() {
if ( ! Jetpack_AMP_Support::is_amp_request() ) {
new A8C_WPCOM_Masterbar();
}
}

Here's the relevant code in the AMP plugin:

https://github.com/ampproject/amp-wp/blob/96b194da2f63234f37748e753f0434614983a2dc/includes/class-amp-theme-support.php#L581-L589

It seems that $query->is_singular() || $query->is_posts_page is true, and yet $query->get_queried_object() is returning null. So then this code in the AMP plugin is raising notices:

https://github.com/ampproject/amp-wp/blob/96b194da2f63234f37748e753f0434614983a2dc/includes/class-amp-post-type-support.php#L74-L101

I'm confused why is_singular would be true but the queried_object seems to be null.

jeherve added a commit that referenced this pull request Jan 3, 2019
* Add first version of the Changelog and testing list for 6.9

* Changelog: add #10710

* changelog: add #10538

* changelog: add #10741

* changelog: add #10749

* changelog: add #10664

* changelog: add #10224

* changelog: add #10788

* Changelog: add #10560

* Chanegelog: add #10812

* changelog: add #10556

* Changelog: add #10668

* Changelog: add #10846

* Changelog: add #10947

* Changelog: add #10962

* Changelog: add #10956

* Changelog: add #10940

* Changelog: add #10934

* Changelog: add #10912

* changelog: add #10866

* changelog: add #10924

* Changelog: add #10936

* Changelog: add #10833

* changelog: add #10867

* Changelog: add #10960

* Changelog: add #10888

* changelog: add #10840

* changelog: add #10972

* Changelog: add #10979

* changelog: add #10909

* Changelog: add #10958

* Changelog: add #10981

* Changelog: add #10564

* Changelog: add #10809

* Changelog: add #10982

* Changelog: add #10706

* Changelog: add #10978

* Changelog: add #10132

* Changelog: add #11022

* Changelog: add #11024

* Changelog: add #10875

* Changelog: add #11030

* Changelog: add #11053

* Changelog: add #10880

* Changelog: add #9359

* Changelog: add #11037

* Update block list

* Changelog: add #11060

* Changelog: add #10755

* changelog: add #11000

* Changelog: add #10786

* Changelog: add #10945

* Changelog: add #10597
@westonruter
Copy link
Collaborator

westonruter commented Jan 3, 2019

Seems the problem is that parse_query happens before the query actually has been made, so $wp_query->post is empty. If this is the case, then really is_amp_endpoint() should be disallowed before the wp action not the parse_query action.

@dereksmart If you do:

- add_action( 'parse_query', 'jetpack_initialize_masterbar', 99 ); 
+ add_action( 'wp', 'jetpack_initialize_masterbar', 99 ); 

Does that have the intended result in paired mode, where the AMP version continues to hide the masterbar but in the non-AMP version, the masterbar is shown?

@westonruter
Copy link
Collaborator

It looks like we'll have to make these changes to the AMP plugin as well, to warn when calling before wp rather than before parse_query:

diff --git a/includes/amp-helper-functions.php b/wp-content/plugins/amp/includes/amp-helper-functions.php
index 938eb40b..4a29b021 100644
--- a/includes/amp-helper-functions.php
+++ b/includes/amp-helper-functions.php
@@ -252,10 +252,10 @@ function is_amp_endpoint() {
 		return false;
 	}
 
-	$did_parse_query = did_action( 'parse_query' );
+	$did_parse_query = did_action( 'wp' );
 
 	if ( ! $did_parse_query ) {
-		_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( "is_amp_endpoint() was called before the 'parse_query' hook was called. This function will always return 'false' before the 'parse_query' hook is called.", 'amp' ) ), '0.4.2' );
+		_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( "is_amp_endpoint() was called before the 'wp' hook was called. This function will always return 'false' before the 'wp' hook is called.", 'amp' ) ), '0.4.2' );
 	}
 
 	$has_amp_query_var = (
diff --git a/includes/class-amp-post-type-support.php b/wp-content/plugins/amp/includes/class-amp-post-type-support.php
index 3863b1f2..a1dad0c4 100644
--- a/includes/class-amp-post-type-support.php
+++ b/includes/class-amp-post-type-support.php
@@ -75,6 +75,9 @@ class AMP_Post_Type_Support {
 		if ( ! ( $post instanceof WP_Post ) ) {
 			$post = get_post( $post );
 		}
+		if ( ! $post ) {
+			return array();
+		}
 		$errors = array();
 
 		if ( ! post_type_supports( $post->post_type, self::SLUG ) ) {
diff --git a/includes/class-amp-theme-support.php b/wp-content/plugins/amp/includes/class-amp-theme-support.php
index 215ce42c..97357edb 100644
--- a/includes/class-amp-theme-support.php
+++ b/includes/class-amp-theme-support.php
@@ -586,10 +586,12 @@ class AMP_Theme_Support {
 			 * @var WP_Post $queried_object
 			 */
 			$queried_object = $query->get_queried_object();
-			$support_errors = AMP_Post_Type_Support::get_support_errors( $queried_object );
-			if ( ! empty( $support_errors ) ) {
-				$matching_template['errors']    = array_merge( $matching_template['errors'], $support_errors );
-				$matching_template['supported'] = false;
+			if ( $queried_object ) {
+				$support_errors = AMP_Post_Type_Support::get_support_errors( $queried_object );
+				if ( ! empty( $support_errors ) ) {
+					$matching_template['errors']    = array_merge( $matching_template['errors'], $support_errors );
+					$matching_template['supported'] = false;
+				}
 			}
 		}

@westonruter
Copy link
Collaborator

I've opened an AMP plugin PR to warn when calling is_amp_endpoint() before wp action: ampproject/amp-wp#1794

@jeherve
Copy link
Member

jeherve commented Jan 4, 2019

I moved this remaining problem to a new issue: #11082

@dereksmart
Copy link
Member

dereksmart commented Jan 4, 2019

Thanks for digging @westonruter!

add_action( 'wp', 'jetpack_initialize_masterbar', 99 );

seems to somewhat work. I still see the masterbar in AMP mode, but there are no notices.

@jeherve jeherve mentioned this pull request Jan 21, 2019
3 tasks
jeherve added a commit that referenced this pull request Jan 23, 2019
This reverts some of the changes to Carousel that were made in #10945.
Instead, we try to defer the is_amp_endpoint until wp is loaded,
to avoid the problems raised in #11169.
jeherve added a commit that referenced this pull request Apr 24, 2019
Summary:
After adding the AMP compat file that ships with Jetpack (this was done in D22313, D26814, D26819, D26921, and D26928), let's start using this class.

This replaces D22154 and D23501, and allows us to start syncing any future changes that would rely on the Jetpack_AMP_Support class.

Related Jetpack PRs:
- #10945
- #11195, which reverted some of the changes in the PR above.
- #12054
- #12053
- #12026

Discussion:
- https://[private link]

We'll need to test for issues like the ones that popped up on Jetpack at the time:
#11169

We will also need to account for the fact that WordPress.com does not run the latest version of the AMP plugin. It runs an old version (0.6.2 vs. the current version on W.org 1.0.2), with its own version of some of the compatibility fixes that come with the class we've added (see [[ https://[private link].php | here ]]) and needs to be updated as per the discussions here:
- https://[private link] (D14521)
- https://[private link]

Test Plan:
* Create a post with a gallery
* Add a Facebook sharing button to that post.
* Try Liking that post.
* Comment on one of the images in the gallery.
* Load the post in an AMP view, and repeat the steps below.

In all cases:
- You should not see any js errors in the browser console.
- You should not get any PHP notices in your debug log.

Reviewers: zinigor

Tags: #touches_jetpack_files

Differential Revision: D26821-code

This commit syncs r190790-wpcom.
jeherve added a commit that referenced this pull request Apr 24, 2019
Summary:
After adding the AMP compat file that ships with Jetpack (this was done in D22313, D26814, D26819, D26921, and D26928), let's start using this class.

This replaces D22154 and D23501, and allows us to start syncing any future changes that would rely on the Jetpack_AMP_Support class.

Related Jetpack PRs:
- #10945
- #11195, which reverted some of the changes in the PR above.
- #12054
- #12053
- #12026

Discussion:
- https://[private link]

We'll need to test for issues like the ones that popped up on Jetpack at the time:
#11169

We will also need to account for the fact that WordPress.com does not run the latest version of the AMP plugin. It runs an old version (0.6.2 vs. the current version on W.org 1.0.2), with its own version of some of the compatibility fixes that come with the class we've added (see [[ https://[private link].php | here ]]) and needs to be updated as per the discussions here:
- https://[private link] (D14521)
- https://[private link]

Test Plan:
* Create a post with a gallery
* Add a Facebook sharing button to that post.
* Try Liking that post.
* Comment on one of the images in the gallery.
* Load the post in an AMP view, and repeat the steps below.

In all cases:
- You should not see any js errors in the browser console.
- You should not get any PHP notices in your debug log.

Reviewers: zinigor

Tags: #touches_jetpack_files

Differential Revision: D26821-code

This commit syncs r190790-wpcom.
'href' => $url,
);
if ( Jetpack_AMP_Support::is_amp_request() ) {
$menu['title'] = "<amp-img src='$img_src_2x' width=112 height=24 layout=fixed alt='$alt' title='$title'></amp-img>";
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is made obsolete by #13450.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AMP [Feature] Stats Data Feature that enables users to track their site's traffic and gain insights on popular content. Touches WP.com Files [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants