From a27f07f7a99182277818be6e5bb202ab24259519 Mon Sep 17 00:00:00 2001 From: Takshil Kunadia Date: Wed, 13 Nov 2024 16:18:36 +0530 Subject: [PATCH 1/4] Add check for empty dependencies --- includes/class-amp-theme-support.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index d7bab488213..02c89f13d50 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -1379,7 +1379,7 @@ protected static function is_exclusively_dependent( WP_Dependencies $dependencie */ public static function filter_admin_bar_style_loader_tag( $tag, $handle ) { if ( - is_array( wp_styles()->registered['admin-bar']->deps ) && in_array( $handle, wp_styles()->registered['admin-bar']->deps, true ) ? + ! empty( wp_styles()->registered['admin-bar']->deps ) && is_array( wp_styles()->registered['admin-bar']->deps ) && in_array( $handle, wp_styles()->registered['admin-bar']->deps, true ) ? self::is_exclusively_dependent( wp_styles(), $handle, 'admin-bar' ) : self::has_dependency( wp_styles(), $handle, 'admin-bar' ) ) { From 52fc4624d1a9853514d830b595c9466a5764cf9f Mon Sep 17 00:00:00 2001 From: Takshil Kunadia Date: Thu, 14 Nov 2024 11:41:38 +0530 Subject: [PATCH 2/4] rorganise code --- includes/class-amp-theme-support.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 02c89f13d50..457c6b6399d 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 ( - ! empty( wp_styles()->registered['admin-bar']->deps ) && 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' ) ) { From 79b1b7a5b00e2d4e5872b26c5f96469b244dca96 Mon Sep 17 00:00:00 2001 From: Takshil Kunadia Date: Thu, 14 Nov 2024 11:41:57 +0530 Subject: [PATCH 3/4] Update test --- tests/php/test-class-amp-theme-support.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/php/test-class-amp-theme-support.php b/tests/php/test-class-amp-theme-support.php index ef9861578ee..15b7e002859 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' ) ); } From 5f17b90a4a2da7af18e652da6c65ea205bc4f267 Mon Sep 17 00:00:00 2001 From: Takshil Kunadia Date: Fri, 15 Nov 2024 15:50:39 +0530 Subject: [PATCH 4/4] refactor similar functions & test --- includes/class-amp-theme-support.php | 14 ++++++++++++-- tests/php/test-class-amp-theme-support.php | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 457c6b6399d..75db51dd1db 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -1402,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 ) ) { @@ -1424,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 15b7e002859..8620144c09b 100644 --- a/tests/php/test-class-amp-theme-support.php +++ b/tests/php/test-class-amp-theme-support.php @@ -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' ) ); }