Ticket #22249: 22249.3.diff
| File 22249.3.diff, 10.7 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/class.wp-scripts.php
195 195 if ( ! $src ) 196 196 return true; 197 197 198 $tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}"; 198 $attributes = $this->get_concatenated_script_attributes( $handle, $src ); 199 $tag = "{$cond_before}<script$attributes></script>\n{$cond_after}"; 199 200 200 201 /** 201 202 * Filter the HTML script tag of an enqueued script. … … 218 219 } 219 220 220 221 /** 222 * Get attributes for the script tag. 223 * 224 * @since 4.0.0 225 * 226 * @param string $handle Script registered handle. 227 * @param string $src Script registered src. 228 * @return array Attribues. 229 */ 230 public function get_script_attributes( $handle, $src ) { 231 $default_attributes = array( 232 'type' => 'text/javascript', 233 'src' => $src 234 ); 235 236 $attributes = isset( $this->registered[ $handle ]->args['attributes'] ) 237 ? (array) $this->registered[ $handle ]->args['attributes'] 238 : array(); 239 240 $attributes = wp_parse_args( $attributes, $default_attributes ); 241 return $attributes; 242 } 243 244 245 /** 246 * Get Concatenated element attributes for the script tag. 247 * 248 * @since 4.5.0 249 * 250 * @param string $handle Script registered handle 251 * @param string $src Script registered src 252 * @return string Concatenated attributes string 253 */ 254 public function get_concatenated_script_attributes( $handle, $src ) { 255 $attributes = $this->get_script_attributes( $handle, $src ); 256 257 /** 258 * Filter the script loader attributes. 259 * 260 * @since 4.5.0 261 * 262 * @param array $attributes Array of script tag attributes. 263 * @param string $handle Script handle. 264 * @param string $src Script loader source path. 265 */ 266 $attributes = apply_filters( 'script_loader_attributes', $attributes, $handle, $src ); 267 // Ensure source is set. 268 $attributes['src'] = isset( $attributes['src'] ) ? $attributes['src'] : $src; 269 270 $concat_attributes = ''; 271 foreach ( $attributes as $attribute => $attribute_value ) { 272 if ( ! is_null( $attribute ) && ! is_null( $attribute_value ) ) { 273 $concat_attributes .= sprintf( " %s='%s'", esc_attr( $attribute ), esc_attr( $attribute_value ) ); 274 } 275 } 276 277 return $concat_attributes; 278 } 279 280 /** 221 281 * Localizes a script, only if the script has already been added 222 282 * 223 283 * @param string $handle -
src/wp-includes/functions.wp-scripts.php
95 95 * 96 96 * @since 2.6.0 97 97 * @since 4.3.0 A return value was added. 98 * @since 4.5.0 The sixth argument was added. 98 99 * 99 100 * @param string $handle Name of the script. Should be unique. 100 101 * @param string $src Path to the script from the WordPress root directory. Example: '/js/myscript.js'. … … 106 107 * If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'. 107 108 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>. 108 109 * Default 'false'. Accepts 'false' or 'true'. 110 * @param array $args { 111 * Optional script arguments. 112 * 113 * @type array $attributes Array of script tag attributes. 114 * Default: array( 'type' => 'text/javascript' ) 115 * } 109 116 * @return bool Whether the script has been registered. True on success, false on failure. 110 117 */ 111 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {118 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false, $attributes = array() ) { 112 119 $wp_scripts = wp_scripts(); 113 120 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 114 121 115 $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); 122 $attributes = wp_parse_args( $attributes, array( 'type' => 'text/javascript' ) ); 123 124 $registered = $wp_scripts->add( $handle, $src, $deps, $ver, array( 'attributes' => $attributes ) ); 116 125 if ( $in_footer ) { 117 126 $wp_scripts->add_data( $handle, 'group', 1 ); 118 127 } … … 211 220 * @see WP_Dependencies::enqueue() 212 221 * 213 222 * @since 2.6.0 223 * @since 4.5.0 The sixth argument was added. 214 224 * 215 225 * @param string $handle Name of the script. 216 226 * @param string|bool $src Path to the script from the root directory of WordPress. Example: '/js/myscript.js'. … … 220 230 * and so should be included if a version number is available and makes sense for the script. 221 231 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>. 222 232 * Default 'false'. Accepts 'false' or 'true'. 233 * @param array $args { 234 * Optional script arguments. 235 * 236 * @type array $attributes Array of script tag attributes. 237 * Default: array( 'type' => 'text/javascript' ) 238 * } 223 239 */ 224 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {240 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false, $attributes = array() ) { 225 241 $wp_scripts = wp_scripts(); 226 242 227 243 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 228 244 245 $attributes = wp_parse_args( $attributes, array( 'type' => 'text/javascript' ) ); 229 246 230 247 if ( $src || $in_footer ) { 231 248 $_handle = explode( '?', $handle ); 232 249 233 250 if ( $src ) { 234 $wp_scripts->add( $_handle[0], $src, $deps, $ver );251 $wp_scripts->add( $_handle[0], $src, $deps, $ver, array( 'attributes' => $attributes ) ); 235 252 } 236 253 237 254 if ( $in_footer ) { -
tests/phpunit/tests/dependencies/getConcatenatedScriptAttributes.php
1 <?php 2 /** 3 * @group dependencies 4 * @group scripts 5 */ 6 class Tests_Dependencies_GetConcatenatedScriptAttributes extends WP_UnitTestCase { 7 var $old_wp_scripts; 8 9 function setUp() { 10 parent::setUp(); 11 $this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null; 12 remove_action( 'wp_default_scripts', 'wp_default_scripts' ); 13 $GLOBALS['wp_scripts'] = new WP_Scripts(); 14 $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' ); 15 } 16 17 function tearDown() { 18 $GLOBALS['wp_scripts'] = $this->old_wp_scripts; 19 add_action( 'wp_default_scripts', 'wp_default_scripts' ); 20 parent::tearDown(); 21 } 22 23 /** 24 * @ticket 22249 25 */ 26 function test_get_concatenated_script_attributes_with_defaults() { 27 global $wp_scripts; 28 wp_register_script( 'cool-script', '/cool-script.js', array(), '1' ); 29 30 $actual = $wp_scripts->get_concatenated_script_attributes( 'cool-script', '/cool-script.js' ); 31 $expected = " type='text/javascript' src='/cool-script.js'"; 32 $this->assertEquals( $expected, $actual ); 33 } 34 35 /** 36 * @ticket 22249 37 */ 38 function test_get_concatenated_script_attributes_with_arbitrary_attributes_added() { 39 global $wp_scripts; 40 wp_register_script( 'cool-script', '/cool-script.js', array(), '1', false, array( 'async' => 'async' ) ); 41 42 $actual = $wp_scripts->get_concatenated_script_attributes( 'cool-script', '/cool-script.js' ); 43 $expected = " type='text/javascript' src='/cool-script.js' async='async'"; 44 $this->assertEquals( $expected, $actual ); 45 } 46 47 } -
tests/phpunit/tests/dependencies/getScriptAttributes.php
1 <?php 2 /** 3 * @group dependencies 4 * @group scripts 5 */ 6 class Tests_Dependencies_GetScriptAttributes extends WP_UnitTestCase { 7 var $old_wp_scripts; 8 9 function setUp() { 10 parent::setUp(); 11 $this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null; 12 remove_action( 'wp_default_scripts', 'wp_default_scripts' ); 13 $GLOBALS['wp_scripts'] = new WP_Scripts(); 14 $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' ); 15 } 16 17 function tearDown() { 18 $GLOBALS['wp_scripts'] = $this->old_wp_scripts; 19 add_action( 'wp_default_scripts', 'wp_default_scripts' ); 20 parent::tearDown(); 21 } 22 23 /** 24 * @ticket 22249 25 */ 26 function test_get_script_attributes_with_defaults() { 27 global $wp_scripts; 28 wp_register_script( 'cool-script', '/cool-script.js' ); 29 30 $expected_attributes = array( 31 'type' => 'text/javascript', 32 'src' => '/cool-script.js', 33 ); 34 $actual_attributes = $wp_scripts->get_script_attributes( 'cool-script', '/cool-script.js' ); 35 $this->assertEquals( $expected_attributes, $actual_attributes ); 36 } 37 38 /** 39 * @ticket 22249 40 */ 41 function test_get_script_attributes_with_arbitrary_attributes_added() { 42 global $wp_scripts; 43 wp_register_script( 'cool-script', '/cool-script.js', array(), '1', false, array( 'async' => 'async' ) ); 44 45 $expected_attributes = array( 46 'type' => 'text/javascript', 47 'src' => '/cool-script.js', 48 'async' => 'async', 49 ); 50 $actual_attributes = $wp_scripts->get_script_attributes( 'cool-script', '/cool-script.js' ); 51 $this->assertEquals( $expected_attributes, $actual_attributes ); 52 } 53 54 } -
tests/phpunit/tests/dependencies/scripts.php
266 266 $this->assertEquals( $expected_header, $header ); 267 267 $this->assertEquals( $expected_footer, $footer ); 268 268 } 269 270 /** 271 * @ticket 22249 272 */ 273 function test_wp_register_script_should_allow_arbitrary_element_attributes() { 274 wp_register_script( 'cool-script', '/cool-script.js', array(), '1', false, array( 'async' => 'async' ) ); 275 276 wp_enqueue_script( 'cool-script' ); 277 278 $header = get_echo( 'wp_print_head_scripts' ); 279 $expected_header = "<script type='text/javascript' src='/cool-script.js?ver=1' async='async'></script>\n"; 280 281 $this->assertEquals( $expected_header, $header ); 282 } 283 284 /** 285 * @ticket 22249 286 */ 287 function test_wp_enqueue_script_should_allow_arbitrary_element_attributes() { 288 wp_enqueue_script( 'cool-script', '/cool-script.js', array(), '1', false, array( 'async' => 'async' ) ); 289 290 $header = get_echo( 'wp_print_head_scripts' ); 291 $expected_header = "<script type='text/javascript' src='/cool-script.js?ver=1' async='async'></script>\n"; 292 293 $this->assertEquals( $expected_header, $header ); 294 } 295 269 296 }