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:
parent
7163d610e5
commit
263011bdd2
|
@ -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,21 +35,24 @@ 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;
|
||||||
if (
|
|
||||||
window.hasOwnProperty( 'wcCommandPaletteAnalytics' ) &&
|
|
||||||
window.wcCommandPaletteAnalytics.hasOwnProperty( 'reports' ) &&
|
|
||||||
Array.isArray( window.wcCommandPaletteAnalytics.reports )
|
|
||||||
) {
|
|
||||||
const analyticsReports = window.wcCommandPaletteAnalytics.reports;
|
|
||||||
|
|
||||||
analyticsReports.forEach( ( analyticsReport ) => {
|
useEffect( () => {
|
||||||
registerWooCommerceAnalyticsCommand( {
|
if (
|
||||||
label: analyticsReport.title,
|
window.hasOwnProperty( 'wcCommandPaletteAnalytics' ) &&
|
||||||
path: analyticsReport.path,
|
window.wcCommandPaletteAnalytics.hasOwnProperty( 'reports' ) &&
|
||||||
origin,
|
Array.isArray( window.wcCommandPaletteAnalytics.reports )
|
||||||
|
) {
|
||||||
|
const analyticsReports = window.wcCommandPaletteAnalytics.reports;
|
||||||
|
|
||||||
|
analyticsReports.forEach( ( analyticsReport ) => {
|
||||||
|
registerWooCommerceAnalyticsCommand( {
|
||||||
|
label: analyticsReport.title,
|
||||||
|
path: analyticsReport.path,
|
||||||
|
origin,
|
||||||
|
} );
|
||||||
} );
|
} );
|
||||||
} );
|
}
|
||||||
}
|
}, [ origin ] );
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
|
@ -136,71 +136,74 @@ const WooCommerceCommands = () => {
|
||||||
wasCommandPaletteOpen.current = isCommandPaletteOpen;
|
wasCommandPaletteOpen.current = isCommandPaletteOpen;
|
||||||
}, [ isCommandPaletteOpen, origin ] );
|
}, [ isCommandPaletteOpen, origin ] );
|
||||||
|
|
||||||
registerCommandWithTracking( {
|
useEffect( () => {
|
||||||
name: 'woocommerce/add-new-product',
|
registerCommandWithTracking( {
|
||||||
label: __( 'Add new product', 'woocommerce' ),
|
name: 'woocommerce/add-new-product',
|
||||||
icon: plus,
|
label: __( 'Add new product', 'woocommerce' ),
|
||||||
callback: () => {
|
icon: plus,
|
||||||
document.location = addQueryArgs( 'post-new.php', {
|
callback: () => {
|
||||||
post_type: 'product',
|
document.location = addQueryArgs( 'post-new.php', {
|
||||||
} );
|
post_type: 'product',
|
||||||
},
|
} );
|
||||||
origin,
|
},
|
||||||
} );
|
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,
|
|
||||||
} );
|
|
||||||
} );
|
} );
|
||||||
}
|
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;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: patch
|
||||||
|
Type: fix
|
||||||
|
|
||||||
|
Fix `Store "core/interface" is already registered.` error in the block editor.
|
|
@ -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',
|
||||||
|
|
Loading…
Reference in New Issue