wp/wp-includes/class-wp-hook.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    62 	 *
    62 	 *
    63 	 * @since 4.7.0
    63 	 * @since 4.7.0
    64 	 *
    64 	 *
    65 	 * @param string   $tag             The name of the filter to hook the $function_to_add callback to.
    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.
    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
    67 	 * @param int      $priority        The order in which the functions associated with a particular action
    68 	 *                                  particular action are executed. Lower numbers correspond with
    68 	 *                                  are executed. Lower numbers correspond with earlier execution,
    69 	 *                                  earlier execution, and functions with the same priority are executed
    69 	 *                                  and functions with the same priority are executed in the order
    70 	 *                                  in the order in which they were added to the action.
    70 	 *                                  in which they were added to the action.
    71 	 * @param int      $accepted_args   The number of arguments the function accepts.
    71 	 * @param int      $accepted_args   The number of arguments the function accepts.
    72 	 */
    72 	 */
    73 	public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) {
    73 	public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) {
    74 		$idx              = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
    74 		$idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
       
    75 
    75 		$priority_existed = isset( $this->callbacks[ $priority ] );
    76 		$priority_existed = isset( $this->callbacks[ $priority ] );
    76 
    77 
    77 		$this->callbacks[ $priority ][ $idx ] = array(
    78 		$this->callbacks[ $priority ][ $idx ] = array(
    78 			'function'      => $function_to_add,
    79 			'function'      => $function_to_add,
    79 			'accepted_args' => $accepted_args,
    80 			'accepted_args' => $accepted_args,
    80 		);
    81 		);
    81 
    82 
    82 		// if we're adding a new priority to the list, put them back in sorted order
    83 		// 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 		if ( ! $priority_existed && count( $this->callbacks ) > 1 ) {
    84 			ksort( $this->callbacks, SORT_NUMERIC );
    85 			ksort( $this->callbacks, SORT_NUMERIC );
    85 		}
    86 		}
    86 
    87 
    87 		if ( $this->nesting_level > 0 ) {
    88 		if ( $this->nesting_level > 0 ) {
    88 			$this->resort_active_iterations( $priority, $priority_existed );
    89 			$this->resort_active_iterations( $priority, $priority_existed );
    89 		}
    90 		}
    90 	}
    91 	}
    91 
    92 
    92 	/**
    93 	/**
    93 	 * Handles reseting callback priority keys mid-iteration.
    94 	 * Handles resetting callback priority keys mid-iteration.
    94 	 *
    95 	 *
    95 	 * @since 4.7.0
    96 	 * @since 4.7.0
    96 	 *
    97 	 *
    97 	 * @param bool|int $new_priority     Optional. The priority of the new filter being added. Default false,
    98 	 * @param bool|int $new_priority     Optional. The priority of the new filter being added. Default false,
    98 	 *                                   for no priority being added.
    99 	 *                                   for no priority being added.
   132 			}
   133 			}
   133 
   134 
   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 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 			if ( $new_priority === $this->current_priority[ $index ] && ! $priority_existed ) {
   136 				/*
   137 				/*
   137 				 * ... and the new priority is the same as what $this->iterations thinks is the previous
   138 				 * ...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 				 * priority, we need to move back to it.
   139 				 */
   140 				 */
   140 
   141 
   141 				if ( false === current( $iteration ) ) {
   142 				if ( false === current( $iteration ) ) {
   142 					// If we've already moved off the end of the array, go back to the last element.
   143 					// If we've already moved off the end of the array, go back to the last element.
   160 	/**
   161 	/**
   161 	 * Unhooks a function or method from a specific filter action.
   162 	 * Unhooks a function or method from a specific filter action.
   162 	 *
   163 	 *
   163 	 * @since 4.7.0
   164 	 * @since 4.7.0
   164 	 *
   165 	 *
   165 	 * @param string   $tag                The filter hook to which the function to be removed is hooked. Used
   166 	 * @param string   $tag                The filter hook to which the function to be removed is hooked.
   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.
   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.
   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.
   169 	 * @return bool Whether the callback existed before it was removed.
   170 	 */
   170 	 */
   171 	public function remove_filter( $tag, $function_to_remove, $priority ) {
   171 	public function remove_filter( $tag, $function_to_remove, $priority ) {
   187 	/**
   187 	/**
   188 	 * Checks if a specific action has been registered for this hook.
   188 	 * Checks if a specific action has been registered for this hook.
   189 	 *
   189 	 *
   190 	 * @since 4.7.0
   190 	 * @since 4.7.0
   191 	 *
   191 	 *
   192 	 * @param string        $tag               Optional. The name of the filter hook. Used for building
   192 	 * @param string        $tag               Optional. The name of the filter hook. Default empty.
   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.
   193 	 * @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.
   194 	 * @return bool|int The priority of that hook is returned, or false if the function is not attached.
   196 	 */
   195 	 */
   197 	public function has_filter( $tag = '', $function_to_check = false ) {
   196 	public function has_filter( $tag = '', $function_to_check = false ) {
   198 		if ( false === $function_to_check ) {
   197 		if ( false === $function_to_check ) {
   251 			$this->resort_active_iterations();
   250 			$this->resort_active_iterations();
   252 		}
   251 		}
   253 	}
   252 	}
   254 
   253 
   255 	/**
   254 	/**
   256 	 * Calls the callback functions added to a filter hook.
   255 	 * Calls the callback functions that have been added to a filter hook.
   257 	 *
   256 	 *
   258 	 * @since 4.7.0
   257 	 * @since 4.7.0
   259 	 *
   258 	 *
   260 	 * @param mixed $value The value to filter.
   259 	 * @param mixed $value The value to filter.
   261 	 * @param array $args  Arguments to pass to callbacks.
   260 	 * @param array $args  Additional parameters to pass to the callback functions.
       
   261 	 *                     This array is expected to include $value at index 0.
   262 	 * @return mixed The filtered value after all hooked functions are applied to it.
   262 	 * @return mixed The filtered value after all hooked functions are applied to it.
   263 	 */
   263 	 */
   264 	public function apply_filters( $value, $args ) {
   264 	public function apply_filters( $value, $args ) {
   265 		if ( ! $this->callbacks ) {
   265 		if ( ! $this->callbacks ) {
   266 			return $value;
   266 			return $value;
   270 
   270 
   271 		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
   271 		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
   272 		$num_args                           = count( $args );
   272 		$num_args                           = count( $args );
   273 
   273 
   274 		do {
   274 		do {
   275 			$this->current_priority[ $nesting_level ] = $priority = current( $this->iterations[ $nesting_level ] );
   275 			$this->current_priority[ $nesting_level ] = current( $this->iterations[ $nesting_level ] );
       
   276 			$priority                                 = $this->current_priority[ $nesting_level ];
   276 
   277 
   277 			foreach ( $this->callbacks[ $priority ] as $the_ ) {
   278 			foreach ( $this->callbacks[ $priority ] as $the_ ) {
   278 				if ( ! $this->doing_action ) {
   279 				if ( ! $this->doing_action ) {
   279 					$args[0] = $value;
   280 					$args[0] = $value;
   280 				}
   281 				}
   281 
   282 
   282 				// Avoid the array_slice if possible.
   283 				// Avoid the array_slice() if possible.
   283 				if ( $the_['accepted_args'] == 0 ) {
   284 				if ( 0 == $the_['accepted_args'] ) {
   284 					$value = call_user_func_array( $the_['function'], array() );
   285 					$value = call_user_func( $the_['function'] );
   285 				} elseif ( $the_['accepted_args'] >= $num_args ) {
   286 				} elseif ( $the_['accepted_args'] >= $num_args ) {
   286 					$value = call_user_func_array( $the_['function'], $args );
   287 					$value = call_user_func_array( $the_['function'], $args );
   287 				} else {
   288 				} else {
   288 					$value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
   289 					$value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
   289 				}
   290 				}
   297 
   298 
   298 		return $value;
   299 		return $value;
   299 	}
   300 	}
   300 
   301 
   301 	/**
   302 	/**
   302 	 * Executes the callback functions hooked on a specific action hook.
   303 	 * Calls the callback functions that have been added to an action hook.
   303 	 *
   304 	 *
   304 	 * @since 4.7.0
   305 	 * @since 4.7.0
   305 	 *
   306 	 *
   306 	 * @param mixed $args Arguments to pass to the hook callbacks.
   307 	 * @param array $args Parameters to pass to the callback functions.
   307 	 */
   308 	 */
   308 	public function do_action( $args ) {
   309 	public function do_action( $args ) {
   309 		$this->doing_action = true;
   310 		$this->doing_action = true;
   310 		$this->apply_filters( '', $args );
   311 		$this->apply_filters( '', $args );
   311 
   312 
   387 	/**
   388 	/**
   388 	 * Determines whether an offset value exists.
   389 	 * Determines whether an offset value exists.
   389 	 *
   390 	 *
   390 	 * @since 4.7.0
   391 	 * @since 4.7.0
   391 	 *
   392 	 *
   392 	 * @link https://secure.php.net/manual/en/arrayaccess.offsetexists.php
   393 	 * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
   393 	 *
   394 	 *
   394 	 * @param mixed $offset An offset to check for.
   395 	 * @param mixed $offset An offset to check for.
   395 	 * @return bool True if the offset exists, false otherwise.
   396 	 * @return bool True if the offset exists, false otherwise.
   396 	 */
   397 	 */
   397 	public function offsetExists( $offset ) {
   398 	public function offsetExists( $offset ) {
   401 	/**
   402 	/**
   402 	 * Retrieves a value at a specified offset.
   403 	 * Retrieves a value at a specified offset.
   403 	 *
   404 	 *
   404 	 * @since 4.7.0
   405 	 * @since 4.7.0
   405 	 *
   406 	 *
   406 	 * @link https://secure.php.net/manual/en/arrayaccess.offsetget.php
   407 	 * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
   407 	 *
   408 	 *
   408 	 * @param mixed $offset The offset to retrieve.
   409 	 * @param mixed $offset The offset to retrieve.
   409 	 * @return mixed If set, the value at the specified offset, null otherwise.
   410 	 * @return mixed If set, the value at the specified offset, null otherwise.
   410 	 */
   411 	 */
   411 	public function offsetGet( $offset ) {
   412 	public function offsetGet( $offset ) {
   415 	/**
   416 	/**
   416 	 * Sets a value at a specified offset.
   417 	 * Sets a value at a specified offset.
   417 	 *
   418 	 *
   418 	 * @since 4.7.0
   419 	 * @since 4.7.0
   419 	 *
   420 	 *
   420 	 * @link https://secure.php.net/manual/en/arrayaccess.offsetset.php
   421 	 * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
   421 	 *
   422 	 *
   422 	 * @param mixed $offset The offset to assign the value to.
   423 	 * @param mixed $offset The offset to assign the value to.
   423 	 * @param mixed $value The value to set.
   424 	 * @param mixed $value The value to set.
   424 	 */
   425 	 */
   425 	public function offsetSet( $offset, $value ) {
   426 	public function offsetSet( $offset, $value ) {
   433 	/**
   434 	/**
   434 	 * Unsets a specified offset.
   435 	 * Unsets a specified offset.
   435 	 *
   436 	 *
   436 	 * @since 4.7.0
   437 	 * @since 4.7.0
   437 	 *
   438 	 *
   438 	 * @link https://secure.php.net/manual/en/arrayaccess.offsetunset.php
   439 	 * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
   439 	 *
   440 	 *
   440 	 * @param mixed $offset The offset to unset.
   441 	 * @param mixed $offset The offset to unset.
   441 	 */
   442 	 */
   442 	public function offsetUnset( $offset ) {
   443 	public function offsetUnset( $offset ) {
   443 		unset( $this->callbacks[ $offset ] );
   444 		unset( $this->callbacks[ $offset ] );
   446 	/**
   447 	/**
   447 	 * Returns the current element.
   448 	 * Returns the current element.
   448 	 *
   449 	 *
   449 	 * @since 4.7.0
   450 	 * @since 4.7.0
   450 	 *
   451 	 *
   451 	 * @link https://secure.php.net/manual/en/iterator.current.php
   452 	 * @link https://www.php.net/manual/en/iterator.current.php
   452 	 *
   453 	 *
   453 	 * @return array Of callbacks at current priority.
   454 	 * @return array Of callbacks at current priority.
   454 	 */
   455 	 */
   455 	public function current() {
   456 	public function current() {
   456 		return current( $this->callbacks );
   457 		return current( $this->callbacks );
   459 	/**
   460 	/**
   460 	 * Moves forward to the next element.
   461 	 * Moves forward to the next element.
   461 	 *
   462 	 *
   462 	 * @since 4.7.0
   463 	 * @since 4.7.0
   463 	 *
   464 	 *
   464 	 * @link https://secure.php.net/manual/en/iterator.next.php
   465 	 * @link https://www.php.net/manual/en/iterator.next.php
   465 	 *
   466 	 *
   466 	 * @return array Of callbacks at next priority.
   467 	 * @return array Of callbacks at next priority.
   467 	 */
   468 	 */
   468 	public function next() {
   469 	public function next() {
   469 		return next( $this->callbacks );
   470 		return next( $this->callbacks );
   472 	/**
   473 	/**
   473 	 * Returns the key of the current element.
   474 	 * Returns the key of the current element.
   474 	 *
   475 	 *
   475 	 * @since 4.7.0
   476 	 * @since 4.7.0
   476 	 *
   477 	 *
   477 	 * @link https://secure.php.net/manual/en/iterator.key.php
   478 	 * @link https://www.php.net/manual/en/iterator.key.php
   478 	 *
   479 	 *
   479 	 * @return mixed Returns current priority on success, or NULL on failure
   480 	 * @return mixed Returns current priority on success, or NULL on failure
   480 	 */
   481 	 */
   481 	public function key() {
   482 	public function key() {
   482 		return key( $this->callbacks );
   483 		return key( $this->callbacks );
   485 	/**
   486 	/**
   486 	 * Checks if current position is valid.
   487 	 * Checks if current position is valid.
   487 	 *
   488 	 *
   488 	 * @since 4.7.0
   489 	 * @since 4.7.0
   489 	 *
   490 	 *
   490 	 * @link https://secure.php.net/manual/en/iterator.valid.php
   491 	 * @link https://www.php.net/manual/en/iterator.valid.php
   491 	 *
   492 	 *
   492 	 * @return boolean
   493 	 * @return boolean
   493 	 */
   494 	 */
   494 	public function valid() {
   495 	public function valid() {
   495 		return key( $this->callbacks ) !== null;
   496 		return key( $this->callbacks ) !== null;
   498 	/**
   499 	/**
   499 	 * Rewinds the Iterator to the first element.
   500 	 * Rewinds the Iterator to the first element.
   500 	 *
   501 	 *
   501 	 * @since 4.7.0
   502 	 * @since 4.7.0
   502 	 *
   503 	 *
   503 	 * @link https://secure.php.net/manual/en/iterator.rewind.php
   504 	 * @link https://www.php.net/manual/en/iterator.rewind.php
   504 	 */
   505 	 */
   505 	public function rewind() {
   506 	public function rewind() {
   506 		reset( $this->callbacks );
   507 		reset( $this->callbacks );
   507 	}
   508 	}
   508 
   509