@wordpress/core-data 7.3.0 → 7.5.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 (153) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +54 -6
  3. package/build/actions.js +1 -1
  4. package/build/actions.js.map +1 -1
  5. package/build/entity-context.js +13 -0
  6. package/build/entity-context.js.map +1 -0
  7. package/build/entity-provider.js +4 -189
  8. package/build/entity-provider.js.map +1 -1
  9. package/build/footnotes/get-rich-text-values-cached.js +2 -2
  10. package/build/footnotes/get-rich-text-values-cached.js.map +1 -1
  11. package/build/hooks/index.js +22 -0
  12. package/build/hooks/index.js.map +1 -1
  13. package/build/hooks/use-entity-block-editor.js +140 -0
  14. package/build/hooks/use-entity-block-editor.js.map +1 -0
  15. package/build/hooks/use-entity-id.js +28 -0
  16. package/build/hooks/use-entity-id.js.map +1 -0
  17. package/build/hooks/use-entity-prop.js +65 -0
  18. package/build/hooks/use-entity-prop.js.map +1 -0
  19. package/build/hooks/use-entity-records.js +39 -0
  20. package/build/hooks/use-entity-records.js.map +1 -1
  21. package/build/hooks/use-resource-permissions.js +25 -8
  22. package/build/hooks/use-resource-permissions.js.map +1 -1
  23. package/build/index.js +14 -2
  24. package/build/index.js.map +1 -1
  25. package/build/lock-unlock.js +18 -0
  26. package/build/lock-unlock.js.map +1 -0
  27. package/build/private-apis.js +8 -9
  28. package/build/private-apis.js.map +1 -1
  29. package/build/private-selectors.js +35 -0
  30. package/build/private-selectors.js.map +1 -1
  31. package/build/resolvers.js +64 -60
  32. package/build/resolvers.js.map +1 -1
  33. package/build/selectors.js +16 -8
  34. package/build/selectors.js.map +1 -1
  35. package/build/utils/index.js +19 -0
  36. package/build/utils/index.js.map +1 -1
  37. package/build/utils/user-permissions.js +32 -0
  38. package/build/utils/user-permissions.js.map +1 -0
  39. package/build-module/actions.js +1 -1
  40. package/build-module/actions.js.map +1 -1
  41. package/build-module/entity-context.js +6 -0
  42. package/build-module/entity-context.js.map +1 -0
  43. package/build-module/entity-provider.js +3 -183
  44. package/build-module/entity-provider.js.map +1 -1
  45. package/build-module/footnotes/get-rich-text-values-cached.js +1 -1
  46. package/build-module/footnotes/get-rich-text-values-cached.js.map +1 -1
  47. package/build-module/hooks/index.js +3 -0
  48. package/build-module/hooks/index.js.map +1 -1
  49. package/build-module/hooks/use-entity-block-editor.js +132 -0
  50. package/build-module/hooks/use-entity-block-editor.js.map +1 -0
  51. package/build-module/hooks/use-entity-id.js +22 -0
  52. package/build-module/hooks/use-entity-id.js.map +1 -0
  53. package/build-module/hooks/use-entity-prop.js +58 -0
  54. package/build-module/hooks/use-entity-prop.js.map +1 -0
  55. package/build-module/hooks/use-entity-records.js +38 -0
  56. package/build-module/hooks/use-entity-records.js.map +1 -1
  57. package/build-module/hooks/use-resource-permissions.js +25 -8
  58. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  59. package/build-module/index.js +2 -1
  60. package/build-module/index.js.map +1 -1
  61. package/build-module/lock-unlock.js +9 -0
  62. package/build-module/lock-unlock.js.map +1 -0
  63. package/build-module/private-apis.js +7 -6
  64. package/build-module/private-apis.js.map +1 -1
  65. package/build-module/private-selectors.js +33 -0
  66. package/build-module/private-selectors.js.map +1 -1
  67. package/build-module/resolvers.js +65 -61
  68. package/build-module/resolvers.js.map +1 -1
  69. package/build-module/selectors.js +17 -9
  70. package/build-module/selectors.js.map +1 -1
  71. package/build-module/utils/index.js +1 -0
  72. package/build-module/utils/index.js.map +1 -1
  73. package/build-module/utils/user-permissions.js +24 -0
  74. package/build-module/utils/user-permissions.js.map +1 -0
  75. package/build-types/actions.d.ts +3 -3
  76. package/build-types/actions.d.ts.map +1 -1
  77. package/build-types/batch/create-batch.d.ts.map +1 -1
  78. package/build-types/entities.d.ts.map +1 -1
  79. package/build-types/entity-context.d.ts +2 -0
  80. package/build-types/entity-context.d.ts.map +1 -0
  81. package/build-types/entity-provider.d.ts +0 -47
  82. package/build-types/entity-provider.d.ts.map +1 -1
  83. package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -1
  84. package/build-types/hooks/index.d.ts +3 -0
  85. package/build-types/hooks/index.d.ts.map +1 -1
  86. package/build-types/hooks/use-entity-block-editor.d.ts +22 -0
  87. package/build-types/hooks/use-entity-block-editor.d.ts.map +1 -0
  88. package/build-types/hooks/use-entity-id.d.ts +9 -0
  89. package/build-types/hooks/use-entity-id.d.ts.map +1 -0
  90. package/build-types/hooks/use-entity-prop.d.ts +19 -0
  91. package/build-types/hooks/use-entity-prop.d.ts.map +1 -0
  92. package/build-types/hooks/use-entity-records.d.ts +1 -0
  93. package/build-types/hooks/use-entity-records.d.ts.map +1 -1
  94. package/build-types/hooks/use-resource-permissions.d.ts +8 -70
  95. package/build-types/hooks/use-resource-permissions.d.ts.map +1 -1
  96. package/build-types/index.d.ts +36 -32
  97. package/build-types/index.d.ts.map +1 -1
  98. package/build-types/lock-unlock.d.ts +3 -0
  99. package/build-types/lock-unlock.d.ts.map +1 -0
  100. package/build-types/locks/reducer.d.ts +1 -1
  101. package/build-types/locks/reducer.d.ts.map +1 -1
  102. package/build-types/private-apis.d.ts +1 -2
  103. package/build-types/private-apis.d.ts.map +1 -1
  104. package/build-types/private-selectors.d.ts +15 -0
  105. package/build-types/private-selectors.d.ts.map +1 -1
  106. package/build-types/queried-data/actions.d.ts +1 -1
  107. package/build-types/queried-data/actions.d.ts.map +1 -1
  108. package/build-types/queried-data/get-query-parts.d.ts.map +1 -1
  109. package/build-types/queried-data/reducer.d.ts +1 -1
  110. package/build-types/queried-data/reducer.d.ts.map +1 -1
  111. package/build-types/queried-data/selectors.d.ts +0 -1
  112. package/build-types/queried-data/selectors.d.ts.map +1 -1
  113. package/build-types/reducer.d.ts +13 -13
  114. package/build-types/reducer.d.ts.map +1 -1
  115. package/build-types/resolvers.d.ts +3 -2
  116. package/build-types/resolvers.d.ts.map +1 -1
  117. package/build-types/selectors.d.ts +11 -6
  118. package/build-types/selectors.d.ts.map +1 -1
  119. package/build-types/utils/get-nested-value.d.ts.map +1 -1
  120. package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -1
  121. package/build-types/utils/if-matching-action.d.ts +1 -1
  122. package/build-types/utils/index.d.ts +1 -0
  123. package/build-types/utils/on-sub-key.d.ts +1 -1
  124. package/build-types/utils/replace-action.d.ts +1 -1
  125. package/build-types/utils/set-nested-value.d.ts.map +1 -1
  126. package/build-types/utils/user-permissions.d.ts +4 -0
  127. package/build-types/utils/user-permissions.d.ts.map +1 -0
  128. package/package.json +18 -17
  129. package/src/actions.js +1 -1
  130. package/src/entity-context.js +6 -0
  131. package/src/entity-provider.js +2 -209
  132. package/src/footnotes/get-rich-text-values-cached.js +1 -1
  133. package/src/hooks/index.ts +3 -0
  134. package/src/hooks/test/use-entity-record.js +5 -3
  135. package/src/hooks/test/use-resource-permissions.js +96 -5
  136. package/src/hooks/use-entity-block-editor.js +148 -0
  137. package/src/hooks/use-entity-id.js +21 -0
  138. package/src/hooks/use-entity-prop.js +60 -0
  139. package/src/hooks/use-entity-records.ts +50 -0
  140. package/src/hooks/use-resource-permissions.ts +46 -9
  141. package/src/index.js +2 -1
  142. package/src/lock-unlock.js +10 -0
  143. package/src/private-apis.js +7 -7
  144. package/src/private-selectors.ts +43 -0
  145. package/src/resolvers.js +85 -67
  146. package/src/selectors.ts +18 -9
  147. package/src/test/entity-provider.js +6 -2
  148. package/src/test/resolvers.js +217 -50
  149. package/src/test/selectors.js +18 -55
  150. package/src/utils/index.js +5 -0
  151. package/src/utils/user-permissions.js +39 -0
  152. package/tsconfig.json +2 -1
  153. package/tsconfig.tsbuildinfo +1 -1
package/src/resolvers.js CHANGED
@@ -15,7 +15,13 @@ import apiFetch from '@wordpress/api-fetch';
15
15
  */
16
16
  import { STORE_NAME } from './name';
17
17
  import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
18
- import { forwardResolver, getNormalizedCommaSeparable } from './utils';
18
+ import {
19
+ forwardResolver,
20
+ getNormalizedCommaSeparable,
21
+ getUserPermissionCacheKey,
22
+ getUserPermissionsFromResponse,
23
+ ALLOWED_RESOURCE_ACTIONS,
24
+ } from './utils';
19
25
  import { getSyncProvider } from './sync';
20
26
  import { fetchBlockPatterns } from './fetch';
21
27
 
@@ -58,7 +64,7 @@ export const getCurrentUser =
58
64
  */
59
65
  export const getEntityRecord =
60
66
  ( kind, name, key = '', query ) =>
61
- async ( { select, dispatch } ) => {
67
+ async ( { select, dispatch, registry } ) => {
62
68
  const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
63
69
  const entityConfig = configs.find(
64
70
  ( config ) => config.name === name && config.kind === kind
@@ -165,8 +171,29 @@ export const getEntityRecord =
165
171
  }
166
172
  }
167
173
 
168
- const record = await apiFetch( { path } );
169
- dispatch.receiveEntityRecords( kind, name, record, query );
174
+ const response = await apiFetch( { path, parse: false } );
175
+ const record = await response.json();
176
+ const permissions = getUserPermissionsFromResponse( response );
177
+
178
+ registry.batch( () => {
179
+ dispatch.receiveEntityRecords( kind, name, record, query );
180
+
181
+ for ( const action of ALLOWED_RESOURCE_ACTIONS ) {
182
+ const permissionKey = getUserPermissionCacheKey(
183
+ action,
184
+ { kind, name, id: key }
185
+ );
186
+
187
+ dispatch.receiveUserPermission(
188
+ permissionKey,
189
+ permissions[ action ]
190
+ );
191
+ dispatch.finishResolution( 'canUser', [
192
+ action,
193
+ { kind, name, id: key },
194
+ ] );
195
+ }
196
+ } );
170
197
  }
171
198
  } finally {
172
199
  dispatch.__unstableReleaseStoreLock( lock );
@@ -281,16 +308,10 @@ export const getEntityRecords =
281
308
  .filter( ( record ) => record?.[ key ] )
282
309
  .map( ( record ) => [ kind, name, record[ key ] ] );
283
310
 
284
- dispatch( {
285
- type: 'START_RESOLUTIONS',
286
- selectorName: 'getEntityRecord',
287
- args: resolutionsArgs,
288
- } );
289
- dispatch( {
290
- type: 'FINISH_RESOLUTIONS',
291
- selectorName: 'getEntityRecord',
292
- args: resolutionsArgs,
293
- } );
311
+ dispatch.finishResolutions(
312
+ 'getEntityRecord',
313
+ resolutionsArgs
314
+ );
294
315
  }
295
316
 
296
317
  dispatch.__unstableReleaseStoreLock( lock );
@@ -352,25 +373,47 @@ export const getEmbedPreview =
352
373
  * Checks whether the current user can perform the given action on the given
353
374
  * REST resource.
354
375
  *
355
- * @param {string} requestedAction Action to check. One of: 'create', 'read', 'update',
356
- * 'delete'.
357
- * @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
358
- * @param {?string} id ID of the rest resource to check.
376
+ * @param {string} requestedAction Action to check. One of: 'create', 'read', 'update',
377
+ * 'delete'.
378
+ * @param {string|Object} resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`
379
+ * or REST base as a string - `media`.
380
+ * @param {?string} id ID of the rest resource to check.
359
381
  */
360
382
  export const canUser =
361
383
  ( requestedAction, resource, id ) =>
362
384
  async ( { dispatch, registry } ) => {
363
- const { hasStartedResolution } = registry.select( STORE_NAME );
385
+ if ( ! ALLOWED_RESOURCE_ACTIONS.includes( requestedAction ) ) {
386
+ throw new Error( `'${ requestedAction }' is not a valid action.` );
387
+ }
364
388
 
365
- const resourcePath = id ? `${ resource }/${ id }` : resource;
366
- const retrievedActions = [ 'create', 'read', 'update', 'delete' ];
389
+ let resourcePath = null;
390
+ if ( typeof resource === 'object' ) {
391
+ if ( ! resource.kind || ! resource.name ) {
392
+ throw new Error( 'The entity resource object is not valid.' );
393
+ }
367
394
 
368
- if ( ! retrievedActions.includes( requestedAction ) ) {
369
- throw new Error( `'${ requestedAction }' is not a valid action.` );
395
+ const configs = await dispatch(
396
+ getOrLoadEntitiesConfig( resource.kind, resource.name )
397
+ );
398
+ const entityConfig = configs.find(
399
+ ( config ) =>
400
+ config.name === resource.name &&
401
+ config.kind === resource.kind
402
+ );
403
+ if ( ! entityConfig ) {
404
+ return;
405
+ }
406
+
407
+ resourcePath =
408
+ entityConfig.baseURL + ( resource.id ? '/' + resource.id : '' );
409
+ } else {
410
+ resourcePath = `/wp/v2/${ resource }` + ( id ? '/' + id : '' );
370
411
  }
371
412
 
413
+ const { hasStartedResolution } = registry.select( STORE_NAME );
414
+
372
415
  // Prevent resolving the same resource twice.
373
- for ( const relatedAction of retrievedActions ) {
416
+ for ( const relatedAction of ALLOWED_RESOURCE_ACTIONS ) {
374
417
  if ( relatedAction === requestedAction ) {
375
418
  continue;
376
419
  }
@@ -387,7 +430,7 @@ export const canUser =
387
430
  let response;
388
431
  try {
389
432
  response = await apiFetch( {
390
- path: `/wp/v2/${ resourcePath }`,
433
+ path: resourcePath,
391
434
  method: 'OPTIONS',
392
435
  parse: false,
393
436
  } );
@@ -397,29 +440,21 @@ export const canUser =
397
440
  return;
398
441
  }
399
442
 
400
- // Optional chaining operator is used here because the API requests don't
401
- // return the expected result in the native version. Instead, API requests
402
- // only return the result, without including response properties like the headers.
403
- const allowHeader = response.headers?.get( 'allow' );
404
- const allowedMethods = allowHeader?.allow || allowHeader || '';
405
-
406
- const permissions = {};
407
- const methods = {
408
- create: 'POST',
409
- read: 'GET',
410
- update: 'PUT',
411
- delete: 'DELETE',
412
- };
413
- for ( const [ actionName, methodName ] of Object.entries( methods ) ) {
414
- permissions[ actionName ] = allowedMethods.includes( methodName );
415
- }
416
-
443
+ const permissions = getUserPermissionsFromResponse( response );
417
444
  registry.batch( () => {
418
- for ( const action of retrievedActions ) {
419
- dispatch.receiveUserPermission(
420
- `${ action }/${ resourcePath }`,
421
- permissions[ action ]
422
- );
445
+ for ( const action of ALLOWED_RESOURCE_ACTIONS ) {
446
+ const key = getUserPermissionCacheKey( action, resource, id );
447
+
448
+ dispatch.receiveUserPermission( key, permissions[ action ] );
449
+
450
+ // Mark related action resolutions as finished.
451
+ if ( action !== requestedAction ) {
452
+ dispatch.finishResolution( 'canUser', [
453
+ action,
454
+ resource,
455
+ id,
456
+ ] );
457
+ }
423
458
  }
424
459
  } );
425
460
  };
@@ -435,16 +470,7 @@ export const canUser =
435
470
  export const canUserEditEntityRecord =
436
471
  ( kind, name, recordId ) =>
437
472
  async ( { dispatch } ) => {
438
- const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
439
- const entityConfig = configs.find(
440
- ( config ) => config.name === name && config.kind === kind
441
- );
442
- if ( ! entityConfig ) {
443
- return;
444
- }
445
-
446
- const resource = entityConfig.__unstable_rest_base;
447
- await dispatch( canUser( 'update', resource, recordId ) );
473
+ await dispatch( canUser( 'update', { kind, name, id: recordId } ) );
448
474
  };
449
475
 
450
476
  /**
@@ -826,16 +852,8 @@ export const getRevisions =
826
852
  record[ key ],
827
853
  ] );
828
854
 
829
- dispatch( {
830
- type: 'START_RESOLUTIONS',
831
- selectorName: 'getRevision',
832
- args: resolutionsArgs,
833
- } );
834
- dispatch( {
835
- type: 'FINISH_RESOLUTIONS',
836
- selectorName: 'getRevision',
837
- args: resolutionsArgs,
838
- } );
855
+ dispatch.startResolutions( 'getRevision', resolutionsArgs );
856
+ dispatch.finishResolutions( 'getRevision', resolutionsArgs );
839
857
  }
840
858
  }
841
859
  };
package/src/selectors.ts CHANGED
@@ -20,6 +20,7 @@ import {
20
20
  isRawAttribute,
21
21
  setNestedValue,
22
22
  isNumericID,
23
+ getUserPermissionCacheKey,
23
24
  } from './utils';
24
25
  import type * as ET from './entity-types';
25
26
  import type { UndoManager } from '@wordpress/undo-manager';
@@ -120,6 +121,8 @@ type EntityRecordArgs =
120
121
  | [ string, string, EntityRecordKey ]
121
122
  | [ string, string, EntityRecordKey, GetRecordsHttpQuery ];
122
123
 
124
+ type EntityResource = { kind: string; name: string; id?: EntityRecordKey };
125
+
123
126
  /**
124
127
  * Shared reference to an empty object for cases where it is important to avoid
125
128
  * returning a new object reference on every invocation, as in a connected or
@@ -1136,7 +1139,8 @@ export function isPreviewEmbedFallback( state: State, url: string ): boolean {
1136
1139
  *
1137
1140
  * @param state Data state.
1138
1141
  * @param action Action to check. One of: 'create', 'read', 'update', 'delete'.
1139
- * @param resource REST resource to check, e.g. 'media' or 'posts'.
1142
+ * @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`
1143
+ * or REST base as a string - `media`.
1140
1144
  * @param id Optional ID of the rest resource to check.
1141
1145
  *
1142
1146
  * @return Whether or not the user can perform the action,
@@ -1145,10 +1149,16 @@ export function isPreviewEmbedFallback( state: State, url: string ): boolean {
1145
1149
  export function canUser(
1146
1150
  state: State,
1147
1151
  action: string,
1148
- resource: string,
1152
+ resource: string | EntityResource,
1149
1153
  id?: EntityRecordKey
1150
1154
  ): boolean | undefined {
1151
- const key = [ action, resource, id ].filter( Boolean ).join( '/' );
1155
+ const isEntity = typeof resource === 'object';
1156
+ if ( isEntity && ( ! resource.kind || ! resource.name ) ) {
1157
+ return false;
1158
+ }
1159
+
1160
+ const key = getUserPermissionCacheKey( action, resource, id );
1161
+
1152
1162
  return state.userPermissions[ key ];
1153
1163
  }
1154
1164
 
@@ -1173,13 +1183,12 @@ export function canUserEditEntityRecord(
1173
1183
  name: string,
1174
1184
  recordId: EntityRecordKey
1175
1185
  ): boolean | undefined {
1176
- const entityConfig = getEntityConfig( state, kind, name );
1177
- if ( ! entityConfig ) {
1178
- return false;
1179
- }
1180
- const resource = entityConfig.__unstable_rest_base;
1186
+ deprecated( `wp.data.select( 'core' ).canUserEditEntityRecord()`, {
1187
+ since: '6.7',
1188
+ alternative: `wp.data.select( 'core' ).canUser( 'update', { kind, name, id } )`,
1189
+ } );
1181
1190
 
1182
- return canUser( state, 'update', resource, recordId );
1191
+ return canUser( state, 'update', { kind, name, id: recordId } );
1183
1192
  }
1184
1193
 
1185
1194
  /**
@@ -14,13 +14,14 @@ import {
14
14
  } from '@wordpress/blocks';
15
15
  import { RichText, useBlockProps } from '@wordpress/block-editor';
16
16
  import { createRegistry, RegistryProvider } from '@wordpress/data';
17
- import '@wordpress/block-library';
17
+ import { registerCoreBlocks } from '@wordpress/block-library';
18
+ import { unregisterFormatType } from '@wordpress/rich-text';
18
19
 
19
20
  /**
20
21
  * Internal dependencies
21
22
  */
22
23
  import { store as coreDataStore } from '../index';
23
- import { useEntityBlockEditor } from '../entity-provider';
24
+ import useEntityBlockEditor from '../hooks/use-entity-block-editor';
24
25
 
25
26
  const postTypeConfig = {
26
27
  kind: 'postType',
@@ -137,12 +138,15 @@ describe( 'useEntityBlockEditor', () => {
137
138
  title: 'block title',
138
139
  edit,
139
140
  } );
141
+
142
+ registerCoreBlocks();
140
143
  } );
141
144
 
142
145
  afterEach( () => {
143
146
  getBlockTypes().forEach( ( block ) => {
144
147
  unregisterBlockType( block.name );
145
148
  } );
149
+ unregisterFormatType( 'core/footnote' );
146
150
  } );
147
151
 
148
152
  it( 'does not mutate block attributes that include an array of strings or null values', async () => {