wp/wp-includes/plugin.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   149  *      * - 'filter me' is the value being filtered.
   149  *      * - 'filter me' is the value being filtered.
   150  *      * - $arg1 and $arg2 are the additional arguments passed to the callback.
   150  *      * - $arg1 and $arg2 are the additional arguments passed to the callback.
   151  *     $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
   151  *     $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
   152  *
   152  *
   153  * @since 0.71
   153  * @since 0.71
       
   154  * @since 6.0.0 Formalized the existing and already documented `...$args` parameter
       
   155  *              by adding it to the function signature.
   154  *
   156  *
   155  * @global WP_Hook[] $wp_filter         Stores all of the filters and actions.
   157  * @global WP_Hook[] $wp_filter         Stores all of the filters and actions.
   156  * @global string[]  $wp_current_filter Stores the list of current filters with the current one last.
   158  * @global string[]  $wp_current_filter Stores the list of current filters with the current one last.
   157  *
   159  *
   158  * @param string $hook_name The name of the filter hook.
   160  * @param string $hook_name The name of the filter hook.
   159  * @param mixed  $value     The value to filter.
   161  * @param mixed  $value     The value to filter.
   160  * @param mixed  ...$args   Additional parameters to pass to the callback functions.
   162  * @param mixed  ...$args   Additional parameters to pass to the callback functions.
   161  * @return mixed The filtered value after all hooked functions are applied to it.
   163  * @return mixed The filtered value after all hooked functions are applied to it.
   162  */
   164  */
   163 function apply_filters( $hook_name, $value ) {
   165 function apply_filters( $hook_name, $value, ...$args ) {
   164 	global $wp_filter, $wp_current_filter;
   166 	global $wp_filter, $wp_current_filter;
   165 
       
   166 	$args = func_get_args();
       
   167 
   167 
   168 	// Do 'all' actions first.
   168 	// Do 'all' actions first.
   169 	if ( isset( $wp_filter['all'] ) ) {
   169 	if ( isset( $wp_filter['all'] ) ) {
   170 		$wp_current_filter[] = $hook_name;
   170 		$wp_current_filter[] = $hook_name;
   171 		_wp_call_all_hook( $args );
   171 
       
   172 		$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
       
   173 		_wp_call_all_hook( $all_args );
   172 	}
   174 	}
   173 
   175 
   174 	if ( ! isset( $wp_filter[ $hook_name ] ) ) {
   176 	if ( ! isset( $wp_filter[ $hook_name ] ) ) {
   175 		if ( isset( $wp_filter['all'] ) ) {
   177 		if ( isset( $wp_filter['all'] ) ) {
   176 			array_pop( $wp_current_filter );
   178 			array_pop( $wp_current_filter );
   181 
   183 
   182 	if ( ! isset( $wp_filter['all'] ) ) {
   184 	if ( ! isset( $wp_filter['all'] ) ) {
   183 		$wp_current_filter[] = $hook_name;
   185 		$wp_current_filter[] = $hook_name;
   184 	}
   186 	}
   185 
   187 
   186 	// Don't pass the tag name to WP_Hook.
   188 	// Pass the value to WP_Hook.
   187 	array_shift( $args );
   189 	array_unshift( $args, $value );
   188 
   190 
   189 	$filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );
   191 	$filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );
   190 
   192 
   191 	array_pop( $wp_current_filter );
   193 	array_pop( $wp_current_filter );
   192 
   194 
   245  *
   247  *
   246  * @since 2.5.0
   248  * @since 2.5.0
   247  *
   249  *
   248  * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
   250  * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
   249  *
   251  *
   250  * @param string         $hook_name The name of the filter hook.
   252  * @param string                      $hook_name The name of the filter hook.
   251  * @param callable|false $callback  Optional. The callback to check for. Default false.
   253  * @param callable|string|array|false $callback  Optional. The callback to check for.
       
   254  *                                               This function can be called unconditionally to speculatively check
       
   255  *                                               a callback that may or may not exist. Default false.
   252  * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
   256  * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
   253  *                  anything registered. When checking a specific function, the priority
   257  *                  anything registered. When checking a specific function, the priority
   254  *                  of that hook is returned, or false if the function is not attached.
   258  *                  of that hook is returned, or false if the function is not attached.
   255  */
   259  */
   256 function has_filter( $hook_name, $callback = false ) {
   260 function has_filter( $hook_name, $callback = false ) {
   275  *
   279  *
   276  * @since 1.2.0
   280  * @since 1.2.0
   277  *
   281  *
   278  * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
   282  * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
   279  *
   283  *
   280  * @param string   $hook_name The filter hook to which the function to be removed is hooked.
   284  * @param string                $hook_name The filter hook to which the function to be removed is hooked.
   281  * @param callable $callback  The name of the function which should be removed.
   285  * @param callable|string|array $callback  The callback to be removed from running when the filter is applied.
   282  * @param int      $priority  Optional. The exact priority used when adding the original
   286  *                                         This function can be called unconditionally to speculatively remove
   283  *                            filter callback. Default 10.
   287  *                                         a callback that may or may not exist.
       
   288  * @param int                   $priority  Optional. The exact priority used when adding the original
       
   289  *                                         filter callback. Default 10.
   284  * @return bool Whether the function existed before it was removed.
   290  * @return bool Whether the function existed before it was removed.
   285  */
   291  */
   286 function remove_filter( $hook_name, $callback, $priority = 10 ) {
   292 function remove_filter( $hook_name, $callback, $priority = 10 ) {
   287 	global $wp_filter;
   293 	global $wp_filter;
   288 
   294 
   528  *
   534  *
   529  * @since 2.5.0
   535  * @since 2.5.0
   530  *
   536  *
   531  * @see has_filter() has_action() is an alias of has_filter().
   537  * @see has_filter() has_action() is an alias of has_filter().
   532  *
   538  *
   533  * @param string         $hook_name The name of the action hook.
   539  * @param string                      $hook_name The name of the action hook.
   534  * @param callable|false $callback  Optional. The callback to check for. Default false.
   540  * @param callable|string|array|false $callback  Optional. The callback to check for.
       
   541  *                                               This function can be called unconditionally to speculatively check
       
   542  *                                               a callback that may or may not exist. Default false.
   535  * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
   543  * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
   536  *                  anything registered. When checking a specific function, the priority
   544  *                  anything registered. When checking a specific function, the priority
   537  *                  of that hook is returned, or false if the function is not attached.
   545  *                  of that hook is returned, or false if the function is not attached.
   538  */
   546  */
   539 function has_action( $hook_name, $callback = false ) {
   547 function has_action( $hook_name, $callback = false ) {
   550  * when the hook was added. This goes for both filters and actions. No warning
   558  * when the hook was added. This goes for both filters and actions. No warning
   551  * will be given on removal failure.
   559  * will be given on removal failure.
   552  *
   560  *
   553  * @since 1.2.0
   561  * @since 1.2.0
   554  *
   562  *
   555  * @param string   $hook_name The action hook to which the function to be removed is hooked.
   563  * @param string                $hook_name The action hook to which the function to be removed is hooked.
   556  * @param callable $callback  The name of the function which should be removed.
   564  * @param callable|string|array $callback  The name of the function which should be removed.
   557  * @param int      $priority  Optional. The exact priority used when adding the original
   565  *                                         This function can be called unconditionally to speculatively remove
   558  *                            action callback. Default 10.
   566  *                                         a callback that may or may not exist.
       
   567  * @param int                   $priority  Optional. The exact priority used when adding the original
       
   568  *                                         action callback. Default 10.
   559  * @return bool Whether the function is removed.
   569  * @return bool Whether the function is removed.
   560  */
   570  */
   561 function remove_action( $hook_name, $callback, $priority = 10 ) {
   571 function remove_action( $hook_name, $callback, $priority = 10 ) {
   562 	return remove_filter( $hook_name, $callback, $priority );
   572 	return remove_filter( $hook_name, $callback, $priority );
   563 }
   573 }
   922  *
   932  *
   923  * @since 2.2.3
   933  * @since 2.2.3
   924  * @since 5.3.0 Removed workarounds for spl_object_hash().
   934  * @since 5.3.0 Removed workarounds for spl_object_hash().
   925  *              `$hook_name` and `$priority` are no longer used,
   935  *              `$hook_name` and `$priority` are no longer used,
   926  *              and the function always returns a string.
   936  *              and the function always returns a string.
       
   937  *
   927  * @access private
   938  * @access private
   928  *
   939  *
   929  * @param string   $hook_name Unused. The name of the filter to build ID for.
   940  * @param string                $hook_name Unused. The name of the filter to build ID for.
   930  * @param callable $callback  The function to generate ID for.
   941  * @param callable|string|array $callback  The callback to generate ID for. The callback may
   931  * @param int      $priority  Unused. The order in which the functions
   942  *                                         or may not exist.
   932  *                            associated with a particular action are executed.
   943  * @param int                   $priority  Unused. The order in which the functions
       
   944  *                                         associated with a particular action are executed.
   933  * @return string Unique function ID for usage as array key.
   945  * @return string Unique function ID for usage as array key.
   934  */
   946  */
   935 function _wp_filter_build_unique_id( $hook_name, $callback, $priority ) {
   947 function _wp_filter_build_unique_id( $hook_name, $callback, $priority ) {
   936 	if ( is_string( $callback ) ) {
   948 	if ( is_string( $callback ) ) {
   937 		return $callback;
   949 		return $callback;