WP_Scripts::get_highest_fetchpriority_with_dependentsprivateWP 6.9.0

Gets the highest fetch priority for a given script and all of its dependent scripts.

Method of the class: WP_Scripts{}

No Hooks.

Returns

String|null. Highest fetch priority for the script and its dependents.

Usage

// private - for code of main (parent) class only
$result = $this->get_highest_fetchpriority_with_dependents( $handle, $checked, $stored_results ): ?string;
$handle(string) (required)
Script module ID.
$checked(array)
.
Default: array()
$stored_results(array)
.
Default: array()

Notes

Changelog

Since 6.9.0 Introduced.

WP_Scripts::get_highest_fetchpriority_with_dependents() code WP 7.0

private function get_highest_fetchpriority_with_dependents( string $handle, array $checked = array(), array &$stored_results = array() ): ?string {
	if ( isset( $stored_results[ $handle ] ) ) {
		return $stored_results[ $handle ];
	}

	// If there is a recursive dependency, return early.
	if ( isset( $checked[ $handle ] ) ) {
		return null;
	}

	// Mark this handle as checked to guard against infinite recursion.
	$checked[ $handle ] = true;

	// Abort if the script is not enqueued or a dependency of an enqueued script.
	if ( ! $this->query( $handle, 'enqueued' ) ) {
		return null;
	}

	$fetchpriority = $this->get_data( $handle, 'fetchpriority' );
	if ( ! $this->is_valid_fetchpriority( $fetchpriority ) ) {
		$fetchpriority = 'auto';
	}

	static $priorities   = array(
		'low',
		'auto',
		'high',
	);
	$high_priority_index = count( $priorities ) - 1;

	$highest_priority_index = (int) array_search( $fetchpriority, $priorities, true );
	if ( $highest_priority_index !== $high_priority_index ) {
		foreach ( $this->get_dependents( $handle ) as $dependent_handle ) {
			$dependent_priority = $this->get_highest_fetchpriority_with_dependents( $dependent_handle, $checked, $stored_results );
			if ( is_string( $dependent_priority ) ) {
				$highest_priority_index = max(
					$highest_priority_index,
					(int) array_search( $dependent_priority, $priorities, true )
				);
				if ( $highest_priority_index === $high_priority_index ) {
					break;
				}
			}
		}
	}
	$stored_results[ $handle ] = $priorities[ $highest_priority_index ];
	return $priorities[ $highest_priority_index ];
}