Make WordPress Core

Ticket #59599: 58599.2.diff

File 58599.2.diff, 4.9 KB (added by adamsilverstein, 16 months ago)
  • src/wp-includes/class-wp-scripts.php

    diff --git src/wp-includes/class-wp-scripts.php src/wp-includes/class-wp-scripts.php
    index a6a283f953..4e61d28b10 100644
    class WP_Scripts extends WP_Dependencies { 
    281281                        $intended_strategy = '';
    282282                }
    283283
     284                // If the script is in the header group, current output is the header, the intended strategy is `defer`, the
     285                // actual strategy is not defer and all dependent scripts are in the footer - move this script to the footer.
     286                if ( 'defer' === $intended_strategy && 'defer' !== $strategy && 0 === $this->groups[ $handle ] && 0 === $group ) {
     287                        // Check that all depedencies are in the footer.
     288                        $all_deps_in_footer = true;
     289                        foreach ( $this->get_dependents( $handle ) as $dep ) {
     290                                if ( isset( $this->groups[ $dep ] ) && 1 !== $this->groups[ $dep ] ) {
     291                                        $all_deps_in_footer = false;
     292                                        break;
     293                                }
     294                        }
     295                        if ( $all_deps_in_footer ) {
     296                                $this->in_footer[] = $handle;
     297                                return false;
     298                        }
     299                }
     300
    284301                if ( $conditional ) {
    285302                        $cond_before = "<!--[if {$conditional}]>\n";
    286303                        $cond_after  = "<![endif]-->\n";
  • tests/phpunit/tests/dependencies/scripts.php

    diff --git tests/phpunit/tests/dependencies/scripts.php tests/phpunit/tests/dependencies/scripts.php
    index 7f2b956127..50f00f2ef7 100644
    HTML 
    30653065        protected function add_html5_script_theme_support() {
    30663066                add_theme_support( 'html5', array( 'script' ) );
    30673067        }
     3068
     3069        /**
     3070         * Test that a script is moved to the footer if it is made non deferrable, was in the header and
     3071         * all scripts that depend on it are in the footer.
     3072         *
     3073         * @ticket 58599
     3074         *
     3075         * @dataProvider data_provider_script_move_to_footer
     3076         *
     3077         * @param string $b_in_footer in_footer value for script B.
     3078         * @param string $expected    Expected output.
     3079         * @param bool   $expect_a_in_footer Is script A in the footer?
     3080         * @param bool   $expect_b_in_footer Is script B in the footer?
     3081         */
     3082        public function test_wp_scripts_move_to_footer( $b_in_footer, $expected, $expect_a_in_footer, $expect_b_in_footer ) {
     3083                // Script A uses strategy=>defer. Script B depends on script A and uses in_footer=>true.
     3084                wp_enqueue_script( 'script-a', 'script-a.js', array(), null, array( 'strategy' => 'defer' ) );
     3085                wp_enqueue_script( 'script-b', 'script-b.js', array( 'script-a' ), null, $b_in_footer );
     3086
     3087                // Run the header and footer capturing the output.
     3088                ob_start();
     3089                wp_scripts()->do_head_items();
     3090                wp_scripts()->do_footer_items();
     3091                $actual = ob_get_clean();
     3092
     3093                $this->assertEquals( $expected, $actual );
     3094                $this->assertEquals( $expect_a_in_footer, in_array( 'script-a', wp_scripts()->in_footer, true ) );
     3095                $this->assertEquals( $expect_b_in_footer, in_array( 'script-b', wp_scripts()->in_footer, true ) );
     3096        }
     3097
     3098        /**
     3099         * Data provider for test_wp_scripts_move_to_footer.
     3100         *
     3101         * @return array[] {
     3102         *     Array of arguments for test.
     3103         *
     3104         *     @type string $b_in_footer in_footer value for script B.
     3105         *     @type string $expected    Expected output.
     3106         * }
     3107         */
     3108        public function data_provider_script_move_to_footer() {
     3109                return array(
     3110
     3111                        'b in the footer, not deferred' => array(
     3112                                // Script B is in the footer, but not deferred.
     3113                                'b_in_footer'        => true,
     3114                                'expect'             => '<script type="text/javascript" src="http://script-a.js" id="script-a-js" data-wp-strategy="defer"></script>
     3115<script type="text/javascript" src="http://script-b.js" id="script-b-js"></script>
     3116',
     3117                                'expect_a_in_footer' => true,
     3118                                'expect_b_in_footer' => true,
     3119                        ),
     3120                        'b in header not deferred'      => array(
     3121                                'b_in_footer'        => false,
     3122                                'expect'             => '<script type="text/javascript" src="http://script-a.js" id="script-a-js" data-wp-strategy="defer"></script>
     3123<script type="text/javascript" src="http://script-b.js" id="script-b-js"></script>
     3124',
     3125                                'expect_a_in_footer' => false,
     3126                                'expect_b_in_footer' => false,
     3127                        ),
     3128                        'b in footer and deferred'      => array(
     3129                                'b_in_footer'        => array(
     3130                                        'strategy'  => 'defer',
     3131                                        'in_footer' => true,
     3132                                ),
     3133                                'expect'             => '<script type="text/javascript" src="http://script-a.js" id="script-a-js" defer="defer" data-wp-strategy="defer"></script>
     3134<script type="text/javascript" src="http://script-b.js" id="script-b-js" defer="defer" data-wp-strategy="defer"></script>
     3135',
     3136                                'expect_a_in_footer' => false,
     3137                                'expect_b_in_footer' => true,
     3138                        ),
     3139                        'b in header and deferred'      => array(
     3140                                'b_in_footer'        => array( 'strategy' => 'defer' ),
     3141                                'expect'             => '<script type="text/javascript" src="http://script-a.js" id="script-a-js" defer="defer" data-wp-strategy="defer"></script>
     3142<script type="text/javascript" src="http://script-b.js" id="script-b-js" defer="defer" data-wp-strategy="defer"></script>
     3143',
     3144                                'expect_a_in_footer' => false,
     3145                                'expect_b_in_footer' => false,
     3146                        ),
     3147                );
     3148        }
    30683149}