@wordpress/core-commands 0.26.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,7 @@ import {
10
10
  post,
11
11
  page,
12
12
  layout,
13
+ symbol,
13
14
  symbolFilled,
14
15
  styles,
15
16
  navigation,
@@ -21,7 +22,7 @@ import { useDebounce } from '@wordpress/compose';
21
22
  /**
22
23
  * Internal dependencies
23
24
  */
24
- import { useIsTemplatesAccessible, useIsBlockBasedTheme } from './hooks';
25
+ import { useIsBlockBasedTheme } from './hooks';
25
26
  import { unlock } from './lock-unlock';
26
27
  import { orderEntityRecordsBySearch } from './utils/order-entity-records-by-search';
27
28
 
@@ -196,31 +197,67 @@ const getNavigationCommandLoaderPerTemplate = ( templateType ) =>
196
197
  ) {
197
198
  return [];
198
199
  }
199
- return orderedRecords.map( ( record ) => {
200
- const isSiteEditor = getPath( window.location.href )?.includes(
201
- 'site-editor.php'
202
- );
203
- const extraArgs = isSiteEditor
204
- ? { canvas: getQueryArg( window.location.href, 'canvas' ) }
205
- : {};
200
+ const isSiteEditor = getPath( window.location.href )?.includes(
201
+ 'site-editor.php'
202
+ );
203
+ const result = [];
204
+ result.push(
205
+ ...orderedRecords.map( ( record ) => {
206
+ const extraArgs = isSiteEditor
207
+ ? {
208
+ canvas: getQueryArg(
209
+ window.location.href,
210
+ 'canvas'
211
+ ),
212
+ }
213
+ : {};
206
214
 
207
- return {
208
- name: templateType + '-' + record.id,
209
- searchLabel: record.title?.rendered + ' ' + record.id,
210
- label: record.title?.rendered
211
- ? record.title?.rendered
212
- : __( '(no title)' ),
213
- icon: icons[ templateType ],
215
+ return {
216
+ name: templateType + '-' + record.id,
217
+ searchLabel: record.title?.rendered + ' ' + record.id,
218
+ label: record.title?.rendered
219
+ ? record.title?.rendered
220
+ : __( '(no title)' ),
221
+ icon: icons[ templateType ],
222
+ callback: ( { close } ) => {
223
+ const args = {
224
+ postType: templateType,
225
+ postId: record.id,
226
+ didAccessPatternsPage:
227
+ ! isBlockBasedTheme &&
228
+ ( isPatternsPage || didAccessPatternsPage )
229
+ ? 1
230
+ : undefined,
231
+ ...extraArgs,
232
+ };
233
+ const targetUrl = addQueryArgs(
234
+ 'site-editor.php',
235
+ args
236
+ );
237
+ if ( isSiteEditor ) {
238
+ history.push( args );
239
+ } else {
240
+ document.location = targetUrl;
241
+ }
242
+ close();
243
+ },
244
+ };
245
+ } )
246
+ );
247
+
248
+ if (
249
+ orderedRecords?.length > 0 &&
250
+ templateType === 'wp_template_part'
251
+ ) {
252
+ result.push( {
253
+ name: 'core/edit-site/open-template-parts',
254
+ label: __( 'Template parts' ),
255
+ icon: symbolFilled,
214
256
  callback: ( { close } ) => {
215
257
  const args = {
216
- postType: templateType,
217
- postId: record.id,
218
- didAccessPatternsPage:
219
- ! isBlockBasedTheme &&
220
- ( isPatternsPage || didAccessPatternsPage )
221
- ? 1
222
- : undefined,
223
- ...extraArgs,
258
+ path: '/patterns',
259
+ categoryType: 'wp_template_part',
260
+ categoryId: 'all-parts',
224
261
  };
225
262
  const targetUrl = addQueryArgs(
226
263
  'site-editor.php',
@@ -233,8 +270,9 @@ const getNavigationCommandLoaderPerTemplate = ( templateType ) =>
233
270
  }
234
271
  close();
235
272
  },
236
- };
237
- } );
273
+ } );
274
+ }
275
+ return result;
238
276
  }, [ isBlockBasedTheme, orderedRecords, history ] );
239
277
 
240
278
  return {
@@ -257,89 +295,112 @@ function useSiteEditorBasicNavigationCommands() {
257
295
  const isSiteEditor = getPath( window.location.href )?.includes(
258
296
  'site-editor.php'
259
297
  );
260
- const isTemplatesAccessible = useIsTemplatesAccessible();
298
+ const canCreateTemplate = useSelect( ( select ) => {
299
+ return select( coreStore ).canUser( 'create', 'templates' );
300
+ }, [] );
261
301
  const isBlockBasedTheme = useIsBlockBasedTheme();
262
302
  const commands = useMemo( () => {
263
303
  const result = [];
264
304
 
265
- if ( ! isTemplatesAccessible || ! isBlockBasedTheme ) {
266
- return result;
267
- }
305
+ if ( canCreateTemplate && isBlockBasedTheme ) {
306
+ result.push( {
307
+ name: 'core/edit-site/open-navigation',
308
+ label: __( 'Navigation' ),
309
+ icon: navigation,
310
+ callback: ( { close } ) => {
311
+ const args = {
312
+ path: '/navigation',
313
+ };
314
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
315
+ if ( isSiteEditor ) {
316
+ history.push( args );
317
+ } else {
318
+ document.location = targetUrl;
319
+ }
320
+ close();
321
+ },
322
+ } );
268
323
 
269
- result.push( {
270
- name: 'core/edit-site/open-navigation',
271
- label: __( 'Navigation' ),
272
- icon: navigation,
273
- callback: ( { close } ) => {
274
- const args = {
275
- path: '/navigation',
276
- };
277
- const targetUrl = addQueryArgs( 'site-editor.php', args );
278
- if ( isSiteEditor ) {
279
- history.push( args );
280
- } else {
281
- document.location = targetUrl;
282
- }
283
- close();
284
- },
285
- } );
324
+ result.push( {
325
+ name: 'core/edit-site/open-styles',
326
+ label: __( 'Styles' ),
327
+ icon: styles,
328
+ callback: ( { close } ) => {
329
+ const args = {
330
+ path: '/wp_global_styles',
331
+ };
332
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
333
+ if ( isSiteEditor ) {
334
+ history.push( args );
335
+ } else {
336
+ document.location = targetUrl;
337
+ }
338
+ close();
339
+ },
340
+ } );
286
341
 
287
- result.push( {
288
- name: 'core/edit-site/open-styles',
289
- label: __( 'Styles' ),
290
- icon: styles,
291
- callback: ( { close } ) => {
292
- const args = {
293
- path: '/wp_global_styles',
294
- };
295
- const targetUrl = addQueryArgs( 'site-editor.php', args );
296
- if ( isSiteEditor ) {
297
- history.push( args );
298
- } else {
299
- document.location = targetUrl;
300
- }
301
- close();
302
- },
303
- } );
342
+ result.push( {
343
+ name: 'core/edit-site/open-pages',
344
+ label: __( 'Pages' ),
345
+ icon: page,
346
+ callback: ( { close } ) => {
347
+ const args = {
348
+ path: '/page',
349
+ };
350
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
351
+ if ( isSiteEditor ) {
352
+ history.push( args );
353
+ } else {
354
+ document.location = targetUrl;
355
+ }
356
+ close();
357
+ },
358
+ } );
304
359
 
305
- result.push( {
306
- name: 'core/edit-site/open-pages',
307
- label: __( 'Pages' ),
308
- icon: page,
309
- callback: ( { close } ) => {
310
- const args = {
311
- path: '/page',
312
- };
313
- const targetUrl = addQueryArgs( 'site-editor.php', args );
314
- if ( isSiteEditor ) {
315
- history.push( args );
316
- } else {
317
- document.location = targetUrl;
318
- }
319
- close();
320
- },
321
- } );
360
+ result.push( {
361
+ name: 'core/edit-site/open-templates',
362
+ label: __( 'Templates' ),
363
+ icon: layout,
364
+ callback: ( { close } ) => {
365
+ const args = {
366
+ path: '/wp_template',
367
+ };
368
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
369
+ if ( isSiteEditor ) {
370
+ history.push( args );
371
+ } else {
372
+ document.location = targetUrl;
373
+ }
374
+ close();
375
+ },
376
+ } );
377
+ }
322
378
 
323
379
  result.push( {
324
- name: 'core/edit-site/open-templates',
325
- label: __( 'Templates' ),
326
- icon: layout,
380
+ name: 'core/edit-site/open-patterns',
381
+ label: __( 'Patterns' ),
382
+ icon: symbol,
327
383
  callback: ( { close } ) => {
328
- const args = {
329
- path: '/wp_template',
330
- };
331
- const targetUrl = addQueryArgs( 'site-editor.php', args );
332
- if ( isSiteEditor ) {
333
- history.push( args );
384
+ if ( canCreateTemplate ) {
385
+ const args = {
386
+ path: '/patterns',
387
+ };
388
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
389
+ if ( isSiteEditor ) {
390
+ history.push( args );
391
+ } else {
392
+ document.location = targetUrl;
393
+ }
394
+ close();
334
395
  } else {
335
- document.location = targetUrl;
396
+ // If a user cannot access the site editor
397
+ document.location.href = 'edit.php?post_type=wp_block';
336
398
  }
337
- close();
338
399
  },
339
400
  } );
340
401
 
341
402
  return result;
342
- }, [ history, isSiteEditor, isTemplatesAccessible, isBlockBasedTheme ] );
403
+ }, [ history, isSiteEditor, canCreateTemplate, isBlockBasedTheme ] );
343
404
 
344
405
  return {
345
406
  commands,