Skip to content

Commit

Permalink
Introduce PairedAmpRouting service
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 27, 2020
1 parent b6a9209 commit 3420914
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 119 deletions.
1 change: 1 addition & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'server_timing' => \AmpProject\AmpWP\Instrumentation\ServerTiming::class,
'site_health_integration' => \AmpProject\AmpWP\Admin\SiteHealth::class,
'validated_url_stylesheet_gc' => \AmpProject\AmpWP\BackgroundTask\ValidatedUrlStylesheetDataGarbageCollection::class,
'paired_amp_routing' => \AmpProject\AmpWP\PairedAmpRouting::class,
] )
);

Expand Down
120 changes: 3 additions & 117 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
*/
function amp_activate( $network_wide = false ) {
AmpWpPluginFactory::create()->activate( $network_wide );
amp_after_setup_theme();
if ( ! did_action( 'amp_init' ) ) {
amp_init();
}
flush_rewrite_rules();
}

/**
Expand All @@ -39,16 +34,6 @@ function amp_activate( $network_wide = false ) {
*/
function amp_deactivate( $network_wide = false ) {
AmpWpPluginFactory::create()->deactivate( $network_wide );
// We need to manually remove the amp endpoint.
global $wp_rewrite;
foreach ( $wp_rewrite->endpoints as $index => $endpoint ) {
if ( amp_get_slug() === $endpoint[1] ) {
unset( $wp_rewrite->endpoints[ $index ] );
break;
}
}

flush_rewrite_rules( false );
}

/**
Expand Down Expand Up @@ -122,7 +107,6 @@ function amp_init() {
add_action( 'rest_api_init', 'AMP_Options_Manager::register_settings' );
add_action( 'wp_loaded', 'amp_bootstrap_admin' );

add_rewrite_endpoint( amp_get_slug(), EP_PERMALINK );
add_action( 'parse_query', 'amp_correct_query_when_is_front_page' );
add_action( 'admin_bar_menu', 'amp_add_admin_bar_view_link', 100 );

Expand Down Expand Up @@ -1901,54 +1885,7 @@ function amp_generate_script_hash( $script ) {
* @return string AMP URL.
*/
function amp_add_paired_endpoint( $url ) {

$post = null;
if ( has_filter( 'amp_pre_get_permalink' ) || has_filter( 'amp_get_permalink' ) ) {
$post_id = url_to_postid( $url );
if ( $post_id ) {
$post = get_post( $post_id );
}
}

if ( $post instanceof WP_Post ) {
/**
* Filters the AMP permalink to short-circuit normal generation.
*
* Returning a non-false value in this filter will cause the `get_permalink()` to get called and the `amp_get_permalink` filter to not apply.
*
* @since 0.4
* @since 1.0 This filter does not apply when 'amp' theme support is present.
* @since 2.1 This filter applies again when in non-legacy Reader mode but only when obtaining an AMP URL for a post.
* @todo Deprecate this filter?
*
* @param false $url Short-circuited URL.
* @param int $post_id Post ID.
*/
$pre_url = apply_filters( 'amp_pre_get_permalink', false, $post->ID );

if ( false !== $pre_url ) {
return $pre_url;
}
}

$amp_url = add_query_arg( amp_get_slug(), '1', $url );

if ( $post instanceof WP_Post ) {
/**
* Filters AMP permalink.
*
* @since 0.2
* @since 1.0 This filter does not apply when 'amp' theme support is present.
* @since 2.1 This filter applies again when in non-legacy Reader mode but only when obtaining an AMP URL for a post.
* @todo Deprecate this filter?
*
* @param false $amp_url AMP URL.
* @param int $post_id Post ID.
*/
$amp_url = apply_filters( 'amp_get_permalink', $amp_url, $post->ID );
}

return $amp_url;
return Services::get( 'paired_amp_routing' )->add_paired_endpoint( $url );
}

/**
Expand All @@ -1958,45 +1895,9 @@ function amp_add_paired_endpoint( $url ) {
*
* @param string $url URL to examine. If empty, will use the current URL.
* @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_paired_endpoint( $url = '' ) {
$slug = amp_get_slug();

// If the URL was not provided, then use the environment which is already parsed.
if ( empty( $url ) ) {
global $wp_query;
return (
isset( $_GET[ $slug ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
||
(
$wp_query instanceof WP_Query
&&
false !== $wp_query->get( $slug, false )
)
);
}

$parsed_url = wp_parse_url( $url );
if ( ! empty( $parsed_url['query'] ) ) {
$query_vars = [];
wp_parse_str( $parsed_url['query'], $query_vars );
if ( isset( $query_vars[ $slug ] ) ) {
return true;
}
}

if ( ! empty( $parsed_url['path'] ) ) {
$pattern = sprintf(
'#/%s(/[^/^])?/?$#',
preg_quote( $slug, '#' )
);
if ( preg_match( $pattern, $parsed_url['path'] ) ) {
return true;
}
}

return false;
return Services::get( 'paired_amp_routing' )->has_paired_endpoint( $url );
}

/**
Expand All @@ -2008,20 +1909,5 @@ function amp_has_paired_endpoint( $url = '' ) {
* @return string URL with AMP stripped.
*/
function amp_remove_paired_endpoint( $url ) {
$slug = amp_get_slug();

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

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

return $url;
return Services::get( 'paired_amp_routing' )->remove_paired_endpoint( $url );
}
5 changes: 3 additions & 2 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ static function ( $supported ) {
* Filters default options.
*
* @internal
* @param array $defaults Default options.
* @param array $defaults Default options.
* @param array $current_options Current options.
*/
(array) apply_filters( 'amp_default_options', $defaults ),
(array) apply_filters( 'amp_default_options', $defaults, $options ),
$options
);

Expand Down
2 changes: 2 additions & 0 deletions src/AmpWpPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ final class AmpWpPlugin extends ServiceBasedPlugin {
'server_timing' => Instrumentation\ServerTiming::class,
'site_health_integration' => Admin\SiteHealth::class,
'validated_url_stylesheet_gc' => BackgroundTask\ValidatedUrlStylesheetDataGarbageCollection::class,
'paired_amp_routing' => PairedAmpRouting::class,
];

/**
Expand Down Expand Up @@ -161,6 +162,7 @@ protected function get_shared_instances() {
DevTools\CallbackReflection::class,
DevTools\FileReflection::class,
ReaderThemeLoader::class,
PairedAmpRouting::class,
];
}

Expand Down
9 changes: 9 additions & 0 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ interface Option {
*/
const DISABLE_CSS_TRANSIENT_CACHING = 'amp_css_transient_monitor_disable_caching';

/**
* Indicate the structure for Paired AMP URLs.
*
* Default value: 'query_var'
*
* @var string
*/
const PERMALINK_STRUCTURE = 'permalink_structure';

/**
* Redirect mobile visitors to the AMP version of a page when the site is in Transitional or Reader mode.
*
Expand Down
Loading

0 comments on commit 3420914

Please sign in to comment.