diff --git a/amp.php b/amp.php index e9c3c730c22..55aa2f356ea 100644 --- a/amp.php +++ b/amp.php @@ -55,7 +55,7 @@ function amp_deactivate() { // We need to manually remove the amp endpoint global $wp_rewrite; foreach ( $wp_rewrite->endpoints as $index => $endpoint ) { - if ( AMP_QUERY_VAR === $endpoint[1] ) { + if ( amp_get_slug() === $endpoint[1] ) { unset( $wp_rewrite->endpoints[ $index ] ); break; } @@ -73,20 +73,12 @@ function amp_deactivate() { * @since 0.6 */ function amp_after_setup_theme() { + amp_get_slug(); // Ensure AMP_QUERY_VAR is set. + if ( false === apply_filters( 'amp_is_enabled', true ) ) { return; } - if ( ! defined( 'AMP_QUERY_VAR' ) ) { - /** - * Filter the AMP query variable. - * - * @since 0.3.2 - * @param string $query_var The AMP query variable. - */ - define( 'AMP_QUERY_VAR', apply_filters( 'amp_query_var', 'amp' ) ); - } - add_action( 'init', 'amp_init', 0 ); // Must be 0 because widgets_init happens at init priority 1. } add_action( 'after_setup_theme', 'amp_after_setup_theme', 5 ); @@ -107,7 +99,7 @@ function amp_init() { load_plugin_textdomain( 'amp', false, plugin_basename( AMP__DIR__ ) . '/languages' ); - add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK ); + add_rewrite_endpoint( amp_get_slug(), EP_PERMALINK ); AMP_Validation_Utils::init(); AMP_Theme_Support::init(); @@ -132,8 +124,8 @@ function amp_init() { // Make sure the `amp` query var has an explicit value. // Avoids issues when filtering the deprecated `query_string` hook. function amp_force_query_var_value( $query_vars ) { - if ( isset( $query_vars[ AMP_QUERY_VAR ] ) && '' === $query_vars[ AMP_QUERY_VAR ] ) { - $query_vars[ AMP_QUERY_VAR ] = 1; + if ( isset( $query_vars[ amp_get_slug() ] ) && '' === $query_vars[ amp_get_slug() ] ) { + $query_vars[ amp_get_slug() ] = 1; } return $query_vars; } @@ -202,7 +194,7 @@ function amp_correct_query_when_is_front_page( WP_Query $query ) { $query->is_home() && // Is AMP endpoint. - false !== $query->get( AMP_QUERY_VAR, false ) + false !== $query->get( amp_get_slug(), false ) && // Is query not yet fixed uo up to be front page. ! $query->is_front_page() @@ -214,7 +206,7 @@ function amp_correct_query_when_is_front_page( WP_Query $query ) { get_option( 'page_on_front' ) && // See line in WP_Query::parse_query() at . - 0 === count( array_diff( array_keys( wp_parse_args( $query->query ) ), array( AMP_QUERY_VAR, 'preview', 'page', 'paged', 'cpage' ) ) ) + 0 === count( array_diff( array_keys( wp_parse_args( $query->query ) ), array( amp_get_slug(), 'preview', 'page', 'paged', 'cpage' ) ) ) ); if ( $is_front_page_query ) { $query->is_home = false; @@ -304,9 +296,9 @@ function amp_render_post( $post ) { * which is not ideal for any code that expects to run in an AMP context. * Let's force the value to be true while we render AMP. */ - $was_set = isset( $wp_query->query_vars[ AMP_QUERY_VAR ] ); + $was_set = isset( $wp_query->query_vars[ amp_get_slug() ] ); if ( ! $was_set ) { - $wp_query->query_vars[ AMP_QUERY_VAR ] = true; + $wp_query->query_vars[ amp_get_slug() ] = true; } // Prevent New Relic from causing invalid AMP responses due the NREUM script it injects after the meta charset. @@ -328,7 +320,7 @@ function amp_render_post( $post ) { $template->load(); if ( ! $was_set ) { - unset( $wp_query->query_vars[ AMP_QUERY_VAR ] ); + unset( $wp_query->query_vars[ amp_get_slug() ] ); } } @@ -359,7 +351,7 @@ function _amp_bootstrap_customizer() { function amp_redirect_old_slug_to_new_url( $link ) { if ( is_amp_endpoint() ) { - $link = trailingslashit( trailingslashit( $link ) . AMP_QUERY_VAR ); + $link = trailingslashit( trailingslashit( $link ) . amp_get_slug() ); } return $link; diff --git a/includes/admin/class-amp-customizer.php b/includes/admin/class-amp-customizer.php index a3efe74c55e..cfb7bb30fc6 100644 --- a/includes/admin/class-amp-customizer.php +++ b/includes/admin/class-amp-customizer.php @@ -134,7 +134,7 @@ public function add_customizer_scripts() { wp_add_inline_script( 'amp-customize-controls', sprintf( 'ampCustomizeControls.boot( %s );', wp_json_encode( array( - 'queryVar' => AMP_QUERY_VAR, + 'queryVar' => amp_get_slug(), 'panelId' => self::PANEL_ID, 'ampUrl' => amp_admin_get_preview_permalink(), 'l10n' => array( diff --git a/includes/admin/class-amp-post-meta-box.php b/includes/admin/class-amp-post-meta-box.php index 8a1fe0db156..55842b335ba 100644 --- a/includes/admin/class-amp-post-meta-box.php +++ b/includes/admin/class-amp-post-meta-box.php @@ -145,7 +145,7 @@ public function enqueue_admin_assets() { ); wp_add_inline_script( self::ASSETS_HANDLE, sprintf( 'ampPostMetaBox.boot( %s );', wp_json_encode( array( - 'previewLink' => esc_url_raw( add_query_arg( AMP_QUERY_VAR, '', get_preview_post_link( $post ) ) ), + 'previewLink' => esc_url_raw( add_query_arg( amp_get_slug(), '', get_preview_post_link( $post ) ) ), 'canonical' => amp_is_canonical(), 'enabled' => post_supports_amp( $post ), 'canSupport' => count( AMP_Post_Type_Support::get_support_errors( $post ) ) === 0, @@ -237,7 +237,7 @@ public function preview_post_link( $link ) { ); if ( $is_amp ) { - $link = add_query_arg( AMP_QUERY_VAR, true, $link ); + $link = add_query_arg( amp_get_slug(), true, $link ); } return $link; diff --git a/includes/admin/functions.php b/includes/admin/functions.php index f5539a05a04..80b94511e3d 100644 --- a/includes/admin/functions.php +++ b/includes/admin/functions.php @@ -48,7 +48,7 @@ function amp_admin_get_preview_permalink() { */ $post_type = (string) apply_filters( 'amp_customizer_post_type', 'post' ); - if ( ! post_type_supports( $post_type, AMP_QUERY_VAR ) ) { + if ( ! post_type_supports( $post_type, amp_get_slug() ) ) { return null; } diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index be8fd0e3619..a49e5867fa6 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -5,6 +5,36 @@ * @package AMP */ +/** + * Get the slug used in AMP for the query var, endpoint, and post type support. + * + * The return value can be overridden by previously defining a AMP_QUERY_VAR + * constant or by adding a 'amp_query_var' filter, but *warning* this ability + * may be deprecated in the future. Normally the slug should be just 'amp'. + * + * @since 0.7 + * @return string Slug used for query var, endpoint, and post type support. + */ +function amp_get_slug() { + if ( defined( 'AMP_QUERY_VAR' ) ) { + return AMP_QUERY_VAR; + } + + /** + * Filter the AMP query variable. + * + * Warning: This filter may become deprecated. + * + * @since 0.3.2 + * @param string $query_var The AMP query variable. + */ + $query_var = apply_filters( 'amp_query_var', 'amp' ); + + define( 'AMP_QUERY_VAR', $query_var ); + + return $query_var; +} + /** * Retrieves the full AMP-specific permalink for the given post ID. * @@ -38,9 +68,9 @@ function amp_get_permalink( $post_id ) { $parsed_url = wp_parse_url( get_permalink( $post_id ) ); $structure = get_option( 'permalink_structure' ); if ( empty( $structure ) || ! empty( $parsed_url['query'] ) || is_post_type_hierarchical( get_post_type( $post_id ) ) ) { - $amp_url = add_query_arg( AMP_QUERY_VAR, '', get_permalink( $post_id ) ); + $amp_url = add_query_arg( amp_get_slug(), '', get_permalink( $post_id ) ); } else { - $amp_url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( AMP_QUERY_VAR, 'single_amp' ); + $amp_url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( amp_get_slug(), 'single_amp' ); } } @@ -66,10 +96,10 @@ function amp_get_permalink( $post_id ) { function amp_remove_endpoint( $url ) { // Strip endpoint. - $url = preg_replace( ':/' . preg_quote( AMP_QUERY_VAR, ':' ) . '(?=/?(\?|#|$)):', '', $url ); + $url = preg_replace( ':/' . preg_quote( amp_get_slug(), ':' ) . '(?=/?(\?|#|$)):', '', $url ); // Strip query var. - $url = remove_query_arg( AMP_QUERY_VAR, $url ); + $url = remove_query_arg( amp_get_slug(), $url ); return $url; } @@ -149,7 +179,7 @@ function is_amp_endpoint() { _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( "is_amp_endpoint() was called before the 'parse_query' hook was called. This function will always return 'false' before the 'parse_query' hook is called.", 'amp' ) ), '0.4.2' ); } - return false !== get_query_var( AMP_QUERY_VAR, false ); + return false !== get_query_var( amp_get_slug(), false ); } /** diff --git a/includes/class-amp-post-type-support.php b/includes/class-amp-post-type-support.php index dfe1e0496d4..22a1bef580b 100644 --- a/includes/class-amp-post-type-support.php +++ b/includes/class-amp-post-type-support.php @@ -54,7 +54,7 @@ public static function add_post_type_support() { AMP_Options_Manager::get_option( 'supported_post_types', array() ) ); foreach ( $post_types as $post_type ) { - add_post_type_support( $post_type, AMP_QUERY_VAR ); + add_post_type_support( $post_type, amp_get_slug() ); } } @@ -73,7 +73,7 @@ public static function get_support_errors( $post ) { $errors = array(); // Because `add_rewrite_endpoint` doesn't let us target specific post_types. - if ( isset( $post->post_type ) && ! post_type_supports( $post->post_type, AMP_QUERY_VAR ) ) { + if ( isset( $post->post_type ) && ! post_type_supports( $post->post_type, amp_get_slug() ) ) { $errors[] = 'post-type-support'; } diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index ee7da221ad0..fd3b78ce39b 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -130,7 +130,7 @@ public static function finish_init() { * @since 0.7 */ public static function redirect_canonical_amp() { - if ( false !== get_query_var( AMP_QUERY_VAR, false ) ) { // Because is_amp_endpoint() now returns true if amp_is_canonical(). + if ( false !== get_query_var( amp_get_slug(), false ) ) { // Because is_amp_endpoint() now returns true if amp_is_canonical(). $url = preg_replace( '#^(https?://.+?)(/.*)$#', '$1', home_url( '/' ) ); if ( isset( $_SERVER['REQUEST_URI'] ) ) { $url .= wp_unslash( $_SERVER['REQUEST_URI'] ); diff --git a/includes/options/class-amp-options-manager.php b/includes/options/class-amp-options-manager.php index f4f9f083665..9437700ed0a 100644 --- a/includes/options/class-amp-options-manager.php +++ b/includes/options/class-amp-options-manager.php @@ -153,7 +153,7 @@ public static function check_supported_post_type_update_errors() { continue; } - $post_type_supported = post_type_supports( $post_type->name, AMP_QUERY_VAR ); + $post_type_supported = post_type_supports( $post_type->name, amp_get_slug() ); $is_support_elected = in_array( $post_type->name, $supported_types, true ); $error = null; diff --git a/includes/options/class-amp-options-menu.php b/includes/options/class-amp-options-menu.php index 93e51e69c0b..3bc556a551f 100644 --- a/includes/options/class-amp-options-menu.php +++ b/includes/options/class-amp-options-menu.php @@ -94,7 +94,7 @@ public function render_post_types_support() { id="" name="" value="name ); ?>" - name, AMP_QUERY_VAR ) ); ?> + name, amp_get_slug() ) ); ?> >