Ticket #35357: 35357.4.patch
| File 35357.4.patch, 6.4 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/plugin.php
20 20 */ 21 21 22 22 // Initialize the filter globals. 23 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter ;23 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter, $wp_done_filters; 24 24 25 25 if ( ! isset( $wp_filter ) ) 26 26 $wp_filter = array(); … … 34 34 if ( ! isset( $wp_current_filter ) ) 35 35 $wp_current_filter = array(); 36 36 37 if ( ! isset( $wp_done_filters ) ) { 38 $wp_done_filters = array(); 39 } 40 37 41 /** 38 42 * Hook a function or method to a specific filter action. 39 43 * … … 190 194 * @global array $wp_filter Stores all of the filters. 191 195 * @global array $merged_filters Merges the filter hooks using this function. 192 196 * @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. 193 198 * 194 199 * @param string $tag The name of the filter hook. 195 200 * @param mixed $value The value on which the filters hooked to `$tag` are applied on. … … 197 202 * @return mixed The filtered value after all hooked functions are applied to it. 198 203 */ 199 204 function 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; 201 206 207 if ( ! isset( $wp_actions[ $tag ] ) ) { 208 $wp_actions[$tag] = 1; 209 } else { 210 ++$wp_actions[ $tag] ; 211 } 212 202 213 $args = array(); 203 214 204 215 // Do 'all' actions first. … … 233 244 if ( !is_null($the_['function']) ){ 234 245 $args[1] = $value; 235 246 $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 } 236 252 } 237 253 238 254 } while ( next($wp_filter[$tag]) !== false ); … … 252 268 * 253 269 * @global array $wp_filter Stores all of the filters 254 270 * @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. 256 273 * 257 274 * @param string $tag The name of the filter hook. 258 275 * @param array $args The arguments supplied to the functions hooked to $tag. … … 259 276 * @return mixed The filtered value after all hooked functions are applied to it. 260 277 */ 261 278 function 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; 263 280 281 if ( ! isset( $wp_actions[ $tag ] ) ) { 282 $wp_actions[$tag] = 1; 283 } else { 284 ++$wp_actions[ $tag] ; 285 } 286 264 287 // Do 'all' actions first 265 288 if ( isset($wp_filter['all']) ) { 266 289 $wp_current_filter[] = $tag; … … 287 310 288 311 do { 289 312 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 290 if ( !is_null($the_['function'] ) )313 if ( !is_null($the_['function'] ) ) { 291 314 $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 } 292 321 293 322 } while ( next($wp_filter[$tag]) !== false ); 294 323 … … 530 559 } 531 560 532 561 /** 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 */ 571 function 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 */ 593 function did_filter($tag) { 594 return did_action($tag); 595 } 596 597 /** 533 598 * Retrieve the number of times an action is fired. 534 599 * 535 600 * @since 2.1.0 -
tests/phpunit/tests/filters.php
235 235 } 236 236 237 237 /** 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 /** 238 288 * @ticket 12723 239 289 */ 240 290 function test_filter_ref_array_result() {