@wordpress/core-data 7.3.0 → 7.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/README.md +53 -5
- 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/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-resource-permissions.js +25 -8
- package/build/hooks/use-resource-permissions.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/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/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-resource-permissions.js +25 -8
- package/build-module/hooks/use-resource-permissions.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 +2 -2
- 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-resource-permissions.d.ts +8 -70
- package/build-types/hooks/use-resource-permissions.d.ts.map +1 -1
- package/build-types/index.d.ts +35 -32
- package/build-types/index.d.ts.map +1 -1
- package/build-types/locks/reducer.d.ts +1 -1
- package/build-types/locks/reducer.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/entity-context.js +6 -0
- package/src/entity-provider.js +2 -209
- 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-resource-permissions.ts +46 -9
- 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,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = useEntityProp;
|
|
8
|
+
var _element = require("@wordpress/element");
|
|
9
|
+
var _data = require("@wordpress/data");
|
|
10
|
+
var _name = require("../name");
|
|
11
|
+
var _useEntityId = _interopRequireDefault(require("./use-entity-id"));
|
|
12
|
+
/**
|
|
13
|
+
* WordPress dependencies
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Internal dependencies
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Hook that returns the value and a setter for the
|
|
22
|
+
* specified property of the nearest provided
|
|
23
|
+
* entity of the specified type.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} kind The entity kind.
|
|
26
|
+
* @param {string} name The entity name.
|
|
27
|
+
* @param {string} prop The property name.
|
|
28
|
+
* @param {string} [_id] An entity ID to use instead of the context-provided one.
|
|
29
|
+
*
|
|
30
|
+
* @return {[*, Function, *]} An array where the first item is the
|
|
31
|
+
* property value, the second is the
|
|
32
|
+
* setter and the third is the full value
|
|
33
|
+
* object from REST API containing more
|
|
34
|
+
* information like `raw`, `rendered` and
|
|
35
|
+
* `protected` props.
|
|
36
|
+
*/
|
|
37
|
+
function useEntityProp(kind, name, prop, _id) {
|
|
38
|
+
const providerId = (0, _useEntityId.default)(kind, name);
|
|
39
|
+
const id = _id !== null && _id !== void 0 ? _id : providerId;
|
|
40
|
+
const {
|
|
41
|
+
value,
|
|
42
|
+
fullValue
|
|
43
|
+
} = (0, _data.useSelect)(select => {
|
|
44
|
+
const {
|
|
45
|
+
getEntityRecord,
|
|
46
|
+
getEditedEntityRecord
|
|
47
|
+
} = select(_name.STORE_NAME);
|
|
48
|
+
const record = getEntityRecord(kind, name, id); // Trigger resolver.
|
|
49
|
+
const editedRecord = getEditedEntityRecord(kind, name, id);
|
|
50
|
+
return record && editedRecord ? {
|
|
51
|
+
value: editedRecord[prop],
|
|
52
|
+
fullValue: record[prop]
|
|
53
|
+
} : {};
|
|
54
|
+
}, [kind, name, id, prop]);
|
|
55
|
+
const {
|
|
56
|
+
editEntityRecord
|
|
57
|
+
} = (0, _data.useDispatch)(_name.STORE_NAME);
|
|
58
|
+
const setValue = (0, _element.useCallback)(newValue => {
|
|
59
|
+
editEntityRecord(kind, name, id, {
|
|
60
|
+
[prop]: newValue
|
|
61
|
+
});
|
|
62
|
+
}, [editEntityRecord, kind, name, id, prop]);
|
|
63
|
+
return [value, setValue, fullValue];
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=use-entity-prop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_element","require","_data","_name","_useEntityId","_interopRequireDefault","useEntityProp","kind","name","prop","_id","providerId","useEntityId","id","value","fullValue","useSelect","select","getEntityRecord","getEditedEntityRecord","STORE_NAME","record","editedRecord","editEntityRecord","useDispatch","setValue","useCallback","newValue"],"sources":["@wordpress/core-data/src/hooks/use-entity-prop.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from '../name';\nimport useEntityId from './use-entity-id';\n\n/**\n * Hook that returns the value and a setter for the\n * specified property of the nearest provided\n * entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {string} prop The property name.\n * @param {string} [_id] An entity ID to use instead of the context-provided one.\n *\n * @return {[*, Function, *]} An array where the first item is the\n * property value, the second is the\n * setter and the third is the full value\n * \t\t\t\t\t\t\t object from REST API containing more\n * \t\t\t\t\t\t\t information like `raw`, `rendered` and\n * \t\t\t\t\t\t\t `protected` props.\n */\nexport default function useEntityProp( kind, name, prop, _id ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\n\tconst { value, fullValue } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecord, getEditedEntityRecord } =\n\t\t\t\tselect( STORE_NAME );\n\t\t\tconst record = getEntityRecord( kind, name, id ); // Trigger resolver.\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn record && editedRecord\n\t\t\t\t? {\n\t\t\t\t\t\tvalue: editedRecord[ prop ],\n\t\t\t\t\t\tfullValue: record[ prop ],\n\t\t\t\t }\n\t\t\t\t: {};\n\t\t},\n\t\t[ kind, name, id, prop ]\n\t);\n\tconst { editEntityRecord } = useDispatch( STORE_NAME );\n\tconst setValue = useCallback(\n\t\t( newValue ) => {\n\t\t\teditEntityRecord( kind, name, id, {\n\t\t\t\t[ prop ]: newValue,\n\t\t\t} );\n\t\t},\n\t\t[ editEntityRecord, kind, name, id, prop ]\n\t);\n\n\treturn [ value, setValue, fullValue ];\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AAKA,IAAAE,KAAA,GAAAF,OAAA;AACA,IAAAG,YAAA,GAAAC,sBAAA,CAAAJ,OAAA;AAVA;AACA;AACA;;AAIA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASK,aAAaA,CAAEC,IAAI,EAAEC,IAAI,EAAEC,IAAI,EAAEC,GAAG,EAAG;EAC9D,MAAMC,UAAU,GAAG,IAAAC,oBAAW,EAAEL,IAAI,EAAEC,IAAK,CAAC;EAC5C,MAAMK,EAAE,GAAGH,GAAG,aAAHA,GAAG,cAAHA,GAAG,GAAIC,UAAU;EAE5B,MAAM;IAAEG,KAAK;IAAEC;EAAU,CAAC,GAAG,IAAAC,eAAS,EACnCC,MAAM,IAAM;IACb,MAAM;MAAEC,eAAe;MAAEC;IAAsB,CAAC,GAC/CF,MAAM,CAAEG,gBAAW,CAAC;IACrB,MAAMC,MAAM,GAAGH,eAAe,CAAEX,IAAI,EAAEC,IAAI,EAAEK,EAAG,CAAC,CAAC,CAAC;IAClD,MAAMS,YAAY,GAAGH,qBAAqB,CAAEZ,IAAI,EAAEC,IAAI,EAAEK,EAAG,CAAC;IAC5D,OAAOQ,MAAM,IAAIC,YAAY,GAC1B;MACAR,KAAK,EAAEQ,YAAY,CAAEb,IAAI,CAAE;MAC3BM,SAAS,EAAEM,MAAM,CAAEZ,IAAI;IACvB,CAAC,GACD,CAAC,CAAC;EACN,CAAC,EACD,CAAEF,IAAI,EAAEC,IAAI,EAAEK,EAAE,EAAEJ,IAAI,CACvB,CAAC;EACD,MAAM;IAAEc;EAAiB,CAAC,GAAG,IAAAC,iBAAW,EAAEJ,gBAAW,CAAC;EACtD,MAAMK,QAAQ,GAAG,IAAAC,oBAAW,EACzBC,QAAQ,IAAM;IACfJ,gBAAgB,CAAEhB,IAAI,EAAEC,IAAI,EAAEK,EAAE,EAAE;MACjC,CAAEJ,IAAI,GAAIkB;IACX,CAAE,CAAC;EACJ,CAAC,EACD,CAAEJ,gBAAgB,EAAEhB,IAAI,EAAEC,IAAI,EAAEK,EAAE,EAAEJ,IAAI,CACzC,CAAC;EAED,OAAO,CAAEK,KAAK,EAAEW,QAAQ,EAAEV,SAAS,CAAE;AACtC","ignoreList":[]}
|
|
@@ -5,8 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.__experimentalUseResourcePermissions = __experimentalUseResourcePermissions;
|
|
8
|
-
exports.default =
|
|
8
|
+
exports.default = void 0;
|
|
9
9
|
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
|
|
10
|
+
var _warning = _interopRequireDefault(require("@wordpress/warning"));
|
|
10
11
|
var _ = require("../");
|
|
11
12
|
var _constants = require("./constants");
|
|
12
13
|
var _useQuerySelect = _interopRequireDefault(require("./use-query-select"));
|
|
@@ -27,15 +28,17 @@ var _useQuerySelect = _interopRequireDefault(require("./use-query-select"));
|
|
|
27
28
|
*
|
|
28
29
|
* @since 6.1.0 Introduced in WordPress core.
|
|
29
30
|
*
|
|
30
|
-
* @param resource
|
|
31
|
-
*
|
|
31
|
+
* @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`
|
|
32
|
+
* or REST base as a string - `media`.
|
|
33
|
+
* @param id Optional ID of the resource to check, e.g. 10. Note: This argument is discouraged
|
|
34
|
+
* when using an entity object as a resource to check permissions and will be ignored.
|
|
32
35
|
*
|
|
33
36
|
* @example
|
|
34
37
|
* ```js
|
|
35
38
|
* import { useResourcePermissions } from '@wordpress/core-data';
|
|
36
39
|
*
|
|
37
40
|
* function PagesList() {
|
|
38
|
-
* const { canCreate, isResolving } = useResourcePermissions( '
|
|
41
|
+
* const { canCreate, isResolving } = useResourcePermissions( { kind: 'postType', name: 'page' } );
|
|
39
42
|
*
|
|
40
43
|
* if ( isResolving ) {
|
|
41
44
|
* return 'Loading ...';
|
|
@@ -63,7 +66,7 @@ var _useQuerySelect = _interopRequireDefault(require("./use-query-select"));
|
|
|
63
66
|
* canUpdate,
|
|
64
67
|
* canDelete,
|
|
65
68
|
* isResolving
|
|
66
|
-
* } = useResourcePermissions( '
|
|
69
|
+
* } = useResourcePermissions( { kind: 'postType', name: 'page', id: pageId } );
|
|
67
70
|
*
|
|
68
71
|
* if ( isResolving ) {
|
|
69
72
|
* return 'Loading ...';
|
|
@@ -91,12 +94,25 @@ var _useQuerySelect = _interopRequireDefault(require("./use-query-select"));
|
|
|
91
94
|
* @template IdType
|
|
92
95
|
*/
|
|
93
96
|
function useResourcePermissions(resource, id) {
|
|
97
|
+
// Serialize `resource` to a string that can be safely used as a React dep.
|
|
98
|
+
// We can't just pass `resource` as one of the deps, because if it is passed
|
|
99
|
+
// as an object literal, then it will be a different object on each call even
|
|
100
|
+
// if the values remain the same.
|
|
101
|
+
const isEntity = typeof resource === 'object';
|
|
102
|
+
const resourceAsString = isEntity ? JSON.stringify(resource) : resource;
|
|
103
|
+
if (isEntity && typeof id !== 'undefined') {
|
|
104
|
+
globalThis.SCRIPT_DEBUG === true ? (0, _warning.default)(`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`) : void 0;
|
|
105
|
+
}
|
|
94
106
|
return (0, _useQuerySelect.default)(resolve => {
|
|
107
|
+
const hasId = isEntity ? !!resource.id : !!id;
|
|
95
108
|
const {
|
|
96
109
|
canUser
|
|
97
110
|
} = resolve(_.store);
|
|
98
|
-
const create = canUser('create',
|
|
99
|
-
|
|
111
|
+
const create = canUser('create', isEntity ? {
|
|
112
|
+
kind: resource.kind,
|
|
113
|
+
name: resource.name
|
|
114
|
+
} : resource);
|
|
115
|
+
if (!hasId) {
|
|
100
116
|
const read = canUser('read', resource);
|
|
101
117
|
const isResolving = create.isResolving || read.isResolving;
|
|
102
118
|
const hasResolved = create.hasResolved && read.hasResolved;
|
|
@@ -134,8 +150,9 @@ function useResourcePermissions(resource, id) {
|
|
|
134
150
|
canUpdate: hasResolved && update.data,
|
|
135
151
|
canDelete: hasResolved && _delete.data
|
|
136
152
|
};
|
|
137
|
-
}, [
|
|
153
|
+
}, [resourceAsString, id]);
|
|
138
154
|
}
|
|
155
|
+
var _default = exports.default = useResourcePermissions;
|
|
139
156
|
function __experimentalUseResourcePermissions(resource, id) {
|
|
140
157
|
(0, _deprecated.default)(`wp.data.__experimentalUseResourcePermissions`, {
|
|
141
158
|
alternative: 'wp.data.useResourcePermissions',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_deprecated","_interopRequireDefault","require","_","_constants","_useQuerySelect","useResourcePermissions","resource","id","useQuerySelect","resolve","canUser","coreStore","create","read","isResolving","hasResolved","status","Status","Idle","Resolving","Success","canCreate","data","canRead","update","_delete","canUpdate","canDelete","__experimentalUseResourcePermissions","deprecated","alternative","since"],"sources":["@wordpress/core-data/src/hooks/use-resource-permissions.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { store as coreStore } from '../';\nimport { Status } from './constants';\nimport useQuerySelect from './use-query-select';\n\ninterface GlobalResourcePermissionsResolution {\n\t/** Can the current user create new resources of this type? */\n\tcanCreate: boolean;\n}\ninterface SpecificResourcePermissionsResolution {\n\t/** Can the current user update resources of this type? */\n\tcanUpdate: boolean;\n\t/** Can the current user delete resources of this type? */\n\tcanDelete: boolean;\n}\ninterface ResolutionDetails {\n\t/** Resolution status */\n\tstatus: Status;\n\t/**\n\t * Is the data still being resolved?\n\t */\n\tisResolving: boolean;\n}\n\n/**\n * Is the data resolved by now?\n */\ntype HasResolved = boolean;\n\ntype ResourcePermissionsResolution< IdType > = [\n\tHasResolved,\n\tResolutionDetails &\n\t\tGlobalResourcePermissionsResolution &\n\t\t( IdType extends void ? SpecificResourcePermissionsResolution : {} ),\n];\n\n/**\n * Resolves resource permissions.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param resource The resource in question, e.g. media.\n * @param id ID of a specific resource entry, if needed, e.g. 10.\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function PagesList() {\n * const { canCreate, isResolving } = useResourcePermissions( 'pages' );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PagesList />\n * ```\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function Page({ pageId }) {\n * const {\n * canCreate,\n * canUpdate,\n * canDelete,\n * isResolving\n * } = useResourcePermissions( 'pages', pageId );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * {canUpdate ? (<button>Edit page</button>) : false}\n * {canDelete ? (<button>Delete page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <Page pageId={ 15 } />\n * ```\n *\n * In the above example, when `PagesList` is rendered into an\n * application, the appropriate permissions and the resolution details will be retrieved from\n * the store state using `canUser()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template IdType\n */\nexport default function useResourcePermissions< IdType = void >(\n\tresource: string,\n\tid?: IdType\n): ResourcePermissionsResolution< IdType > {\n\treturn useQuerySelect(\n\t\t( resolve ) => {\n\t\t\tconst { canUser } = resolve( coreStore );\n\t\t\tconst create = canUser( 'create', resource );\n\t\t\tif ( ! id ) {\n\t\t\t\tconst read = canUser( 'read', resource );\n\n\t\t\t\tconst isResolving = create.isResolving || read.isResolving;\n\t\t\t\tconst hasResolved = create.hasResolved && read.hasResolved;\n\t\t\t\tlet status = Status.Idle;\n\t\t\t\tif ( isResolving ) {\n\t\t\t\t\tstatus = Status.Resolving;\n\t\t\t\t} else if ( hasResolved ) {\n\t\t\t\t\tstatus = Status.Success;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\tstatus,\n\t\t\t\t\tisResolving,\n\t\t\t\t\thasResolved,\n\t\t\t\t\tcanCreate: create.hasResolved && create.data,\n\t\t\t\t\tcanRead: read.hasResolved && read.data,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst read = canUser( 'read', resource, id );\n\t\t\tconst update = canUser( 'update', resource, id );\n\t\t\tconst _delete = canUser( 'delete', resource, id );\n\t\t\tconst isResolving =\n\t\t\t\tread.isResolving ||\n\t\t\t\tcreate.isResolving ||\n\t\t\t\tupdate.isResolving ||\n\t\t\t\t_delete.isResolving;\n\t\t\tconst hasResolved =\n\t\t\t\tread.hasResolved &&\n\t\t\t\tcreate.hasResolved &&\n\t\t\t\tupdate.hasResolved &&\n\t\t\t\t_delete.hasResolved;\n\n\t\t\tlet status = Status.Idle;\n\t\t\tif ( isResolving ) {\n\t\t\t\tstatus = Status.Resolving;\n\t\t\t} else if ( hasResolved ) {\n\t\t\t\tstatus = Status.Success;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstatus,\n\t\t\t\tisResolving,\n\t\t\t\thasResolved,\n\t\t\t\tcanRead: hasResolved && read.data,\n\t\t\t\tcanCreate: hasResolved && create.data,\n\t\t\t\tcanUpdate: hasResolved && update.data,\n\t\t\t\tcanDelete: hasResolved && _delete.data,\n\t\t\t};\n\t\t},\n\t\t[ resource, id ]\n\t);\n}\n\nexport function __experimentalUseResourcePermissions(\n\tresource: string,\n\tid?: unknown\n) {\n\tdeprecated( `wp.data.__experimentalUseResourcePermissions`, {\n\t\talternative: 'wp.data.useResourcePermissions',\n\t\tsince: '6.1',\n\t} );\n\treturn useResourcePermissions( resource, id );\n}\n"],"mappings":";;;;;;;;AAG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mBAAU,EAAG,8CAA6C,EAAE;IAC3DC,WAAW,EAAE,gCAAgC;IAC7CC,KAAK,EAAE;EACR,CAAE,CAAC;EACH,OAAO1B,sBAAsB,CAAEC,QAAQ,EAAEC,EAAG,CAAC;AAC9C","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["_deprecated","_interopRequireDefault","require","_warning","_","_constants","_useQuerySelect","useResourcePermissions","resource","id","isEntity","resourceAsString","JSON","stringify","globalThis","SCRIPT_DEBUG","warning","useQuerySelect","resolve","hasId","canUser","coreStore","create","kind","name","read","isResolving","hasResolved","status","Status","Idle","Resolving","Success","canCreate","data","canRead","update","_delete","canUpdate","canDelete","_default","exports","default","__experimentalUseResourcePermissions","deprecated","alternative","since"],"sources":["@wordpress/core-data/src/hooks/use-resource-permissions.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\nimport warning from '@wordpress/warning';\n\n/**\n * Internal dependencies\n */\nimport { store as coreStore } from '../';\nimport { Status } from './constants';\nimport useQuerySelect from './use-query-select';\n\ninterface GlobalResourcePermissionsResolution {\n\t/** Can the current user create new resources of this type? */\n\tcanCreate: boolean;\n}\ninterface SpecificResourcePermissionsResolution {\n\t/** Can the current user update resources of this type? */\n\tcanUpdate: boolean;\n\t/** Can the current user delete resources of this type? */\n\tcanDelete: boolean;\n}\ninterface ResolutionDetails {\n\t/** Resolution status */\n\tstatus: Status;\n\t/**\n\t * Is the data still being resolved?\n\t */\n\tisResolving: boolean;\n}\n\n/**\n * Is the data resolved by now?\n */\ntype HasResolved = boolean;\n\ntype ResourcePermissionsResolution< IdType > = [\n\tHasResolved,\n\tResolutionDetails &\n\t\tGlobalResourcePermissionsResolution &\n\t\t( IdType extends void ? SpecificResourcePermissionsResolution : {} ),\n];\n\ntype EntityResource = { kind: string; name: string; id?: string | number };\n\nfunction useResourcePermissions< IdType = void >(\n\tresource: string,\n\tid?: IdType\n): ResourcePermissionsResolution< IdType >;\n\nfunction useResourcePermissions< IdType = void >(\n\tresource: EntityResource,\n\tid?: never\n): ResourcePermissionsResolution< IdType >;\n\n/**\n * Resolves resource permissions.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`\n * or REST base as a string - `media`.\n * @param id Optional ID of the resource to check, e.g. 10. Note: This argument is discouraged\n * when using an entity object as a resource to check permissions and will be ignored.\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function PagesList() {\n * const { canCreate, isResolving } = useResourcePermissions( { kind: 'postType', name: 'page' } );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PagesList />\n * ```\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function Page({ pageId }) {\n * const {\n * canCreate,\n * canUpdate,\n * canDelete,\n * isResolving\n * } = useResourcePermissions( { kind: 'postType', name: 'page', id: pageId } );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * {canUpdate ? (<button>Edit page</button>) : false}\n * {canDelete ? (<button>Delete page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <Page pageId={ 15 } />\n * ```\n *\n * In the above example, when `PagesList` is rendered into an\n * application, the appropriate permissions and the resolution details will be retrieved from\n * the store state using `canUser()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template IdType\n */\nfunction useResourcePermissions< IdType = void >(\n\tresource: string | EntityResource,\n\tid?: IdType\n): ResourcePermissionsResolution< IdType > {\n\t// Serialize `resource` to a string that can be safely used as a React dep.\n\t// We can't just pass `resource` as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst isEntity = typeof resource === 'object';\n\tconst resourceAsString = isEntity ? JSON.stringify( resource ) : resource;\n\n\tif ( isEntity && typeof id !== 'undefined' ) {\n\t\twarning(\n\t\t\t`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`\n\t\t);\n\t}\n\n\treturn useQuerySelect(\n\t\t( resolve ) => {\n\t\t\tconst hasId = isEntity ? !! resource.id : !! id;\n\t\t\tconst { canUser } = resolve( coreStore );\n\t\t\tconst create = canUser(\n\t\t\t\t'create',\n\t\t\t\tisEntity\n\t\t\t\t\t? { kind: resource.kind, name: resource.name }\n\t\t\t\t\t: resource\n\t\t\t);\n\n\t\t\tif ( ! hasId ) {\n\t\t\t\tconst read = canUser( 'read', resource );\n\n\t\t\t\tconst isResolving = create.isResolving || read.isResolving;\n\t\t\t\tconst hasResolved = create.hasResolved && read.hasResolved;\n\t\t\t\tlet status = Status.Idle;\n\t\t\t\tif ( isResolving ) {\n\t\t\t\t\tstatus = Status.Resolving;\n\t\t\t\t} else if ( hasResolved ) {\n\t\t\t\t\tstatus = Status.Success;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\tstatus,\n\t\t\t\t\tisResolving,\n\t\t\t\t\thasResolved,\n\t\t\t\t\tcanCreate: create.hasResolved && create.data,\n\t\t\t\t\tcanRead: read.hasResolved && read.data,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst read = canUser( 'read', resource, id );\n\t\t\tconst update = canUser( 'update', resource, id );\n\t\t\tconst _delete = canUser( 'delete', resource, id );\n\t\t\tconst isResolving =\n\t\t\t\tread.isResolving ||\n\t\t\t\tcreate.isResolving ||\n\t\t\t\tupdate.isResolving ||\n\t\t\t\t_delete.isResolving;\n\t\t\tconst hasResolved =\n\t\t\t\tread.hasResolved &&\n\t\t\t\tcreate.hasResolved &&\n\t\t\t\tupdate.hasResolved &&\n\t\t\t\t_delete.hasResolved;\n\n\t\t\tlet status = Status.Idle;\n\t\t\tif ( isResolving ) {\n\t\t\t\tstatus = Status.Resolving;\n\t\t\t} else if ( hasResolved ) {\n\t\t\t\tstatus = Status.Success;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstatus,\n\t\t\t\tisResolving,\n\t\t\t\thasResolved,\n\t\t\t\tcanRead: hasResolved && read.data,\n\t\t\t\tcanCreate: hasResolved && create.data,\n\t\t\t\tcanUpdate: hasResolved && update.data,\n\t\t\t\tcanDelete: hasResolved && _delete.data,\n\t\t\t};\n\t\t},\n\t\t[ resourceAsString, id ]\n\t);\n}\n\nexport default useResourcePermissions;\n\nexport function __experimentalUseResourcePermissions(\n\tresource: string,\n\tid?: unknown\n) {\n\tdeprecated( `wp.data.__experimentalUseResourcePermissions`, {\n\t\talternative: 'wp.data.useResourcePermissions',\n\t\tsince: '6.1',\n\t} );\n\treturn useResourcePermissions( resource, id );\n}\n"],"mappings":";;;;;;;;AAGA,IAAAA,WAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,QAAA,GAAAF,sBAAA,CAAAC,OAAA;AAKA,IAAAE,CAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AACA,IAAAI,eAAA,GAAAL,sBAAA,CAAAC,OAAA;AAXA;AACA;AACA;;AAIA;AACA;AACA;;AAwBA;AACA;AACA;;AAsBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASK,sBAAsBA,CAC9BC,QAAiC,EACjCC,EAAW,EAC+B;EAC1C;EACA;EACA;EACA;EACA,MAAMC,QAAQ,GAAG,OAAOF,QAAQ,KAAK,QAAQ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C;EACH,OAAOvC,sBAAsB,CAAEC,QAAQ,EAAEC,EAAG,CAAC;AAC9C","ignoreList":[]}
|
package/build/resolvers.js
CHANGED
|
@@ -68,7 +68,8 @@ const getCurrentUser = () => async ({
|
|
|
68
68
|
exports.getCurrentUser = getCurrentUser;
|
|
69
69
|
const getEntityRecord = (kind, name, key = '', query) => async ({
|
|
70
70
|
select,
|
|
71
|
-
dispatch
|
|
71
|
+
dispatch,
|
|
72
|
+
registry
|
|
72
73
|
}) => {
|
|
73
74
|
const configs = await dispatch((0, _entities.getOrLoadEntitiesConfig)(kind, name));
|
|
74
75
|
const entityConfig = configs.find(config => config.name === name && config.kind === kind);
|
|
@@ -140,10 +141,28 @@ const getEntityRecord = (kind, name, key = '', query) => async ({
|
|
|
140
141
|
return;
|
|
141
142
|
}
|
|
142
143
|
}
|
|
143
|
-
const
|
|
144
|
-
path
|
|
144
|
+
const response = await (0, _apiFetch.default)({
|
|
145
|
+
path,
|
|
146
|
+
parse: false
|
|
147
|
+
});
|
|
148
|
+
const record = await response.json();
|
|
149
|
+
const permissions = (0, _utils.getUserPermissionsFromResponse)(response);
|
|
150
|
+
registry.batch(() => {
|
|
151
|
+
dispatch.receiveEntityRecords(kind, name, record, query);
|
|
152
|
+
for (const action of _utils.ALLOWED_RESOURCE_ACTIONS) {
|
|
153
|
+
const permissionKey = (0, _utils.getUserPermissionCacheKey)(action, {
|
|
154
|
+
kind,
|
|
155
|
+
name,
|
|
156
|
+
id: key
|
|
157
|
+
});
|
|
158
|
+
dispatch.receiveUserPermission(permissionKey, permissions[action]);
|
|
159
|
+
dispatch.finishResolution('canUser', [action, {
|
|
160
|
+
kind,
|
|
161
|
+
name,
|
|
162
|
+
id: key
|
|
163
|
+
}]);
|
|
164
|
+
}
|
|
145
165
|
});
|
|
146
|
-
dispatch.receiveEntityRecords(kind, name, record, query);
|
|
147
166
|
}
|
|
148
167
|
} finally {
|
|
149
168
|
dispatch.__unstableReleaseStoreLock(lock);
|
|
@@ -234,16 +253,7 @@ const getEntityRecords = (kind, name, query = {}) => async ({
|
|
|
234
253
|
if (!query?._fields && !query.context) {
|
|
235
254
|
const key = entityConfig.key || _entities.DEFAULT_ENTITY_KEY;
|
|
236
255
|
const resolutionsArgs = records.filter(record => record?.[key]).map(record => [kind, name, record[key]]);
|
|
237
|
-
dispatch(
|
|
238
|
-
type: 'START_RESOLUTIONS',
|
|
239
|
-
selectorName: 'getEntityRecord',
|
|
240
|
-
args: resolutionsArgs
|
|
241
|
-
});
|
|
242
|
-
dispatch({
|
|
243
|
-
type: 'FINISH_RESOLUTIONS',
|
|
244
|
-
selectorName: 'getEntityRecord',
|
|
245
|
-
args: resolutionsArgs
|
|
246
|
-
});
|
|
256
|
+
dispatch.finishResolutions('getEntityRecord', resolutionsArgs);
|
|
247
257
|
}
|
|
248
258
|
dispatch.__unstableReleaseStoreLock(lock);
|
|
249
259
|
});
|
|
@@ -300,27 +310,40 @@ const getEmbedPreview = url => async ({
|
|
|
300
310
|
* Checks whether the current user can perform the given action on the given
|
|
301
311
|
* REST resource.
|
|
302
312
|
*
|
|
303
|
-
* @param {string}
|
|
304
|
-
*
|
|
305
|
-
* @param {string}
|
|
306
|
-
*
|
|
313
|
+
* @param {string} requestedAction Action to check. One of: 'create', 'read', 'update',
|
|
314
|
+
* 'delete'.
|
|
315
|
+
* @param {string|Object} resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`
|
|
316
|
+
* or REST base as a string - `media`.
|
|
317
|
+
* @param {?string} id ID of the rest resource to check.
|
|
307
318
|
*/
|
|
308
319
|
exports.getEmbedPreview = getEmbedPreview;
|
|
309
320
|
const canUser = (requestedAction, resource, id) => async ({
|
|
310
321
|
dispatch,
|
|
311
322
|
registry
|
|
312
323
|
}) => {
|
|
324
|
+
if (!_utils.ALLOWED_RESOURCE_ACTIONS.includes(requestedAction)) {
|
|
325
|
+
throw new Error(`'${requestedAction}' is not a valid action.`);
|
|
326
|
+
}
|
|
327
|
+
let resourcePath = null;
|
|
328
|
+
if (typeof resource === 'object') {
|
|
329
|
+
if (!resource.kind || !resource.name) {
|
|
330
|
+
throw new Error('The entity resource object is not valid.');
|
|
331
|
+
}
|
|
332
|
+
const configs = await dispatch((0, _entities.getOrLoadEntitiesConfig)(resource.kind, resource.name));
|
|
333
|
+
const entityConfig = configs.find(config => config.name === resource.name && config.kind === resource.kind);
|
|
334
|
+
if (!entityConfig) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
resourcePath = entityConfig.baseURL + (resource.id ? '/' + resource.id : '');
|
|
338
|
+
} else {
|
|
339
|
+
resourcePath = `/wp/v2/${resource}` + (id ? '/' + id : '');
|
|
340
|
+
}
|
|
313
341
|
const {
|
|
314
342
|
hasStartedResolution
|
|
315
343
|
} = registry.select(_name.STORE_NAME);
|
|
316
|
-
const resourcePath = id ? `${resource}/${id}` : resource;
|
|
317
|
-
const retrievedActions = ['create', 'read', 'update', 'delete'];
|
|
318
|
-
if (!retrievedActions.includes(requestedAction)) {
|
|
319
|
-
throw new Error(`'${requestedAction}' is not a valid action.`);
|
|
320
|
-
}
|
|
321
344
|
|
|
322
345
|
// Prevent resolving the same resource twice.
|
|
323
|
-
for (const relatedAction of
|
|
346
|
+
for (const relatedAction of _utils.ALLOWED_RESOURCE_ACTIONS) {
|
|
324
347
|
if (relatedAction === requestedAction) {
|
|
325
348
|
continue;
|
|
326
349
|
}
|
|
@@ -332,7 +355,7 @@ const canUser = (requestedAction, resource, id) => async ({
|
|
|
332
355
|
let response;
|
|
333
356
|
try {
|
|
334
357
|
response = await (0, _apiFetch.default)({
|
|
335
|
-
path:
|
|
358
|
+
path: resourcePath,
|
|
336
359
|
method: 'OPTIONS',
|
|
337
360
|
parse: false
|
|
338
361
|
});
|
|
@@ -341,25 +364,16 @@ const canUser = (requestedAction, resource, id) => async ({
|
|
|
341
364
|
// 5xx). The previously determined isAllowed value will remain in the store.
|
|
342
365
|
return;
|
|
343
366
|
}
|
|
344
|
-
|
|
345
|
-
// Optional chaining operator is used here because the API requests don't
|
|
346
|
-
// return the expected result in the native version. Instead, API requests
|
|
347
|
-
// only return the result, without including response properties like the headers.
|
|
348
|
-
const allowHeader = response.headers?.get('allow');
|
|
349
|
-
const allowedMethods = allowHeader?.allow || allowHeader || '';
|
|
350
|
-
const permissions = {};
|
|
351
|
-
const methods = {
|
|
352
|
-
create: 'POST',
|
|
353
|
-
read: 'GET',
|
|
354
|
-
update: 'PUT',
|
|
355
|
-
delete: 'DELETE'
|
|
356
|
-
};
|
|
357
|
-
for (const [actionName, methodName] of Object.entries(methods)) {
|
|
358
|
-
permissions[actionName] = allowedMethods.includes(methodName);
|
|
359
|
-
}
|
|
367
|
+
const permissions = (0, _utils.getUserPermissionsFromResponse)(response);
|
|
360
368
|
registry.batch(() => {
|
|
361
|
-
for (const action of
|
|
362
|
-
|
|
369
|
+
for (const action of _utils.ALLOWED_RESOURCE_ACTIONS) {
|
|
370
|
+
const key = (0, _utils.getUserPermissionCacheKey)(action, resource, id);
|
|
371
|
+
dispatch.receiveUserPermission(key, permissions[action]);
|
|
372
|
+
|
|
373
|
+
// Mark related action resolutions as finished.
|
|
374
|
+
if (action !== requestedAction) {
|
|
375
|
+
dispatch.finishResolution('canUser', [action, resource, id]);
|
|
376
|
+
}
|
|
363
377
|
}
|
|
364
378
|
});
|
|
365
379
|
};
|
|
@@ -376,13 +390,11 @@ exports.canUser = canUser;
|
|
|
376
390
|
const canUserEditEntityRecord = (kind, name, recordId) => async ({
|
|
377
391
|
dispatch
|
|
378
392
|
}) => {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
const resource = entityConfig.__unstable_rest_base;
|
|
385
|
-
await dispatch(canUser('update', resource, recordId));
|
|
393
|
+
await dispatch(canUser('update', {
|
|
394
|
+
kind,
|
|
395
|
+
name,
|
|
396
|
+
id: recordId
|
|
397
|
+
}));
|
|
386
398
|
};
|
|
387
399
|
|
|
388
400
|
/**
|
|
@@ -675,16 +687,8 @@ const getRevisions = (kind, name, recordKey, query = {}) => async ({
|
|
|
675
687
|
if (!query?._fields && !query.context) {
|
|
676
688
|
const key = entityConfig.key || _entities.DEFAULT_ENTITY_KEY;
|
|
677
689
|
const resolutionsArgs = records.filter(record => record[key]).map(record => [kind, name, recordKey, record[key]]);
|
|
678
|
-
dispatch(
|
|
679
|
-
|
|
680
|
-
selectorName: 'getRevision',
|
|
681
|
-
args: resolutionsArgs
|
|
682
|
-
});
|
|
683
|
-
dispatch({
|
|
684
|
-
type: 'FINISH_RESOLUTIONS',
|
|
685
|
-
selectorName: 'getRevision',
|
|
686
|
-
args: resolutionsArgs
|
|
687
|
-
});
|
|
690
|
+
dispatch.startResolutions('getRevision', resolutionsArgs);
|
|
691
|
+
dispatch.finishResolutions('getRevision', resolutionsArgs);
|
|
688
692
|
}
|
|
689
693
|
}
|
|
690
694
|
};
|