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

Prevent errors in admin bar filters from non-array arguments #4207

Merged
merged 3 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ protected static function is_exclusively_dependent( WP_Dependencies $dependencie
*/
public static function filter_admin_bar_style_loader_tag( $tag, $handle ) {
if (
in_array( $handle, wp_styles()->registered['admin-bar']->deps, true ) ?
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' )
) {
Expand All @@ -1586,7 +1586,7 @@ public static function filter_admin_bar_style_loader_tag( $tag, $handle ) {
*/
public static function filter_admin_bar_script_loader_tag( $tag, $handle ) {
if (
in_array( $handle, wp_scripts()->registered['admin-bar']->deps, true ) ?
is_array( wp_scripts()->registered['admin-bar']->deps ) && in_array( $handle, wp_scripts()->registered['admin-bar']->deps, true ) ?
self::is_exclusively_dependent( wp_scripts(), $handle, 'admin-bar' ) :
self::has_dependency( wp_scripts(), $handle, 'admin-bar' )
) {
Expand Down
24 changes: 24 additions & 0 deletions tests/php/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,18 @@ public function test_filter_admin_bar_style_loader_tag( $setup_callback, $assert
$assert_callback( new DOMXPath( $dom ) );
}

/**
* Test filter_admin_bar_style_loader_tag when ->deps is not an array.
*
* @covers \AMP_Theme_Support::filter_admin_bar_style_loader_tag()
*/
public function test_filter_admin_bar_style_loader_tag_non_array() {
wp_enqueue_style( 'admin-bar' );
$GLOBALS['wp_styles']->registered['admin-bar']->deps = null;
$tag = '<link rel="stylesheet" id="dashicons-css" href="https://example.com/wp-includes/css/dashicons.css?ver=5.3.2" media="all" />';
$this->assertEquals( $tag, AMP_Theme_Support::filter_admin_bar_style_loader_tag( $tag, 'baz' ) );
}

/**
* Get data to test AMP_Theme_Support::filter_admin_bar_script_loader_tag().
*
Expand Down Expand Up @@ -1547,6 +1559,18 @@ public function test_filter_admin_bar_script_loader_tag( $setup_callback, $asser
$assert_callback( new DOMXPath( $dom ) );
}

/**
* Test filter_admin_bar_script_loader_tag when ->deps is not an array.
*
* @covers \AMP_Theme_Support::filter_admin_bar_script_loader_tag()
*/
public function test_filter_admin_bar_script_loader_tag_non_array() {
wp_enqueue_script( 'admin-bar' );
$GLOBALS['wp_scripts']->registered['admin-bar']->deps = null;
$tag = '<script src="https://example.com/wp-includes/js/admin-bar.js?ver=5.3.2"></script>';
$this->assertEquals( $tag, AMP_Theme_Support::filter_admin_bar_script_loader_tag( $tag, 'example' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose in reality the assertion here serves the purpose of preventing the test from being flagged for having no assertions. Since there is no style registered with the handle of baz, neither of the conditions self::is_exclusively_dependent( wp_scripts(), $handle, 'admin-bar' ) nor self::has_dependency( wp_scripts(), $handle, 'admin-bar' ) will ever be true.

}

/**
* Test init_admin_bar to ensure dashicons are not added to dev mode when directly enqueued.
*
Expand Down