Skip to content

Commit

Permalink
Prevent errors in admin bar filters from non-array arguments (#4207)
Browse files Browse the repository at this point in the history
Commit Weston's fix for the in_array() error, add small tests

Prevents running in_array() if the deps are not an array.
  • Loading branch information
kienstra authored and westonruter committed Jan 31, 2020
1 parent 760cd44 commit f8acc9a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,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 @@ -1550,7 +1550,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 @@ -1416,6 +1416,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" />'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
$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 @@ -1484,6 +1496,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>'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
$this->assertEquals( $tag, AMP_Theme_Support::filter_admin_bar_script_loader_tag( $tag, 'example' ) );
}

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

0 comments on commit f8acc9a

Please sign in to comment.