From 06eea1b33d3bb526f64e09e5c91d16672e72cde0 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 7 Aug 2014 02:29:15 +0000 Subject: [PATCH] After [28883], ensure that priorities have callbacks before returning `true` in `has_filter()`. Adds unit tests. Props boonebgorges. Fixes #29070. git-svn-id: https://develop.svn.wordpress.org/trunk@29422 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/plugin.php | 18 +++++++++++++++++- tests/phpunit/tests/filters.php | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php index ea5832abaf..bfa5f9d1bb 100644 --- a/src/wp-includes/plugin.php +++ b/src/wp-includes/plugin.php @@ -107,7 +107,23 @@ function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 function has_filter($tag, $function_to_check = false) { global $wp_filter; - $has = !empty($wp_filter[$tag]); + $has = ! empty( $wp_filter[ $tag ] ); + + // Make sure at least one priority has a filter callback + if ( $has ) { + $exists = false; + foreach ( $wp_filter[ $tag ] as $callbacks ) { + if ( ! empty( $callbacks ) ) { + $exists = true; + break; + } + } + + if ( ! $exists ) { + $has = false; + } + } + if ( false === $function_to_check || false == $has ) return $has; diff --git a/tests/phpunit/tests/filters.php b/tests/phpunit/tests/filters.php index 0f6d00a320..34fdf56355 100644 --- a/tests/phpunit/tests/filters.php +++ b/tests/phpunit/tests/filters.php @@ -267,4 +267,30 @@ class Tests_Filters extends WP_UnitTestCase { $this->assertEquals( 1, $b->get_call_count(), 'priority 12 filters should run after priority 10 empties itself and priority 11 runs' ); $this->assertEquals( $result, $tag . '_append_append', 'priority 11 and 12 filters should run after priority 10 empties itself' ); } + + /** + * @ticket 29070 + */ + function test_has_filter_after_remove_all_filters() { + $a = new MockAction(); + $tag = rand_str(); + $val = rand_str(); + + // No priority + add_filter( $tag, array( $a, 'filter' ), 11 ); + add_filter( $tag, array( $a, 'filter' ), 12 ); + $this->assertTrue( has_filter( $tag ) ); + + remove_all_filters( $tag ); + $this->assertFalse( has_filter( $tag ) ); + + // Remove priorities one at a time + add_filter( $tag, array( $a, 'filter' ), 11 ); + add_filter( $tag, array( $a, 'filter' ), 12 ); + $this->assertTrue( has_filter( $tag ) ); + + remove_all_filters( $tag, 11 ); + remove_all_filters( $tag, 12 ); + $this->assertFalse( has_filter( $tag ) ); + } }