Make WordPress Core

Ticket #37985: 37985.1.diff

File 37985.1.diff, 2.4 KB (added by aaemnnosttv, 9 years ago)

Initial patch

  • src/wp-includes/class-wp-hook.php

    diff --git a/src/wp-includes/class-wp-hook.php b/src/wp-includes/class-wp-hook.php
    index 823b4b5..2fba490 100755
    a b final class WP_Hook implements Iterator, ArrayAccess { 
    287287                        $this->current_priority[ $nesting_level ] = $priority = current( $this->iterations[ $nesting_level ] );
    288288
    289289                        foreach ( $this->callbacks[ $priority ] as $the_ ) {
    290                                 if( ! $this->doing_action ) {
     290                                if ( ! $this->doing_action ) {
    291291                                        $args[ 0 ] = $value;
    292292                                }
    293293
    294294                                // Avoid the array_slice if possible.
    295                                 if ( $the_['accepted_args'] == 0 ) {
    296                                         $value = call_user_func_array( $the_['function'], array() );
    297                                 } elseif ( $the_['accepted_args'] >= $num_args ) {
    298                                         $value = call_user_func_array( $the_['function'], $args );
     295                                if ( is_numeric( $the_['accepted_args'] ) && $the_['accepted_args'] == 0 ) {
     296                                        $call_args = array();
     297                                } elseif ( $the_['accepted_args'] > 0 && $the_['accepted_args'] < $num_args ) {
     298                                        $call_args = array_slice( $args, 0, (int) $the_['accepted_args'] );
    299299                                } else {
    300                                         $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int)$the_['accepted_args'] ) );
     300                                        $call_args = $args;
    301301                                }
     302
     303                                $value = call_user_func_array( $the_['function'], $call_args );
    302304                        }
    303305                } while ( false !== next( $this->iterations[ $nesting_level ] ) );
    304306
  • tests/phpunit/tests/hooks/do_action.php

    diff --git a/tests/phpunit/tests/hooks/do_action.php b/tests/phpunit/tests/hooks/do_action.php
    index b810d46..c7255f6 100755
    a b class Tests_WP_Hook_Do_Action extends WP_UnitTestCase { 
    139139                $this->assertSame( 'a1-b1b3-a2a3', $this->action_output );
    140140        }
    141141
     142        public function test_can_pass_all_args() {
     143                $a = new MockAction();
     144                $callback = array( $a, 'action' );
     145                $hook = new WP_Hook();
     146                $tag = rand_str();
     147                $priority = rand( 1, 100 );
     148                $accepted_args = -1; // will pass all
     149
     150                $args = array();
     151                for ( $i = 1; $i <= rand( 5, 10 ); $i++ ) {
     152                        $args[] = rand_str();
     153                }
     154
     155                $hook->add_filter( $tag, $callback, $priority, $accepted_args );
     156                $hook->do_action( $args );
     157
     158                $called_args = $a->get_args();
     159
     160                $this->assertEquals( count( $args ), count( $called_args[0] ) );
     161        }
     162
    142163        public function _filter_do_action_doesnt_change_value1( $value ) {
    143164                $this->action_output .= $value . 1;
    144165                return 'x1';