Changeset 38571 for trunk/src/wp-includes/plugin.php
- Timestamp:
- 09/08/2016 03:54:13 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/plugin.php
r38282 r38571 23 23 24 24 // Initialize the filter globals. 25 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter; 26 27 if ( ! isset( $wp_filter ) ) 25 require( ABSPATH . WPINC . '/class-wp-hook.php' ); 26 27 /** @var WP_Hook[] $wp_filter */ 28 global $wp_filter, $wp_actions, $wp_current_filter; 29 30 if ( $wp_filter ) { 31 $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter ); 32 } else { 28 33 $wp_filter = array(); 34 } 29 35 30 36 if ( ! isset( $wp_actions ) ) 31 37 $wp_actions = array(); 32 33 if ( ! isset( $merged_filters ) )34 $merged_filters = array();35 38 36 39 if ( ! isset( $wp_current_filter ) ) … … 90 93 * 91 94 * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. 92 * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added,93 * it doesn't need to run through that process.94 95 * 95 96 * @param string $tag The name of the filter to hook the $function_to_add callback to. … … 104 105 */ 105 106 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { 106 global $wp_filter , $merged_filters;107 108 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);109 $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);110 unset( $merged_filters[ $tag ]);107 global $wp_filter; 108 if ( ! isset( $wp_filter[ $tag ] ) ) { 109 $wp_filter[ $tag ] = new WP_Hook(); 110 } 111 $wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args ); 111 112 return true; 112 113 } … … 129 130 */ 130 131 function has_filter($tag, $function_to_check = false) { 131 // Don't reset the internal array pointer 132 $wp_filter = $GLOBALS['wp_filter']; 133 134 $has = ! empty( $wp_filter[ $tag ] ); 135 136 // Make sure at least one priority has a filter callback 137 if ( $has ) { 138 $exists = false; 139 foreach ( $wp_filter[ $tag ] as $callbacks ) { 140 if ( ! empty( $callbacks ) ) { 141 $exists = true; 142 break; 143 } 144 } 145 146 if ( ! $exists ) { 147 $has = false; 148 } 149 } 150 151 if ( false === $function_to_check || false === $has ) 152 return $has; 153 154 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) ) 132 global $wp_filter; 133 134 if ( ! isset( $wp_filter[ $tag ] ) ) { 155 135 return false; 156 157 foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) { 158 if ( isset($wp_filter[$tag][$priority][$idx]) ) 159 return $priority; 160 } 161 162 return false; 136 } 137 138 return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check ); 163 139 } 164 140 … … 191 167 * 192 168 * @global array $wp_filter Stores all of the filters. 193 * @global array $merged_filters Merges the filter hooks using this function.194 169 * @global array $wp_current_filter Stores the list of current filters with the current one last. 195 170 * … … 200 175 */ 201 176 function apply_filters( $tag, $value ) { 202 global $wp_filter, $ merged_filters, $wp_current_filter;177 global $wp_filter, $wp_current_filter; 203 178 204 179 $args = array(); … … 220 195 $wp_current_filter[] = $tag; 221 196 222 // Sort.223 if ( !isset( $merged_filters[ $tag ] ) ) {224 ksort($wp_filter[$tag]);225 $merged_filters[ $tag ] = true;226 }227 228 reset( $wp_filter[ $tag ] );229 230 197 if ( empty($args) ) 231 198 $args = func_get_args(); 232 199 233 do { 234 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 235 if ( !is_null($the_['function']) ){ 236 $args[1] = $value; 237 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); 238 } 239 240 } while ( next($wp_filter[$tag]) !== false ); 200 // don't pass the tag name to WP_Hook 201 array_shift( $args ); 202 203 $filtered = $wp_filter[ $tag ]->apply_filters( $value, $args ); 241 204 242 205 array_pop( $wp_current_filter ); 243 206 244 return $ value;207 return $filtered; 245 208 } 246 209 … … 254 217 * 255 218 * @global array $wp_filter Stores all of the filters 256 * @global array $merged_filters Merges the filter hooks using this function.257 219 * @global array $wp_current_filter Stores the list of current filters with the current one last 258 220 * … … 262 224 */ 263 225 function apply_filters_ref_array($tag, $args) { 264 global $wp_filter, $ merged_filters, $wp_current_filter;226 global $wp_filter, $wp_current_filter; 265 227 266 228 // Do 'all' actions first … … 280 242 $wp_current_filter[] = $tag; 281 243 282 // Sort 283 if ( !isset( $merged_filters[ $tag ] ) ) { 284 ksort($wp_filter[$tag]); 285 $merged_filters[ $tag ] = true; 286 } 287 288 reset( $wp_filter[ $tag ] ); 289 290 do { 291 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 292 if ( !is_null($the_['function']) ) 293 $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 294 295 } while ( next($wp_filter[$tag]) !== false ); 244 $filtered = $wp_filter[ $tag ]->apply_filters( $args[0], $args ); 296 245 297 246 array_pop( $wp_current_filter ); 298 247 299 return $ args[0];248 return $filtered; 300 249 } 301 250 … … 314 263 * 315 264 * @global array $wp_filter Stores all of the filters 316 * @global array $merged_filters Merges the filter hooks using this function.317 265 * 318 266 * @param string $tag The filter hook to which the function to be removed is hooked. … … 322 270 */ 323 271 function remove_filter( $tag, $function_to_remove, $priority = 10 ) { 324 $function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority ); 325 326 $r = isset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] ); 327 328 if ( true === $r ) { 329 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] ); 330 if ( empty( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) ) { 331 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ] ); 272 global $wp_filter; 273 274 $r = false; 275 if ( isset( $wp_filter[ $tag ] ) ) { 276 $r = $wp_filter[ $tag ]->remove_filter( $tag, $function_to_remove, $priority ); 277 if ( ! $wp_filter[ $tag ]->callbacks ) { 278 unset( $wp_filter[ $tag ] ); 332 279 } 333 if ( empty( $GLOBALS['wp_filter'][ $tag ] ) ) {334 $GLOBALS['wp_filter'][ $tag ] = array();335 }336 unset( $GLOBALS['merged_filters'][ $tag ] );337 280 } 338 281 … … 345 288 * @since 2.7.0 346 289 * 347 * @global array $wp_filter Stores all of the filters 348 * @global array $merged_filters Merges the filter hooks using this function. 290 * @global array $wp_filter Stores all of the filters 349 291 * 350 292 * @param string $tag The filter to remove hooks from. … … 353 295 */ 354 296 function remove_all_filters( $tag, $priority = false ) { 355 global $wp_filter , $merged_filters;297 global $wp_filter; 356 298 357 299 if ( isset( $wp_filter[ $tag ]) ) { 358 if ( false === $priority ) { 359 $wp_filter[ $tag ] = array(); 360 } elseif ( isset( $wp_filter[ $tag ][ $priority ] ) ) { 361 $wp_filter[ $tag ][ $priority ] = array(); 300 $wp_filter[ $tag ]->remove_all_filters( $priority ); 301 if ( ! $wp_filter[ $tag ]->has_filters() ) { 302 unset( $wp_filter[ $tag ] ); 362 303 } 363 304 } 364 365 unset( $merged_filters[ $tag ] );366 305 367 306 return true; … … 474 413 * @global array $wp_filter Stores all of the filters 475 414 * @global array $wp_actions Increments the amount of times action was triggered. 476 * @global array $merged_filters Merges the filter hooks using this function.477 415 * @global array $wp_current_filter Stores the list of current filters with the current one last 478 416 * … … 482 420 */ 483 421 function do_action($tag, $arg = '') { 484 global $wp_filter, $wp_actions, $ merged_filters, $wp_current_filter;422 global $wp_filter, $wp_actions, $wp_current_filter; 485 423 486 424 if ( ! isset($wp_actions[$tag]) ) … … 513 451 $args[] = func_get_arg($a); 514 452 515 // Sort 516 if ( !isset( $merged_filters[ $tag ] ) ) { 517 ksort($wp_filter[$tag]); 518 $merged_filters[ $tag ] = true; 519 } 520 521 reset( $wp_filter[ $tag ] ); 522 523 do { 524 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 525 if ( !is_null($the_['function']) ) 526 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 527 528 } while ( next($wp_filter[$tag]) !== false ); 453 $wp_filter[ $tag ]->do_action( $args ); 529 454 530 455 array_pop($wp_current_filter); … … 559 484 * @global array $wp_filter Stores all of the filters 560 485 * @global array $wp_actions Increments the amount of times action was triggered. 561 * @global array $merged_filters Merges the filter hooks using this function.562 486 * @global array $wp_current_filter Stores the list of current filters with the current one last 563 487 * … … 566 490 */ 567 491 function do_action_ref_array($tag, $args) { 568 global $wp_filter, $wp_actions, $ merged_filters, $wp_current_filter;492 global $wp_filter, $wp_actions, $wp_current_filter; 569 493 570 494 if ( ! isset($wp_actions[$tag]) ) … … 589 513 $wp_current_filter[] = $tag; 590 514 591 // Sort 592 if ( !isset( $merged_filters[ $tag ] ) ) { 593 ksort($wp_filter[$tag]); 594 $merged_filters[ $tag ] = true; 595 } 596 597 reset( $wp_filter[ $tag ] ); 598 599 do { 600 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 601 if ( !is_null($the_['function']) ) 602 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 603 604 } while ( next($wp_filter[$tag]) !== false ); 515 $wp_filter[ $tag ]->do_action( $args ); 605 516 606 517 array_pop($wp_current_filter); … … 924 835 global $wp_filter; 925 836 926 reset( $wp_filter['all'] ); 927 do { 928 foreach ( (array) current($wp_filter['all']) as $the_ ) 929 if ( !is_null($the_['function']) ) 930 call_user_func_array($the_['function'], $args); 931 932 } while ( next($wp_filter['all']) !== false ); 837 $wp_filter['all']->do_all_hook( $args ); 933 838 } 934 839
Note: See TracChangeset
for help on using the changeset viewer.