[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Plugin API: WP_Hook class 4 * 5 * @package WordPress 6 * @subpackage Plugin 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used to implement action and filter hook functionality. 12 * 13 * @since 4.7.0 14 * 15 * @see Iterator 16 * @see ArrayAccess 17 */ 18 final class WP_Hook implements Iterator, ArrayAccess { 19 20 /** 21 * Hook callbacks. 22 * 23 * @since 4.7.0 24 * @var array 25 */ 26 public $callbacks = array(); 27 28 /** 29 * The priority keys of actively running iterations of a hook. 30 * 31 * @since 4.7.0 32 * @var array 33 */ 34 private $iterations = array(); 35 36 /** 37 * The current priority of actively running iterations of a hook. 38 * 39 * @since 4.7.0 40 * @var array 41 */ 42 private $current_priority = array(); 43 44 /** 45 * Number of levels this hook can be recursively called. 46 * 47 * @since 4.7.0 48 * @var int 49 */ 50 private $nesting_level = 0; 51 52 /** 53 * Flag for if we're currently doing an action, rather than a filter. 54 * 55 * @since 4.7.0 56 * @var bool 57 */ 58 private $doing_action = false; 59 60 /** 61 * Adds a callback function to a filter hook. 62 * 63 * @since 4.7.0 64 * 65 * @param string $hook_name The name of the filter to add the callback to. 66 * @param callable $callback The callback to be run when the filter is applied. 67 * @param int $priority The order in which the functions associated with a particular filter 68 * are executed. Lower numbers correspond with earlier execution, 69 * and functions with the same priority are executed in the order 70 * in which they were added to the filter. 71 * @param int $accepted_args The number of arguments the function accepts. 72 */ 73 public function add_filter( $hook_name, $callback, $priority, $accepted_args ) { 74 $idx = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); 75 76 $priority_existed = isset( $this->callbacks[ $priority ] ); 77 78 $this->callbacks[ $priority ][ $idx ] = array( 79 'function' => $callback, 80 'accepted_args' => $accepted_args, 81 ); 82 83 // If we're adding a new priority to the list, put them back in sorted order. 84 if ( ! $priority_existed && count( $this->callbacks ) > 1 ) { 85 ksort( $this->callbacks, SORT_NUMERIC ); 86 } 87 88 if ( $this->nesting_level > 0 ) { 89 $this->resort_active_iterations( $priority, $priority_existed ); 90 } 91 } 92 93 /** 94 * Handles resetting callback priority keys mid-iteration. 95 * 96 * @since 4.7.0 97 * 98 * @param false|int $new_priority Optional. The priority of the new filter being added. Default false, 99 * for no priority being added. 100 * @param bool $priority_existed Optional. Flag for whether the priority already existed before the new 101 * filter was added. Default false. 102 */ 103 private function resort_active_iterations( $new_priority = false, $priority_existed = false ) { 104 $new_priorities = array_keys( $this->callbacks ); 105 106 // If there are no remaining hooks, clear out all running iterations. 107 if ( ! $new_priorities ) { 108 foreach ( $this->iterations as $index => $iteration ) { 109 $this->iterations[ $index ] = $new_priorities; 110 } 111 112 return; 113 } 114 115 $min = min( $new_priorities ); 116 117 foreach ( $this->iterations as $index => &$iteration ) { 118 $current = current( $iteration ); 119 120 // If we're already at the end of this iteration, just leave the array pointer where it is. 121 if ( false === $current ) { 122 continue; 123 } 124 125 $iteration = $new_priorities; 126 127 if ( $current < $min ) { 128 array_unshift( $iteration, $current ); 129 continue; 130 } 131 132 while ( current( $iteration ) < $current ) { 133 if ( false === next( $iteration ) ) { 134 break; 135 } 136 } 137 138 // If we have a new priority that didn't exist, but ::apply_filters() or ::do_action() thinks it's the current priority... 139 if ( $new_priority === $this->current_priority[ $index ] && ! $priority_existed ) { 140 /* 141 * ...and the new priority is the same as what $this->iterations thinks is the previous 142 * priority, we need to move back to it. 143 */ 144 145 if ( false === current( $iteration ) ) { 146 // If we've already moved off the end of the array, go back to the last element. 147 $prev = end( $iteration ); 148 } else { 149 // Otherwise, just go back to the previous element. 150 $prev = prev( $iteration ); 151 } 152 153 if ( false === $prev ) { 154 // Start of the array. Reset, and go about our day. 155 reset( $iteration ); 156 } elseif ( $new_priority !== $prev ) { 157 // Previous wasn't the same. Move forward again. 158 next( $iteration ); 159 } 160 } 161 } 162 163 unset( $iteration ); 164 } 165 166 /** 167 * Removes a callback function from a filter hook. 168 * 169 * @since 4.7.0 170 * 171 * @param string $hook_name The filter hook to which the function to be removed is hooked. 172 * @param callable|string|array $callback The callback to be removed from running when the filter is applied. 173 * This method can be called unconditionally to speculatively remove 174 * a callback that may or may not exist. 175 * @param int $priority The exact priority used when adding the original filter callback. 176 * @return bool Whether the callback existed before it was removed. 177 */ 178 public function remove_filter( $hook_name, $callback, $priority ) { 179 $function_key = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); 180 181 $exists = isset( $this->callbacks[ $priority ][ $function_key ] ); 182 183 if ( $exists ) { 184 unset( $this->callbacks[ $priority ][ $function_key ] ); 185 186 if ( ! $this->callbacks[ $priority ] ) { 187 unset( $this->callbacks[ $priority ] ); 188 189 if ( $this->nesting_level > 0 ) { 190 $this->resort_active_iterations(); 191 } 192 } 193 } 194 195 return $exists; 196 } 197 198 /** 199 * Checks if a specific callback has been registered for this hook. 200 * 201 * When using the `$callback` argument, this function may return a non-boolean value 202 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. 203 * 204 * @since 4.7.0 205 * 206 * @param string $hook_name Optional. The name of the filter hook. Default empty. 207 * @param callable|string|array|false $callback Optional. The callback to check for. 208 * This method can be called unconditionally to speculatively check 209 * a callback that may or may not exist. Default false. 210 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has 211 * anything registered. When checking a specific function, the priority 212 * of that hook is returned, or false if the function is not attached. 213 */ 214 public function has_filter( $hook_name = '', $callback = false ) { 215 if ( false === $callback ) { 216 return $this->has_filters(); 217 } 218 219 $function_key = _wp_filter_build_unique_id( $hook_name, $callback, false ); 220 221 if ( ! $function_key ) { 222 return false; 223 } 224 225 foreach ( $this->callbacks as $priority => $callbacks ) { 226 if ( isset( $callbacks[ $function_key ] ) ) { 227 return $priority; 228 } 229 } 230 231 return false; 232 } 233 234 /** 235 * Checks if any callbacks have been registered for this hook. 236 * 237 * @since 4.7.0 238 * 239 * @return bool True if callbacks have been registered for the current hook, otherwise false. 240 */ 241 public function has_filters() { 242 foreach ( $this->callbacks as $callbacks ) { 243 if ( $callbacks ) { 244 return true; 245 } 246 } 247 248 return false; 249 } 250 251 /** 252 * Removes all callbacks from the current filter. 253 * 254 * @since 4.7.0 255 * 256 * @param int|false $priority Optional. The priority number to remove. Default false. 257 */ 258 public function remove_all_filters( $priority = false ) { 259 if ( ! $this->callbacks ) { 260 return; 261 } 262 263 if ( false === $priority ) { 264 $this->callbacks = array(); 265 } elseif ( isset( $this->callbacks[ $priority ] ) ) { 266 unset( $this->callbacks[ $priority ] ); 267 } 268 269 if ( $this->nesting_level > 0 ) { 270 $this->resort_active_iterations(); 271 } 272 } 273 274 /** 275 * Calls the callback functions that have been added to a filter hook. 276 * 277 * @since 4.7.0 278 * 279 * @param mixed $value The value to filter. 280 * @param array $args Additional parameters to pass to the callback functions. 281 * This array is expected to include $value at index 0. 282 * @return mixed The filtered value after all hooked functions are applied to it. 283 */ 284 public function apply_filters( $value, $args ) { 285 if ( ! $this->callbacks ) { 286 return $value; 287 } 288 289 $nesting_level = $this->nesting_level++; 290 291 $this->iterations[ $nesting_level ] = array_keys( $this->callbacks ); 292 $num_args = count( $args ); 293 294 do { 295 $this->current_priority[ $nesting_level ] = current( $this->iterations[ $nesting_level ] ); 296 $priority = $this->current_priority[ $nesting_level ]; 297 298 foreach ( $this->callbacks[ $priority ] as $the_ ) { 299 if ( ! $this->doing_action ) { 300 $args[0] = $value; 301 } 302 303 // Avoid the array_slice() if possible. 304 if ( 0 == $the_['accepted_args'] ) { 305 $value = call_user_func( $the_['function'] ); 306 } elseif ( $the_['accepted_args'] >= $num_args ) { 307 $value = call_user_func_array( $the_['function'], $args ); 308 } else { 309 $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) ); 310 } 311 } 312 } while ( false !== next( $this->iterations[ $nesting_level ] ) ); 313 314 unset( $this->iterations[ $nesting_level ] ); 315 unset( $this->current_priority[ $nesting_level ] ); 316 317 $this->nesting_level--; 318 319 return $value; 320 } 321 322 /** 323 * Calls the callback functions that have been added to an action hook. 324 * 325 * @since 4.7.0 326 * 327 * @param array $args Parameters to pass to the callback functions. 328 */ 329 public function do_action( $args ) { 330 $this->doing_action = true; 331 $this->apply_filters( '', $args ); 332 333 // If there are recursive calls to the current action, we haven't finished it until we get to the last one. 334 if ( ! $this->nesting_level ) { 335 $this->doing_action = false; 336 } 337 } 338 339 /** 340 * Processes the functions hooked into the 'all' hook. 341 * 342 * @since 4.7.0 343 * 344 * @param array $args Arguments to pass to the hook callbacks. Passed by reference. 345 */ 346 public function do_all_hook( &$args ) { 347 $nesting_level = $this->nesting_level++; 348 $this->iterations[ $nesting_level ] = array_keys( $this->callbacks ); 349 350 do { 351 $priority = current( $this->iterations[ $nesting_level ] ); 352 353 foreach ( $this->callbacks[ $priority ] as $the_ ) { 354 call_user_func_array( $the_['function'], $args ); 355 } 356 } while ( false !== next( $this->iterations[ $nesting_level ] ) ); 357 358 unset( $this->iterations[ $nesting_level ] ); 359 $this->nesting_level--; 360 } 361 362 /** 363 * Return the current priority level of the currently running iteration of the hook. 364 * 365 * @since 4.7.0 366 * 367 * @return int|false If the hook is running, return the current priority level. 368 * If it isn't running, return false. 369 */ 370 public function current_priority() { 371 if ( false === current( $this->iterations ) ) { 372 return false; 373 } 374 375 return current( current( $this->iterations ) ); 376 } 377 378 /** 379 * Normalizes filters set up before WordPress has initialized to WP_Hook objects. 380 * 381 * The `$filters` parameter should be an array keyed by hook name, with values 382 * containing either: 383 * 384 * - A `WP_Hook` instance 385 * - An array of callbacks keyed by their priorities 386 * 387 * Examples: 388 * 389 * $filters = array( 390 * 'wp_fatal_error_handler_enabled' => array( 391 * 10 => array( 392 * array( 393 * 'accepted_args' => 0, 394 * 'function' => function() { 395 * return false; 396 * }, 397 * ), 398 * ), 399 * ), 400 * ); 401 * 402 * @since 4.7.0 403 * 404 * @param array $filters Filters to normalize. See documentation above for details. 405 * @return WP_Hook[] Array of normalized filters. 406 */ 407 public static function build_preinitialized_hooks( $filters ) { 408 /** @var WP_Hook[] $normalized */ 409 $normalized = array(); 410 411 foreach ( $filters as $hook_name => $callback_groups ) { 412 if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) { 413 $normalized[ $hook_name ] = $callback_groups; 414 continue; 415 } 416 417 $hook = new WP_Hook(); 418 419 // Loop through callback groups. 420 foreach ( $callback_groups as $priority => $callbacks ) { 421 422 // Loop through callbacks. 423 foreach ( $callbacks as $cb ) { 424 $hook->add_filter( $hook_name, $cb['function'], $priority, $cb['accepted_args'] ); 425 } 426 } 427 428 $normalized[ $hook_name ] = $hook; 429 } 430 431 return $normalized; 432 } 433 434 /** 435 * Determines whether an offset value exists. 436 * 437 * @since 4.7.0 438 * 439 * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php 440 * 441 * @param mixed $offset An offset to check for. 442 * @return bool True if the offset exists, false otherwise. 443 */ 444 #[ReturnTypeWillChange] 445 public function offsetExists( $offset ) { 446 return isset( $this->callbacks[ $offset ] ); 447 } 448 449 /** 450 * Retrieves a value at a specified offset. 451 * 452 * @since 4.7.0 453 * 454 * @link https://www.php.net/manual/en/arrayaccess.offsetget.php 455 * 456 * @param mixed $offset The offset to retrieve. 457 * @return mixed If set, the value at the specified offset, null otherwise. 458 */ 459 #[ReturnTypeWillChange] 460 public function offsetGet( $offset ) { 461 return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null; 462 } 463 464 /** 465 * Sets a value at a specified offset. 466 * 467 * @since 4.7.0 468 * 469 * @link https://www.php.net/manual/en/arrayaccess.offsetset.php 470 * 471 * @param mixed $offset The offset to assign the value to. 472 * @param mixed $value The value to set. 473 */ 474 #[ReturnTypeWillChange] 475 public function offsetSet( $offset, $value ) { 476 if ( is_null( $offset ) ) { 477 $this->callbacks[] = $value; 478 } else { 479 $this->callbacks[ $offset ] = $value; 480 } 481 } 482 483 /** 484 * Unsets a specified offset. 485 * 486 * @since 4.7.0 487 * 488 * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php 489 * 490 * @param mixed $offset The offset to unset. 491 */ 492 #[ReturnTypeWillChange] 493 public function offsetUnset( $offset ) { 494 unset( $this->callbacks[ $offset ] ); 495 } 496 497 /** 498 * Returns the current element. 499 * 500 * @since 4.7.0 501 * 502 * @link https://www.php.net/manual/en/iterator.current.php 503 * 504 * @return array Of callbacks at current priority. 505 */ 506 #[ReturnTypeWillChange] 507 public function current() { 508 return current( $this->callbacks ); 509 } 510 511 /** 512 * Moves forward to the next element. 513 * 514 * @since 4.7.0 515 * 516 * @link https://www.php.net/manual/en/iterator.next.php 517 * 518 * @return array Of callbacks at next priority. 519 */ 520 #[ReturnTypeWillChange] 521 public function next() { 522 return next( $this->callbacks ); 523 } 524 525 /** 526 * Returns the key of the current element. 527 * 528 * @since 4.7.0 529 * 530 * @link https://www.php.net/manual/en/iterator.key.php 531 * 532 * @return mixed Returns current priority on success, or NULL on failure 533 */ 534 #[ReturnTypeWillChange] 535 public function key() { 536 return key( $this->callbacks ); 537 } 538 539 /** 540 * Checks if current position is valid. 541 * 542 * @since 4.7.0 543 * 544 * @link https://www.php.net/manual/en/iterator.valid.php 545 * 546 * @return bool Whether the current position is valid. 547 */ 548 #[ReturnTypeWillChange] 549 public function valid() { 550 return key( $this->callbacks ) !== null; 551 } 552 553 /** 554 * Rewinds the Iterator to the first element. 555 * 556 * @since 4.7.0 557 * 558 * @link https://www.php.net/manual/en/iterator.rewind.php 559 */ 560 #[ReturnTypeWillChange] 561 public function rewind() { 562 reset( $this->callbacks ); 563 } 564 565 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |