WordPress.org

Make WordPress Core

Ticket #35956: 35956.diff

File 35956.diff, 5.1 KB (added by stephenharris, 6 years ago)

Improvements to earlier patch: Removed changes for styles as these were not relevant. Included tests and used $_group to $new_group for clarity.

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

    diff --git src/wp-includes/class.wp-dependencies.php src/wp-includes/class.wp-dependencies.php
    index 31bc84d..2473974 100644
    class WP_Dependencies { 
    153153                        if ( in_array($handle, $this->done, true) ) // Already done
    154154                                continue;
    155155
    156                         $moved = $this->set_group( $handle, $recursion, $group );
     156                        $moved     = $this->set_group( $handle, $recursion, $group );
     157                        $new_group = $this->groups[$handle];
    157158
    158159                        if ( $queued && !$moved ) // already queued and in the right group
    159160                                continue;
    class WP_Dependencies { 
    163164                                $keep_going = false; // Item doesn't exist.
    164165                        elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
    165166                                $keep_going = false; // Item requires dependencies that don't exist.
    166                         elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) )
     167                        elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $new_group ) )
    167168                                $keep_going = false; // Item requires dependencies that don't exist.
    168169
    169170                        if ( ! $keep_going ) { // Either item or its dependencies don't exist.
    class WP_Dependencies { 
    384385        public function set_group( $handle, $recursion, $group ) {
    385386                $group = (int) $group;
    386387
    387                 if ( $recursion ) {
    388                         $group = min( $this->group, $group );
    389                 }
    390 
    391                 $this->group = $group;
    392 
    393388                if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group )
    394389                        return false;
    395390
  • src/wp-includes/class.wp-scripts.php

    diff --git src/wp-includes/class.wp-scripts.php src/wp-includes/class.wp-scripts.php
    index 34f1016..9c04c27 100644
    class WP_Scripts extends WP_Dependencies { 
    356356         * @return bool True on success, false on failure.
    357357         */
    358358        public function all_deps( $handles, $recursion = false, $group = false ) {
    359                 $r = parent::all_deps( $handles, $recursion );
     359                $r = parent::all_deps( $handles, $recursion, $group );
    360360                if ( ! $recursion ) {
    361361                        /**
    362362                         * Filter the list of script dependencies left to print.
  • tests/phpunit/tests/dependencies/scripts.php

    diff --git tests/phpunit/tests/dependencies/scripts.php tests/phpunit/tests/dependencies/scripts.php
    index 02045cf..f2a8903 100644
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    290290        }
    291291
    292292        /**
     293         * @ticket 35956
     294         */
     295        function test_wp_register_script_with_dependencies_in_footer() {
     296                wp_register_script( 'child-head', '/child-head.js', array(), null, false );
     297                wp_register_script( 'child-footer', '/child-footer.js', array(), null, true );
     298                wp_register_script( 'parent', '/parent.js', array( 'child-head', 'child-footer' ), null, true );
     299
     300                wp_enqueue_script( 'parent' );
     301
     302                $header = get_echo( 'wp_print_head_scripts' );
     303                $footer = get_echo( 'wp_print_footer_scripts' );
     304
     305                $expected_header  = "<script type='text/javascript' src='/child-head.js'></script>\n";
     306                $expected_footer  = "<script type='text/javascript' src='/child-footer.js'></script>\n";
     307                $expected_footer .= "<script type='text/javascript' src='/parent.js'></script>\n";
     308
     309                $this->assertEquals( $expected_header, $header );
     310                $this->assertEquals( $expected_footer, $footer );
     311        }
     312
     313        /**
     314         * @ticket 35956
     315         */
     316        function test_wp_register_script_with_dependencies_in_footer2() {
     317                wp_register_script( 'grandchild-head', '/grandchild-head.js', array(), null, false );
     318                wp_register_script( 'child-head', '/child-head.js', array(), null, false );
     319                wp_register_script( 'child-footer', '/child-footer.js', array( 'grandchild-head' ), null, true );
     320                wp_register_script( 'child2-head', '/child2-head.js', array(), null, false );
     321                wp_register_script( 'child2-footer', '/child2-footer.js', array(), null, true );
     322                wp_register_script( 'parent-footer', '/parent-footer.js', array( 'child-head', 'child-footer', 'child2-head', 'child2-footer' ), null, true );
     323                wp_register_script( 'parent-header', '/parent-header.js', array( 'child-head' ), null, false );
     324
     325                wp_enqueue_script( 'parent-footer' );
     326                wp_enqueue_script( 'parent-header' );
     327
     328                $header = get_echo( 'wp_print_head_scripts' );
     329                $footer = get_echo( 'wp_print_footer_scripts' );
     330
     331                $expected_header  = "<script type='text/javascript' src='/child-head.js'></script>\n";
     332                $expected_header .= "<script type='text/javascript' src='/grandchild-head.js'></script>\n";
     333                $expected_header .= "<script type='text/javascript' src='/child2-head.js'></script>\n";
     334                $expected_header .= "<script type='text/javascript' src='/parent-header.js'></script>\n";
     335
     336                $expected_footer  = "<script type='text/javascript' src='/child-footer.js'></script>\n";
     337                $expected_footer .= "<script type='text/javascript' src='/child2-footer.js'></script>\n";
     338                $expected_footer .= "<script type='text/javascript' src='/parent-footer.js'></script>\n";
     339
     340                $this->assertEquals( $expected_header, $header );
     341                $this->assertEquals( $expected_footer, $footer );
     342        }
     343
     344        /**
    293345         * @ticket 14853
    294346         */
    295347        function test_wp_add_inline_script_returns_bool() {