diff --git a/assets/js/blocks/mini-cart/frontend.ts b/assets/js/blocks/mini-cart/frontend.ts index 90dbbfec7bc..79bc9a65ec8 100644 --- a/assets/js/blocks/mini-cart/frontend.ts +++ b/assets/js/blocks/mini-cart/frontend.ts @@ -1,7 +1,6 @@ /** * External dependencies */ -import { getSetting } from '@woocommerce/settings'; import preloadScript from '@woocommerce/base-utils/preload-script'; import lazyLoadScript from '@woocommerce/base-utils/lazy-load-script'; import getNavigationType from '@woocommerce/base-utils/get-navigation-type'; @@ -23,10 +22,10 @@ window.addEventListener( 'load', () => { return; } - const dependencies = getSetting( - 'mini_cart_block_frontend_dependencies', - {} - ) as Record< string, dependencyData >; + const dependencies = window.wcBlocksMiniCartFrontendDependencies as Record< + string, + dependencyData + >; // Preload scripts for ( const dependencyHandle in dependencies ) { diff --git a/src/Assets/AssetDataRegistry.php b/src/Assets/AssetDataRegistry.php index 521e7000cac..a72a81c960d 100644 --- a/src/Assets/AssetDataRegistry.php +++ b/src/Assets/AssetDataRegistry.php @@ -67,8 +67,8 @@ public function __construct( Api $asset_api ) { */ protected function init() { add_action( 'init', array( $this, 'register_data_script' ) ); - add_action( 'wp_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 ); - add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 ); + add_action( 'wp_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 2 ); + add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 2 ); } /** diff --git a/src/BlockTypes/MiniCart.php b/src/BlockTypes/MiniCart.php index 8797b027176..58d40c056e1 100644 --- a/src/BlockTypes/MiniCart.php +++ b/src/BlockTypes/MiniCart.php @@ -70,6 +70,9 @@ public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_ protected function initialize() { parent::initialize(); add_action( 'wp_loaded', array( $this, 'register_empty_cart_message_block_pattern' ) ); + add_action( 'wp_print_footer_scripts', array( $this, 'enqueue_wc_settings' ), 1 ); + // We need this action to run after the equivalent in AssetDataRegistry. + add_action( 'wp_print_footer_scripts', array( $this, 'print_lazy_load_scripts' ), 3 ); } /** @@ -149,6 +152,86 @@ protected function enqueue_data( array $attributes = [] ) { ); } + $this->asset_data_registry->add( + 'displayCartPricesIncludingTax', + $this->display_cart_prices_including_tax, + true + ); + + $template_part_edit_uri = ''; + + if ( + current_user_can( 'edit_theme_options' ) && + wc_current_theme_is_fse_theme() + ) { + $theme_slug = BlockTemplateUtils::theme_has_template_part( 'mini-cart' ) ? wp_get_theme()->get_stylesheet() : BlockTemplateUtils::PLUGIN_SLUG; + + if ( version_compare( get_bloginfo( 'version' ), '5.9', '<' ) ) { + $site_editor_uri = add_query_arg( + array( 'page' => 'gutenberg-edit-site' ), + admin_url( 'themes.php' ) + ); + } else { + $site_editor_uri = add_query_arg( + array( + 'canvas' => 'edit', + 'path' => '/template-parts/single', + ), + admin_url( 'site-editor.php' ) + ); + } + + $template_part_edit_uri = add_query_arg( + array( + 'postId' => sprintf( '%s//%s', $theme_slug, 'mini-cart' ), + 'postType' => 'wp_template_part', + ), + $site_editor_uri + ); + } + + $this->asset_data_registry->add( + 'templatePartEditUri', + $template_part_edit_uri, + '' + ); + + /** + * Fires after cart block data is registered. + * + * @since 5.8.0 + */ + do_action( 'woocommerce_blocks_cart_enqueue_data' ); + } + + /** + * Function to enqueue `wc-settings` script and dequeue it later on so when + * AssetDataRegistry runs, it appears enqueued- This allows the necessary + * data to be printed to the page. + */ + public function enqueue_wc_settings() { + // Return early if another block has already enqueued `wc-settings`. + if ( wp_script_is( 'wc-settings', 'enqueued' ) ) { + return; + } + // We are lazy-loading `wc-settings`, but we need to enqueue it here so + // AssetDataRegistry knows it's going to load. + wp_enqueue_script( 'wc-settings' ); + // After AssetDataRegistry function runs, we dequeue `wc-settings`. + add_action( 'wp_print_footer_scripts', array( $this, 'dequeue_wc_settings' ), 4 ); + } + + /** + * Function to dequeue `wc-settings` script. + */ + public function dequeue_wc_settings() { + wp_dequeue_script( 'wc-settings' ); + } + + /** + * Prints the variable containing information about the scripts to lazy load. + */ + public function print_lazy_load_scripts() { $script_data = $this->asset_api->get_script_data( 'build/mini-cart-component-frontend.js' ); $num_dependencies = count( $script_data['dependencies'] ); @@ -207,62 +290,14 @@ protected function enqueue_data( array $attributes = [] ) { ); } - $this->asset_data_registry->add( - 'mini_cart_block_frontend_dependencies', - $this->scripts_to_lazy_load, - true - ); - - $this->asset_data_registry->add( - 'displayCartPricesIncludingTax', - $this->display_cart_prices_including_tax, - true - ); - - $template_part_edit_uri = ''; - - if ( - current_user_can( 'edit_theme_options' ) && - wc_current_theme_is_fse_theme() - ) { - $theme_slug = BlockTemplateUtils::theme_has_template_part( 'mini-cart' ) ? wp_get_theme()->get_stylesheet() : BlockTemplateUtils::PLUGIN_SLUG; - - if ( version_compare( get_bloginfo( 'version' ), '5.9', '<' ) ) { - $site_editor_uri = add_query_arg( - array( 'page' => 'gutenberg-edit-site' ), - admin_url( 'themes.php' ) - ); - } else { - $site_editor_uri = add_query_arg( - array( - 'canvas' => 'edit', - 'path' => '/template-parts/single', - ), - admin_url( 'site-editor.php' ) - ); - } - - $template_part_edit_uri = add_query_arg( - array( - 'postId' => sprintf( '%s//%s', $theme_slug, 'mini-cart' ), - 'postType' => 'wp_template_part', - ), - $site_editor_uri - ); - } + $data = rawurlencode( wp_json_encode( $this->scripts_to_lazy_load ) ); + $mini_cart_dependencies_script = "var wcBlocksMiniCartFrontendDependencies = JSON.parse( decodeURIComponent( '" . esc_js( $data ) . "' ) );"; - $this->asset_data_registry->add( - 'templatePartEditUri', - $template_part_edit_uri, - '' + wp_add_inline_script( + 'wc-mini-cart-block-frontend', + $mini_cart_dependencies_script, + 'before' ); - - /** - * Fires after cart block data is registered. - * - * @since 5.8.0 - */ - do_action( 'woocommerce_blocks_cart_enqueue_data' ); } /**