Cleanup code to lazy-load wc-settings in the Mini-Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/10042)
This commit is contained in:
parent
b5080e379d
commit
74e39331fb
|
@ -67,7 +67,7 @@ class AssetDataRegistry {
|
|||
*/
|
||||
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 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -70,10 +70,7 @@ class MiniCart extends AbstractBlock {
|
|||
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 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -193,30 +190,6 @@ class MiniCart extends AbstractBlock {
|
|||
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.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue