Make WordPress Core


Ignore:
Timestamp:
03/18/2022 06:21:40 PM (3 years ago)
Author:
johnbillion
Message:

Plugins: Convert apply_filters() into a proper variadic function.

This makes its signature more correct by implementing the spread operator, and adjusts the internal logic correspondingly without affecting performance.

Props jrf, SergeyBiryukov, davidbaumwald, mauriac, johnbillion

Fixes #53218

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/plugin.php

    r52300 r52952  
    152152 *
    153153 * @since 0.71
     154 * @since 6.0.0 Formalized the existing and already documented `...$args` parameter
     155 *              by adding it to the function signature.
    154156 *
    155157 * @global WP_Hook[] $wp_filter         Stores all of the filters and actions.
     
    161163 * @return mixed The filtered value after all hooked functions are applied to it.
    162164 */
    163 function apply_filters( $hook_name, $value ) {
     165function apply_filters( $hook_name, $value, ...$args ) {
    164166    global $wp_filter, $wp_current_filter;
    165 
    166     $args = func_get_args();
    167167
    168168    // Do 'all' actions first.
    169169    if ( isset( $wp_filter['all'] ) ) {
    170170        $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 );
    172174    }
    173175
     
    184186    }
    185187
    186     // Don't pass the tag name to WP_Hook.
    187     array_shift( $args );
     188    // Pass the value to WP_Hook.
     189    array_unshift( $args, $value );
    188190
    189191    $filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );
Note: See TracChangeset for help on using the changeset viewer.