Automattic\WooCommerce\Internal\Admin

WCAdminAssets::maybe_output_preload_link_tag()privateWC 1.0

Render a preload link tag for a dependency, optionally checked against a provided 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->maybe_output_preload_link_tag( $dependency, $type, $allowlist );
$dependency(WP_Dependency) (required)
The WP_Dependency being preloaded.
$type(string) (required)
Dependency type - 'script' or 'style'.
$allowlist(array)
List of allowed dependency handles.
Default: array()

WCAdminAssets::maybe_output_preload_link_tag() code WC 8.7.0

private function maybe_output_preload_link_tag( $dependency, $type, $allowlist = array() ) {
	if (
		(
			! empty( $allowlist ) &&
			! in_array( $dependency->handle, $allowlist, true )
		) ||
		( ! empty( $this->preloaded_dependencies[ $type ] ) &&
		in_array( $dependency->handle, $this->preloaded_dependencies[ $type ], true ) )
	) {
		return;
	}

	$this->preloaded_dependencies[ $type ][] = $dependency->handle;

	$source = $dependency->ver ? add_query_arg( 'ver', $dependency->ver, $dependency->src ) : $dependency->src;

	echo '<link rel="preload" href="', esc_url( $source ), '" as="', esc_attr( $type ), '" />', "\n";
}