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

Detect /amp/ suffix when determining AMP request #9625

Merged
merged 4 commits into from
May 24, 2018

Conversation

gravityrail
Copy link
Contributor

@gravityrail gravityrail commented May 23, 2018

Most AMP sites are currently configured to use the /amp/ suffix as the canonical path to AMP content.

In Jetpack's AMP support code, we need to hook earlier than parse_query in order to properly intercept and modify Jetpack's output, however this means we can't use the standard means to detect /amp/ from WP_Query and must roll out own solution that assumes that /amp/ is truly the AMP path for the site.

The downside is that if the site has changed it to something else, e.g. /mobile/ or /lite/, we won't detect it. The upside is that 99% of sites just go with the default, so this provides a workable solution for them.

cc @westonruter

@gravityrail gravityrail requested a review from a team as a code owner May 23, 2018 21:51
@gravityrail gravityrail added this to the 6.2 milestone May 23, 2018
@gravityrail gravityrail added AMP [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels May 23, 2018
@gravityrail gravityrail self-assigned this May 23, 2018
@@ -78,6 +78,11 @@ function_exists( 'amp_is_canonical' ) // this is really just testing if the plug
return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
}

static function has_amp_suffix() {
$request_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably want to use wp_parse_url() instead of parse_url(). Prior to PHP 5.4, parse_url() didn't support relative URLs such as parsing the bare path. Alternatively, it would probably be better to use strtok instead:

$request_path = strtok( $_SERVER['REQUEST_URI'], '?#' );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@@ -78,6 +78,11 @@ function_exists( 'amp_is_canonical' ) // this is really just testing if the plug
return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
}

static function has_amp_suffix() {
$request_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
return $request_path && substr_compare( $request_path, '/amp/', -4, 5, true );
Copy link
Contributor

@westonruter westonruter May 23, 2018

Choose a reason for hiding this comment

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

This wouldn't handle the case when a user has filtered user_trailingslashit to not use trailing slashes. I think it would be better to do:

return (bool) preg_match( '#/amp/?$#i', $request_path );

This will ensure that the slash is optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@@ -66,7 +66,7 @@ static function is_amp_request() {
&&
function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists
&&
( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) );
( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) || self::has_amp_suffix() );
Copy link
Contributor

Choose a reason for hiding this comment

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

The last part in this conditional should be wrapped in a check for AMP__VERSION being less than 1.0 since with ampproject/amp-wp#1148 there will no longer be an /amp/ endpoint, and this will help guard against the case when a user publishes a page about guitar amps at like /guitar/amp/, to avoid ambiguity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@gravityrail
Copy link
Contributor Author

Feedback addressed, thanks @westonruter

@gravityrail gravityrail merged commit de978b5 into master May 24, 2018
@gravityrail gravityrail deleted the fix/detect-amp-suffix branch May 24, 2018 17:11
@jeherve jeherve removed the [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. label May 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants