413 * |
413 * |
414 * @since 4.5.0 |
414 * @since 4.5.0 |
415 * |
415 * |
416 * @param string $handle Name of the script to add the inline script to. |
416 * @param string $handle Name of the script to add the inline script to. |
417 * Must be lowercase. |
417 * Must be lowercase. |
418 * @param string $data String containing the javascript to be added. |
418 * @param string $data String containing the JavaScript to be added. |
419 * @param string $position Optional. Whether to add the inline script |
419 * @param string $position Optional. Whether to add the inline script |
420 * before the handle or after. Default 'after'. |
420 * before the handle or after. Default 'after'. |
421 * @return bool True on success, false on failure. |
421 * @return bool True on success, false on failure. |
422 */ |
422 */ |
423 public function add_inline_script( $handle, $data, $position = 'after' ) { |
423 public function add_inline_script( $handle, $data, $position = 'after' ) { |
482 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. |
483 $after = $l10n['l10n_print_after']; |
483 $after = $l10n['l10n_print_after']; |
484 unset( $l10n['l10n_print_after'] ); |
484 unset( $l10n['l10n_print_after'] ); |
485 } |
485 } |
486 |
486 |
487 foreach ( (array) $l10n as $key => $value ) { |
487 if ( ! is_array( $l10n ) ) { |
488 if ( ! is_scalar( $value ) ) { |
488 _doing_it_wrong( |
489 continue; |
489 __METHOD__, |
|
490 sprintf( |
|
491 /* translators: 1: $l10n, 2: wp_add_inline_script() */ |
|
492 __( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ), |
|
493 '<code>$l10n</code>', |
|
494 '<code>wp_add_inline_script()</code>' |
|
495 ), |
|
496 '5.7.0' |
|
497 ); |
|
498 } |
|
499 |
|
500 if ( is_string( $l10n ) ) { |
|
501 $l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' ); |
|
502 } else { |
|
503 foreach ( (array) $l10n as $key => $value ) { |
|
504 if ( ! is_scalar( $value ) ) { |
|
505 continue; |
|
506 } |
|
507 |
|
508 $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); |
490 } |
509 } |
491 |
|
492 $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); |
|
493 } |
510 } |
494 |
511 |
495 $script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; |
512 $script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; |
496 |
513 |
497 if ( ! empty( $after ) ) { |
514 if ( ! empty( $after ) ) { |