@wordpress/core-data 4.13.0 → 5.0.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.
- package/CHANGELOG.md +12 -0
- package/README.md +148 -65
- package/build/entities.js +38 -51
- package/build/entities.js.map +1 -1
- package/build/hooks/index.js +14 -0
- package/build/hooks/index.js.map +1 -1
- package/build/hooks/use-entity-record.js +10 -6
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-entity-records.js +2 -0
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/hooks/use-query-select.js +5 -2
- package/build/hooks/use-query-select.js.map +1 -1
- package/build/hooks/use-resource-permissions.js +74 -11
- package/build/hooks/use-resource-permissions.js.map +1 -1
- package/build/index.js +1 -30
- package/build/index.js.map +1 -1
- package/build/resolvers.js +59 -39
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +9 -46
- package/build/selectors.js.map +1 -1
- package/build-module/entities.js +38 -52
- package/build-module/entities.js.map +1 -1
- package/build-module/hooks/index.js +1 -0
- package/build-module/hooks/index.js.map +1 -1
- package/build-module/hooks/use-entity-record.js +10 -6
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-entity-records.js +2 -0
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/hooks/use-query-select.js +4 -1
- package/build-module/hooks/use-query-select.js.map +1 -1
- package/build-module/hooks/use-resource-permissions.js +70 -10
- package/build-module/hooks/use-resource-permissions.js.map +1 -1
- package/build-module/index.js +0 -5
- package/build-module/index.js.map +1 -1
- package/build-module/resolvers.js +59 -39
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +7 -44
- package/build-module/selectors.js.map +1 -1
- package/build-types/actions.d.ts +188 -0
- package/build-types/actions.d.ts.map +1 -0
- package/build-types/batch/create-batch.d.ts +71 -0
- package/build-types/batch/create-batch.d.ts.map +1 -0
- package/build-types/batch/default-processor.d.ts +11 -0
- package/build-types/batch/default-processor.d.ts.map +1 -0
- package/build-types/batch/index.d.ts +3 -0
- package/build-types/batch/index.d.ts.map +1 -0
- package/build-types/entities.d.ts +128 -0
- package/build-types/entities.d.ts.map +1 -0
- package/build-types/entity-provider.d.ts +68 -0
- package/build-types/entity-provider.d.ts.map +1 -0
- package/build-types/entity-types/attachment.d.ts +121 -0
- package/build-types/entity-types/attachment.d.ts.map +1 -0
- package/build-types/entity-types/base-entity-records.d.ts +37 -0
- package/build-types/entity-types/base-entity-records.d.ts.map +1 -0
- package/build-types/entity-types/comment.d.ts +82 -0
- package/build-types/entity-types/comment.d.ts.map +1 -0
- package/build-types/entity-types/helpers.d.ts +123 -0
- package/build-types/entity-types/helpers.d.ts.map +1 -0
- package/build-types/entity-types/index.d.ts +64 -0
- package/build-types/entity-types/index.d.ts.map +1 -0
- package/build-types/entity-types/menu-location.d.ts +25 -0
- package/build-types/entity-types/menu-location.d.ts.map +1 -0
- package/build-types/entity-types/nav-menu-item.d.ts +88 -0
- package/build-types/entity-types/nav-menu-item.d.ts.map +1 -0
- package/build-types/entity-types/nav-menu.d.ts +45 -0
- package/build-types/entity-types/nav-menu.d.ts.map +1 -0
- package/build-types/entity-types/page.d.ts +120 -0
- package/build-types/entity-types/page.d.ts.map +1 -0
- package/build-types/entity-types/plugin.d.ts +62 -0
- package/build-types/entity-types/plugin.d.ts.map +1 -0
- package/build-types/entity-types/post.d.ts +128 -0
- package/build-types/entity-types/post.d.ts.map +1 -0
- package/build-types/entity-types/settings.d.ts +89 -0
- package/build-types/entity-types/settings.d.ts.map +1 -0
- package/build-types/entity-types/sidebar.d.ts +55 -0
- package/build-types/entity-types/sidebar.d.ts.map +1 -0
- package/build-types/entity-types/taxonomy.d.ts +83 -0
- package/build-types/entity-types/taxonomy.d.ts.map +1 -0
- package/build-types/entity-types/theme.d.ts +206 -0
- package/build-types/entity-types/theme.d.ts.map +1 -0
- package/build-types/entity-types/type.d.ts +71 -0
- package/build-types/entity-types/type.d.ts.map +1 -0
- package/build-types/entity-types/user.d.ts +93 -0
- package/build-types/entity-types/user.d.ts.map +1 -0
- package/build-types/entity-types/widget-type.d.ts +33 -0
- package/build-types/entity-types/widget-type.d.ts.map +1 -0
- package/build-types/entity-types/widget.d.ts +59 -0
- package/build-types/entity-types/widget.d.ts.map +1 -0
- package/build-types/entity-types/wp-template-part.d.ts +80 -0
- package/build-types/entity-types/wp-template-part.d.ts.map +1 -0
- package/build-types/entity-types/wp-template.d.ts +80 -0
- package/build-types/entity-types/wp-template.d.ts.map +1 -0
- package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts +139 -0
- package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts.map +1 -0
- package/build-types/fetch/__experimental-fetch-url-data.d.ts +35 -0
- package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -0
- package/build-types/fetch/index.d.ts +3 -0
- package/build-types/fetch/index.d.ts.map +1 -0
- package/build-types/hooks/constants.d.ts +7 -0
- package/build-types/hooks/constants.d.ts.map +1 -0
- package/build-types/hooks/index.d.ts +4 -0
- package/build-types/hooks/index.d.ts.map +1 -0
- package/build-types/hooks/memoize.d.ts +3 -0
- package/build-types/hooks/memoize.d.ts.map +1 -0
- package/build-types/hooks/use-entity-record.d.ts +124 -0
- package/build-types/hooks/use-entity-record.d.ts.map +1 -0
- package/build-types/hooks/use-entity-records.d.ts +49 -0
- package/build-types/hooks/use-entity-records.d.ts.map +1 -0
- package/build-types/hooks/use-query-select.d.ts +46 -0
- package/build-types/hooks/use-query-select.d.ts.map +1 -0
- package/build-types/hooks/use-resource-permissions.d.ts +99 -0
- package/build-types/hooks/use-resource-permissions.d.ts.map +1 -0
- package/build-types/index.d.ts +131 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/locks/actions.d.ts +7 -0
- package/build-types/locks/actions.d.ts.map +1 -0
- package/build-types/locks/engine.d.ts +5 -0
- package/build-types/locks/engine.d.ts.map +1 -0
- package/build-types/locks/reducer.d.ts +10 -0
- package/build-types/locks/reducer.d.ts.map +1 -0
- package/build-types/locks/selectors.d.ts +5 -0
- package/build-types/locks/selectors.d.ts.map +1 -0
- package/build-types/locks/utils.d.ts +8 -0
- package/build-types/locks/utils.d.ts.map +1 -0
- package/build-types/name.d.ts +8 -0
- package/build-types/name.d.ts.map +1 -0
- package/build-types/queried-data/actions.d.ts +32 -0
- package/build-types/queried-data/actions.d.ts.map +1 -0
- package/build-types/queried-data/get-query-parts.d.ts +60 -0
- package/build-types/queried-data/get-query-parts.d.ts.map +1 -0
- package/build-types/queried-data/index.d.ts +4 -0
- package/build-types/queried-data/index.d.ts.map +1 -0
- package/build-types/queried-data/reducer.d.ts +54 -0
- package/build-types/queried-data/reducer.d.ts.map +1 -0
- package/build-types/queried-data/selectors.d.ts +16 -0
- package/build-types/queried-data/selectors.d.ts.map +1 -0
- package/build-types/reducer.d.ts +156 -0
- package/build-types/reducer.d.ts.map +1 -0
- package/build-types/resolvers.d.ts +75 -0
- package/build-types/resolvers.d.ts.map +1 -0
- package/build-types/selectors.d.ts +524 -0
- package/build-types/selectors.d.ts.map +1 -0
- package/build-types/types.d.ts +4 -0
- package/build-types/types.d.ts.map +1 -0
- package/build-types/utils/conservative-map-item.d.ts +12 -0
- package/build-types/utils/conservative-map-item.d.ts.map +1 -0
- package/build-types/utils/forward-resolver.d.ts +10 -0
- package/build-types/utils/forward-resolver.d.ts.map +1 -0
- package/build-types/utils/get-normalized-comma-separable.d.ts +12 -0
- package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -0
- package/build-types/utils/if-matching-action.d.ts +14 -0
- package/build-types/utils/if-matching-action.d.ts.map +1 -0
- package/build-types/utils/index.d.ts +9 -0
- package/build-types/utils/index.d.ts.map +1 -0
- package/build-types/utils/is-raw-attribute.d.ts +10 -0
- package/build-types/utils/is-raw-attribute.d.ts.map +1 -0
- package/build-types/utils/on-sub-key.d.ts +4 -0
- package/build-types/utils/on-sub-key.d.ts.map +1 -0
- package/build-types/utils/replace-action.d.ts +13 -0
- package/build-types/utils/replace-action.d.ts.map +1 -0
- package/build-types/utils/with-weak-map-cache.d.ts +12 -0
- package/build-types/utils/with-weak-map-cache.d.ts.map +1 -0
- package/package.json +13 -11
- package/src/entities.js +325 -0
- package/src/entity-types/attachment.ts +3 -4
- package/src/entity-types/comment.ts +3 -4
- package/src/entity-types/index.ts +31 -88
- package/src/entity-types/menu-location.ts +3 -4
- package/src/entity-types/nav-menu-item.ts +3 -4
- package/src/entity-types/nav-menu.ts +3 -3
- package/src/entity-types/page.ts +3 -3
- package/src/entity-types/plugin.ts +3 -3
- package/src/entity-types/post.ts +3 -3
- package/src/entity-types/settings.ts +3 -3
- package/src/entity-types/sidebar.ts +3 -4
- package/src/entity-types/taxonomy.ts +3 -4
- package/src/entity-types/theme.ts +7 -3
- package/src/entity-types/type.ts +3 -3
- package/src/entity-types/user.ts +3 -3
- package/src/entity-types/widget-type.ts +3 -4
- package/src/entity-types/widget.ts +3 -3
- package/src/entity-types/wp-template-part.ts +3 -4
- package/src/entity-types/wp-template.ts +3 -4
- package/src/hooks/index.ts +4 -0
- package/src/hooks/test/use-entity-record.js +41 -1
- package/src/hooks/test/use-resource-permissions.js +32 -36
- package/src/hooks/use-entity-record.ts +18 -6
- package/src/hooks/use-entity-records.ts +2 -0
- package/src/hooks/use-query-select.ts +4 -1
- package/src/hooks/use-resource-permissions.ts +84 -20
- package/src/index.js +0 -5
- package/src/resolvers.js +80 -45
- package/src/selectors.ts +202 -341
- package/src/test/resolvers.js +118 -4
- package/tsconfig.json +21 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/build/entity-types/entities.js +0 -6
- package/build/entity-types/entities.js.map +0 -1
- package/build-module/entity-types/entities.js +0 -2
- package/build-module/entity-types/entities.js.map +0 -1
- package/src/entities.ts +0 -548
- package/src/entity-types/entities.ts +0 -130
package/src/entities.ts
DELETED
|
@@ -1,548 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { upperFirst, camelCase, map, find, get, startCase } from 'lodash';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* WordPress dependencies
|
|
8
|
-
*/
|
|
9
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
10
|
-
import { __ } from '@wordpress/i18n';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Internal dependencies
|
|
14
|
-
*/
|
|
15
|
-
import { addEntities } from './actions';
|
|
16
|
-
import type * as Records from './entity-types';
|
|
17
|
-
import type {
|
|
18
|
-
EntityType,
|
|
19
|
-
Context,
|
|
20
|
-
Post,
|
|
21
|
-
Taxonomy,
|
|
22
|
-
Type,
|
|
23
|
-
Updatable,
|
|
24
|
-
} from './entity-types';
|
|
25
|
-
|
|
26
|
-
export const DEFAULT_ENTITY_KEY = 'id';
|
|
27
|
-
|
|
28
|
-
const POST_RAW_ATTRIBUTES = [ 'title', 'excerpt', 'content' ];
|
|
29
|
-
|
|
30
|
-
type AttachmentEntity< C extends Context = Context > = EntityType<
|
|
31
|
-
{
|
|
32
|
-
name: 'media';
|
|
33
|
-
kind: 'root';
|
|
34
|
-
baseURLParams: { context: 'edit' };
|
|
35
|
-
},
|
|
36
|
-
Records.Attachment< C >,
|
|
37
|
-
C
|
|
38
|
-
>;
|
|
39
|
-
|
|
40
|
-
const attachmentConfig: AttachmentEntity[ 'config' ] = {
|
|
41
|
-
name: 'media',
|
|
42
|
-
kind: 'root',
|
|
43
|
-
baseURL: '/wp/v2/media',
|
|
44
|
-
baseURLParams: { context: 'edit' },
|
|
45
|
-
plural: 'mediaItems',
|
|
46
|
-
label: __( 'Media' ),
|
|
47
|
-
rawAttributes: [ 'caption', 'title', 'description' ],
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
type SiteEntity< C extends Context = Context > = EntityType<
|
|
51
|
-
{
|
|
52
|
-
name: 'site';
|
|
53
|
-
kind: 'root';
|
|
54
|
-
},
|
|
55
|
-
Records.Settings< C >,
|
|
56
|
-
C
|
|
57
|
-
>;
|
|
58
|
-
|
|
59
|
-
const siteConfig: SiteEntity[ 'config' ] = {
|
|
60
|
-
label: __( 'Site' ),
|
|
61
|
-
name: 'site',
|
|
62
|
-
kind: 'root',
|
|
63
|
-
baseURL: '/wp/v2/settings',
|
|
64
|
-
getTitle: ( record: Records.Settings< 'edit' > ) => {
|
|
65
|
-
return get( record, [ 'title' ], __( 'Site Title' ) );
|
|
66
|
-
},
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
type PostTypeEntity< C extends Context = Context > = EntityType<
|
|
70
|
-
{
|
|
71
|
-
name: 'postType';
|
|
72
|
-
kind: 'root';
|
|
73
|
-
key: 'slug';
|
|
74
|
-
baseURLParams: { context: 'edit' };
|
|
75
|
-
},
|
|
76
|
-
Records.Type< C >,
|
|
77
|
-
C
|
|
78
|
-
>;
|
|
79
|
-
|
|
80
|
-
const postTypeConfig: PostTypeEntity[ 'config' ] = {
|
|
81
|
-
label: __( 'Post Type' ),
|
|
82
|
-
name: 'postType',
|
|
83
|
-
kind: 'root',
|
|
84
|
-
key: 'slug',
|
|
85
|
-
baseURL: '/wp/v2/types',
|
|
86
|
-
baseURLParams: { context: 'edit' },
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
type TaxonomyEntity< C extends Context = Context > = EntityType<
|
|
90
|
-
{
|
|
91
|
-
name: 'taxonomy';
|
|
92
|
-
kind: 'root';
|
|
93
|
-
key: 'slug';
|
|
94
|
-
baseURLParams: { context: 'edit' };
|
|
95
|
-
},
|
|
96
|
-
Records.Taxonomy< C >,
|
|
97
|
-
C
|
|
98
|
-
>;
|
|
99
|
-
|
|
100
|
-
const taxonomyConfig: TaxonomyEntity[ 'config' ] = {
|
|
101
|
-
name: 'taxonomy',
|
|
102
|
-
kind: 'root',
|
|
103
|
-
key: 'slug',
|
|
104
|
-
baseURL: '/wp/v2/taxonomies',
|
|
105
|
-
baseURLParams: { context: 'edit' },
|
|
106
|
-
plural: 'taxonomies',
|
|
107
|
-
label: __( 'Taxonomy' ),
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
type SidebarEntity< C extends Context = Context > = EntityType<
|
|
111
|
-
{
|
|
112
|
-
name: 'sidebar';
|
|
113
|
-
kind: 'root';
|
|
114
|
-
baseURLParams: { context: 'edit' };
|
|
115
|
-
},
|
|
116
|
-
Records.Sidebar< C >,
|
|
117
|
-
C
|
|
118
|
-
>;
|
|
119
|
-
|
|
120
|
-
const sidebarConfig: SidebarEntity[ 'config' ] = {
|
|
121
|
-
name: 'sidebar',
|
|
122
|
-
kind: 'root',
|
|
123
|
-
baseURL: '/wp/v2/sidebars',
|
|
124
|
-
baseURLParams: { context: 'edit' },
|
|
125
|
-
plural: 'sidebars',
|
|
126
|
-
transientEdits: { blocks: true },
|
|
127
|
-
label: __( 'Widget areas' ),
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
type WidgetEntity< C extends Context = Context > = EntityType<
|
|
131
|
-
{
|
|
132
|
-
name: 'widget';
|
|
133
|
-
kind: 'root';
|
|
134
|
-
baseURLParams: { context: 'edit' };
|
|
135
|
-
},
|
|
136
|
-
Records.Widget< C >,
|
|
137
|
-
C
|
|
138
|
-
>;
|
|
139
|
-
const widgetConfig: WidgetEntity[ 'config' ] = {
|
|
140
|
-
name: 'widget',
|
|
141
|
-
kind: 'root',
|
|
142
|
-
baseURL: '/wp/v2/widgets',
|
|
143
|
-
baseURLParams: { context: 'edit' },
|
|
144
|
-
plural: 'widgets',
|
|
145
|
-
transientEdits: { blocks: true },
|
|
146
|
-
label: __( 'Widgets' ),
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
type WidgetTypeEntity< C extends Context = Context > = EntityType<
|
|
150
|
-
{
|
|
151
|
-
name: 'widgetType';
|
|
152
|
-
kind: 'root';
|
|
153
|
-
baseURLParams: { context: 'edit' };
|
|
154
|
-
},
|
|
155
|
-
Records.WidgetType< C >,
|
|
156
|
-
C
|
|
157
|
-
>;
|
|
158
|
-
const widgetTypeConfig: WidgetTypeEntity[ 'config' ] = {
|
|
159
|
-
name: 'widgetType',
|
|
160
|
-
kind: 'root',
|
|
161
|
-
baseURL: '/wp/v2/widget-types',
|
|
162
|
-
baseURLParams: { context: 'edit' },
|
|
163
|
-
plural: 'widgetTypes',
|
|
164
|
-
label: __( 'Widget types' ),
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
type UserEntity< C extends Context = Context > = EntityType<
|
|
168
|
-
{
|
|
169
|
-
name: 'user';
|
|
170
|
-
kind: 'root';
|
|
171
|
-
baseURLParams: { context: 'edit' };
|
|
172
|
-
},
|
|
173
|
-
Records.User< C >,
|
|
174
|
-
C
|
|
175
|
-
>;
|
|
176
|
-
const userConfig: UserEntity[ 'config' ] = {
|
|
177
|
-
label: __( 'User' ),
|
|
178
|
-
name: 'user',
|
|
179
|
-
kind: 'root',
|
|
180
|
-
baseURL: '/wp/v2/users',
|
|
181
|
-
baseURLParams: { context: 'edit' },
|
|
182
|
-
plural: 'users',
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
type CommentEntity< C extends Context = Context > = EntityType<
|
|
186
|
-
{
|
|
187
|
-
name: 'comment';
|
|
188
|
-
kind: 'root';
|
|
189
|
-
baseURLParams: { context: 'edit' };
|
|
190
|
-
},
|
|
191
|
-
Records.Comment< C >,
|
|
192
|
-
C
|
|
193
|
-
>;
|
|
194
|
-
const commentConfig: CommentEntity[ 'config' ] = {
|
|
195
|
-
name: 'comment',
|
|
196
|
-
kind: 'root',
|
|
197
|
-
baseURL: '/wp/v2/comments',
|
|
198
|
-
baseURLParams: { context: 'edit' },
|
|
199
|
-
plural: 'comments',
|
|
200
|
-
label: __( 'Comment' ),
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
type NavMenuEntity< C extends Context = Context > = EntityType<
|
|
204
|
-
{
|
|
205
|
-
name: 'menu';
|
|
206
|
-
kind: 'root';
|
|
207
|
-
baseURLParams: { context: 'edit' };
|
|
208
|
-
},
|
|
209
|
-
Records.NavMenu< C >,
|
|
210
|
-
C
|
|
211
|
-
>;
|
|
212
|
-
|
|
213
|
-
const menuConfig: NavMenuEntity[ 'config' ] = {
|
|
214
|
-
name: 'menu',
|
|
215
|
-
kind: 'root',
|
|
216
|
-
baseURL: '/wp/v2/menus',
|
|
217
|
-
baseURLParams: { context: 'edit' },
|
|
218
|
-
plural: 'menus',
|
|
219
|
-
label: __( 'Menu' ),
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
type NavMenuItemEntity< C extends Context = Context > = EntityType<
|
|
223
|
-
{
|
|
224
|
-
name: 'menuItem';
|
|
225
|
-
kind: 'root';
|
|
226
|
-
baseURLParams: { context: 'edit' };
|
|
227
|
-
},
|
|
228
|
-
Records.NavMenuItem< C >,
|
|
229
|
-
C
|
|
230
|
-
>;
|
|
231
|
-
|
|
232
|
-
const menuItemConfig: NavMenuItemEntity[ 'config' ] = {
|
|
233
|
-
name: 'menuItem',
|
|
234
|
-
kind: 'root',
|
|
235
|
-
baseURL: '/wp/v2/menu-items',
|
|
236
|
-
baseURLParams: { context: 'edit' },
|
|
237
|
-
plural: 'menuItems',
|
|
238
|
-
label: __( 'Menu Item' ),
|
|
239
|
-
rawAttributes: [ 'title' ],
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
type MenuLocationEntity< C extends Context = Context > = EntityType<
|
|
243
|
-
{
|
|
244
|
-
name: 'menuLocation';
|
|
245
|
-
kind: 'root';
|
|
246
|
-
key: 'name';
|
|
247
|
-
baseURLParams: { context: 'edit' };
|
|
248
|
-
},
|
|
249
|
-
Records.MenuLocation< C >,
|
|
250
|
-
C
|
|
251
|
-
>;
|
|
252
|
-
|
|
253
|
-
const menuLocationConfig: MenuLocationEntity[ 'config' ] = {
|
|
254
|
-
name: 'menuLocation',
|
|
255
|
-
kind: 'root',
|
|
256
|
-
baseURL: '/wp/v2/menu-locations',
|
|
257
|
-
baseURLParams: { context: 'edit' },
|
|
258
|
-
plural: 'menuLocations',
|
|
259
|
-
label: __( 'Menu Location' ),
|
|
260
|
-
key: 'name',
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
const globalStyleConfig = {
|
|
264
|
-
label: __( 'Global Styles' ),
|
|
265
|
-
name: 'globalStyles',
|
|
266
|
-
kind: 'root',
|
|
267
|
-
baseURL: '/wp/v2/global-styles',
|
|
268
|
-
baseURLParams: { context: 'edit' },
|
|
269
|
-
plural: 'globalStylesVariations', // Should be different than name.
|
|
270
|
-
getTitle: ( record ) => record?.title?.rendered || record?.title,
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
type ThemeEntity< C extends Context = Context > = EntityType<
|
|
274
|
-
{
|
|
275
|
-
name: 'theme';
|
|
276
|
-
kind: 'root';
|
|
277
|
-
baseURLParams: { context: 'edit' };
|
|
278
|
-
key: 'stylesheet';
|
|
279
|
-
},
|
|
280
|
-
Records.Theme< C >,
|
|
281
|
-
C
|
|
282
|
-
>;
|
|
283
|
-
|
|
284
|
-
const themeConfig: ThemeEntity[ 'config' ] = {
|
|
285
|
-
label: __( 'Themes' ),
|
|
286
|
-
name: 'theme',
|
|
287
|
-
kind: 'root',
|
|
288
|
-
baseURL: '/wp/v2/themes',
|
|
289
|
-
baseURLParams: { context: 'edit' },
|
|
290
|
-
key: 'stylesheet',
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
type PluginEntity< C extends Context = Context > = EntityType<
|
|
294
|
-
{
|
|
295
|
-
name: 'plugin';
|
|
296
|
-
kind: 'root';
|
|
297
|
-
baseURLParams: { context: 'edit' };
|
|
298
|
-
key: 'plugin';
|
|
299
|
-
},
|
|
300
|
-
Records.Plugin< C >,
|
|
301
|
-
C
|
|
302
|
-
>;
|
|
303
|
-
const pluginConfig: PluginEntity[ 'config' ] = {
|
|
304
|
-
label: __( 'Plugins' ),
|
|
305
|
-
name: 'plugin',
|
|
306
|
-
kind: 'root',
|
|
307
|
-
baseURL: '/wp/v2/plugins',
|
|
308
|
-
baseURLParams: { context: 'edit' },
|
|
309
|
-
key: 'plugin',
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
export const rootEntitiesConfig = [
|
|
313
|
-
{
|
|
314
|
-
label: __( 'Base' ),
|
|
315
|
-
kind: 'root',
|
|
316
|
-
name: '__unstableBase',
|
|
317
|
-
baseURL: '/',
|
|
318
|
-
baseURLParams: {
|
|
319
|
-
_fields: [
|
|
320
|
-
'description',
|
|
321
|
-
'gmt_offset',
|
|
322
|
-
'home',
|
|
323
|
-
'name',
|
|
324
|
-
'site_icon',
|
|
325
|
-
'site_icon_url',
|
|
326
|
-
'site_logo',
|
|
327
|
-
'timezone_string',
|
|
328
|
-
'url',
|
|
329
|
-
].join( ',' ),
|
|
330
|
-
},
|
|
331
|
-
},
|
|
332
|
-
siteConfig,
|
|
333
|
-
postTypeConfig,
|
|
334
|
-
attachmentConfig,
|
|
335
|
-
taxonomyConfig,
|
|
336
|
-
sidebarConfig,
|
|
337
|
-
widgetConfig,
|
|
338
|
-
widgetTypeConfig,
|
|
339
|
-
userConfig,
|
|
340
|
-
commentConfig,
|
|
341
|
-
menuConfig,
|
|
342
|
-
menuItemConfig,
|
|
343
|
-
menuLocationConfig,
|
|
344
|
-
globalStyleConfig,
|
|
345
|
-
themeConfig,
|
|
346
|
-
pluginConfig,
|
|
347
|
-
];
|
|
348
|
-
|
|
349
|
-
type PostTypeConfig = {
|
|
350
|
-
kind: 'postType';
|
|
351
|
-
key: 'id';
|
|
352
|
-
defaultContext: 'edit';
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
type PostEntity< C extends Context = Context > = EntityType<
|
|
356
|
-
PostTypeConfig & { name: 'post' },
|
|
357
|
-
Records.Post< C >,
|
|
358
|
-
C
|
|
359
|
-
>;
|
|
360
|
-
type PageEntity< C extends Context > = EntityType<
|
|
361
|
-
PostTypeConfig & { name: 'page' },
|
|
362
|
-
Records.Page< C >,
|
|
363
|
-
C
|
|
364
|
-
>;
|
|
365
|
-
type WpTemplateEntity< C extends Context > = EntityType<
|
|
366
|
-
PostTypeConfig & { name: 'wp_template' },
|
|
367
|
-
Records.WpTemplate< C >,
|
|
368
|
-
C
|
|
369
|
-
>;
|
|
370
|
-
type WpTemplatePartEntity< C extends Context > = EntityType<
|
|
371
|
-
PostTypeConfig & { name: 'wp_template_part' },
|
|
372
|
-
Records.WpTemplatePart< C >,
|
|
373
|
-
C
|
|
374
|
-
>;
|
|
375
|
-
|
|
376
|
-
export type CoreEntities< C extends Context > =
|
|
377
|
-
| SiteEntity< C >
|
|
378
|
-
| PostTypeEntity< C >
|
|
379
|
-
| AttachmentEntity< C >
|
|
380
|
-
| TaxonomyEntity< C >
|
|
381
|
-
| SidebarEntity< C >
|
|
382
|
-
| WidgetEntity< C >
|
|
383
|
-
| WidgetTypeEntity< C >
|
|
384
|
-
| UserEntity< C >
|
|
385
|
-
| CommentEntity< C >
|
|
386
|
-
| NavMenuEntity< C >
|
|
387
|
-
| NavMenuItemEntity< C >
|
|
388
|
-
| MenuLocationEntity< C >
|
|
389
|
-
| ThemeEntity< C >
|
|
390
|
-
| PluginEntity< C >
|
|
391
|
-
| PostEntity< C >
|
|
392
|
-
| PageEntity< C >
|
|
393
|
-
| WpTemplateEntity< C >
|
|
394
|
-
| WpTemplatePartEntity< C >;
|
|
395
|
-
|
|
396
|
-
export const additionalEntityConfigLoaders = [
|
|
397
|
-
{ kind: 'postType', loadEntities: loadPostTypeEntities },
|
|
398
|
-
{ kind: 'taxonomy', loadEntities: loadTaxonomyEntities },
|
|
399
|
-
];
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Returns a function to be used to retrieve extra edits to apply before persisting a post type.
|
|
403
|
-
*
|
|
404
|
-
* @param {Object} persistedRecord Already persisted Post
|
|
405
|
-
* @param {Object} edits Edits.
|
|
406
|
-
* @return {Object} Updated edits.
|
|
407
|
-
*/
|
|
408
|
-
export const prePersistPostType = ( persistedRecord, edits ) => {
|
|
409
|
-
const newEdits = {} as Partial< Updatable< Post< 'edit' > > >;
|
|
410
|
-
|
|
411
|
-
if ( persistedRecord?.status === 'auto-draft' ) {
|
|
412
|
-
// Saving an auto-draft should create a draft by default.
|
|
413
|
-
if ( ! edits.status && ! newEdits.status ) {
|
|
414
|
-
newEdits.status = 'draft';
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
// Fix the auto-draft default title.
|
|
418
|
-
if (
|
|
419
|
-
( ! edits.title || edits.title === 'Auto Draft' ) &&
|
|
420
|
-
! newEdits.title &&
|
|
421
|
-
( ! persistedRecord?.title ||
|
|
422
|
-
persistedRecord?.title === 'Auto Draft' )
|
|
423
|
-
) {
|
|
424
|
-
newEdits.title = '';
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
return newEdits;
|
|
429
|
-
};
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Returns the list of post type entities.
|
|
433
|
-
*
|
|
434
|
-
* @return {Promise} Entities promise
|
|
435
|
-
*/
|
|
436
|
-
async function loadPostTypeEntities() {
|
|
437
|
-
const postTypes = ( await apiFetch( {
|
|
438
|
-
path: '/wp/v2/types?context=view',
|
|
439
|
-
} ) ) as Record< string, Type< 'view' > >;
|
|
440
|
-
return map( postTypes, ( postType, name ) => {
|
|
441
|
-
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes(
|
|
442
|
-
name
|
|
443
|
-
);
|
|
444
|
-
const namespace = postType?.rest_namespace ?? 'wp/v2';
|
|
445
|
-
return {
|
|
446
|
-
kind: 'postType',
|
|
447
|
-
baseURL: `/${ namespace }/${ postType.rest_base }`,
|
|
448
|
-
baseURLParams: { context: 'edit' },
|
|
449
|
-
name,
|
|
450
|
-
label: postType.name,
|
|
451
|
-
transientEdits: {
|
|
452
|
-
blocks: true,
|
|
453
|
-
selection: true,
|
|
454
|
-
},
|
|
455
|
-
mergedEdits: { meta: true },
|
|
456
|
-
rawAttributes: POST_RAW_ATTRIBUTES,
|
|
457
|
-
getTitle: ( record ) =>
|
|
458
|
-
record?.title?.rendered ||
|
|
459
|
-
record?.title ||
|
|
460
|
-
( isTemplate ? startCase( record.slug ) : String( record.id ) ),
|
|
461
|
-
__unstablePrePersist: isTemplate ? undefined : prePersistPostType,
|
|
462
|
-
__unstable_rest_base: postType.rest_base,
|
|
463
|
-
};
|
|
464
|
-
} );
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
/**
|
|
468
|
-
* Returns the list of the taxonomies entities.
|
|
469
|
-
*
|
|
470
|
-
* @return {Promise} Entities promise
|
|
471
|
-
*/
|
|
472
|
-
async function loadTaxonomyEntities() {
|
|
473
|
-
const taxonomies = ( await apiFetch( {
|
|
474
|
-
path: '/wp/v2/taxonomies?context=view',
|
|
475
|
-
} ) ) as Record< string, Taxonomy< 'view' > >;
|
|
476
|
-
return map( taxonomies, ( taxonomy, name ) => {
|
|
477
|
-
const namespace = taxonomy?.rest_namespace ?? 'wp/v2';
|
|
478
|
-
return {
|
|
479
|
-
kind: 'taxonomy',
|
|
480
|
-
baseURL: `/${ namespace }/${ taxonomy.rest_base }`,
|
|
481
|
-
baseURLParams: { context: 'edit' },
|
|
482
|
-
name,
|
|
483
|
-
label: taxonomy.name,
|
|
484
|
-
};
|
|
485
|
-
} );
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
/**
|
|
489
|
-
* Returns the entity's getter method name given its kind and name.
|
|
490
|
-
*
|
|
491
|
-
* @example
|
|
492
|
-
* ```js
|
|
493
|
-
* const nameSingular = getMethodName( 'root', 'theme', 'get' );
|
|
494
|
-
* // nameSingular is getRootTheme
|
|
495
|
-
*
|
|
496
|
-
* const namePlural = getMethodName( 'root', 'theme', 'set' );
|
|
497
|
-
* // namePlural is setRootThemes
|
|
498
|
-
* ```
|
|
499
|
-
*
|
|
500
|
-
* @param {string} kind Entity kind.
|
|
501
|
-
* @param {string} name Entity name.
|
|
502
|
-
* @param {string} prefix Function prefix.
|
|
503
|
-
* @param {boolean} usePlural Whether to use the plural form or not.
|
|
504
|
-
*
|
|
505
|
-
* @return {string} Method name
|
|
506
|
-
*/
|
|
507
|
-
export const getMethodName = (
|
|
508
|
-
kind,
|
|
509
|
-
name,
|
|
510
|
-
prefix = 'get',
|
|
511
|
-
usePlural = false
|
|
512
|
-
) => {
|
|
513
|
-
const entityConfig = find( rootEntitiesConfig, { kind, name } );
|
|
514
|
-
const kindPrefix = kind === 'root' ? '' : upperFirst( camelCase( kind ) );
|
|
515
|
-
const nameSuffix =
|
|
516
|
-
upperFirst( camelCase( name ) ) + ( usePlural ? 's' : '' );
|
|
517
|
-
const suffix =
|
|
518
|
-
usePlural && 'plural' in entityConfig! && entityConfig?.plural
|
|
519
|
-
? upperFirst( camelCase( entityConfig.plural ) )
|
|
520
|
-
: nameSuffix;
|
|
521
|
-
return `${ prefix }${ kindPrefix }${ suffix }`;
|
|
522
|
-
};
|
|
523
|
-
|
|
524
|
-
/**
|
|
525
|
-
* Loads the kind entities into the store.
|
|
526
|
-
*
|
|
527
|
-
* @param {string} kind Kind
|
|
528
|
-
*
|
|
529
|
-
* @return {(thunkArgs: object) => Promise<Array>} Entities
|
|
530
|
-
*/
|
|
531
|
-
export const getOrLoadEntitiesConfig =
|
|
532
|
-
( kind ) =>
|
|
533
|
-
async ( { select, dispatch } ) => {
|
|
534
|
-
let configs = select.getEntitiesConfig( kind );
|
|
535
|
-
if ( configs && configs.length !== 0 ) {
|
|
536
|
-
return configs;
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
const loader = find( additionalEntityConfigLoaders, { kind } );
|
|
540
|
-
if ( ! loader ) {
|
|
541
|
-
return [];
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
configs = await loader.loadEntities();
|
|
545
|
-
dispatch( addEntities( configs ) );
|
|
546
|
-
|
|
547
|
-
return configs;
|
|
548
|
-
};
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { Context } from './helpers';
|
|
5
|
-
import type { EntityRecord } from './index';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* HTTP Query parameters sent with the API request to fetch the entity records.
|
|
9
|
-
*/
|
|
10
|
-
export type EntityQuery<
|
|
11
|
-
C extends Context,
|
|
12
|
-
Fields extends string[] | undefined = undefined
|
|
13
|
-
> = Record< string, any > & {
|
|
14
|
-
context?: C;
|
|
15
|
-
/**
|
|
16
|
-
* The requested fields. If specified, the REST API will remove from the response
|
|
17
|
-
* any fields not on that list.
|
|
18
|
-
*/
|
|
19
|
-
_fields?: Fields;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
interface Edit {}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Helper type that transforms "raw" entity configuration from entities.ts
|
|
26
|
-
* into a format that makes searching by root and kind easy and extensible.
|
|
27
|
-
*
|
|
28
|
-
* This is the foundation of return type inference in calls such as:
|
|
29
|
-
* `getEntityRecord( "root", "comment", 15 )`.
|
|
30
|
-
*
|
|
31
|
-
* @see EntityRecordOf
|
|
32
|
-
* @see getEntityRecord
|
|
33
|
-
*/
|
|
34
|
-
export type EntityType<
|
|
35
|
-
Config extends Pick< EntityConfig< Record, Ctx >, RequiredConfigKeys >,
|
|
36
|
-
Record extends EntityRecord< Ctx >,
|
|
37
|
-
Ctx extends Context
|
|
38
|
-
> = {
|
|
39
|
-
record: Record;
|
|
40
|
-
config: Omit< EntityConfig< Record, Ctx >, RequiredConfigKeys > & Config;
|
|
41
|
-
key: Config[ 'key' ] extends string ? Config[ 'key' ] : 'id';
|
|
42
|
-
defaultContext: Config[ 'baseURLParams' ] extends {
|
|
43
|
-
context: infer InferredContext;
|
|
44
|
-
}
|
|
45
|
-
? InferredContext
|
|
46
|
-
: 'view';
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
type RequiredConfigKeys = 'name' | 'kind' | 'key' | 'baseURLParams';
|
|
50
|
-
|
|
51
|
-
export interface EntityConfig<
|
|
52
|
-
R extends EntityRecord< C >,
|
|
53
|
-
C extends Context
|
|
54
|
-
> {
|
|
55
|
-
/** Path in WP REST API from which to request records of this entity. */
|
|
56
|
-
baseURL: string;
|
|
57
|
-
|
|
58
|
-
/** Arguments to supply by default to API requests for records of this entity. */
|
|
59
|
-
baseURLParams?: EntityQuery< Context >;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Returns the title for a given record of this entity.
|
|
63
|
-
*
|
|
64
|
-
* Some entities have an associated title, such as the name of a
|
|
65
|
-
* particular template part ("full width") or of a menu ("main nav").
|
|
66
|
-
*/
|
|
67
|
-
getTitle?: ( record: R ) => string;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Indicates an alternate field in record that can be used for identification.
|
|
71
|
-
*
|
|
72
|
-
* e.g. a post has an id but may also be uniquely identified by its `slug`
|
|
73
|
-
*/
|
|
74
|
-
key?: string;
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Collection in which to classify records of this entity.
|
|
78
|
-
*
|
|
79
|
-
* 'root' is a special name given to the core entities provided by the editor.
|
|
80
|
-
*
|
|
81
|
-
* It may be the case that we request an entity record for which we have no
|
|
82
|
-
* valid config in memory. In these cases the editor will look for a loader
|
|
83
|
-
* function to requests more entity configs from the server for the given
|
|
84
|
-
* "kind." This is how WordPress defers loading of template entity configs.
|
|
85
|
-
*/
|
|
86
|
-
kind: string;
|
|
87
|
-
|
|
88
|
-
/** Translated form of human-recognizable name or reference to records of this entity. */
|
|
89
|
-
label: string;
|
|
90
|
-
|
|
91
|
-
mergedEdits?: {
|
|
92
|
-
meta?: boolean;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/** Name given to records of this entity, e.g. 'media', 'postType', 'widget' */
|
|
96
|
-
name: string;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Manually provided plural form of the entity name.
|
|
100
|
-
*
|
|
101
|
-
* When not supplied the editor will attempt to auto-generate a plural form.
|
|
102
|
-
*/
|
|
103
|
-
plural?: string;
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Fields in record of this entity which may appear as a compound object with
|
|
107
|
-
* a source value (`raw`) as well as a processed value (`rendered`).
|
|
108
|
-
*
|
|
109
|
-
* e.g. a post's `content` in the edit context contains the raw value stored
|
|
110
|
-
* in the database as well as the rendered version with shortcodes replaced,
|
|
111
|
-
* content texturized, blocks transformed, etc…
|
|
112
|
-
*/
|
|
113
|
-
rawAttributes?: ( keyof R )[];
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Which transient edit operations records of this entity support.
|
|
117
|
-
*/
|
|
118
|
-
transientEdits?: {
|
|
119
|
-
blocks?: boolean;
|
|
120
|
-
selection?: boolean;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
// Unstable properties
|
|
124
|
-
|
|
125
|
-
/** Returns additional changes before applying edits to a record of this entity. */
|
|
126
|
-
__unstablePrePersist?: ( record: R, edits: Edit[] ) => Edit[];
|
|
127
|
-
|
|
128
|
-
/** Used in `canEdit()` */
|
|
129
|
-
__unstable_rest_base?: string;
|
|
130
|
-
}
|