@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.
- package/CHANGELOG.md +4 -0
- package/README.md +54 -6
- package/build/actions.js +1 -1
- package/build/actions.js.map +1 -1
- package/build/entity-context.js +13 -0
- package/build/entity-context.js.map +1 -0
- package/build/entity-provider.js +4 -189
- package/build/entity-provider.js.map +1 -1
- package/build/footnotes/get-rich-text-values-cached.js +2 -2
- package/build/footnotes/get-rich-text-values-cached.js.map +1 -1
- package/build/hooks/index.js +22 -0
- package/build/hooks/index.js.map +1 -1
- package/build/hooks/use-entity-block-editor.js +140 -0
- package/build/hooks/use-entity-block-editor.js.map +1 -0
- package/build/hooks/use-entity-id.js +28 -0
- package/build/hooks/use-entity-id.js.map +1 -0
- package/build/hooks/use-entity-prop.js +65 -0
- package/build/hooks/use-entity-prop.js.map +1 -0
- package/build/hooks/use-entity-records.js +39 -0
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/hooks/use-resource-permissions.js +25 -8
- package/build/hooks/use-resource-permissions.js.map +1 -1
- package/build/index.js +14 -2
- package/build/index.js.map +1 -1
- package/build/lock-unlock.js +18 -0
- package/build/lock-unlock.js.map +1 -0
- package/build/private-apis.js +8 -9
- package/build/private-apis.js.map +1 -1
- package/build/private-selectors.js +35 -0
- package/build/private-selectors.js.map +1 -1
- package/build/resolvers.js +64 -60
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +16 -8
- package/build/selectors.js.map +1 -1
- package/build/utils/index.js +19 -0
- package/build/utils/index.js.map +1 -1
- package/build/utils/user-permissions.js +32 -0
- package/build/utils/user-permissions.js.map +1 -0
- package/build-module/actions.js +1 -1
- package/build-module/actions.js.map +1 -1
- package/build-module/entity-context.js +6 -0
- package/build-module/entity-context.js.map +1 -0
- package/build-module/entity-provider.js +3 -183
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/footnotes/get-rich-text-values-cached.js +1 -1
- package/build-module/footnotes/get-rich-text-values-cached.js.map +1 -1
- package/build-module/hooks/index.js +3 -0
- package/build-module/hooks/index.js.map +1 -1
- package/build-module/hooks/use-entity-block-editor.js +132 -0
- package/build-module/hooks/use-entity-block-editor.js.map +1 -0
- package/build-module/hooks/use-entity-id.js +22 -0
- package/build-module/hooks/use-entity-id.js.map +1 -0
- package/build-module/hooks/use-entity-prop.js +58 -0
- package/build-module/hooks/use-entity-prop.js.map +1 -0
- package/build-module/hooks/use-entity-records.js +38 -0
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/hooks/use-resource-permissions.js +25 -8
- package/build-module/hooks/use-resource-permissions.js.map +1 -1
- package/build-module/index.js +2 -1
- package/build-module/index.js.map +1 -1
- package/build-module/lock-unlock.js +9 -0
- package/build-module/lock-unlock.js.map +1 -0
- package/build-module/private-apis.js +7 -6
- package/build-module/private-apis.js.map +1 -1
- package/build-module/private-selectors.js +33 -0
- package/build-module/private-selectors.js.map +1 -1
- package/build-module/resolvers.js +65 -61
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +17 -9
- package/build-module/selectors.js.map +1 -1
- package/build-module/utils/index.js +1 -0
- package/build-module/utils/index.js.map +1 -1
- package/build-module/utils/user-permissions.js +24 -0
- package/build-module/utils/user-permissions.js.map +1 -0
- package/build-types/actions.d.ts +3 -3
- package/build-types/actions.d.ts.map +1 -1
- package/build-types/batch/create-batch.d.ts.map +1 -1
- package/build-types/entities.d.ts.map +1 -1
- package/build-types/entity-context.d.ts +2 -0
- package/build-types/entity-context.d.ts.map +1 -0
- package/build-types/entity-provider.d.ts +0 -47
- package/build-types/entity-provider.d.ts.map +1 -1
- package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -1
- package/build-types/hooks/index.d.ts +3 -0
- package/build-types/hooks/index.d.ts.map +1 -1
- package/build-types/hooks/use-entity-block-editor.d.ts +22 -0
- package/build-types/hooks/use-entity-block-editor.d.ts.map +1 -0
- package/build-types/hooks/use-entity-id.d.ts +9 -0
- package/build-types/hooks/use-entity-id.d.ts.map +1 -0
- package/build-types/hooks/use-entity-prop.d.ts +19 -0
- package/build-types/hooks/use-entity-prop.d.ts.map +1 -0
- package/build-types/hooks/use-entity-records.d.ts +1 -0
- package/build-types/hooks/use-entity-records.d.ts.map +1 -1
- package/build-types/hooks/use-resource-permissions.d.ts +8 -70
- package/build-types/hooks/use-resource-permissions.d.ts.map +1 -1
- package/build-types/index.d.ts +36 -32
- package/build-types/index.d.ts.map +1 -1
- package/build-types/lock-unlock.d.ts +3 -0
- package/build-types/lock-unlock.d.ts.map +1 -0
- package/build-types/locks/reducer.d.ts +1 -1
- package/build-types/locks/reducer.d.ts.map +1 -1
- package/build-types/private-apis.d.ts +1 -2
- package/build-types/private-apis.d.ts.map +1 -1
- package/build-types/private-selectors.d.ts +15 -0
- package/build-types/private-selectors.d.ts.map +1 -1
- package/build-types/queried-data/actions.d.ts +1 -1
- package/build-types/queried-data/actions.d.ts.map +1 -1
- package/build-types/queried-data/get-query-parts.d.ts.map +1 -1
- package/build-types/queried-data/reducer.d.ts +1 -1
- package/build-types/queried-data/reducer.d.ts.map +1 -1
- package/build-types/queried-data/selectors.d.ts +0 -1
- package/build-types/queried-data/selectors.d.ts.map +1 -1
- package/build-types/reducer.d.ts +13 -13
- package/build-types/reducer.d.ts.map +1 -1
- package/build-types/resolvers.d.ts +3 -2
- package/build-types/resolvers.d.ts.map +1 -1
- package/build-types/selectors.d.ts +11 -6
- package/build-types/selectors.d.ts.map +1 -1
- package/build-types/utils/get-nested-value.d.ts.map +1 -1
- package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -1
- package/build-types/utils/if-matching-action.d.ts +1 -1
- package/build-types/utils/index.d.ts +1 -0
- package/build-types/utils/on-sub-key.d.ts +1 -1
- package/build-types/utils/replace-action.d.ts +1 -1
- package/build-types/utils/set-nested-value.d.ts.map +1 -1
- package/build-types/utils/user-permissions.d.ts +4 -0
- package/build-types/utils/user-permissions.d.ts.map +1 -0
- package/package.json +18 -17
- package/src/actions.js +1 -1
- package/src/entity-context.js +6 -0
- package/src/entity-provider.js +2 -209
- package/src/footnotes/get-rich-text-values-cached.js +1 -1
- package/src/hooks/index.ts +3 -0
- package/src/hooks/test/use-entity-record.js +5 -3
- package/src/hooks/test/use-resource-permissions.js +96 -5
- package/src/hooks/use-entity-block-editor.js +148 -0
- package/src/hooks/use-entity-id.js +21 -0
- package/src/hooks/use-entity-prop.js +60 -0
- package/src/hooks/use-entity-records.ts +50 -0
- package/src/hooks/use-resource-permissions.ts +46 -9
- package/src/index.js +2 -1
- package/src/lock-unlock.js +10 -0
- package/src/private-apis.js +7 -7
- package/src/private-selectors.ts +43 -0
- package/src/resolvers.js +85 -67
- package/src/selectors.ts +18 -9
- package/src/test/entity-provider.js +6 -2
- package/src/test/resolvers.js +217 -50
- package/src/test/selectors.js +18 -55
- package/src/utils/index.js +5 -0
- package/src/utils/user-permissions.js +39 -0
- package/tsconfig.json +2 -1
- 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
|
|
50
|
-
*
|
|
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( '
|
|
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( '
|
|
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
|
-
|
|
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(
|
|
120
|
-
|
|
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
|
-
[
|
|
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 './
|
|
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
|
+
);
|
package/src/private-apis.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { useEntityRecordsWithPermissions } from './hooks/use-entity-records';
|
|
5
|
+
import { lock } from './lock-unlock';
|
|
5
6
|
|
|
6
|
-
export const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
);
|
|
7
|
+
export const privateApis = {};
|
|
8
|
+
lock( privateApis, {
|
|
9
|
+
useEntityRecordsWithPermissions,
|
|
10
|
+
} );
|
package/src/private-selectors.ts
CHANGED
|
@@ -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
|
+
}
|