-
Notifications
You must be signed in to change notification settings - Fork 4k
/
new-template.js
268 lines (257 loc) · 6.67 KB
/
new-template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* External dependencies
*/
import { filter, includes } from 'lodash';
/**
* WordPress dependencies
*/
import {
DropdownMenu,
MenuGroup,
MenuItem,
NavigableMenu,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import {
archive,
blockMeta,
category,
home,
list,
media,
notFound,
page,
post,
postAuthor,
postDate,
search,
tag,
} from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import AddCustomTemplateModal from './add-custom-template-modal';
import { usePostTypes, usePostTypesEntitiesInfo } from './utils';
import { useHistory } from '../routes';
import { store as editSiteStore } from '../../store';
const DEFAULT_TEMPLATE_SLUGS = [
'front-page',
'single',
'page',
'index',
'archive',
'author',
'category',
'date',
'tag',
'taxonomy',
'search',
'404',
];
const TEMPLATE_ICONS = {
'front-page': home,
single: post,
page,
archive,
search,
404: notFound,
index: list,
category,
author: postAuthor,
taxonomy: blockMeta,
date: postDate,
tag,
attachment: media,
};
export default function NewTemplate( { postType } ) {
const history = useHistory();
const postTypes = usePostTypes();
const [ showCustomTemplateModal, setShowCustomTemplateModal ] =
useState( false );
const [ entityForSuggestions, setEntityForSuggestions ] = useState( {} );
const { existingTemplates, defaultTemplateTypes } = useSelect(
( select ) => ( {
existingTemplates: select( coreStore ).getEntityRecords(
'postType',
'wp_template',
{ per_page: -1 }
),
defaultTemplateTypes:
select( editorStore ).__experimentalGetDefaultTemplateTypes(),
} ),
[]
);
const postTypesEntitiesInfo = usePostTypesEntitiesInfo( existingTemplates );
const { saveEntityRecord } = useDispatch( coreStore );
const { createErrorNotice } = useDispatch( noticesStore );
const { setTemplate } = useDispatch( editSiteStore );
async function createTemplate( template ) {
try {
const { title, description, slug } = template;
const newTemplate = await saveEntityRecord(
'postType',
'wp_template',
{
description,
// Slugs need to be strings, so this is for template `404`
slug: slug.toString(),
status: 'publish',
title,
// This adds a post meta field in template that is part of `is_custom` value calculation.
is_wp_suggestion: true,
},
{ throwOnError: true }
);
// Set template before navigating away to avoid initial stale value.
setTemplate( newTemplate.id, newTemplate.slug );
// Navigate to the created template editor.
history.push( {
postId: newTemplate.id,
postType: newTemplate.type,
} );
// TODO: Add a success notice?
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while creating the template.' );
createErrorNotice( errorMessage, {
type: 'snackbar',
} );
}
}
const existingTemplateSlugs = ( existingTemplates || [] ).map(
( { slug } ) => slug
);
const missingTemplates = filter(
defaultTemplateTypes,
( template ) =>
includes( DEFAULT_TEMPLATE_SLUGS, template.slug ) &&
! includes( existingTemplateSlugs, template.slug )
);
const extraTemplates = ( postTypes || [] ).reduce(
( accumulator, _postType ) => {
const { slug, labels, icon } = _postType;
const hasGeneralTemplate = existingTemplateSlugs?.includes(
`single-${ slug }`
);
const hasEntities = postTypesEntitiesInfo?.[ slug ]?.hasEntities;
const menuItem = {
slug: `single-${ slug }`,
title: sprintf(
// translators: %s: Name of the post type e.g: "Post".
__( 'Single item: %s' ),
labels.singular_name
),
description: sprintf(
// translators: %s: Name of the post type e.g: "Post".
__( 'Displays a single item: %s.' ),
labels.singular_name
),
// `icon` is the `menu_icon` property of a post type. We
// only handle `dashicons` for now, even if the `menu_icon`
// also supports urls and svg as values.
icon: icon?.startsWith( 'dashicons-' )
? icon.slice( 10 )
: null,
};
// We have a different template creation flow only if they have entities.
if ( hasEntities ) {
menuItem.onClick = ( template ) => {
setShowCustomTemplateModal( true );
setEntityForSuggestions( {
type: 'postType',
slug,
labels,
hasGeneralTemplate,
template,
postsToExclude:
postTypesEntitiesInfo[ slug ].existingPosts,
} );
};
}
// We don't need to add the menu item if there are no
// entities and the general template exists.
if ( ! hasGeneralTemplate || hasEntities ) {
accumulator.push( menuItem );
}
return accumulator;
},
[]
);
if ( ! missingTemplates.length && ! extraTemplates.length ) {
return null;
}
// Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.
missingTemplates?.sort( ( template1, template2 ) => {
return (
DEFAULT_TEMPLATE_SLUGS.indexOf( template1.slug ) -
DEFAULT_TEMPLATE_SLUGS.indexOf( template2.slug )
);
} );
// Append all extra templates at the end of the list for now.
missingTemplates.push( ...extraTemplates );
return (
<>
<DropdownMenu
className="edit-site-new-template-dropdown"
icon={ null }
text={ postType.labels.add_new }
label={ postType.labels.add_new_item }
popoverProps={ {
noArrow: false,
} }
toggleProps={ {
variant: 'primary',
} }
>
{ () => (
<NavigableMenu className="edit-site-new-template-dropdown__popover">
<MenuGroup label={ postType.labels.add_new_item }>
{ missingTemplates.map( ( template ) => {
const {
title,
description,
slug,
onClick,
icon,
} = template;
return (
<MenuItem
icon={
icon ||
TEMPLATE_ICONS[ slug ] ||
post
}
iconPosition="left"
info={ description }
key={ slug }
onClick={ () =>
onClick
? onClick( template )
: createTemplate( template )
}
>
{ title }
</MenuItem>
);
} ) }
</MenuGroup>
</NavigableMenu>
) }
</DropdownMenu>
{ showCustomTemplateModal && (
<AddCustomTemplateModal
onClose={ () => setShowCustomTemplateModal( false ) }
onSelect={ createTemplate }
entityForSuggestions={ entityForSuggestions }
/>
) }
</>
);
}