Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check for empty dependencies on admin bar style loader tag filter #7920

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1378,8 +1378,12 @@
* @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' )
) {
Expand All @@ -1398,9 +1402,15 @@
* @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 warning on line 1409 in includes/class-amp-theme-support.php

View check run for this annotation

Codecov / codecov/patch

includes/class-amp-theme-support.php#L1409

Added line #L1409 was not covered by tests

// 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 )

Check warning on line 1413 in includes/class-amp-theme-support.php

View check run for this annotation

Codecov / codecov/patch

includes/class-amp-theme-support.php#L1413

Added line #L1413 was not covered by tests
? self::is_exclusively_dependent( wp_styles(), $handle, $customize_preview )
: self::has_dependency( wp_styles(), $handle, $customize_preview )
) {
Expand All @@ -1420,8 +1430,12 @@
* @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' )
) {
Expand Down
12 changes: 6 additions & 6 deletions tests/php/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<link rel="stylesheet" id="dashicons-css" href="https://example.com/wp-includes/css/dashicons.css?ver=5.3.2" media="all" />'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
$this->assertEquals( $tag, AMP_Theme_Support::filter_admin_bar_style_loader_tag( $tag, 'baz' ) );
}
Expand Down Expand Up @@ -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 = '<script src="https://example.com/wp-includes/js/admin-bar.js?ver=5.3.2"></script>'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
$this->assertEquals( $tag, AMP_Theme_Support::filter_admin_bar_script_loader_tag( $tag, 'example' ) );
}
Expand Down
Loading