194 * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
194 * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
195 * Default true. |
195 * Default true. |
196 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
196 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
197 */ |
197 */ |
198 public function print_extra_script( $handle, $echo = true ) { |
198 public function print_extra_script( $handle, $echo = true ) { |
199 if ( !$output = $this->get_data( $handle, 'data' ) ) |
199 if ( ! $output = $this->get_data( $handle, 'data' ) ) { |
200 return; |
200 return; |
201 |
201 } |
202 if ( !$echo ) |
202 |
|
203 if ( ! $echo ) { |
203 return $output; |
204 return $output; |
204 |
205 } |
205 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5 |
206 |
|
207 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5. |
206 echo "/* <![CDATA[ */\n"; |
208 echo "/* <![CDATA[ */\n"; |
207 echo "$output\n"; |
209 echo "$output\n"; |
208 echo "/* ]]> */\n"; |
210 echo "/* ]]> */\n"; |
209 echo "</script>\n"; |
211 echo "</script>\n"; |
210 |
212 |
217 * @since 2.6.0 |
219 * @since 2.6.0 |
218 * @since 2.8.0 Added the `$group` parameter. |
220 * @since 2.8.0 Added the `$group` parameter. |
219 * |
221 * |
220 * @see WP_Dependencies::do_item() |
222 * @see WP_Dependencies::do_item() |
221 * |
223 * |
222 * @param string $handle The script's registered handle. |
224 * @param string $handle The script's registered handle. |
223 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
225 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
224 * @return bool True on success, false on failure. |
226 * @return bool True on success, false on failure. |
225 */ |
227 */ |
226 public function do_item( $handle, $group = false ) { |
228 public function do_item( $handle, $group = false ) { |
227 if ( !parent::do_item($handle) ) |
229 if ( ! parent::do_item( $handle ) ) { |
228 return false; |
230 return false; |
229 |
231 } |
230 if ( 0 === $group && $this->groups[$handle] > 0 ) { |
232 |
|
233 if ( 0 === $group && $this->groups[ $handle ] > 0 ) { |
231 $this->in_footer[] = $handle; |
234 $this->in_footer[] = $handle; |
232 return false; |
235 return false; |
233 } |
236 } |
234 |
237 |
235 if ( false === $group && in_array($handle, $this->in_footer, true) ) |
238 if ( false === $group && in_array( $handle, $this->in_footer, true ) ) { |
236 $this->in_footer = array_diff( $this->in_footer, (array) $handle ); |
239 $this->in_footer = array_diff( $this->in_footer, (array) $handle ); |
237 |
240 } |
238 $obj = $this->registered[$handle]; |
241 |
|
242 $obj = $this->registered[ $handle ]; |
239 |
243 |
240 if ( null === $obj->ver ) { |
244 if ( null === $obj->ver ) { |
241 $ver = ''; |
245 $ver = ''; |
242 } else { |
246 } else { |
243 $ver = $obj->ver ? $obj->ver : $this->default_version; |
247 $ver = $obj->ver ? $obj->ver : $this->default_version; |
244 } |
248 } |
245 |
249 |
246 if ( isset($this->args[$handle]) ) |
250 if ( isset( $this->args[ $handle ] ) ) { |
247 $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; |
251 $ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ]; |
248 |
252 } |
249 $src = $obj->src; |
253 |
|
254 $src = $obj->src; |
250 $cond_before = $cond_after = ''; |
255 $cond_before = $cond_after = ''; |
251 $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : ''; |
256 $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : ''; |
252 |
257 |
253 if ( $conditional ) { |
258 if ( $conditional ) { |
254 $cond_before = "<!--[if {$conditional}]>\n"; |
259 $cond_before = "<!--[if {$conditional}]>\n"; |
255 $cond_after = "<![endif]-->\n"; |
260 $cond_after = "<![endif]-->\n"; |
256 } |
261 } |
257 |
262 |
258 $before_handle = $this->print_inline_script( $handle, 'before', false ); |
263 $before_handle = $this->print_inline_script( $handle, 'before', false ); |
259 $after_handle = $this->print_inline_script( $handle, 'after', false ); |
264 $after_handle = $this->print_inline_script( $handle, 'after', false ); |
260 |
265 |
261 if ( $before_handle ) { |
266 if ( $before_handle ) { |
262 $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle ); |
267 $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle ); |
263 } |
268 } |
264 |
269 |
265 if ( $after_handle ) { |
270 if ( $after_handle ) { |
266 $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle ); |
271 $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle ); |
|
272 } |
|
273 |
|
274 if ( $before_handle || $after_handle ) { |
|
275 $inline_script_tag = "{$cond_before}{$before_handle}{$after_handle}{$cond_after}"; |
|
276 } else { |
|
277 $inline_script_tag = ''; |
267 } |
278 } |
268 |
279 |
269 if ( $this->do_concat ) { |
280 if ( $this->do_concat ) { |
270 /** |
281 /** |
271 * Filters the script loader source. |
282 * Filters the script loader source. |
282 |
293 |
283 // Have to print the so-far concatenated scripts right away to maintain the right order. |
294 // Have to print the so-far concatenated scripts right away to maintain the right order. |
284 _print_scripts(); |
295 _print_scripts(); |
285 $this->reset(); |
296 $this->reset(); |
286 } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) { |
297 } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) { |
287 $this->print_code .= $this->print_extra_script( $handle, false ); |
298 $this->print_code .= $this->print_extra_script( $handle, false ); |
288 $this->concat .= "$handle,"; |
299 $this->concat .= "$handle,"; |
289 $this->concat_version .= "$handle$ver"; |
300 $this->concat_version .= "$handle$ver"; |
290 return true; |
301 return true; |
291 } else { |
302 } else { |
292 $this->ext_handles .= "$handle,"; |
303 $this->ext_handles .= "$handle,"; |
293 $this->ext_version .= "$handle$ver"; |
304 $this->ext_version .= "$handle$ver"; |
305 if ( $has_conditional_data ) { |
316 if ( $has_conditional_data ) { |
306 echo $cond_after; |
317 echo $cond_after; |
307 } |
318 } |
308 |
319 |
309 // A single item may alias a set of items, by having dependencies, but no source. |
320 // A single item may alias a set of items, by having dependencies, but no source. |
310 if ( ! $obj->src ) { |
321 if ( ! $src ) { |
|
322 if ( $inline_script_tag ) { |
|
323 if ( $this->do_concat ) { |
|
324 $this->print_html .= $inline_script_tag; |
|
325 } else { |
|
326 echo $inline_script_tag; |
|
327 } |
|
328 } |
|
329 |
311 return true; |
330 return true; |
|
331 } |
|
332 |
|
333 $translations = $this->print_translations( $handle, false ); |
|
334 if ( $translations ) { |
|
335 $translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations ); |
312 } |
336 } |
313 |
337 |
314 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) { |
338 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) { |
315 $src = $this->base_url . $src; |
339 $src = $this->base_url . $src; |
316 } |
340 } |
317 |
341 |
318 if ( ! empty( $ver ) ) |
342 if ( ! empty( $ver ) ) { |
319 $src = add_query_arg( 'ver', $ver, $src ); |
343 $src = add_query_arg( 'ver', $ver, $src ); |
|
344 } |
320 |
345 |
321 /** This filter is documented in wp-includes/class.wp-scripts.php */ |
346 /** This filter is documented in wp-includes/class.wp-scripts.php */ |
322 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); |
347 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); |
323 |
348 |
324 if ( ! $src ) |
349 if ( ! $src ) { |
325 return true; |
350 return true; |
326 |
351 } |
327 $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}"; |
352 |
|
353 $tag = "{$translations}{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}"; |
328 |
354 |
329 /** |
355 /** |
330 * Filters the HTML script tag of an enqueued script. |
356 * Filters the HTML script tag of an enqueued script. |
331 * |
357 * |
332 * @since 4.1.0 |
358 * @since 4.1.0 |
378 * @since 4.5.0 |
404 * @since 4.5.0 |
379 * |
405 * |
380 * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
406 * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
381 * @param string $position Optional. Whether to add the inline script before the handle |
407 * @param string $position Optional. Whether to add the inline script before the handle |
382 * or after. Default 'after'. |
408 * or after. Default 'after'. |
383 * @param bool $echo Optional. Whether to echo the script instead of just returning it. |
409 * @param bool $echo Optional. Whether to echo the script instead of just returning it. |
384 * Default true. |
410 * Default true. |
385 * @return string|false Script on success, false otherwise. |
411 * @return string|false Script on success, false otherwise. |
386 */ |
412 */ |
387 public function print_inline_script( $handle, $position = 'after', $echo = true ) { |
413 public function print_inline_script( $handle, $position = 'after', $echo = true ) { |
388 $output = $this->get_data( $handle, $position ); |
414 $output = $this->get_data( $handle, $position ); |
403 /** |
429 /** |
404 * Localizes a script, only if the script has already been added. |
430 * Localizes a script, only if the script has already been added. |
405 * |
431 * |
406 * @since 2.1.0 |
432 * @since 2.1.0 |
407 * |
433 * |
408 * @param string $handle |
434 * @param string $handle Name of the script to attach data to. |
409 * @param string $object_name |
435 * @param string $object_name Name of the variable that will contain the data. |
410 * @param array $l10n |
436 * @param array $l10n Array of data to localize. |
411 * @return bool |
437 * @return bool True on success, false on failure. |
412 */ |
438 */ |
413 public function localize( $handle, $object_name, $l10n ) { |
439 public function localize( $handle, $object_name, $l10n ) { |
414 if ( $handle === 'jquery' ) |
440 if ( $handle === 'jquery' ) { |
415 $handle = 'jquery-core'; |
441 $handle = 'jquery-core'; |
416 |
442 } |
417 if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present |
443 |
|
444 if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present. |
418 $after = $l10n['l10n_print_after']; |
445 $after = $l10n['l10n_print_after']; |
419 unset($l10n['l10n_print_after']); |
446 unset( $l10n['l10n_print_after'] ); |
420 } |
447 } |
421 |
448 |
422 foreach ( (array) $l10n as $key => $value ) { |
449 foreach ( (array) $l10n as $key => $value ) { |
423 if ( !is_scalar($value) ) |
450 if ( ! is_scalar( $value ) ) { |
424 continue; |
451 continue; |
425 |
452 } |
426 $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8'); |
453 |
|
454 $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); |
427 } |
455 } |
428 |
456 |
429 $script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; |
457 $script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; |
430 |
458 |
431 if ( !empty($after) ) |
459 if ( ! empty( $after ) ) { |
432 $script .= "\n$after;"; |
460 $script .= "\n$after;"; |
|
461 } |
433 |
462 |
434 $data = $this->get_data( $handle, 'data' ); |
463 $data = $this->get_data( $handle, 'data' ); |
435 |
464 |
436 if ( !empty( $data ) ) |
465 if ( ! empty( $data ) ) { |
437 $script = "$data\n$script"; |
466 $script = "$data\n$script"; |
|
467 } |
438 |
468 |
439 return $this->add_data( $handle, 'data', $script ); |
469 return $this->add_data( $handle, 'data', $script ); |
440 } |
470 } |
441 |
471 |
442 /** |
472 /** |
450 * @param bool $recursion Internal flag that calling function was called recursively. |
480 * @param bool $recursion Internal flag that calling function was called recursively. |
451 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
481 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
452 * @return bool Not already in the group or a lower group |
482 * @return bool Not already in the group or a lower group |
453 */ |
483 */ |
454 public function set_group( $handle, $recursion, $group = false ) { |
484 public function set_group( $handle, $recursion, $group = false ) { |
455 if ( isset( $this->registered[$handle]->args ) && $this->registered[$handle]->args === 1 ) |
485 if ( isset( $this->registered[ $handle ]->args ) && $this->registered[ $handle ]->args === 1 ) { |
456 $grp = 1; |
486 $grp = 1; |
457 else |
487 } else { |
458 $grp = (int) $this->get_data( $handle, 'group' ); |
488 $grp = (int) $this->get_data( $handle, 'group' ); |
459 |
489 } |
460 if ( false !== $group && $grp > $group ) |
490 |
|
491 if ( false !== $group && $grp > $group ) { |
461 $grp = $group; |
492 $grp = $group; |
|
493 } |
462 |
494 |
463 return parent::set_group( $handle, $recursion, $grp ); |
495 return parent::set_group( $handle, $recursion, $grp ); |
464 } |
496 } |
465 |
497 |
466 /** |
498 /** |
|
499 * Sets a translation textdomain. |
|
500 * |
|
501 * @since 5.0.0 |
|
502 * @since 5.1.0 The `$domain` parameter was made optional. |
|
503 * |
|
504 * @param string $handle Name of the script to register a translation domain to. |
|
505 * @param string $domain Optional. Text domain. Default 'default'. |
|
506 * @param string $path Optional. The full file path to the directory containing translation files. |
|
507 * @return bool True if the text domain was registered, false if not. |
|
508 */ |
|
509 public function set_translations( $handle, $domain = 'default', $path = null ) { |
|
510 if ( ! isset( $this->registered[ $handle ] ) ) { |
|
511 return false; |
|
512 } |
|
513 |
|
514 /** @var \_WP_Dependency $obj */ |
|
515 $obj = $this->registered[ $handle ]; |
|
516 |
|
517 if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) { |
|
518 $obj->deps[] = 'wp-i18n'; |
|
519 } |
|
520 |
|
521 return $obj->set_translations( $domain, $path ); |
|
522 } |
|
523 |
|
524 /** |
|
525 * Prints translations set for a specific handle. |
|
526 * |
|
527 * @since 5.0.0 |
|
528 * |
|
529 * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
|
530 * @param bool $echo Optional. Whether to echo the script instead of just returning it. |
|
531 * Default true. |
|
532 * @return string|false Script on success, false otherwise. |
|
533 */ |
|
534 public function print_translations( $handle, $echo = true ) { |
|
535 if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) { |
|
536 return false; |
|
537 } |
|
538 |
|
539 $domain = $this->registered[ $handle ]->textdomain; |
|
540 $path = $this->registered[ $handle ]->translations_path; |
|
541 |
|
542 $json_translations = load_script_textdomain( $handle, $domain, $path ); |
|
543 |
|
544 if ( ! $json_translations ) { |
|
545 // Register empty locale data object to ensure the domain still exists. |
|
546 $json_translations = '{ "locale_data": { "messages": { "": {} } } }'; |
|
547 } |
|
548 |
|
549 $output = <<<JS |
|
550 ( function( domain, translations ) { |
|
551 var localeData = translations.locale_data[ domain ] || translations.locale_data.messages; |
|
552 localeData[""].domain = domain; |
|
553 wp.i18n.setLocaleData( localeData, domain ); |
|
554 } )( "{$domain}", {$json_translations} ); |
|
555 JS; |
|
556 |
|
557 if ( $echo ) { |
|
558 printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); |
|
559 } |
|
560 |
|
561 return $output; |
|
562 } |
|
563 |
|
564 /** |
467 * Determines script dependencies. |
565 * Determines script dependencies. |
468 * |
566 * |
469 * @since 2.1.0 |
567 * @since 2.1.0 |
470 * |
568 * |
471 * @see WP_Dependencies::all_deps() |
569 * @see WP_Dependencies::all_deps() |
472 * |
570 * |
473 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
571 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |