Prevent Command Palette scripts to enqueue unnecessary scripts in the editor (#43221)

* Prevent Command Palette scripts to enqueue unnecessary scripts in the editor

* Add changefile(s) from automation for the following project(s): woocommerce

* Properly set script asset dependencies

* Create util function to simplify the code

* Fix JS warning when registering WooCommerce Commands

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Albert Juhé Lluveras 2024-01-03 18:07:43 +01:00 committed by GitHub
parent 7163d610e5
commit 263011bdd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 109 additions and 79 deletions

View File

@ -3,6 +3,7 @@
*/ */
import { __, sprintf } from '@wordpress/i18n'; import { __, sprintf } from '@wordpress/i18n';
import { chartBar } from '@wordpress/icons'; import { chartBar } from '@wordpress/icons';
import { useEffect } from '@wordpress/element';
import { registerPlugin } from '@wordpress/plugins'; import { registerPlugin } from '@wordpress/plugins';
import { addQueryArgs } from '@wordpress/url'; import { addQueryArgs } from '@wordpress/url';
@ -34,6 +35,8 @@ const registerWooCommerceAnalyticsCommand = ( { label, path, origin } ) => {
const WooCommerceAnalyticsCommands = () => { const WooCommerceAnalyticsCommands = () => {
const { editedPostType } = useEditedPostType(); const { editedPostType } = useEditedPostType();
const origin = editedPostType ? editedPostType + '-editor' : null; const origin = editedPostType ? editedPostType + '-editor' : null;
useEffect( () => {
if ( if (
window.hasOwnProperty( 'wcCommandPaletteAnalytics' ) && window.hasOwnProperty( 'wcCommandPaletteAnalytics' ) &&
window.wcCommandPaletteAnalytics.hasOwnProperty( 'reports' ) && window.wcCommandPaletteAnalytics.hasOwnProperty( 'reports' ) &&
@ -49,6 +52,7 @@ const WooCommerceAnalyticsCommands = () => {
} ); } );
} ); } );
} }
}, [ origin ] );
return null; return null;
}; };

View File

@ -136,6 +136,7 @@ const WooCommerceCommands = () => {
wasCommandPaletteOpen.current = isCommandPaletteOpen; wasCommandPaletteOpen.current = isCommandPaletteOpen;
}, [ isCommandPaletteOpen, origin ] ); }, [ isCommandPaletteOpen, origin ] );
useEffect( () => {
registerCommandWithTracking( { registerCommandWithTracking( {
name: 'woocommerce/add-new-product', name: 'woocommerce/add-new-product',
label: __( 'Add new product', 'woocommerce' ), label: __( 'Add new product', 'woocommerce' ),
@ -191,7 +192,8 @@ const WooCommerceCommands = () => {
window.wcCommandPaletteSettings.hasOwnProperty( 'settingsTabs' ) && window.wcCommandPaletteSettings.hasOwnProperty( 'settingsTabs' ) &&
Array.isArray( window.wcCommandPaletteSettings.settingsTabs ) Array.isArray( window.wcCommandPaletteSettings.settingsTabs )
) { ) {
const settingsCommands = window.wcCommandPaletteSettings.settingsTabs; const settingsCommands =
window.wcCommandPaletteSettings.settingsTabs;
settingsCommands.forEach( ( settingsCommand ) => { settingsCommands.forEach( ( settingsCommand ) => {
registerWooCommerceSettingsCommand( { registerWooCommerceSettingsCommand( {
@ -201,6 +203,7 @@ const WooCommerceCommands = () => {
} ); } );
} ); } );
} }
}, [ origin ] );
return null; return null;
}; };

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix `Store "core/interface" is already registered.` error in the block editor.

View File

@ -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. * 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( wp_localize_script(
'wc-admin-command-palette', 'wc-admin-command-palette',
'wcCommandPaletteSettings', 'wcCommandPaletteSettings',
@ -611,7 +630,7 @@ if ( ! class_exists( 'WC_Admin_Assets', false ) ) :
}, $analytics_reports ); }, $analytics_reports );
$formatted_analytics_reports = array_filter( $formatted_analytics_reports, 'is_array' ); $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( wp_localize_script(
'wc-admin-command-palette-analytics', 'wc-admin-command-palette-analytics',
'wcCommandPaletteAnalytics', 'wcCommandPaletteAnalytics',