Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -2529,17 +2534,14 @@ 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 <https://github.com/google/site-kit-wp/pull/505#discussion_r348683617>.
add_filter(
'script_loader_tag',
static function( $tag, $handle ) {
if ( self::has_dependency( wp_scripts(), 'amp-paired-browsing-client', $handle ) ) {
$tag = preg_replace( '/(?<=<script)(?=\s|>)/i', ' ' . AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, $tag );
$attrs = [ AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, 'async' ];
$tag = preg_replace( '/(?<=<script)(?=\s|>)/i', ' ' . implode( ' ', $attrs ), $tag );
}
return $tag;
},
Expand Down Expand Up @@ -2601,6 +2603,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' ),
Expand Down
2 changes: 1 addition & 1 deletion includes/templates/amp-paired-browsing.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<div class="dialog-text">
<span class="general">
<?php esc_html_e( 'The navigated URL is not available for paired browsing. Would you like to go back or exit to continue to that URL?', 'amp' ); ?>
<?php esc_html_e( 'The navigated URL is not available for paired browsing.', 'amp' ); ?>
</span>

<span class="invalid-amp">
Expand Down
2 changes: 1 addition & 1 deletion includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tests/php/validation/test-class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down