Ticket #22249: 22249-2.diff
| File 22249-2.diff, 9.4 KB (added by , 11 years ago) |
|---|
-
src/wp-includes/functions.wp-scripts.php
Property changes on: src ___________________________________________________________________ Modified: svn:ignore - .wp-tests-version .htaccess + .wp-tests-version .htaccess wp-config.php
67 67 * to end of path as a query string. If no version is specified or set to false, a version 68 68 * number is automatically added equal to current installed WordPress version. 69 69 * If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'. 70 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>. 71 * Default 'false'. Accepts 'false' or 'true'. 70 * @param array $args Optional script arguments. 71 * - bool - in_footer - Whether to enqueue the script before </head> or before </body>. Default 'false'. Accepts 'false' or 'true'. 72 * - array - attributes - Array of script tag attributes. Default: array( 'type' => 'text/javascript' ) 72 73 */ 73 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $ in_footer = false) {74 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args = array() ) { 74 75 global $wp_scripts; 76 77 // For back-compat. If it's not an array, convert to boolean. 78 $args = ! is_array( $args ) ? array( 'in_footer' => (bool) $args ) : $args; 79 $args = wp_parse_args( $args, array( 80 'in_footer' => false, 81 'attributes' => array(), 82 ) ); 83 84 $attributes = wp_parse_args( $args['attributes'], array( 'type' => 'text/javascript' ) ); 85 75 86 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 76 87 if ( ! did_action( 'init' ) ) 77 88 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), … … 79 90 $wp_scripts = new WP_Scripts(); 80 91 } 81 92 82 $wp_scripts->add( $handle, $src, $deps, $ver );83 if ( $ in_footer )93 $wp_scripts->add( $handle, $src, $deps, $ver, array( 'attributes' => $attributes ) ); 94 if ( $args['in_footer'] ) { 84 95 $wp_scripts->add_data( $handle, 'group', 1 ); 96 } 85 97 } 86 98 87 99 /** … … 189 201 * @param string|bool $ver Optional. String specifying the script version number, if it has one. This parameter 190 202 * is used to ensure that the correct version is sent to the client regardless of caching, 191 203 * and so should be included if a version number is available and makes sense for the script. 192 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>. 193 * Default 'false'. Accepts 'false' or 'true'. 204 * @param array $args Optional script arguments. 205 * - bool - in_footer - Whether to enqueue the script before </head> or before </body>. Default 'false'. Accepts 'false' or 'true'. 206 * - array - attributes - Array of script tag attributes. Default: array( 'type' => 'text/javascript' ) 194 207 */ 195 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $ in_footer = false) {208 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $args = array() ) { 196 209 global $wp_scripts; 210 211 // For back-compat. If it's not an array, convert to boolean. 212 $args = ! is_array( $args ) ? array( 'in_footer' => (bool) $args ) : $args; 213 $args = wp_parse_args( $args, array( 214 'in_footer' => false, 215 'attributes' => array(), 216 ) ); 217 218 $attributes = wp_parse_args( $args['attributes'], array( 'type' => 'text/javascript' ) ); 219 197 220 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 198 221 if ( ! did_action( 'init' ) ) 199 222 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), … … 203 226 204 227 if ( $src ) { 205 228 $_handle = explode('?', $handle); 206 $wp_scripts->add( $_handle[0], $src, $deps, $ver );207 if ( $ in_footer )229 $wp_scripts->add( $_handle[0], $src, $deps, $ver, array( 'attributes' => $attributes ) ); 230 if ( $args['in_footer'] ) { 208 231 $wp_scripts->add_data( $_handle[0], 'group', 1 ); 232 } 209 233 } 210 234 $wp_scripts->enqueue( $handle ); 211 235 } -
src/wp-includes/class.wp-scripts.php
82 82 } 83 83 84 84 public function do_item( $handle, $group = false ) { 85 if ( !parent::do_item( $handle) )85 if ( !parent::do_item( $handle ) ) { 86 86 return false; 87 } 87 88 88 if ( 0 === $group && $this->groups[ $handle] > 0 ) {89 if ( 0 === $group && $this->groups[ $handle ] > 0 ) { 89 90 $this->in_footer[] = $handle; 90 91 return false; 91 92 } 92 93 93 if ( false === $group && in_array($handle, $this->in_footer, true) ) 94 if ( false === $group && in_array($handle, $this->in_footer, true) ) { 94 95 $this->in_footer = array_diff( $this->in_footer, (array) $handle ); 96 } 95 97 96 if ( null === $this->registered[ $handle]->ver )98 if ( null === $this->registered[ $handle ]->ver ) { 97 99 $ver = ''; 98 else 99 $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version; 100 } else { 101 $ver = $this->registered[ $handle ]->ver ? $this->registered[ $handle ]->ver : $this->default_version; 102 } 100 103 101 if ( isset($this->args[$handle]) ) 102 $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; 104 if ( isset( $this->args[ $handle ] ) ) { 105 $ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ]; 106 } 103 107 104 $src = $this->registered[ $handle]->src;108 $src = $this->registered[ $handle ]->src; 105 109 106 110 if ( $this->do_concat ) { 107 111 /** … … 113 117 * @param string $handle Script handle. 114 118 */ 115 119 $srce = apply_filters( 'script_loader_src', $src, $handle ); 116 if ( $this->in_default_dir( $srce) ) {120 if ( $this->in_default_dir( $srce ) ) { 117 121 $this->print_code .= $this->print_extra_script( $handle, false ); 118 122 $this->concat .= "$handle,"; 119 123 $this->concat_version .= "$handle$ver"; … … 124 128 } 125 129 } 126 130 127 $this->print_extra_script( $handle ); 128 if ( !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) { 129 $src = $this->base_url . $src; 131 $this->print_extra_script( $handle ); 132 if ( !preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) { 133 $src = $this->base_url . $src; 134 } 135 136 if ( !empty( $ver ) ) { 137 $src = add_query_arg( 'ver', $ver, $src ); 130 138 } 131 139 132 if ( !empty($ver) )133 $src = add_query_arg('ver', $ver, $src);134 135 140 /** This filter is documented in wp-includes/class.wp-scripts.php */ 136 141 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); 137 142 138 if ( ! $src ) 143 if ( ! $src ) { 139 144 return true; 145 } 140 146 141 $tag = "<script type='text/javascript' src='$src'></script>\n";142 147 143 /** 148 $attributes = $this->get_script_attributes( $handle, $src ); 149 $script_tag = "<script$attributes></script>\n"; 150 151 /** 144 152 * Filter the HTML script tag of an enqueued script. 145 153 * 146 154 * @since 4.1.0 147 155 * 148 * @param string $ tagThe `<script>` tag for the enqueued script.149 * @param string $handle The script's registered handle.150 * @param string $src The script's source URL.156 * @param string $script_tag The `<script>` tag for the enqueued script. 157 * @param string $handle The script's registered handle. 158 * @param string $src The script's source URL. 151 159 */ 152 $ tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );160 $script_tag = apply_filters( 'script_loader_tag', $script_tag, $handle, $src ); 153 161 154 162 if ( $this->do_concat ) { 155 $this->print_html .= $ tag;163 $this->print_html .= $script_tag; 156 164 } else { 157 echo $ tag;165 echo $script_tag; 158 166 } 159 167 160 return true;168 return true; 161 169 } 162 170 171 /** 172 * Concatenates attributes for the script tag 173 * 174 * @since 4.0.0 175 * 176 * @param string $handle Script registered handle 177 * @param string $src Script registered src 178 * 179 * @return string Concatenated attributes string 180 */ 181 public function get_script_attributes( $handle, $src ) { 182 183 $default_attributes = array( 184 'type' => 'text/javascript', 185 'src' => $src 186 ); 187 188 $attributes = isset( $this->registered[ $handle ]->args['attributes'] ) 189 ? (array) $this->registered[ $handle ]->args['attributes'] 190 : array(); 191 192 $attributes = wp_parse_args( $attributes, $default_attributes ); 193 194 /** 195 * Filter the script loader attributes. 196 * 197 * @since 4.1.0 198 * 199 * @param array $attributes Array of script tag attributes. 200 * @param string $handle Script handle. 201 * @param string $src Script loader source path. 202 */ 203 $attributes = apply_filters( 'script_loader_attributes', $attributes, $handle, $src ); 204 // Ensure source is set 205 $attributes['src'] = isset( $attributes['src'] ) ? $attributes['src'] : $src; 206 207 $concat_attributes = ''; 208 foreach ( $attributes as $attribute => $attribute_value ) { 209 if ( ! is_null( $attribute ) && ! is_null( $attribute_value ) ) { 210 $concat_attributes .= ' '. esc_attr( $attribute ) .'="'. esc_attr( $attribute_value ) .'"'; 211 } 212 } 213 214 return $concat_attributes; 215 } 216 163 217 /** 164 218 * Localizes a script 165 219 *