wp/wp-includes/class-wp-hook.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   166 	/**
   166 	/**
   167 	 * Removes a callback function from a filter hook.
   167 	 * Removes a callback function from a filter hook.
   168 	 *
   168 	 *
   169 	 * @since 4.7.0
   169 	 * @since 4.7.0
   170 	 *
   170 	 *
   171 	 * @param string   $hook_name The filter hook to which the function to be removed is hooked.
   171 	 * @param string                $hook_name The filter hook to which the function to be removed is hooked.
   172 	 * @param callable $callback  The callback to be removed from running when the filter is applied.
   172 	 * @param callable|string|array $callback  The callback to be removed from running when the filter is applied.
   173 	 * @param int      $priority  The exact priority used when adding the original filter callback.
   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.
   174 	 * @return bool Whether the callback existed before it was removed.
   176 	 * @return bool Whether the callback existed before it was removed.
   175 	 */
   177 	 */
   176 	public function remove_filter( $hook_name, $callback, $priority ) {
   178 	public function remove_filter( $hook_name, $callback, $priority ) {
   177 		$function_key = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
   179 		$function_key = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
   178 
   180 
   199 	 * When using the `$callback` argument, this function may return a non-boolean value
   201 	 * When using the `$callback` argument, this function may return a non-boolean value
   200 	 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
   202 	 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
   201 	 *
   203 	 *
   202 	 * @since 4.7.0
   204 	 * @since 4.7.0
   203 	 *
   205 	 *
   204 	 * @param string         $hook_name Optional. The name of the filter hook. Default empty.
   206 	 * @param string                      $hook_name Optional. The name of the filter hook. Default empty.
   205 	 * @param callable|false $callback  Optional. The callback to check for. Default false.
   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.
   206 	 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
   210 	 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
   207 	 *                  anything registered. When checking a specific function, the priority
   211 	 *                  anything registered. When checking a specific function, the priority
   208 	 *                  of that hook is returned, or false if the function is not attached.
   212 	 *                  of that hook is returned, or false if the function is not attached.
   209 	 */
   213 	 */
   210 	public function has_filter( $hook_name = '', $callback = false ) {
   214 	public function has_filter( $hook_name = '', $callback = false ) {
   435 	 * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
   439 	 * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
   436 	 *
   440 	 *
   437 	 * @param mixed $offset An offset to check for.
   441 	 * @param mixed $offset An offset to check for.
   438 	 * @return bool True if the offset exists, false otherwise.
   442 	 * @return bool True if the offset exists, false otherwise.
   439 	 */
   443 	 */
       
   444 	#[ReturnTypeWillChange]
   440 	public function offsetExists( $offset ) {
   445 	public function offsetExists( $offset ) {
   441 		return isset( $this->callbacks[ $offset ] );
   446 		return isset( $this->callbacks[ $offset ] );
   442 	}
   447 	}
   443 
   448 
   444 	/**
   449 	/**
   449 	 * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
   454 	 * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
   450 	 *
   455 	 *
   451 	 * @param mixed $offset The offset to retrieve.
   456 	 * @param mixed $offset The offset to retrieve.
   452 	 * @return mixed If set, the value at the specified offset, null otherwise.
   457 	 * @return mixed If set, the value at the specified offset, null otherwise.
   453 	 */
   458 	 */
       
   459 	#[ReturnTypeWillChange]
   454 	public function offsetGet( $offset ) {
   460 	public function offsetGet( $offset ) {
   455 		return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null;
   461 		return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null;
   456 	}
   462 	}
   457 
   463 
   458 	/**
   464 	/**
   463 	 * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
   469 	 * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
   464 	 *
   470 	 *
   465 	 * @param mixed $offset The offset to assign the value to.
   471 	 * @param mixed $offset The offset to assign the value to.
   466 	 * @param mixed $value The value to set.
   472 	 * @param mixed $value The value to set.
   467 	 */
   473 	 */
       
   474 	#[ReturnTypeWillChange]
   468 	public function offsetSet( $offset, $value ) {
   475 	public function offsetSet( $offset, $value ) {
   469 		if ( is_null( $offset ) ) {
   476 		if ( is_null( $offset ) ) {
   470 			$this->callbacks[] = $value;
   477 			$this->callbacks[] = $value;
   471 		} else {
   478 		} else {
   472 			$this->callbacks[ $offset ] = $value;
   479 			$this->callbacks[ $offset ] = $value;
   480 	 *
   487 	 *
   481 	 * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
   488 	 * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
   482 	 *
   489 	 *
   483 	 * @param mixed $offset The offset to unset.
   490 	 * @param mixed $offset The offset to unset.
   484 	 */
   491 	 */
       
   492 	#[ReturnTypeWillChange]
   485 	public function offsetUnset( $offset ) {
   493 	public function offsetUnset( $offset ) {
   486 		unset( $this->callbacks[ $offset ] );
   494 		unset( $this->callbacks[ $offset ] );
   487 	}
   495 	}
   488 
   496 
   489 	/**
   497 	/**
   493 	 *
   501 	 *
   494 	 * @link https://www.php.net/manual/en/iterator.current.php
   502 	 * @link https://www.php.net/manual/en/iterator.current.php
   495 	 *
   503 	 *
   496 	 * @return array Of callbacks at current priority.
   504 	 * @return array Of callbacks at current priority.
   497 	 */
   505 	 */
       
   506 	#[ReturnTypeWillChange]
   498 	public function current() {
   507 	public function current() {
   499 		return current( $this->callbacks );
   508 		return current( $this->callbacks );
   500 	}
   509 	}
   501 
   510 
   502 	/**
   511 	/**
   506 	 *
   515 	 *
   507 	 * @link https://www.php.net/manual/en/iterator.next.php
   516 	 * @link https://www.php.net/manual/en/iterator.next.php
   508 	 *
   517 	 *
   509 	 * @return array Of callbacks at next priority.
   518 	 * @return array Of callbacks at next priority.
   510 	 */
   519 	 */
       
   520 	#[ReturnTypeWillChange]
   511 	public function next() {
   521 	public function next() {
   512 		return next( $this->callbacks );
   522 		return next( $this->callbacks );
   513 	}
   523 	}
   514 
   524 
   515 	/**
   525 	/**
   519 	 *
   529 	 *
   520 	 * @link https://www.php.net/manual/en/iterator.key.php
   530 	 * @link https://www.php.net/manual/en/iterator.key.php
   521 	 *
   531 	 *
   522 	 * @return mixed Returns current priority on success, or NULL on failure
   532 	 * @return mixed Returns current priority on success, or NULL on failure
   523 	 */
   533 	 */
       
   534 	#[ReturnTypeWillChange]
   524 	public function key() {
   535 	public function key() {
   525 		return key( $this->callbacks );
   536 		return key( $this->callbacks );
   526 	}
   537 	}
   527 
   538 
   528 	/**
   539 	/**
   532 	 *
   543 	 *
   533 	 * @link https://www.php.net/manual/en/iterator.valid.php
   544 	 * @link https://www.php.net/manual/en/iterator.valid.php
   534 	 *
   545 	 *
   535 	 * @return bool Whether the current position is valid.
   546 	 * @return bool Whether the current position is valid.
   536 	 */
   547 	 */
       
   548 	#[ReturnTypeWillChange]
   537 	public function valid() {
   549 	public function valid() {
   538 		return key( $this->callbacks ) !== null;
   550 		return key( $this->callbacks ) !== null;
   539 	}
   551 	}
   540 
   552 
   541 	/**
   553 	/**
   543 	 *
   555 	 *
   544 	 * @since 4.7.0
   556 	 * @since 4.7.0
   545 	 *
   557 	 *
   546 	 * @link https://www.php.net/manual/en/iterator.rewind.php
   558 	 * @link https://www.php.net/manual/en/iterator.rewind.php
   547 	 */
   559 	 */
       
   560 	#[ReturnTypeWillChange]
   548 	public function rewind() {
   561 	public function rewind() {
   549 		reset( $this->callbacks );
   562 		reset( $this->callbacks );
   550 	}
   563 	}
   551 
   564 
   552 }
   565 }