@wordpress/core-data 4.6.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.
Files changed (98) hide show
  1. package/CHANGELOG.md +11 -1
  2. package/README.md +209 -119
  3. package/build/actions.js.map +1 -1
  4. package/build/batch/create-batch.js +1 -1
  5. package/build/batch/create-batch.js.map +1 -1
  6. package/build/entities.js +49 -35
  7. package/build/entities.js.map +1 -1
  8. package/build/entity-provider.js.map +1 -1
  9. package/build/entity-types/entities.js +6 -0
  10. package/build/entity-types/entities.js.map +1 -0
  11. package/build/hooks/constants.js +0 -2
  12. package/build/hooks/constants.js.map +1 -1
  13. package/build/hooks/index.js +38 -0
  14. package/build/hooks/index.js.map +1 -0
  15. package/build/hooks/use-entity-record.js +22 -8
  16. package/build/hooks/use-entity-record.js.map +1 -1
  17. package/build/hooks/use-entity-records.js +21 -8
  18. package/build/hooks/use-entity-records.js.map +1 -1
  19. package/build/hooks/use-query-select.js.map +1 -1
  20. package/build/index.js +17 -21
  21. package/build/index.js.map +1 -1
  22. package/build/queried-data/selectors.js.map +1 -1
  23. package/build/resolvers.js.map +1 -1
  24. package/build/selectors.js +206 -169
  25. package/build/selectors.js.map +1 -1
  26. package/build/utils/forward-resolver.js.map +1 -1
  27. package/build/utils/on-sub-key.js.map +1 -1
  28. package/build/utils/with-weak-map-cache.js +1 -7
  29. package/build/utils/with-weak-map-cache.js.map +1 -1
  30. package/build-module/actions.js.map +1 -1
  31. package/build-module/batch/create-batch.js +2 -2
  32. package/build-module/batch/create-batch.js.map +1 -1
  33. package/build-module/entities.js +49 -35
  34. package/build-module/entities.js.map +1 -1
  35. package/build-module/entity-provider.js.map +1 -1
  36. package/build-module/entity-types/entities.js +2 -0
  37. package/build-module/entity-types/entities.js.map +1 -0
  38. package/build-module/hooks/constants.js +0 -1
  39. package/build-module/hooks/constants.js.map +1 -1
  40. package/build-module/hooks/index.js +3 -0
  41. package/build-module/hooks/index.js.map +1 -0
  42. package/build-module/hooks/use-entity-record.js +18 -7
  43. package/build-module/hooks/use-entity-record.js.map +1 -1
  44. package/build-module/hooks/use-entity-records.js +17 -7
  45. package/build-module/hooks/use-entity-records.js.map +1 -1
  46. package/build-module/hooks/use-query-select.js.map +1 -1
  47. package/build-module/index.js +2 -3
  48. package/build-module/index.js.map +1 -1
  49. package/build-module/queried-data/selectors.js.map +1 -1
  50. package/build-module/resolvers.js.map +1 -1
  51. package/build-module/selectors.js +203 -166
  52. package/build-module/selectors.js.map +1 -1
  53. package/build-module/utils/forward-resolver.js.map +1 -1
  54. package/build-module/utils/on-sub-key.js.map +1 -1
  55. package/build-module/utils/with-weak-map-cache.js +1 -6
  56. package/build-module/utils/with-weak-map-cache.js.map +1 -1
  57. package/package.json +12 -12
  58. package/src/actions.js +389 -372
  59. package/src/batch/create-batch.js +2 -2
  60. package/src/entities.ts +357 -135
  61. package/src/entity-provider.js +4 -6
  62. package/src/entity-types/attachment.ts +4 -3
  63. package/src/entity-types/comment.ts +4 -3
  64. package/src/entity-types/entities.ts +130 -0
  65. package/src/entity-types/index.ts +115 -20
  66. package/src/entity-types/menu-location.ts +4 -3
  67. package/src/entity-types/nav-menu-item.ts +4 -3
  68. package/src/entity-types/nav-menu.ts +3 -3
  69. package/src/entity-types/page.ts +3 -3
  70. package/src/entity-types/plugin.ts +3 -3
  71. package/src/entity-types/post.ts +3 -3
  72. package/src/entity-types/settings.ts +3 -3
  73. package/src/entity-types/sidebar.ts +4 -3
  74. package/src/entity-types/taxonomy.ts +4 -3
  75. package/src/entity-types/theme.ts +3 -3
  76. package/src/entity-types/type.ts +3 -3
  77. package/src/entity-types/user.ts +3 -3
  78. package/src/entity-types/widget-type.ts +4 -3
  79. package/src/entity-types/widget.ts +3 -3
  80. package/src/entity-types/wp-template-part.ts +4 -3
  81. package/src/entity-types/wp-template.ts +4 -3
  82. package/src/fetch/test/__experimental-fetch-link-suggestions.js +2 -4
  83. package/src/hooks/constants.ts +1 -2
  84. package/src/hooks/index.ts +8 -0
  85. package/src/hooks/test/use-query-select.js +4 -2
  86. package/src/hooks/use-entity-record.ts +31 -9
  87. package/src/hooks/use-entity-records.ts +28 -30
  88. package/src/hooks/use-query-select.ts +26 -24
  89. package/src/index.js +2 -3
  90. package/src/locks/test/selectors.js +2 -1
  91. package/src/queried-data/selectors.js +2 -8
  92. package/src/resolvers.js +344 -325
  93. package/src/selectors.ts +639 -296
  94. package/src/test/resolvers.js +1 -3
  95. package/src/test/selectors.js +1 -2
  96. package/src/utils/forward-resolver.js +6 -5
  97. package/src/utils/on-sub-key.js +20 -20
  98. 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 = ( query ) => async ( { dispatch } ) => {
26
- const path = addQueryArgs(
27
- '/wp/v2/users/?who=authors&per_page=100',
28
- query
29
- );
30
- const users = await apiFetch( { path } );
31
- dispatch.receiveUserQuery( path, users );
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 = () => async ( { dispatch } ) => {
38
- const currentUser = await apiFetch( { path: '/wp/v2/users/me' } );
39
- dispatch.receiveCurrentUser( currentUser );
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 = ( kind, name, key = '', query ) => async ( {
52
- select,
53
- dispatch,
54
- } ) => {
55
- const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
56
- const entityConfig = find( configs, { kind, name } );
57
- if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
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
- // Disable reason: While true that an early return could leave `path`
82
- // unused, it's important that path is derived using the query prior to
83
- // additional query modifications in the condition below, since those
84
- // modifications are relevant to how the data is tracked in state, and not
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
- if ( query !== undefined ) {
97
- query = { ...query, include: [ key ] };
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
- // The resolution cache won't consider query as reusable based on the
100
- // fields, so it's tested here, prior to initiating the REST request,
101
- // and without causing `getEntityRecords` resolution to occur.
102
- const hasRecords = select.hasEntityRecords( kind, name, query );
103
- if ( hasRecords ) {
104
- return;
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
- const record = await apiFetch( { path } );
109
- dispatch.receiveEntityRecords( kind, name, record, query );
110
- } finally {
111
- dispatch.__unstableReleaseStoreLock( lock );
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 = ( kind, name, query = {} ) => async ( {
133
- dispatch,
134
- } ) => {
135
- const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
136
- const entityConfig = find( configs, { kind, name } );
137
- if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
138
- return;
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 path = addQueryArgs( entityConfig.baseURL, {
162
- ...entityConfig.baseURLParams,
163
- ...query,
164
- } );
145
+ const lock = await dispatch.__unstableAcquireStoreLock(
146
+ STORE_NAME,
147
+ [ 'entities', 'records', kind, name ],
148
+ { exclusive: false }
149
+ );
165
150
 
166
- let records = Object.values( await apiFetch( { path } ) );
167
- // If we request fields but the result doesn't contain the fields,
168
- // explicitely set these fields as "undefined"
169
- // that way we consider the query "fullfilled".
170
- if ( query._fields ) {
171
- records = records.map( ( record ) => {
172
- query._fields.split( ',' ).forEach( ( field ) => {
173
- if ( ! record.hasOwnProperty( field ) ) {
174
- record[ field ] = undefined;
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
- return record;
166
+ const path = addQueryArgs( entityConfig.baseURL, {
167
+ ...entityConfig.baseURLParams,
168
+ ...query,
179
169
  } );
180
- }
181
170
 
182
- dispatch.receiveEntityRecords( kind, name, records, query );
183
-
184
- // When requesting all fields, the list of results can be used to
185
- // resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
186
- // See https://github.com/WordPress/gutenberg/pull/26575
187
- if ( ! query?._fields && ! query.context ) {
188
- const key = entityConfig.key || DEFAULT_ENTITY_KEY;
189
- const resolutionsArgs = records
190
- .filter( ( record ) => record[ key ] )
191
- .map( ( record ) => [ kind, name, record[ key ] ] );
192
-
193
- dispatch( {
194
- type: 'START_RESOLUTIONS',
195
- selectorName: 'getEntityRecord',
196
- args: resolutionsArgs,
197
- } );
198
- dispatch( {
199
- type: 'FINISH_RESOLUTIONS',
200
- selectorName: 'getEntityRecord',
201
- args: resolutionsArgs,
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
- } finally {
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 = () => async ( { dispatch, resolveSelect } ) => {
222
- const activeThemes = await resolveSelect.getEntityRecords(
223
- 'root',
224
- 'theme',
225
- { status: 'active' }
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
- dispatch.receiveCurrentTheme( activeThemes[ 0 ] );
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 = ( url ) => async ( { dispatch } ) => {
242
- try {
243
- const embedProxyResponse = await apiFetch( {
244
- path: addQueryArgs( '/oembed/1.0/proxy', { url } ),
245
- } );
246
- dispatch.receiveEmbedPreview( url, embedProxyResponse );
247
- } catch ( error ) {
248
- // Embed API 404s if the URL cannot be embedded, so we have to catch the error from the apiRequest here.
249
- dispatch.receiveEmbedPreview( url, false );
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 = ( action, resource, id ) => async ( { dispatch } ) => {
263
- const methods = {
264
- create: 'POST',
265
- read: 'GET',
266
- update: 'PUT',
267
- delete: 'DELETE',
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
- const method = methods[ action ];
271
- if ( ! method ) {
272
- throw new Error( `'${ action }' is not a valid action.` );
273
- }
286
+ const path = id
287
+ ? `/wp/v2/${ resource }/${ id }`
288
+ : `/wp/v2/${ resource }`;
274
289
 
275
- const path = id ? `/wp/v2/${ resource }/${ id }` : `/wp/v2/${ resource }`;
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
- let response;
278
- try {
279
- response = await apiFetch( {
280
- path,
281
- method: 'OPTIONS',
282
- parse: false,
283
- } );
284
- } catch ( error ) {
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 = ( kind, name, recordId ) => async ( {
308
- dispatch,
309
- } ) => {
310
- const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
311
- const entityConfig = find( configs, { kind, name } );
312
- if ( ! entityConfig ) {
313
- return;
314
- }
315
-
316
- const resource = entityConfig.__unstable_rest_base;
317
- await dispatch( canUser( 'update', resource, recordId ) );
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 = ( postType, postId ) => async ( {
327
- dispatch,
328
- resolveSelect,
329
- } ) => {
330
- const { rest_base: restBase } = await resolveSelect.getPostType( postType );
331
- const autosaves = await apiFetch( {
332
- path: `/wp/v2/${ restBase }/${ postId }/autosaves?context=edit`,
333
- } );
334
-
335
- if ( autosaves && autosaves.length ) {
336
- dispatch.receiveAutosaves( postId, autosaves );
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 = ( postType, postId ) => async ( {
350
- resolveSelect,
351
- } ) => {
352
- await resolveSelect.getAutosaves( postType, postId );
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 = ( link ) => async ( {
361
- dispatch,
362
- resolveSelect,
363
- } ) => {
364
- // Ideally this should be using an apiFetch call
365
- // We could potentially do so by adding a "filter" to the `wp_template` end point.
366
- // Also it seems the returned object is not a regular REST API post type.
367
- let template;
368
- try {
369
- template = await window
370
- .fetch( addQueryArgs( link, { '_wp-find-template': true } ) )
371
- .then( ( res ) => res.json() )
372
- .then( ( { data } ) => data );
373
- } catch ( e ) {
374
- // For non-FSE themes, it is possible that this request returns an error.
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
- if ( record ) {
388
- dispatch.receiveEntityRecords( 'postType', 'wp_template', [ record ], {
389
- 'find-template': link,
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 = () => async ( {
404
- dispatch,
405
- resolveSelect,
406
- } ) => {
407
- const activeThemes = await resolveSelect.getEntityRecords(
408
- 'root',
409
- 'theme',
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 = () => async ( {
430
- resolveSelect,
431
- dispatch,
432
- } ) => {
433
- const currentTheme = await resolveSelect.getCurrentTheme();
434
- const themeGlobalStyles = await apiFetch( {
435
- path: `/wp/v2/global-styles/themes/${ currentTheme.stylesheet }`,
436
- } );
437
- dispatch.__experimentalReceiveThemeBaseGlobalStyles(
438
- currentTheme.stylesheet,
439
- themeGlobalStyles
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 = () => async ( {
444
- resolveSelect,
445
- dispatch,
446
- } ) => {
447
- const currentTheme = await resolveSelect.getCurrentTheme();
448
- const variations = await apiFetch( {
449
- path: `/wp/v2/global-styles/themes/${ currentTheme.stylesheet }/variations`,
450
- } );
451
- dispatch.__experimentalReceiveThemeGlobalStyleVariations(
452
- currentTheme.stylesheet,
453
- variations
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 = () => async ( { dispatch } ) => {
458
- const restPatterns = await apiFetch( {
459
- path: '/wp/v2/block-patterns/patterns',
460
- } );
461
- const patterns = map( restPatterns, ( pattern ) =>
462
- mapKeys( pattern, ( value, key ) => {
463
- switch ( key ) {
464
- case 'block_types':
465
- return 'blockTypes';
466
- case 'viewport_width':
467
- return 'viewportWidth';
468
- default:
469
- return key;
470
- }
471
- } )
472
- );
473
- dispatch( { type: 'RECEIVE_BLOCK_PATTERNS', patterns } );
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 = () => async ( { dispatch } ) => {
477
- const categories = await apiFetch( {
478
- path: '/wp/v2/block-patterns/categories',
479
- } );
480
- dispatch( { type: 'RECEIVE_BLOCK_PATTERN_CATEGORIES', categories } );
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
+ };