: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
echo $this->get_inline_script_tag( $handle, $position );
* Gets data for inline scripts registered for a specific handle.
* @param string $handle Name of the script to get data for.
* @param string $position Optional. Whether to add the inline script
* before the handle or after. Default 'after'.
* @return string Inline script, which may be empty string.
public function get_inline_script_data( $handle, $position = 'after' ) {
$data = $this->get_data( $handle, $position );
if ( empty( $data ) || ! is_array( $data ) ) {
return trim( implode( "\n", $data ), "\n" );
* Gets tags for inline scripts registered for a specific handle.
* @param string $handle Name of the script to get associated inline script tag for.
* @param string $position Optional. Whether to get tag for inline
* scripts in the before or after position. Default 'after'.
* @return string Inline script, which may be empty string.
public function get_inline_script_tag( $handle, $position = 'after' ) {
$js = $this->get_inline_script_data( $handle, $position );
$id = "{$handle}-js-{$position}";
return wp_get_inline_script_tag( $js, compact( 'id' ) );
* Localizes a script, only if the script has already been added.
* @param string $handle Name of the script to attach data to.
* @param string $object_name Name of the variable that will contain the data.
* @param array $l10n Array of data to localize.
* @return bool True on success, false on failure.
public function localize( $handle, $object_name, $l10n ) {
if ( 'jquery' === $handle ) {
if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present.
$after = $l10n['l10n_print_after'];
unset( $l10n['l10n_print_after'] );
if ( ! is_array( $l10n ) ) {
/* translators: 1: $l10n, 2: wp_add_inline_script() */
__( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ),
'<code>wp_add_inline_script()</code>'
// This should really not be needed, but is necessary for backward compatibility.
if ( is_string( $l10n ) ) {
$l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' );
} elseif ( is_array( $l10n ) ) {
foreach ( $l10n as $key => $value ) {
if ( ! is_scalar( $value ) ) {
$l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
$script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
if ( ! empty( $after ) ) {
$data = $this->get_data( $handle, 'data' );
if ( ! empty( $data ) ) {
$script = "$data\n$script";
return $this->add_data( $handle, 'data', $script );
* @see WP_Dependencies::set_group()
* @param string $handle Name of the item. Should be unique.
* @param bool $recursion Internal flag that calling function was called recursively.
* @param int|false $group Optional. Group level: level (int), no groups (false).
* @return bool Not already in the group or a lower group.
public function set_group( $handle, $recursion, $group = false ) {
if ( isset( $this->registered[ $handle ]->args ) && 1 === $this->registered[ $handle ]->args ) {
$grp = (int) $this->get_data( $handle, 'group' );
if ( false !== $group && $grp > $group ) {
return parent::set_group( $handle, $recursion, $grp );
* Sets a translation textdomain.
* @since 5.1.0 The `$domain` parameter was made optional.
* @param string $handle Name of the script to register a translation domain to.
* @param string $domain Optional. Text domain. Default 'default'.
* @param string $path Optional. The full file path to the directory containing translation files.
* @return bool True if the text domain was registered, false if not.
public function set_translations( $handle, $domain = 'default', $path = '' ) {
if ( ! isset( $this->registered[ $handle ] ) ) {
/** @var \_WP_Dependency $obj */
$obj = $this->registered[ $handle ];
if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) {
$obj->deps[] = 'wp-i18n';
return $obj->set_translations( $domain, $path );
* Prints translations set for a specific handle.
* @param string $handle Name of the script to add the inline script to.
* @param bool $display Optional. Whether to print the script
* instead of just returning it. Default true.
* @return string|false Script on success, false otherwise.
public function print_translations( $handle, $display = true ) {
if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
$domain = $this->registered[ $handle ]->textdomain;
if ( isset( $this->registered[ $handle ]->translations_path ) ) {
$path = $this->registered[ $handle ]->translations_path;
$json_translations = load_script_textdomain( $handle, $domain, $path );
if ( ! $json_translations ) {
( function( domain, translations ) {
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
localeData[""].domain = domain;
wp.i18n.setLocaleData( localeData, domain );
} )( "{$domain}", {$json_translations} );
wp_print_inline_script_tag( $output, array( 'id' => "{$handle}-js-translations" ) );
* Determines script dependencies.
* @see WP_Dependencies::all_deps()
* @param string|string[] $handles Item handle (string) or item handles (array of strings).
* @param bool $recursion Optional. Internal flag that function is calling itself.
* @param int|false $group Optional. Group level: level (int), no groups (false).
* @return bool True on success, false on failure.
public function all_deps( $handles, $recursion = false, $group = false ) {
$r = parent::all_deps( $handles, $recursion, $group );
* Filters the list of script dependencies left to print.
* @param string[] $to_do An array of script dependency handles.
$this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
* Processes items and dependencies for the head group.
* @see WP_Dependencies::do_items()
* @return string[] Handles of items that have been processed.
public function do_head_items() {
$this->do_items( false, 0 );
* Processes items and dependencies for the footer group.
* @see WP_Dependencies::do_items()
* @return string[] Handles of items that have been processed.
public function do_footer_items() {
$this->do_items( false, 1 );
* Whether a handle's source is in a default directory.
* @param string $src The source of the enqueued script.
* @return bool True if found, false if not.
public function in_default_dir( $src ) {
if ( ! $this->default_dirs ) {
if ( str_starts_with( $src, '/' . WPINC . '/js/l10n' ) ) {
foreach ( (array) $this->default_dirs as $test ) {
if ( str_starts_with( $src, $test ) ) {
* This overrides the add_data method from WP_Dependencies, to support normalizing of $args.
* @param string $handle Name of the item. Should be unique.
* @param string $key The data key.
* @param mixed $value The data value.
* @return bool True on success, false on failure.
public function add_data( $handle, $key, $value ) {
if ( ! isset( $this->registered[ $handle ] ) ) {
if ( 'strategy' === $key ) {
if ( ! empty( $value ) && ! $this->is_delayed_strategy( $value ) ) {
/* translators: 1: $strategy, 2: $handle */
__( 'Invalid strategy `%1$s` defined for `%2$s` during script registration.' ),
} elseif ( ! $this->registered[ $handle ]->src && $this->is_delayed_strategy( $value ) ) {
/* translators: 1: $strategy, 2: $handle */
__( 'Cannot supply a strategy `%1$s` for script `%2$s` because it is an alias (it lacks a `src` value).' ),
return parent::add_data( $handle, $key, $value );
* Gets all dependents of a script.
* @param string $handle The script handle.
* @return string[] Script handles.
private function get_dependents( $handle ) {
// Check if dependents map for the handle in question is present. If so, use it.
if ( isset( $this->dependents_map[ $handle ] ) ) {
return $this->dependents_map[ $handle ];
// Iterate over all registered scripts, finding dependents of the script passed to this method.
foreach ( $this->registered as $registered_handle => $args ) {
if ( in_array( $handle, $args->deps, true ) ) {
$dependents[] = $registered_handle;
// Add the handles dependents to the map to ease future lookups.
$this->dependents_map[ $handle ] = $dependents;
* Checks if the strategy passed is a valid delayed (non-blocking) strategy.
* @param string $strategy The strategy to check.
* @return bool True if $strategy is one of the delayed strategies, otherwise false.
private function is_delayed_strategy( $strategy ) {
$this->delayed_strategies,
* Gets the best eligible loading strategy for a script.
* @param string $handle The script handle.
* @return string The best eligible loading strategy.
private function get_eligible_loading_strategy( $handle ) {
$intended = (string) $this->get_data( $handle, 'strategy' );
// Bail early if there is no intended strategy.
* If the intended strategy is 'defer', limit the initial list of eligible
* strategies, since 'async' can fallback to 'defer', but not vice-versa.
$initial = ( 'defer' === $intended ) ? array( 'defer' ) : null;
$eligible = $this->filter_eligible_strategies( $handle, $initial );
// Return early once we know the eligible strategy is blocking.
if ( empty( $eligible ) ) {
return in_array( 'async', $eligible, true ) ? 'async' : 'defer';
* Filter the list of eligible loading strategies for a script.
* @param string $handle The script handle.
* @param string[]|null $eligible Optional. The list of strategies to filter. Default null.
* @param array<string, true> $checked Optional. An array of already checked script handles, used to avoid recursive loops.
* @return string[] A list of eligible loading strategies that could be used.
private function filter_eligible_strategies( $handle, $eligible = null, $checked = array() ) {
// If no strategies are being passed, all strategies are eligible.
if ( null === $eligible ) {
$eligible = $this->delayed_strategies;
// If this handle was already checked, return early.
if ( isset( $checked[ $handle ] ) ) {
// Mark this handle as checked.
$checked[ $handle ] = true;
// If this handle isn't registered, don't filter anything and return.
if ( ! isset( $this->registered[ $handle ] ) ) {
// If the handle is not enqueued, don't filter anything and return.
if ( ! $this->query( $handle, 'enqueued' ) ) {
$is_alias = (bool) ! $this->registered[ $handle ]->src;
$intended_strategy = $this->get_data( $handle, 'strategy' );
// For non-alias handles, an empty intended strategy filters all strategies.
if ( ! $is_alias && empty( $intended_strategy ) ) {
// Handles with inline scripts attached in the 'after' position cannot be delayed.
if ( $this->has_inline_script( $handle, 'after' ) ) {
// If the intended strategy is 'defer', filter out 'async'.
if ( 'defer' === $intended_strategy ) {
$eligible = array( 'defer' );
$dependents = $this->get_dependents( $handle );
// Recursively filter eligible strategies for dependents.
foreach ( $dependents as $dependent ) {
// Bail early once we know the eligible strategy is blocking.
if ( empty( $eligible ) ) {
$eligible = $this->filter_eligible_strategies( $dependent, $eligible, $checked );
* Gets data for inline scripts registered for a specific handle.
* @param string $handle Name of the script to get data for. Must be lowercase.
* @param string $position The position of the inline script.
* @return bool Whether the handle has an inline script (either before or after).
private function has_inline_script( $handle, $position = null ) {
if ( $position && in_array( $position, array( 'before', 'after' ), true ) ) {
return (bool) $this->get_data( $handle, $position );
return (bool) ( $this->get_data( $handle, 'before' ) || $this->get_data( $handle, 'after' ) );
* Resets class properties.