WP_Scripts::get_eligible_loading_strategy()privateWP 6.3.0

Gets the best eligible loading strategy for a script.

Method of the class: WP_Scripts{}

No Hooks.

Return

String. The best eligible loading strategy.

Usage

// private - for code of main (parent) class only
$result = $this->get_eligible_loading_strategy( $handle );
$handle(string) (required)
The script handle.

Changelog

Since 6.3.0 Introduced.

WP_Scripts::get_eligible_loading_strategy() code WP 6.7.1

private function get_eligible_loading_strategy( $handle ) {
	$intended_strategy = (string) $this->get_data( $handle, 'strategy' );

	// Bail early if there is no intended strategy.
	if ( ! $intended_strategy ) {
		return '';
	}

	/*
	 * If the intended strategy is 'defer', limit the initial list of eligible
	 * strategies, since 'async' can fallback to 'defer', but not vice-versa.
	 */
	$initial_strategy = ( 'defer' === $intended_strategy ) ? array( 'defer' ) : null;

	$eligible_strategies = $this->filter_eligible_strategies( $handle, $initial_strategy );

	// Return early once we know the eligible strategy is blocking.
	if ( empty( $eligible_strategies ) ) {
		return '';
	}

	return in_array( 'async', $eligible_strategies, true ) ? 'async' : 'defer';
}