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:
Sergey Biryukov
2022-07-31 14:18:36 +00:00
parent 95efa62a7d
commit 65e266ede6
2 changed files with 68 additions and 2 deletions

View File

@@ -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';