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

Default to ?amp=1 query parameter #4312

Merged
merged 18 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4b770da
Remove unused parameter in call to AMP_Theme_Support::ensure_proper_a…
pierlon Aug 29, 2020
d0ea373
Default to ?amp=1 query parameter
pierlon Aug 30, 2020
f806254
Remove amp_url filter
pierlon Aug 30, 2020
83ec4a8
Revert making the value '1' required for the AMP query var
pierlon Sep 22, 2020
b05bb24
Merge branch 'develop' of github.com:ampproject/amp-wp into enhanceme…
westonruter Oct 5, 2020
aaf0e2e
Ensure amp_remove_endpoint removes repeated /amp/ slugs
westonruter Oct 5, 2020
ecd9860
Extend amp_has_query_var() to look at WP_Query for query vars
westonruter Oct 5, 2020
123a64f
Add value of 1 to amp query var added in AMP-to-AMP linking
westonruter Oct 5, 2020
52a6fbf
Reuse amp_has_query_var() in AMP_Theme_Support::finish_init()
westonruter Oct 5, 2020
cf221bd
Remove obsolete /amp/=>?amp redirection in ensure_proper_amp_location()
westonruter Oct 5, 2020
b66c233
Rename amp_get_url() to amp_get_paired_url()
westonruter Oct 6, 2020
d5610f2
Assemble CRUD functions for paired AMP endpoints
westonruter Oct 6, 2020
aafcaec
Skip test_theme_data_exists when Neve already installed
westonruter Oct 6, 2020
04f53d9
Remove hard-coded dependency on amp=1 query var for AMP-to-AMP linking
westonruter Oct 6, 2020
ce34a99
Replace usages of amp_remove_endpoint() with amp_remove_paired_endpoi…
westonruter Oct 7, 2020
0931662
Use strings for query var values
westonruter Oct 7, 2020
0f7a3a9
Use amp_has_paired_endpoint() and amp_add_paired_endpoint() naming
westonruter Oct 9, 2020
ac94637
Merge branch 'develop' of github.com:ampproject/amp-wp into enhanceme…
westonruter Oct 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions includes/admin/class-amp-post-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function enqueue_admin_assets() {
'ampPostMetaBox.boot( %s );',
wp_json_encode(
[
'previewLink' => esc_url_raw( add_query_arg( amp_get_slug(), '', get_preview_post_link( $post ) ) ),
'previewLink' => esc_url_raw( amp_get_url( get_preview_post_link( $post ) ) ),
'canonical' => amp_is_canonical(),
'enabled' => empty( $support_errors ),
'canSupport' => 0 === count( array_diff( $support_errors, [ 'post-status-disabled' ] ) ),
Expand Down Expand Up @@ -447,7 +447,7 @@ public function preview_post_link( $link ) {
);

if ( $is_amp ) {
$link = add_query_arg( amp_get_slug(), true, $link );
$link = amp_get_url( $link );
}

return $link;
Expand Down
110 changes: 55 additions & 55 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ function amp_redirect_old_slug_to_new_url( $link ) {

if ( amp_is_request() && ! amp_is_canonical() ) {
if ( ! amp_is_legacy() ) {
$link = add_query_arg( amp_get_slug(), '', $link );
$link = amp_get_url( $link );
} else {
$link = trailingslashit( trailingslashit( $link ) . amp_get_slug() );
}
Expand Down Expand Up @@ -699,16 +699,6 @@ function amp_get_current_url() {
* @return string AMP permalink.
*/
function amp_get_permalink( $post_id ) {

// When theme support is present (i.e. not using legacy Reader post templates), the plain query var should always be used.
if ( ! amp_is_legacy() ) {
$permalink = get_permalink( $post_id );
if ( ! amp_is_canonical() ) {
$permalink = add_query_arg( amp_get_slug(), '', $permalink );
}
return $permalink;
}

/**
* Filters the AMP permalink to short-circuit normal generation.
*
Expand All @@ -727,35 +717,7 @@ function amp_get_permalink( $post_id ) {
}

$permalink = get_permalink( $post_id );

if ( amp_is_canonical() ) {
$amp_url = $permalink;
} else {
$parsed_url = wp_parse_url( get_permalink( $post_id ) );
$structure = get_option( 'permalink_structure' );
$use_query_var = (
// If pretty permalinks aren't available, then query var must be used.
empty( $structure )
||
// If there are existing query vars, then always use the amp query var as well.
! empty( $parsed_url['query'] )
||
// If the post type is hierarchical then the /amp/ endpoint isn't available.
is_post_type_hierarchical( get_post_type( $post_id ) )
||
// Attachment pages don't accept the /amp/ endpoint.
'attachment' === get_post_type( $post_id )
);
if ( $use_query_var ) {
$amp_url = add_query_arg( amp_get_slug(), '', $permalink );
} else {
$amp_url = preg_replace( '/#.*/', '', $permalink );
$amp_url = trailingslashit( $amp_url ) . user_trailingslashit( amp_get_slug(), 'single_amp' );
if ( ! empty( $parsed_url['fragment'] ) ) {
$amp_url .= '#' . $parsed_url['fragment'];
}
}
}
$amp_url = amp_is_canonical() ? $permalink : amp_get_url( $permalink );

/**
* Filters AMP permalink.
Expand All @@ -778,12 +740,20 @@ function amp_get_permalink( $post_id ) {
* @return string URL with AMP stripped.
*/
function amp_remove_endpoint( $url ) {
$slug = amp_get_slug();

// Strip endpoint, including /amp/, /amp/amp/, /amp/foo/.
$url = preg_replace(
sprintf(
':(/%s(/[^/?#]+)?)+(?=/?(\?|#|$)):',
preg_quote( $slug, ':' )
),
'',
$url
);

// Strip endpoint.
$url = preg_replace( ':/' . preg_quote( amp_get_slug(), ':' ) . '(?=/?(\?|#|$)):', '', $url );

// Strip query var.
$url = remove_query_arg( amp_get_slug(), $url );
// Strip query var, including ?amp, ?amp=1, etc.
$url = remove_query_arg( $slug, $url );

return $url;
}
Expand Down Expand Up @@ -836,7 +806,7 @@ function amp_add_amphtml_link() {
}

if ( AMP_Theme_Support::is_paired_available() ) {
$amp_url = add_query_arg( amp_get_slug(), '', amp_get_current_url() );
$amp_url = amp_get_url( amp_get_current_url() );
} else {
$amp_url = amp_get_permalink( get_queried_object_id() );
}
Expand Down Expand Up @@ -899,13 +869,7 @@ function amp_is_request() {
$is_amp_url = (
amp_is_canonical()
||
isset( $_GET[ amp_get_slug() ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
||
(
$wp_query instanceof WP_Query
&&
false !== $wp_query->get( amp_get_slug(), false )
)
amp_has_query_var()
);

// If AMP is not available, then it's definitely not an AMP endpoint.
Expand Down Expand Up @@ -1914,7 +1878,7 @@ function amp_add_admin_bar_view_link( $wp_admin_bar ) {
} elseif ( is_singular() ) {
$href = amp_get_permalink( get_queried_object_id() ); // For sake of Reader mode.
} else {
$href = add_query_arg( amp_get_slug(), '', amp_get_current_url() );
$href = amp_get_url( amp_get_current_url() );
}

$href = remove_query_arg( QueryVar::NOAMP, $href );
Expand Down Expand Up @@ -1949,7 +1913,7 @@ function amp_add_admin_bar_view_link( $wp_admin_bar ) {
if ( amp_is_legacy() ) {
$args['href'] = add_query_arg( 'autofocus[panel]', AMP_Template_Customizer::PANEL_ID, $args['href'] );
} else {
$args['href'] = add_query_arg( amp_get_slug(), '1', $args['href'] );
$args['href'] = amp_get_url( $args['href'] );
}
$wp_admin_bar->add_node( $args );
}
Expand Down Expand Up @@ -1983,3 +1947,39 @@ function amp_generate_script_hash( $script ) {
);
return 'sha384-' . $hash;
}

/**
* Get the AMP version of a URL.
*
* @since 2.1
* @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 ) {
Copy link
Member

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().

Copy link
Member

Choose a reason for hiding this comment

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

Done in b66c233

return add_query_arg( amp_get_slug(), '1', $url );
}

/**
* Determine whether the current request has the AMP query parameter set.
*
* @since 2.1
* @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() {
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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 ).

Copy link
Member

@westonruter westonruter Oct 6, 2020

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

Copy link
Member

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 ) (formerly amp_remove_endpoint())

Thoughts?

Copy link
Contributor Author

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.

Copy link
Member

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

global $wp_query;
$slug = amp_get_slug();
return (
isset( $_GET[ $slug ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
||
(
$wp_query instanceof WP_Query
&&
false !== $wp_query->get( $slug, false )
)
);
}
47 changes: 14 additions & 33 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,14 @@ public static function finish_init() {
add_filter( 'template_include', [ __CLASS__, 'serve_paired_browsing_experience' ], PHP_INT_MAX );
}

$has_query_var = (
isset( $_GET[ amp_get_slug() ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
||
false !== get_query_var( amp_get_slug(), false )
);

if ( ! amp_is_request() ) {
/*
* Redirect to AMP-less URL if AMP is not available for this URL and yet the query var is present.
* Temporary redirect is used for admin users because implied transitional mode and template support can be
* enabled by user ay any time, so they will be able to make AMP available for this URL and see the change
* without wrestling with the redirect cache.
*/
if ( $has_query_var ) {
if ( amp_has_query_var() ) {
self::redirect_non_amp_url( current_user_can( 'manage_options' ) ? 302 : 301 );
}

Expand Down Expand Up @@ -390,13 +384,11 @@ static function() {
*
* @since 1.0
* @since 2.0 Removed $exit param.
* @since 2.1 Remove obsolete redirection from /amp/ to ?amp when on non-legacy Reader mode.
*
* @return bool Whether redirection should have been done.
*/
public static function ensure_proper_amp_location() {
$has_query_var = false !== get_query_var( amp_get_slug(), false ); // May come from URL param or endpoint slug.
$has_url_param = isset( $_GET[ amp_get_slug() ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( amp_is_canonical() ) {
/*
* When AMP-first/canonical, then when there is an /amp/ endpoint or ?amp URL param,
Expand All @@ -405,34 +397,23 @@ public static function ensure_proper_amp_location() {
* should happen infrequently. For admin users, this is kept temporary to allow them
* to not be hampered by browser remembering permanent redirects and preventing test.
*/
if ( $has_query_var || $has_url_param ) {
if ( amp_has_query_var() ) {
return self::redirect_non_amp_url( current_user_can( 'manage_options' ) ? 302 : 301 );
}
} elseif ( amp_is_legacy() && is_singular() ) {
// Prevent infinite URL space under /amp/ endpoint.
} elseif ( amp_has_query_var() ) {
/*
* Prevent infinite URL space under /amp/ endpoint. Note that WordPress allows endpoints to have a value,
* such as the case of /feed/ where /feed/atom/ is the same as saying ?feed=atom. In this case, we need to
* check for /amp/x/ to protect against links like `<a href="./amp/">AMP!</a>`.
* See https://github.com/ampproject/amp-wp/pull/1846.
*/
global $wp;
$path_args = [];
wp_parse_str( $wp->matched_query, $path_args );
if ( isset( $path_args[ amp_get_slug() ] ) && '' !== $path_args[ amp_get_slug() ] ) {
if ( wp_safe_redirect( amp_get_permalink( get_queried_object_id() ), 301 ) ) {
// @codeCoverageIgnoreStart
exit;
// @codeCoverageIgnoreEnd
}
return true;
}
} elseif ( $has_query_var && ! $has_url_param ) {
/*
* When in AMP transitional mode *with* theme support, then the proper AMP URL has the 'amp' URL param
* and not the /amp/ endpoint. The URL param is now the exclusive way to mark AMP in transitional mode
* when amp theme support present. This is important for plugins to be able to reliably call
* amp_is_request() before the parse_query action.
*/
$old_url = amp_get_current_url();
$new_url = add_query_arg( amp_get_slug(), '', amp_remove_endpoint( $old_url ) );
if ( $old_url !== $new_url ) {
// A temporary redirect is used for admin users to allow them to see changes between reader mode and transitional modes.
if ( wp_safe_redirect( $new_url, current_user_can( 'manage_options' ) ? 302 : 301 ) ) {
$current_url = amp_get_current_url();
$redirect_url = amp_get_url( amp_remove_endpoint( $current_url ) );
if ( $current_url !== $redirect_url && wp_safe_redirect( $redirect_url, 301 ) ) {
// @codeCoverageIgnoreStart
exit;
// @codeCoverageIgnoreEnd
Expand Down Expand Up @@ -1132,7 +1113,7 @@ public static function amend_comment_form() {
}

/**
* Amend the comments/redpond links to go to non-AMP page when in legacy Reader mode.
* Amend the comments/respond links to go to non-AMP page when in legacy Reader mode.
*
* @see get_comments_link()
* @see comments_popup_link()
Expand Down
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-core-block-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private function process_archives_widgets( Document $dom, $args = [] ) {
*
* @var DOMElement $option
*/
$option->setAttribute( 'value', add_query_arg( amp_get_slug(), '', $option->getAttribute( 'value' ) ) );
$option->setAttribute( 'value', amp_get_url( $option->getAttribute( 'value' ) ) );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static function ( $supported ) {
&&
get_stylesheet() === $options[ Option::READER_THEME ]
&&
! isset( $_GET[ amp_get_slug() ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
! amp_has_query_var()
) {
/*
* When Reader mode is selected and a Reader theme has been chosen, if the active theme switches to be the
Expand Down
2 changes: 1 addition & 1 deletion includes/sanitizers/class-amp-form-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function sanitize() {
// Record that action was converted to action-xhr.
$action_url = add_query_arg( AMP_HTTP::ACTION_XHR_CONVERTED_QUERY_VAR, 1, $action_url );
if ( ! amp_is_canonical() ) {
$action_url = add_query_arg( amp_get_slug(), '', $action_url );
$action_url = amp_get_url( $action_url );
}
$node->setAttribute( 'action-xhr', $action_url );
// Append success/error handlers if not found.
Expand Down
2 changes: 1 addition & 1 deletion includes/sanitizers/class-amp-link-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private function process_element( DOMElement $element, $attribute_name ) {

// Only add the AMP query var when requested (in Transitional or Reader mode).
if ( ! empty( $this->args['paired'] ) ) {
$query_vars[ amp_get_slug() ] = '';
$query_vars[ amp_get_slug() ] = '1'; // @todo Would be preferable to use amp_get_url() somehow here.
}
}

Expand Down
2 changes: 1 addition & 1 deletion includes/templates/amp-paired-browsing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

$url = remove_query_arg( [ AMP_Theme_Support::PAIRED_BROWSING_QUERY_VAR, QueryVar::NOAMP ] );
$non_amp_url = add_query_arg( QueryVar::NOAMP, QueryVar::NOAMP_MOBILE, $url );
$amp_url = add_query_arg( amp_get_slug(), '1', $url );
$amp_url = amp_get_url( $url );
?>

<!DOCTYPE html>
Expand Down
2 changes: 1 addition & 1 deletion includes/validation/class-amp-validated-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ public static function get_url_from_post( $post ) {

// Add AMP query var if in transitional mode.
if ( ! amp_is_canonical() ) {
$url = add_query_arg( amp_get_slug(), '', $url );
$url = amp_get_url( $url );
}

// Set URL scheme based on whether HTTPS is current.
Expand Down
2 changes: 1 addition & 1 deletion includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public static function add_admin_bar_menu_items( $wp_admin_bar ) {
$current_url
);
if ( ! amp_is_canonical() ) {
$amp_url = add_query_arg( amp_get_slug(), '', $amp_url );
$amp_url = amp_get_url( $amp_url );
}

$validate_url = AMP_Validated_URL_Post_Type::get_recheck_url( AMP_Validated_URL_Post_Type::get_invalid_url_post( $amp_url ) ?: $amp_url );
Expand Down
2 changes: 1 addition & 1 deletion src/MobileRedirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function sanitize_options( $options, $new_options ) {
* @return string AMP URL.
*/
public function get_current_amp_url() {
$url = add_query_arg( amp_get_slug(), '1', amp_get_current_url() );
$url = amp_get_url( amp_get_current_url() );
$url = remove_query_arg( QueryVar::NOAMP, $url );
return $url;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PluginSuppression.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function is_reader_theme_request() {
&&
ReaderThemes::DEFAULT_READER_THEME !== AMP_Options_Manager::get_option( Option::READER_THEME )
&&
isset( $_GET[ amp_get_slug() ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
amp_has_query_var()
);
}

Expand Down
11 changes: 1 addition & 10 deletions src/ReaderThemeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@ public function is_theme_overridden() {
return $this->theme_overridden;
}

/**
* Is an AMP request.
*
* @return bool Whether AMP request.
*/
public function is_amp_request() {
return isset( $_GET[ amp_get_slug() ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}

/**
* Register the service with the system.
*
Expand Down Expand Up @@ -311,7 +302,7 @@ public function get_active_theme() {
*/
public function override_theme() {
$this->theme_overridden = false;
if ( ! $this->is_enabled() || ! $this->is_amp_request() ) {
if ( ! $this->is_enabled() || ! amp_has_query_var() ) {
return;
}

Expand Down
Loading