Ticket #14853: 14853.diff
| File 14853.diff, 8.7 KB (added by , 11 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 52c6824..6278c5a 100644
class WP_Scripts extends WP_Dependencies { 145 145 * @param string $handle Script handle. 146 146 */ 147 147 $srce = apply_filters( 'script_loader_src', $src, $handle ); 148 149 $this->print_code .= $this->print_inline_script( $handle, 'before', false ); 150 148 151 if ( $this->in_default_dir( $srce ) && ! $conditional ) { 149 152 $this->print_code .= $this->print_extra_script( $handle, false ); 150 153 $this->concat .= "$handle,"; … … class WP_Scripts extends WP_Dependencies { 154 157 $this->ext_handles .= "$handle,"; 155 158 $this->ext_version .= "$handle$ver"; 156 159 } 160 161 $this->print_code .= $this->print_inline_script( $handle, 'after', false ); 157 162 } 158 163 159 164 $has_conditional_data = $conditional && $this->get_data( $handle, 'data' ); … … class WP_Scripts extends WP_Dependencies { 194 199 */ 195 200 $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src ); 196 201 202 $before_handle = $this->print_inline_script( $handle, 'before', false ); 203 $after_handle = $this->print_inline_script( $handle, 'after', false ); 204 197 205 if ( $this->do_concat ) { 206 207 if ( $before_handle ) { 208 $this->print_html .= sprintf( "<script id='%s-inline-js' type='text/javascript'>\n%s\n</script>\n", esc_attr( $handle ), $before_handle ); 209 } 210 198 211 $this->print_html .= $tag; 212 213 if ( $after_handle ) { 214 $this->print_html .= sprintf( "<script id='%s-inline-js' type='text/javascript'>\n%s\n</script>\n", esc_attr( $handle ), $after_handle ); 215 } 199 216 } else { 217 $this->print_inline_script( $handle, 'before' ); 200 218 echo $tag; 219 $this->print_inline_script( $handle, 'after' ); 220 } 221 222 return true; 223 } 224 225 /** 226 * Add extra code to a registered script. 227 * 228 * @since 4.5.0 229 * 230 * @param string $handle Name of the script to add the inline script to. Must be lowercase. 231 * @param string $data String containing the javascript to be added. 232 * @param string $position Optional. Whether to add the inline script before the handle 233 * or after. Default 'after'. 234 * 235 * @return bool True on success, false on failure. 236 */ 237 public function add_inline_script( $handle, $data, $position = 'after' ) { 238 if ( ! $data ) { 239 return false; 240 } 241 242 if ( 'after' !== $position ) { 243 $position = 'before'; 244 } 245 246 $script = (array) $this->get_data( $handle, $position ); 247 $script[] = $data; 248 249 return $this->add_data( $handle, $position, $script ); 250 } 251 252 /** 253 * Print inline scripts registered for a specific handle. 254 * 255 * @param string $handle Name of the script to add the inline script to. Must be lowercase. 256 * @param string $position Optional. Whether to add the inline script before the handle 257 * or after. Default 'after'. 258 * @param bool $echo Optional. Whether to echo the script instead of just returning it. 259 * Default true. 260 * @return bool True on success, false otherwise. 261 */ 262 public function print_inline_script( $handle, $position = 'after', $echo = true ) { 263 $output = $this->get_data( $handle, $position ); 264 265 if ( empty( $output ) ) { 266 return false; 267 } 268 269 $output = trim( implode( "\n", $output ), "\n" ); 270 271 if ( $echo ) { 272 printf( "<script id='%s-inline-js-%s' type='text/javascript'>\n%s\n</script>\n", esc_attr( $handle ), $position, $output ); 201 273 } 202 274 203 275 return true; -
src/wp-includes/functions.wp-scripts.php
diff --git src/wp-includes/functions.wp-scripts.php src/wp-includes/functions.wp-scripts.php index a3ac7e6..4d6b732 100644
function wp_print_scripts( $handles = false ) { 86 86 } 87 87 88 88 /** 89 * Add extra code to a registered script. 90 * 91 * Code will only be added if the script in already in the queue. 92 * Accepts a string $data containing the Code. If two or more code blocks 93 * are added to the same script $handle, they will be printed in the order 94 * they were added, i.e. the latter added code can redeclare the previous. 95 * 96 * @since 4.5.0 97 * 98 * @see WP_Scripts::add_inline_script() 99 * 100 * @param string $handle Name of the script to add the inline script to. Must be lowercase. 101 * @param string $data String containing the javascript to be added. 102 * @param string $position Optional. Whether to add the inline script before the handle 103 * or after. Default 'after'. 104 * @return bool True on success, false on failure. 105 */ 106 function wp_add_inline_script( $handle, $data, $position = 'after' ) { 107 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 108 109 if ( false !== stripos( $data, '</script>' ) ) { 110 _doing_it_wrong( __FUNCTION__, __( 'Do not pass script tags to wp_add_inline_script().' ), '4.1.3' ); 111 $data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) ); 112 } 113 114 return wp_scripts()->add_inline_script( $handle, $data, $position ); 115 } 116 117 118 /** 89 119 * Register a new script. 90 120 * 91 121 * Registers a script to be linked later using the wp_enqueue_script() function. -
tests/phpunit/tests/dependencies/scripts.php
diff --git tests/phpunit/tests/dependencies/scripts.php tests/phpunit/tests/dependencies/scripts.php index 73680df..0fec6a3 100644
class Tests_Dependencies_Scripts extends WP_UnitTestCase { 166 166 $this->assertFalse( wp_register_script( 'duplicate-handler', 'http://example.com' ) ); 167 167 } 168 168 169 /** 170 * @ticket 14853 171 */ 172 function test_wp_add_inline_script_returns_bool() { 173 $this->assertFalse( wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ) ); 174 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 175 $this->assertTrue( wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ) ); 176 } 177 178 /** 179 * @ticket 14853 180 */ 181 function test_wp_add_inline_script_unknown_handle() { 182 $this->assertFalse( wp_add_inline_script( 'test-invalid', 'console.log("before");', 'before' ) ); 183 $this->assertEquals( '', get_echo( 'wp_print_scripts' ) ); 184 } 185 186 /** 187 * @ticket 14853 188 */ 189 function test_wp_add_inline_script_before() { 190 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 191 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 192 193 $expected = "<script id='test-example-inline-js-before' type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 194 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 195 196 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 197 } 198 199 /** 200 * @ticket 14853 201 */ 202 function test_wp_add_inline_script_after() { 203 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 204 wp_add_inline_script( 'test-example', 'console.log("after");' ); 205 206 $expected = "<script type='text/javascript' src='http://example.com'></script>\n"; 207 $expected .= "<script id='test-example-inline-js-after' type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 208 209 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 210 } 211 212 /** 213 * @ticket 14853 214 */ 215 function test_wp_add_inline_script_before_and_after() { 216 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 217 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 218 wp_add_inline_script( 'test-example', 'console.log("after");' ); 219 220 $expected = "<script id='test-example-inline-js-before' type='text/javascript'>\nconsole.log(\"before\");\n</script>\n"; 221 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 222 $expected .= "<script id='test-example-inline-js-after' type='text/javascript'>\nconsole.log(\"after\");\n</script>\n"; 223 224 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 225 } 226 227 /** 228 * @ticket 14853 229 */ 230 function test_wp_add_inline_script_multiple() { 231 wp_enqueue_script( 'test-example', 'example.com', array(), null ); 232 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 233 wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); 234 wp_add_inline_script( 'test-example', 'console.log("after");' ); 235 wp_add_inline_script( 'test-example', 'console.log("after");' ); 236 237 $expected = "<script id='test-example-inline-js-before' type='text/javascript'>\nconsole.log(\"before\");\nconsole.log(\"before\");\n</script>\n"; 238 $expected .= "<script type='text/javascript' src='http://example.com'></script>\n"; 239 $expected .= "<script id='test-example-inline-js-after' type='text/javascript'>\nconsole.log(\"after\");\nconsole.log(\"after\");\n</script>\n"; 240 241 $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); 242 } 169 243 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)