2023-11-28 11:26:30 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { registerPlugin } from '@wordpress/plugins';
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
|
|
|
import { box, plus, settings } from '@wordpress/icons';
|
2023-12-18 13:36:53 +00:00
|
|
|
import { useEffect, useMemo, useRef } from '@wordpress/element';
|
2023-11-28 11:26:30 +00:00
|
|
|
import { dispatch, useSelect } from '@wordpress/data';
|
|
|
|
import { store as coreStore } from '@wordpress/core-data';
|
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
2023-12-18 13:36:53 +00:00
|
|
|
import { recordEvent, queueRecordEvent } from '@woocommerce/tracks';
|
2023-11-28 11:26:30 +00:00
|
|
|
import { store as commandsStore } from '@wordpress/commands';
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { registerCommandWithTracking } from './register-command-with-tracking';
|
2023-12-18 13:36:53 +00:00
|
|
|
import { useEditedPostType } from './use-edited-post-type';
|
2023-11-28 11:26:30 +00:00
|
|
|
|
2023-12-18 13:36:53 +00:00
|
|
|
const registerWooCommerceSettingsCommand = ( { label, tab, origin } ) => {
|
2023-11-28 11:26:30 +00:00
|
|
|
registerCommandWithTracking( {
|
|
|
|
name: `woocommerce/settings-${ tab }`,
|
|
|
|
label: sprintf(
|
|
|
|
// translators: %s is the title of the Settings Tab. This is used as a command in the Command Palette.
|
|
|
|
__( 'WooCommerce Settings: %s', 'woocommerce' ),
|
|
|
|
label
|
|
|
|
),
|
|
|
|
icon: settings,
|
|
|
|
callback: () => {
|
|
|
|
document.location = addQueryArgs( 'admin.php', {
|
|
|
|
page: 'wc-settings',
|
|
|
|
tab,
|
|
|
|
} );
|
|
|
|
},
|
2023-12-18 13:36:53 +00:00
|
|
|
origin,
|
2023-11-28 11:26:30 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Code adapted from the equivalent in Gutenberg:
|
|
|
|
// https://github.com/WordPress/gutenberg/blob/8863b49b7e686f555e8b8adf70cc588c4feebfbf/packages/core-commands/src/site-editor-navigation-commands.js#L36C7-L36C44
|
|
|
|
function useProductCommandLoader( { search } ) {
|
2023-12-18 13:36:53 +00:00
|
|
|
const { editedPostType } = useEditedPostType();
|
|
|
|
const origin = editedPostType ? editedPostType + '-editor' : null;
|
|
|
|
// Track searched values. We add a 300 ms delay to avoid tracking while typing.
|
|
|
|
const trackingSearchTimeout = useRef( null );
|
|
|
|
useEffect( () => {
|
|
|
|
if ( search !== '' ) {
|
|
|
|
clearTimeout( trackingSearchTimeout.current );
|
|
|
|
trackingSearchTimeout.current = setTimeout( () => {
|
|
|
|
recordEvent( 'woocommerce_command_palette_search', {
|
|
|
|
value: search,
|
|
|
|
origin,
|
|
|
|
} );
|
|
|
|
}, 300 );
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
clearTimeout( trackingSearchTimeout.current );
|
|
|
|
};
|
|
|
|
}, [ search, origin ] );
|
|
|
|
|
2023-11-28 11:26:30 +00:00
|
|
|
const postType = 'product';
|
|
|
|
const { records, isLoading } = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
const { getEntityRecords } = select( coreStore );
|
|
|
|
const query = {
|
|
|
|
search: !! search ? search : undefined,
|
|
|
|
per_page: 10,
|
|
|
|
orderby: search ? 'relevance' : 'date',
|
|
|
|
status: [ 'publish', 'future', 'draft', 'pending', 'private' ],
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
records: getEntityRecords( 'postType', postType, query ),
|
|
|
|
isLoading: ! select( coreStore ).hasFinishedResolution(
|
|
|
|
'getEntityRecords',
|
|
|
|
[ 'postType', postType, query ]
|
|
|
|
),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[ search ]
|
|
|
|
);
|
|
|
|
|
|
|
|
const commands = useMemo( () => {
|
|
|
|
return ( records ?? [] ).map( ( record ) => {
|
|
|
|
const command = {
|
|
|
|
name: postType + '-' + record.id,
|
|
|
|
searchLabel: record.title?.rendered + ' ' + record.id,
|
|
|
|
label: record.title?.rendered
|
|
|
|
? decodeEntities( record.title?.rendered )
|
|
|
|
: __( '(no title)', 'woocommerce' ),
|
|
|
|
icon: box,
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
...command,
|
|
|
|
callback: ( { close } ) => {
|
|
|
|
queueRecordEvent( 'woocommerce_command_palette_submit', {
|
|
|
|
name: 'woocommerce/product',
|
2023-12-18 13:36:53 +00:00
|
|
|
origin,
|
2023-11-28 11:26:30 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
post: record.id,
|
|
|
|
action: 'edit',
|
|
|
|
};
|
|
|
|
const targetUrl = addQueryArgs( 'post.php', args );
|
|
|
|
document.location = targetUrl;
|
|
|
|
close();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
2023-12-18 13:36:53 +00:00
|
|
|
}, [ records, origin ] );
|
2023-11-28 11:26:30 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
commands,
|
|
|
|
isLoading,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const WooCommerceCommands = () => {
|
2023-12-18 13:36:53 +00:00
|
|
|
const { editedPostType } = useEditedPostType();
|
|
|
|
const origin = editedPostType ? editedPostType + '-editor' : null;
|
|
|
|
const { isCommandPaletteOpen } = useSelect( ( select ) => {
|
|
|
|
const { isOpen } = select( commandsStore );
|
|
|
|
return {
|
|
|
|
isCommandPaletteOpen: isOpen(),
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
const wasCommandPaletteOpen = useRef( false );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( isCommandPaletteOpen && ! wasCommandPaletteOpen.current ) {
|
|
|
|
recordEvent( 'woocommerce_command_palette_open', {
|
|
|
|
origin,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
wasCommandPaletteOpen.current = isCommandPaletteOpen;
|
|
|
|
}, [ isCommandPaletteOpen, origin ] );
|
|
|
|
|
2023-11-28 11:26:30 +00:00
|
|
|
registerCommandWithTracking( {
|
|
|
|
name: 'woocommerce/add-new-product',
|
|
|
|
label: __( 'Add new product', 'woocommerce' ),
|
|
|
|
icon: plus,
|
|
|
|
callback: () => {
|
|
|
|
document.location = addQueryArgs( 'post-new.php', {
|
|
|
|
post_type: 'product',
|
|
|
|
} );
|
|
|
|
},
|
2023-12-18 13:36:53 +00:00
|
|
|
origin,
|
2023-11-28 11:26:30 +00:00
|
|
|
} );
|
|
|
|
registerCommandWithTracking( {
|
|
|
|
name: 'woocommerce/add-new-order',
|
|
|
|
label: __( 'Add new order', 'woocommerce' ),
|
|
|
|
icon: plus,
|
|
|
|
callback: () => {
|
|
|
|
document.location = addQueryArgs( 'admin.php', {
|
|
|
|
page: 'wc-orders',
|
|
|
|
action: 'new',
|
|
|
|
} );
|
|
|
|
},
|
2023-12-18 13:36:53 +00:00
|
|
|
origin,
|
2023-11-28 11:26:30 +00:00
|
|
|
} );
|
|
|
|
registerCommandWithTracking( {
|
|
|
|
name: 'woocommerce/view-products',
|
|
|
|
label: __( 'Products', 'woocommerce' ),
|
|
|
|
icon: box,
|
|
|
|
callback: () => {
|
|
|
|
document.location = addQueryArgs( 'edit.php', {
|
|
|
|
post_type: 'product',
|
|
|
|
} );
|
|
|
|
},
|
2023-12-18 13:36:53 +00:00
|
|
|
origin,
|
2023-11-28 11:26:30 +00:00
|
|
|
} );
|
|
|
|
registerCommandWithTracking( {
|
|
|
|
name: 'woocommerce/view-orders',
|
|
|
|
label: __( 'Orders', 'woocommerce' ),
|
|
|
|
icon: box,
|
|
|
|
callback: () => {
|
|
|
|
document.location = addQueryArgs( 'admin.php', {
|
|
|
|
page: 'wc-orders',
|
|
|
|
} );
|
|
|
|
},
|
2023-12-18 13:36:53 +00:00
|
|
|
origin,
|
2023-11-28 11:26:30 +00:00
|
|
|
} );
|
|
|
|
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,
|
2023-12-18 13:36:53 +00:00
|
|
|
origin,
|
2023-11-28 11:26:30 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
registerPlugin( 'woocommerce-commands-registration', {
|
|
|
|
render: WooCommerceCommands,
|
|
|
|
} );
|