diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 0bfca629a3c..f193247beb3 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -960,8 +960,28 @@ function amp_get_content_sanitizers( $post = null ) { if ( is_admin_bar_showing() ) { $dev_mode_xpaths[] = '//*[ @id = "wpadminbar" ]'; $dev_mode_xpaths[] = '//*[ @id = "wpadminbar" ]//*'; - $dev_mode_xpaths[] = '//style[ @id = "admin-bar-inline-css" ]'; } + + // Ensure script localization data gets flagged for dev mode. This only applies to wp_localize_script() as + // inline scripts added via wp_add_inline_script() get filtered by script_loader_tag and thus will have the + // data-ampdevmode attribute added via AMP_Theme_Support::filter_script_loader_tag_for_dev_mode(). + foreach ( wp_scripts()->done as $script_handle ) { + if ( ! AMP_Theme_Support::dependency_needs_dev_mode( wp_scripts(), $script_handle ) ) { + continue; + } + $data = wp_scripts()->get_data( $script_handle, 'data' ); + if ( preg_match( '/(\bvar\s*\w+\s+=)/', $data, $matches ) ) { + $dev_mode_xpaths[] = sprintf( '//script[ not( @src ) ][ contains( text(), "%s" ) ]', $matches[1] ); + } + } + + // Ensure all inline styles added via wp_add_inline_style() get the data-ampdevmode attribute. + foreach ( wp_styles()->done as $style_handle ) { + if ( AMP_Theme_Support::dependency_needs_dev_mode( wp_styles(), $style_handle ) ) { + $dev_mode_xpaths[] = sprintf( '//style[ @id = "%s" ]', "$style_handle-inline-css" ); + } + } + $sanitizers = array_merge( [ 'AMP_Dev_Mode_Sanitizer' => [ diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index b9fe763e303..ac8dcf1c733 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -99,13 +99,6 @@ class AMP_Theme_Support { const PAIRED_BROWSING_QUERY_VAR = 'amp-paired-browsing'; - /** - * Sanitizer classes. - * - * @var array - */ - protected static $sanitizer_classes = []; - /** * Embed handlers. * @@ -442,20 +435,32 @@ static function() { } self::add_hooks(); - self::$sanitizer_classes = amp_get_content_sanitizers(); - if ( ! $is_reader_mode ) { - self::$sanitizer_classes = AMP_Validation_Manager::filter_sanitizer_args( self::$sanitizer_classes ); - } self::$embed_handlers = self::register_content_embed_handlers(); - self::$sanitizer_classes['AMP_Embed_Sanitizer']['embed_handlers'] = self::$embed_handlers; - foreach ( self::$sanitizer_classes as $sanitizer_class => $args ) { + // @todo It is not ideal that get_sanitizer_classes() is called here before the template is rendered and after the template is rendered. + foreach ( self::get_sanitizer_classes() as $sanitizer_class => $args ) { if ( method_exists( $sanitizer_class, 'add_buffering_hooks' ) ) { call_user_func( [ $sanitizer_class, 'add_buffering_hooks' ], $args ); } } } + /** + * Get sanitizer classes. + * + * @see amp_get_content_sanitizers() + * + * @return array Mapping of sanitizer class name to constructor args array. + */ + protected static function get_sanitizer_classes() { + $sanitizer_classes = amp_get_content_sanitizers(); + if ( self::READER_MODE_SLUG !== self::get_support_mode() ) { + $sanitizer_classes = AMP_Validation_Manager::filter_sanitizer_args( $sanitizer_classes ); + } + $sanitizer_classes['AMP_Embed_Sanitizer']['embed_handlers'] = self::$embed_handlers; + return $sanitizer_classes; + } + /** * Ensure that the current AMP location is correct. * @@ -1098,6 +1103,8 @@ static function( $html ) { ); add_action( 'admin_bar_init', [ __CLASS__, 'init_admin_bar' ] ); + add_filter( 'style_loader_tag', [ __CLASS__, 'filter_style_loader_tag_for_dev_mode' ], 10, 2 ); + add_filter( 'script_loader_tag', [ __CLASS__, 'filter_script_loader_tag_for_dev_mode' ], 10, 2 ); add_action( 'wp_head', 'amp_add_generator_metadata', 20 ); add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_assets' ], 0 ); // Enqueue before theme's styles. add_action( 'wp_enqueue_scripts', [ __CLASS__, 'dequeue_customize_preview_scripts' ], 1000 ); @@ -1427,8 +1434,6 @@ public static function filter_cancel_comment_reply_link( $formatted_link, $link, * @since 1.0 */ public static function init_admin_bar() { - add_filter( 'style_loader_tag', [ __CLASS__, 'filter_admin_bar_style_loader_tag' ], 10, 2 ); - add_filter( 'script_loader_tag', [ __CLASS__, 'filter_admin_bar_script_loader_tag' ], 10, 2 ); // Inject the data-ampdevmode attribute into the admin bar bump style. See \WP_Admin_Bar::initialize(). if ( current_theme_supports( 'admin-bar' ) ) { @@ -1551,40 +1556,53 @@ protected static function is_exclusively_dependent( WP_Dependencies $dependencie } /** - * Add data-ampdevmode attribute to any enqueued style that depends on the admin-bar. + * Determine whether a given dependency handle needs dev mode. * - * @since 1.3 + * @since 1.5 + * + * @param WP_Dependencies $dependencies Dependencies (wither WP_Scripts or WP_Styles). + * @param string $handle Dependency handle (for script or style). + * @return bool Whether the