diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index d7bab488213..75db51dd1db 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -1378,8 +1378,12 @@ protected static function is_exclusively_dependent( WP_Dependencies $dependencie * @return string Tag. */ public static function filter_admin_bar_style_loader_tag( $tag, $handle ) { + // Get Admin bar styles. + $admin_bar_dep = wp_styles()->query( 'admin-bar' ); + + // Check if the handle is a dependency of the admin-bar. If so, add the data-ampdevmode attribute. if ( - is_array( wp_styles()->registered['admin-bar']->deps ) && in_array( $handle, wp_styles()->registered['admin-bar']->deps, true ) ? + $admin_bar_dep && in_array( $handle, $admin_bar_dep->deps, true ) ? self::is_exclusively_dependent( wp_styles(), $handle, 'admin-bar' ) : self::has_dependency( wp_styles(), $handle, 'admin-bar' ) ) { @@ -1398,9 +1402,15 @@ public static function filter_admin_bar_style_loader_tag( $tag, $handle ) { * @return string Tag. */ public static function filter_customize_preview_style_loader_tag( $tag, $handle ) { + // Handle for customize-preview. $customize_preview = 'customize-preview'; + + // Registered styles for customize-preview. + $customize_preview_dep = wp_styles()->query( $customize_preview ); + + // Check if the handle is a dependency of the customize-preview. If so, add the data-ampdevmode attribute. if ( - is_array( wp_styles()->registered[ $customize_preview ]->deps ) && in_array( $handle, wp_styles()->registered[ $customize_preview ]->deps, true ) + in_array( $handle, $customize_preview_dep->deps, true ) ? self::is_exclusively_dependent( wp_styles(), $handle, $customize_preview ) : self::has_dependency( wp_styles(), $handle, $customize_preview ) ) { @@ -1420,8 +1430,12 @@ public static function filter_customize_preview_style_loader_tag( $tag, $handle * @return string Tag. */ public static function filter_admin_bar_script_loader_tag( $tag, $handle ) { + // Get Admin bar scripts. + $admin_bar_dep = wp_scripts()->query( 'admin-bar' ); + + // Check if the handle is a dependency of the admin-bar. If so, add the data-ampdevmode attribute. if ( - is_array( wp_scripts()->registered['admin-bar']->deps ) && in_array( $handle, wp_scripts()->registered['admin-bar']->deps, true ) ? + in_array( $handle, $admin_bar_dep->deps, true ) ? self::is_exclusively_dependent( wp_scripts(), $handle, 'admin-bar' ) : self::has_dependency( wp_scripts(), $handle, 'admin-bar' ) ) { diff --git a/tests/php/test-class-amp-theme-support.php b/tests/php/test-class-amp-theme-support.php index ef9861578ee..8620144c09b 100644 --- a/tests/php/test-class-amp-theme-support.php +++ b/tests/php/test-class-amp-theme-support.php @@ -1205,13 +1205,13 @@ public function test_filter_admin_bar_style_loader_tag( $setup_callback, $assert } /** - * Test filter_admin_bar_style_loader_tag when ->deps is not an array. + * Test filter_admin_bar_style_loader_tag when ->deps is an empty array. * * @covers \AMP_Theme_Support::filter_admin_bar_style_loader_tag() */ - public function test_filter_admin_bar_style_loader_tag_non_array() { + public function test_filter_admin_bar_style_loader_tag_empty_array() { wp_enqueue_style( 'admin-bar' ); - $GLOBALS['wp_styles']->registered['admin-bar']->deps = null; + $GLOBALS['wp_styles']->registered['admin-bar']->deps = []; $tag = ''; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet $this->assertEquals( $tag, AMP_Theme_Support::filter_admin_bar_style_loader_tag( $tag, 'baz' ) ); } @@ -1285,13 +1285,13 @@ public function test_filter_admin_bar_script_loader_tag( $setup_callback, $asser } /** - * Test filter_admin_bar_script_loader_tag when ->deps is not an array. + * Test filter_admin_bar_script_loader_tag when ->deps is an empty array. * * @covers \AMP_Theme_Support::filter_admin_bar_script_loader_tag() */ - public function test_filter_admin_bar_script_loader_tag_non_array() { + public function test_filter_admin_bar_script_loader_tag_empty_array() { wp_enqueue_script( 'admin-bar' ); - $GLOBALS['wp_scripts']->registered['admin-bar']->deps = null; + $GLOBALS['wp_scripts']->registered['admin-bar']->deps = []; $tag = ''; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript $this->assertEquals( $tag, AMP_Theme_Support::filter_admin_bar_script_loader_tag( $tag, 'example' ) ); }