Ticket #35357: 35357.3.patch
File 35357.3.patch, 4.9 KB (added by , 8 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; 201 206 202 207 $args = array(); 203 208 … … 233 238 if ( !is_null($the_['function']) ){ 234 239 $args[1] = $value; 235 240 $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 } 236 246 } 237 247 238 248 } while ( next($wp_filter[$tag]) !== false ); … … 252 262 * 253 263 * @global array $wp_filter Stores all of the filters 254 264 * @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. 256 267 * 257 268 * @param string $tag The name of the filter hook. 258 269 * @param array $args The arguments supplied to the functions hooked to $tag. … … 259 270 * @return mixed The filtered value after all hooked functions are applied to it. 260 271 */ 261 272 function 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; 263 274 264 275 // Do 'all' actions first 265 276 if ( isset($wp_filter['all']) ) { … … 287 298 288 299 do { 289 300 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 290 if ( !is_null($the_['function'] ) )301 if ( !is_null($the_['function'] ) ) { 291 302 $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 } 292 309 293 310 } while ( next($wp_filter[$tag]) !== false ); 294 311 … … 530 547 } 531 548 532 549 /** 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 */ 559 function 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 /** 533 570 * Retrieve the number of times an action is fired. 534 571 * 535 572 * @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 $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 /** 238 263 * @ticket 12723 239 264 */ 240 265 function test_filter_ref_array_result() {