This overrides the add_data method from WP_Dependencies, to support normalizing of $args.
Parameters
$handlestringrequired- Name of the item. Should be unique.
$keystringrequired- The data key.
$valuemixedrequired- The data value.
Source
public function add_data( $handle, $key, $value ) {
if ( ! isset( $this->registered[ $handle ] ) ) {
return false;
}
if ( 'conditional' === $key ) {
// If a dependency is declared by a conditional script, remove it.
$this->registered[ $handle ]->deps = array();
}
if ( 'strategy' === $key ) {
if ( ! empty( $value ) && ! $this->is_delayed_strategy( $value ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: $strategy, 2: $handle */
__( 'Invalid strategy `%1$s` defined for `%2$s` during script registration.' ),
is_string( $value ) ? $value : gettype( $value ),
$handle
),
'6.3.0'
);
return false;
} elseif ( ! $this->registered[ $handle ]->src && $this->is_delayed_strategy( $value ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* 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).' ),
is_string( $value ) ? $value : gettype( $value ),
$handle
),
'6.3.0'
);
return false;
}
} elseif ( 'fetchpriority' === $key ) {
if ( empty( $value ) ) {
$value = 'auto';
}
if ( ! $this->is_valid_fetchpriority( $value ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: $fetchpriority, 2: $handle */
__( 'Invalid fetchpriority `%1$s` defined for `%2$s` during script registration.' ),
is_string( $value ) ? $value : gettype( $value ),
$handle
),
'6.9.0'
);
return false;
} elseif ( ! $this->registered[ $handle ]->src ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: $fetchpriority, 2: $handle */
__( 'Cannot supply a fetchpriority `%1$s` for script `%2$s` because it is an alias (it lacks a `src` value).' ),
is_string( $value ) ? $value : gettype( $value ),
$handle
),
'6.9.0'
);
return false;
}
} elseif ( 'module_dependencies' === $key ) {
if ( ! is_array( $value ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: 'module_dependencies', 2: Script handle. */
__( 'The value for "%1$s" must be an array for the "%2$s" script.' ),
'module_dependencies',
$handle
),
'7.0.0'
);
return false;
}
$sanitized_value = array();
$has_invalid_ids = false;
foreach ( $value as $module ) {
if (
is_string( $module ) ||
( is_array( $module ) && isset( $module['id'] ) && is_string( $module['id'] ) )
) {
$sanitized_value[] = $module;
} else {
$has_invalid_ids = true;
}
}
if ( $has_invalid_ids ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: Script handle, 2: 'module_dependencies' */
__( 'The script handle "%1$s" has one or more of its script module dependencies ("%2$s") which are invalid.' ),
$handle,
'module_dependencies'
),
'7.0.0'
);
}
$value = $sanitized_value;
}
return parent::add_data( $handle, $key, $value );
}
Changelog
| Version | Description |
|---|---|
| 6.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.