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. git-svn-id: https://develop.svn.wordpress.org/trunk@37171 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
fc1f8db19a
commit
f98744ec86
@ -92,15 +92,6 @@ class WP_Scripts extends WP_Dependencies {
|
||||
*/
|
||||
public $print_html = '';
|
||||
|
||||
/**
|
||||
* HTML to print before the script handle.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $print_html_before = '';
|
||||
|
||||
/**
|
||||
* Holds inline code if concatenation is enabled.
|
||||
*
|
||||
@ -304,11 +295,13 @@ class WP_Scripts extends WP_Dependencies {
|
||||
*/
|
||||
$srce = apply_filters( 'script_loader_src', $src, $handle );
|
||||
|
||||
if ( $before_handle && ! $conditional ) {
|
||||
$this->print_html_before .= $before_handle;
|
||||
}
|
||||
if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) {
|
||||
$this->do_concat = false;
|
||||
|
||||
if ( $this->in_default_dir( $srce ) && ! $conditional && ! $after_handle ) {
|
||||
// Have to print the so-far concatenated scripts right away to maintain the right order.
|
||||
_print_scripts();
|
||||
$this->reset();
|
||||
} elseif ( $this->in_default_dir( $srce ) && ! $conditional ) {
|
||||
$this->print_code .= $this->print_extra_script( $handle, false );
|
||||
$this->concat .= "$handle,";
|
||||
$this->concat_version .= "$handle$ver";
|
||||
@ -363,11 +356,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
|
||||
|
||||
if ( $this->do_concat ) {
|
||||
if ( $after_handle ) {
|
||||
$this->print_html_before .= $tag;
|
||||
} else {
|
||||
$this->print_html .= $tag;
|
||||
}
|
||||
$this->print_html .= $tag;
|
||||
} else {
|
||||
echo $tag;
|
||||
}
|
||||
@ -592,7 +581,6 @@ class WP_Scripts extends WP_Dependencies {
|
||||
$this->concat = '';
|
||||
$this->concat_version = '';
|
||||
$this->print_html = '';
|
||||
$this->print_html_before = '';
|
||||
$this->ext_version = '';
|
||||
$this->ext_handles = '';
|
||||
}
|
||||
|
@ -998,10 +998,6 @@ function _print_scripts() {
|
||||
echo "</script>\n";
|
||||
}
|
||||
|
||||
if ( ! empty( $wp_scripts->print_html_before ) ) {
|
||||
echo $wp_scripts->print_html_before;
|
||||
}
|
||||
|
||||
$concat = str_split( $concat, 128 );
|
||||
$concat = 'load%5B%5D=' . implode( '&load%5B%5D=', $concat );
|
||||
|
||||
|
@ -449,15 +449,38 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
|
||||
wp_add_inline_script( 'one', 'console.log("before one");', 'before' );
|
||||
wp_add_inline_script( 'two', 'console.log("before two");', 'before' );
|
||||
|
||||
wp_print_scripts();
|
||||
$print_scripts = get_echo( '_print_scripts' );
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"before two\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n";
|
||||
|
||||
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 14853
|
||||
*/
|
||||
public function test_wp_add_inline_script_before_with_concat2() {
|
||||
global $wp_scripts;
|
||||
|
||||
$wp_scripts->do_concat = true;
|
||||
$wp_scripts->default_dirs = array( '/directory/' );
|
||||
|
||||
wp_enqueue_script( 'one', '/directory/one.js' );
|
||||
wp_enqueue_script( 'two', '/directory/two.js' );
|
||||
wp_enqueue_script( 'three', '/directory/three.js' );
|
||||
|
||||
wp_add_inline_script( 'one', 'console.log("before one");', 'before' );
|
||||
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"before two\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one,two,three&ver={$ver}'></script>\n";
|
||||
$expected = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n";
|
||||
|
||||
$this->assertEquals( $expected, $print_scripts );
|
||||
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -477,23 +500,21 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
|
||||
wp_add_inline_script( 'two', 'console.log("after two");' );
|
||||
wp_add_inline_script( 'three', 'console.log("after three");' );
|
||||
|
||||
wp_print_scripts();
|
||||
$print_scripts = get_echo( '_print_scripts' );
|
||||
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$expected = "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one&ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"after two\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"after three\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one,four&ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/directory/four.js?ver={$ver}'></script>\n";
|
||||
|
||||
$this->assertEquals( $expected, $print_scripts );
|
||||
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 14853
|
||||
*/
|
||||
public function test_wp_add_inline_script_concat_with_conditional() {
|
||||
public function test_wp_add_inline_script_after_and_before_with_concat_and_conditional() {
|
||||
global $wp_scripts;
|
||||
|
||||
$wp_scripts->do_concat = true;
|
||||
@ -516,8 +537,191 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
|
||||
wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' );
|
||||
|
||||
$this->assertEquals( $expected_localized, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEquals( $expected, $wp_scripts->print_html_before );
|
||||
$this->assertEquals( '', $wp_scripts->print_html );
|
||||
$this->assertEquals( $expected, $wp_scripts->print_html );
|
||||
$this->assertTrue( $wp_scripts->do_concat );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36392
|
||||
*/
|
||||
public function test_wp_add_inline_script_after_with_concat_and_core_dependency() {
|
||||
global $wp_scripts;
|
||||
|
||||
wp_default_scripts( $wp_scripts );
|
||||
|
||||
$wp_scripts->base_url = '';
|
||||
$wp_scripts->do_concat = true;
|
||||
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
|
||||
|
||||
wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
|
||||
wp_add_inline_script( 'test-example', 'console.log("after");' );
|
||||
|
||||
wp_print_scripts();
|
||||
$print_scripts = get_echo( '_print_scripts' );
|
||||
|
||||
$this->assertEquals( $expected, $print_scripts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36392
|
||||
*/
|
||||
public function test_wp_add_inline_script_after_with_concat_and_conditional_and_core_dependency() {
|
||||
global $wp_scripts;
|
||||
|
||||
wp_default_scripts( $wp_scripts );
|
||||
|
||||
$wp_scripts->base_url = '';
|
||||
$wp_scripts->do_concat = true;
|
||||
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n";
|
||||
$expected .= "<!--[if gte IE 9]>\n";
|
||||
$expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
|
||||
$expected .= "<![endif]-->\n";
|
||||
|
||||
wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
|
||||
wp_add_inline_script( 'test-example', 'console.log("after");' );
|
||||
wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' );
|
||||
|
||||
wp_print_scripts();
|
||||
$print_scripts = get_echo( '_print_scripts' );
|
||||
|
||||
$this->assertEquals( $expected, $print_scripts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36392
|
||||
*/
|
||||
public function test_wp_add_inline_script_before_with_concat_and_core_dependency() {
|
||||
global $wp_scripts;
|
||||
|
||||
wp_default_scripts( $wp_scripts );
|
||||
|
||||
$wp_scripts->base_url = '';
|
||||
$wp_scripts->do_concat = true;
|
||||
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate&ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
|
||||
|
||||
wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
|
||||
wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
|
||||
|
||||
wp_print_scripts();
|
||||
$print_scripts = get_echo( '_print_scripts' );
|
||||
|
||||
$this->assertEquals( $expected, $print_scripts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36392
|
||||
*/
|
||||
public function test_wp_add_inline_script_before_after_concat_with_core_dependency() {
|
||||
global $wp_scripts;
|
||||
|
||||
wp_default_scripts( $wp_scripts );
|
||||
|
||||
$wp_scripts->base_url = '';
|
||||
$wp_scripts->do_concat = true;
|
||||
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=jquery-core,jquery-migrate,wp-a11y&ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='http://example2.com'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
|
||||
|
||||
wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
|
||||
wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
|
||||
wp_enqueue_script( 'test-example2', 'http://example2.com', array( 'wp-a11y' ), null );
|
||||
wp_add_inline_script( 'test-example2', 'console.log("after");', 'after' );
|
||||
|
||||
wp_print_scripts();
|
||||
$print_scripts = get_echo( '_print_scripts' );
|
||||
|
||||
$this->assertEquals( $expected, $print_scripts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36392
|
||||
*/
|
||||
public function test_wp_add_inline_script_customize_dependency() {
|
||||
global $wp_scripts;
|
||||
|
||||
wp_default_scripts( $wp_scripts );
|
||||
|
||||
$wp_scripts->base_url = '';
|
||||
$wp_scripts->do_concat = true;
|
||||
|
||||
$expected_tail = "<![endif]-->\n";
|
||||
$expected_tail .= "<script type='text/javascript' src='/customize-dependency.js'></script>\n";
|
||||
$expected_tail .= "<script type='text/javascript'>\n";
|
||||
$expected_tail .= "tryCustomizeDependency()\n";
|
||||
$expected_tail .= "</script>\n";
|
||||
|
||||
$handle = 'customize-dependency';
|
||||
wp_enqueue_script( $handle, '/customize-dependency.js', array( 'customize-controls' ), null );
|
||||
wp_add_inline_script( $handle, 'tryCustomizeDependency()' );
|
||||
|
||||
wp_print_scripts();
|
||||
$print_scripts = get_echo( '_print_scripts' );
|
||||
|
||||
$tail = substr( $print_scripts, strrpos( $print_scripts, "<![endif]-->" ) );
|
||||
$this->assertEquals( $expected_tail, $tail );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36392
|
||||
*/
|
||||
public function test_wp_add_inline_script_after_for_core_scripts_with_concat_is_limited_and_falls_back_to_no_concat() {
|
||||
global $wp_scripts;
|
||||
|
||||
$wp_scripts->do_concat = true;
|
||||
$wp_scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' ); // Default dirs as in wp-includes/script-loader.php
|
||||
|
||||
wp_enqueue_script( 'one', '/wp-includes/js/script.js' );
|
||||
wp_enqueue_script( 'two', '/wp-includes/js/script2.js', array( 'one' ) );
|
||||
wp_add_inline_script( 'one', 'console.log("after one");', 'after' );
|
||||
wp_enqueue_script( 'three', '/wp-includes/js/script3.js' );
|
||||
wp_enqueue_script( 'four', '/wp-includes/js/script4.js' );
|
||||
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$expected = "<script type='text/javascript' src='/wp-includes/js/script.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"after one\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}'></script>\n";
|
||||
|
||||
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36392
|
||||
*/
|
||||
public function test_wp_add_inline_script_before_third_core_script_prints_two_concat_scripts() {
|
||||
global $wp_scripts;
|
||||
|
||||
$wp_scripts->do_concat = true;
|
||||
$wp_scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' ); // Default dirs as in wp-includes/script-loader.php
|
||||
|
||||
wp_enqueue_script( 'one', '/wp-includes/js/script.js' );
|
||||
wp_enqueue_script( 'two', '/wp-includes/js/script2.js', array( 'one' ) );
|
||||
wp_enqueue_script( 'three', '/wp-includes/js/script3.js' );
|
||||
wp_add_inline_script( 'three', 'console.log("before three");', 'before' );
|
||||
wp_enqueue_script( 'four', '/wp-includes/js/script4.js' );
|
||||
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5B%5D=one,two&ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript'>\nconsole.log(\"before three\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}'></script>\n";
|
||||
|
||||
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user