diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
index 42dcc426d0f..a8d4d9a2967 100644
--- a/.github/PULL_REQUEST_TEMPLATE.md
+++ b/.github/PULL_REQUEST_TEMPLATE.md
@@ -5,6 +5,6 @@ Fixes #
## Checklist
-- [ ] My pull request is addressing an [open issue](https://github.com/ampproject/amp-wp/contributing/project-management.md#life-of-an-issue) (please create one otherwise).
-- [ ] My code is tested and passes existing [tests](https://github.com/ampproject/amp-wp/contributing/engineering.md#tests).
-- [ ] My code follows the [Engineering Guidelines](https://github.com/ampproject/amp-wp/contributing/engineering.md) (updates are often made to the guidelines, check it out periodically).
+- [ ] My pull request is addressing an open issue (please create one otherwise).
+- [ ] My code is tested and passes existing [tests](https://github.com/ampproject/amp-wp/wiki/Engineering-Guidelines#tests).
+- [ ] My code follows the [Engineering Guidelines](https://github.com/ampproject/amp-wp/wiki/Engineering-Guidelines) (updates are often made to the guidelines, check it out periodically).
diff --git a/assets/src/block-editor/components/amp-preview.js b/assets/src/block-editor/components/amp-preview.js
index dfbace46e04..c09ed032019 100644
--- a/assets/src/block-editor/components/amp-preview.js
+++ b/assets/src/block-editor/components/amp-preview.js
@@ -8,7 +8,7 @@ import PropTypes from 'prop-types';
* WordPress dependencies
*/
import { Component, createRef, renderToString } from '@wordpress/element';
-import { Button, Icon, Tooltip } from '@wordpress/components';
+import { Button, Icon } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
@@ -236,25 +236,23 @@ class AMPPreview extends Component {
return (
isEnabled && ! errorMessages.length && ! isStandardMode && (
-
Step |
Task |
Role |
-| :--- | :--- | :--- |
-| 1. | An issue requiring work is added to the [Definition](https://github.com/ampproject/amp-wp/projects/17) project board (automatically added to Prioritization column). | `Product Manager` `Program Manager`
-| 2. | The issue is assigned a “Priority” and moved to the “Acceptance Criteria” column. | `Product Manager` `Program Manager`
-| 3. | “Acceptance Criteria” are added to the issue description, and the issue is moved to the “Implementation Brief” column. | `Product Manager` `Product Owner` `Lead Engineer`
-| 4. | “Implementation Brief” is added to the issue description, and the issue is moved to the “Implementation Brief Review” column. | `Engineer`
-| 5. | The “Implementation Brief” is reviewed and the issue is moved to the “Estimate” column upon approval. The issue is moved back to the “Implementation Brief” column if changes in the “Implementation Brief” description are requested. | `Lead Engineer`
-| 6. | The issue is estimated using T-Shirt sizing and moved to the [Execution](https://github.com/ampproject/amp-wp/projects/16) project board (automatically added to the Backlog). | `Project Manager` `Engineer`
-
-##### Execution
-|
Step |
Task |
Role |
-| :--- | :--- | :--- |
-| 1. | An issue requiring work is added to the [Execution](https://github.com/ampproject/amp-wp/projects/16) project board (automatically added to the Backlog) after going through the [Definition](https://github.com/ampproject/amp-wp/projects/17) project board pipeline. | `Product Manager` `Program Manager`
-| 2. | The issue is assigned a “[Milestone](https://github.com/ampproject/amp-wp/milestones)” and “Sprint” label. | `Product Manager` `Program Manager`
-| 3. | The issue is moved to the “To Do” column if it is assigned to the current sprint. | `Project Manager`
-| 4. | The issue is assigned (or may be self-assigned) to an engineer. | `Project Manager` `Engineer`
-| 5. | The issue is moved to the “In Progress” column when development starts. A PR is created, following the [Branching Strategy](https://github.com/ampproject/amp-wp/contributing/engineering.md#branches). The PR must contain details for each section predefined in the PR template, with a reference to the associated issue. **IMPORTANT:** do not add [GitHub keywords](https://help.github.com/en/articles/closing-issues-using-keywords) which would automatically close an issue once the PR is merged. | `Engineer`
-| 6. | The “[Changelog Message](https://github.com/ampproject/amp-wp/contributing/engineering.md#changelog)” is added to the relevant section of the issue description once development is completed. | `Engineer`
-| 7. | The “QA Testing Instructions“ are added to the relevant section of the issue description and the issue is moved to the “Code Review” column. | `Engineer`
-| 8. | The code review is done in the referred PR and the issue is moved to the “QA” column once the review is completed and the PR is approved, merged and deployed to the QA environment. The reviewer must ensure that the “Acceptance Criteria” match the implementation and that the “QA Testing Instructions“ has been added to the issue before moving it to QA. | `Engineer`
-| 9. | The issue is moved to the “Demo” column once QA is passed or moved back to the “To Do” column if changes are required, in which case the cycle from the “To Do” column onwards is repeated. | `QA Specialist`
-| 9. | A video or screenshots demoing the implementation are added to the relevant section of the issue description. | `Engineer`
-| 10. | The issue is moved to the “Approval” column once the demo is added. | `Engineer`
-| 11. | The issue goes through a final review and moved to the “Done” once approved or moved back to the “To Do” column if changes are required, in which case the cycle from the “To Do” column onwards is repeated. | `Product Manager`
-| 12. | The issue is closed. | `Product Manager`
-
-#### Type: `Task`
-
-##### Definition
-
-|
Step |
Task |
Role |
-| :--- | :--- | :--- |
-| 1. | An issue requiring work is added to the [Definition](https://github.com/ampproject/amp-wp/projects/17) project board (automatically added to Prioritization column). | `Product Manager` `Program Manager`
-| 2. | The issue is assigned a “Priority” and moved to the “Acceptance Criteria” column. | `Product Manager` `Program Manager`
-| 3. | “Acceptance Criteria” are added to the issue description, and the issue is moved to the “Estimate" column. | `Product Manager` `Product Owner`
-| 4. | The issue is estimated using T-Shirt sizing and moved to the [Execution](https://github.com/ampproject/amp-wp/projects/16) project board (automatically added to the Backlog). | `Task specific`
-
-##### Execution
-|
Step |
Task |
Role |
-| :--- | :--- | :--- |
-| 1. | An issue requiring work is added to the [Execution](https://github.com/ampproject/amp-wp/projects/16) project board (automatically added to the Backlog) after going through the [Definition](https://github.com/ampproject/amp-wp/projects/17) project board pipeline. | `Product Manager` `Program Manager`
-| 2. | The issue is assigned a “Sprint” label. | `Product Manager` `Program Manager`
-| 3. | The issue is moved to the “To Do” column if it is assigned to the current sprint. | `Project Manager`
-| 4. | The issue is assigned (or may be self-assigned) | `Project Manager` `Assignee`
-| 5. | The issue is moved to the “In progress” column when work starts. | `Assignee`
-| 8. | The issue is moved to the “Approval” column the work is done. | `Assignee`
-| 9. | The issue goes through a final review and moved to the “Done” once approved or moved back to the “To Do” column if changes are required, in which case the cycle from the “To Do” column onwards is repeated. | `Product Manager`
-| 10. | The issue is closed. | `Product Manager`
diff --git a/includes/admin/functions.php b/includes/admin/functions.php
index ace4f332343..ba9ad3b7c55 100644
--- a/includes/admin/functions.php
+++ b/includes/admin/functions.php
@@ -6,6 +6,7 @@
*/
use AmpProject\AmpWP\Admin\SiteHealth;
+use AmpProject\AmpWP\Option;
/**
* Obsolete constant for flagging when Customizer is opened for AMP.
@@ -22,7 +23,7 @@
* And this does not need to toggle between the AMP and normal display.
*/
function amp_init_customizer() {
- if ( AMP_Theme_Support::READER_MODE_SLUG !== AMP_Options_Manager::get_option( 'theme_support' ) ) {
+ if ( AMP_Theme_Support::READER_MODE_SLUG !== AMP_Options_Manager::get_option( Option::THEME_SUPPORT ) ) {
return;
}
diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php
index 5a043682087..b7d1212d7d5 100644
--- a/includes/amp-helper-functions.php
+++ b/includes/amp-helper-functions.php
@@ -5,6 +5,7 @@
* @package AMP
*/
+use AmpProject\AmpWP\Option;
use AmpProject\AmpWP\Services;
/**
@@ -118,20 +119,20 @@ function amp_init() {
/*
* Broadcast plugin updates.
- * Note that AMP_Options_Manager::get_option( 'version', '0.0' ) cannot be used because
+ * Note that AMP_Options_Manager::get_option( Option::VERSION, '0.0' ) cannot be used because
* version was new option added, and in that case default would never be used for a site
* upgrading from a version prior to 1.0. So this is why get_option() is currently used.
*/
$options = get_option( AMP_Options_Manager::OPTION_NAME, [] );
- $old_version = isset( $options['version'] ) ? $options['version'] : '0.0';
- if ( AMP__VERSION !== $old_version ) {
+ $old_version = isset( $options[ Option::VERSION ] ) ? $options[ Option::VERSION ] : '0.0';
+ if ( AMP__VERSION !== $old_version && is_admin() && current_user_can( 'manage_options' ) ) {
/**
* Triggers when after amp_init when the plugin version has updated.
*
* @param string $old_version Old version.
*/
do_action( 'amp_plugin_update', $old_version );
- AMP_Options_Manager::update_option( 'version', AMP__VERSION );
+ AMP_Options_Manager::update_option( Option::VERSION, AMP__VERSION );
}
}
@@ -479,8 +480,10 @@ function amp_remove_endpoint( $url ) {
* If there are known validation errors for the current URL then do not output anything.
*
* @since 1.0
+ * @global WP_Query $wp_query
*/
function amp_add_amphtml_link() {
+ global $wp_query;
/**
* Filters whether to show the amphtml link on the frontend.
@@ -499,7 +502,7 @@ function amp_add_amphtml_link() {
if ( AMP_Theme_Support::is_paired_available() ) {
$amp_url = add_query_arg( amp_get_slug(), '', $current_url );
}
- } elseif ( is_singular() && post_supports_amp( get_post( get_queried_object_id() ) ) ) {
+ } elseif ( $wp_query instanceof WP_Query && ( $wp_query->is_singular() || $wp_query->is_posts_page ) && post_supports_amp( get_post( get_queried_object_id() ) ) ) {
$amp_url = amp_get_permalink( get_queried_object_id() );
}
@@ -566,41 +569,62 @@ function post_supports_amp( $post ) {
function is_amp_endpoint() {
global $pagenow, $wp_query;
- if ( is_admin() || is_embed() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || in_array( $pagenow, [ 'wp-login.php', 'wp-signup.php', 'wp-activate.php' ], true ) ) {
+ // Short-circuit for admin requests or requests to non-frontend pages.
+ if ( is_admin() || in_array( $pagenow, [ 'wp-login.php', 'wp-signup.php', 'wp-activate.php' ], true ) ) {
return false;
}
- // Always return false when requesting service worker.
- if ( class_exists( 'WP_Service_Workers' ) && ! empty( $wp_query ) && defined( 'WP_Service_Workers::QUERY_VAR' ) && $wp_query->get( WP_Service_Workers::QUERY_VAR ) ) {
+ $warned = false;
+ $error_message = sprintf(
+ /* translators: %1$s: is_amp_endpoint(), %2$s: the current action, %3$s: the wp action, %4$s: the WP_Query class, %5$s: the amp_skip_post() function */
+ __( '%1$s was called too early and so it will not work properly. WordPress is currently doing the "%2$s" action. Calling this function before the "%3$s" action means it will not have access to %4$s and the queried object to determine if it is an AMP response, thus neither the "%5$s" filter nor the AMP enabled toggle will be considered.', 'amp' ),
+ __FUNCTION__ . '()',
+ current_action(),
+ 'wp',
+ 'WP_Query',
+ 'amp_skip_post()'
+ );
+
+ // Make sure the parse_request action has triggered before trying to read from the REST_REQUEST constant, which is set during rest_api_loaded().
+ if ( ! did_action( 'parse_request' ) ) {
+ _doing_it_wrong( __FUNCTION__, esc_html( $error_message ), '1.6.0' );
+ $warned = true;
+ } elseif ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return false;
}
- $did_parse_query = did_action( 'parse_query' );
+ // Make sure that the parse_query action has triggered, as this is required to initially populate the global WP_Query.
+ if ( ! $warned && ! ( $wp_query instanceof WP_Query || did_action( 'parse_query' ) ) ) {
+ _doing_it_wrong( __FUNCTION__, esc_html( $error_message ), '0.4.2' );
+ $warned = true;
+ }
- if ( ! $did_parse_query ) {
- _doing_it_wrong(
- __FUNCTION__,
- sprintf(
- /* translators: 1: is_amp_endpoint(), 2: parse_query */
- esc_html__( '%1$s was called before the %2$s hook was called.', 'amp' ),
- 'is_amp_endpoint()',
- 'parse_query'
- ),
- '0.4.2'
- );
+ // Always return false when requesting the service worker.
+ // Note this is no longer required because AMP_Theme_Support::prepare_response() will abort for non-HTML responses.
+ if ( class_exists( 'WP_Service_Workers' ) && $wp_query instanceof WP_Query && defined( 'WP_Service_Workers::QUERY_VAR' ) && $wp_query->get( WP_Service_Workers::QUERY_VAR ) ) {
+ return false;
}
- if ( empty( $wp_query ) || ! ( $wp_query instanceof WP_Query ) ) {
- _doing_it_wrong(
- __FUNCTION__,
- sprintf(
- /* translators: 1: is_amp_endpoint(), 2: WP_Query */
- esc_html__( '%1$s was called before the %2$s was instantiated.', 'amp' ),
- 'is_amp_endpoint()',
- 'WP_Query'
- ),
- '1.1'
- );
+ // Short-circuit queries that can never have AMP responses (e.g. post embeds and feeds).
+ // Note that these conditionals only require the parse_query action to have been run. They don't depend on the wp action having been fired.
+ if (
+ $wp_query instanceof WP_Query
+ &&
+ (
+ $wp_query->is_embed()
+ ||
+ $wp_query->is_feed()
+ ||
+ $wp_query->is_comment_feed()
+ ||
+ $wp_query->is_trackback()
+ ||
+ $wp_query->is_robots()
+ ||
+ ( method_exists( $wp_query, 'is_favicon' ) && $wp_query->is_favicon() )
+ )
+ ) {
+ return false;
}
/*
@@ -623,34 +647,29 @@ function is_amp_endpoint() {
)
);
- if ( ! current_theme_supports( AMP_Theme_Support::SLUG ) ) {
- return $has_amp_query_var;
- }
-
// When there is no query var and AMP is not canonical (AMP-first), then this is definitely not an AMP endpoint.
if ( ! $has_amp_query_var && ! amp_is_canonical() ) {
return false;
}
- if ( ! did_action( 'wp' ) ) {
- _doing_it_wrong(
- __FUNCTION__,
- sprintf(
- /* translators: 1: is_amp_endpoint(). 2: wp. 3: amp_skip_post */
- esc_html__( '%1$s was called before the %2$s action which means it will not have access to the queried object to determine if it is an AMP response, thus neither the %3$s filter nor the AMP enabled publish metabox toggle will be considered.', 'amp' ),
- 'is_amp_endpoint()',
- 'wp',
- 'amp_skip_post'
- ),
- '1.0.2'
- );
- $supported = true;
+ if ( did_action( 'wp' ) && $wp_query instanceof WP_Query ) {
+ if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
+ $availability = AMP_Theme_Support::get_template_availability( $wp_query );
+ return $availability['supported'];
+ } else {
+ $queried_object = get_queried_object();
+ return $queried_object instanceof WP_Post && ( $wp_query->is_singular() || $wp_query->is_posts_page ) && post_supports_amp( $queried_object );
+ }
} else {
- $availability = AMP_Theme_Support::get_template_availability();
- $supported = $availability['supported'];
+ // If WP_Query was not available yet, then we will just assume the query is supported since at this point we do
+ // know either that the site is in Standard mode or the URL was requested with the AMP query var. This can still
+ // produce an undesired result when a Standard mode site has a post that opts out of AMP, but this issue will
+ // have been flagged via _doing_it_wrong() above.
+ if ( ! $warned ) {
+ _doing_it_wrong( __FUNCTION__, esc_html( $error_message ), '1.0.2' );
+ }
+ return amp_is_canonical() || $has_amp_query_var;
}
-
- return amp_is_canonical() ? $supported : ( $has_amp_query_var && $supported );
}
/**
@@ -948,7 +967,7 @@ function amp_filter_font_style_loader_tag_with_crossorigin_anonymous( $tag, $han
* @return array Analytics.
*/
function amp_get_analytics( $analytics = [] ) {
- $analytics_entries = AMP_Options_Manager::get_option( 'analytics', [] );
+ $analytics_entries = AMP_Options_Manager::get_option( Option::ANALYTICS, [] );
/**
* Add amp-analytics tags.
@@ -1220,6 +1239,7 @@ function amp_get_content_sanitizers( $post = null ) {
'AMP_Style_Sanitizer' => [],
'AMP_Meta_Sanitizer' => [],
'AMP_Layout_Sanitizer' => [],
+ 'AMP_Accessibility_Sanitizer' => [],
'AMP_Tag_And_Attribute_Sanitizer' => [], // Note: This whitelist sanitizer must come at the end to clean up any remaining issues the other sanitizers didn't catch.
];
diff --git a/includes/class-amp-post-type-support.php b/includes/class-amp-post-type-support.php
index d307a3e7559..e1129a6a285 100644
--- a/includes/class-amp-post-type-support.php
+++ b/includes/class-amp-post-type-support.php
@@ -6,6 +6,8 @@
* @since 0.6
*/
+use AmpProject\AmpWP\Option;
+
/**
* Class AMP_Post_Type_Support.
*/
@@ -56,10 +58,10 @@ public static function get_eligible_post_types() {
* @since 0.6
*/
public static function add_post_type_support() {
- if ( current_theme_supports( AMP_Theme_Support::SLUG ) && AMP_Options_Manager::get_option( 'all_templates_supported' ) ) {
+ if ( current_theme_supports( AMP_Theme_Support::SLUG ) && AMP_Options_Manager::get_option( Option::ALL_TEMPLATES_SUPPORTED ) ) {
$post_types = self::get_eligible_post_types();
} else {
- $post_types = AMP_Options_Manager::get_option( 'supported_post_types', [] );
+ $post_types = AMP_Options_Manager::get_option( Option::SUPPORTED_POST_TYPES, [] );
}
foreach ( $post_types as $post_type ) {
add_post_type_support( $post_type, self::SLUG );
diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php
index 47170285c1b..40286768734 100644
--- a/includes/class-amp-theme-support.php
+++ b/includes/class-amp-theme-support.php
@@ -6,6 +6,7 @@
*/
use AmpProject\Amp;
+use AmpProject\AmpWP\Option;
use AmpProject\AmpWP\RemoteRequest\CachedRemoteGetRequest;
use AmpProject\AmpWP\ConfigurationArgument;
use AmpProject\AmpWP\Transformer;
@@ -253,7 +254,7 @@ public static function read_theme_support() {
self::$support_added_via_theme = null;
self::$support_added_via_option = null;
- $theme_support_option = AMP_Options_Manager::get_option( 'theme_support' );
+ $theme_support_option = AMP_Options_Manager::get_option( Option::THEME_SUPPORT );
if ( current_theme_supports( self::SLUG ) ) {
$args = self::get_theme_support_args();
@@ -282,7 +283,7 @@ public static function read_theme_support() {
/* translators: 1: available_callback. 2: supported_templates */
esc_html__( 'The %1$s is deprecated when adding amp theme support in favor of declaratively setting the %2$s.', 'amp' ),
'available_callback',
- 'supported_templates'
+ Option::SUPPORTED_TEMPLATES // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
),
'1.0'
);
@@ -376,25 +377,14 @@ public static function finish_init() {
add_filter( 'template_include', [ __CLASS__, 'serve_paired_browsing_experience' ] );
}
+ $is_reader_mode = self::READER_MODE_SLUG === self::get_support_mode();
$has_query_var = (
isset( $_GET[ amp_get_slug() ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
||
false !== get_query_var( amp_get_slug(), false )
);
- $is_reader_mode = self::READER_MODE_SLUG === self::get_support_mode();
- if (
- $is_reader_mode
- &&
- $has_query_var
- &&
- ( ! is_singular() || ! post_supports_amp( get_post( get_queried_object_id() ) ) )
- ) {
- // Reader mode only supports the singular template (for now) so redirect non-singular queries in reader mode to non-AMP version.
- // Also ensure redirecting to non-AMP version when accessing a post which does not support AMP.
- // A temporary redirect is used for admin users to allow them to see changes between reader mode and transitional modes.
- wp_safe_redirect( amp_remove_endpoint( amp_get_current_url() ), current_user_can( 'manage_options' ) ? 302 : 301 );
- return;
- } elseif ( ! is_amp_endpoint() ) {
+
+ if ( ! is_amp_endpoint() ) {
/*
* 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
@@ -402,7 +392,7 @@ public static function finish_init() {
* without wrestling with the redirect cache.
*/
if ( $has_query_var ) {
- self::redirect_non_amp_url( current_user_can( 'manage_options' ) ? 302 : 301, true );
+ self::redirect_non_amp_url( current_user_can( 'manage_options' ) ? 302 : 301 );
}
amp_add_frontend_actions();
@@ -443,11 +433,11 @@ static function() {
* Ensure that the current AMP location is correct.
*
* @since 1.0
+ * @since 1.6 Removed $exit param.
*
- * @param bool $exit Whether to exit after redirecting.
- * @return bool Whether redirection was done. Naturally this is irrelevant if $exit is true.
+ * @return bool Whether redirection should have been done.
*/
- public static function ensure_proper_amp_location( $exit = true ) {
+ 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
@@ -460,7 +450,7 @@ public static function ensure_proper_amp_location( $exit = true ) {
* to not be hampered by browser remembering permanent redirects and preventing test.
*/
if ( $has_query_var || $has_url_param ) {
- return self::redirect_non_amp_url( current_user_can( 'manage_options' ) ? 302 : 301, $exit );
+ return self::redirect_non_amp_url( current_user_can( 'manage_options' ) ? 302 : 301 );
}
} elseif ( self::READER_MODE_SLUG === self::get_support_mode() && is_singular() ) {
// Prevent infinite URL space under /amp/ endpoint.
@@ -468,9 +458,10 @@ public static function ensure_proper_amp_location( $exit = true ) {
$path_args = [];
wp_parse_str( $wp->matched_query, $path_args );
if ( isset( $path_args[ amp_get_slug() ] ) && '' !== $path_args[ amp_get_slug() ] ) {
- wp_safe_redirect( amp_get_permalink( get_queried_object_id() ), 301 );
- if ( $exit ) {
+ if ( wp_safe_redirect( amp_get_permalink( get_queried_object_id() ), 301 ) ) {
+ // @codeCoverageIgnoreStart
exit;
+ // @codeCoverageIgnoreEnd
}
return true;
}
@@ -485,13 +476,12 @@ public static function ensure_proper_amp_location( $exit = true ) {
$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.
- wp_safe_redirect( $new_url, current_user_can( 'manage_options' ) ? 302 : 301 );
- // @codeCoverageIgnoreStart
- if ( $exit ) {
+ if ( wp_safe_redirect( $new_url, current_user_can( 'manage_options' ) ? 302 : 301 ) ) {
+ // @codeCoverageIgnoreStart
exit;
+ // @codeCoverageIgnoreEnd
}
return true;
- // @codeCoverageIgnoreEnd
}
}
return false;
@@ -505,25 +495,24 @@ public static function ensure_proper_amp_location( $exit = true ) {
* @since 0.7
* @since 1.0 Added $exit param.
* @since 1.0 Renamed from redirect_canonical_amp().
+ * @since 1.6 Removed $exit param.
*
- * @param int $status Status code (301 or 302).
- * @param bool $exit Whether to exit after redirecting.
- * @return bool Whether redirection was done. Naturally this is irrelevant if $exit is true.
+ * @param int $status Status code (301 or 302).
+ * @return bool Whether redirection should have be done.
*/
- public static function redirect_non_amp_url( $status = 302, $exit = true ) {
+ public static function redirect_non_amp_url( $status = 302 ) {
$current_url = amp_get_current_url();
$non_amp_url = amp_remove_endpoint( $current_url );
if ( $non_amp_url === $current_url ) {
return false;
}
- wp_safe_redirect( $non_amp_url, $status );
- // @codeCoverageIgnoreStart
- if ( $exit ) {
+ if ( wp_safe_redirect( $non_amp_url, $status ) ) {
+ // @codeCoverageIgnoreStart
exit;
+ // @codeCoverageIgnoreEnd
}
return true;
- // @codeCoverageIgnoreEnd
}
/**
@@ -669,7 +658,7 @@ public static function get_template_availability( $query = null ) {
$all_templates_supported_by_theme_support = 'all' === $theme_support_args['templates_supported'];
}
$all_templates_supported = (
- $all_templates_supported_by_theme_support || AMP_Options_Manager::get_option( 'all_templates_supported' )
+ $all_templates_supported_by_theme_support || AMP_Options_Manager::get_option( Option::ALL_TEMPLATES_SUPPORTED )
);
// Make sure global $wp_query is set in case of conditionals that unfortunately look at global scope.
@@ -1008,7 +997,7 @@ public static function get_supportable_templates() {
$theme_supported_templates = $theme_support_args['templates_supported'];
}
- $supported_templates = AMP_Options_Manager::get_option( 'supported_templates' );
+ $supported_templates = AMP_Options_Manager::get_option( Option::SUPPORTED_TEMPLATES );
foreach ( $templates as $id => &$template ) {
// Capture user-elected support from options. This allows us to preserve the original user selection through programmatic overrides.
@@ -1028,7 +1017,7 @@ public static function get_supportable_templates() {
// Set supported state from user preference.
if ( ! $template['immutable'] ) {
- $template['supported'] = AMP_Options_Manager::get_option( 'all_templates_supported' ) || $template['user_supported'];
+ $template['supported'] = AMP_Options_Manager::get_option( Option::ALL_TEMPLATES_SUPPORTED ) || $template['user_supported'];
}
}
diff --git a/includes/embeds/class-amp-dailymotion-embed.php b/includes/embeds/class-amp-dailymotion-embed-handler.php
similarity index 100%
rename from includes/embeds/class-amp-dailymotion-embed.php
rename to includes/embeds/class-amp-dailymotion-embed-handler.php
diff --git a/includes/embeds/class-amp-facebook-embed.php b/includes/embeds/class-amp-facebook-embed-handler.php
similarity index 100%
rename from includes/embeds/class-amp-facebook-embed.php
rename to includes/embeds/class-amp-facebook-embed-handler.php
diff --git a/includes/embeds/class-amp-gallery-embed.php b/includes/embeds/class-amp-gallery-embed-handler.php
similarity index 100%
rename from includes/embeds/class-amp-gallery-embed.php
rename to includes/embeds/class-amp-gallery-embed-handler.php
diff --git a/includes/embeds/class-amp-instagram-embed.php b/includes/embeds/class-amp-instagram-embed-handler.php
similarity index 100%
rename from includes/embeds/class-amp-instagram-embed.php
rename to includes/embeds/class-amp-instagram-embed-handler.php
diff --git a/includes/embeds/class-amp-pinterest-embed.php b/includes/embeds/class-amp-pinterest-embed-handler.php
similarity index 100%
rename from includes/embeds/class-amp-pinterest-embed.php
rename to includes/embeds/class-amp-pinterest-embed-handler.php
diff --git a/includes/embeds/class-amp-soundcloud-embed.php b/includes/embeds/class-amp-soundcloud-embed-handler.php
similarity index 100%
rename from includes/embeds/class-amp-soundcloud-embed.php
rename to includes/embeds/class-amp-soundcloud-embed-handler.php
diff --git a/includes/embeds/class-amp-vimeo-embed.php b/includes/embeds/class-amp-vimeo-embed-handler.php
similarity index 100%
rename from includes/embeds/class-amp-vimeo-embed.php
rename to includes/embeds/class-amp-vimeo-embed-handler.php
diff --git a/includes/embeds/class-amp-vine-embed.php b/includes/embeds/class-amp-vine-embed-handler.php
similarity index 100%
rename from includes/embeds/class-amp-vine-embed.php
rename to includes/embeds/class-amp-vine-embed-handler.php
diff --git a/includes/options/class-amp-options-manager.php b/includes/options/class-amp-options-manager.php
index a654f0b1e8c..28096a8e6b5 100644
--- a/includes/options/class-amp-options-manager.php
+++ b/includes/options/class-amp-options-manager.php
@@ -5,6 +5,8 @@
* @package AMP
*/
+use AmpProject\AmpWP\Option;
+
/**
* Class AMP_Options_Manager
*/
@@ -23,12 +25,12 @@ class AMP_Options_Manager {
* @var array
*/
protected static $defaults = [
- 'theme_support' => AMP_Theme_Support::READER_MODE_SLUG,
- 'supported_post_types' => [ 'post' ],
- 'analytics' => [],
- 'all_templates_supported' => true,
- 'supported_templates' => [ 'is_singular' ],
- 'version' => AMP__VERSION,
+ Option::THEME_SUPPORT => AMP_Theme_Support::READER_MODE_SLUG,
+ Option::SUPPORTED_POST_TYPES => [ 'post' ],
+ Option::ANALYTICS => [],
+ Option::ALL_TEMPLATES_SUPPORTED => true,
+ Option::SUPPORTED_TEMPLATES => [ 'is_singular' ],
+ Option::VERSION => AMP__VERSION,
];
/**
@@ -59,8 +61,8 @@ public static function register_settings() {
* @param array $new_options New options.
*/
public static function maybe_flush_rewrite_rules( $old_options, $new_options ) {
- $old_post_types = isset( $old_options['supported_post_types'] ) ? $old_options['supported_post_types'] : [];
- $new_post_types = isset( $new_options['supported_post_types'] ) ? $new_options['supported_post_types'] : [];
+ $old_post_types = isset( $old_options[ Option::SUPPORTED_POST_TYPES ] ) ? $old_options[ Option::SUPPORTED_POST_TYPES ] : [];
+ $new_post_types = isset( $new_options[ Option::SUPPORTED_POST_TYPES ] ) ? $new_options[ Option::SUPPORTED_POST_TYPES ] : [];
sort( $old_post_types );
sort( $new_post_types );
if ( $old_post_types !== $new_post_types ) {
@@ -84,17 +86,17 @@ public static function get_options() {
$defaults = self::$defaults;
if ( current_theme_supports( 'amp' ) ) {
- $defaults['theme_support'] = amp_is_canonical() ? AMP_Theme_Support::STANDARD_MODE_SLUG : AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
+ $defaults[ Option::THEME_SUPPORT ] = amp_is_canonical() ? AMP_Theme_Support::STANDARD_MODE_SLUG : AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
}
$options = array_merge( $defaults, $options );
// Migrate theme support slugs.
- if ( 'native' === $options['theme_support'] ) {
- $options['theme_support'] = AMP_Theme_Support::STANDARD_MODE_SLUG;
- } elseif ( 'paired' === $options['theme_support'] ) {
- $options['theme_support'] = AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
- } elseif ( 'disabled' === $options['theme_support'] ) {
+ if ( 'native' === $options[ Option::THEME_SUPPORT ] ) {
+ $options[ Option::THEME_SUPPORT ] = AMP_Theme_Support::STANDARD_MODE_SLUG;
+ } elseif ( 'paired' === $options[ Option::THEME_SUPPORT ] ) {
+ $options[ Option::THEME_SUPPORT ] = AMP_Theme_Support::TRANSITIONAL_MODE_SLUG;
+ } elseif ( 'disabled' === $options[ Option::THEME_SUPPORT ] ) {
/*
* Prior to 1.2, the theme support slug for Reader mode was 'disabled'. This would be saved in options for
* themes that had 'amp' theme support defined. Also prior to 1.2, the user could not switch between modes
@@ -106,7 +108,7 @@ public static function get_options() {
* become 'transitional'. Otherwise, if the theme lacks 'amp' theme support, then this will become the
* default 'reader' mode.
*/
- $options['theme_support'] = $defaults['theme_support'];
+ $options[ Option::THEME_SUPPORT ] = $defaults[ Option::THEME_SUPPORT ];
}
unset(
@@ -115,28 +117,30 @@ public static function get_options() {
*
* @since 1.4.0
*/
- $options['auto_accept_sanitization'],
+ $options[ Option::AUTO_ACCEPT_SANITIZATION ],
/**
* Remove Story related options.
*
+ * Option::ENABLE_AMP_STORIES was added in 1.2-beta and later migrated into the `experiences` option.
+ *
* @since 1.5.0
*/
- $options['story_templates_version'],
- $options['story_export_base_url'],
- $options['story_settings'],
- $options['enable_amp_stories'], // This was added in 1.2-beta and later migrated into the `experiences` option.
+ $options[ Option::STORY_TEMPLATES_VERSION ],
+ $options[ Option::STORY_EXPORT_BASE_URL ],
+ $options[ Option::STORY_SETTINGS ],
+ $options[ Option::ENABLE_AMP_STORIES ],
/**
* Remove 'experiences' option.
*
* @since 1.5.0
*/
- $options['experiences'],
+ $options[ Option::EXPERIENCES ],
/**
* Remove 'enable_response_caching' option.
*
* @since 1.5.0
*/
- $options['enable_response_caching']
+ $options[ Option::ENABLE_RESPONSE_CACHING ]
);
return $options;
@@ -179,24 +183,24 @@ public static function validate_options( $new_options ) {
AMP_Theme_Support::TRANSITIONAL_MODE_SLUG,
AMP_Theme_Support::STANDARD_MODE_SLUG,
];
- if ( isset( $new_options['theme_support'] ) && in_array( $new_options['theme_support'], $recognized_theme_supports, true ) ) {
- $options['theme_support'] = $new_options['theme_support'];
+ if ( isset( $new_options[ Option::THEME_SUPPORT ] ) && in_array( $new_options[ Option::THEME_SUPPORT ], $recognized_theme_supports, true ) ) {
+ $options[ Option::THEME_SUPPORT ] = $new_options[ Option::THEME_SUPPORT ];
// If this option was changed, display a notice with the new template mode.
- if ( self::get_option( 'theme_support' ) !== $new_options['theme_support'] ) {
+ if ( self::get_option( Option::THEME_SUPPORT ) !== $new_options[ Option::THEME_SUPPORT ] ) {
add_action( 'update_option_' . self::OPTION_NAME, [ __CLASS__, 'handle_updated_theme_support_option' ] );
}
}
// Validate post type support.
- if ( isset( $new_options['supported_post_types'] ) ) {
- $options['supported_post_types'] = [];
+ if ( isset( $new_options[ Option::SUPPORTED_POST_TYPES ] ) ) {
+ $options[ Option::SUPPORTED_POST_TYPES ] = [];
- foreach ( $new_options['supported_post_types'] as $post_type ) {
+ foreach ( $new_options[ Option::SUPPORTED_POST_TYPES ] as $post_type ) {
if ( ! post_type_exists( $post_type ) ) {
add_settings_error( self::OPTION_NAME, 'unknown_post_type', __( 'Unrecognized post type.', 'amp' ) );
} else {
- $options['supported_post_types'][] = $post_type;
+ $options[ Option::SUPPORTED_POST_TYPES ][] = $post_type;
}
}
}
@@ -205,21 +209,21 @@ public static function validate_options( $new_options ) {
$is_template_support_required = ( isset( $theme_support_args['templates_supported'] ) && 'all' === $theme_support_args['templates_supported'] );
if ( ! $is_template_support_required && ! isset( $theme_support_args['available_callback'] ) ) {
- $options['all_templates_supported'] = ! empty( $new_options['all_templates_supported'] );
+ $options[ Option::ALL_TEMPLATES_SUPPORTED ] = ! empty( $new_options[ Option::ALL_TEMPLATES_SUPPORTED ] );
// Validate supported templates.
- $options['supported_templates'] = [];
- if ( isset( $new_options['supported_templates'] ) ) {
- $options['supported_templates'] = array_intersect(
- $new_options['supported_templates'],
+ $options[ Option::SUPPORTED_TEMPLATES ] = [];
+ if ( isset( $new_options[ Option::SUPPORTED_TEMPLATES ] ) ) {
+ $options[ Option::SUPPORTED_TEMPLATES ] = array_intersect(
+ $new_options[ Option::SUPPORTED_TEMPLATES ],
array_keys( AMP_Theme_Support::get_supportable_templates() )
);
}
}
// Validate analytics.
- if ( isset( $new_options['analytics'] ) ) {
- foreach ( $new_options['analytics'] as $id => $data ) {
+ if ( isset( $new_options[ Option::ANALYTICS ] ) ) {
+ foreach ( $new_options[ Option::ANALYTICS ] as $id => $data ) {
// Check save/delete pre-conditions and proceed if correct.
if ( empty( $data['type'] ) || empty( $data['config'] ) ) {
@@ -245,16 +249,16 @@ public static function validate_options( $new_options ) {
$entry_id = substr( md5( $entry_vendor_type . $entry_config ), 0, 12 );
// Avoid duplicates.
- if ( isset( $options['analytics'][ $entry_id ] ) ) {
+ if ( isset( $options[ Option::ANALYTICS ][ $entry_id ] ) ) {
add_settings_error( self::OPTION_NAME, 'duplicate_analytics_entry', __( 'Duplicate analytics entry found.', 'amp' ) );
continue;
}
}
if ( isset( $data['delete'] ) ) {
- unset( $options['analytics'][ $entry_id ] );
+ unset( $options[ Option::ANALYTICS ][ $entry_id ] );
} else {
- $options['analytics'][ $entry_id ] = [
+ $options[ Option::ANALYTICS ][ $entry_id ] = [
'type' => $entry_vendor_type,
'config' => $entry_config,
];
@@ -262,8 +266,14 @@ public static function validate_options( $new_options ) {
}
}
+ if ( array_key_exists( Option::DISABLE_CSS_TRANSIENT_CACHING, $new_options ) && true === $new_options[ Option::DISABLE_CSS_TRANSIENT_CACHING ] ) {
+ $options[ Option::DISABLE_CSS_TRANSIENT_CACHING ] = true;
+ } else {
+ unset( $options[ Option::DISABLE_CSS_TRANSIENT_CACHING ] );
+ }
+
// Store the current version with the options so we know the format.
- $options['version'] = AMP__VERSION;
+ $options[ Option::VERSION ] = AMP__VERSION;
return $options;
}
@@ -276,11 +286,11 @@ public static function validate_options( $new_options ) {
*/
public static function check_supported_post_type_update_errors() {
// If all templates are supported then skip check since all post types are also supported. This option only applies with standard/transitional theme support.
- if ( self::get_option( 'all_templates_supported', false ) && AMP_Theme_Support::READER_MODE_SLUG !== self::get_option( 'theme_support' ) ) {
+ if ( self::get_option( Option::ALL_TEMPLATES_SUPPORTED, false ) && AMP_Theme_Support::READER_MODE_SLUG !== self::get_option( Option::THEME_SUPPORT ) ) {
return;
}
- $supported_types = self::get_option( 'supported_post_types', [] );
+ $supported_types = self::get_option( Option::SUPPORTED_POST_TYPES, [] );
foreach ( AMP_Post_Type_Support::get_eligible_post_types() as $name ) {
$post_type = get_post_type_object( $name );
if ( empty( $post_type ) ) {
@@ -346,8 +356,8 @@ public static function handle_analytics_submit() {
// Ensure request is coming from analytics option form.
check_admin_referer( 'analytics-options', 'analytics-options' );
- if ( isset( $_POST['amp-options']['analytics'] ) ) {
- self::update_option( 'analytics', wp_unslash( $_POST['amp-options']['analytics'] ) );
+ if ( isset( $_POST[ self::OPTION_NAME ][ Option::ANALYTICS ] ) ) {
+ self::update_option( Option::ANALYTICS, wp_unslash( $_POST[ self::OPTION_NAME ][ Option::ANALYTICS ] ) );
$errors = get_settings_errors( self::OPTION_NAME );
if ( empty( $errors ) ) {
@@ -375,7 +385,7 @@ public static function handle_analytics_submit() {
*/
public static function update_analytics_options( $data ) {
_deprecated_function( __METHOD__, '0.6', __CLASS__ . '::update_option' );
- return self::update_option( 'analytics', wp_unslash( $data ) );
+ return self::update_option( Option::ANALYTICS, wp_unslash( $data ) );
}
/**
@@ -529,7 +539,7 @@ public static function insecure_connection_notice() {
* Adds a message for an update of the theme support setting.
*/
public static function handle_updated_theme_support_option() {
- $template_mode = self::get_option( 'theme_support' );
+ $template_mode = self::get_option( Option::THEME_SUPPORT );
// Make sure post type support has been added for sake of amp_admin_get_preview_permalink().
foreach ( AMP_Post_Type_Support::get_eligible_post_types() as $post_type ) {
diff --git a/includes/options/class-amp-options-menu.php b/includes/options/class-amp-options-menu.php
index da9638354ae..b313a63b0cc 100644
--- a/includes/options/class-amp-options-menu.php
+++ b/includes/options/class-amp-options-menu.php
@@ -5,6 +5,8 @@
* @package AMP
*/
+use AmpProject\AmpWP\Option;
+
/**
* AMP_Options_Menu class.
*/
@@ -80,7 +82,7 @@ public function add_menu_items() {
);
add_settings_field(
- 'theme_support',
+ Option::THEME_SUPPORT,
__( 'Template Mode', 'amp' ),
[ $this, 'render_theme_support' ],
AMP_Options_Manager::OPTION_NAME,
@@ -91,7 +93,7 @@ public function add_menu_items() {
);
add_settings_field(
- 'supported_templates',
+ Option::SUPPORTED_TEMPLATES,
__( 'Supported Templates', 'amp' ),
[ $this, 'render_supported_templates' ],
AMP_Options_Manager::OPTION_NAME,
@@ -246,7 +248,7 @@ public function render_supported_templates() {
@@ -272,7 +274,7 @@ public function render_supported_templates() {