Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Remove wc-settings from Mini Cart block dependencies (#8703)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu authored Mar 10, 2023
1 parent 0e7855c commit 69b65af
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 61 deletions.
9 changes: 4 additions & 5 deletions assets/js/blocks/mini-cart/frontend.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 ) {
Expand Down
4 changes: 2 additions & 2 deletions src/Assets/AssetDataRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
143 changes: 89 additions & 54 deletions src/BlockTypes/MiniCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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'] );
Expand Down Expand Up @@ -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' );
}

/**
Expand Down

0 comments on commit 69b65af

Please sign in to comment.