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

Commit

Permalink
Cleanup code to lazy-load wc-settings in the Mini-Cart block (#10042)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu authored Jul 3, 2023
1 parent a14891f commit a775fcb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Assets/AssetDataRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct( Api $asset_api ) {
*/
protected function init() {
add_action( 'init', array( $this, 'register_data_script' ) );
add_action( is_admin() ? 'admin_print_footer_scripts' : 'wp_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 2 );
add_action( is_admin() ? 'admin_print_footer_scripts' : 'wp_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 );
}

/**
Expand Down
29 changes: 1 addition & 28 deletions src/BlockTypes/MiniCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ 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 enqueue_wc_settings() and dequeue_wc_settings(),
// otherwise it might incorrectly consider wc_settings script to be enqueued.
add_action( 'wp_print_footer_scripts', array( $this, 'print_lazy_load_scripts' ), 4 );
add_action( 'wp_print_footer_scripts', array( $this, 'print_lazy_load_scripts' ), 2 );
}

/**
Expand Down Expand Up @@ -193,30 +190,6 @@ protected function enqueue_data( array $attributes = [] ) {
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' ), 3 );
}

/**
* 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.
*/
Expand Down

0 comments on commit a775fcb

Please sign in to comment.