Automattic\WooCommerce\Blocks\BlockTypes

MiniCart::print_lazy_load_scripts()publicWC 1.0

Prints the variable containing information about the scripts to lazy load.

Method of the class: MiniCart{}

No Hooks.

Return

null. Nothing (null).

Usage

$MiniCart = new MiniCart();
$MiniCart->print_lazy_load_scripts();

MiniCart::print_lazy_load_scripts() code WC 9.4.2

public function print_lazy_load_scripts() {
	$script_data = $this->asset_api->get_script_data( 'assets/client/blocks/mini-cart-component-frontend.js' );

	$num_dependencies = is_countable( $script_data['dependencies'] ) ? count( $script_data['dependencies'] ) : 0;
	$wp_scripts       = wp_scripts();

	for ( $i = 0; $i < $num_dependencies; $i++ ) {
		$dependency = $script_data['dependencies'][ $i ];

		foreach ( $wp_scripts->registered as $script ) {
			if ( $script->handle === $dependency ) {
				$this->append_script_and_deps_src( $script );
				break;
			}
		}
	}

	$payment_method_registry = Package::container()->get( PaymentMethodRegistry::class );
	$payment_methods         = $payment_method_registry->get_all_active_payment_method_script_dependencies();

	foreach ( $payment_methods as $payment_method ) {
		$payment_method_script = $this->get_script_from_handle( $payment_method );

		if ( ! is_null( $payment_method_script ) ) {
			$this->append_script_and_deps_src( $payment_method_script );
		}
	}

	$this->scripts_to_lazy_load['wc-block-mini-cart-component-frontend'] = array(
		'src'          => $script_data['src'],
		'version'      => $script_data['version'],
		'translations' => $this->get_inner_blocks_translations(),
	);

	$inner_blocks_frontend_scripts = array();
	$cart                          = $this->get_cart_instance();
	if ( $cart ) {
		// Preload inner blocks frontend scripts.
		$inner_blocks_frontend_scripts = $cart->is_empty() ? array(
			'empty-cart-frontend',
			'filled-cart-frontend',
			'shopping-button-frontend',
		) : array(
			'empty-cart-frontend',
			'filled-cart-frontend',
			'title-frontend',
			'items-frontend',
			'footer-frontend',
			'products-table-frontend',
			'cart-button-frontend',
			'checkout-button-frontend',
			'title-label-frontend',
			'title-items-counter-frontend',
		);
	}
	foreach ( $inner_blocks_frontend_scripts as $inner_block_frontend_script ) {
		$script_data = $this->asset_api->get_script_data( 'assets/client/blocks/mini-cart-contents-block/' . $inner_block_frontend_script . '.js' );
		$this->scripts_to_lazy_load[ 'wc-block-' . $inner_block_frontend_script ] = array(
			'src'     => $script_data['src'],
			'version' => $script_data['version'],
		);
	}

	$data                          = rawurlencode( wp_json_encode( $this->scripts_to_lazy_load ) );
	$mini_cart_dependencies_script = "var wcBlocksMiniCartFrontendDependencies = JSON.parse( decodeURIComponent( '" . esc_js( $data ) . "' ) );";

	wp_add_inline_script(
		'wc-mini-cart-block-frontend',
		$mini_cart_dependencies_script,
		'before'
	);
}