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