@@ -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