154 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
173 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
155 * |
174 * |
156 * @since 2.1.0 |
175 * @since 2.1.0 |
157 * @since 2.8.0 Added the `$group` parameter. |
176 * @since 2.8.0 Added the `$group` parameter. |
158 * |
177 * |
159 * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints |
178 * @param string|string[]|false $handles Optional. Scripts to be printed: queue (false), |
160 * that script, (array of strings) prints those scripts. Default false. |
179 * single script (string), or multiple scripts (array of strings). |
161 * @param int $group Optional. If scripts were queued in groups prints this group number. |
180 * Default false. |
162 * Default false. |
181 * @param int|false $group Optional. Group level: level (int), no groups (false). |
163 * @return array Scripts that have been printed. |
182 * Default false. |
|
183 * @return string[] Handles of scripts that have been printed. |
164 */ |
184 */ |
165 public function print_scripts( $handles = false, $group = false ) { |
185 public function print_scripts( $handles = false, $group = false ) { |
166 return $this->do_items( $handles, $group ); |
186 return $this->do_items( $handles, $group ); |
167 } |
187 } |
168 |
188 |
174 * @deprecated 3.3.0 |
194 * @deprecated 3.3.0 |
175 * |
195 * |
176 * @see print_extra_script() |
196 * @see print_extra_script() |
177 * |
197 * |
178 * @param string $handle The script's registered handle. |
198 * @param string $handle The script's registered handle. |
179 * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
199 * @param bool $echo Optional. Whether to echo the extra script |
180 * Default true. |
200 * instead of just returning it. Default true. |
181 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
201 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, |
|
202 * true otherwise. |
182 */ |
203 */ |
183 public function print_scripts_l10n( $handle, $echo = true ) { |
204 public function print_scripts_l10n( $handle, $echo = true ) { |
184 _deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' ); |
205 _deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' ); |
185 return $this->print_extra_script( $handle, $echo ); |
206 return $this->print_extra_script( $handle, $echo ); |
186 } |
207 } |
189 * Prints extra scripts of a registered script. |
210 * Prints extra scripts of a registered script. |
190 * |
211 * |
191 * @since 3.3.0 |
212 * @since 3.3.0 |
192 * |
213 * |
193 * @param string $handle The script's registered handle. |
214 * @param string $handle The script's registered handle. |
194 * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
215 * @param bool $echo Optional. Whether to echo the extra script |
195 * Default true. |
216 * instead of just returning it. Default true. |
196 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
217 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, |
|
218 * true otherwise. |
197 */ |
219 */ |
198 public function print_extra_script( $handle, $echo = true ) { |
220 public function print_extra_script( $handle, $echo = true ) { |
199 if ( ! $output = $this->get_data( $handle, 'data' ) ) { |
221 $output = $this->get_data( $handle, 'data' ); |
|
222 if ( ! $output ) { |
200 return; |
223 return; |
201 } |
224 } |
202 |
225 |
203 if ( ! $echo ) { |
226 if ( ! $echo ) { |
204 return $output; |
227 return $output; |
205 } |
228 } |
206 |
229 |
207 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5. |
230 printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) ); |
208 echo "/* <![CDATA[ */\n"; |
231 |
|
232 // CDATA is not needed for HTML 5. |
|
233 if ( $this->type_attr ) { |
|
234 echo "/* <![CDATA[ */\n"; |
|
235 } |
|
236 |
209 echo "$output\n"; |
237 echo "$output\n"; |
210 echo "/* ]]> */\n"; |
238 |
|
239 if ( $this->type_attr ) { |
|
240 echo "/* ]]> */\n"; |
|
241 } |
|
242 |
211 echo "</script>\n"; |
243 echo "</script>\n"; |
212 |
244 |
213 return true; |
245 return true; |
214 } |
246 } |
215 |
247 |
220 * @since 2.8.0 Added the `$group` parameter. |
252 * @since 2.8.0 Added the `$group` parameter. |
221 * |
253 * |
222 * @see WP_Dependencies::do_item() |
254 * @see WP_Dependencies::do_item() |
223 * |
255 * |
224 * @param string $handle The script's registered handle. |
256 * @param string $handle The script's registered handle. |
225 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
257 * @param int|false $group Optional. Group level: level (int), no groups (false). |
|
258 * Default false. |
226 * @return bool True on success, false on failure. |
259 * @return bool True on success, false on failure. |
227 */ |
260 */ |
228 public function do_item( $handle, $group = false ) { |
261 public function do_item( $handle, $group = false ) { |
229 if ( ! parent::do_item( $handle ) ) { |
262 if ( ! parent::do_item( $handle ) ) { |
230 return false; |
263 return false; |
250 if ( isset( $this->args[ $handle ] ) ) { |
283 if ( isset( $this->args[ $handle ] ) ) { |
251 $ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ]; |
284 $ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ]; |
252 } |
285 } |
253 |
286 |
254 $src = $obj->src; |
287 $src = $obj->src; |
255 $cond_before = $cond_after = ''; |
288 $cond_before = ''; |
|
289 $cond_after = ''; |
256 $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : ''; |
290 $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : ''; |
257 |
291 |
258 if ( $conditional ) { |
292 if ( $conditional ) { |
259 $cond_before = "<!--[if {$conditional}]>\n"; |
293 $cond_before = "<!--[if {$conditional}]>\n"; |
260 $cond_after = "<![endif]-->\n"; |
294 $cond_after = "<![endif]-->\n"; |
262 |
296 |
263 $before_handle = $this->print_inline_script( $handle, 'before', false ); |
297 $before_handle = $this->print_inline_script( $handle, 'before', false ); |
264 $after_handle = $this->print_inline_script( $handle, 'after', false ); |
298 $after_handle = $this->print_inline_script( $handle, 'after', false ); |
265 |
299 |
266 if ( $before_handle ) { |
300 if ( $before_handle ) { |
267 $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle ); |
301 $before_handle = sprintf( "<script%s id='%s-js-before'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $before_handle ); |
268 } |
302 } |
269 |
303 |
270 if ( $after_handle ) { |
304 if ( $after_handle ) { |
271 $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle ); |
305 $after_handle = sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_handle ); |
272 } |
306 } |
273 |
307 |
274 if ( $before_handle || $after_handle ) { |
308 if ( $before_handle || $after_handle ) { |
275 $inline_script_tag = "{$cond_before}{$before_handle}{$after_handle}{$cond_after}"; |
309 $inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after; |
276 } else { |
310 } else { |
277 $inline_script_tag = ''; |
311 $inline_script_tag = ''; |
|
312 } |
|
313 |
|
314 $translations = $this->print_translations( $handle, false ); |
|
315 if ( $translations ) { |
|
316 $translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations ); |
278 } |
317 } |
279 |
318 |
280 if ( $this->do_concat ) { |
319 if ( $this->do_concat ) { |
281 /** |
320 /** |
282 * Filters the script loader source. |
321 * Filters the script loader source. |
286 * @param string $src Script loader source path. |
325 * @param string $src Script loader source path. |
287 * @param string $handle Script handle. |
326 * @param string $handle Script handle. |
288 */ |
327 */ |
289 $srce = apply_filters( 'script_loader_src', $src, $handle ); |
328 $srce = apply_filters( 'script_loader_src', $src, $handle ); |
290 |
329 |
291 if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) { |
330 if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations ) ) { |
292 $this->do_concat = false; |
331 $this->do_concat = false; |
293 |
332 |
294 // Have to print the so-far concatenated scripts right away to maintain the right order. |
333 // Have to print the so-far concatenated scripts right away to maintain the right order. |
295 _print_scripts(); |
334 _print_scripts(); |
296 $this->reset(); |
335 $this->reset(); |
328 } |
367 } |
329 |
368 |
330 return true; |
369 return true; |
331 } |
370 } |
332 |
371 |
333 $translations = $this->print_translations( $handle, false ); |
|
334 if ( $translations ) { |
|
335 $translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations ); |
|
336 } |
|
337 |
|
338 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) { |
372 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) { |
339 $src = $this->base_url . $src; |
373 $src = $this->base_url . $src; |
340 } |
374 } |
341 |
375 |
342 if ( ! empty( $ver ) ) { |
376 if ( ! empty( $ver ) ) { |
375 /** |
411 /** |
376 * Adds extra code to a registered script. |
412 * Adds extra code to a registered script. |
377 * |
413 * |
378 * @since 4.5.0 |
414 * @since 4.5.0 |
379 * |
415 * |
380 * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
416 * @param string $handle Name of the script to add the inline script to. |
|
417 * Must be lowercase. |
381 * @param string $data String containing the javascript to be added. |
418 * @param string $data String containing the javascript to be added. |
382 * @param string $position Optional. Whether to add the inline script before the handle |
419 * @param string $position Optional. Whether to add the inline script |
383 * or after. Default 'after'. |
420 * before the handle or after. Default 'after'. |
384 * @return bool True on success, false on failure. |
421 * @return bool True on success, false on failure. |
385 */ |
422 */ |
386 public function add_inline_script( $handle, $data, $position = 'after' ) { |
423 public function add_inline_script( $handle, $data, $position = 'after' ) { |
387 if ( ! $data ) { |
424 if ( ! $data ) { |
388 return false; |
425 return false; |
401 /** |
438 /** |
402 * Prints inline scripts registered for a specific handle. |
439 * Prints inline scripts registered for a specific handle. |
403 * |
440 * |
404 * @since 4.5.0 |
441 * @since 4.5.0 |
405 * |
442 * |
406 * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
443 * @param string $handle Name of the script to add the inline script to. |
407 * @param string $position Optional. Whether to add the inline script before the handle |
444 * Must be lowercase. |
408 * or after. Default 'after'. |
445 * @param string $position Optional. Whether to add the inline script |
409 * @param bool $echo Optional. Whether to echo the script instead of just returning it. |
446 * before the handle or after. Default 'after'. |
410 * Default true. |
447 * @param bool $echo Optional. Whether to echo the script |
|
448 * instead of just returning it. Default true. |
411 * @return string|false Script on success, false otherwise. |
449 * @return string|false Script on success, false otherwise. |
412 */ |
450 */ |
413 public function print_inline_script( $handle, $position = 'after', $echo = true ) { |
451 public function print_inline_script( $handle, $position = 'after', $echo = true ) { |
414 $output = $this->get_data( $handle, $position ); |
452 $output = $this->get_data( $handle, $position ); |
415 |
453 |
435 * @param string $object_name Name of the variable that will contain the data. |
473 * @param string $object_name Name of the variable that will contain the data. |
436 * @param array $l10n Array of data to localize. |
474 * @param array $l10n Array of data to localize. |
437 * @return bool True on success, false on failure. |
475 * @return bool True on success, false on failure. |
438 */ |
476 */ |
439 public function localize( $handle, $object_name, $l10n ) { |
477 public function localize( $handle, $object_name, $l10n ) { |
440 if ( $handle === 'jquery' ) { |
478 if ( 'jquery' === $handle ) { |
441 $handle = 'jquery-core'; |
479 $handle = 'jquery-core'; |
442 } |
480 } |
443 |
481 |
444 if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present. |
482 if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present. |
445 $after = $l10n['l10n_print_after']; |
483 $after = $l10n['l10n_print_after']; |
476 * |
514 * |
477 * @see WP_Dependencies::set_group() |
515 * @see WP_Dependencies::set_group() |
478 * |
516 * |
479 * @param string $handle Name of the item. Should be unique. |
517 * @param string $handle Name of the item. Should be unique. |
480 * @param bool $recursion Internal flag that calling function was called recursively. |
518 * @param bool $recursion Internal flag that calling function was called recursively. |
481 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
519 * @param int|false $group Optional. Group level: level (int), no groups (false). |
482 * @return bool Not already in the group or a lower group |
520 * Default false. |
|
521 * @return bool Not already in the group or a lower group. |
483 */ |
522 */ |
484 public function set_group( $handle, $recursion, $group = false ) { |
523 public function set_group( $handle, $recursion, $group = false ) { |
485 if ( isset( $this->registered[ $handle ]->args ) && $this->registered[ $handle ]->args === 1 ) { |
524 if ( isset( $this->registered[ $handle ]->args ) && 1 === $this->registered[ $handle ]->args ) { |
486 $grp = 1; |
525 $grp = 1; |
487 } else { |
526 } else { |
488 $grp = (int) $this->get_data( $handle, 'group' ); |
527 $grp = (int) $this->get_data( $handle, 'group' ); |
489 } |
528 } |
490 |
529 |
524 /** |
563 /** |
525 * Prints translations set for a specific handle. |
564 * Prints translations set for a specific handle. |
526 * |
565 * |
527 * @since 5.0.0 |
566 * @since 5.0.0 |
528 * |
567 * |
529 * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
568 * @param string $handle Name of the script to add the inline script to. |
530 * @param bool $echo Optional. Whether to echo the script instead of just returning it. |
569 * Must be lowercase. |
531 * Default true. |
570 * @param bool $echo Optional. Whether to echo the script |
|
571 * instead of just returning it. Default true. |
532 * @return string|false Script on success, false otherwise. |
572 * @return string|false Script on success, false otherwise. |
533 */ |
573 */ |
534 public function print_translations( $handle, $echo = true ) { |
574 public function print_translations( $handle, $echo = true ) { |
535 if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) { |
575 if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) { |
536 return false; |
576 return false; |
553 wp.i18n.setLocaleData( localeData, domain ); |
593 wp.i18n.setLocaleData( localeData, domain ); |
554 } )( "{$domain}", {$json_translations} ); |
594 } )( "{$domain}", {$json_translations} ); |
555 JS; |
595 JS; |
556 |
596 |
557 if ( $echo ) { |
597 if ( $echo ) { |
558 printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); |
598 printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output ); |
559 } |
599 } |
560 |
600 |
561 return $output; |
601 return $output; |
562 } |
602 } |
563 |
603 |
566 * |
606 * |
567 * @since 2.1.0 |
607 * @since 2.1.0 |
568 * |
608 * |
569 * @see WP_Dependencies::all_deps() |
609 * @see WP_Dependencies::all_deps() |
570 * |
610 * |
571 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
611 * @param string|string[] $handles Item handle (string) or item handles (array of strings). |
572 * @param bool $recursion Internal flag that function is calling itself. |
612 * @param bool $recursion Optional. Internal flag that function is calling itself. |
573 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
613 * Default false. |
|
614 * @param int|false $group Optional. Group level: level (int), no groups (false). |
|
615 * Default false. |
574 * @return bool True on success, false on failure. |
616 * @return bool True on success, false on failure. |
575 */ |
617 */ |
576 public function all_deps( $handles, $recursion = false, $group = false ) { |
618 public function all_deps( $handles, $recursion = false, $group = false ) { |
577 $r = parent::all_deps( $handles, $recursion, $group ); |
619 $r = parent::all_deps( $handles, $recursion, $group ); |
578 if ( ! $recursion ) { |
620 if ( ! $recursion ) { |