Automattic\WooCommerce\Blocks

AssetsController::add_resource_hints()publicWC 1.0

Defines resource hints to help speed up the loading of some critical blocks.

These will not impact page loading times negatively because they are loaded once the current page is idle.

{} Это метод класса: AssetsController{}

Хуков нет.

Возвращает

Массив. URLs to print for resource hints.

Использование

$AssetsController = new AssetsController();
$AssetsController->add_resource_hints( $urls, $relation_type );
$urls(массив) (обязательный)
URLs to print for resource hints. Each URL is an array of resource attributes, or a URL string.
$relation_type(строка) (обязательный)
The relation type the URLs are printed. Possible values: preconnect, dns-prefetch, prefetch, prerender.

Код AssetsController::add_resource_hints() WC 6.8.0

public function add_resource_hints( $urls, $relation_type ) {
	if ( ! Package::feature()->is_feature_plugin_build() || ! in_array( $relation_type, [ 'prefetch', 'prerender' ], true ) ) {
		return $urls;
	}

	// We only need to prefetch when the cart has contents.
	$cart = wc()->cart;

	if ( ! $cart || ! $cart instanceof \WC_Cart || 0 === $cart->get_cart_contents_count() ) {
		return $urls;
	}

	$resources         = [];
	$is_block_cart     = has_block( 'woocommerce/cart' );
	$is_block_checkout = has_block( 'woocommerce/checkout' );

	if ( 'prefetch' === $relation_type ) {
		if ( ! $is_block_cart ) {
			$resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'cart-frontend' ) );
		}

		if ( ! $is_block_checkout ) {
			$resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'checkout-frontend' ) );
		}

		$urls = array_merge(
			$urls,
			array_map(
				function( $src ) {
					return array(
						'href' => $src,
						'as'   => 'script',
					);
				},
				array_unique( array_filter( $resources ) )
			)
		);
	}

	if ( 'prerender' === $relation_type && $is_block_cart ) {
		$checkout_page_id  = wc_get_page_id( 'checkout' );
		$checkout_page_url = $checkout_page_id ? get_permalink( $checkout_page_id ) : '';
		if ( $checkout_page_url ) {
			$urls[] = $checkout_page_url;
		}
	}

	return $urls;
}