@wordpress/core-commands 0.26.0 → 1.0.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,22 +10,23 @@ import {
10
10
  post,
11
11
  page,
12
12
  layout,
13
+ symbol,
13
14
  symbolFilled,
14
15
  styles,
15
16
  navigation,
16
17
  } from '@wordpress/icons';
17
18
  import { privateApis as routerPrivateApis } from '@wordpress/router';
18
- import { getQueryArg, addQueryArgs, getPath } from '@wordpress/url';
19
+ import { addQueryArgs, getPath } from '@wordpress/url';
19
20
  import { useDebounce } from '@wordpress/compose';
20
21
 
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
 
28
- const { useHistory, useLocation } = unlock( routerPrivateApis );
29
+ const { useHistory } = unlock( routerPrivateApis );
29
30
 
30
31
  const icons = {
31
32
  post,
@@ -118,14 +119,6 @@ const getNavigationCommandLoaderPerPostType = ( postType ) =>
118
119
  const isSiteEditor = getPath( window.location.href )?.includes(
119
120
  'site-editor.php'
120
121
  );
121
- const extraArgs = isSiteEditor
122
- ? {
123
- canvas: getQueryArg(
124
- window.location.href,
125
- 'canvas'
126
- ),
127
- }
128
- : {};
129
122
 
130
123
  return {
131
124
  ...command,
@@ -133,7 +126,7 @@ const getNavigationCommandLoaderPerPostType = ( postType ) =>
133
126
  const args = {
134
127
  postType,
135
128
  postId: record.id,
136
- ...extraArgs,
129
+ canvas: 'edit',
137
130
  };
138
131
  const targetUrl = addQueryArgs(
139
132
  'site-editor.php',
@@ -159,14 +152,6 @@ const getNavigationCommandLoaderPerPostType = ( postType ) =>
159
152
  const getNavigationCommandLoaderPerTemplate = ( templateType ) =>
160
153
  function useNavigationCommandLoader( { search } ) {
161
154
  const history = useHistory();
162
- const location = useLocation();
163
-
164
- const isPatternsPage =
165
- location?.params?.path === '/patterns' ||
166
- location?.params?.postType === 'wp_block';
167
- const didAccessPatternsPage =
168
- !! location?.params?.didAccessPatternsPage;
169
-
170
155
  const isBlockBasedTheme = useIsBlockBasedTheme();
171
156
  const { records, isLoading } = useSelect( ( select ) => {
172
157
  const { getEntityRecords } = select( coreStore );
@@ -196,31 +181,52 @@ const getNavigationCommandLoaderPerTemplate = ( templateType ) =>
196
181
  ) {
197
182
  return [];
198
183
  }
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
- : {};
184
+ const isSiteEditor = getPath( window.location.href )?.includes(
185
+ 'site-editor.php'
186
+ );
187
+ const result = [];
188
+ result.push(
189
+ ...orderedRecords.map( ( record ) => {
190
+ return {
191
+ name: templateType + '-' + record.id,
192
+ searchLabel: record.title?.rendered + ' ' + record.id,
193
+ label: record.title?.rendered
194
+ ? record.title?.rendered
195
+ : __( '(no title)' ),
196
+ icon: icons[ templateType ],
197
+ callback: ( { close } ) => {
198
+ const args = {
199
+ postType: templateType,
200
+ postId: record.id,
201
+ canvas: 'edit',
202
+ };
203
+ const targetUrl = addQueryArgs(
204
+ 'site-editor.php',
205
+ args
206
+ );
207
+ if ( isSiteEditor ) {
208
+ history.push( args );
209
+ } else {
210
+ document.location = targetUrl;
211
+ }
212
+ close();
213
+ },
214
+ };
215
+ } )
216
+ );
206
217
 
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 ],
218
+ if (
219
+ orderedRecords?.length > 0 &&
220
+ templateType === 'wp_template_part'
221
+ ) {
222
+ result.push( {
223
+ name: 'core/edit-site/open-template-parts',
224
+ label: __( 'Template parts' ),
225
+ icon: symbolFilled,
214
226
  callback: ( { close } ) => {
215
227
  const args = {
216
- postType: templateType,
217
- postId: record.id,
218
- didAccessPatternsPage:
219
- ! isBlockBasedTheme &&
220
- ( isPatternsPage || didAccessPatternsPage )
221
- ? 1
222
- : undefined,
223
- ...extraArgs,
228
+ postType: 'wp_template_part',
229
+ categoryId: 'all-parts',
224
230
  };
225
231
  const targetUrl = addQueryArgs(
226
232
  'site-editor.php',
@@ -233,8 +239,9 @@ const getNavigationCommandLoaderPerTemplate = ( templateType ) =>
233
239
  }
234
240
  close();
235
241
  },
236
- };
237
- } );
242
+ } );
243
+ }
244
+ return result;
238
245
  }, [ isBlockBasedTheme, orderedRecords, history ] );
239
246
 
240
247
  return {
@@ -257,89 +264,112 @@ function useSiteEditorBasicNavigationCommands() {
257
264
  const isSiteEditor = getPath( window.location.href )?.includes(
258
265
  'site-editor.php'
259
266
  );
260
- const isTemplatesAccessible = useIsTemplatesAccessible();
267
+ const canCreateTemplate = useSelect( ( select ) => {
268
+ return select( coreStore ).canUser( 'create', 'templates' );
269
+ }, [] );
261
270
  const isBlockBasedTheme = useIsBlockBasedTheme();
262
271
  const commands = useMemo( () => {
263
272
  const result = [];
264
273
 
265
- if ( ! isTemplatesAccessible || ! isBlockBasedTheme ) {
266
- return result;
267
- }
274
+ if ( canCreateTemplate && isBlockBasedTheme ) {
275
+ result.push( {
276
+ name: 'core/edit-site/open-navigation',
277
+ label: __( 'Navigation' ),
278
+ icon: navigation,
279
+ callback: ( { close } ) => {
280
+ const args = {
281
+ postType: 'wp_navigation',
282
+ };
283
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
284
+ if ( isSiteEditor ) {
285
+ history.push( args );
286
+ } else {
287
+ document.location = targetUrl;
288
+ }
289
+ close();
290
+ },
291
+ } );
268
292
 
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
- } );
293
+ result.push( {
294
+ name: 'core/edit-site/open-styles',
295
+ label: __( 'Styles' ),
296
+ icon: styles,
297
+ callback: ( { close } ) => {
298
+ const args = {
299
+ path: '/wp_global_styles',
300
+ };
301
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
302
+ if ( isSiteEditor ) {
303
+ history.push( args );
304
+ } else {
305
+ document.location = targetUrl;
306
+ }
307
+ close();
308
+ },
309
+ } );
286
310
 
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
- } );
311
+ result.push( {
312
+ name: 'core/edit-site/open-pages',
313
+ label: __( 'Pages' ),
314
+ icon: page,
315
+ callback: ( { close } ) => {
316
+ const args = {
317
+ post_type: 'page',
318
+ };
319
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
320
+ if ( isSiteEditor ) {
321
+ history.push( args );
322
+ } else {
323
+ document.location = targetUrl;
324
+ }
325
+ close();
326
+ },
327
+ } );
304
328
 
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
- } );
329
+ result.push( {
330
+ name: 'core/edit-site/open-templates',
331
+ label: __( 'Templates' ),
332
+ icon: layout,
333
+ callback: ( { close } ) => {
334
+ const args = {
335
+ postType: 'wp_template',
336
+ };
337
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
338
+ if ( isSiteEditor ) {
339
+ history.push( args );
340
+ } else {
341
+ document.location = targetUrl;
342
+ }
343
+ close();
344
+ },
345
+ } );
346
+ }
322
347
 
323
348
  result.push( {
324
- name: 'core/edit-site/open-templates',
325
- label: __( 'Templates' ),
326
- icon: layout,
349
+ name: 'core/edit-site/open-patterns',
350
+ label: __( 'Patterns' ),
351
+ icon: symbol,
327
352
  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 );
353
+ if ( canCreateTemplate ) {
354
+ const args = {
355
+ postType: 'wp_block',
356
+ };
357
+ const targetUrl = addQueryArgs( 'site-editor.php', args );
358
+ if ( isSiteEditor ) {
359
+ history.push( args );
360
+ } else {
361
+ document.location = targetUrl;
362
+ }
363
+ close();
334
364
  } else {
335
- document.location = targetUrl;
365
+ // If a user cannot access the site editor
366
+ document.location.href = 'edit.php?post_type=wp_block';
336
367
  }
337
- close();
338
368
  },
339
369
  } );
340
370
 
341
371
  return result;
342
- }, [ history, isSiteEditor, isTemplatesAccessible, isBlockBasedTheme ] );
372
+ }, [ history, isSiteEditor, canCreateTemplate, isBlockBasedTheme ] );
343
373
 
344
374
  return {
345
375
  commands,