WordPress.org

Make WordPress Core

Ticket #36392: 36392.diff

File 36392.diff, 12.5 KB (added by swissspidy, 4 years 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 ce5469e..144bfce 100644
    class WP_Scripts extends WP_Dependencies { 
    9393        public $print_html = '';
    9494
    9595        /**
    96          * HTML to print before the script handle.
    97          *
    98          * @since 4.5.0
    99          * @access public
    100          * @var string
    101          */
    102         public $print_html_before = '';
    103 
    104         /**
    10596         * Holds inline code if concatenation is enabled.
    10697         *
    10798         * @since 2.8.0
    class WP_Scripts extends WP_Dependencies { 
    304295                         */
    305296                        $srce = apply_filters( 'script_loader_src', $src, $handle );
    306297
    307                         if ( $before_handle && ! $conditional ) {
    308                                 $this->print_html_before .= $before_handle;
    309                         }
     298                        if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) {
     299                                $this->do_concat = false;
    310300
    311                         if ( $this->in_default_dir( $srce ) && ! $conditional && ! $after_handle ) {
     301                                // Have to print the so-far concatenated scripts right away to maintain the right order.
     302                                _print_scripts();
     303                                $this->reset();
     304                        } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) {
    312305                                $this->print_code .= $this->print_extra_script( $handle, false );
    313306                                $this->concat .= "$handle,";
    314307                                $this->concat_version .= "$handle$ver";
    class WP_Scripts extends WP_Dependencies { 
    363356                $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
    364357
    365358                if ( $this->do_concat ) {
    366                         if ( $after_handle ) {
    367                                 $this->print_html_before .= $tag;
    368                         } else {
    369                                 $this->print_html .= $tag;
    370                         }
     359                        $this->print_html .= $tag;
    371360                } else {
    372361                        echo $tag;
    373362                }
    class WP_Scripts extends WP_Dependencies { 
    592581                $this->concat = '';
    593582                $this->concat_version = '';
    594583                $this->print_html = '';
    595                 $this->print_html_before = '';
    596584                $this->ext_version = '';
    597585                $this->ext_handles = '';
    598586        }
  • src/wp-includes/script-loader.php

    diff --git src/wp-includes/script-loader.php src/wp-includes/script-loader.php
    index ab02bfd..66080a8 100644
    function _print_scripts() { 
    998998                        echo "</script>\n";
    999999                }
    10001000
    1001                 if ( ! empty( $wp_scripts->print_html_before ) ) {
    1002                         echo $wp_scripts->print_html_before;
    1003                 }
    1004 
    10051001                $concat = str_split( $concat, 128 );
    10061002                $concat = 'load%5B%5D=' . implode( '&load%5B%5D=', $concat );
    10071003
  • tests/phpunit/tests/dependencies/scripts.php

    diff --git tests/phpunit/tests/dependencies/scripts.php tests/phpunit/tests/dependencies/scripts.php
    index a852503..b7df93f 100644
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    449449                wp_add_inline_script( 'one', 'console.log("before one");', 'before' );
    450450                wp_add_inline_script( 'two', 'console.log("before two");', 'before' );
    451451
    452                 wp_print_scripts();
    453                 $print_scripts = get_echo( '_print_scripts' );
    454 
    455452                $ver = get_bloginfo( 'version' );
    456                 $expected  = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n";
     453                $expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n";
     454                $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n";
    457455                $expected .= "<script type='text/javascript'>\nconsole.log(\"before two\");\n</script>\n";
    458                 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5B%5D=one,two,three&amp;ver={$ver}'></script>\n";
     456                $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
     457                $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n";
    459458
    460                 $this->assertEquals( $expected, $print_scripts );
     459                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     460        }
     461
     462        /**
     463         * @ticket 14853
     464         */
     465        public function test_wp_add_inline_script_before_with_concat2() {
     466                global $wp_scripts;
     467
     468                $wp_scripts->do_concat = true;
     469                $wp_scripts->default_dirs = array( '/directory/' );
     470
     471                wp_enqueue_script( 'one', '/directory/one.js' );
     472                wp_enqueue_script( 'two', '/directory/two.js' );
     473                wp_enqueue_script( 'three', '/directory/three.js' );
     474
     475                wp_add_inline_script( 'one', 'console.log("before one");', 'before' );
     476
     477                $ver = get_bloginfo( 'version' );
     478                $expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n";
     479                $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n";
     480                $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
     481                $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n";
     482
     483                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
    461484        }
    462485
    463486        /**
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    477500                wp_add_inline_script( 'two', 'console.log("after two");' );
    478501                wp_add_inline_script( 'three', 'console.log("after three");' );
    479502
    480                 wp_print_scripts();
    481                 $print_scripts = get_echo( '_print_scripts' );
    482 
    483503                $ver = get_bloginfo( 'version' );
    484                 $expected  = "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
     504                $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5B%5D=one&amp;ver={$ver}'></script>\n";
     505                $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
    485506                $expected .= "<script type='text/javascript'>\nconsole.log(\"after two\");\n</script>\n";
    486507                $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n";
    487508                $expected .= "<script type='text/javascript'>\nconsole.log(\"after three\");\n</script>\n";
    488                 $expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5B%5D=one,four&amp;ver={$ver}'></script>\n";
     509                $expected .= "<script type='text/javascript' src='/directory/four.js?ver={$ver}'></script>\n";
    489510
    490                 $this->assertEquals( $expected, $print_scripts );
     511                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
    491512        }
    492513
    493514        /**
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    516537                wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' );
    517538
    518539                $this->assertEquals( $expected_localized, get_echo( 'wp_print_scripts' ) );
    519                 $this->assertEquals( $expected, $wp_scripts->print_html_before );
    520                 $this->assertEquals( '', $wp_scripts->print_html );
     540                $this->assertEquals( $expected, $wp_scripts->print_html );
     541                $this->assertTrue( $wp_scripts->do_concat );
    521542        }
    522543
     544        /**
     545         * @ticket 36392
     546         */
     547        public function test_wp_add_inline_script_concat_with_core_dependency() {
     548                global $wp_scripts;
     549
     550                wp_default_scripts( $wp_scripts );
     551
     552                $wp_scripts->base_url = '';
     553                $wp_scripts->do_concat = true;
     554
     555                $ver = get_bloginfo( 'version' );
     556                $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5B%5D=jquery-core,jquery-migrate&amp;ver={$ver}'></script>\n";
     557                $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     558                $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     559
     560                wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
     561                wp_add_inline_script( 'test-example', 'console.log("after");' );
     562
     563                wp_print_scripts();
     564                $print_scripts = get_echo( '_print_scripts' );
     565
     566                $this->assertEquals( $expected, $print_scripts );
     567        }
     568
     569        /**
     570         * @ticket 36392
     571         */
     572        public function test_wp_add_inline_script_concat_with_conditional_and_core_dependency() {
     573                global $wp_scripts;
     574
     575                wp_default_scripts( $wp_scripts );
     576
     577                $wp_scripts->base_url  = '';
     578                $wp_scripts->do_concat = true;
     579
     580                $ver      = get_bloginfo( 'version' );
     581                $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5B%5D=jquery-core,jquery-migrate&amp;ver={$ver}'></script>\n";
     582                $expected .= "<!--[if gte IE 9]>\n";
     583                $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     584                $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     585                $expected .= "<![endif]-->\n";
     586
     587                wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
     588                wp_add_inline_script( 'test-example', 'console.log("after");' );
     589                wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' );
     590
     591                wp_print_scripts();
     592                $print_scripts = get_echo( '_print_scripts' );
     593
     594                $this->assertEquals( $expected, $print_scripts );
     595        }
     596
     597        /**
     598         * @ticket 36392
     599         */
     600        public function test_wp_add_inline_script_before_concat_with_core_dependency() {
     601                global $wp_scripts;
     602
     603                wp_default_scripts( $wp_scripts );
     604
     605                $wp_scripts->base_url  = '';
     606                $wp_scripts->do_concat = true;
     607
     608                $ver      = get_bloginfo( 'version' );
     609                $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5B%5D=jquery-core,jquery-migrate&amp;ver={$ver}'></script>\n";
     610                $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
     611                $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     612
     613                wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
     614                wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
     615
     616                wp_print_scripts();
     617                $print_scripts = get_echo( '_print_scripts' );
     618
     619                $this->assertEquals( $expected, $print_scripts );
     620        }
     621
     622        /**
     623         * @ticket 36392
     624         */
     625        public function test_wp_add_inline_script_before_after_concat_with_core_dependency() {
     626                global $wp_scripts;
     627
     628                wp_default_scripts( $wp_scripts );
     629
     630                $wp_scripts->base_url  = '';
     631                $wp_scripts->do_concat = true;
     632
     633                $ver      = get_bloginfo( 'version' );
     634                $expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5B%5D=jquery-core,jquery-migrate,wp-a11y&amp;ver={$ver}'></script>\n";
     635                $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
     636                $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     637                $expected .= "<script type='text/javascript' src='http://example2.com'></script>\n";
     638                $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     639
     640                wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
     641                wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
     642                wp_enqueue_script( 'test-example2', 'http://example2.com', array( 'wp-a11y' ), null );
     643                wp_add_inline_script( 'test-example2', 'console.log("after");', 'after' );
     644
     645                wp_print_scripts();
     646                $print_scripts = get_echo( '_print_scripts' );
     647
     648                $this->assertEquals( $expected, $print_scripts );
     649        }
     650
     651        /**
     652         * @ticket 36392
     653         */
     654        public function test_wp_add_inline_script_customize_dependency() {
     655                global $wp_scripts;
     656
     657                wp_default_scripts( $wp_scripts );
     658
     659                $wp_scripts->base_url  = '';
     660                $wp_scripts->do_concat = true;
     661
     662                $expected_tail = "<![endif]-->\n";
     663                $expected_tail .= "<script type='text/javascript' src='/customize-dependency.js'></script>\n";
     664                $expected_tail .= "<script type='text/javascript'>\n";
     665                $expected_tail .= "tryCustomizeDependency()\n";
     666                $expected_tail .= "</script>\n";
     667
     668                $handle = 'customize-dependency';
     669                wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null );
     670                wp_add_inline_script( $handle, 'tryCustomizeDependency()' );
     671
     672                wp_print_scripts();
     673                $print_scripts = get_echo( '_print_scripts' );
     674
     675                $tail = substr( $print_scripts, strrpos( $print_scripts, "<![endif]-->" ) );
     676                $this->assertEquals( $expected_tail, $tail );
     677        }
     678
     679        /**
     680         * Limitation: One can't add inline script to scripts in the $default_dirs (which in standard use are
     681         * '/wp-admin/js/' and '/wp-includes/js/') if those scripts have dependencies on each other.
     682         *
     683         * @ticket 36392
     684         */
     685        public function test_wp_add_inline_script_limitation() {
     686                global $wp_scripts;
     687
     688                $wp_scripts->do_concat    = true;
     689                $wp_scripts->default_dirs = array(
     690                        '/wp-admin/js/',
     691                        '/wp-includes/js/',
     692                ); // Default dirs as in wp-includes/script-loader.php
     693
     694                wp_enqueue_script( 'one', '/wp-includes/js/script.js' );
     695                wp_enqueue_script( 'two', '/wp-includes/js/script2.js', array( 'one' ) );
     696                wp_add_inline_script( 'one', 'console.log("after one");', 'after' );
     697
     698                $ver = get_bloginfo( 'version' );
     699
     700                $expected = "<script type='text/javascript' src='/wp-includes/js/script.js?ver={$ver}'></script>\n";
     701                $expected .= "<script type='text/javascript'>\nconsole.log(\"after one\");\n</script>\n";
     702                $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js?ver={$ver}'></script>\n";
     703
     704                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     705        }
    523706}