diff --git a/plugins/woocommerce-admin/client/wp-admin-scripts/command-palette-analytics/index.js b/plugins/woocommerce-admin/client/wp-admin-scripts/command-palette-analytics/index.js index f04a592f368..07cc96e9510 100644 --- a/plugins/woocommerce-admin/client/wp-admin-scripts/command-palette-analytics/index.js +++ b/plugins/woocommerce-admin/client/wp-admin-scripts/command-palette-analytics/index.js @@ -3,6 +3,7 @@ */ import { __, sprintf } from '@wordpress/i18n'; import { chartBar } from '@wordpress/icons'; +import { useEffect } from '@wordpress/element'; import { registerPlugin } from '@wordpress/plugins'; import { addQueryArgs } from '@wordpress/url'; @@ -34,21 +35,24 @@ const registerWooCommerceAnalyticsCommand = ( { label, path, origin } ) => { const WooCommerceAnalyticsCommands = () => { const { editedPostType } = useEditedPostType(); const origin = editedPostType ? editedPostType + '-editor' : null; - if ( - window.hasOwnProperty( 'wcCommandPaletteAnalytics' ) && - window.wcCommandPaletteAnalytics.hasOwnProperty( 'reports' ) && - Array.isArray( window.wcCommandPaletteAnalytics.reports ) - ) { - const analyticsReports = window.wcCommandPaletteAnalytics.reports; - analyticsReports.forEach( ( analyticsReport ) => { - registerWooCommerceAnalyticsCommand( { - label: analyticsReport.title, - path: analyticsReport.path, - origin, + useEffect( () => { + if ( + window.hasOwnProperty( 'wcCommandPaletteAnalytics' ) && + window.wcCommandPaletteAnalytics.hasOwnProperty( 'reports' ) && + Array.isArray( window.wcCommandPaletteAnalytics.reports ) + ) { + const analyticsReports = window.wcCommandPaletteAnalytics.reports; + + analyticsReports.forEach( ( analyticsReport ) => { + registerWooCommerceAnalyticsCommand( { + label: analyticsReport.title, + path: analyticsReport.path, + origin, + } ); } ); - } ); - } + } + }, [ origin ] ); return null; }; diff --git a/plugins/woocommerce-admin/client/wp-admin-scripts/command-palette/index.js b/plugins/woocommerce-admin/client/wp-admin-scripts/command-palette/index.js index 31cfcbaf68c..992404c42dc 100644 --- a/plugins/woocommerce-admin/client/wp-admin-scripts/command-palette/index.js +++ b/plugins/woocommerce-admin/client/wp-admin-scripts/command-palette/index.js @@ -136,71 +136,74 @@ const WooCommerceCommands = () => { wasCommandPaletteOpen.current = isCommandPaletteOpen; }, [ isCommandPaletteOpen, origin ] ); - registerCommandWithTracking( { - name: 'woocommerce/add-new-product', - label: __( 'Add new product', 'woocommerce' ), - icon: plus, - callback: () => { - document.location = addQueryArgs( 'post-new.php', { - post_type: 'product', - } ); - }, - origin, - } ); - registerCommandWithTracking( { - name: 'woocommerce/add-new-order', - label: __( 'Add new order', 'woocommerce' ), - icon: plus, - callback: () => { - document.location = addQueryArgs( 'admin.php', { - page: 'wc-orders', - action: 'new', - } ); - }, - origin, - } ); - registerCommandWithTracking( { - name: 'woocommerce/view-products', - label: __( 'Products', 'woocommerce' ), - icon: box, - callback: () => { - document.location = addQueryArgs( 'edit.php', { - post_type: 'product', - } ); - }, - origin, - } ); - registerCommandWithTracking( { - name: 'woocommerce/view-orders', - label: __( 'Orders', 'woocommerce' ), - icon: box, - callback: () => { - document.location = addQueryArgs( 'admin.php', { - page: 'wc-orders', - } ); - }, - origin, - } ); - dispatch( commandsStore ).registerCommandLoader( { - name: 'woocommerce/product', - hook: useProductCommandLoader, - } ); - - if ( - window.hasOwnProperty( 'wcCommandPaletteSettings' ) && - window.wcCommandPaletteSettings.hasOwnProperty( 'settingsTabs' ) && - Array.isArray( window.wcCommandPaletteSettings.settingsTabs ) - ) { - const settingsCommands = window.wcCommandPaletteSettings.settingsTabs; - - settingsCommands.forEach( ( settingsCommand ) => { - registerWooCommerceSettingsCommand( { - label: settingsCommand.label, - tab: settingsCommand.key, - origin, - } ); + useEffect( () => { + registerCommandWithTracking( { + name: 'woocommerce/add-new-product', + label: __( 'Add new product', 'woocommerce' ), + icon: plus, + callback: () => { + document.location = addQueryArgs( 'post-new.php', { + post_type: 'product', + } ); + }, + origin, } ); - } + registerCommandWithTracking( { + name: 'woocommerce/add-new-order', + label: __( 'Add new order', 'woocommerce' ), + icon: plus, + callback: () => { + document.location = addQueryArgs( 'admin.php', { + page: 'wc-orders', + action: 'new', + } ); + }, + origin, + } ); + registerCommandWithTracking( { + name: 'woocommerce/view-products', + label: __( 'Products', 'woocommerce' ), + icon: box, + callback: () => { + document.location = addQueryArgs( 'edit.php', { + post_type: 'product', + } ); + }, + origin, + } ); + registerCommandWithTracking( { + name: 'woocommerce/view-orders', + label: __( 'Orders', 'woocommerce' ), + icon: box, + callback: () => { + document.location = addQueryArgs( 'admin.php', { + page: 'wc-orders', + } ); + }, + origin, + } ); + dispatch( commandsStore ).registerCommandLoader( { + name: 'woocommerce/product', + hook: useProductCommandLoader, + } ); + + if ( + window.hasOwnProperty( 'wcCommandPaletteSettings' ) && + window.wcCommandPaletteSettings.hasOwnProperty( 'settingsTabs' ) && + Array.isArray( window.wcCommandPaletteSettings.settingsTabs ) + ) { + const settingsCommands = + window.wcCommandPaletteSettings.settingsTabs; + + settingsCommands.forEach( ( settingsCommand ) => { + registerWooCommerceSettingsCommand( { + label: settingsCommand.label, + tab: settingsCommand.key, + origin, + } ); + } ); + } + }, [ origin ] ); return null; }; diff --git a/plugins/woocommerce/changelog/43221-fix-command-palette-unnecessary-scripts b/plugins/woocommerce/changelog/43221-fix-command-palette-unnecessary-scripts new file mode 100644 index 00000000000..63644a9ca19 --- /dev/null +++ b/plugins/woocommerce/changelog/43221-fix-command-palette-unnecessary-scripts @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix `Store "core/interface" is already registered.` error in the block editor. \ No newline at end of file diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-assets.php b/plugins/woocommerce/includes/admin/class-wc-admin-assets.php index bf482c4e3f6..9b5b4e425b7 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-assets.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-assets.php @@ -556,6 +556,25 @@ if ( ! class_exists( 'WC_Admin_Assets', false ) ) : } } + /** + * Enqueue a script in the block editor. + * Similar to `WCAdminAssets::register_script()` but without enqueuing unnecessary dependencies. + * + * @return void + */ + private function enqueue_block_editor_script( $script_path_name, $script_name ) { + $script_assets_filename = WCAdminAssets::get_script_asset_filename( $script_path_name, $script_name ); + $script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/' . $script_assets_filename; + + wp_enqueue_script( + 'wc-admin-' . $script_name, + WCAdminAssets::get_url( $script_path_name . '/' . $script_name, 'js' ), + $script_assets['dependencies'], + WCAdminAssets::get_file_version( 'js' ), + true + ); + } + /** * Enqueue block editor assets. * @@ -578,7 +597,7 @@ if ( ! class_exists( 'WC_Admin_Assets', false ) ) : } } - WCAdminAssets::register_script( 'wp-admin-scripts', 'command-palette' ); + self::enqueue_block_editor_script( 'wp-admin-scripts', 'command-palette' ); wp_localize_script( 'wc-admin-command-palette', 'wcCommandPaletteSettings', @@ -611,7 +630,7 @@ if ( ! class_exists( 'WC_Admin_Assets', false ) ) : }, $analytics_reports ); $formatted_analytics_reports = array_filter( $formatted_analytics_reports, 'is_array' ); - WCAdminAssets::register_script( 'wp-admin-scripts', 'command-palette-analytics' ); + self::enqueue_block_editor_script( 'wp-admin-scripts', 'command-palette-analytics' ); wp_localize_script( 'wc-admin-command-palette-analytics', 'wcCommandPaletteAnalytics',