WordPress.org

Make WordPress Core

Ticket #35357: 35357.2.patch

File 35357.2.patch, 6.6 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;
    201206
    202207        $args = array();
    203208
     
    233238                        if ( !is_null($the_['function']) ){
    234239                                $args[1] = $value;
    235240                                $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
     241                                if ( empty( $wp_done_filters[ $tag ] ) ){
     242                                        $wp_done_filters[ $tag ] = 1;
     243                                } else {
     244                                        $wp_done_filters[ $tag ] = $wp_done_filters[ $tag ] + 1;
     245                                }
    236246                        }
    237247
    238248        } while ( next($wp_filter[$tag]) !== false );
     
    252262 *
    253263 * @global array $wp_filter         Stores all of the filters
    254264 * @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
     265 * @global array $wp_current_filter Stores the list of current filters with the current one last.
     266 * @global array $wp_done_filters   Stores the number of times filters have been run.
    256267 *
    257268 * @param string $tag  The name of the filter hook.
    258269 * @param array  $args The arguments supplied to the functions hooked to $tag.
     
    259270 * @return mixed The filtered value after all hooked functions are applied to it.
    260271 */
    261272function apply_filters_ref_array($tag, $args) {
    262         global $wp_filter, $merged_filters, $wp_current_filter;
     273        global $wp_filter, $merged_filters, $wp_current_filter, $wp_done_filters;
    263274
    264275        // Do 'all' actions first
    265276        if ( isset($wp_filter['all']) ) {
     
    287298
    288299        do {
    289300                foreach ( (array) current($wp_filter[$tag]) as $the_ )
    290                         if ( !is_null($the_['function']) )
     301                        if ( !is_null($the_['function'] ) ) {
    291302                                $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     303                                if ( empty( $wp_done_filters[ $tag ] ) ){
     304                                        $wp_done_filters[ $tag ] = 1;
     305                                } else {
     306                                        $wp_done_filters[ $tag ] = $wp_done_filters[ $tag ] + 1;
     307                                }
     308                        }
    292309
    293310        } while ( next($wp_filter[$tag]) !== false );
    294311
     
    530547}
    531548
    532549/**
     550 * Retrieve the number of times a filter is fired.
     551 *
     552 * @since 4.5.0
     553 *
     554 * @global array $wp_done_filters Increments the amount of times filter was triggered.
     555 *
     556 * @param string $tag The name of the filter hook.
     557 * @return int The number of times filter hook $tag is fired.
     558 */
     559function did_filter($tag) {
     560        global $wp_done_filters;
     561
     562        if ( ! isset( $wp_done_filters[ $tag ] ) ) {
     563                return 0;
     564        }
     565
     566        return $wp_done_filters[$tag];
     567}
     568
     569/**
    533570 * Retrieve the number of times an action is fired.
    534571 *
    535572 * @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                $tag1 = rand_str();
     245                $tag2 = rand_str();
     246                $tag3 = rand_str();
     247
     248                add_action($tag2, array($a, 'filter_append'), 10, 2);
     249
     250                add_action($tag3, array($a, 'filter_append'), 10, 2);
     251                add_action($tag3, array($b, 'filter_append'), 10, 2);
     252
     253                $result1 = apply_filters_ref_array($tag1, array('string', &$obj));
     254                $result2 = apply_filters_ref_array($tag2, array('string', &$obj));
     255                $result3 = apply_filters_ref_array($tag3, array('string', &$obj));
     256
     257                $this->assertEquals( did_filter( $tag1 ) , 0 );
     258                $this->assertEquals( did_filter( $tag2 ) , 1 );
     259                $this->assertEquals( did_filter( $tag3 ) , 2 );
     260        }
     261
     262        /**
    238263         * @ticket 12723
    239264         */
    240265        function test_filter_ref_array_result() {
     
    299324         * @ticket 29070
    300325         */
    301326         function test_has_filter_doesnt_reset_wp_filter() {
    302                 add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 1 );
    303                 add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 2 );
    304                 add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 3 );
    305                 add_action( 'action_test_has_filter_doesnt_reset_wp_filter', array( $this, '_action_test_has_filter_doesnt_reset_wp_filter' ), 4 );
     327                add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 1 );
     328                add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 2 );
     329                add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 3 );
     330                add_action( 'action_test_has_filter_doesnt_reset_wp_filter', array( $this, '_action_test_has_filter_doesnt_reset_wp_filter' ), 4 );
    306331
    307                 do_action( 'action_test_has_filter_doesnt_reset_wp_filter' );
     332                do_action( 'action_test_has_filter_doesnt_reset_wp_filter' );
    308333         }
    309334         function _action_test_has_filter_doesnt_reset_wp_filter() {
    310                 global $wp_filter;
     335                global $wp_filter;
    311336
    312                 has_action( 'action_test_has_filter_doesnt_reset_wp_filter', '_function_that_doesnt_exist' );
     337                has_action( 'action_test_has_filter_doesnt_reset_wp_filter', '_function_that_doesnt_exist' );
    313338
    314339                $filters = current( $wp_filter['action_test_has_filter_doesnt_reset_wp_filter'] );
    315                 $the_ = current( $filters );
    316                 $this->assertEquals( $the_['function'], array( $this, '_action_test_has_filter_doesnt_reset_wp_filter' ) );
     340                $the_ = current( $filters );
     341                $this->assertEquals( $the_['function'], array( $this, '_action_test_has_filter_doesnt_reset_wp_filter' ) );
    317342         }
    318343}