Skip to content

Commit

Permalink
Allow AMP to be disabled for a request via noamp=available when user …
Browse files Browse the repository at this point in the history
…can validate

Also add a "View with AMP disabled" link to the admin bar for such users to easily disable AMP for a URL when a site is in Standard mode without requiring them to switch the entire site to Transitional mode or to disable AMP for a given URL.

See #1294
  • Loading branch information
westonruter committed Jun 27, 2020
1 parent 5079df5 commit e843d0b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ function is_amp_available() {

// If redirected to this page because AMP is not available due to validation errors, prevent AMP from being available (if not AMP-first).
if (
! amp_is_canonical()
( ! amp_is_canonical() || AMP_Validation_Manager::has_cap() )
&&
( isset( $_GET[ QueryVars::NOAMP ] ) && QueryVars::NOAMP_AVAILABLE === $_GET[ QueryVars::NOAMP ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
) {
Expand Down
15 changes: 10 additions & 5 deletions includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ public static function add_admin_bar_menu_items( $wp_admin_bar ) {

$current_url = amp_get_current_url();
$non_amp_url = amp_remove_endpoint( $current_url );
if ( amp_is_canonical() ) {
$non_amp_url = add_query_arg( QueryVars::NOAMP, QueryVars::NOAMP_AVAILABLE, $non_amp_url );
}

$amp_url = remove_query_arg(
array_merge( wp_removable_query_args(), [ QueryVars::NOAMP ] ),
Expand Down Expand Up @@ -423,21 +426,23 @@ public static function add_admin_bar_menu_items( $wp_admin_bar ) {
$link_item = [
'parent' => 'amp',
'id' => 'amp-view',
'title' => esc_html( is_amp_endpoint() ? __( 'View non-AMP version', 'amp' ) : __( 'View AMP version', 'amp' ) ),
'href' => esc_url( is_amp_endpoint() ? $non_amp_url : $amp_url ),
];
if ( amp_is_canonical() ) {
$link_item['title'] = esc_html__( 'View with AMP disabled', 'amp' );
} else {
$link_item['title'] = esc_html( is_amp_endpoint() ? __( 'View non-AMP version', 'amp' ) : __( 'View AMP version', 'amp' ) );
}

// Add top-level menu item. Note that this will correctly merge/amend any existing AMP nav menu item added in amp_add_admin_bar_view_link().
$wp_admin_bar->add_node( $parent );

if ( amp_is_canonical() ) {
if ( is_amp_endpoint() ) {
$wp_admin_bar->add_node( $validate_item );
} elseif ( ! is_amp_endpoint() ) {
$wp_admin_bar->add_node( $link_item );
$wp_admin_bar->add_node( $validate_item );
} else {
$wp_admin_bar->add_node( $validate_item );
$wp_admin_bar->add_node( $link_item );
$wp_admin_bar->add_node( $validate_item );
}

if ( AMP_Theme_Support::is_paired_available() && amp_is_dev_mode() ) { // @todo And user can manage_options.
Expand Down
32 changes: 27 additions & 5 deletions tests/php/validation/test-class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// phpcs:disable Generic.Formatting.MultipleStatementAlignment.NotSameWarning

use AmpProject\AmpWP\Option;
use AmpProject\AmpWP\QueryVars;
use AmpProject\AmpWP\Tests\AssertContainsCompatibility;
use AmpProject\AmpWP\Tests\HandleValidation;
use AmpProject\AmpWP\Tests\PrivateAccess;
Expand Down Expand Up @@ -330,7 +331,9 @@ public function test_add_admin_bar_menu_items() {
$node = $admin_bar->get_node( 'amp' );
$this->assertInternalType( 'object', $node );
$this->assertStringContains( 'action=amp_validate', $node->href );
$this->assertNull( $admin_bar->get_node( 'amp-view' ) );
$view_item = $admin_bar->get_node( 'amp-view' );
$this->assertInternalType( 'object', $view_item );
$this->assertEqualSets( [ QueryVars::NOAMP ], array_keys( $this->get_url_query_vars( $view_item->href ) ) );
$this->assertInternalType( 'object', $admin_bar->get_node( 'amp-validity' ) );

// Admin bar item available in paired mode.
Expand All @@ -357,13 +360,32 @@ public function test_add_admin_bar_menu_items() {
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
$admin_bar = new WP_Admin_Bar();
AMP_Validation_Manager::add_admin_bar_menu_items( $admin_bar );
$node = $admin_bar->get_node( 'amp' );
$this->assertInternalType( 'object', $node );
$this->assertStringEndsWith( '?amp', $node->href );
$this->assertInternalType( 'object', $admin_bar->get_node( 'amp-view' ) );
$root_node = $admin_bar->get_node( 'amp' );
$this->assertInternalType( 'object', $root_node );
$this->assertEqualSets( [ QueryVars::AMP ], array_keys( $this->get_url_query_vars( $root_node->href ) ) );

$view_item = $admin_bar->get_node( 'amp-view' );
$this->assertInternalType( 'object', $view_item );
$this->assertEqualSets( [ QueryVars::AMP ], array_keys( $this->get_url_query_vars( $view_item->href ) ) );
$this->assertInternalType( 'object', $admin_bar->get_node( 'amp-validity' ) );
}

/**
* Get URL query vars.
*
* @param string $url URL.
* @return array Query vars.
*/
private function get_url_query_vars( $url ) {
$query_string = wp_parse_url( $url, PHP_URL_QUERY );
if ( empty( $query_string ) ) {
return [];
}
$query_vars = [];
parse_str( $query_string, $query_vars );
return $query_vars;
}

/**
* Test overrides.
*
Expand Down

0 comments on commit e843d0b

Please sign in to comment.