From 739a8f752d8483256dd9d33fa0921a9f33713e59 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 20 Dec 2019 12:26:29 -0800 Subject: [PATCH 1/3] Introduce ampdevmode data flag on script/style dependencies to indicate data-ampdevmode needed --- includes/amp-helper-functions.php | 22 +++++++- includes/class-amp-theme-support.php | 66 ++++++++++++++-------- tests/php/test-amp-helper-functions.php | 1 - tests/php/test-class-amp-theme-support.php | 56 ++++++++++++------ 4 files changed, 101 insertions(+), 44 deletions(-) diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 0bfca629a3c..e7b3d9cc27a 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 ) { // @todo Fail! This is currently being called _before_ the template is rendered, so it will always be empty. + 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 ) { // @todo Fail! This is currently being called _before_ the template is rendered, so it will always be empty. + 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..a2bd4017e7d 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -1098,6 +1098,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 +1429,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 +1551,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