Automattic\WooCommerce\Internal\Admin

WCAdminAssets::output_header_preload_tags_for_type()privateWC 1.0

Output a preload link tag for dependencies (and their sub dependencies) with an optional allowlist.

See: https://macarthur.me/posts/preloading-javascript-in-wordpress

Method of the class: WCAdminAssets{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->output_header_preload_tags_for_type( $type, $allowlist );
$type(string) (required)
Dependency type - 'script' or 'style'.
$allowlist(array)
List of allowed dependency handles.
Default: array()

WCAdminAssets::output_header_preload_tags_for_type() code WC 8.7.0

private function output_header_preload_tags_for_type( $type, $allowlist = array() ) {
	if ( $type === 'script' ) {
		$dependencies_of_type = wp_scripts();
	} elseif ( $type === 'style' ) {
		$dependencies_of_type = wp_styles();
	} else {
		return;
	}

	foreach ( $dependencies_of_type->queue as $dependency_handle ) {
		$dependency = $dependencies_of_type->query( $dependency_handle, 'registered' );

		if ( $dependency === false ) {
			continue;
		}

		// Preload the subdependencies first.
		foreach ( $dependency->deps as $sub_dependency_handle ) {
			$sub_dependency = $dependencies_of_type->query( $sub_dependency_handle, 'registered' );

			if ( $sub_dependency ) {
				$this->maybe_output_preload_link_tag( $sub_dependency, $type, $allowlist );
			}
		}

		$this->maybe_output_preload_link_tag( $dependency, $type, $allowlist );
	}
}