@wordpress/edit-site 6.33.4 → 6.33.6

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 (61) hide show
  1. package/build/components/add-new-template/add-custom-template-modal-content.js +3 -1
  2. package/build/components/add-new-template/add-custom-template-modal-content.js.map +2 -2
  3. package/build/components/add-new-template/index.js +6 -4
  4. package/build/components/add-new-template/index.js.map +2 -2
  5. package/build/components/add-new-template/utils.js +84 -9
  6. package/build/components/add-new-template/utils.js.map +2 -2
  7. package/build/components/dataviews-actions/index.js +2 -55
  8. package/build/components/dataviews-actions/index.js.map +3 -3
  9. package/build/components/editor/index.js +0 -1
  10. package/build/components/editor/index.js.map +2 -2
  11. package/build/components/editor/use-resolve-edited-entity.js +5 -11
  12. package/build/components/editor/use-resolve-edited-entity.js.map +2 -2
  13. package/build/components/page-templates/fields.js +5 -80
  14. package/build/components/page-templates/fields.js.map +3 -3
  15. package/build/components/page-templates/hooks.js +1 -1
  16. package/build/components/page-templates/hooks.js.map +2 -2
  17. package/build/components/page-templates/index.js +46 -217
  18. package/build/components/page-templates/index.js.map +3 -3
  19. package/build/components/page-templates/view-utils.js +1 -5
  20. package/build/components/page-templates/view-utils.js.map +2 -2
  21. package/build/components/sidebar-navigation-screen-templates-browse/content.js +6 -21
  22. package/build/components/sidebar-navigation-screen-templates-browse/content.js.map +2 -2
  23. package/build/components/site-editor-routes/template-item.js +14 -15
  24. package/build/components/site-editor-routes/template-item.js.map +2 -2
  25. package/build-module/components/add-new-template/add-custom-template-modal-content.js +3 -1
  26. package/build-module/components/add-new-template/add-custom-template-modal-content.js.map +2 -2
  27. package/build-module/components/add-new-template/index.js +7 -4
  28. package/build-module/components/add-new-template/index.js.map +2 -2
  29. package/build-module/components/add-new-template/utils.js +84 -9
  30. package/build-module/components/add-new-template/utils.js.map +2 -2
  31. package/build-module/components/dataviews-actions/index.js +1 -53
  32. package/build-module/components/dataviews-actions/index.js.map +2 -2
  33. package/build-module/components/editor/index.js +0 -1
  34. package/build-module/components/editor/index.js.map +2 -2
  35. package/build-module/components/editor/use-resolve-edited-entity.js +5 -11
  36. package/build-module/components/editor/use-resolve-edited-entity.js.map +2 -2
  37. package/build-module/components/page-templates/fields.js +6 -85
  38. package/build-module/components/page-templates/fields.js.map +3 -3
  39. package/build-module/components/page-templates/hooks.js +1 -1
  40. package/build-module/components/page-templates/hooks.js.map +2 -2
  41. package/build-module/components/page-templates/index.js +51 -235
  42. package/build-module/components/page-templates/index.js.map +2 -2
  43. package/build-module/components/page-templates/view-utils.js +1 -5
  44. package/build-module/components/page-templates/view-utils.js.map +2 -2
  45. package/build-module/components/sidebar-navigation-screen-templates-browse/content.js +7 -22
  46. package/build-module/components/sidebar-navigation-screen-templates-browse/content.js.map +2 -2
  47. package/build-module/components/site-editor-routes/template-item.js +14 -15
  48. package/build-module/components/site-editor-routes/template-item.js.map +2 -2
  49. package/package.json +17 -17
  50. package/src/components/add-new-template/add-custom-template-modal-content.js +3 -1
  51. package/src/components/add-new-template/index.js +9 -4
  52. package/src/components/add-new-template/utils.js +118 -9
  53. package/src/components/dataviews-actions/index.js +0 -61
  54. package/src/components/editor/index.js +0 -1
  55. package/src/components/editor/use-resolve-edited-entity.js +5 -20
  56. package/src/components/page-templates/fields.js +5 -95
  57. package/src/components/page-templates/hooks.js +1 -1
  58. package/src/components/page-templates/index.js +35 -244
  59. package/src/components/page-templates/view-utils.js +1 -8
  60. package/src/components/sidebar-navigation-screen-templates-browse/content.js +7 -22
  61. package/src/components/site-editor-routes/template-item.js +26 -28
@@ -2,22 +2,16 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { Page } from '@wordpress/admin-ui';
5
- import { __, sprintf } from '@wordpress/i18n';
6
- import { decodeEntities } from '@wordpress/html-entities';
5
+ import { __ } from '@wordpress/i18n';
7
6
  import { useState, useMemo, useCallback } from '@wordpress/element';
8
- import {
9
- privateApis as corePrivateApis,
10
- store as coreStore,
11
- } from '@wordpress/core-data';
7
+ import { privateApis as corePrivateApis } from '@wordpress/core-data';
12
8
  import { DataViews, filterSortAndPaginate } from '@wordpress/dataviews';
13
9
  import { privateApis as routerPrivateApis } from '@wordpress/router';
14
10
  import { privateApis as editorPrivateApis } from '@wordpress/editor';
15
11
  import { addQueryArgs } from '@wordpress/url';
16
- import { useSelect, useDispatch } from '@wordpress/data';
17
12
  import { useEvent } from '@wordpress/compose';
18
13
  import { useView } from '@wordpress/views';
19
- import { Button, Modal } from '@wordpress/components';
20
- import { store as noticesStore } from '@wordpress/notices';
14
+ import { Button } from '@wordpress/components';
21
15
 
22
16
  /**
23
17
  * Internal dependencies
@@ -25,22 +19,11 @@ import { store as noticesStore } from '@wordpress/notices';
25
19
  import AddNewTemplate from '../add-new-template';
26
20
  import { TEMPLATE_POST_TYPE } from '../../utils/constants';
27
21
  import { unlock } from '../../lock-unlock';
28
- import {
29
- useEditPostAction,
30
- useSetActiveTemplateAction,
31
- } from '../dataviews-actions';
32
- import {
33
- authorField,
34
- descriptionField,
35
- previewField,
36
- activeField,
37
- slugField,
38
- useThemeField,
39
- } from './fields';
22
+ import { useEditPostAction } from '../dataviews-actions';
23
+ import { authorField, descriptionField, previewField } from './fields';
40
24
  import { defaultLayouts, getDefaultView } from './view-utils';
41
25
 
42
- const { usePostActions, usePostFields, templateTitleField } =
43
- unlock( editorPrivateApis );
26
+ const { usePostActions, templateTitleField } = unlock( editorPrivateApis );
44
27
  const { useHistory, useLocation } = unlock( routerPrivateApis );
45
28
  const { useEntityRecordsWithPermissions } = unlock( corePrivateApis );
46
29
 
@@ -48,8 +31,7 @@ export default function PageTemplates() {
48
31
  const { path, query } = useLocation();
49
32
  const { activeView = 'active', postId } = query;
50
33
  const [ selection, setSelection ] = useState( [ postId ] );
51
- const [ selectedRegisteredTemplate, setSelectedRegisteredTemplate ] =
52
- useState( false );
34
+
53
35
  const defaultView = useMemo( () => {
54
36
  return getDefaultView( activeView );
55
37
  }, [ activeView ] );
@@ -73,129 +55,10 @@ export default function PageTemplates() {
73
55
  },
74
56
  } );
75
57
 
76
- const { activeTemplatesOption, activeTheme, defaultTemplateTypes } =
77
- useSelect( ( select ) => {
78
- const { getEntityRecord, getCurrentTheme } = select( coreStore );
79
- return {
80
- activeTemplatesOption: getEntityRecord( 'root', 'site' )
81
- ?.active_templates,
82
- activeTheme: getCurrentTheme(),
83
- defaultTemplateTypes:
84
- select( coreStore ).getCurrentTheme()
85
- ?.default_template_types,
86
- };
87
- } );
88
- // Todo: this will have to be better so that we're not fetching all the
89
- // records all the time. Active templates query will need to move server
90
- // side.
91
- const { records: userRecords, isResolving: isLoadingUserRecords } =
58
+ const { records, isResolving: isLoadingData } =
92
59
  useEntityRecordsWithPermissions( 'postType', TEMPLATE_POST_TYPE, {
93
60
  per_page: -1,
94
- combinedTemplates: false,
95
61
  } );
96
- const { records: staticRecords, isResolving: isLoadingStaticData } =
97
- useEntityRecordsWithPermissions( 'root', 'registeredTemplate', {
98
- // This should not be needed, the endpoint returns all registered
99
- // templates, but it's not possible right now to turn off pagination
100
- // for entity configs.
101
- per_page: -1,
102
- } );
103
-
104
- const activeTemplates = useMemo( () => {
105
- const _active = [ ...staticRecords ];
106
- if ( activeTemplatesOption ) {
107
- for ( const activeSlug in activeTemplatesOption ) {
108
- const activeId = activeTemplatesOption[ activeSlug ];
109
- // Replace the template in the array.
110
- const template = userRecords.find(
111
- ( userRecord ) =>
112
- userRecord.id === activeId &&
113
- userRecord.theme === activeTheme.stylesheet
114
- );
115
- if ( template ) {
116
- const index = _active.findIndex(
117
- ( { slug } ) => slug === template.slug
118
- );
119
- if ( index !== -1 ) {
120
- _active[ index ] = template;
121
- } else {
122
- _active.push( template );
123
- }
124
- }
125
- }
126
- }
127
- return _active;
128
- }, [ userRecords, staticRecords, activeTemplatesOption, activeTheme ] );
129
-
130
- let isLoadingData;
131
- if ( activeView === 'active' ) {
132
- isLoadingData = isLoadingUserRecords || isLoadingStaticData;
133
- } else if ( activeView === 'user' ) {
134
- isLoadingData = isLoadingUserRecords;
135
- } else {
136
- isLoadingData = isLoadingStaticData;
137
- }
138
-
139
- const records = useMemo( () => {
140
- function isCustom( record ) {
141
- // For registered templates, the is_custom field is defined.
142
- return (
143
- record.is_custom ??
144
- // For user templates it's custom if the is_wp_suggestion meta
145
- // field is not set and the slug is not found in the default
146
- // template types.
147
- ( ! record.meta?.is_wp_suggestion &&
148
- ! defaultTemplateTypes.some(
149
- ( type ) => type.slug === record.slug
150
- ) )
151
- );
152
- }
153
-
154
- let _records;
155
- if ( activeView === 'active' ) {
156
- // Don't show active custom templates in the active view.
157
- _records = activeTemplates.filter(
158
- ( record ) => ! isCustom( record )
159
- );
160
- } else if ( activeView === 'user' ) {
161
- _records = userRecords;
162
- } else {
163
- _records = staticRecords;
164
- }
165
- return _records.map( ( record ) => ( {
166
- ...record,
167
- _isActive: activeTemplates.some(
168
- ( template ) => template.id === record.id
169
- ),
170
- _isCustom: isCustom( record ),
171
- } ) );
172
- }, [
173
- activeTemplates,
174
- defaultTemplateTypes,
175
- userRecords,
176
- staticRecords,
177
- activeView,
178
- ] );
179
-
180
- const users = useSelect(
181
- ( select ) => {
182
- const { getUser } = select( coreStore );
183
- return records.reduce( ( acc, record ) => {
184
- if ( record.author_text ) {
185
- if ( ! acc[ record.author_text ] ) {
186
- acc[ record.author_text ] = record.author_text;
187
- }
188
- } else if ( record.author ) {
189
- if ( ! acc[ record.author ] ) {
190
- acc[ record.author ] = getUser( record.author );
191
- }
192
- }
193
- return acc;
194
- }, {} );
195
- },
196
- [ records ]
197
- );
198
-
199
62
  const history = useHistory();
200
63
  const onChangeSelection = useCallback(
201
64
  ( items ) => {
@@ -211,95 +74,45 @@ export default function PageTemplates() {
211
74
  [ history, path, view?.type ]
212
75
  );
213
76
 
214
- const postTypeFields = usePostFields( {
215
- postType: TEMPLATE_POST_TYPE,
216
- } );
217
- const dateField = postTypeFields.find( ( field ) => field.id === 'date' );
218
- const themeField = useThemeField();
219
- const fields = useMemo( () => {
220
- const _fields = [
77
+ const authors = useMemo( () => {
78
+ if ( ! records ) {
79
+ return [];
80
+ }
81
+ const authorsSet = new Set();
82
+ records.forEach( ( template ) => {
83
+ authorsSet.add( template.author_text );
84
+ } );
85
+ return Array.from( authorsSet ).map( ( author ) => ( {
86
+ value: author,
87
+ label: author,
88
+ } ) );
89
+ }, [ records ] );
90
+
91
+ const fields = useMemo(
92
+ () => [
221
93
  previewField,
222
94
  templateTitleField,
223
95
  descriptionField,
224
- activeField,
225
- slugField,
226
- ];
227
- if ( activeView === 'user' ) {
228
- _fields.push( themeField );
229
- if ( dateField ) {
230
- _fields.push( dateField );
231
- }
232
- }
233
- const elements = [];
234
- for ( const author in users ) {
235
- elements.push( {
236
- value: users[ author ]?.id ?? author,
237
- label: users[ author ]?.name ?? author,
238
- } );
239
- }
240
- _fields.push( {
241
- ...authorField,
242
- elements,
243
- } );
244
- return _fields;
245
- }, [ users, activeView, themeField, dateField ] );
96
+ {
97
+ ...authorField,
98
+ elements: authors,
99
+ },
100
+ ],
101
+ [ authors ]
102
+ );
246
103
 
247
104
  const { data, paginationInfo } = useMemo( () => {
248
105
  return filterSortAndPaginate( records, view, fields );
249
106
  }, [ records, view, fields ] );
250
107
 
251
- const { createSuccessNotice } = useDispatch( noticesStore );
252
- const onActionPerformed = useCallback(
253
- ( actionId, items ) => {
254
- switch ( actionId ) {
255
- case 'duplicate-post':
256
- {
257
- const newItem = items[ 0 ];
258
- const _title =
259
- typeof newItem.title === 'string'
260
- ? newItem.title
261
- : newItem.title?.rendered;
262
- history.navigate( `/template?activeView=user` );
263
- createSuccessNotice(
264
- sprintf(
265
- // translators: %s: Title of the created post or template, e.g: "Hello world".
266
- __( '"%s" successfully created.' ),
267
- decodeEntities( _title ) || __( '(no title)' )
268
- ),
269
- {
270
- type: 'snackbar',
271
- id: 'duplicate-post-action',
272
- actions: [
273
- {
274
- label: __( 'Edit' ),
275
- onClick: () => {
276
- history.navigate(
277
- `/${ newItem.type }/${ newItem.id }?canvas=edit`
278
- );
279
- },
280
- },
281
- ],
282
- }
283
- );
284
- }
285
- break;
286
- }
287
- },
288
- [ history, createSuccessNotice ]
289
- );
290
108
  const postTypeActions = usePostActions( {
291
109
  postType: TEMPLATE_POST_TYPE,
292
110
  context: 'list',
293
- onActionPerformed,
294
111
  } );
295
112
  const editAction = useEditPostAction();
296
- const setActiveTemplateAction = useSetActiveTemplateAction();
297
113
  const actions = useMemo(
298
- () =>
299
- activeView === 'user'
300
- ? [ setActiveTemplateAction, editAction, ...postTypeActions ]
301
- : [ setActiveTemplateAction, ...postTypeActions ],
302
- [ postTypeActions, setActiveTemplateAction, editAction, activeView ]
114
+ () => [ editAction, ...postTypeActions ],
115
+ [ postTypeActions, editAction ]
303
116
  );
304
117
 
305
118
  const onChangeView = useEvent( ( newView ) => {
@@ -310,10 +123,6 @@ export default function PageTemplates() {
310
123
  updateView( newView );
311
124
  } );
312
125
 
313
- const duplicateAction = actions.find(
314
- ( action ) => action.id === 'duplicate-post'
315
- );
316
-
317
126
  return (
318
127
  <Page
319
128
  className="edit-site-page-templates"
@@ -346,30 +155,12 @@ export default function PageTemplates() {
346
155
  onChangeView={ onChangeView }
347
156
  onChangeSelection={ onChangeSelection }
348
157
  isItemClickable={ () => true }
349
- onClickItem={ ( item ) => {
350
- if ( typeof item.id === 'string' ) {
351
- setSelectedRegisteredTemplate( item );
352
- } else {
353
- history.navigate(
354
- `/${ item.type }/${ item.id }?canvas=edit`
355
- );
356
- }
158
+ onClickItem={ ( { id } ) => {
159
+ history.navigate( `/wp_template/${ id }?canvas=edit` );
357
160
  } }
358
161
  selection={ selection }
359
162
  defaultLayouts={ defaultLayouts }
360
163
  />
361
- { selectedRegisteredTemplate && duplicateAction && (
362
- <Modal
363
- title={ __( 'Duplicate' ) }
364
- onRequestClose={ () => setSelectedRegisteredTemplate() }
365
- size="small"
366
- >
367
- <duplicateAction.RenderModal
368
- items={ [ selectedRegisteredTemplate ] }
369
- closeModal={ () => setSelectedRegisteredTemplate() }
370
- />
371
- </Modal>
372
- ) }
373
164
  </Page>
374
165
  );
375
166
  }
@@ -20,7 +20,7 @@ const DEFAULT_VIEW = {
20
20
  titleField: 'title',
21
21
  descriptionField: 'description',
22
22
  mediaField: 'preview',
23
- fields: [ 'author', 'active', 'slug', 'theme' ],
23
+ fields: [ 'author', 'active', 'slug' ],
24
24
  filters: [],
25
25
  ...defaultLayouts.grid,
26
26
  };
@@ -28,13 +28,6 @@ const DEFAULT_VIEW = {
28
28
  export function getDefaultView( activeView ) {
29
29
  return {
30
30
  ...DEFAULT_VIEW,
31
- sort:
32
- activeView === 'user'
33
- ? {
34
- field: 'date',
35
- direction: 'desc',
36
- }
37
- : DEFAULT_VIEW.sort,
38
31
  filters: ! [ 'active', 'user' ].includes( activeView )
39
32
  ? [
40
33
  {
@@ -13,7 +13,8 @@ import { addQueryArgs } from '@wordpress/url';
13
13
  */
14
14
  import SidebarNavigationItem from '../sidebar-navigation-item';
15
15
  import { useAddedBy } from '../page-templates/hooks';
16
- import { commentAuthorAvatar, published } from '@wordpress/icons';
16
+ import { layout } from '@wordpress/icons';
17
+ import { TEMPLATE_POST_TYPE } from '../../utils/constants';
17
18
  import { unlock } from '../../lock-unlock';
18
19
 
19
20
  const { useLocation } = unlock( routerPrivateApis );
@@ -36,12 +37,9 @@ function TemplateDataviewItem( { template, isActive } ) {
36
37
 
37
38
  export default function DataviewsTemplatesSidebarContent() {
38
39
  const {
39
- query: { activeView = 'active' },
40
+ query: { activeView = 'all' },
40
41
  } = useLocation();
41
- const { records } = useEntityRecords( 'root', 'registeredTemplate', {
42
- // This should not be needed, the endpoint returns all registered
43
- // templates, but it's not possible right now to turn off pagination for
44
- // entity configs.
42
+ const { records } = useEntityRecords( 'postType', TEMPLATE_POST_TYPE, {
45
43
  per_page: -1,
46
44
  } );
47
45
  const firstItemPerAuthorText = useMemo( () => {
@@ -62,23 +60,10 @@ export default function DataviewsTemplatesSidebarContent() {
62
60
  <ItemGroup className="edit-site-sidebar-navigation-screen-templates-browse">
63
61
  <SidebarNavigationItem
64
62
  to="/template"
65
- icon={ published }
66
- aria-current={ activeView === 'active' }
63
+ icon={ layout }
64
+ aria-current={ activeView === 'all' }
67
65
  >
68
- { __( 'Active templates' ) }
69
- </SidebarNavigationItem>
70
- <SidebarNavigationItem
71
- to={ addQueryArgs( '/template', { activeView: 'user' } ) }
72
- icon={ commentAuthorAvatar }
73
- aria-current={ activeView === 'user' }
74
- >
75
- {
76
- // Let's avoid calling them "custom templates" to avoid
77
- // confusion. "Created" is closest to meaning database
78
- // templates, created by users.
79
- // https://developer.wordpress.org/themes/classic-themes/templates/page-template-files/#creating-custom-page-templates-for-global-use
80
- __( 'Created templates' )
81
- }
66
+ { __( 'All templates' ) }
82
67
  </SidebarNavigationItem>
83
68
  { firstItemPerAuthorText.map( ( template ) => {
84
69
  return (
@@ -5,35 +5,33 @@ import Editor from '../editor';
5
5
  import SidebarNavigationScreenTemplatesBrowse from '../sidebar-navigation-screen-templates-browse';
6
6
  import SidebarNavigationScreenUnsupported from '../sidebar-navigation-screen-unsupported';
7
7
 
8
- const areas = {
9
- sidebar( { siteData } ) {
10
- const isBlockTheme = siteData.currentTheme?.is_block_theme;
11
- return isBlockTheme ? (
12
- <SidebarNavigationScreenTemplatesBrowse backPath="/" />
13
- ) : (
14
- <SidebarNavigationScreenUnsupported />
15
- );
16
- },
17
- mobile( { siteData } ) {
18
- const isBlockTheme = siteData.currentTheme?.is_block_theme;
19
- return isBlockTheme ? (
20
- <Editor />
21
- ) : (
22
- <SidebarNavigationScreenUnsupported />
23
- );
24
- },
25
- preview( { siteData } ) {
26
- const isBlockTheme = siteData.currentTheme?.is_block_theme;
27
- return isBlockTheme ? (
28
- <Editor />
29
- ) : (
30
- <SidebarNavigationScreenUnsupported />
31
- );
32
- },
33
- };
34
-
35
8
  export const templateItemRoute = {
36
9
  name: 'template-item',
37
10
  path: '/wp_template/*postId',
38
- areas,
11
+ areas: {
12
+ sidebar( { siteData } ) {
13
+ const isBlockTheme = siteData.currentTheme?.is_block_theme;
14
+ return isBlockTheme ? (
15
+ <SidebarNavigationScreenTemplatesBrowse backPath="/" />
16
+ ) : (
17
+ <SidebarNavigationScreenUnsupported />
18
+ );
19
+ },
20
+ mobile( { siteData } ) {
21
+ const isBlockTheme = siteData.currentTheme?.is_block_theme;
22
+ return isBlockTheme ? (
23
+ <Editor />
24
+ ) : (
25
+ <SidebarNavigationScreenUnsupported />
26
+ );
27
+ },
28
+ preview( { siteData } ) {
29
+ const isBlockTheme = siteData.currentTheme?.is_block_theme;
30
+ return isBlockTheme ? (
31
+ <Editor />
32
+ ) : (
33
+ <SidebarNavigationScreenUnsupported />
34
+ );
35
+ },
36
+ },
39
37
  };