Processes a script dependency.
Description
See also
Parameters
$handlestringrequired- The script’s registered handle.
$groupint|falseoptional- Group level: level (int), no groups (false).
Default:
false
Source
public function do_item( $handle, $group = false ) {
if ( ! parent::do_item( $handle ) ) {
return false;
}
if ( 0 === $group && $this->groups[ $handle ] > 0 ) {
$this->in_footer[] = $handle;
return false;
}
if ( false === $group && in_array( $handle, $this->in_footer, true ) ) {
$this->in_footer = array_diff( $this->in_footer, (array) $handle );
}
$obj = $this->registered[ $handle ];
if ( $obj->extra['conditional'] ?? false ) {
return false;
}
if ( null === $obj->ver ) {
$ver = '';
} else {
$ver = $obj->ver ? $obj->ver : $this->default_version;
}
if ( isset( $this->args[ $handle ] ) ) {
$ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ];
}
$src = $obj->src;
$strategy = $this->get_eligible_loading_strategy( $handle );
$intended_strategy = (string) $this->get_data( $handle, 'strategy' );
if ( ! $this->is_delayed_strategy( $intended_strategy ) ) {
$intended_strategy = '';
}
/*
* Move this script to the footer if:
* 1. The script is in the header group.
* 2. The current output is the header.
* 3. The intended strategy is delayed.
* 4. The actual strategy is not delayed.
* 5. All dependent scripts are in the footer.
*/
if (
0 === $group &&
0 === $this->groups[ $handle ] &&
$intended_strategy &&
! $this->is_delayed_strategy( $strategy ) &&
$this->are_all_dependents_in_footer( $handle )
) {
$this->in_footer[] = $handle;
return false;
}
$before_script = $this->get_inline_script_tag( $handle, 'before' );
$after_script = $this->get_inline_script_tag( $handle, 'after' );
if ( $before_script || $after_script ) {
$inline_script_tag = $before_script . $after_script;
} else {
$inline_script_tag = '';
}
/*
* Prevent concatenation of scripts if the text domain is defined
* to ensure the dependency order is respected.
*/
$translations_stop_concat = ! empty( $obj->textdomain );
$translations = $this->print_translations( $handle, false );
if ( $translations ) {
/*
* The sourceURL comment is not included by WP_Scripts::print_translations()
* when `$display` is `false` to prevent issues where the script tag contents are used
* by extenders for other purposes, for example concatenated with other script content.
*
* Include the sourceURL comment here as it would be when printed directly.
*/
$source_url = rawurlencode( "{$handle}-js-translations" );
$translations .= "\n//# sourceURL={$source_url}";
$translations = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) );
}
if ( $this->do_concat ) {
/**
* Filters the script loader source.
*
* @since 2.2.0
*
* @param string $src Script loader source path.
* @param string $handle Script handle.
*/
$filtered_src = apply_filters( 'script_loader_src', $src, $handle );
if (
$this->in_default_dir( $filtered_src )
&& ( $before_script || $after_script || $translations_stop_concat || $this->is_delayed_strategy( $strategy ) )
) {
$this->do_concat = false;
// Have to print the so-far concatenated scripts right away to maintain the right order.
_print_scripts();
$this->reset();
} elseif ( $this->in_default_dir( $filtered_src ) ) {
$this->print_code .= $this->print_extra_script( $handle, false );
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
return true;
} else {
$this->ext_handles .= "$handle,";
$this->ext_version .= "$handle$ver";
}
}
$this->print_extra_script( $handle );
// A single item may alias a set of items, by having dependencies, but no source.
if ( ! $src ) {
if ( $inline_script_tag ) {
if ( $this->do_concat ) {
$this->print_html .= $inline_script_tag;
} else {
echo $inline_script_tag;
}
}
return true;
}
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) {
$src = $this->base_url . $src;
}
if ( ! empty( $ver ) ) {
$src = add_query_arg( 'ver', $ver, $src );
}
/** This filter is documented in wp-includes/class-wp-scripts.php */
$src = esc_url_raw( apply_filters( 'script_loader_src', $src, $handle ) );
if ( ! $src ) {
return true;
}
$attr = array(
'src' => $src,
'id' => "{$handle}-js",
);
if ( $strategy ) {
$attr[ $strategy ] = true;
}
if ( $intended_strategy ) {
$attr['data-wp-strategy'] = $intended_strategy;
}
// Determine fetchpriority.
$original_fetchpriority = isset( $obj->extra['fetchpriority'] ) ? $obj->extra['fetchpriority'] : null;
if ( null === $original_fetchpriority || ! $this->is_valid_fetchpriority( $original_fetchpriority ) ) {
$original_fetchpriority = 'auto';
}
$actual_fetchpriority = $this->get_highest_fetchpriority_with_dependents( $handle );
if ( null === $actual_fetchpriority ) {
// If null, it's likely this script was not explicitly enqueued, so in this case use the original priority.
$actual_fetchpriority = $original_fetchpriority;
}
if ( is_string( $actual_fetchpriority ) && 'auto' !== $actual_fetchpriority ) {
$attr['fetchpriority'] = $actual_fetchpriority;
}
if ( $original_fetchpriority !== $actual_fetchpriority ) {
$attr['data-wp-fetchpriority'] = $original_fetchpriority;
}
$tag = $translations . $before_script;
$tag .= wp_get_script_tag( $attr );
$tag .= $after_script;
/**
* Filters the HTML script tag of an enqueued script.
*
* @since 4.1.0
*
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
*/
$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
if ( $this->do_concat ) {
$this->print_html .= $tag;
} else {
echo $tag;
}
return true;
}
Hooks
- apply_filters( ‘script_loader_src’,
string $src ,string $handle ) Filters the script loader source.
- apply_filters( ‘script_loader_tag’,
string $tag ,string $handle ,string $src ) Filters the HTML script tag of an enqueued script.
User Contributed Notes
You must log in before being able to contribute a note or feedback.