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

Add _doing_it_wrong() when calling is_amp_endpoint() before queried object is available #1794

Merged
merged 2 commits into from
Jan 4, 2019

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Jan 4, 2019

Ever since 0.4.2, the AMP plugin has issued a _doing_it_wrong() when is_amp_endpoint() is called before the parse_query action. This was needed to ensure that the amp query var was parsed and available for inspection.

It turns out that this did not go far enough. As of 1.0 the is_amp_endpoint() needs to look at the queried object as well to determine whether AMP has been enabled/disabled for a given post on singular queries. The queried object is not available at parse_query since the posts have not been queried yet at this point. Rather, it is only at the wp action that we can be assured that $wp_query->post has been populated and thus that the queried object is present for inspection.

Also make sure that only a WP_Post is passed into AMP_Post_Type_Support::get_support_errors().

Without the changes in this PR, if you have a plugin that does:

add_action( 'parse_query', function() {
	if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
		add_action( 'wp_footer', function() {
			echo "HELLO PAIRED AMP!";
		} );
		add_action( 'amp_post_template_footer', function() {
			echo "HELLO CLASSIC AMP!";
		} );
	} else {
		add_action( 'wp_footer', function() {
			echo "HELLO NOT AMP!";
		} );
	}
} );

You will get PHP notices when viewing a singular post in paired mode:

Notice: Trying to get property 'post_type' of non-object in /app/public/content/plugins/amp/includes/class-amp-post-type-support.php on line 80
Notice: Trying to get property 'ID' of non-object in /app/public/content/plugins/amp/includes/class-amp-post-type-support.php on line 101

Changing parse_query to wp fixes the issue in plugin code.

Issue discovered originally in a Jetpack PR: Automattic/jetpack#10945 (comment)

… action

Also make sure that only a WP_Post is passed into AMP_Post_Type_Support::get_support_errors()
@westonruter westonruter merged commit 4c7c3c8 into 1.0 Jan 4, 2019
@westonruter westonruter deleted the fix/is-endpoint-timing-check branch January 4, 2019 19:32
jeherve pushed a commit to Automattic/jetpack that referenced this pull request Jan 8, 2019
<!--- Provide a general summary of your changes in the Title above -->

Fixes #11082 
Fixes #11081

#### Changes proposed in this Pull Request:

* Defer masterbar setup until the `wp` action, instead of doing the setup at the `init` action. This is needed because the `is_amp_endpoint()` function cannot be called before the `wp` action, as it needs access to the queried object.

#### Testing instructions:
<!-- Please include detailed testing steps, explaining how to test your change. -->
<!-- Bear in mind that context you working on is not obvious for everyone.  -->
<!-- Adding "simple" configuration steps will help reviewers to get to your PR as quickly as possible. -->
<!-- "Before / After" screenshots can also be very helpful when the change is visual. -->

1. Ensure Jetpack's masterbar module is active.
2. Activate the AMP plugin v1.0.1
3. Enable the new paired mode via the AMP settings.
4. Navigate to a singular post.

Without the changes in this PR, there should be two PHP notices being raised:

```
( ! ) Notice: Trying to get property 'post_type' of non-object in .../amp/includes/class-amp-post-type-support.php on line 80

( ! ) Notice: Trying to get property 'ID' of non-object in .../amp/includes/class-amp-post-type-support.php on line 101
```

See also ampproject/amp-wp#1794 which will catch the incorrect usage of `is_amp_endpoint()`.

#### Proposed changelog entry for your changes:
<!-- Please do not leave this empty. If no changelog entry needed, state as such. -->

* Fix AMP integration with masterbar module.
jeherve pushed a commit to Automattic/jetpack that referenced this pull request Jan 8, 2019
<!--- Provide a general summary of your changes in the Title above -->

Fixes #11082 
Fixes #11081

#### Changes proposed in this Pull Request:

* Defer masterbar setup until the `wp` action, instead of doing the setup at the `init` action. This is needed because the `is_amp_endpoint()` function cannot be called before the `wp` action, as it needs access to the queried object.

#### Testing instructions:
<!-- Please include detailed testing steps, explaining how to test your change. -->
<!-- Bear in mind that context you working on is not obvious for everyone.  -->
<!-- Adding "simple" configuration steps will help reviewers to get to your PR as quickly as possible. -->
<!-- "Before / After" screenshots can also be very helpful when the change is visual. -->

1. Ensure Jetpack's masterbar module is active.
2. Activate the AMP plugin v1.0.1
3. Enable the new paired mode via the AMP settings.
4. Navigate to a singular post.

Without the changes in this PR, there should be two PHP notices being raised:

```
( ! ) Notice: Trying to get property 'post_type' of non-object in .../amp/includes/class-amp-post-type-support.php on line 80

( ! ) Notice: Trying to get property 'ID' of non-object in .../amp/includes/class-amp-post-type-support.php on line 101
```

See also ampproject/amp-wp#1794 which will catch the incorrect usage of `is_amp_endpoint()`.

#### Proposed changelog entry for your changes:
<!-- Please do not leave this empty. If no changelog entry needed, state as such. -->

* Fix AMP integration with masterbar module.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants