WP_Scripts::do_item( string $handle, int|false $group = false ): bool

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

Return

bool True on success, false on failure.

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 ( 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' );
	$cond_before       = '';
	$cond_after        = '';
	$conditional       = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';

	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;
	}

	if ( $conditional ) {
		$cond_before = "<!--[if {$conditional}]>\n";
		$cond_after  = "<![endif]-->\n";
	}

	$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 = $cond_before . $before_script . $after_script . $cond_after;
	} 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 ) {
		$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.
		 */
		$srce = apply_filters( 'script_loader_src', $src, $handle );

		if (
			$this->in_default_dir( $srce )
			&& ( $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( $srce ) && ! $conditional ) {
			$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";
		}
	}

	$has_conditional_data = $conditional && $this->get_data( $handle, 'data' );

	if ( $has_conditional_data ) {
		echo $cond_before;
	}

	$this->print_extra_script( $handle );

	if ( $has_conditional_data ) {
		echo $cond_after;
	}

	// 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;
	}
	$tag  = $translations . $cond_before . $before_script;
	$tag .= wp_get_script_tag( $attr );
	$tag .= $after_script . $cond_after;

	/**
	 * 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.

Changelog

VersionDescription
2.8.0Added the $group parameter.
2.6.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.