WP_Scripts::filter_eligible_strategies()privateWP 6.3.0

Filter the list of eligible loading strategies for a script.

Method of the class: WP_Scripts{}

No Hooks.

Return

String[]. A list of eligible loading strategies that could be used.

Usage

// private - for code of main (parent) class only
$result = $this->filter_eligible_strategies( $handle, $eligible_strategies, $checked );
$handle(string) (required)
The script handle.
$eligible_strategies(string[]|null)
The list of strategies to filter.
Default: null
$checked **
-
Default: array()

Changelog

Since 6.3.0 Introduced.

WP_Scripts::filter_eligible_strategies() code WP 6.7.1

private function filter_eligible_strategies( $handle, $eligible_strategies = null, $checked = array() ) {
	// If no strategies are being passed, all strategies are eligible.
	if ( null === $eligible_strategies ) {
		$eligible_strategies = $this->delayed_strategies;
	}

	// If this handle was already checked, return early.
	if ( isset( $checked[ $handle ] ) ) {
		return $eligible_strategies;
	}

	// 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 ] ) ) {
		return $eligible_strategies;
	}

	// If the handle is not enqueued, don't filter anything and return.
	if ( ! $this->query( $handle, 'enqueued' ) ) {
		return $eligible_strategies;
	}

	$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 ) ) {
		return array();
	}

	// Handles with inline scripts attached in the 'after' position cannot be delayed.
	if ( $this->has_inline_script( $handle, 'after' ) ) {
		return array();
	}

	// If the intended strategy is 'defer', filter out 'async'.
	if ( 'defer' === $intended_strategy ) {
		$eligible_strategies = 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_strategies ) ) {
			return array();
		}

		$eligible_strategies = $this->filter_eligible_strategies( $dependent, $eligible_strategies, $checked );
	}

	return $eligible_strategies;
}