Repository navigation
Expand file tree
/
Copy pathindex.jsx
More file actions
414 lines (398 loc) · 12.2 KB
/
Copy pathindex.jsx
File metadata and controls
414 lines (398 loc) · 12.2 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import {
setDefaultBlockName,
setFreeformContentHandlerName,
setUnregisteredTypeHandlerName,
setGroupingBlockName,
registerBlockType,
store as blocksStore,
} from '@wordpress/blocks';
import { useDisabled } from '@wordpress/compose';
import { select } from '@wordpress/data';
import { useBlockProps } from '@wordpress/block-editor';
import { useServerSideRender } from '@wordpress/server-side-render';
import { __, sprintf } from '@wordpress/i18n';
import HtmlRenderer from './utils/html-renderer';
// Experimental blocks are only registered in the Gutenberg plugin (see
// `__experimentalRegisterExperimentalCoreBlocks`). `registerCoreBlocks`
// filters them out via `isBlockMetadataExperimental`, so they are never
// available in WordPress core regardless of what ends up in the bundle.
import * as accordion from './accordion';
import * as accordionItem from './accordion-item';
import * as accordionHeading from './accordion-heading';
import * as accordionPanel from './accordion-panel';
import * as archives from './archives';
import * as avatar from './avatar';
import * as audio from './audio';
import * as breadcrumbs from './breadcrumbs';
import * as button from './button';
import * as buttons from './buttons';
import * as calendar from './calendar';
import * as categories from './categories';
import * as classic from './freeform';
import * as code from './code';
import * as column from './column';
import * as columns from './columns';
import * as comments from './comments';
import * as commentAuthorAvatar from './comment-author-avatar';
import * as commentAuthorName from './comment-author-name';
import * as commentContent from './comment-content';
import * as commentDate from './comment-date';
import * as commentEditLink from './comment-edit-link';
import * as commentReplyLink from './comment-reply-link';
import * as commentTemplate from './comment-template';
import * as commentsPaginationPrevious from './comments-pagination-previous';
import * as commentsPagination from './comments-pagination';
import * as commentsPaginationNext from './comments-pagination-next';
import * as commentsPaginationNumbers from './comments-pagination-numbers';
import * as commentsTitle from './comments-title';
import * as cover from './cover';
import * as details from './details';
import * as embed from './embed';
import * as file from './file';
import * as gallery from './gallery';
import * as group from './group';
import * as heading from './heading';
import * as homeLink from './home-link';
import * as html from './html';
import * as icon from './icon';
import * as image from './image';
import * as latestComments from './latest-comments';
import * as latestPosts from './latest-posts';
import * as list from './list';
import * as math from './math';
import * as listItem from './list-item';
import * as logInOut from './loginout';
import * as mediaText from './media-text';
import * as missing from './missing';
import * as more from './more';
import * as navigation from './navigation';
import * as navigationLink from './navigation-link';
import * as navigationSubmenu from './navigation-submenu';
import * as nextpage from './nextpage';
import * as navigationOverlayClose from './navigation-overlay-close';
import * as pattern from './pattern';
import * as pageList from './page-list';
import * as pageListItem from './page-list-item';
import * as paragraph from './paragraph';
import * as playlist from './playlist';
import * as playlistTrack from './playlist-track';
import * as postAuthor from './post-author';
import * as postAuthorName from './post-author-name';
import * as postAuthorBiography from './post-author-biography';
import * as postComment from './post-comment';
import * as postCommentsCount from './post-comments-count';
import * as postCommentsForm from './post-comments-form';
import * as postCommentsLink from './post-comments-link';
import * as postContent from './post-content';
import * as postDate from './post-date';
import * as postExcerpt from './post-excerpt';
import * as postFeaturedImage from './post-featured-image';
import * as postNavigationLink from './post-navigation-link';
import * as postTemplate from './post-template';
import * as postTerms from './post-terms';
import * as postTimeToRead from './post-time-to-read';
import * as postTitle from './post-title';
import * as preformatted from './preformatted';
import * as pullquote from './pullquote';
import * as query from './query';
import * as queryNoResults from './query-no-results';
import * as queryPagination from './query-pagination';
import * as queryPaginationNext from './query-pagination-next';
import * as queryPaginationNumbers from './query-pagination-numbers';
import * as queryPaginationPrevious from './query-pagination-previous';
import * as queryTitle from './query-title';
import * as queryTotal from './query-total';
import * as quote from './quote';
import * as reusableBlock from './block';
import * as readMore from './read-more';
import * as rss from './rss';
import * as search from './search';
import * as separator from './separator';
import * as shortcode from './shortcode';
import * as siteLogo from './site-logo';
import * as siteTagline from './site-tagline';
import * as siteTitle from './site-title';
import * as socialLink from './social-link';
import * as socialLinks from './social-links';
import * as spacer from './spacer';
import * as tabPanel from './tab-panel';
import * as tabPanels from './tab-panels';
import * as table from './table';
import * as tableOfContents from './table-of-contents';
import * as tabList from './tab-list';
import * as tabs from './tabs';
import * as tagCloud from './tag-cloud';
import * as templatePart from './template-part';
import * as termCount from './term-count';
import * as termDescription from './term-description';
import * as termName from './term-name';
import * as termsQuery from './terms-query';
import * as termTemplate from './term-template';
import * as textColumns from './text-columns';
import * as verse from './verse';
import * as video from './video';
import * as footnotes from './footnotes';
import isBlockMetadataExperimental from './utils/is-block-metadata-experimental';
import { unlock } from './lock-unlock';
/**
* Function to get all the block-library blocks in an array
*/
const getAllBlocks = () => {
const blocks = [
// Common blocks are grouped at the top to prioritize their display
// in various contexts — like the inserter and auto-complete components.
paragraph,
image,
heading,
gallery,
list,
listItem,
quote,
// Register all remaining core blocks.
accordion,
accordionItem,
accordionHeading,
accordionPanel,
archives,
audio,
button,
buttons,
calendar,
categories,
code,
column,
columns,
commentAuthorAvatar,
cover,
details,
embed,
file,
group,
html,
math,
latestComments,
latestPosts,
mediaText,
missing,
more,
nextpage,
pageList,
pageListItem,
pattern,
playlist,
playlistTrack,
preformatted,
pullquote,
reusableBlock,
rss,
search,
separator,
shortcode,
socialLink,
socialLinks,
spacer,
table,
tabs,
tabList,
tabPanels,
tabPanel,
tagCloud,
textColumns,
verse,
video,
footnotes,
// theme blocks
navigation,
navigationLink,
navigationSubmenu,
siteLogo,
siteTitle,
siteTagline,
query,
templatePart,
avatar,
postTitle,
postExcerpt,
postFeaturedImage,
postContent,
postAuthor,
postAuthorName,
postComment,
postCommentsCount,
postCommentsLink,
postDate,
postTerms,
postNavigationLink,
postTemplate,
postTimeToRead,
queryPagination,
queryPaginationNext,
queryPaginationNumbers,
queryPaginationPrevious,
queryNoResults,
queryTotal,
readMore,
comments,
commentAuthorName,
commentContent,
commentDate,
commentEditLink,
commentReplyLink,
commentTemplate,
commentsTitle,
commentsPagination,
commentsPaginationNext,
commentsPaginationNumbers,
commentsPaginationPrevious,
postCommentsForm,
tableOfContents,
homeLink,
icon,
logInOut,
navigationOverlayClose,
termCount,
termDescription,
termName,
termsQuery,
termTemplate,
queryTitle,
postAuthorBiography,
breadcrumbs,
];
if ( window?.__experimentalEnableBlockExperiments ) {
// Blocks added here are only registered when the "Block experiments"
// option is enabled in the Gutenberg > Experiments settings page.
}
blocks.push( classic );
return blocks.filter( Boolean );
};
/**
* Function to get all the core blocks in an array.
*
* @example
* ```js
* import { __experimentalGetCoreBlocks } from '@wordpress/block-library';
*
* const coreBlocks = __experimentalGetCoreBlocks();
* ```
*/
export const __experimentalGetCoreBlocks = () =>
getAllBlocks().filter(
( { metadata } ) => ! isBlockMetadataExperimental( metadata )
);
/**
* Function to register core blocks provided by the block editor.
*
* @param {Array} blocks An optional array of the core blocks being registered.
*
* @example
* ```js
* import { registerCoreBlocks } from '@wordpress/block-library';
*
* registerCoreBlocks();
* ```
*/
export const registerCoreBlocks = (
blocks = __experimentalGetCoreBlocks()
) => {
blocks.forEach( ( { init } ) => init() );
// Auto-register PHP-only blocks with ServerSideRender
if ( window.__unstableAutoRegisterBlocks ) {
window.__unstableAutoRegisterBlocks.forEach( ( blockName ) => {
const bootstrappedBlockType = unlock(
select( blocksStore )
).getBootstrappedBlockType( blockName );
registerBlockType( blockName, {
// Use all metadata from PHP registration,
// but fall back title to block name if not provided,
// ensure minimum apiVersion 3 for block wrapper support,
// and override with a ServerSideRender-based edit function.
...bootstrappedBlockType,
title: bootstrappedBlockType?.title || blockName,
...( ( bootstrappedBlockType?.apiVersion ?? 0 ) < 3 && {
apiVersion: 3,
} ),
// Always pass the postId context so the server-side render can
// reproduce the same output as the front end, while preserving
// any context declared in the block's PHP registration.
usesContext: Array.from(
new Set( [
...( bootstrappedBlockType?.usesContext ?? [] ),
'postId',
] )
),
// Inspector controls are rendered by the auto-register hook in block-editor
edit: function Edit( { attributes, context } ) {
const disabledRef = useDisabled();
const blockProps = useBlockProps( { ref: disabledRef } );
const { content, status, error } = useServerSideRender( {
block: blockName,
attributes,
urlQueryArgs: { post_id: context?.postId },
} );
if ( status === 'loading' ) {
return (
<div { ...blockProps }>{ __( 'Loading…' ) }</div>
);
}
if ( status === 'error' ) {
return (
<div { ...blockProps }>
{ sprintf(
/* translators: %s: error message describing the problem */
__( 'Error loading block: %s' ),
error
) }
</div>
);
}
return (
<HtmlRenderer
wrapperProps={ blockProps }
html={ content }
/>
);
},
save: () => null,
} );
} );
}
setDefaultBlockName( paragraph.name );
if (
window.wp &&
window.wp.oldEditor &&
blocks.some( ( { name } ) => name === classic.name )
) {
setFreeformContentHandlerName( classic.name );
}
setUnregisteredTypeHandlerName( missing.name );
setGroupingBlockName( group.name );
};
/**
* Function to register experimental core blocks depending on editor settings.
*
* @param {boolean} enableFSEBlocks Whether to enable the full site editing blocks.
* @example
* ```js
* import { __experimentalRegisterExperimentalCoreBlocks } from '@wordpress/block-library';
*
* __experimentalRegisterExperimentalCoreBlocks( settings );
* ```
*/
export const __experimentalRegisterExperimentalCoreBlocks =
globalThis.IS_GUTENBERG_PLUGIN
? ( { enableFSEBlocks } = {} ) => {
const enabledExperiments = [ enableFSEBlocks ? 'fse' : null ];
getAllBlocks()
.filter( ( { metadata } ) =>
isBlockMetadataExperimental( metadata )
)
.filter(
( { metadata: { __experimental } } ) =>
__experimental === true ||
enabledExperiments.includes( __experimental )
)
.forEach( ( { init } ) => init() );
}
: undefined;
export { privateApis } from './private-apis';