[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> class-wp-hook.php (source)

   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 current 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       * Hooks a function or method to a specific filter action.
  62       *
  63       * @since 4.7.0
  64       *
  65       * @param string   $tag             The name of the filter to hook the $function_to_add callback to.
  66       * @param callable $function_to_add The callback to be run when the filter is applied.
  67       * @param int      $priority        The order in which the functions associated with a
  68       *                                  particular action are executed. Lower numbers correspond with
  69       *                                  earlier execution, and functions with the same priority are executed
  70       *                                  in the order in which they were added to the action.
  71       * @param int      $accepted_args   The number of arguments the function accepts.
  72       */
  73  	public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) {
  74          $idx              = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
  75          $priority_existed = isset( $this->callbacks[ $priority ] );
  76  
  77          $this->callbacks[ $priority ][ $idx ] = array(
  78              'function'      => $function_to_add,
  79              'accepted_args' => $accepted_args,
  80          );
  81  
  82          // if we're adding a new priority to the list, put them back in sorted order
  83          if ( ! $priority_existed && count( $this->callbacks ) > 1 ) {
  84              ksort( $this->callbacks, SORT_NUMERIC );
  85          }
  86  
  87          if ( $this->nesting_level > 0 ) {
  88              $this->resort_active_iterations( $priority, $priority_existed );
  89          }
  90      }
  91  
  92      /**
  93       * Handles resetting callback priority keys mid-iteration.
  94       *
  95       * @since 4.7.0
  96       *
  97       * @param bool|int $new_priority     Optional. The priority of the new filter being added. Default false,
  98       *                                   for no priority being added.
  99       * @param bool     $priority_existed Optional. Flag for whether the priority already existed before the new
 100       *                                   filter was added. Default false.
 101       */
 102  	private function resort_active_iterations( $new_priority = false, $priority_existed = false ) {
 103          $new_priorities = array_keys( $this->callbacks );
 104  
 105          // If there are no remaining hooks, clear out all running iterations.
 106          if ( ! $new_priorities ) {
 107              foreach ( $this->iterations as $index => $iteration ) {
 108                  $this->iterations[ $index ] = $new_priorities;
 109              }
 110              return;
 111          }
 112  
 113          $min = min( $new_priorities );
 114          foreach ( $this->iterations as $index => &$iteration ) {
 115              $current = current( $iteration );
 116              // If we're already at the end of this iteration, just leave the array pointer where it is.
 117              if ( false === $current ) {
 118                  continue;
 119              }
 120  
 121              $iteration = $new_priorities;
 122  
 123              if ( $current < $min ) {
 124                  array_unshift( $iteration, $current );
 125                  continue;
 126              }
 127  
 128              while ( current( $iteration ) < $current ) {
 129                  if ( false === next( $iteration ) ) {
 130                      break;
 131                  }
 132              }
 133  
 134              // If we have a new priority that didn't exist, but ::apply_filters() or ::do_action() thinks it's the current priority...
 135              if ( $new_priority === $this->current_priority[ $index ] && ! $priority_existed ) {
 136                  /*
 137                   * ... and the new priority is the same as what $this->iterations thinks is the previous
 138                   * priority, we need to move back to it.
 139                   */
 140  
 141                  if ( false === current( $iteration ) ) {
 142                      // If we've already moved off the end of the array, go back to the last element.
 143                      $prev = end( $iteration );
 144                  } else {
 145                      // Otherwise, just go back to the previous element.
 146                      $prev = prev( $iteration );
 147                  }
 148                  if ( false === $prev ) {
 149                      // Start of the array. Reset, and go about our day.
 150                      reset( $iteration );
 151                  } elseif ( $new_priority !== $prev ) {
 152                      // Previous wasn't the same. Move forward again.
 153                      next( $iteration );
 154                  }
 155              }
 156          }
 157          unset( $iteration );
 158      }
 159  
 160      /**
 161       * Unhooks a function or method from a specific filter action.
 162       *
 163       * @since 4.7.0
 164       *
 165       * @param string   $tag                The filter hook to which the function to be removed is hooked. Used
 166       *                                     for building the callback ID when SPL is not available.
 167       * @param callable $function_to_remove The callback to be removed from running when the filter is applied.
 168       * @param int      $priority           The exact priority used when adding the original filter callback.
 169       * @return bool Whether the callback existed before it was removed.
 170       */
 171  	public function remove_filter( $tag, $function_to_remove, $priority ) {
 172          $function_key = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
 173  
 174          $exists = isset( $this->callbacks[ $priority ][ $function_key ] );
 175          if ( $exists ) {
 176              unset( $this->callbacks[ $priority ][ $function_key ] );
 177              if ( ! $this->callbacks[ $priority ] ) {
 178                  unset( $this->callbacks[ $priority ] );
 179                  if ( $this->nesting_level > 0 ) {
 180                      $this->resort_active_iterations();
 181                  }
 182              }
 183          }
 184          return $exists;
 185      }
 186  
 187      /**
 188       * Checks if a specific action has been registered for this hook.
 189       *
 190       * @since 4.7.0
 191       *
 192       * @param string        $tag               Optional. The name of the filter hook. Used for building
 193       *                                         the callback ID when SPL is not available. Default empty.
 194       * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
 195       * @return bool|int The priority of that hook is returned, or false if the function is not attached.
 196       */
 197  	public function has_filter( $tag = '', $function_to_check = false ) {
 198          if ( false === $function_to_check ) {
 199              return $this->has_filters();
 200          }
 201  
 202          $function_key = _wp_filter_build_unique_id( $tag, $function_to_check, false );
 203          if ( ! $function_key ) {
 204              return false;
 205          }
 206  
 207          foreach ( $this->callbacks as $priority => $callbacks ) {
 208              if ( isset( $callbacks[ $function_key ] ) ) {
 209                  return $priority;
 210              }
 211          }
 212  
 213          return false;
 214      }
 215  
 216      /**
 217       * Checks if any callbacks have been registered for this hook.
 218       *
 219       * @since 4.7.0
 220       *
 221       * @return bool True if callbacks have been registered for the current hook, otherwise false.
 222       */
 223  	public function has_filters() {
 224          foreach ( $this->callbacks as $callbacks ) {
 225              if ( $callbacks ) {
 226                  return true;
 227              }
 228          }
 229          return false;
 230      }
 231  
 232      /**
 233       * Removes all callbacks from the current filter.
 234       *
 235       * @since 4.7.0
 236       *
 237       * @param int|bool $priority Optional. The priority number to remove. Default false.
 238       */
 239  	public function remove_all_filters( $priority = false ) {
 240          if ( ! $this->callbacks ) {
 241              return;
 242          }
 243  
 244          if ( false === $priority ) {
 245              $this->callbacks = array();
 246          } elseif ( isset( $this->callbacks[ $priority ] ) ) {
 247              unset( $this->callbacks[ $priority ] );
 248          }
 249  
 250          if ( $this->nesting_level > 0 ) {
 251              $this->resort_active_iterations();
 252          }
 253      }
 254  
 255      /**
 256       * Calls the callback functions that have been added to a filter hook.
 257       *
 258       * @since 4.7.0
 259       *
 260       * @param mixed $value The value to filter.
 261       * @param array $args  Additional parameters to pass to the callback functions.
 262       *                     This array is expected to include $value at index 0.
 263       * @return mixed The filtered value after all hooked functions are applied to it.
 264       */
 265  	public function apply_filters( $value, $args ) {
 266          if ( ! $this->callbacks ) {
 267              return $value;
 268          }
 269  
 270          $nesting_level = $this->nesting_level++;
 271  
 272          $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
 273          $num_args                           = count( $args );
 274  
 275          do {
 276              $this->current_priority[ $nesting_level ] = current( $this->iterations[ $nesting_level ] );
 277              $priority                                 = $this->current_priority[ $nesting_level ];
 278  
 279              foreach ( $this->callbacks[ $priority ] as $the_ ) {
 280                  if ( ! $this->doing_action ) {
 281                      $args[0] = $value;
 282                  }
 283  
 284                  // Avoid the array_slice if possible.
 285                  if ( $the_['accepted_args'] == 0 ) {
 286                      $value = call_user_func( $the_['function'] );
 287                  } elseif ( $the_['accepted_args'] >= $num_args ) {
 288                      $value = call_user_func_array( $the_['function'], $args );
 289                  } else {
 290                      $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
 291                  }
 292              }
 293          } while ( false !== next( $this->iterations[ $nesting_level ] ) );
 294  
 295          unset( $this->iterations[ $nesting_level ] );
 296          unset( $this->current_priority[ $nesting_level ] );
 297  
 298          $this->nesting_level--;
 299  
 300          return $value;
 301      }
 302  
 303      /**
 304       * Calls the callback functions that have been added to an action hook.
 305       *
 306       * @since 4.7.0
 307       *
 308       * @param array $args Parameters to pass to the callback functions.
 309       */
 310  	public function do_action( $args ) {
 311          $this->doing_action = true;
 312          $this->apply_filters( '', $args );
 313  
 314          // If there are recursive calls to the current action, we haven't finished it until we get to the last one.
 315          if ( ! $this->nesting_level ) {
 316              $this->doing_action = false;
 317          }
 318      }
 319  
 320      /**
 321       * Processes the functions hooked into the 'all' hook.
 322       *
 323       * @since 4.7.0
 324       *
 325       * @param array $args Arguments to pass to the hook callbacks. Passed by reference.
 326       */
 327  	public function do_all_hook( &$args ) {
 328          $nesting_level                      = $this->nesting_level++;
 329          $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
 330  
 331          do {
 332              $priority = current( $this->iterations[ $nesting_level ] );
 333              foreach ( $this->callbacks[ $priority ] as $the_ ) {
 334                  call_user_func_array( $the_['function'], $args );
 335              }
 336          } while ( false !== next( $this->iterations[ $nesting_level ] ) );
 337  
 338          unset( $this->iterations[ $nesting_level ] );
 339          $this->nesting_level--;
 340      }
 341  
 342      /**
 343       * Return the current priority level of the currently running iteration of the hook.
 344       *
 345       * @since 4.7.0
 346       *
 347       * @return int|false If the hook is running, return the current priority level. If it isn't running, return false.
 348       */
 349  	public function current_priority() {
 350          if ( false === current( $this->iterations ) ) {
 351              return false;
 352          }
 353  
 354          return current( current( $this->iterations ) );
 355      }
 356  
 357      /**
 358       * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
 359       *
 360       * @since 4.7.0
 361       *
 362       * @param array $filters Filters to normalize.
 363       * @return WP_Hook[] Array of normalized filters.
 364       */
 365  	public static function build_preinitialized_hooks( $filters ) {
 366          /** @var WP_Hook[] $normalized */
 367          $normalized = array();
 368  
 369          foreach ( $filters as $tag => $callback_groups ) {
 370              if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
 371                  $normalized[ $tag ] = $callback_groups;
 372                  continue;
 373              }
 374              $hook = new WP_Hook();
 375  
 376              // Loop through callback groups.
 377              foreach ( $callback_groups as $priority => $callbacks ) {
 378  
 379                  // Loop through callbacks.
 380                  foreach ( $callbacks as $cb ) {
 381                      $hook->add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] );
 382                  }
 383              }
 384              $normalized[ $tag ] = $hook;
 385          }
 386          return $normalized;
 387      }
 388  
 389      /**
 390       * Determines whether an offset value exists.
 391       *
 392       * @since 4.7.0
 393       *
 394       * @link https://secure.php.net/manual/en/arrayaccess.offsetexists.php
 395       *
 396       * @param mixed $offset An offset to check for.
 397       * @return bool True if the offset exists, false otherwise.
 398       */
 399  	public function offsetExists( $offset ) {
 400          return isset( $this->callbacks[ $offset ] );
 401      }
 402  
 403      /**
 404       * Retrieves a value at a specified offset.
 405       *
 406       * @since 4.7.0
 407       *
 408       * @link https://secure.php.net/manual/en/arrayaccess.offsetget.php
 409       *
 410       * @param mixed $offset The offset to retrieve.
 411       * @return mixed If set, the value at the specified offset, null otherwise.
 412       */
 413  	public function offsetGet( $offset ) {
 414          return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null;
 415      }
 416  
 417      /**
 418       * Sets a value at a specified offset.
 419       *
 420       * @since 4.7.0
 421       *
 422       * @link https://secure.php.net/manual/en/arrayaccess.offsetset.php
 423       *
 424       * @param mixed $offset The offset to assign the value to.
 425       * @param mixed $value The value to set.
 426       */
 427  	public function offsetSet( $offset, $value ) {
 428          if ( is_null( $offset ) ) {
 429              $this->callbacks[] = $value;
 430          } else {
 431              $this->callbacks[ $offset ] = $value;
 432          }
 433      }
 434  
 435      /**
 436       * Unsets a specified offset.
 437       *
 438       * @since 4.7.0
 439       *
 440       * @link https://secure.php.net/manual/en/arrayaccess.offsetunset.php
 441       *
 442       * @param mixed $offset The offset to unset.
 443       */
 444  	public function offsetUnset( $offset ) {
 445          unset( $this->callbacks[ $offset ] );
 446      }
 447  
 448      /**
 449       * Returns the current element.
 450       *
 451       * @since 4.7.0
 452       *
 453       * @link https://secure.php.net/manual/en/iterator.current.php
 454       *
 455       * @return array Of callbacks at current priority.
 456       */
 457  	public function current() {
 458          return current( $this->callbacks );
 459      }
 460  
 461      /**
 462       * Moves forward to the next element.
 463       *
 464       * @since 4.7.0
 465       *
 466       * @link https://secure.php.net/manual/en/iterator.next.php
 467       *
 468       * @return array Of callbacks at next priority.
 469       */
 470  	public function next() {
 471          return next( $this->callbacks );
 472      }
 473  
 474      /**
 475       * Returns the key of the current element.
 476       *
 477       * @since 4.7.0
 478       *
 479       * @link https://secure.php.net/manual/en/iterator.key.php
 480       *
 481       * @return mixed Returns current priority on success, or NULL on failure
 482       */
 483  	public function key() {
 484          return key( $this->callbacks );
 485      }
 486  
 487      /**
 488       * Checks if current position is valid.
 489       *
 490       * @since 4.7.0
 491       *
 492       * @link https://secure.php.net/manual/en/iterator.valid.php
 493       *
 494       * @return boolean
 495       */
 496  	public function valid() {
 497          return key( $this->callbacks ) !== null;
 498      }
 499  
 500      /**
 501       * Rewinds the Iterator to the first element.
 502       *
 503       * @since 4.7.0
 504       *
 505       * @link https://secure.php.net/manual/en/iterator.rewind.php
 506       */
 507  	public function rewind() {
 508          reset( $this->callbacks );
 509      }
 510  
 511  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7