@wordpress/edit-site 6.35.2-next.dc3f6d3c1.0 → 6.36.1-next.8b30e05b0.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.
Files changed (64) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/build/components/add-new-template-legacy/add-custom-generic-template-modal-content.js +109 -0
  3. package/build/components/add-new-template-legacy/add-custom-generic-template-modal-content.js.map +7 -0
  4. package/build/components/add-new-template-legacy/add-custom-template-modal-content.js +328 -0
  5. package/build/components/add-new-template-legacy/add-custom-template-modal-content.js.map +7 -0
  6. package/build/components/add-new-template-legacy/index.js +389 -0
  7. package/build/components/add-new-template-legacy/index.js.map +7 -0
  8. package/build/components/add-new-template-legacy/utils.js +620 -0
  9. package/build/components/add-new-template-legacy/utils.js.map +7 -0
  10. package/build/components/page-templates/fields.js +3 -1
  11. package/build/components/page-templates/fields.js.map +2 -2
  12. package/build/components/page-templates/index-legacy.js +185 -0
  13. package/build/components/page-templates/index-legacy.js.map +7 -0
  14. package/build/components/post-list/index.js +10 -0
  15. package/build/components/post-list/index.js.map +2 -2
  16. package/build/components/sidebar-navigation-screen-templates-browse/content-legacy.js +101 -0
  17. package/build/components/sidebar-navigation-screen-templates-browse/content-legacy.js.map +7 -0
  18. package/build/components/sidebar-navigation-screen-templates-browse/index.js +2 -1
  19. package/build/components/sidebar-navigation-screen-templates-browse/index.js.map +3 -3
  20. package/build/components/site-editor-routes/templates.js +10 -2
  21. package/build/components/site-editor-routes/templates.js.map +3 -3
  22. package/build/components/site-hub/index.js +1 -1
  23. package/build/components/site-hub/index.js.map +2 -2
  24. package/build/utils/constants.js +6 -0
  25. package/build/utils/constants.js.map +2 -2
  26. package/build-module/components/add-new-template-legacy/add-custom-generic-template-modal-content.js +93 -0
  27. package/build-module/components/add-new-template-legacy/add-custom-generic-template-modal-content.js.map +7 -0
  28. package/build-module/components/add-new-template-legacy/add-custom-template-modal-content.js +316 -0
  29. package/build-module/components/add-new-template-legacy/add-custom-template-modal-content.js.map +7 -0
  30. package/build-module/components/add-new-template-legacy/index.js +390 -0
  31. package/build-module/components/add-new-template-legacy/index.js.map +7 -0
  32. package/build-module/components/add-new-template-legacy/utils.js +589 -0
  33. package/build-module/components/add-new-template-legacy/utils.js.map +7 -0
  34. package/build-module/components/page-templates/fields.js +3 -1
  35. package/build-module/components/page-templates/fields.js.map +2 -2
  36. package/build-module/components/page-templates/index-legacy.js +154 -0
  37. package/build-module/components/page-templates/index-legacy.js.map +7 -0
  38. package/build-module/components/post-list/index.js +12 -0
  39. package/build-module/components/post-list/index.js.map +2 -2
  40. package/build-module/components/sidebar-navigation-screen-templates-browse/content-legacy.js +70 -0
  41. package/build-module/components/sidebar-navigation-screen-templates-browse/content-legacy.js.map +7 -0
  42. package/build-module/components/sidebar-navigation-screen-templates-browse/index.js +2 -1
  43. package/build-module/components/sidebar-navigation-screen-templates-browse/index.js.map +2 -2
  44. package/build-module/components/site-editor-routes/templates.js +10 -2
  45. package/build-module/components/site-editor-routes/templates.js.map +2 -2
  46. package/build-module/components/site-hub/index.js +1 -1
  47. package/build-module/components/site-hub/index.js.map +2 -2
  48. package/build-module/utils/constants.js +4 -0
  49. package/build-module/utils/constants.js.map +2 -2
  50. package/build-style/style-rtl.css +263 -27
  51. package/build-style/style.css +263 -27
  52. package/package.json +46 -46
  53. package/src/components/add-new-template-legacy/add-custom-generic-template-modal-content.js +94 -0
  54. package/src/components/add-new-template-legacy/add-custom-template-modal-content.js +314 -0
  55. package/src/components/add-new-template-legacy/index.js +454 -0
  56. package/src/components/add-new-template-legacy/utils.js +760 -0
  57. package/src/components/page-templates/fields.js +13 -9
  58. package/src/components/page-templates/index-legacy.js +166 -0
  59. package/src/components/post-list/index.js +13 -0
  60. package/src/components/sidebar-navigation-screen-templates-browse/content-legacy.js +79 -0
  61. package/src/components/sidebar-navigation-screen-templates-browse/index.js +8 -1
  62. package/src/components/site-editor-routes/templates.js +22 -3
  63. package/src/components/site-hub/index.js +1 -1
  64. package/src/utils/constants.js +2 -0
@@ -0,0 +1,760 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useSelect } from '@wordpress/data';
5
+ import { store as coreStore } from '@wordpress/core-data';
6
+ import { decodeEntities } from '@wordpress/html-entities';
7
+ import { useMemo, useCallback } from '@wordpress/element';
8
+ import { __, _x, sprintf } from '@wordpress/i18n';
9
+ import { blockMeta, post, archive } from '@wordpress/icons';
10
+ import { safeDecodeURI } from '@wordpress/url';
11
+
12
+ /**
13
+ * Internal dependencies
14
+ */
15
+ import { TEMPLATE_POST_TYPE } from '../../utils/constants';
16
+
17
+ const EMPTY_OBJECT = {};
18
+
19
+ /**
20
+ * @typedef IHasNameAndId
21
+ * @property {string|number} id The entity's id.
22
+ * @property {string} name The entity's name.
23
+ */
24
+
25
+ const getValueFromObjectPath = ( object, path ) => {
26
+ let value = object;
27
+ path.split( '.' ).forEach( ( fieldName ) => {
28
+ value = value?.[ fieldName ];
29
+ } );
30
+ return value;
31
+ };
32
+
33
+ /**
34
+ * Helper that adds a prefix to a post slug. The slug needs to be URL-decoded first,
35
+ * so that we have raw Unicode characters there. The server will truncate the slug to
36
+ * 200 characters, respecing Unicode char boundary. On the other hand, the server
37
+ * doesn't detect urlencoded octet boundary and can possibly construct slugs that
38
+ * are not valid urlencoded strings.
39
+ * @param {string} prefix The prefix to add to the slug.
40
+ * @param {string} slug The slug to add the prefix to.
41
+ * @return {string} The slug with the prefix.
42
+ */
43
+ function prefixSlug( prefix, slug ) {
44
+ return `${ prefix }-${ safeDecodeURI( slug ) }`;
45
+ }
46
+
47
+ /**
48
+ * Helper util to map records to add a `name` prop from a
49
+ * provided path, in order to handle all entities in the same
50
+ * fashion(implementing`IHasNameAndId` interface).
51
+ *
52
+ * @param {Object[]} entities The array of entities.
53
+ * @param {string} path The path to map a `name` property from the entity.
54
+ * @return {IHasNameAndId[]} An array of entities that now implement the `IHasNameAndId` interface.
55
+ */
56
+ export const mapToIHasNameAndId = ( entities, path ) => {
57
+ return ( entities || [] ).map( ( entity ) => ( {
58
+ ...entity,
59
+ name: decodeEntities( getValueFromObjectPath( entity, path ) ),
60
+ } ) );
61
+ };
62
+
63
+ /**
64
+ * @typedef {Object} EntitiesInfo
65
+ * @property {boolean} hasEntities If an entity has available records(posts, terms, etc..).
66
+ * @property {number[]} existingEntitiesIds An array of the existing entities ids.
67
+ */
68
+
69
+ export const useExistingTemplates = () => {
70
+ return useSelect(
71
+ ( select ) =>
72
+ select( coreStore ).getEntityRecords(
73
+ 'postType',
74
+ TEMPLATE_POST_TYPE,
75
+ {
76
+ per_page: -1,
77
+ }
78
+ ),
79
+ []
80
+ );
81
+ };
82
+
83
+ export const useDefaultTemplateTypes = () => {
84
+ return useSelect(
85
+ ( select ) =>
86
+ select( coreStore ).getCurrentTheme()?.default_template_types || [],
87
+ []
88
+ );
89
+ };
90
+
91
+ const usePublicPostTypes = () => {
92
+ const postTypes = useSelect(
93
+ ( select ) => select( coreStore ).getPostTypes( { per_page: -1 } ),
94
+ []
95
+ );
96
+ return useMemo( () => {
97
+ const excludedPostTypes = [ 'attachment' ];
98
+ return postTypes
99
+ ?.filter(
100
+ ( { viewable, slug } ) =>
101
+ viewable && ! excludedPostTypes.includes( slug )
102
+ )
103
+ .sort( ( a, b ) => {
104
+ // Sort post types alphabetically by name,
105
+ // but exclude the built-in 'post' type from sorting.
106
+ if ( a.slug === 'post' || b.slug === 'post' ) {
107
+ return 0;
108
+ }
109
+
110
+ return a.name.localeCompare( b.name );
111
+ } );
112
+ }, [ postTypes ] );
113
+ };
114
+
115
+ const usePublicTaxonomies = () => {
116
+ const taxonomies = useSelect(
117
+ ( select ) => select( coreStore ).getTaxonomies( { per_page: -1 } ),
118
+ []
119
+ );
120
+ return useMemo( () => {
121
+ return taxonomies?.filter(
122
+ ( { visibility } ) => visibility?.publicly_queryable
123
+ );
124
+ }, [ taxonomies ] );
125
+ };
126
+
127
+ export function usePostTypeArchiveMenuItems() {
128
+ const publicPostTypes = usePublicPostTypes();
129
+ const postTypesWithArchives = useMemo(
130
+ () => publicPostTypes?.filter( ( postType ) => postType.has_archive ),
131
+ [ publicPostTypes ]
132
+ );
133
+ const existingTemplates = useExistingTemplates();
134
+ // We need to keep track of naming conflicts. If a conflict
135
+ // occurs, we need to add slug.
136
+ const postTypeLabels = useMemo(
137
+ () =>
138
+ publicPostTypes?.reduce( ( accumulator, { labels } ) => {
139
+ const singularName = labels.singular_name.toLowerCase();
140
+ accumulator[ singularName ] =
141
+ ( accumulator[ singularName ] || 0 ) + 1;
142
+ return accumulator;
143
+ }, {} ),
144
+ [ publicPostTypes ]
145
+ );
146
+ const needsUniqueIdentifier = useCallback(
147
+ ( { labels, slug } ) => {
148
+ const singularName = labels.singular_name.toLowerCase();
149
+ return postTypeLabels[ singularName ] > 1 && singularName !== slug;
150
+ },
151
+ [ postTypeLabels ]
152
+ );
153
+ return useMemo(
154
+ () =>
155
+ postTypesWithArchives
156
+ ?.filter(
157
+ ( postType ) =>
158
+ ! ( existingTemplates || [] ).some(
159
+ ( existingTemplate ) =>
160
+ existingTemplate.slug ===
161
+ 'archive-' + postType.slug
162
+ )
163
+ )
164
+ .map( ( postType ) => {
165
+ let title;
166
+ if ( needsUniqueIdentifier( postType ) ) {
167
+ title = sprintf(
168
+ // translators: %1s: Name of the post type e.g: "Post"; %2s: Slug of the post type e.g: "book".
169
+ __( 'Archive: %1$s (%2$s)' ),
170
+ postType.labels.singular_name,
171
+ postType.slug
172
+ );
173
+ } else {
174
+ title = sprintf(
175
+ // translators: %s: Name of the post type e.g: "Post".
176
+ __( 'Archive: %s' ),
177
+ postType.labels.singular_name
178
+ );
179
+ }
180
+ return {
181
+ slug: 'archive-' + postType.slug,
182
+ description: sprintf(
183
+ // translators: %s: Name of the post type e.g: "Post".
184
+ __(
185
+ 'Displays an archive with the latest posts of type: %s.'
186
+ ),
187
+ postType.labels.singular_name
188
+ ),
189
+ title,
190
+ // `icon` is the `menu_icon` property of a post type. We
191
+ // only handle `dashicons` for now, even if the `menu_icon`
192
+ // also supports urls and svg as values.
193
+ icon:
194
+ typeof postType.icon === 'string' &&
195
+ postType.icon.startsWith( 'dashicons-' )
196
+ ? postType.icon.slice( 10 )
197
+ : archive,
198
+ templatePrefix: 'archive',
199
+ };
200
+ } ) || [],
201
+ [ postTypesWithArchives, existingTemplates, needsUniqueIdentifier ]
202
+ );
203
+ }
204
+
205
+ export const usePostTypeMenuItems = ( onClickMenuItem ) => {
206
+ const publicPostTypes = usePublicPostTypes();
207
+ const existingTemplates = useExistingTemplates();
208
+ const defaultTemplateTypes = useDefaultTemplateTypes();
209
+ // We need to keep track of naming conflicts. If a conflict
210
+ // occurs, we need to add slug.
211
+ const templateLabels = useMemo(
212
+ () =>
213
+ publicPostTypes?.reduce( ( accumulator, { labels } ) => {
214
+ const templateName = (
215
+ labels.template_name || labels.singular_name
216
+ ).toLowerCase();
217
+ accumulator[ templateName ] =
218
+ ( accumulator[ templateName ] || 0 ) + 1;
219
+ return accumulator;
220
+ }, {} ),
221
+ [ publicPostTypes ]
222
+ );
223
+ const needsUniqueIdentifier = useCallback(
224
+ ( { labels, slug } ) => {
225
+ const templateName = (
226
+ labels.template_name || labels.singular_name
227
+ ).toLowerCase();
228
+ return templateLabels[ templateName ] > 1 && templateName !== slug;
229
+ },
230
+ [ templateLabels ]
231
+ );
232
+
233
+ // `page`is a special case in template hierarchy.
234
+ const templatePrefixes = useMemo(
235
+ () =>
236
+ publicPostTypes?.reduce( ( accumulator, { slug } ) => {
237
+ let suffix = slug;
238
+ if ( slug !== 'page' ) {
239
+ suffix = `single-${ suffix }`;
240
+ }
241
+ accumulator[ slug ] = suffix;
242
+ return accumulator;
243
+ }, {} ),
244
+ [ publicPostTypes ]
245
+ );
246
+ const postTypesInfo = useEntitiesInfo( 'postType', templatePrefixes );
247
+ const existingTemplateSlugs = ( existingTemplates || [] ).map(
248
+ ( { slug } ) => slug
249
+ );
250
+ const menuItems = ( publicPostTypes || [] ).reduce(
251
+ ( accumulator, postType ) => {
252
+ const { slug, labels, icon } = postType;
253
+ // We need to check if the general template is part of the
254
+ // defaultTemplateTypes. If it is, just use that info and
255
+ // augment it with the specific template functionality.
256
+ const generalTemplateSlug = templatePrefixes[ slug ];
257
+ const defaultTemplateType = defaultTemplateTypes?.find(
258
+ ( { slug: _slug } ) => _slug === generalTemplateSlug
259
+ );
260
+ const hasGeneralTemplate =
261
+ existingTemplateSlugs?.includes( generalTemplateSlug );
262
+ const _needsUniqueIdentifier = needsUniqueIdentifier( postType );
263
+ let menuItemTitle =
264
+ labels.template_name ||
265
+ sprintf(
266
+ // translators: %s: Name of the post type e.g: "Post".
267
+ __( 'Single item: %s' ),
268
+ labels.singular_name
269
+ );
270
+ if ( _needsUniqueIdentifier ) {
271
+ menuItemTitle = labels.template_name
272
+ ? sprintf(
273
+ // translators: 1: Name of the template e.g: "Single Item: Post". 2: Slug of the post type e.g: "book".
274
+ _x( '%1$s (%2$s)', 'post type menu label' ),
275
+ labels.template_name,
276
+ slug
277
+ )
278
+ : sprintf(
279
+ // translators: 1: Name of the post type e.g: "Post". 2: Slug of the post type e.g: "book".
280
+ _x(
281
+ 'Single item: %1$s (%2$s)',
282
+ 'post type menu label'
283
+ ),
284
+ labels.singular_name,
285
+ slug
286
+ );
287
+ }
288
+ const menuItem = defaultTemplateType
289
+ ? {
290
+ ...defaultTemplateType,
291
+ templatePrefix: templatePrefixes[ slug ],
292
+ }
293
+ : {
294
+ slug: generalTemplateSlug,
295
+ title: menuItemTitle,
296
+ description: sprintf(
297
+ // translators: %s: Name of the post type e.g: "Post".
298
+ __( 'Displays a single item: %s.' ),
299
+ labels.singular_name
300
+ ),
301
+ // `icon` is the `menu_icon` property of a post type. We
302
+ // only handle `dashicons` for now, even if the `menu_icon`
303
+ // also supports urls and svg as values.
304
+ icon:
305
+ typeof icon === 'string' &&
306
+ icon.startsWith( 'dashicons-' )
307
+ ? icon.slice( 10 )
308
+ : post,
309
+ templatePrefix: templatePrefixes[ slug ],
310
+ };
311
+ const hasEntities = postTypesInfo?.[ slug ]?.hasEntities;
312
+ // We have a different template creation flow only if they have entities.
313
+ if ( hasEntities ) {
314
+ menuItem.onClick = ( template ) => {
315
+ onClickMenuItem( {
316
+ type: 'postType',
317
+ slug,
318
+ config: {
319
+ recordNamePath: 'title.rendered',
320
+ queryArgs: ( { search } ) => {
321
+ return {
322
+ _fields: 'id,title,slug,link',
323
+ orderBy: search ? 'relevance' : 'modified',
324
+ exclude:
325
+ postTypesInfo[ slug ]
326
+ .existingEntitiesIds,
327
+ };
328
+ },
329
+ getSpecificTemplate: ( suggestion ) => {
330
+ const templateSlug = prefixSlug(
331
+ templatePrefixes[ slug ],
332
+ suggestion.slug
333
+ );
334
+ return {
335
+ title: templateSlug,
336
+ slug: templateSlug,
337
+ templatePrefix: templatePrefixes[ slug ],
338
+ };
339
+ },
340
+ },
341
+ labels,
342
+ hasGeneralTemplate,
343
+ template,
344
+ } );
345
+ };
346
+ }
347
+ // We don't need to add the menu item if there are no
348
+ // entities and the general template exists.
349
+ if ( ! hasGeneralTemplate || hasEntities ) {
350
+ accumulator.push( menuItem );
351
+ }
352
+ return accumulator;
353
+ },
354
+ []
355
+ );
356
+ // Split menu items into two groups: one for the default post types
357
+ // and one for the rest.
358
+ const postTypesMenuItems = useMemo(
359
+ () =>
360
+ menuItems.reduce(
361
+ ( accumulator, postType ) => {
362
+ const { slug } = postType;
363
+ let key = 'postTypesMenuItems';
364
+ if ( slug === 'page' ) {
365
+ key = 'defaultPostTypesMenuItems';
366
+ }
367
+ accumulator[ key ].push( postType );
368
+ return accumulator;
369
+ },
370
+ { defaultPostTypesMenuItems: [], postTypesMenuItems: [] }
371
+ ),
372
+ [ menuItems ]
373
+ );
374
+ return postTypesMenuItems;
375
+ };
376
+
377
+ export const useTaxonomiesMenuItems = ( onClickMenuItem ) => {
378
+ const publicTaxonomies = usePublicTaxonomies();
379
+ const existingTemplates = useExistingTemplates();
380
+ const defaultTemplateTypes = useDefaultTemplateTypes();
381
+ // `category` and `post_tag` are special cases in template hierarchy.
382
+ const templatePrefixes = useMemo(
383
+ () =>
384
+ publicTaxonomies?.reduce( ( accumulator, { slug } ) => {
385
+ let suffix = slug;
386
+ if ( ! [ 'category', 'post_tag' ].includes( slug ) ) {
387
+ suffix = `taxonomy-${ suffix }`;
388
+ }
389
+ if ( slug === 'post_tag' ) {
390
+ suffix = `tag`;
391
+ }
392
+ accumulator[ slug ] = suffix;
393
+ return accumulator;
394
+ }, {} ),
395
+ [ publicTaxonomies ]
396
+ );
397
+ // We need to keep track of naming conflicts. If a conflict
398
+ // occurs, we need to add slug.
399
+ const taxonomyLabels = publicTaxonomies?.reduce(
400
+ ( accumulator, { labels } ) => {
401
+ const templateName = (
402
+ labels.template_name || labels.singular_name
403
+ ).toLowerCase();
404
+ accumulator[ templateName ] =
405
+ ( accumulator[ templateName ] || 0 ) + 1;
406
+ return accumulator;
407
+ },
408
+ {}
409
+ );
410
+ const needsUniqueIdentifier = ( labels, slug ) => {
411
+ if ( [ 'category', 'post_tag' ].includes( slug ) ) {
412
+ return false;
413
+ }
414
+ const templateName = (
415
+ labels.template_name || labels.singular_name
416
+ ).toLowerCase();
417
+ return taxonomyLabels[ templateName ] > 1 && templateName !== slug;
418
+ };
419
+ const taxonomiesInfo = useEntitiesInfo( 'taxonomy', templatePrefixes );
420
+ const existingTemplateSlugs = ( existingTemplates || [] ).map(
421
+ ( { slug } ) => slug
422
+ );
423
+ const menuItems = ( publicTaxonomies || [] ).reduce(
424
+ ( accumulator, taxonomy ) => {
425
+ const { slug, labels } = taxonomy;
426
+ // We need to check if the general template is part of the
427
+ // defaultTemplateTypes. If it is, just use that info and
428
+ // augment it with the specific template functionality.
429
+ const generalTemplateSlug = templatePrefixes[ slug ];
430
+ const defaultTemplateType = defaultTemplateTypes?.find(
431
+ ( { slug: _slug } ) => _slug === generalTemplateSlug
432
+ );
433
+ const hasGeneralTemplate =
434
+ existingTemplateSlugs?.includes( generalTemplateSlug );
435
+ const _needsUniqueIdentifier = needsUniqueIdentifier(
436
+ labels,
437
+ slug
438
+ );
439
+ let menuItemTitle = labels.template_name || labels.singular_name;
440
+ if ( _needsUniqueIdentifier ) {
441
+ menuItemTitle = labels.template_name
442
+ ? sprintf(
443
+ // translators: 1: Name of the template e.g: "Products by Category". 2: Slug of the taxonomy e.g: "product_cat".
444
+ _x( '%1$s (%2$s)', 'taxonomy template menu label' ),
445
+ labels.template_name,
446
+ slug
447
+ )
448
+ : sprintf(
449
+ // translators: 1: Name of the taxonomy e.g: "Category". 2: Slug of the taxonomy e.g: "product_cat".
450
+ _x( '%1$s (%2$s)', 'taxonomy menu label' ),
451
+ labels.singular_name,
452
+ slug
453
+ );
454
+ }
455
+ const menuItem = defaultTemplateType
456
+ ? {
457
+ ...defaultTemplateType,
458
+ templatePrefix: templatePrefixes[ slug ],
459
+ }
460
+ : {
461
+ slug: generalTemplateSlug,
462
+ title: menuItemTitle,
463
+ description: sprintf(
464
+ // translators: %s: Name of the taxonomy e.g: "Product Categories".
465
+ __( 'Displays taxonomy: %s.' ),
466
+ labels.singular_name
467
+ ),
468
+ icon: blockMeta,
469
+ templatePrefix: templatePrefixes[ slug ],
470
+ };
471
+ const hasEntities = taxonomiesInfo?.[ slug ]?.hasEntities;
472
+ // We have a different template creation flow only if they have entities.
473
+ if ( hasEntities ) {
474
+ menuItem.onClick = ( template ) => {
475
+ onClickMenuItem( {
476
+ type: 'taxonomy',
477
+ slug,
478
+ config: {
479
+ queryArgs: ( { search } ) => {
480
+ return {
481
+ _fields: 'id,name,slug,link',
482
+ orderBy: search ? 'name' : 'count',
483
+ exclude:
484
+ taxonomiesInfo[ slug ]
485
+ .existingEntitiesIds,
486
+ };
487
+ },
488
+ getSpecificTemplate: ( suggestion ) => {
489
+ const templateSlug = prefixSlug(
490
+ templatePrefixes[ slug ],
491
+ suggestion.slug
492
+ );
493
+ return {
494
+ title: templateSlug,
495
+ slug: templateSlug,
496
+ templatePrefix: templatePrefixes[ slug ],
497
+ };
498
+ },
499
+ },
500
+ labels,
501
+ hasGeneralTemplate,
502
+ template,
503
+ } );
504
+ };
505
+ }
506
+ // We don't need to add the menu item if there are no
507
+ // entities and the general template exists.
508
+ if ( ! hasGeneralTemplate || hasEntities ) {
509
+ accumulator.push( menuItem );
510
+ }
511
+ return accumulator;
512
+ },
513
+ []
514
+ );
515
+ // Split menu items into two groups: one for the default taxonomies
516
+ // and one for the rest.
517
+ const taxonomiesMenuItems = useMemo(
518
+ () =>
519
+ menuItems.reduce(
520
+ ( accumulator, taxonomy ) => {
521
+ const { slug } = taxonomy;
522
+ let key = 'taxonomiesMenuItems';
523
+ if ( [ 'category', 'tag' ].includes( slug ) ) {
524
+ key = 'defaultTaxonomiesMenuItems';
525
+ }
526
+ accumulator[ key ].push( taxonomy );
527
+ return accumulator;
528
+ },
529
+ { defaultTaxonomiesMenuItems: [], taxonomiesMenuItems: [] }
530
+ ),
531
+ [ menuItems ]
532
+ );
533
+ return taxonomiesMenuItems;
534
+ };
535
+
536
+ const USE_AUTHOR_MENU_ITEM_TEMPLATE_PREFIX = { user: 'author' };
537
+ const USE_AUTHOR_MENU_ITEM_QUERY_PARAMETERS = { user: { who: 'authors' } };
538
+ export function useAuthorMenuItem( onClickMenuItem ) {
539
+ const existingTemplates = useExistingTemplates();
540
+ const defaultTemplateTypes = useDefaultTemplateTypes();
541
+ const authorInfo = useEntitiesInfo(
542
+ 'root',
543
+ USE_AUTHOR_MENU_ITEM_TEMPLATE_PREFIX,
544
+ USE_AUTHOR_MENU_ITEM_QUERY_PARAMETERS
545
+ );
546
+ let authorMenuItem = defaultTemplateTypes?.find(
547
+ ( { slug } ) => slug === 'author'
548
+ );
549
+ if ( ! authorMenuItem ) {
550
+ authorMenuItem = {
551
+ description: __(
552
+ 'Displays latest posts written by a single author.'
553
+ ),
554
+ slug: 'author',
555
+ title: 'Author',
556
+ };
557
+ }
558
+ const hasGeneralTemplate = !! existingTemplates?.find(
559
+ ( { slug } ) => slug === 'author'
560
+ );
561
+ if ( authorInfo.user?.hasEntities ) {
562
+ authorMenuItem = { ...authorMenuItem, templatePrefix: 'author' };
563
+ authorMenuItem.onClick = ( template ) => {
564
+ onClickMenuItem( {
565
+ type: 'root',
566
+ slug: 'user',
567
+ config: {
568
+ queryArgs: ( { search } ) => {
569
+ return {
570
+ _fields: 'id,name,slug,link',
571
+ orderBy: search ? 'name' : 'registered_date',
572
+ exclude: authorInfo.user.existingEntitiesIds,
573
+ who: 'authors',
574
+ };
575
+ },
576
+ getSpecificTemplate: ( suggestion ) => {
577
+ const templateSlug = prefixSlug(
578
+ 'author',
579
+ suggestion.slug
580
+ );
581
+ return {
582
+ title: templateSlug,
583
+ slug: templateSlug,
584
+ templatePrefix: 'author',
585
+ };
586
+ },
587
+ },
588
+ labels: {
589
+ singular_name: __( 'Author' ),
590
+ search_items: __( 'Search Authors' ),
591
+ not_found: __( 'No authors found.' ),
592
+ all_items: __( 'All Authors' ),
593
+ },
594
+ hasGeneralTemplate,
595
+ template,
596
+ } );
597
+ };
598
+ }
599
+ if ( ! hasGeneralTemplate || authorInfo.user?.hasEntities ) {
600
+ return authorMenuItem;
601
+ }
602
+ }
603
+
604
+ /**
605
+ * Helper hook that filters all the existing templates by the given
606
+ * object with the entity's slug as key and the template prefix as value.
607
+ *
608
+ * Example:
609
+ * `existingTemplates` is: [ { slug: 'tag-apple' }, { slug: 'page-about' }, { slug: 'tag' } ]
610
+ * `templatePrefixes` is: { post_tag: 'tag' }
611
+ * It will return: { post_tag: ['apple'] }
612
+ *
613
+ * Note: We append the `-` to the given template prefix in this function for our checks.
614
+ *
615
+ * @param {Record<string,string>} templatePrefixes An object with the entity's slug as key and the template prefix as value.
616
+ * @return {Record<string,string[]>} An object with the entity's slug as key and an array with the existing template slugs as value.
617
+ */
618
+ const useExistingTemplateSlugs = ( templatePrefixes ) => {
619
+ const existingTemplates = useExistingTemplates();
620
+ const existingSlugs = useMemo( () => {
621
+ return Object.entries( templatePrefixes || {} ).reduce(
622
+ ( accumulator, [ slug, prefix ] ) => {
623
+ const slugsWithTemplates = ( existingTemplates || [] ).reduce(
624
+ ( _accumulator, existingTemplate ) => {
625
+ const _prefix = `${ prefix }-`;
626
+ if ( existingTemplate.slug.startsWith( _prefix ) ) {
627
+ _accumulator.push(
628
+ existingTemplate.slug.substring(
629
+ _prefix.length
630
+ )
631
+ );
632
+ }
633
+ return _accumulator;
634
+ },
635
+ []
636
+ );
637
+ if ( slugsWithTemplates.length ) {
638
+ accumulator[ slug ] = slugsWithTemplates;
639
+ }
640
+ return accumulator;
641
+ },
642
+ {}
643
+ );
644
+ }, [ templatePrefixes, existingTemplates ] );
645
+ return existingSlugs;
646
+ };
647
+
648
+ /**
649
+ * Helper hook that finds the existing records with an associated template,
650
+ * as they need to be excluded from the template suggestions.
651
+ *
652
+ * @param {string} entityName The entity's name.
653
+ * @param {Record<string,string>} templatePrefixes An object with the entity's slug as key and the template prefix as value.
654
+ * @param {Record<string,Object>} additionalQueryParameters An object with the entity's slug as key and additional query parameters as value.
655
+ * @return {Record<string,EntitiesInfo>} An object with the entity's slug as key and the existing records as value.
656
+ */
657
+ const useTemplatesToExclude = (
658
+ entityName,
659
+ templatePrefixes,
660
+ additionalQueryParameters = {}
661
+ ) => {
662
+ const slugsToExcludePerEntity =
663
+ useExistingTemplateSlugs( templatePrefixes );
664
+ const recordsToExcludePerEntity = useSelect(
665
+ ( select ) => {
666
+ return Object.entries( slugsToExcludePerEntity || {} ).reduce(
667
+ ( accumulator, [ slug, slugsWithTemplates ] ) => {
668
+ const entitiesWithTemplates = select(
669
+ coreStore
670
+ ).getEntityRecords( entityName, slug, {
671
+ _fields: 'id',
672
+ context: 'view',
673
+ slug: slugsWithTemplates,
674
+ ...additionalQueryParameters[ slug ],
675
+ } );
676
+ if ( entitiesWithTemplates?.length ) {
677
+ accumulator[ slug ] = entitiesWithTemplates;
678
+ }
679
+ return accumulator;
680
+ },
681
+ {}
682
+ );
683
+ },
684
+ [ slugsToExcludePerEntity ]
685
+ );
686
+ return recordsToExcludePerEntity;
687
+ };
688
+
689
+ /**
690
+ * Helper hook that returns information about an entity having
691
+ * records that we can create a specific template for.
692
+ *
693
+ * For example we can search for `terms` in `taxonomy` entity or
694
+ * `posts` in `postType` entity.
695
+ *
696
+ * First we need to find the existing records with an associated template,
697
+ * to query afterwards for any remaining record, by excluding them.
698
+ *
699
+ * @param {string} entityName The entity's name.
700
+ * @param {Record<string,string>} templatePrefixes An object with the entity's slug as key and the template prefix as value.
701
+ * @param {Record<string,Object>} additionalQueryParameters An object with the entity's slug as key and additional query parameters as value.
702
+ * @return {Record<string,EntitiesInfo>} An object with the entity's slug as key and the EntitiesInfo as value.
703
+ */
704
+ const useEntitiesInfo = (
705
+ entityName,
706
+ templatePrefixes,
707
+ additionalQueryParameters = EMPTY_OBJECT
708
+ ) => {
709
+ const recordsToExcludePerEntity = useTemplatesToExclude(
710
+ entityName,
711
+ templatePrefixes,
712
+ additionalQueryParameters
713
+ );
714
+ const entitiesHasRecords = useSelect(
715
+ ( select ) => {
716
+ return Object.keys( templatePrefixes || {} ).reduce(
717
+ ( accumulator, slug ) => {
718
+ const existingEntitiesIds =
719
+ recordsToExcludePerEntity?.[ slug ]?.map(
720
+ ( { id } ) => id
721
+ ) || [];
722
+ accumulator[ slug ] = !! select(
723
+ coreStore
724
+ ).getEntityRecords( entityName, slug, {
725
+ per_page: 1,
726
+ _fields: 'id',
727
+ context: 'view',
728
+ exclude: existingEntitiesIds,
729
+ ...additionalQueryParameters[ slug ],
730
+ } )?.length;
731
+ return accumulator;
732
+ },
733
+ {}
734
+ );
735
+ },
736
+ [
737
+ templatePrefixes,
738
+ recordsToExcludePerEntity,
739
+ entityName,
740
+ additionalQueryParameters,
741
+ ]
742
+ );
743
+ const entitiesInfo = useMemo( () => {
744
+ return Object.keys( templatePrefixes || {} ).reduce(
745
+ ( accumulator, slug ) => {
746
+ const existingEntitiesIds =
747
+ recordsToExcludePerEntity?.[ slug ]?.map(
748
+ ( { id } ) => id
749
+ ) || [];
750
+ accumulator[ slug ] = {
751
+ hasEntities: entitiesHasRecords[ slug ],
752
+ existingEntitiesIds,
753
+ };
754
+ return accumulator;
755
+ },
756
+ {}
757
+ );
758
+ }, [ templatePrefixes, recordsToExcludePerEntity, entitiesHasRecords ] );
759
+ return entitiesInfo;
760
+ };