Skip to content

Commit 294c3af

Browse files
authored
Merge pull request #1143 from google/fix/1142-amp-dev-mode
Fix AMP dev mode support
2 parents 1d244bb + 705bb1c commit 294c3af

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

includes/Core/Admin_Bar/Admin_Bar.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ function( $wp_admin_bar ) {
8282
99
8383
);
8484

85+
// TODO: This can be removed at some point, see https://github.com/ampproject/amp-wp/pull/4001.
86+
add_filter( 'amp_dev_mode_element_xpaths', array( $this, 'add_amp_dev_mode' ) );
87+
8588
$admin_bar_callback = function() {
8689
if ( ! $this->is_active() ) {
8790
return;
@@ -93,12 +96,9 @@ function( $wp_admin_bar ) {
9396
// Enqueue styles.
9497
$this->assets->enqueue_asset( 'googlesitekit_adminbar_css' );
9598

96-
if ( $this->context->is_amp() ) {
97-
if ( ! $this->is_amp_dev_mode() ) {
98-
// AMP Dev Mode support was added in v1.4, and if it is not enabled then short-circuit since scripts will be invalid.
99-
return;
100-
}
101-
add_filter( 'amp_dev_mode_element_xpaths', array( $this, 'add_amp_dev_mode' ) );
99+
if ( $this->context->is_amp() && ! $this->is_amp_dev_mode() ) {
100+
// AMP Dev Mode support was added in v1.4, and if it is not enabled then short-circuit since scripts will be invalid.
101+
return;
102102
}
103103

104104
// Enqueue scripts.

includes/Core/Assets/Assets.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ private function add_amp_dev_mode_attributes( $assets ) {
288288
add_filter(
289289
'script_loader_tag',
290290
function ( $tag, $handle ) use ( $assets ) {
291-
if ( $this->context->is_amp() && isset( $assets[ $handle ] ) && $assets[ $handle ] instanceof Script ) {
291+
// TODO: 'hoverintent-js' can be removed from here at some point, see https://github.com/ampproject/amp-wp/pull/3928.
292+
if ( $this->context->is_amp() && ( isset( $assets[ $handle ] ) && $assets[ $handle ] instanceof Script || 'hoverintent-js' === $handle ) ) {
292293
$tag = preg_replace( '/(?<=<script)(?=\s|>)/i', ' data-ampdevmode', $tag );
293294
}
294295
return $tag;
@@ -376,7 +377,7 @@ private function get_assets() {
376377
'nonceEndpoint' => admin_url( 'admin-ajax.php?action=rest-nonce' ),
377378
'nonceMiddleware' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
378379
'rootURL' => esc_url_raw( get_rest_url() ),
379-
)
380+
)
380381
),
381382
'before'
382383
);
@@ -719,6 +720,8 @@ private function add_async_defer_attribute( $tag, $handle ) {
719720
* @param array $handles List of handles to run before print callbacks for.
720721
*/
721722
private function run_before_print_callbacks( WP_Dependencies $dependencies, array $handles ) {
723+
$is_amp = $this->context->is_amp();
724+
722725
foreach ( $handles as $handle ) {
723726
if ( isset( $this->print_callbacks_done[ $handle ] ) ) {
724727
continue;
@@ -728,11 +731,34 @@ private function run_before_print_callbacks( WP_Dependencies $dependencies, arra
728731

729732
if ( isset( $this->assets[ $handle ] ) ) {
730733
$this->assets[ $handle ]->before_print();
734+
735+
// TODO: This can be removed at some point, see https://github.com/ampproject/amp-wp/pull/4001.
736+
if ( $is_amp && $this->assets[ $handle ] instanceof Script ) {
737+
$this->add_extra_script_amp_dev_mode( $handle );
738+
}
731739
}
732740

733741
if ( isset( $dependencies->registered[ $handle ] ) && is_array( $dependencies->registered[ $handle ]->deps ) ) {
734742
$this->run_before_print_callbacks( $dependencies, $dependencies->registered[ $handle ]->deps );
735743
}
736744
}
737745
}
746+
747+
/**
748+
* Adds a comment to all extra scripts so that they are considered compatible with AMP dev mode.
749+
*
750+
* {@see Assets::add_amp_dev_mode_attributes()} makes all registered scripts and stylesheets compatible, including
751+
* their potential inline additions. This method does the same for extra scripts, which are registered under the
752+
* 'data' key.
753+
*
754+
* @since n.e.x.t
755+
*
756+
* @param string $handle The handle of a registered script.
757+
*/
758+
private function add_extra_script_amp_dev_mode( $handle ) {
759+
$data = wp_scripts()->get_data( $handle, 'data' ) ?: '';
760+
if ( ! empty( $data ) && is_string( $data ) ) {
761+
wp_scripts()->add_data( $handle, 'data', '/*googlesitekit*/ ' . $data );
762+
}
763+
}
738764
}

0 commit comments

Comments
 (0)