@wordpress/core-data 4.8.0 → 4.9.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 +2 -0
- package/README.md +56 -56
- package/build/actions.js.map +1 -1
- package/build/batch/create-batch.js +1 -1
- package/build/batch/create-batch.js.map +1 -1
- package/build/entities.js.map +1 -1
- package/build/entity-provider.js.map +1 -1
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-query-select.js.map +1 -1
- package/build/queried-data/selectors.js.map +1 -1
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +45 -8
- package/build/selectors.js.map +1 -1
- package/build/utils/forward-resolver.js.map +1 -1
- package/build/utils/on-sub-key.js.map +1 -1
- package/build/utils/with-weak-map-cache.js +1 -7
- package/build/utils/with-weak-map-cache.js.map +1 -1
- package/build-module/actions.js.map +1 -1
- package/build-module/batch/create-batch.js +2 -2
- package/build-module/batch/create-batch.js.map +1 -1
- package/build-module/entities.js.map +1 -1
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-query-select.js.map +1 -1
- package/build-module/queried-data/selectors.js.map +1 -1
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +40 -4
- package/build-module/selectors.js.map +1 -1
- package/build-module/utils/forward-resolver.js.map +1 -1
- package/build-module/utils/on-sub-key.js.map +1 -1
- package/build-module/utils/with-weak-map-cache.js +1 -6
- package/build-module/utils/with-weak-map-cache.js.map +1 -1
- package/package.json +11 -11
- package/src/actions.js +389 -372
- package/src/batch/create-batch.js +2 -2
- package/src/entities.ts +16 -17
- package/src/entity-provider.js +4 -6
- package/src/entity-types/attachment.ts +4 -3
- package/src/entity-types/comment.ts +4 -3
- package/src/entity-types/entities.ts +5 -2
- package/src/entity-types/index.ts +114 -20
- package/src/entity-types/menu-location.ts +4 -3
- package/src/entity-types/nav-menu-item.ts +4 -3
- 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 +4 -3
- package/src/entity-types/taxonomy.ts +4 -3
- package/src/entity-types/theme.ts +3 -3
- package/src/entity-types/type.ts +3 -3
- package/src/entity-types/user.ts +3 -3
- package/src/entity-types/widget-type.ts +4 -3
- package/src/entity-types/widget.ts +3 -3
- package/src/entity-types/wp-template-part.ts +4 -3
- package/src/entity-types/wp-template.ts +4 -3
- package/src/fetch/test/__experimental-fetch-link-suggestions.js +2 -4
- package/src/hooks/test/use-query-select.js +4 -2
- package/src/hooks/use-entity-record.ts +0 -1
- package/src/hooks/use-query-select.ts +26 -24
- package/src/locks/test/selectors.js +2 -1
- package/src/queried-data/selectors.js +2 -8
- package/src/resolvers.js +344 -325
- package/src/selectors.ts +347 -194
- package/src/test/resolvers.js +1 -3
- package/src/test/selectors.js +1 -2
- package/src/utils/forward-resolver.js +6 -5
- package/src/utils/on-sub-key.js +20 -20
- package/src/utils/with-weak-map-cache.js +1 -6
package/src/resolvers.js
CHANGED
|
@@ -22,22 +22,26 @@ import { forwardResolver, getNormalizedCommaSeparable } from './utils';
|
|
|
22
22
|
* @param {Object|undefined} query Optional object of query parameters to
|
|
23
23
|
* include with request.
|
|
24
24
|
*/
|
|
25
|
-
export const getAuthors =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
};
|
|
25
|
+
export const getAuthors =
|
|
26
|
+
( query ) =>
|
|
27
|
+
async ( { dispatch } ) => {
|
|
28
|
+
const path = addQueryArgs(
|
|
29
|
+
'/wp/v2/users/?who=authors&per_page=100',
|
|
30
|
+
query
|
|
31
|
+
);
|
|
32
|
+
const users = await apiFetch( { path } );
|
|
33
|
+
dispatch.receiveUserQuery( path, users );
|
|
34
|
+
};
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* Requests the current user from the REST API.
|
|
36
38
|
*/
|
|
37
|
-
export const getCurrentUser =
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
};
|
|
39
|
+
export const getCurrentUser =
|
|
40
|
+
() =>
|
|
41
|
+
async ( { dispatch } ) => {
|
|
42
|
+
const currentUser = await apiFetch( { path: '/wp/v2/users/me' } );
|
|
43
|
+
dispatch.receiveCurrentUser( currentUser );
|
|
44
|
+
};
|
|
41
45
|
|
|
42
46
|
/**
|
|
43
47
|
* Requests an entity's record from the REST API.
|
|
@@ -48,69 +52,69 @@ export const getCurrentUser = () => async ( { dispatch } ) => {
|
|
|
48
52
|
* @param {Object|undefined} query Optional object of query parameters to
|
|
49
53
|
* include with request.
|
|
50
54
|
*/
|
|
51
|
-
export const getEntityRecord =
|
|
52
|
-
|
|
53
|
-
dispatch
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const lock = await dispatch.__unstableAcquireStoreLock(
|
|
62
|
-
STORE_NAME,
|
|
63
|
-
[ 'entities', 'records', kind, name, key ],
|
|
64
|
-
{ exclusive: false }
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
if ( query !== undefined && query._fields ) {
|
|
69
|
-
// If requesting specific fields, items and query association to said
|
|
70
|
-
// records are stored by ID reference. Thus, fields must always include
|
|
71
|
-
// the ID.
|
|
72
|
-
query = {
|
|
73
|
-
...query,
|
|
74
|
-
_fields: uniq( [
|
|
75
|
-
...( getNormalizedCommaSeparable( query._fields ) || [] ),
|
|
76
|
-
entityConfig.key || DEFAULT_ENTITY_KEY,
|
|
77
|
-
] ).join(),
|
|
78
|
-
};
|
|
55
|
+
export const getEntityRecord =
|
|
56
|
+
( kind, name, key = '', query ) =>
|
|
57
|
+
async ( { select, dispatch } ) => {
|
|
58
|
+
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
|
|
59
|
+
const entityConfig = find( configs, { kind, name } );
|
|
60
|
+
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
|
|
61
|
+
return;
|
|
79
62
|
}
|
|
80
63
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
// for how the request is made to the REST API.
|
|
86
|
-
|
|
87
|
-
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
|
|
88
|
-
const path = addQueryArgs(
|
|
89
|
-
entityConfig.baseURL + ( key ? '/' + key : '' ),
|
|
90
|
-
{
|
|
91
|
-
...entityConfig.baseURLParams,
|
|
92
|
-
...query,
|
|
93
|
-
}
|
|
64
|
+
const lock = await dispatch.__unstableAcquireStoreLock(
|
|
65
|
+
STORE_NAME,
|
|
66
|
+
[ 'entities', 'records', kind, name, key ],
|
|
67
|
+
{ exclusive: false }
|
|
94
68
|
);
|
|
95
69
|
|
|
96
|
-
|
|
97
|
-
|
|
70
|
+
try {
|
|
71
|
+
if ( query !== undefined && query._fields ) {
|
|
72
|
+
// If requesting specific fields, items and query association to said
|
|
73
|
+
// records are stored by ID reference. Thus, fields must always include
|
|
74
|
+
// the ID.
|
|
75
|
+
query = {
|
|
76
|
+
...query,
|
|
77
|
+
_fields: uniq( [
|
|
78
|
+
...( getNormalizedCommaSeparable( query._fields ) ||
|
|
79
|
+
[] ),
|
|
80
|
+
entityConfig.key || DEFAULT_ENTITY_KEY,
|
|
81
|
+
] ).join(),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
98
84
|
|
|
99
|
-
//
|
|
100
|
-
//
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
85
|
+
// Disable reason: While true that an early return could leave `path`
|
|
86
|
+
// unused, it's important that path is derived using the query prior to
|
|
87
|
+
// additional query modifications in the condition below, since those
|
|
88
|
+
// modifications are relevant to how the data is tracked in state, and not
|
|
89
|
+
// for how the request is made to the REST API.
|
|
90
|
+
|
|
91
|
+
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
|
|
92
|
+
const path = addQueryArgs(
|
|
93
|
+
entityConfig.baseURL + ( key ? '/' + key : '' ),
|
|
94
|
+
{
|
|
95
|
+
...entityConfig.baseURLParams,
|
|
96
|
+
...query,
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
if ( query !== undefined ) {
|
|
101
|
+
query = { ...query, include: [ key ] };
|
|
102
|
+
|
|
103
|
+
// The resolution cache won't consider query as reusable based on the
|
|
104
|
+
// fields, so it's tested here, prior to initiating the REST request,
|
|
105
|
+
// and without causing `getEntityRecords` resolution to occur.
|
|
106
|
+
const hasRecords = select.hasEntityRecords( kind, name, query );
|
|
107
|
+
if ( hasRecords ) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
105
110
|
}
|
|
106
|
-
}
|
|
107
111
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
};
|
|
112
|
+
const record = await apiFetch( { path } );
|
|
113
|
+
dispatch.receiveEntityRecords( kind, name, record, query );
|
|
114
|
+
} finally {
|
|
115
|
+
dispatch.__unstableReleaseStoreLock( lock );
|
|
116
|
+
}
|
|
117
|
+
};
|
|
114
118
|
|
|
115
119
|
/**
|
|
116
120
|
* Requests an entity's record from the REST API.
|
|
@@ -129,82 +133,83 @@ export const getEditedEntityRecord = forwardResolver( 'getEntityRecord' );
|
|
|
129
133
|
* @param {string} name Entity name.
|
|
130
134
|
* @param {Object?} query Query Object.
|
|
131
135
|
*/
|
|
132
|
-
export const getEntityRecords =
|
|
133
|
-
|
|
134
|
-
} ) => {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const lock = await dispatch.__unstableAcquireStoreLock(
|
|
142
|
-
STORE_NAME,
|
|
143
|
-
[ 'entities', 'records', kind, name ],
|
|
144
|
-
{ exclusive: false }
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
try {
|
|
148
|
-
if ( query._fields ) {
|
|
149
|
-
// If requesting specific fields, items and query association to said
|
|
150
|
-
// records are stored by ID reference. Thus, fields must always include
|
|
151
|
-
// the ID.
|
|
152
|
-
query = {
|
|
153
|
-
...query,
|
|
154
|
-
_fields: uniq( [
|
|
155
|
-
...( getNormalizedCommaSeparable( query._fields ) || [] ),
|
|
156
|
-
entityConfig.key || DEFAULT_ENTITY_KEY,
|
|
157
|
-
] ).join(),
|
|
158
|
-
};
|
|
136
|
+
export const getEntityRecords =
|
|
137
|
+
( kind, name, query = {} ) =>
|
|
138
|
+
async ( { dispatch } ) => {
|
|
139
|
+
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
|
|
140
|
+
const entityConfig = find( configs, { kind, name } );
|
|
141
|
+
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
|
|
142
|
+
return;
|
|
159
143
|
}
|
|
160
144
|
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
145
|
+
const lock = await dispatch.__unstableAcquireStoreLock(
|
|
146
|
+
STORE_NAME,
|
|
147
|
+
[ 'entities', 'records', kind, name ],
|
|
148
|
+
{ exclusive: false }
|
|
149
|
+
);
|
|
165
150
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
151
|
+
try {
|
|
152
|
+
if ( query._fields ) {
|
|
153
|
+
// If requesting specific fields, items and query association to said
|
|
154
|
+
// records are stored by ID reference. Thus, fields must always include
|
|
155
|
+
// the ID.
|
|
156
|
+
query = {
|
|
157
|
+
...query,
|
|
158
|
+
_fields: uniq( [
|
|
159
|
+
...( getNormalizedCommaSeparable( query._fields ) ||
|
|
160
|
+
[] ),
|
|
161
|
+
entityConfig.key || DEFAULT_ENTITY_KEY,
|
|
162
|
+
] ).join(),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
177
165
|
|
|
178
|
-
|
|
166
|
+
const path = addQueryArgs( entityConfig.baseURL, {
|
|
167
|
+
...entityConfig.baseURLParams,
|
|
168
|
+
...query,
|
|
179
169
|
} );
|
|
180
|
-
}
|
|
181
170
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
dispatch(
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
171
|
+
let records = Object.values( await apiFetch( { path } ) );
|
|
172
|
+
// If we request fields but the result doesn't contain the fields,
|
|
173
|
+
// explicitely set these fields as "undefined"
|
|
174
|
+
// that way we consider the query "fullfilled".
|
|
175
|
+
if ( query._fields ) {
|
|
176
|
+
records = records.map( ( record ) => {
|
|
177
|
+
query._fields.split( ',' ).forEach( ( field ) => {
|
|
178
|
+
if ( ! record.hasOwnProperty( field ) ) {
|
|
179
|
+
record[ field ] = undefined;
|
|
180
|
+
}
|
|
181
|
+
} );
|
|
182
|
+
|
|
183
|
+
return record;
|
|
184
|
+
} );
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
dispatch.receiveEntityRecords( kind, name, records, query );
|
|
188
|
+
|
|
189
|
+
// When requesting all fields, the list of results can be used to
|
|
190
|
+
// resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
|
|
191
|
+
// See https://github.com/WordPress/gutenberg/pull/26575
|
|
192
|
+
if ( ! query?._fields && ! query.context ) {
|
|
193
|
+
const key = entityConfig.key || DEFAULT_ENTITY_KEY;
|
|
194
|
+
const resolutionsArgs = records
|
|
195
|
+
.filter( ( record ) => record[ key ] )
|
|
196
|
+
.map( ( record ) => [ kind, name, record[ key ] ] );
|
|
197
|
+
|
|
198
|
+
dispatch( {
|
|
199
|
+
type: 'START_RESOLUTIONS',
|
|
200
|
+
selectorName: 'getEntityRecord',
|
|
201
|
+
args: resolutionsArgs,
|
|
202
|
+
} );
|
|
203
|
+
dispatch( {
|
|
204
|
+
type: 'FINISH_RESOLUTIONS',
|
|
205
|
+
selectorName: 'getEntityRecord',
|
|
206
|
+
args: resolutionsArgs,
|
|
207
|
+
} );
|
|
208
|
+
}
|
|
209
|
+
} finally {
|
|
210
|
+
dispatch.__unstableReleaseStoreLock( lock );
|
|
203
211
|
}
|
|
204
|
-
}
|
|
205
|
-
dispatch.__unstableReleaseStoreLock( lock );
|
|
206
|
-
}
|
|
207
|
-
};
|
|
212
|
+
};
|
|
208
213
|
|
|
209
214
|
getEntityRecords.shouldInvalidate = ( action, kind, name ) => {
|
|
210
215
|
return (
|
|
@@ -218,15 +223,17 @@ getEntityRecords.shouldInvalidate = ( action, kind, name ) => {
|
|
|
218
223
|
/**
|
|
219
224
|
* Requests the current theme.
|
|
220
225
|
*/
|
|
221
|
-
export const getCurrentTheme =
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
226
|
+
export const getCurrentTheme =
|
|
227
|
+
() =>
|
|
228
|
+
async ( { dispatch, resolveSelect } ) => {
|
|
229
|
+
const activeThemes = await resolveSelect.getEntityRecords(
|
|
230
|
+
'root',
|
|
231
|
+
'theme',
|
|
232
|
+
{ status: 'active' }
|
|
233
|
+
);
|
|
227
234
|
|
|
228
|
-
|
|
229
|
-
};
|
|
235
|
+
dispatch.receiveCurrentTheme( activeThemes[ 0 ] );
|
|
236
|
+
};
|
|
230
237
|
|
|
231
238
|
/**
|
|
232
239
|
* Requests theme supports data from the index.
|
|
@@ -238,17 +245,19 @@ export const getThemeSupports = forwardResolver( 'getCurrentTheme' );
|
|
|
238
245
|
*
|
|
239
246
|
* @param {string} url URL to get the preview for.
|
|
240
247
|
*/
|
|
241
|
-
export const getEmbedPreview =
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
248
|
+
export const getEmbedPreview =
|
|
249
|
+
( url ) =>
|
|
250
|
+
async ( { dispatch } ) => {
|
|
251
|
+
try {
|
|
252
|
+
const embedProxyResponse = await apiFetch( {
|
|
253
|
+
path: addQueryArgs( '/oembed/1.0/proxy', { url } ),
|
|
254
|
+
} );
|
|
255
|
+
dispatch.receiveEmbedPreview( url, embedProxyResponse );
|
|
256
|
+
} catch ( error ) {
|
|
257
|
+
// Embed API 404s if the URL cannot be embedded, so we have to catch the error from the apiRequest here.
|
|
258
|
+
dispatch.receiveEmbedPreview( url, false );
|
|
259
|
+
}
|
|
260
|
+
};
|
|
252
261
|
|
|
253
262
|
/**
|
|
254
263
|
* Checks whether the current user can perform the given action on the given
|
|
@@ -259,42 +268,46 @@ export const getEmbedPreview = ( url ) => async ( { dispatch } ) => {
|
|
|
259
268
|
* @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
|
|
260
269
|
* @param {?string} id ID of the rest resource to check.
|
|
261
270
|
*/
|
|
262
|
-
export const canUser =
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
271
|
+
export const canUser =
|
|
272
|
+
( action, resource, id ) =>
|
|
273
|
+
async ( { dispatch } ) => {
|
|
274
|
+
const methods = {
|
|
275
|
+
create: 'POST',
|
|
276
|
+
read: 'GET',
|
|
277
|
+
update: 'PUT',
|
|
278
|
+
delete: 'DELETE',
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const method = methods[ action ];
|
|
282
|
+
if ( ! method ) {
|
|
283
|
+
throw new Error( `'${ action }' is not a valid action.` );
|
|
284
|
+
}
|
|
269
285
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
286
|
+
const path = id
|
|
287
|
+
? `/wp/v2/${ resource }/${ id }`
|
|
288
|
+
: `/wp/v2/${ resource }`;
|
|
274
289
|
|
|
275
|
-
|
|
290
|
+
let response;
|
|
291
|
+
try {
|
|
292
|
+
response = await apiFetch( {
|
|
293
|
+
path,
|
|
294
|
+
method: 'OPTIONS',
|
|
295
|
+
parse: false,
|
|
296
|
+
} );
|
|
297
|
+
} catch ( error ) {
|
|
298
|
+
// Do nothing if our OPTIONS request comes back with an API error (4xx or
|
|
299
|
+
// 5xx). The previously determined isAllowed value will remain in the store.
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
276
302
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
response
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
// Do nothing if our OPTIONS request comes back with an API error (4xx or
|
|
286
|
-
// 5xx). The previously determined isAllowed value will remain in the store.
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// Optional chaining operator is used here because the API requests don't
|
|
291
|
-
// return the expected result in the native version. Instead, API requests
|
|
292
|
-
// only return the result, without including response properties like the headers.
|
|
293
|
-
const allowHeader = response.headers?.get( 'allow' );
|
|
294
|
-
const key = compact( [ action, resource, id ] ).join( '/' );
|
|
295
|
-
const isAllowed = includes( allowHeader, method );
|
|
296
|
-
dispatch.receiveUserPermission( key, isAllowed );
|
|
297
|
-
};
|
|
303
|
+
// Optional chaining operator is used here because the API requests don't
|
|
304
|
+
// return the expected result in the native version. Instead, API requests
|
|
305
|
+
// only return the result, without including response properties like the headers.
|
|
306
|
+
const allowHeader = response.headers?.get( 'allow' );
|
|
307
|
+
const key = compact( [ action, resource, id ] ).join( '/' );
|
|
308
|
+
const isAllowed = includes( allowHeader, method );
|
|
309
|
+
dispatch.receiveUserPermission( key, isAllowed );
|
|
310
|
+
};
|
|
298
311
|
|
|
299
312
|
/**
|
|
300
313
|
* Checks whether the current user can perform the given action on the given
|
|
@@ -304,18 +317,18 @@ export const canUser = ( action, resource, id ) => async ( { dispatch } ) => {
|
|
|
304
317
|
* @param {string} name Entity name.
|
|
305
318
|
* @param {string} recordId Record's id.
|
|
306
319
|
*/
|
|
307
|
-
export const canUserEditEntityRecord =
|
|
308
|
-
|
|
309
|
-
} ) => {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
};
|
|
320
|
+
export const canUserEditEntityRecord =
|
|
321
|
+
( kind, name, recordId ) =>
|
|
322
|
+
async ( { dispatch } ) => {
|
|
323
|
+
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
|
|
324
|
+
const entityConfig = find( configs, { kind, name } );
|
|
325
|
+
if ( ! entityConfig ) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const resource = entityConfig.__unstable_rest_base;
|
|
330
|
+
await dispatch( canUser( 'update', resource, recordId ) );
|
|
331
|
+
};
|
|
319
332
|
|
|
320
333
|
/**
|
|
321
334
|
* Request autosave data from the REST API.
|
|
@@ -323,19 +336,20 @@ export const canUserEditEntityRecord = ( kind, name, recordId ) => async ( {
|
|
|
323
336
|
* @param {string} postType The type of the parent post.
|
|
324
337
|
* @param {number} postId The id of the parent post.
|
|
325
338
|
*/
|
|
326
|
-
export const getAutosaves =
|
|
327
|
-
|
|
328
|
-
resolveSelect
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
+
export const getAutosaves =
|
|
340
|
+
( postType, postId ) =>
|
|
341
|
+
async ( { dispatch, resolveSelect } ) => {
|
|
342
|
+
const { rest_base: restBase } = await resolveSelect.getPostType(
|
|
343
|
+
postType
|
|
344
|
+
);
|
|
345
|
+
const autosaves = await apiFetch( {
|
|
346
|
+
path: `/wp/v2/${ restBase }/${ postId }/autosaves?context=edit`,
|
|
347
|
+
} );
|
|
348
|
+
|
|
349
|
+
if ( autosaves && autosaves.length ) {
|
|
350
|
+
dispatch.receiveAutosaves( postId, autosaves );
|
|
351
|
+
}
|
|
352
|
+
};
|
|
339
353
|
|
|
340
354
|
/**
|
|
341
355
|
* Request autosave data from the REST API.
|
|
@@ -346,50 +360,54 @@ export const getAutosaves = ( postType, postId ) => async ( {
|
|
|
346
360
|
* @param {string} postType The type of the parent post.
|
|
347
361
|
* @param {number} postId The id of the parent post.
|
|
348
362
|
*/
|
|
349
|
-
export const getAutosave =
|
|
350
|
-
|
|
351
|
-
} ) => {
|
|
352
|
-
|
|
353
|
-
};
|
|
363
|
+
export const getAutosave =
|
|
364
|
+
( postType, postId ) =>
|
|
365
|
+
async ( { resolveSelect } ) => {
|
|
366
|
+
await resolveSelect.getAutosaves( postType, postId );
|
|
367
|
+
};
|
|
354
368
|
|
|
355
369
|
/**
|
|
356
370
|
* Retrieve the frontend template used for a given link.
|
|
357
371
|
*
|
|
358
372
|
* @param {string} link Link.
|
|
359
373
|
*/
|
|
360
|
-
export const __experimentalGetTemplateForLink =
|
|
361
|
-
|
|
362
|
-
resolveSelect
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
if ( ! template ) {
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
const record = await resolveSelect.getEntityRecord(
|
|
382
|
-
'postType',
|
|
383
|
-
'wp_template',
|
|
384
|
-
template.id
|
|
385
|
-
);
|
|
374
|
+
export const __experimentalGetTemplateForLink =
|
|
375
|
+
( link ) =>
|
|
376
|
+
async ( { dispatch, resolveSelect } ) => {
|
|
377
|
+
// Ideally this should be using an apiFetch call
|
|
378
|
+
// We could potentially do so by adding a "filter" to the `wp_template` end point.
|
|
379
|
+
// Also it seems the returned object is not a regular REST API post type.
|
|
380
|
+
let template;
|
|
381
|
+
try {
|
|
382
|
+
template = await window
|
|
383
|
+
.fetch( addQueryArgs( link, { '_wp-find-template': true } ) )
|
|
384
|
+
.then( ( res ) => res.json() )
|
|
385
|
+
.then( ( { data } ) => data );
|
|
386
|
+
} catch ( e ) {
|
|
387
|
+
// For non-FSE themes, it is possible that this request returns an error.
|
|
388
|
+
}
|
|
386
389
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
390
|
+
if ( ! template ) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const record = await resolveSelect.getEntityRecord(
|
|
395
|
+
'postType',
|
|
396
|
+
'wp_template',
|
|
397
|
+
template.id
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
if ( record ) {
|
|
401
|
+
dispatch.receiveEntityRecords(
|
|
402
|
+
'postType',
|
|
403
|
+
'wp_template',
|
|
404
|
+
[ record ],
|
|
405
|
+
{
|
|
406
|
+
'find-template': link,
|
|
407
|
+
}
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
};
|
|
393
411
|
|
|
394
412
|
__experimentalGetTemplateForLink.shouldInvalidate = ( action ) => {
|
|
395
413
|
return (
|
|
@@ -400,82 +418,83 @@ __experimentalGetTemplateForLink.shouldInvalidate = ( action ) => {
|
|
|
400
418
|
);
|
|
401
419
|
};
|
|
402
420
|
|
|
403
|
-
export const __experimentalGetCurrentGlobalStylesId =
|
|
404
|
-
|
|
405
|
-
resolveSelect
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
{ status: 'active' }
|
|
411
|
-
);
|
|
412
|
-
const globalStylesURL = get( activeThemes, [
|
|
413
|
-
0,
|
|
414
|
-
'_links',
|
|
415
|
-
'wp:user-global-styles',
|
|
416
|
-
0,
|
|
417
|
-
'href',
|
|
418
|
-
] );
|
|
419
|
-
if ( globalStylesURL ) {
|
|
420
|
-
const globalStylesObject = await apiFetch( {
|
|
421
|
-
url: globalStylesURL,
|
|
422
|
-
} );
|
|
423
|
-
dispatch.__experimentalReceiveCurrentGlobalStylesId(
|
|
424
|
-
globalStylesObject.id
|
|
421
|
+
export const __experimentalGetCurrentGlobalStylesId =
|
|
422
|
+
() =>
|
|
423
|
+
async ( { dispatch, resolveSelect } ) => {
|
|
424
|
+
const activeThemes = await resolveSelect.getEntityRecords(
|
|
425
|
+
'root',
|
|
426
|
+
'theme',
|
|
427
|
+
{ status: 'active' }
|
|
425
428
|
);
|
|
426
|
-
|
|
427
|
-
|
|
429
|
+
const globalStylesURL = get( activeThemes, [
|
|
430
|
+
0,
|
|
431
|
+
'_links',
|
|
432
|
+
'wp:user-global-styles',
|
|
433
|
+
0,
|
|
434
|
+
'href',
|
|
435
|
+
] );
|
|
436
|
+
if ( globalStylesURL ) {
|
|
437
|
+
const globalStylesObject = await apiFetch( {
|
|
438
|
+
url: globalStylesURL,
|
|
439
|
+
} );
|
|
440
|
+
dispatch.__experimentalReceiveCurrentGlobalStylesId(
|
|
441
|
+
globalStylesObject.id
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
428
445
|
|
|
429
|
-
export const __experimentalGetCurrentThemeBaseGlobalStyles =
|
|
430
|
-
|
|
431
|
-
dispatch
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
};
|
|
446
|
+
export const __experimentalGetCurrentThemeBaseGlobalStyles =
|
|
447
|
+
() =>
|
|
448
|
+
async ( { resolveSelect, dispatch } ) => {
|
|
449
|
+
const currentTheme = await resolveSelect.getCurrentTheme();
|
|
450
|
+
const themeGlobalStyles = await apiFetch( {
|
|
451
|
+
path: `/wp/v2/global-styles/themes/${ currentTheme.stylesheet }`,
|
|
452
|
+
} );
|
|
453
|
+
dispatch.__experimentalReceiveThemeBaseGlobalStyles(
|
|
454
|
+
currentTheme.stylesheet,
|
|
455
|
+
themeGlobalStyles
|
|
456
|
+
);
|
|
457
|
+
};
|
|
442
458
|
|
|
443
|
-
export const __experimentalGetCurrentThemeGlobalStylesVariations =
|
|
444
|
-
|
|
445
|
-
dispatch
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
};
|
|
459
|
+
export const __experimentalGetCurrentThemeGlobalStylesVariations =
|
|
460
|
+
() =>
|
|
461
|
+
async ( { resolveSelect, dispatch } ) => {
|
|
462
|
+
const currentTheme = await resolveSelect.getCurrentTheme();
|
|
463
|
+
const variations = await apiFetch( {
|
|
464
|
+
path: `/wp/v2/global-styles/themes/${ currentTheme.stylesheet }/variations`,
|
|
465
|
+
} );
|
|
466
|
+
dispatch.__experimentalReceiveThemeGlobalStyleVariations(
|
|
467
|
+
currentTheme.stylesheet,
|
|
468
|
+
variations
|
|
469
|
+
);
|
|
470
|
+
};
|
|
456
471
|
|
|
457
|
-
export const getBlockPatterns =
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
472
|
+
export const getBlockPatterns =
|
|
473
|
+
() =>
|
|
474
|
+
async ( { dispatch } ) => {
|
|
475
|
+
const restPatterns = await apiFetch( {
|
|
476
|
+
path: '/wp/v2/block-patterns/patterns',
|
|
477
|
+
} );
|
|
478
|
+
const patterns = map( restPatterns, ( pattern ) =>
|
|
479
|
+
mapKeys( pattern, ( value, key ) => {
|
|
480
|
+
switch ( key ) {
|
|
481
|
+
case 'block_types':
|
|
482
|
+
return 'blockTypes';
|
|
483
|
+
case 'viewport_width':
|
|
484
|
+
return 'viewportWidth';
|
|
485
|
+
default:
|
|
486
|
+
return key;
|
|
487
|
+
}
|
|
488
|
+
} )
|
|
489
|
+
);
|
|
490
|
+
dispatch( { type: 'RECEIVE_BLOCK_PATTERNS', patterns } );
|
|
491
|
+
};
|
|
475
492
|
|
|
476
|
-
export const getBlockPatternCategories =
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
};
|
|
493
|
+
export const getBlockPatternCategories =
|
|
494
|
+
() =>
|
|
495
|
+
async ( { dispatch } ) => {
|
|
496
|
+
const categories = await apiFetch( {
|
|
497
|
+
path: '/wp/v2/block-patterns/categories',
|
|
498
|
+
} );
|
|
499
|
+
dispatch( { type: 'RECEIVE_BLOCK_PATTERN_CATEGORIES', categories } );
|
|
500
|
+
};
|