WP_Script_Modules::get_recursive_dependentsprivateWP 6.9.0

Gets all recursive dependents of a script module.

Method of the class: WP_Script_Modules{}

No Hooks.

Returns

String[]. Script module IDs.

Usage

// private - for code of main (parent) class only
$result = $this->get_recursive_dependents( $id ): array;
$id(string) (required)
The script ID.

Notes

Changelog

Since 6.9.0 Introduced.

WP_Script_Modules::get_recursive_dependents() code WP 6.9

private function get_recursive_dependents( string $id ): array {
	$dependents = array();
	$id_queue   = array( $id );
	$processed  = array();

	while ( ! empty( $id_queue ) ) {
		$current_id = array_shift( $id_queue );

		// Skip unregistered or already-processed script modules.
		if ( ! isset( $this->registered[ $current_id ] ) || isset( $processed[ $current_id ] ) ) {
			continue;
		}

		// Mark as processed to guard against infinite loops from circular dependencies.
		$processed[ $current_id ] = true;

		// Find the direct dependents of the current script.
		foreach ( $this->get_dependents( $current_id ) as $dependent_id ) {
			// Only add the dependent if we haven't found it before.
			if ( ! isset( $dependents[ $dependent_id ] ) ) {
				$dependents[ $dependent_id ] = true;

				// Add dependency to the queue.
				$id_queue[] = $dependent_id;
			}
		}
	}

	return array_keys( $dependents );
}