Gets the highest fetch priority for a given script and all of its dependent scripts.
Description
See also
Parameters
$handlestringrequired- Script module ID.
- string> $stored_results Optional. An array of already computed max priority by handle, used to increase performance in large dependency lists.
Source
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 ]; // @phpstan-ignore parameterByRef.type (We know the index is valid and that this will be a string.)
return $priorities[ $highest_priority_index ];
}
Changelog
| Version | Description |
|---|---|
| 6.9.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.