WordPress.org

Make WordPress Core

Ticket #35357: 35357.4.patch

File 35357.4.patch, 6.4 KB (added by chriscct7, 5 years ago)
  • src/wp-includes/plugin.php

     
    2020 */
    2121
    2222// Initialize the filter globals.
    23 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
     23global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter, $wp_done_filters;
    2424
    2525if ( ! isset( $wp_filter ) )
    2626        $wp_filter = array();
     
    3434if ( ! isset( $wp_current_filter ) )
    3535        $wp_current_filter = array();
    3636
     37if ( ! isset( $wp_done_filters ) ) {
     38        $wp_done_filters = array();
     39}
     40
    3741/**
    3842 * Hook a function or method to a specific filter action.
    3943 *
     
    190194 * @global array $wp_filter         Stores all of the filters.
    191195 * @global array $merged_filters    Merges the filter hooks using this function.
    192196 * @global array $wp_current_filter Stores the list of current filters with the current one last.
     197 * @global array $wp_done_filters   Stores the number of times filters have been run.
    193198 *
    194199 * @param string $tag     The name of the filter hook.
    195200 * @param mixed  $value   The value on which the filters hooked to `$tag` are applied on.
     
    197202 * @return mixed The filtered value after all hooked functions are applied to it.
    198203 */
    199204function apply_filters( $tag, $value ) {
    200         global $wp_filter, $merged_filters, $wp_current_filter;
     205        global $wp_filter, $merged_filters, $wp_current_filter, $wp_done_filters, $wp_actions;
    201206
     207        if ( ! isset( $wp_actions[ $tag ] ) ) {
     208                $wp_actions[$tag] = 1;
     209        } else {
     210                ++$wp_actions[ $tag] ;
     211        }
     212
    202213        $args = array();
    203214
    204215        // Do 'all' actions first.
     
    233244                        if ( !is_null($the_['function']) ){
    234245                                $args[1] = $value;
    235246                                $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
     247                                if ( empty( $wp_done_filters[ $tag ] ) ){
     248                                        $wp_done_filters[ $tag ] = 1;
     249                                } else {
     250                                        $wp_done_filters[ $tag ] = $wp_done_filters[ $tag ] + 1;
     251                                }
    236252                        }
    237253
    238254        } while ( next($wp_filter[$tag]) !== false );
     
    252268 *
    253269 * @global array $wp_filter         Stores all of the filters
    254270 * @global array $merged_filters    Merges the filter hooks using this function.
    255  * @global array $wp_current_filter Stores the list of current filters with the current one last
     271 * @global array $wp_current_filter Stores the list of current filters with the current one last.
     272 * @global array $wp_done_filters   Stores the number of times filters have been run.
    256273 *
    257274 * @param string $tag  The name of the filter hook.
    258275 * @param array  $args The arguments supplied to the functions hooked to $tag.
     
    259276 * @return mixed The filtered value after all hooked functions are applied to it.
    260277 */
    261278function apply_filters_ref_array($tag, $args) {
    262         global $wp_filter, $merged_filters, $wp_current_filter;
     279        global $wp_filter, $merged_filters, $wp_current_filter, $wp_done_filters, $wp_actions;
    263280
     281        if ( ! isset( $wp_actions[ $tag ] ) ) {
     282                $wp_actions[$tag] = 1;
     283        } else {
     284                ++$wp_actions[ $tag] ;
     285        }
     286
    264287        // Do 'all' actions first
    265288        if ( isset($wp_filter['all']) ) {
    266289                $wp_current_filter[] = $tag;
     
    287310
    288311        do {
    289312                foreach ( (array) current($wp_filter[$tag]) as $the_ )
    290                         if ( !is_null($the_['function']) )
     313                        if ( !is_null($the_['function'] ) ) {
    291314                                $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     315                                if ( empty( $wp_done_filters[ $tag ] ) ){
     316                                        $wp_done_filters[ $tag ] = 1;
     317                                } else {
     318                                        $wp_done_filters[ $tag ] = $wp_done_filters[ $tag ] + 1;
     319                                }
     320                        }
    292321
    293322        } while ( next($wp_filter[$tag]) !== false );
    294323
     
    530559}
    531560
    532561/**
     562 * Retrieve the number of functions run on a filter.
     563 *
     564 * @since 4.5.0
     565 *
     566 * @global array $wp_done_filters Increments the amount of times a function was run on a filter.
     567 *
     568 * @param string $tag The name of the filter hook.
     569 * @return int The number of times filter hooked function is fired on filter $tag.
     570 */
     571function filter_functions_run($tag) {
     572        global $wp_done_filters;
     573
     574        if ( ! isset( $wp_done_filters[ $tag ] ) ) {
     575                return 0;
     576        }
     577
     578        return $wp_done_filters[$tag];
     579}
     580
     581/**
     582 * Retrieve the number of times a filter is fired.
     583 *
     584 * An alias of did_action for filters.
     585 *
     586 * @since 4.5.0
     587 *
     588 * @uses did_action
     589 *
     590 * @param string $tag The name of the filter hook.
     591 * @return int The number of times filter hook $tag is fired.
     592 */
     593function did_filter($tag) {
     594        return did_action($tag);
     595}
     596
     597/**
    533598 * Retrieve the number of times an action is fired.
    534599 *
    535600 * @since 2.1.0
  • tests/phpunit/tests/filters.php

     
    235235        }
    236236
    237237        /**
     238         * @ticket 35357
     239         */
     240        function test_did_filter() {
     241                $obj = new stdClass();
     242                $a = new MockAction();
     243                $b = new MockAction();
     244                $c = new MockAction();
     245                $tag1 = rand_str();
     246                $tag2 = rand_str();
     247                $tag3 = rand_str();
     248
     249                add_action( $tag1, array( $a, 'filter_append' ), 10, 2 );
     250                add_action( $tag2, array( $b, 'filter_append' ), 10, 2 );
     251                add_action( $tag3, array( $c, 'filter_append' ), 10, 2 );
     252
     253                $result1 = apply_filters_ref_array( $tag2, array( 'string', &$obj ) );
     254                $result2 = apply_filters_ref_array( $tag3, array( 'string', &$obj ) );
     255                $result3 = apply_filters_ref_array( $tag3, array( 'string', &$obj ) );
     256
     257                $this->assertEquals( 0, did_filter( $tag1 ) );
     258                $this->assertEquals( 1, did_filter( $tag2 ) );
     259                $this->assertEquals( 2, did_filter( $tag3 ) );
     260        }
     261
     262        /**
     263         * @ticket 35357
     264         */
     265        function test_filter_functions_run() {
     266                $obj = new stdClass();
     267                $a = new MockAction();
     268                $b = new MockAction();
     269                $tag1 = rand_str();
     270                $tag2 = rand_str();
     271                $tag3 = rand_str();
     272
     273                add_action( $tag2, array( $a, 'filter_append' ), 10, 2 );
     274
     275                add_action( $tag3, array( $a, 'filter_append' ), 10, 2 );
     276                add_action( $tag3, array( $b, 'filter_append' ), 10, 2 );
     277
     278                $result1 = apply_filters_ref_array( $tag1, array('string', &$obj ) );
     279                $result2 = apply_filters_ref_array( $tag2, array('string', &$obj ) );
     280                $result3 = apply_filters_ref_array( $tag3, array('string', &$obj ) );
     281
     282                $this->assertEquals( 0, filter_functions_run( $tag1 ) );
     283                $this->assertEquals( 1, filter_functions_run( $tag2 ) );
     284                $this->assertEquals( 2, filter_functions_run( $tag3 ) );
     285        }
     286
     287        /**
    238288         * @ticket 12723
    239289         */
    240290        function test_filter_ref_array_result() {