Skip to content

Commit

Permalink
Prevent appending /amp/ endpoint to URLs which already have rewrite e…
Browse files Browse the repository at this point in the history
…ndpoint(s)
  • Loading branch information
westonruter committed Nov 8, 2020
1 parent a07a6d4 commit d9b724b
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/PairedAmpRouting.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use AmpProject\AmpWP\Infrastructure\Service;
use AmpProject\AmpWP\Admin\ReaderThemes;
use WP_Query;
use WP_Post;
use WP_Rewrite;

/**
* Service for routing users to and from paired AMP URLs.
Expand All @@ -41,6 +41,13 @@ final class PairedAmpRouting implements Service, Registerable, Activateable, Dea
Option::PAIRED_URL_STRUCTURE_CUSTOM,
];

/**
* Regular expression pattern matching any added endpoints in a URL.
*
* @var string
*/
protected $added_rewrite_endpoints_pattern;

/**
* Activate.
*
Expand Down Expand Up @@ -256,6 +263,36 @@ public function get_query_var_paired_amp_url( $url ) {
return add_query_arg( amp_get_slug(), '1', $url );
}

/**
* Determine whether a given URL path contains a registered rewrite endpoint.
*
* This is needed to prevent adding the `/amp/` rewrite endpoint to a URL which already has an existing rewrite
* endpoint, as in this case it will fail to match.
*
* @param string $path URL Path.
* @return bool Whether the supplied path contains a registered rewrite endpoint.
* @global WP_Rewrite
*/
private function is_added_rewrite_endpoint_in_path( $path ) {
global $wp_rewrite;
if ( null === $this->added_rewrite_endpoints_pattern && $wp_rewrite instanceof WP_Rewrite ) {
$endpoint_patterns = [];
foreach ( $wp_rewrite->endpoints as $endpoint ) {
$endpoint_patterns[] = preg_quote( $endpoint[1], '#' );
}
if ( empty( $endpoint_patterns ) ) {
return false;
}
$this->added_rewrite_endpoints_pattern = '#/' . implode( '|', $endpoint_patterns ) . '(/|$)#';
}

if ( empty( $this->added_rewrite_endpoints_pattern ) ) {
return false;
}

return (bool) preg_match( $this->added_rewrite_endpoints_pattern, $path );
}

/**
* Get paired AMP URL using a rewrite endpoint.
*
Expand All @@ -278,8 +315,11 @@ public function get_rewrite_endpoint_paired_amp_url( $url ) {
$parsed_url['host'] = isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : 'localhost';
}

$parsed_url['path'] = trailingslashit( $parsed_url['path'] );
$parsed_url['path'] .= user_trailingslashit( amp_get_slug(), 'amp' );
$has_existing_rewrite_endpoint = $this->is_added_rewrite_endpoint_in_path( $parsed_url['path'] );
if ( ! $has_existing_rewrite_endpoint ) {
$parsed_url['path'] = trailingslashit( $parsed_url['path'] );
$parsed_url['path'] .= user_trailingslashit( amp_get_slug(), 'amp' );
}

$amp_url = $parsed_url['scheme'] . '://';
if ( isset( $parsed_url['user'] ) ) {
Expand All @@ -297,6 +337,9 @@ public function get_rewrite_endpoint_paired_amp_url( $url ) {
if ( isset( $parsed_url['query'] ) ) {
$amp_url .= '?' . $parsed_url['query'];
}
if ( $has_existing_rewrite_endpoint ) {
$amp_url = $this->get_query_var_paired_amp_url( $amp_url );
}
if ( isset( $parsed_url['fragment'] ) ) {
$amp_url .= '#' . $parsed_url['fragment'];
}
Expand Down

0 comments on commit d9b724b

Please sign in to comment.