Ticket #22249: 22249.2.diff
File 22249.2.diff, 6.8 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class.wp-scripts.php
190 190 if ( ! $src ) 191 191 return true; 192 192 193 $tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}"; 193 $attributes = $this->get_script_attributes( $handle, $src ); 194 $tag = "{$cond_before}<script$attributes></script>\n{$cond_after}"; 194 195 195 196 /** 196 197 * Filter the HTML script tag of an enqueued script. … … 213 214 } 214 215 215 216 /** 217 * Concatenates attributes for the script tag 218 * 219 * @since 4.0.0 220 * 221 * @param string $handle Script registered handle 222 * @param string $src Script registered src 223 * 224 * @return string Concatenated attributes string 225 */ 226 public function get_script_attributes( $handle, $src ) { 227 228 $default_attributes = array( 229 'type' => 'text/javascript', 230 'src' => $src 231 ); 232 233 $attributes = isset( $this->registered[ $handle ]->args['attributes'] ) 234 ? (array) $this->registered[ $handle ]->args['attributes'] 235 : array(); 236 237 $attributes = wp_parse_args( $attributes, $default_attributes ); 238 239 /** 240 * Filter the script loader attributes. 241 * 242 * @since 4.1.0 243 * 244 * @param array $attributes Array of script tag attributes. 245 * @param string $handle Script handle. 246 * @param string $src Script loader source path. 247 */ 248 $attributes = apply_filters( 'script_loader_attributes', $attributes, $handle, $src ); 249 // Ensure source is set. 250 $attributes['src'] = isset( $attributes['src'] ) ? $attributes['src'] : $src; 251 252 $concat_attributes = ''; 253 foreach ( $attributes as $attribute => $attribute_value ) { 254 if ( ! is_null( $attribute ) && ! is_null( $attribute_value ) ) { 255 $concat_attributes .= ' '. esc_attr( $attribute ) .'="'. esc_attr( $attribute_value ) .'"'; 256 } 257 } 258 259 return $concat_attributes; 260 } 261 262 /** 216 263 * Localizes a script, only if the script has already been added 217 264 * 218 265 * @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 fifth argument was changed to an array of options. 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'. … … 104 105 * to end of path as a query string. If no version is specified or set to false, a version 105 106 * number is automatically added equal to current installed WordPress version. 106 107 * If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'. 107 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>. 108 * Default 'false'. Accepts 'false' or 'true'. 108 * @param array $args { 109 * Optional script arguments. 110 * 111 * @type bool $in_footer Whether to enqueue the script before </head> or before </body>. 112 * Default 'false'. Accepts 'false' or 'true'. 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, $args = 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 ); 116 if ( $in_footer ) { 122 // Back-compat. If the fifth argument is not an array, it's `in_footer`. 123 $args = ! is_array( $args ) ? array( 'in_footer' => (bool) $args ) : $args; 124 $args = wp_parse_args( $args, array( 125 'in_footer' => false, 126 'attributes' => array(), 127 ) ); 128 129 $attributes = wp_parse_args( $args['attributes'], array( 'type' => 'text/javascript' ) ); 130 131 $registered = $wp_scripts->add( $handle, $src, $deps, $ver, array( 'attributes' => $attributes ) ); 132 if ( $args['in_footer'] ) { 117 133 $wp_scripts->add_data( $handle, 'group', 1 ); 118 134 } 119 135 … … 211 227 * @see WP_Dependencies::enqueue() 212 228 * 213 229 * @since 2.6.0 230 * @since 4.5.0 The fifth argument was changed to an array of options. 214 231 * 215 232 * @param string $handle Name of the script. 216 233 * @param string|bool $src Path to the script from the root directory of WordPress. Example: '/js/myscript.js'. … … 218 235 * @param string|bool $ver Optional. String specifying the script version number, if it has one. This parameter 219 236 * is used to ensure that the correct version is sent to the client regardless of caching, 220 237 * and so should be included if a version number is available and makes sense for the script. 221 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>. 222 * Default 'false'. Accepts 'false' or 'true'. 238 * @param array $args { 239 * Optional script arguments. 240 * 241 * @type bool $in_footer Whether to enqueue the script before </head> or before </body>. 242 * Default 'false'. Accepts 'false' or 'true'. 243 * @type array $attributes Array of script tag attributes. 244 * Default: array( 'type' => 'text/javascript' ) 245 * } 223 246 */ 224 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $ in_footer = false) {247 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $args = array() ) { 225 248 $wp_scripts = wp_scripts(); 226 249 227 250 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 228 251 252 // Back-compat. If the fifth argument is not an array, it's `in_footer`. 253 $args = ! is_array( $args ) ? array( 'in_footer' => (bool) $args ) : $args; 254 $args = wp_parse_args( $args, array( 255 'in_footer' => false, 256 'attributes' => array(), 257 ) ); 229 258 259 $attributes = wp_parse_args( $args['attributes'], array( 'type' => 'text/javascript' ) ); 260 230 261 if ( $src || $in_footer ) { 231 262 $_handle = explode( '?', $handle ); 232 263 233 264 if ( $src ) { 234 $wp_scripts->add( $_handle[0], $src, $deps, $ver );265 $wp_scripts->add( $_handle[0], $src, $deps, $ver, array( 'attributes' => $attributes ) ); 235 266 } 236 267 237 if ( $ in_footer) {268 if ( $args['in_footer'] ) { 238 269 $wp_scripts->add_data( $_handle[0], 'group', 1 ); 239 270 } 240 271 }