@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
@@ -0,0 +1,148 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useCallback, useMemo } from '@wordpress/element';
5
+ import { useDispatch, useSelect } from '@wordpress/data';
6
+ import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
7
+
8
+ /**
9
+ * Internal dependencies
10
+ */
11
+ import { STORE_NAME } from '../name';
12
+ import useEntityId from './use-entity-id';
13
+ import { updateFootnotesFromMeta } from '../footnotes';
14
+
15
+ const EMPTY_ARRAY = [];
16
+ const parsedBlocksCache = new WeakMap();
17
+
18
+ /**
19
+ * Hook that returns block content getters and setters for
20
+ * the nearest provided entity of the specified type.
21
+ *
22
+ * The return value has the shape `[ blocks, onInput, onChange ]`.
23
+ * `onInput` is for block changes that don't create undo levels
24
+ * or dirty the post, non-persistent changes, and `onChange` is for
25
+ * persistent changes. They map directly to the props of a
26
+ * `BlockEditorProvider` and are intended to be used with it,
27
+ * or similar components or hooks.
28
+ *
29
+ * @param {string} kind The entity kind.
30
+ * @param {string} name The entity name.
31
+ * @param {Object} options
32
+ * @param {string} [options.id] An entity ID to use instead of the context-provided one.
33
+ *
34
+ * @return {[unknown[], Function, Function]} The block array and setters.
35
+ */
36
+ export default function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
37
+ const providerId = useEntityId( kind, name );
38
+ const id = _id ?? providerId;
39
+ const { getEntityRecord, getEntityRecordEdits } = useSelect( STORE_NAME );
40
+ const { content, editedBlocks, meta } = useSelect(
41
+ ( select ) => {
42
+ if ( ! id ) {
43
+ return {};
44
+ }
45
+ const { getEditedEntityRecord } = select( STORE_NAME );
46
+ const editedRecord = getEditedEntityRecord( kind, name, id );
47
+ return {
48
+ editedBlocks: editedRecord.blocks,
49
+ content: editedRecord.content,
50
+ meta: editedRecord.meta,
51
+ };
52
+ },
53
+ [ kind, name, id ]
54
+ );
55
+ const { __unstableCreateUndoLevel, editEntityRecord } =
56
+ useDispatch( STORE_NAME );
57
+
58
+ const blocks = useMemo( () => {
59
+ if ( ! id ) {
60
+ return undefined;
61
+ }
62
+
63
+ if ( editedBlocks ) {
64
+ return editedBlocks;
65
+ }
66
+
67
+ if ( ! content || typeof content !== 'string' ) {
68
+ return EMPTY_ARRAY;
69
+ }
70
+
71
+ // If there's an edit, cache the parsed blocks by the edit.
72
+ // If not, cache by the original enity record.
73
+ const edits = getEntityRecordEdits( kind, name, id );
74
+ const isUnedited = ! edits || ! Object.keys( edits ).length;
75
+ const cackeKey = isUnedited ? getEntityRecord( kind, name, id ) : edits;
76
+ let _blocks = parsedBlocksCache.get( cackeKey );
77
+
78
+ if ( ! _blocks ) {
79
+ _blocks = parse( content );
80
+ parsedBlocksCache.set( cackeKey, _blocks );
81
+ }
82
+
83
+ return _blocks;
84
+ }, [
85
+ kind,
86
+ name,
87
+ id,
88
+ editedBlocks,
89
+ content,
90
+ getEntityRecord,
91
+ getEntityRecordEdits,
92
+ ] );
93
+
94
+ const updateFootnotes = useCallback(
95
+ ( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),
96
+ [ meta ]
97
+ );
98
+
99
+ const onChange = useCallback(
100
+ ( newBlocks, options ) => {
101
+ const noChange = blocks === newBlocks;
102
+ if ( noChange ) {
103
+ return __unstableCreateUndoLevel( kind, name, id );
104
+ }
105
+ const { selection, ...rest } = options;
106
+
107
+ // We create a new function here on every persistent edit
108
+ // to make sure the edit makes the post dirty and creates
109
+ // a new undo level.
110
+ const edits = {
111
+ selection,
112
+ content: ( { blocks: blocksForSerialization = [] } ) =>
113
+ __unstableSerializeAndClean( blocksForSerialization ),
114
+ ...updateFootnotes( newBlocks ),
115
+ };
116
+
117
+ editEntityRecord( kind, name, id, edits, {
118
+ isCached: false,
119
+ ...rest,
120
+ } );
121
+ },
122
+ [
123
+ kind,
124
+ name,
125
+ id,
126
+ blocks,
127
+ updateFootnotes,
128
+ __unstableCreateUndoLevel,
129
+ editEntityRecord,
130
+ ]
131
+ );
132
+
133
+ const onInput = useCallback(
134
+ ( newBlocks, options ) => {
135
+ const { selection, ...rest } = options;
136
+ const footnotesChanges = updateFootnotes( newBlocks );
137
+ const edits = { selection, ...footnotesChanges };
138
+
139
+ editEntityRecord( kind, name, id, edits, {
140
+ isCached: true,
141
+ ...rest,
142
+ } );
143
+ },
144
+ [ kind, name, id, updateFootnotes, editEntityRecord ]
145
+ );
146
+
147
+ return [ blocks, onInput, onChange ];
148
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useContext } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import { EntityContext } from '../entity-context';
10
+
11
+ /**
12
+ * Hook that returns the ID for the nearest
13
+ * provided entity of the specified type.
14
+ *
15
+ * @param {string} kind The entity kind.
16
+ * @param {string} name The entity name.
17
+ */
18
+ export default function useEntityId( kind, name ) {
19
+ const context = useContext( EntityContext );
20
+ return context?.[ kind ]?.[ name ];
21
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useCallback } from '@wordpress/element';
5
+ import { useDispatch, useSelect } from '@wordpress/data';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import { STORE_NAME } from '../name';
11
+ import useEntityId from './use-entity-id';
12
+
13
+ /**
14
+ * Hook that returns the value and a setter for the
15
+ * specified property of the nearest provided
16
+ * entity of the specified type.
17
+ *
18
+ * @param {string} kind The entity kind.
19
+ * @param {string} name The entity name.
20
+ * @param {string} prop The property name.
21
+ * @param {string} [_id] An entity ID to use instead of the context-provided one.
22
+ *
23
+ * @return {[*, Function, *]} An array where the first item is the
24
+ * property value, the second is the
25
+ * setter and the third is the full value
26
+ * object from REST API containing more
27
+ * information like `raw`, `rendered` and
28
+ * `protected` props.
29
+ */
30
+ export default function useEntityProp( kind, name, prop, _id ) {
31
+ const providerId = useEntityId( kind, name );
32
+ const id = _id ?? providerId;
33
+
34
+ const { value, fullValue } = useSelect(
35
+ ( select ) => {
36
+ const { getEntityRecord, getEditedEntityRecord } =
37
+ select( STORE_NAME );
38
+ const record = getEntityRecord( kind, name, id ); // Trigger resolver.
39
+ const editedRecord = getEditedEntityRecord( kind, name, id );
40
+ return record && editedRecord
41
+ ? {
42
+ value: editedRecord[ prop ],
43
+ fullValue: record[ prop ],
44
+ }
45
+ : {};
46
+ },
47
+ [ kind, name, id, prop ]
48
+ );
49
+ const { editEntityRecord } = useDispatch( STORE_NAME );
50
+ const setValue = useCallback(
51
+ ( newValue ) => {
52
+ editEntityRecord( kind, name, id, {
53
+ [ prop ]: newValue,
54
+ } );
55
+ },
56
+ [ editEntityRecord, kind, name, id, prop ]
57
+ );
58
+
59
+ return [ value, setValue, fullValue ];
60
+ }
@@ -4,6 +4,7 @@
4
4
  import { addQueryArgs } from '@wordpress/url';
5
5
  import deprecated from '@wordpress/deprecated';
6
6
  import { useSelect } from '@wordpress/data';
7
+ import { useMemo } from '@wordpress/element';
7
8
 
8
9
  /**
9
10
  * Internal dependencies
@@ -12,6 +13,7 @@ import useQuerySelect from './use-query-select';
12
13
  import { store as coreStore } from '../';
13
14
  import type { Options } from './use-entity-record';
14
15
  import type { Status } from './constants';
16
+ import { unlock } from '../lock-unlock';
15
17
 
16
18
  interface EntityRecordsResolution< RecordType > {
17
19
  /** The requested entity record */
@@ -152,3 +154,51 @@ export function __experimentalUseEntityRecords(
152
154
  } );
153
155
  return useEntityRecords( kind, name, queryArgs, options );
154
156
  }
157
+
158
+ export function useEntityRecordsWithPermissions< RecordType >(
159
+ kind: string,
160
+ name: string,
161
+ queryArgs: Record< string, unknown > = {},
162
+ options: Options = { enabled: true }
163
+ ): EntityRecordsResolution< RecordType > {
164
+ const entityConfig = useSelect(
165
+ ( select ) => select( coreStore ).getEntityConfig( kind, name ),
166
+ [ kind, name ]
167
+ );
168
+ const { records: data, ...ret } = useEntityRecords(
169
+ kind,
170
+ name,
171
+ queryArgs,
172
+ options
173
+ );
174
+ const ids = useMemo(
175
+ () =>
176
+ data?.map(
177
+ // @ts-ignore
178
+ ( record: RecordType ) => record[ entityConfig?.key ?? 'id' ]
179
+ ) ?? [],
180
+ [ data, entityConfig?.key ]
181
+ );
182
+
183
+ const permissions = useSelect(
184
+ ( select ) => {
185
+ const { getEntityRecordsPermissions } = unlock(
186
+ select( coreStore )
187
+ );
188
+ return getEntityRecordsPermissions( kind, name, ids );
189
+ },
190
+ [ ids, kind, name ]
191
+ );
192
+
193
+ const dataWithPermissions = useMemo(
194
+ () =>
195
+ data?.map( ( record, index ) => ( {
196
+ // @ts-ignore
197
+ ...record,
198
+ permissions: permissions[ index ],
199
+ } ) ) ?? [],
200
+ [ data, permissions ]
201
+ );
202
+
203
+ return { records: dataWithPermissions, ...ret };
204
+ }
@@ -2,6 +2,7 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import deprecated from '@wordpress/deprecated';
5
+ import warning from '@wordpress/warning';
5
6
 
6
7
  /**
7
8
  * Internal dependencies
@@ -41,20 +42,34 @@ type ResourcePermissionsResolution< IdType > = [
41
42
  ( IdType extends void ? SpecificResourcePermissionsResolution : {} ),
42
43
  ];
43
44
 
45
+ type EntityResource = { kind: string; name: string; id?: string | number };
46
+
47
+ function useResourcePermissions< IdType = void >(
48
+ resource: string,
49
+ id?: IdType
50
+ ): ResourcePermissionsResolution< IdType >;
51
+
52
+ function useResourcePermissions< IdType = void >(
53
+ resource: EntityResource,
54
+ id?: never
55
+ ): ResourcePermissionsResolution< IdType >;
56
+
44
57
  /**
45
58
  * Resolves resource permissions.
46
59
  *
47
60
  * @since 6.1.0 Introduced in WordPress core.
48
61
  *
49
- * @param resource The resource in question, e.g. media.
50
- * @param id ID of a specific resource entry, if needed, e.g. 10.
62
+ * @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`
63
+ * or REST base as a string - `media`.
64
+ * @param id Optional ID of the resource to check, e.g. 10. Note: This argument is discouraged
65
+ * when using an entity object as a resource to check permissions and will be ignored.
51
66
  *
52
67
  * @example
53
68
  * ```js
54
69
  * import { useResourcePermissions } from '@wordpress/core-data';
55
70
  *
56
71
  * function PagesList() {
57
- * const { canCreate, isResolving } = useResourcePermissions( 'pages' );
72
+ * const { canCreate, isResolving } = useResourcePermissions( { kind: 'postType', name: 'page' } );
58
73
  *
59
74
  * if ( isResolving ) {
60
75
  * return 'Loading ...';
@@ -82,7 +97,7 @@ type ResourcePermissionsResolution< IdType > = [
82
97
  * canUpdate,
83
98
  * canDelete,
84
99
  * isResolving
85
- * } = useResourcePermissions( 'pages', pageId );
100
+ * } = useResourcePermissions( { kind: 'postType', name: 'page', id: pageId } );
86
101
  *
87
102
  * if ( isResolving ) {
88
103
  * return 'Loading ...';
@@ -109,15 +124,35 @@ type ResourcePermissionsResolution< IdType > = [
109
124
  * @return Entity records data.
110
125
  * @template IdType
111
126
  */
112
- export default function useResourcePermissions< IdType = void >(
113
- resource: string,
127
+ function useResourcePermissions< IdType = void >(
128
+ resource: string | EntityResource,
114
129
  id?: IdType
115
130
  ): ResourcePermissionsResolution< IdType > {
131
+ // Serialize `resource` to a string that can be safely used as a React dep.
132
+ // We can't just pass `resource` as one of the deps, because if it is passed
133
+ // as an object literal, then it will be a different object on each call even
134
+ // if the values remain the same.
135
+ const isEntity = typeof resource === 'object';
136
+ const resourceAsString = isEntity ? JSON.stringify( resource ) : resource;
137
+
138
+ if ( isEntity && typeof id !== 'undefined' ) {
139
+ warning(
140
+ `When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`
141
+ );
142
+ }
143
+
116
144
  return useQuerySelect(
117
145
  ( resolve ) => {
146
+ const hasId = isEntity ? !! resource.id : !! id;
118
147
  const { canUser } = resolve( coreStore );
119
- const create = canUser( 'create', resource );
120
- if ( ! id ) {
148
+ const create = canUser(
149
+ 'create',
150
+ isEntity
151
+ ? { kind: resource.kind, name: resource.name }
152
+ : resource
153
+ );
154
+
155
+ if ( ! hasId ) {
121
156
  const read = canUser( 'read', resource );
122
157
 
123
158
  const isResolving = create.isResolving || read.isResolving;
@@ -168,10 +203,12 @@ export default function useResourcePermissions< IdType = void >(
168
203
  canDelete: hasResolved && _delete.data,
169
204
  };
170
205
  },
171
- [ resource, id ]
206
+ [ resourceAsString, id ]
172
207
  );
173
208
  }
174
209
 
210
+ export default useResourcePermissions;
211
+
175
212
  export function __experimentalUseResourcePermissions(
176
213
  resource: string,
177
214
  id?: unknown
package/src/index.js CHANGED
@@ -18,7 +18,7 @@ import {
18
18
  getMethodName,
19
19
  } from './entities';
20
20
  import { STORE_NAME } from './name';
21
- import { unlock } from './private-apis';
21
+ import { unlock } from './lock-unlock';
22
22
 
23
23
  // The entity selectors/resolvers and actions are shortcuts to their generic equivalents
24
24
  // (getEntityRecord, getEntityRecords, updateEntityRecord, updateEntityRecords)
@@ -86,3 +86,4 @@ export * from './entity-provider';
86
86
  export * from './entity-types';
87
87
  export * from './fetch';
88
88
  export * from './hooks';
89
+ export * from './private-apis';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
5
+
6
+ export const { lock, unlock } =
7
+ __dangerousOptInToUnstableAPIsOnlyForCoreModules(
8
+ 'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
9
+ '@wordpress/core-data'
10
+ );
@@ -1,10 +1,10 @@
1
1
  /**
2
- * WordPress dependencies
2
+ * Internal dependencies
3
3
  */
4
- import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
4
+ import { useEntityRecordsWithPermissions } from './hooks/use-entity-records';
5
+ import { lock } from './lock-unlock';
5
6
 
6
- export const { lock, unlock } =
7
- __dangerousOptInToUnstableAPIsOnlyForCoreModules(
8
- 'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
9
- '@wordpress/core-data'
10
- );
7
+ export const privateApis = {};
8
+ lock( privateApis, {
9
+ useEntityRecordsWithPermissions,
10
+ } );
@@ -50,3 +50,46 @@ export const getBlockPatternsForPostType = createRegistrySelector(
50
50
  () => [ select( STORE_NAME ).getBlockPatterns() ]
51
51
  )
52
52
  );
53
+
54
+ /**
55
+ * Returns the entity records permissions for the given entity record ids.
56
+ */
57
+ export const getEntityRecordsPermissions = createRegistrySelector( ( select ) =>
58
+ createSelector(
59
+ ( state: State, kind: string, name: string, ids: string[] ) => {
60
+ const normalizedIds = Array.isArray( ids ) ? ids : [ ids ];
61
+ return normalizedIds.map( ( id ) => ( {
62
+ delete: select( STORE_NAME ).canUser( 'delete', {
63
+ kind,
64
+ name,
65
+ id,
66
+ } ),
67
+ update: select( STORE_NAME ).canUser( 'update', {
68
+ kind,
69
+ name,
70
+ id,
71
+ } ),
72
+ } ) );
73
+ },
74
+ ( state ) => [ state.userPermissions ]
75
+ )
76
+ );
77
+
78
+ /**
79
+ * Returns the entity record permissions for the given entity record id.
80
+ *
81
+ * @param state Data state.
82
+ * @param kind Entity kind.
83
+ * @param name Entity name.
84
+ * @param id Entity record id.
85
+ *
86
+ * @return The entity record permissions.
87
+ */
88
+ export function getEntityRecordPermissions(
89
+ state: State,
90
+ kind: string,
91
+ name: string,
92
+ id: string
93
+ ) {
94
+ return getEntityRecordsPermissions( state, kind, name, id )[ 0 ];
95
+ }