From 0c5c6d9c50aff96c5bd5c2e74bc27e92fb68eced Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 1 Dec 2019 07:09:31 +0700 Subject: [PATCH 1/3] Restrict paired browsing to when dev mode is enabled --- includes/class-amp-theme-support.php | 17 +++++++++++++---- .../validation/class-amp-validation-manager.php | 2 +- .../test-class-amp-validation-manager.php | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index dd566a881a7..6b4235c9ff6 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -2516,6 +2516,11 @@ public static function setup_paired_browsing_client() { return; } + // Paired browsing requires a custom script which in turn requires dev mode. + if ( ! amp_is_dev_mode() ) { + return; + } + $asset_file = AMP__DIR__ . '/assets/js/amp-paired-browsing-client.asset.php'; $asset = require $asset_file; $dependencies = $asset['dependencies']; @@ -2529,10 +2534,6 @@ public static function setup_paired_browsing_client() { true ); - // Force dev mode to be enabled. This ensures that the enqueued script and its dependencies - // will be present when the admin bar is not showing. - add_filter( 'amp_dev_mode_enabled', '__return_true' ); - // Whitelist enqueued script for AMP dev mdoe so that it is not removed. // @todo Revisit with . add_filter( @@ -2601,6 +2602,14 @@ public static function serve_paired_browsing_experience( $template ) { return $template; } + if ( ! amp_is_dev_mode() ) { + wp_die( + esc_html__( 'Paired browsing is only available when AMP dev mode is enabled (e.g. when logged-in and admin bar is showing).', 'amp' ), + esc_html__( 'AMP Paired Browsing Unavailable', 'amp' ), + [ 'response' => 403 ] + ); + } + wp_enqueue_style( 'amp-paired-browsing-app', amp_get_asset_url( '/css/amp-paired-browsing-app-compiled.css' ), diff --git a/includes/validation/class-amp-validation-manager.php b/includes/validation/class-amp-validation-manager.php index 76d01b675d1..101c7152e7e 100644 --- a/includes/validation/class-amp-validation-manager.php +++ b/includes/validation/class-amp-validation-manager.php @@ -493,7 +493,7 @@ public static function add_admin_bar_menu_items( $wp_admin_bar ) { $wp_admin_bar->add_node( $link_item ); } - if ( AMP_Theme_Support::is_paired_available() && $error_count <= 0 ) { + if ( AMP_Theme_Support::is_paired_available() && $error_count <= 0 && amp_is_dev_mode() ) { // Construct admin bar item to link to paired browsing experience. $paired_browsing_item = [ 'parent' => 'amp', diff --git a/tests/php/validation/test-class-amp-validation-manager.php b/tests/php/validation/test-class-amp-validation-manager.php index 19cc914023a..5be98de3f1a 100644 --- a/tests/php/validation/test-class-amp-validation-manager.php +++ b/tests/php/validation/test-class-amp-validation-manager.php @@ -315,6 +315,7 @@ public function test_add_admin_bar_menu_items() { $this->assertInternalType( 'object', $admin_bar->get_node( 'amp-validity' ) ); // Admin bar item available in paired mode. + add_filter( 'amp_dev_mode_enabled', '__return_true' ); 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 ); From 262ba71d9e2efbeddb7f070b3a2502eef3cf7281 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 1 Dec 2019 07:48:24 +0700 Subject: [PATCH 2/3] Add async attribute to amp-paired-browsing-client script --- includes/class-amp-theme-support.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 6b4235c9ff6..a31c59ec423 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -2540,7 +2540,8 @@ public static function setup_paired_browsing_client() { 'script_loader_tag', static function( $tag, $handle ) { if ( self::has_dependency( wp_scripts(), 'amp-paired-browsing-client', $handle ) ) { - $tag = preg_replace( '/(?<=)/i', ' ' . AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, $tag ); + $attrs = [ AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, 'async' ]; + $tag = preg_replace( '/(?<=)/i', ' ' . implode( ' ', $attrs ), $tag ); } return $tag; }, From c10276e4b121342661682f66e3e201afc02da648 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 1 Dec 2019 10:24:56 +0700 Subject: [PATCH 3/3] Remove second sentence from paired browsing dialog --- includes/templates/amp-paired-browsing.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/templates/amp-paired-browsing.php b/includes/templates/amp-paired-browsing.php index e0d1d85c3c9..0e1ed96064e 100644 --- a/includes/templates/amp-paired-browsing.php +++ b/includes/templates/amp-paired-browsing.php @@ -38,7 +38,7 @@
- +