Skip to content

Duplicated script printed to hide admin bar in onboarding wizard #6643

@westonruter

Description

@westonruter

Bug Description

I just noticed that the we're printing the same script multiple times, including at wp_head and wp_footer and anywhere wp_print_scripts() is called:

image

The code in question is the following:

/*
* Hide admin bar if the window is inside the setup wizard iframe.
*
* Detects whether the current window is in an iframe with the specified `name` attribute. The iframe is created
* by Preview component located in <assets/src/setup/pages/save/index.js>.
*/
add_action(
'wp_print_scripts',
function() {
if ( ! amp_is_dev_mode() || ! is_admin_bar_showing() ) {
return;
}
?>
<script data-ampdevmode>
( () => {
if ( 'amp-wizard-completion-preview' !== window.name ) {
return;
}
/** @type {HTMLStyleElement} */
const style = document.createElement( 'style' );
style.setAttribute( 'type', 'text/css' );
style.appendChild( document.createTextNode( 'html:not(#_) { margin-top: 0 !important; } #wpadminbar { display: none !important; }' ) );
document.head.appendChild( style );
document.addEventListener( 'DOMContentLoaded', function() {
const adminBar = document.getElementById( 'wpadminbar' );
if ( adminBar ) {
document.body.classList.remove( 'admin-bar' );
adminBar.remove();
}
});
} )();
</script>
<?php
}
);

Instead of the wp_print_scripts action we should consider the wp_print_footer_scripts action, or else the wp_head (and amp_post_template_head) action. Additionally, the script should be moved from a closure into a separate function.

Expected Behaviour

The script should only be printed once.

Screenshots

No response

PHP Version

No response

Plugin Version

2.1.4

AMP plugin template mode

Standard, Transitional, Reader

WordPress Version

No response

Site Health

No response

Gutenberg Version

No response

OS(s) Affected

No response

Browser(s) Affected

No response

Device(s) Affected

No response

Acceptance Criteria

No response

Implementation Brief

No response

QA Testing Instructions

No response

Demo

No response

Changelog Entry

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't workingChangeloggedWhether the issue/PR has been added to release notes.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions