Skip to content

Commit 95c009a

Browse files
authored
Merge pull request #6877 from rtCamp/bug/6643-prevent-duplicate-scripts
Prevent duplicated script to hide admin bar in onboarding wizard
2 parents 47298c7 + 7f38ba7 commit 95c009a

File tree

3 files changed

+77
-29
lines changed

3 files changed

+77
-29
lines changed

includes/amp-helper-functions.php

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -194,37 +194,43 @@ static function() {
194194
* Detects whether the current window is in an iframe with the specified `name` attribute. The iframe is created
195195
* by Preview component located in <assets/src/setup/pages/save/index.js>.
196196
*/
197-
add_action(
198-
'wp_print_scripts',
199-
function() {
200-
if ( ! amp_is_dev_mode() || ! is_admin_bar_showing() ) {
197+
add_action( 'wp_head', 'amp_remove_admin_bar_in_phone_preview' );
198+
add_action( 'amp_post_template_head', 'amp_remove_admin_bar_in_phone_preview' );
199+
}
200+
201+
/**
202+
* Remove the admin bar in phone preview of the site in AMP Onboarding Wizard.
203+
*
204+
* @since 2.2.2
205+
* @internal
206+
*/
207+
function amp_remove_admin_bar_in_phone_preview() {
208+
if ( ! amp_is_dev_mode() || ! is_admin_bar_showing() ) {
209+
return;
210+
}
211+
?>
212+
<script data-ampdevmode>
213+
( () => {
214+
if ( 'amp-wizard-completion-preview' !== window.name ) {
201215
return;
202216
}
203-
?>
204-
<script data-ampdevmode>
205-
( () => {
206-
if ( 'amp-wizard-completion-preview' !== window.name ) {
207-
return;
208-
}
209-
210-
/** @type {HTMLStyleElement} */
211-
const style = document.createElement( 'style' );
212-
style.setAttribute( 'type', 'text/css' );
213-
style.appendChild( document.createTextNode( 'html:not(#_) { margin-top: 0 !important; } #wpadminbar { display: none !important; }' ) );
214-
document.head.appendChild( style );
215-
216-
document.addEventListener( 'DOMContentLoaded', function() {
217-
const adminBar = document.getElementById( 'wpadminbar' );
218-
if ( adminBar ) {
219-
document.body.classList.remove( 'admin-bar' );
220-
adminBar.remove();
221-
}
222-
});
223-
} )();
224-
</script>
225-
<?php
226-
}
227-
);
217+
218+
/** @type {HTMLStyleElement} */
219+
const style = document.createElement( 'style' );
220+
style.setAttribute( 'type', 'text/css' );
221+
style.appendChild( document.createTextNode( 'html:not(#_) { margin-top: 0 !important; } #wpadminbar { display: none !important; }' ) );
222+
document.head.appendChild( style );
223+
224+
document.addEventListener( 'DOMContentLoaded', function() {
225+
const adminBar = document.getElementById( 'wpadminbar' );
226+
if ( adminBar ) {
227+
document.body.classList.remove( 'admin-bar' );
228+
adminBar.remove();
229+
}
230+
});
231+
} )();
232+
</script>
233+
<?php
228234
}
229235

230236
/**

tests/e2e/specs/amp-onboarding/done.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ async function testCommonDoneStepElements() {
2222
await expect( page ).toMatchElement( 'p', { text: /Browse your site/i } );
2323
await expect( page ).toMatchElement( '.done__preview-iframe' );
2424

25+
// Checks for admin bar in iframe phone preview.
26+
const iframeElement = await page.$( 'iframe[name="amp-wizard-completion-preview"]' );
27+
const previewFrame = await iframeElement.contentFrame();
28+
await expect( previewFrame ).not.toMatchElement( '#wpadminbar' );
29+
2530
await expect( '.done__links-container a' ).not.countToBe( 0 );
2631

2732
const originalIframeSrc = await page.$eval( '.done__preview-iframe', ( e ) => e.getAttribute( 'src' ) );

tests/php/test-amp-helper-functions.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,43 @@ static function() use ( $home_url ) {
547547
);
548548
}
549549

550+
/** @covers ::amp_remove_admin_bar_in_phone_preview() */
551+
public function test_amp_remove_admin_bar_in_phone_preview() {
552+
add_filter( 'amp_dev_mode_enabled', '__return_true', 99 );
553+
add_filter( 'show_admin_bar', '__return_true', 99 );
554+
555+
ob_start();
556+
amp_remove_admin_bar_in_phone_preview();
557+
$output = ob_get_clean();
558+
559+
$this->assertStringContainsString( '<script data-ampdevmode>', $output );
560+
$this->assertStringContainsString( "'amp-wizard-completion-preview' !== window.name )", $output );
561+
562+
remove_filter( 'amp_dev_mode_enabled', '__return_true', 99 );
563+
remove_filter( 'show_admin_bar', '__return_true', 99 );
564+
}
565+
566+
/** @covers ::amp_remove_admin_bar_in_phone_preview() */
567+
public function test_amp_remove_admin_bar_in_phone_preview_without_dev_mode() {
568+
add_filter( 'amp_dev_mode_enabled', '__return_false', 99 );
569+
570+
ob_start();
571+
amp_remove_admin_bar_in_phone_preview();
572+
$output = ob_get_clean();
573+
$this->assertEmpty( $output );
574+
575+
remove_filter( 'amp_dev_mode_enabled', '__return_false', 99 );
576+
577+
add_filter( 'show_admin_bar', '__return_false', 99 );
578+
579+
ob_start();
580+
amp_remove_admin_bar_in_phone_preview();
581+
$output = ob_get_clean();
582+
$this->assertEmpty( $output );
583+
584+
remove_filter( 'show_admin_bar', '__return_false', 99 );
585+
}
586+
550587
/**
551588
* Test amp_get_current_url().
552589
*

0 commit comments

Comments
 (0)