Make WordPress Core


Ignore:
Timestamp:
07/31/2022 02:18:36 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/filters.php

    r53802 r53803  
    151151        }
    152152
     153        /**
     154         * @covers ::did_filter
     155         */
     156        public function test_did_filter() {
     157                $tag1 = 'filter1';
     158                $tag2 = 'filter2';
     159                $val  = __FUNCTION__ . '_val';
     160
     161                // Apply filter $tag1 but not $tag2.
     162                apply_filters( $tag1, $val );
     163                $this->assertSame( 1, did_filter( $tag1 ) );
     164                $this->assertSame( 0, did_filter( $tag2 ) );
     165
     166                // Apply filter $tag2 10 times.
     167                $count = 10;
     168                for ( $i = 0; $i < $count; $i++ ) {
     169                        apply_filters( $tag2, $val );
     170                }
     171
     172                // $tag1's count hasn't changed, $tag2 should be correct.
     173                $this->assertSame( 1, did_filter( $tag1 ) );
     174                $this->assertSame( $count, did_filter( $tag2 ) );
     175
     176        }
     177
    153178        public function test_all_filter() {
    154179                $a    = new MockAction();
Note: See TracChangeset for help on using the changeset viewer.