WordPress.org

Make WordPress Core

Changeset 37171


Ignore:
Timestamp:
04/10/2016 03:32:47 AM (4 years ago)
Author:
jeremyfelt
Message:

Ensure consistent dependency order when using wp_add_inline_script()

This disables the concatenation of remaining enqueued scripts once wp_add_inline_script() is invoked, which allows us to reliably print these scripts and their before/after inline scripts in the desired order.

Props gitlost, azaozz, swisspidy, ocean90.
Fixes #36392.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class.wp-scripts.php

    r36917 r37171  
    9292     */
    9393    public $print_html = '';
    94 
    95     /**
    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 = '';
    10394
    10495    /**
     
    305296            $srce = apply_filters( 'script_loader_src', $src, $handle );
    306297
    307             if ( $before_handle && ! $conditional ) {
    308                 $this->print_html_before .= $before_handle;
    309             }
    310 
    311             if ( $this->in_default_dir( $srce ) && ! $conditional && ! $after_handle ) {
     298            if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) {
     299                $this->do_concat = false;
     300
     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,";
     
    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;
     
    593582        $this->concat_version = '';
    594583        $this->print_html = '';
    595         $this->print_html_before = '';
    596584        $this->ext_version = '';
    597585        $this->ext_handles = '';
  • trunk/src/wp-includes/script-loader.php

    r37164 r37171  
    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 );
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r36871 r37171  
    450450        wp_add_inline_script( 'two', 'console.log("before two");', 'before' );
    451451
    452         wp_print_scripts();
    453         $print_scripts = get_echo( '_print_scripts' );
    454 
    455         $ver = get_bloginfo( 'version' );
    456         $expected  = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n";
     452        $ver = get_bloginfo( 'version' );
     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";
    459 
    460         $this->assertEquals( $expected, $print_scripts );
     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";
     458
     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
     
    478501        wp_add_inline_script( 'three', 'console.log("after three");' );
    479502
    480         wp_print_scripts();
    481         $print_scripts = get_echo( '_print_scripts' );
    482 
    483         $ver = get_bloginfo( 'version' );
    484         $expected  = "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
     503        $ver = get_bloginfo( 'version' );
     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";
    489 
    490         $this->assertEquals( $expected, $print_scripts );
    491     }
    492 
    493     /**
    494      * @ticket 14853
    495      */
    496     public function test_wp_add_inline_script_concat_with_conditional() {
     509        $expected .= "<script type='text/javascript' src='/directory/four.js?ver={$ver}'></script>\n";
     510
     511        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     512    }
     513
     514    /**
     515     * @ticket 14853
     516     */
     517    public function test_wp_add_inline_script_after_and_before_with_concat_and_conditional() {
    497518        global $wp_scripts;
    498519
     
    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 );
    521     }
    522 
     540        $this->assertEquals( $expected, $wp_scripts->print_html );
     541        $this->assertTrue( $wp_scripts->do_concat );
     542    }
     543
     544    /**
     545     * @ticket 36392
     546     */
     547    public function test_wp_add_inline_script_after_with_concat_and_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_after_with_concat_and_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_with_concat_and_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     * @ticket 36392
     681     */
     682    public function test_wp_add_inline_script_after_for_core_scripts_with_concat_is_limited_and_falls_back_to_no_concat() {
     683        global $wp_scripts;
     684
     685        $wp_scripts->do_concat    = true;
     686        $wp_scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' ); // Default dirs as in wp-includes/script-loader.php
     687
     688        wp_enqueue_script( 'one', '/wp-includes/js/script.js' );
     689        wp_enqueue_script( 'two', '/wp-includes/js/script2.js', array( 'one' ) );
     690        wp_add_inline_script( 'one', 'console.log("after one");', 'after' );
     691        wp_enqueue_script( 'three', '/wp-includes/js/script3.js' );
     692        wp_enqueue_script( 'four', '/wp-includes/js/script4.js' );
     693
     694        $ver = get_bloginfo( 'version' );
     695        $expected  = "<script type='text/javascript' src='/wp-includes/js/script.js?ver={$ver}'></script>\n";
     696        $expected .= "<script type='text/javascript'>\nconsole.log(\"after one\");\n</script>\n";
     697        $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js?ver={$ver}'></script>\n";
     698        $expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}'></script>\n";
     699        $expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}'></script>\n";
     700
     701        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     702    }
     703
     704    /**
     705     * @ticket 36392
     706     */
     707    public function test_wp_add_inline_script_before_third_core_script_prints_two_concat_scripts() {
     708        global $wp_scripts;
     709
     710        $wp_scripts->do_concat    = true;
     711        $wp_scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' ); // Default dirs as in wp-includes/script-loader.php
     712
     713        wp_enqueue_script( 'one', '/wp-includes/js/script.js' );
     714        wp_enqueue_script( 'two', '/wp-includes/js/script2.js', array( 'one' ) );
     715        wp_enqueue_script( 'three', '/wp-includes/js/script3.js' );
     716        wp_add_inline_script( 'three', 'console.log("before three");', 'before' );
     717        wp_enqueue_script( 'four', '/wp-includes/js/script4.js' );
     718
     719        $ver = get_bloginfo( 'version' );
     720        $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5B%5D=one,two&amp;ver={$ver}'></script>\n";
     721        $expected .= "<script type='text/javascript'>\nconsole.log(\"before three\");\n</script>\n";
     722        $expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}'></script>\n";
     723        $expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}'></script>\n";
     724
     725        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     726    }
    523727}
Note: See TracChangeset for help on using the changeset viewer.