mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-03 04:04:35 +00:00
Plugins: Introduce did_filter() function.
While most of the action functions are aliases for the respective filter functions, using `did_action()` to detect whether a filter has been run is not possible, as it only works specifically for actions. This is now resolved by introducing a new function, `did_filter()`, which retrieves the number of times a filter has been applied during the current request, bringing parity with `did_action()`. Follow-up to [4630], [6318], [27294]. Props mordauk, chriscct7, andykeith, nacin, dd32, markparnell, SergeyBiryukov. Fixes #35357. git-svn-id: https://develop.svn.wordpress.org/trunk@53803 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -150,6 +150,31 @@ class Tests_Filters extends WP_UnitTestCase {
|
||||
$this->assertSame( $expected, $a->get_events() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::did_filter
|
||||
*/
|
||||
public function test_did_filter() {
|
||||
$tag1 = 'filter1';
|
||||
$tag2 = 'filter2';
|
||||
$val = __FUNCTION__ . '_val';
|
||||
|
||||
// Apply filter $tag1 but not $tag2.
|
||||
apply_filters( $tag1, $val );
|
||||
$this->assertSame( 1, did_filter( $tag1 ) );
|
||||
$this->assertSame( 0, did_filter( $tag2 ) );
|
||||
|
||||
// Apply filter $tag2 10 times.
|
||||
$count = 10;
|
||||
for ( $i = 0; $i < $count; $i++ ) {
|
||||
apply_filters( $tag2, $val );
|
||||
}
|
||||
|
||||
// $tag1's count hasn't changed, $tag2 should be correct.
|
||||
$this->assertSame( 1, did_filter( $tag1 ) );
|
||||
$this->assertSame( $count, did_filter( $tag2 ) );
|
||||
|
||||
}
|
||||
|
||||
public function test_all_filter() {
|
||||
$a = new MockAction();
|
||||
$tag1 = __FUNCTION__ . '_1';
|
||||
|
||||
Reference in New Issue
Block a user