WordPress.org

Make WordPress Core

Ticket #47678: 47678-do_action-spread-operator-v2.patch

File 47678-do_action-spread-operator-v2.patch, 1.6 KB (added by jrf, 5 months ago)

Simplify & modernize do_action() - refreshed after the PHP 4 code was removed from the function

  • src/wp-includes/plugin.php

    From fcdd54cbcd2540955b591a6cd938b7ef50b4614f Mon Sep 17 00:00:00 2001
    From: jrfnl <jrfnl@users.noreply.github.com>
    Date: Thu, 18 Jul 2019 06:18:27 +0200
    Subject: [PATCH] Simplify & modernize do_action()
    
    ---
     src/wp-includes/plugin.php | 14 +++++---------
     1 file changed, 5 insertions(+), 9 deletions(-)
    
    diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php
    index 1f6cacd5b4..02957ee041 100644
    a b function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 
    439439 * @param mixed  ...$arg Optional. Additional arguments which are passed on to the
    440440 *                       functions hooked to the action. Default empty.
    441441 */
    442 function do_action( $tag, $arg = '' ) {
     442function do_action( $tag, ...$arg ) {
    443443        global $wp_filter, $wp_actions, $wp_current_filter;
    444444
    445445        if ( ! isset( $wp_actions[ $tag ] ) ) {
    function do_action( $tag, $arg = '' ) { 
    448448                ++$wp_actions[ $tag ];
    449449        }
    450450
    451         $all_args = func_get_args();
    452 
    453451        // Do 'all' actions first
    454452        if ( isset( $wp_filter['all'] ) ) {
    455453                $wp_current_filter[] = $tag;
     454                $all_args            = func_get_args();
    456455                _wp_call_all_hook( $all_args );
    457456        }
    458457
    function do_action( $tag, $arg = '' ) { 
    467466                $wp_current_filter[] = $tag;
    468467        }
    469468
    470         $args = $all_args;
    471         array_shift( $args );
    472 
    473         if ( empty( $args ) ) {
    474                 $args = array( '' );
     469        if ( empty( $arg ) ) {
     470                $arg[] = '';
    475471        }
    476472
    477         $wp_filter[ $tag ]->do_action( $args );
     473        $wp_filter[ $tag ]->do_action( $arg );
    478474
    479475        array_pop( $wp_current_filter );
    480476}