-
Notifications
You must be signed in to change notification settings - Fork 384
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
Default to ?amp=1 query parameter #4312
Conversation
@schlessera in regards to:
How do you unit test these? AFAIK you can't manipulate the underlying data source that |
Starting this PR anew; this will focus on enforcing the |
9455eaa
to
d0ea373
Compare
Plugin builds for ac94637 are ready 🛎️!
|
includes/amp-helper-functions.php
Outdated
* @return bool True if the AMP query parameter is set with the required value, false if not. | ||
*/ | ||
function amp_has_query_var() { | ||
return isset( $_GET[ amp_get_slug() ] ) && 1 === filter_var( $_GET[ amp_get_slug() ], FILTER_VALIDATE_INT ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
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 don't think the value should need to be required to be 1
. It is just 1
as a generic non-empty value.
return isset( $_GET[ amp_get_slug() ] ) && 1 === filter_var( $_GET[ amp_get_slug() ], FILTER_VALIDATE_INT ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
return isset( $_GET[ amp_get_slug() ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
includes/class-amp-theme-support.php
Outdated
if ( isset( $path_args[ amp_get_slug() ] ) && '' !== $path_args[ amp_get_slug() ] ) { | ||
if ( isset( $path_args[ amp_get_slug() ] ) && '1' !== $path_args[ 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.
I don't think this change is quite right. This code here is asking if the URL looks like this /foo/amp/bar/
, where there is something after the /amp/
(like for feeds there is /feed/rss/
and /feed/atom/
). The $path_args
is not the query vars from $_GET
but rather wat was matched from the rewrite rule. So for a request /foo/amp/bar/
, here $path_args[ amp_get_slug() ]
would be set to bar
. If it is any non-empty string value, then this indicates a redirect is needed to avoid serving an infinite URL space.
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.
Yes that makes sense. I've made the changes in 83ec4a8.
Co-authored-by: Weston Ruter <westonruter@google.com>
…nt/2204-default-amp-endpoint * 'develop' of github.com:ampproject/amp-wp: (386 commits) Improve indentation Only show warning notice to users with `manage_options` capability Reword notice Include note as to why the WP and GB versions were chosen Sanitize translation Let the user know if the current Gutenberg or WordPress version is not supported Update dependency @wordpress/eslint-plugin to v7.2.1 Update dependency eslint-plugin-jsdoc to v30.6.3 Update dependency copy-webpack-plugin to v6.2.0 Update dependency @babel/plugin-proposal-class-properties to v7.10.4 Update dependency eslint-plugin-import to v2.22.1 Add missing docblocks Fix image importing for travel blog Update dependency mini-css-extract-plugin to v0.11.3 Update dependency postcss-loader to v4.0.3 Fix style difference between Gutenberg and Core Revert "Fix visual inconsistency between Core and Gutenberg" Fix visual inconsistency between Core and Gutenberg Make download errors visible Adapt image links in travel blog site ...
includes/amp-helper-functions.php
Outdated
* @todo Rename this to clear up confusion with amp_get_current_url(). | ||
* | ||
* @param string $url URL. | ||
* @return string AMP URL. | ||
*/ | ||
function amp_get_url( $url ) { |
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 have amp_get_current_url()
and amp_get_asset_url()
and amp_get_customizer_url()
. These being here, I think amp_get_url()
is too ambiguous.
How about we call it amp_get_paired_url()
since its purpose is to get the paired AMP URL for the current page.
We also have a \AMP_Theme_Support::get_current_canonical_url()
which could be extracted into a separate global function called amp_get_canonical_url()
.
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 in b66c233
includes/amp-helper-functions.php
Outdated
* @todo Rename this to be more agnostic to what the URL contains. | ||
* | ||
* @return bool True if the AMP query parameter is set with the required value, false if not. | ||
* @global WP_Query $wp_query | ||
*/ | ||
function amp_has_query_var() { |
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.
Given that in the future a paired AMP URL will be able to be indicated arbitrarily, such as via a subdomain, I think we can anticipate that by making amp_has_query_var()
more abstract.
We have amp_is_request()
to indicate that the request is being made for an AMP page. What we need is a method that says that the request was made for a paired AMP URL. What about amp_is_paired_request()
? I'm not entirely satisfied with that since it implies that it entails amp_is_request()
, being more specific. It's actually just giving an indication that the current URL is for paired AMP.
Maybe amp_is_paired_url()
? We could pass it a $url
argument as well, and it could tell you if that URL is paired rather than the current request to WP. This would be useful in other contexts, like in the AMP-to-AMP Linking sanitizer perhaps.
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.
Thinking about this some more… what about amp_is_paired_endpoint
? Using “endpoint” terminology would align with what we already have in amp_remove_endpoint()
(which we could technically rename to amp_remove_paired_endpoint()
).
While in a WordPress context, an “endpoint” refers to the end of the URL, in reality an API endpoint doesn't necessarily refer to any physical part of the URL. Normally the endpoint will still be at the end (?amp=1
) but sites can customize the paired AMP endpoint URLs to look however they want.
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 suppose then we'd rename amp_get_paired_url( $url )
to amp_get_paired_endpoint( $url )
.
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 basically need CRUD functions for manipulating paired AMP URLs:
- Read:
has
/is
- Create/Update:
get
- Delete:
remove
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.
So:
- Read:
amp_is_paired_endpoint( $url )
- Create/Update:
amp_get_paired_endpoint( $url )
- Delete:
amp_remove_paired_endpoint( $url )
(formerlyamp_remove_endpoint()
)
Thoughts?
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.
Using “endpoint” terminology would align with what we already have in amp_remove_endpoint() (which we could technically rename to amp_remove_paired_endpoint()).
That reasoning makes sense. I'm also in support of renaming the functions to represent the CRUD methods of manipulating the paired URL 👍.
Sidenote: That means amp_remove_endpoint()
would have to be deprecated and annotated accordingly since its documented.
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.
Here, check this out: d5610f2
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.
Fixes #2062
It appears that this does not actually fix that issue.
With Legacy AMP Reader mode active, I tried going to /category/uncategorized/amp/
and I was taken to a 404 not being redirected to /category/uncategorized/?amp=1
as I expected.
@pierlon That being the case, should we just remove that Fixes
to address in another PR?
$this->assertStringEndsWith( '&', amp_get_permalink( $published_post ) ); | ||
$this->assertStringEndsWith( '&', amp_get_permalink( $drafted_post ) ); | ||
$this->assertStringEndsWith( '&', amp_get_permalink( $published_page ) ); | ||
|
||
remove_filter( 'amp_pre_get_permalink', [ $this, 'return_example_url' ] ); | ||
remove_filter( 'amp_get_permalink', [ $this, 'return_example_url' ] ); | ||
$this->assertStringEndsWith( '&=1', amp_get_permalink( $published_post ) ); | ||
$this->assertStringEndsWith( '&=1', amp_get_permalink( $drafted_post ) ); | ||
$this->assertStringEndsWith( '&=1', amp_get_permalink( $published_page ) ); | ||
add_filter( 'amp_get_permalink', [ $this, 'return_example_url' ], 10, 2 ); | ||
$this->assertStringNotContains( 'current_filter=amp_get_permalink', amp_get_permalink( $published_post ) ); // Filter does not apply. | ||
$this->assertStringEndsWith( 'current_filter=amp_get_permalink', amp_get_permalink( $published_post ) ); | ||
add_filter( 'amp_pre_get_permalink', [ $this, 'return_example_url' ], 10, 2 ); | ||
$this->assertStringNotContains( 'current_filter=amp_pre_get_permalink', amp_get_permalink( $published_post ) ); // Filter does not apply. | ||
$this->assertStringEndsWith( 'current_filter=amp_pre_get_permalink', amp_get_permalink( $published_post ) ); |
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.
Nice. Looks like you found a bug in the test here.
Yes let's do that. I'll update the PR description. Also, reviewed the recent changes and it's a 👍 from me. |
I find the naming to be confusing, for the reason that I don't think it's a bad idea to use |
OK, that makes sense. In any case, there is no reference to CRUD in the codebase. |
Alright. CRUD really is about manipulating entities. That is not the case here. That being said, I think the names are good and make sense to me. |
I wonder whether it is clear for users, though... I was just now thinking about the following constellation:
|
That would work, though |
Yes, the following would work:
I guess I have an issue with the |
My only concern with |
Here's an alternative suggestion:
|
Note we currently have |
cf. |
…nt/2204-default-amp-endpoint * 'develop' of github.com:ampproject/amp-wp: Fix hard-coded SSR tests Change string rendition of float numbers for sizers Update spec tests Ensure iframes in embeds with aspect ratios get responsive layout (#5486) Include additional sandbox tokens for converted iframes (#5483) Fix alignment for phpcs Update dependency sirbrillig/phpcs-variable-analysis to v2.9.0 Fix PHPStan issue Update dependency terser-webpack-plugin to v4.2.3 Update dependency mini-css-extract-plugin to v0.12.0 Add issue reference to TODO Add expiry to stylesheet cache transients that exceed cache expiry Add test to assert stylesheet cache is not autoloaded Fix rendering translations in JS (#5461) Extract regex into constant Add tests for other doctypes Fix PSR2.Methods.FunctionCallSignature Ensure at least one space after doctype Always normalize to use HTML5 doctype
I'm happy with the naming but we can adjust further in a subsequent PR. |
Summary
Closes #4442
Relates to #2204
This PR enforces the
?amp=1
query parameter throughout the plugin.