@wordpress/core-data 7.2.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.
Files changed (131) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +54 -6
  3. package/build/actions.js +6 -6
  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/entity-types/menu-location.js.map +1 -1
  10. package/build/entity-types/settings.js.map +1 -1
  11. package/build/entity-types/theme.js.map +1 -1
  12. package/build/entity-types/widget-type.js.map +1 -1
  13. package/build/hooks/index.js +22 -0
  14. package/build/hooks/index.js.map +1 -1
  15. package/build/hooks/use-entity-block-editor.js +140 -0
  16. package/build/hooks/use-entity-block-editor.js.map +1 -0
  17. package/build/hooks/use-entity-id.js +28 -0
  18. package/build/hooks/use-entity-id.js.map +1 -0
  19. package/build/hooks/use-entity-prop.js +65 -0
  20. package/build/hooks/use-entity-prop.js.map +1 -0
  21. package/build/hooks/use-resource-permissions.js +25 -8
  22. package/build/hooks/use-resource-permissions.js.map +1 -1
  23. package/build/resolvers.js +81 -70
  24. package/build/resolvers.js.map +1 -1
  25. package/build/selectors.js +23 -9
  26. package/build/selectors.js.map +1 -1
  27. package/build/utils/index.js +19 -0
  28. package/build/utils/index.js.map +1 -1
  29. package/build/utils/user-permissions.js +32 -0
  30. package/build/utils/user-permissions.js.map +1 -0
  31. package/build-module/actions.js +6 -6
  32. package/build-module/actions.js.map +1 -1
  33. package/build-module/entity-context.js +6 -0
  34. package/build-module/entity-context.js.map +1 -0
  35. package/build-module/entity-provider.js +3 -185
  36. package/build-module/entity-provider.js.map +1 -1
  37. package/build-module/entity-types/menu-location.js.map +1 -1
  38. package/build-module/entity-types/settings.js.map +1 -1
  39. package/build-module/entity-types/theme.js.map +1 -1
  40. package/build-module/entity-types/widget-type.js.map +1 -1
  41. package/build-module/hooks/index.js +3 -0
  42. package/build-module/hooks/index.js.map +1 -1
  43. package/build-module/hooks/use-entity-block-editor.js +132 -0
  44. package/build-module/hooks/use-entity-block-editor.js.map +1 -0
  45. package/build-module/hooks/use-entity-id.js +22 -0
  46. package/build-module/hooks/use-entity-id.js.map +1 -0
  47. package/build-module/hooks/use-entity-prop.js +58 -0
  48. package/build-module/hooks/use-entity-prop.js.map +1 -0
  49. package/build-module/hooks/use-resource-permissions.js +25 -8
  50. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  51. package/build-module/resolvers.js +82 -71
  52. package/build-module/resolvers.js.map +1 -1
  53. package/build-module/selectors.js +24 -10
  54. package/build-module/selectors.js.map +1 -1
  55. package/build-module/utils/index.js +1 -0
  56. package/build-module/utils/index.js.map +1 -1
  57. package/build-module/utils/user-permissions.js +24 -0
  58. package/build-module/utils/user-permissions.js.map +1 -0
  59. package/build-types/actions.d.ts +3 -3
  60. package/build-types/actions.d.ts.map +1 -1
  61. package/build-types/batch/create-batch.d.ts.map +1 -1
  62. package/build-types/entities.d.ts.map +1 -1
  63. package/build-types/entity-context.d.ts +2 -0
  64. package/build-types/entity-context.d.ts.map +1 -0
  65. package/build-types/entity-provider.d.ts +0 -48
  66. package/build-types/entity-provider.d.ts.map +1 -1
  67. package/build-types/entity-types/menu-location.d.ts.map +1 -1
  68. package/build-types/entity-types/settings.d.ts.map +1 -1
  69. package/build-types/entity-types/theme.d.ts.map +1 -1
  70. package/build-types/entity-types/widget-type.d.ts.map +1 -1
  71. package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -1
  72. package/build-types/hooks/index.d.ts +3 -0
  73. package/build-types/hooks/index.d.ts.map +1 -1
  74. package/build-types/hooks/use-entity-block-editor.d.ts +22 -0
  75. package/build-types/hooks/use-entity-block-editor.d.ts.map +1 -0
  76. package/build-types/hooks/use-entity-id.d.ts +9 -0
  77. package/build-types/hooks/use-entity-id.d.ts.map +1 -0
  78. package/build-types/hooks/use-entity-prop.d.ts +19 -0
  79. package/build-types/hooks/use-entity-prop.d.ts.map +1 -0
  80. package/build-types/hooks/use-resource-permissions.d.ts +8 -70
  81. package/build-types/hooks/use-resource-permissions.d.ts.map +1 -1
  82. package/build-types/index.d.ts +35 -32
  83. package/build-types/index.d.ts.map +1 -1
  84. package/build-types/locks/reducer.d.ts +1 -1
  85. package/build-types/locks/reducer.d.ts.map +1 -1
  86. package/build-types/queried-data/actions.d.ts +1 -1
  87. package/build-types/queried-data/actions.d.ts.map +1 -1
  88. package/build-types/queried-data/get-query-parts.d.ts.map +1 -1
  89. package/build-types/queried-data/reducer.d.ts +1 -1
  90. package/build-types/queried-data/reducer.d.ts.map +1 -1
  91. package/build-types/queried-data/selectors.d.ts +0 -1
  92. package/build-types/queried-data/selectors.d.ts.map +1 -1
  93. package/build-types/reducer.d.ts +13 -13
  94. package/build-types/reducer.d.ts.map +1 -1
  95. package/build-types/resolvers.d.ts +3 -2
  96. package/build-types/resolvers.d.ts.map +1 -1
  97. package/build-types/selectors.d.ts +11 -6
  98. package/build-types/selectors.d.ts.map +1 -1
  99. package/build-types/utils/get-nested-value.d.ts.map +1 -1
  100. package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -1
  101. package/build-types/utils/if-matching-action.d.ts +1 -1
  102. package/build-types/utils/index.d.ts +1 -0
  103. package/build-types/utils/on-sub-key.d.ts +1 -1
  104. package/build-types/utils/replace-action.d.ts +1 -1
  105. package/build-types/utils/set-nested-value.d.ts.map +1 -1
  106. package/build-types/utils/user-permissions.d.ts +4 -0
  107. package/build-types/utils/user-permissions.d.ts.map +1 -0
  108. package/package.json +18 -17
  109. package/src/actions.js +6 -6
  110. package/src/entity-context.js +6 -0
  111. package/src/entity-provider.js +2 -211
  112. package/src/entity-types/menu-location.ts +1 -0
  113. package/src/entity-types/settings.ts +1 -0
  114. package/src/entity-types/theme.ts +1 -0
  115. package/src/entity-types/widget-type.ts +1 -0
  116. package/src/hooks/index.ts +3 -0
  117. package/src/hooks/test/use-entity-record.js +5 -3
  118. package/src/hooks/test/use-resource-permissions.js +96 -5
  119. package/src/hooks/use-entity-block-editor.js +148 -0
  120. package/src/hooks/use-entity-id.js +21 -0
  121. package/src/hooks/use-entity-prop.js +60 -0
  122. package/src/hooks/use-resource-permissions.ts +46 -9
  123. package/src/resolvers.js +102 -78
  124. package/src/selectors.ts +24 -9
  125. package/src/test/entity-provider.js +6 -2
  126. package/src/test/resolvers.js +221 -50
  127. package/src/test/selectors.js +18 -55
  128. package/src/utils/index.js +5 -0
  129. package/src/utils/user-permissions.js +39 -0
  130. package/tsconfig.json +2 -2
  131. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,132 @@
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
+ const EMPTY_ARRAY = [];
15
+ const parsedBlocksCache = new WeakMap();
16
+
17
+ /**
18
+ * Hook that returns block content getters and setters for
19
+ * the nearest provided entity of the specified type.
20
+ *
21
+ * The return value has the shape `[ blocks, onInput, onChange ]`.
22
+ * `onInput` is for block changes that don't create undo levels
23
+ * or dirty the post, non-persistent changes, and `onChange` is for
24
+ * persistent changes. They map directly to the props of a
25
+ * `BlockEditorProvider` and are intended to be used with it,
26
+ * or similar components or hooks.
27
+ *
28
+ * @param {string} kind The entity kind.
29
+ * @param {string} name The entity name.
30
+ * @param {Object} options
31
+ * @param {string} [options.id] An entity ID to use instead of the context-provided one.
32
+ *
33
+ * @return {[unknown[], Function, Function]} The block array and setters.
34
+ */
35
+ export default function useEntityBlockEditor(kind, name, {
36
+ id: _id
37
+ } = {}) {
38
+ const providerId = useEntityId(kind, name);
39
+ const id = _id !== null && _id !== void 0 ? _id : providerId;
40
+ const {
41
+ getEntityRecord,
42
+ getEntityRecordEdits
43
+ } = useSelect(STORE_NAME);
44
+ const {
45
+ content,
46
+ editedBlocks,
47
+ meta
48
+ } = useSelect(select => {
49
+ if (!id) {
50
+ return {};
51
+ }
52
+ const {
53
+ getEditedEntityRecord
54
+ } = select(STORE_NAME);
55
+ const editedRecord = getEditedEntityRecord(kind, name, id);
56
+ return {
57
+ editedBlocks: editedRecord.blocks,
58
+ content: editedRecord.content,
59
+ meta: editedRecord.meta
60
+ };
61
+ }, [kind, name, id]);
62
+ const {
63
+ __unstableCreateUndoLevel,
64
+ editEntityRecord
65
+ } = useDispatch(STORE_NAME);
66
+ const blocks = useMemo(() => {
67
+ if (!id) {
68
+ return undefined;
69
+ }
70
+ if (editedBlocks) {
71
+ return editedBlocks;
72
+ }
73
+ if (!content || typeof content !== 'string') {
74
+ return EMPTY_ARRAY;
75
+ }
76
+
77
+ // If there's an edit, cache the parsed blocks by the edit.
78
+ // If not, cache by the original enity record.
79
+ const edits = getEntityRecordEdits(kind, name, id);
80
+ const isUnedited = !edits || !Object.keys(edits).length;
81
+ const cackeKey = isUnedited ? getEntityRecord(kind, name, id) : edits;
82
+ let _blocks = parsedBlocksCache.get(cackeKey);
83
+ if (!_blocks) {
84
+ _blocks = parse(content);
85
+ parsedBlocksCache.set(cackeKey, _blocks);
86
+ }
87
+ return _blocks;
88
+ }, [kind, name, id, editedBlocks, content, getEntityRecord, getEntityRecordEdits]);
89
+ const updateFootnotes = useCallback(_blocks => updateFootnotesFromMeta(_blocks, meta), [meta]);
90
+ const onChange = useCallback((newBlocks, options) => {
91
+ const noChange = blocks === newBlocks;
92
+ if (noChange) {
93
+ return __unstableCreateUndoLevel(kind, name, id);
94
+ }
95
+ const {
96
+ selection,
97
+ ...rest
98
+ } = options;
99
+
100
+ // We create a new function here on every persistent edit
101
+ // to make sure the edit makes the post dirty and creates
102
+ // a new undo level.
103
+ const edits = {
104
+ selection,
105
+ content: ({
106
+ blocks: blocksForSerialization = []
107
+ }) => __unstableSerializeAndClean(blocksForSerialization),
108
+ ...updateFootnotes(newBlocks)
109
+ };
110
+ editEntityRecord(kind, name, id, edits, {
111
+ isCached: false,
112
+ ...rest
113
+ });
114
+ }, [kind, name, id, blocks, updateFootnotes, __unstableCreateUndoLevel, editEntityRecord]);
115
+ const onInput = useCallback((newBlocks, options) => {
116
+ const {
117
+ selection,
118
+ ...rest
119
+ } = options;
120
+ const footnotesChanges = updateFootnotes(newBlocks);
121
+ const edits = {
122
+ selection,
123
+ ...footnotesChanges
124
+ };
125
+ editEntityRecord(kind, name, id, edits, {
126
+ isCached: true,
127
+ ...rest
128
+ });
129
+ }, [kind, name, id, updateFootnotes, editEntityRecord]);
130
+ return [blocks, onInput, onChange];
131
+ }
132
+ //# sourceMappingURL=use-entity-block-editor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["useCallback","useMemo","useDispatch","useSelect","parse","__unstableSerializeAndClean","STORE_NAME","useEntityId","updateFootnotesFromMeta","EMPTY_ARRAY","parsedBlocksCache","WeakMap","useEntityBlockEditor","kind","name","id","_id","providerId","getEntityRecord","getEntityRecordEdits","content","editedBlocks","meta","select","getEditedEntityRecord","editedRecord","blocks","__unstableCreateUndoLevel","editEntityRecord","undefined","edits","isUnedited","Object","keys","length","cackeKey","_blocks","get","set","updateFootnotes","onChange","newBlocks","options","noChange","selection","rest","blocksForSerialization","isCached","onInput","footnotesChanges"],"sources":["@wordpress/core-data/src/hooks/use-entity-block-editor.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport { parse, __unstableSerializeAndClean } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from '../name';\nimport useEntityId from './use-entity-id';\nimport { updateFootnotesFromMeta } from '../footnotes';\n\nconst EMPTY_ARRAY = [];\nconst parsedBlocksCache = new WeakMap();\n\n/**\n * Hook that returns block content getters and setters for\n * the nearest provided entity of the specified type.\n *\n * The return value has the shape `[ blocks, onInput, onChange ]`.\n * `onInput` is for block changes that don't create undo levels\n * or dirty the post, non-persistent changes, and `onChange` is for\n * persistent changes. They map directly to the props of a\n * `BlockEditorProvider` and are intended to be used with it,\n * or similar components or hooks.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {Object} options\n * @param {string} [options.id] An entity ID to use instead of the context-provided one.\n *\n * @return {[unknown[], Function, Function]} The block array and setters.\n */\nexport default function useEntityBlockEditor( kind, name, { id: _id } = {} ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\tconst { getEntityRecord, getEntityRecordEdits } = useSelect( STORE_NAME );\n\tconst { content, editedBlocks, meta } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! id ) {\n\t\t\t\treturn {};\n\t\t\t}\n\t\t\tconst { getEditedEntityRecord } = select( STORE_NAME );\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn {\n\t\t\t\teditedBlocks: editedRecord.blocks,\n\t\t\t\tcontent: editedRecord.content,\n\t\t\t\tmeta: editedRecord.meta,\n\t\t\t};\n\t\t},\n\t\t[ kind, name, id ]\n\t);\n\tconst { __unstableCreateUndoLevel, editEntityRecord } =\n\t\tuseDispatch( STORE_NAME );\n\n\tconst blocks = useMemo( () => {\n\t\tif ( ! id ) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif ( editedBlocks ) {\n\t\t\treturn editedBlocks;\n\t\t}\n\n\t\tif ( ! content || typeof content !== 'string' ) {\n\t\t\treturn EMPTY_ARRAY;\n\t\t}\n\n\t\t// If there's an edit, cache the parsed blocks by the edit.\n\t\t// If not, cache by the original enity record.\n\t\tconst edits = getEntityRecordEdits( kind, name, id );\n\t\tconst isUnedited = ! edits || ! Object.keys( edits ).length;\n\t\tconst cackeKey = isUnedited ? getEntityRecord( kind, name, id ) : edits;\n\t\tlet _blocks = parsedBlocksCache.get( cackeKey );\n\n\t\tif ( ! _blocks ) {\n\t\t\t_blocks = parse( content );\n\t\t\tparsedBlocksCache.set( cackeKey, _blocks );\n\t\t}\n\n\t\treturn _blocks;\n\t}, [\n\t\tkind,\n\t\tname,\n\t\tid,\n\t\teditedBlocks,\n\t\tcontent,\n\t\tgetEntityRecord,\n\t\tgetEntityRecordEdits,\n\t] );\n\n\tconst updateFootnotes = useCallback(\n\t\t( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),\n\t\t[ meta ]\n\t);\n\n\tconst onChange = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst noChange = blocks === newBlocks;\n\t\t\tif ( noChange ) {\n\t\t\t\treturn __unstableCreateUndoLevel( kind, name, id );\n\t\t\t}\n\t\t\tconst { selection, ...rest } = options;\n\n\t\t\t// We create a new function here on every persistent edit\n\t\t\t// to make sure the edit makes the post dirty and creates\n\t\t\t// a new undo level.\n\t\t\tconst edits = {\n\t\t\t\tselection,\n\t\t\t\tcontent: ( { blocks: blocksForSerialization = [] } ) =>\n\t\t\t\t\t__unstableSerializeAndClean( blocksForSerialization ),\n\t\t\t\t...updateFootnotes( newBlocks ),\n\t\t\t};\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: false,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[\n\t\t\tkind,\n\t\t\tname,\n\t\t\tid,\n\t\t\tblocks,\n\t\t\tupdateFootnotes,\n\t\t\t__unstableCreateUndoLevel,\n\t\t\teditEntityRecord,\n\t\t]\n\t);\n\n\tconst onInput = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst { selection, ...rest } = options;\n\t\t\tconst footnotesChanges = updateFootnotes( newBlocks );\n\t\t\tconst edits = { selection, ...footnotesChanges };\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: true,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, id, updateFootnotes, editEntityRecord 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;EAED,OAAO,CAAEF,MAAM,EAAEsB,OAAO,EAAER,QAAQ,CAAE;AACrC","ignoreList":[]}
@@ -0,0 +1,22 @@
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
+ }
22
+ //# sourceMappingURL=use-entity-id.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["useContext","EntityContext","useEntityId","kind","name","context"],"sources":["@wordpress/core-data/src/hooks/use-entity-id.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useContext } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { EntityContext } from '../entity-context';\n\n/**\n * Hook that returns the ID for the nearest\n * provided entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n */\nexport default function useEntityId( kind, name ) {\n\tconst context = useContext( EntityContext );\n\treturn context?.[ kind ]?.[ name ];\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAU,QAAQ,oBAAoB;;AAE/C;AACA;AACA;AACA,SAASC,aAAa,QAAQ,mBAAmB;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,WAAWA,CAAEC,IAAI,EAAEC,IAAI,EAAG;EACjD,MAAMC,OAAO,GAAGL,UAAU,CAAEC,aAAc,CAAC;EAC3C,OAAOI,OAAO,GAAIF,IAAI,CAAE,GAAIC,IAAI,CAAE;AACnC","ignoreList":[]}
@@ -0,0 +1,58 @@
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 !== null && _id !== void 0 ? _id : providerId;
33
+ const {
34
+ value,
35
+ fullValue
36
+ } = useSelect(select => {
37
+ const {
38
+ getEntityRecord,
39
+ getEditedEntityRecord
40
+ } = select(STORE_NAME);
41
+ const record = getEntityRecord(kind, name, id); // Trigger resolver.
42
+ const editedRecord = getEditedEntityRecord(kind, name, id);
43
+ return record && editedRecord ? {
44
+ value: editedRecord[prop],
45
+ fullValue: record[prop]
46
+ } : {};
47
+ }, [kind, name, id, prop]);
48
+ const {
49
+ editEntityRecord
50
+ } = useDispatch(STORE_NAME);
51
+ const setValue = useCallback(newValue => {
52
+ editEntityRecord(kind, name, id, {
53
+ [prop]: newValue
54
+ });
55
+ }, [editEntityRecord, kind, name, id, prop]);
56
+ return [value, setValue, fullValue];
57
+ }
58
+ //# sourceMappingURL=use-entity-prop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["useCallback","useDispatch","useSelect","STORE_NAME","useEntityId","useEntityProp","kind","name","prop","_id","providerId","id","value","fullValue","select","getEntityRecord","getEditedEntityRecord","record","editedRecord","editEntityRecord","setValue","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":"AAAA;AACA;AACA;AACA,SAASA,WAAW,QAAQ,oBAAoB;AAChD,SAASC,WAAW,EAAEC,SAAS,QAAQ,iBAAiB;;AAExD;AACA;AACA;AACA,SAASC,UAAU,QAAQ,SAAS;AACpC,OAAOC,WA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K,EAAEQ,QAAQ,EAAEP,SAAS,CAAE;AACtC","ignoreList":[]}
@@ -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
@@ -19,15 +20,17 @@ import useQuerySelect from './use-query-select';
19
20
  *
20
21
  * @since 6.1.0 Introduced in WordPress core.
21
22
  *
22
- * @param resource The resource in question, e.g. media.
23
- * @param id ID of a specific resource entry, if needed, e.g. 10.
23
+ * @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`
24
+ * or REST base as a string - `media`.
25
+ * @param id Optional ID of the resource to check, e.g. 10. Note: This argument is discouraged
26
+ * when using an entity object as a resource to check permissions and will be ignored.
24
27
  *
25
28
  * @example
26
29
  * ```js
27
30
  * import { useResourcePermissions } from '@wordpress/core-data';
28
31
  *
29
32
  * function PagesList() {
30
- * const { canCreate, isResolving } = useResourcePermissions( 'pages' );
33
+ * const { canCreate, isResolving } = useResourcePermissions( { kind: 'postType', name: 'page' } );
31
34
  *
32
35
  * if ( isResolving ) {
33
36
  * return 'Loading ...';
@@ -55,7 +58,7 @@ import useQuerySelect from './use-query-select';
55
58
  * canUpdate,
56
59
  * canDelete,
57
60
  * isResolving
58
- * } = useResourcePermissions( 'pages', pageId );
61
+ * } = useResourcePermissions( { kind: 'postType', name: 'page', id: pageId } );
59
62
  *
60
63
  * if ( isResolving ) {
61
64
  * return 'Loading ...';
@@ -82,13 +85,26 @@ import useQuerySelect from './use-query-select';
82
85
  * @return Entity records data.
83
86
  * @template IdType
84
87
  */
85
- export default function useResourcePermissions(resource, id) {
88
+ function useResourcePermissions(resource, id) {
89
+ // Serialize `resource` to a string that can be safely used as a React dep.
90
+ // We can't just pass `resource` as one of the deps, because if it is passed
91
+ // as an object literal, then it will be a different object on each call even
92
+ // if the values remain the same.
93
+ const isEntity = typeof resource === 'object';
94
+ const resourceAsString = isEntity ? JSON.stringify(resource) : resource;
95
+ if (isEntity && typeof id !== 'undefined') {
96
+ globalThis.SCRIPT_DEBUG === true ? warning(`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`) : void 0;
97
+ }
86
98
  return useQuerySelect(resolve => {
99
+ const hasId = isEntity ? !!resource.id : !!id;
87
100
  const {
88
101
  canUser
89
102
  } = resolve(coreStore);
90
- const create = canUser('create', resource);
91
- if (!id) {
103
+ const create = canUser('create', isEntity ? {
104
+ kind: resource.kind,
105
+ name: resource.name
106
+ } : resource);
107
+ if (!hasId) {
92
108
  const read = canUser('read', resource);
93
109
  const isResolving = create.isResolving || read.isResolving;
94
110
  const hasResolved = create.hasResolved && read.hasResolved;
@@ -126,8 +142,9 @@ export default function useResourcePermissions(resource, id) {
126
142
  canUpdate: hasResolved && update.data,
127
143
  canDelete: hasResolved && _delete.data
128
144
  };
129
- }, [resource, id]);
145
+ }, [resourceAsString, id]);
130
146
  }
147
+ export default useResourcePermissions;
131
148
  export function __experimentalUseResourcePermissions(resource, id) {
132
149
  deprecated(`wp.data.__experimentalUseResourcePermissions`, {
133
150
  alternative: 'wp.data.useResourcePermissions',
@@ -1 +1 @@
1
- {"version":3,"names":["deprecated","store","coreStore","Status","useQuerySelect","useResourcePermissions","resource","id","resolve","canUser","create","read","isResolving","hasResolved","status","Idle","Resolving","Success","canCreate","data","canRead","update","_delete","canUpdate","canDelete","__experimentalUseResourcePermissions","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":"AAAA;AACA;AACA;AACA,OAAOA,UAAU,MAAM,uBAAuB;;AAE9C;AACA;AACA;AACA,SAASC,KAAK,IAAIC,SAAS,QAAQ,KAAK;AACxC,SAASC,MAAM,QAAQ,aAAa;AACpC,OAAOC,cAAc,MAAM,oBAAoB;;AAqB/C;AACA;AACA;;AAUA;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,eAAe,SAASC,sBAAsBA,CAC7CC,QAAgB,EAChBC,EAAW,EAC+B;EAC1C,OAAOH,cAAc,CAClBI,OAAO,IAAM;IACd,MAAM;MAAEC;IAAQ,CAAC,GAAGD,OAAO,CAAEN,SAAU,CAAC;IACxC,MAAMQ,MAAM,GAAGD,OAAO,CAAE,QAAQ,EAAEH,QAAS,CAAC;IAC5C,IAAK,CAAEC,EAAE,EAAG;MACX,MAAMI,IAAI,GAAGF,OAAO,CAAE,MAAM,EAAEH,QAAS,CAAC;MAExC,MAAMM,WAAW,GAAGF,MAAM,CAACE,WAAW,IAAID,IAAI,CAACC,WAAW;MAC1D,MAAMC,WAAW,GAAGH,MAAM,CAACG,WAAW,IAAIF,IAAI,CAACE,WAAW;MAC1D,IAAIC,MAAM,GAAGX,MAAM,CAACY,IAAI;MACxB,IAAKH,WAAW,EAAG;QAClBE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AC;EACF,CAAC,EACD,CAAEb,QAAQ,EAAEC,EAAE,CACf,CAAC;AACF;AAEA,OAAO,SAASkB,oCAAoCA,CACnDnB,QAAgB,EAChBC,EAAY,EACX;EACDP,UAAU,CAAG,8CAA6C,EAAE;IAC3D0B,WAAW,EAAE,gCAAgC;IAC7CC,KAAK,EAAE;EACR,CAAE,CAAC;EACH,OAAOtB,sBAAsB,CAAEC,QAAQ,EAAEC,EAAG,CAAC;AAC9C","ignoreList":[]}
1
+ {"version":3,"names":["deprecated","warning","store","coreStore","Status","useQuerySelect","useResourcePermissions","resource","id","isEntity","resourceAsString","JSON","stringify","globalThis","SCRIPT_DEBUG","resolve","hasId","canUser","create","kind","name","read","isResolving","hasResolved","status","Idle","Resolving","Success","canCreate","data","canRead","update","_delete","canUpdate","canDelete","__experimentalUseResourcePermissions","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":"AAAA;AACA;AACA;AACA,OAAOA,UAAU,MAAM,uBAAuB;AAC9C,OAAOC,OAAO,MAAM,oBAAoB;;AAExC;AACA;AACA;AACA,SAASC,KAAK,IAAIC,SAAS,QAAQ,KAAK;AACxC,SAASC,MAAM,QAAQ,aAAa;AACpC,OAAOC,cAAc,MAAM,oBAAoB;;AAqB/C;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,SAASC,sBAAsBA,CAC9BC,QAAiC,EACjCC,EAAW,EAC+B;EAC1C;EACA;EACA;EACA;EACA,MAAMC,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B,CAAEC,QAAQ,EAAEC,EAAG,CAAC;AAC9C","ignoreList":[]}
@@ -15,7 +15,7 @@ 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 { forwardResolver, getNormalizedCommaSeparable, getUserPermissionCacheKey, getUserPermissionsFromResponse, ALLOWED_RESOURCE_ACTIONS } from './utils';
19
19
  import { getSyncProvider } from './sync';
20
20
  import { fetchBlockPatterns } from './fetch';
21
21
 
@@ -59,11 +59,12 @@ export const getCurrentUser = () => async ({
59
59
  */
60
60
  export const getEntityRecord = (kind, name, key = '', query) => async ({
61
61
  select,
62
- dispatch
62
+ dispatch,
63
+ registry
63
64
  }) => {
64
65
  const configs = await dispatch(getOrLoadEntitiesConfig(kind, name));
65
66
  const entityConfig = configs.find(config => config.name === name && config.kind === kind);
66
- if (!entityConfig || entityConfig?.__experimentalNoFetch) {
67
+ if (!entityConfig) {
67
68
  return;
68
69
  }
69
70
  const lock = await dispatch.__unstableAcquireStoreLock(STORE_NAME, ['entities', 'records', kind, name, key], {
@@ -131,10 +132,28 @@ export const getEntityRecord = (kind, name, key = '', query) => async ({
131
132
  return;
132
133
  }
133
134
  }
134
- const record = await apiFetch({
135
- path
135
+ const response = await apiFetch({
136
+ path,
137
+ parse: false
138
+ });
139
+ const record = await response.json();
140
+ const permissions = getUserPermissionsFromResponse(response);
141
+ registry.batch(() => {
142
+ dispatch.receiveEntityRecords(kind, name, record, query);
143
+ for (const action of ALLOWED_RESOURCE_ACTIONS) {
144
+ const permissionKey = getUserPermissionCacheKey(action, {
145
+ kind,
146
+ name,
147
+ id: key
148
+ });
149
+ dispatch.receiveUserPermission(permissionKey, permissions[action]);
150
+ dispatch.finishResolution('canUser', [action, {
151
+ kind,
152
+ name,
153
+ id: key
154
+ }]);
155
+ }
136
156
  });
137
- dispatch.receiveEntityRecords(kind, name, record, query);
138
157
  }
139
158
  } finally {
140
159
  dispatch.__unstableReleaseStoreLock(lock);
@@ -165,7 +184,7 @@ export const getEntityRecords = (kind, name, query = {}) => async ({
165
184
  }) => {
166
185
  const configs = await dispatch(getOrLoadEntitiesConfig(kind, name));
167
186
  const entityConfig = configs.find(config => config.name === name && config.kind === kind);
168
- if (!entityConfig || entityConfig?.__experimentalNoFetch) {
187
+ if (!entityConfig) {
169
188
  return;
170
189
  }
171
190
  const lock = await dispatch.__unstableAcquireStoreLock(STORE_NAME, ['entities', 'records', kind, name], {
@@ -224,16 +243,7 @@ export const getEntityRecords = (kind, name, query = {}) => async ({
224
243
  if (!query?._fields && !query.context) {
225
244
  const key = entityConfig.key || DEFAULT_ENTITY_KEY;
226
245
  const resolutionsArgs = records.filter(record => record?.[key]).map(record => [kind, name, record[key]]);
227
- dispatch({
228
- type: 'START_RESOLUTIONS',
229
- selectorName: 'getEntityRecord',
230
- args: resolutionsArgs
231
- });
232
- dispatch({
233
- type: 'FINISH_RESOLUTIONS',
234
- selectorName: 'getEntityRecord',
235
- args: resolutionsArgs
236
- });
246
+ dispatch.finishResolutions('getEntityRecord', resolutionsArgs);
237
247
  }
238
248
  dispatch.__unstableReleaseStoreLock(lock);
239
249
  });
@@ -288,26 +298,39 @@ export const getEmbedPreview = url => async ({
288
298
  * Checks whether the current user can perform the given action on the given
289
299
  * REST resource.
290
300
  *
291
- * @param {string} requestedAction Action to check. One of: 'create', 'read', 'update',
292
- * 'delete'.
293
- * @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
294
- * @param {?string} id ID of the rest resource to check.
301
+ * @param {string} requestedAction Action to check. One of: 'create', 'read', 'update',
302
+ * 'delete'.
303
+ * @param {string|Object} resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`
304
+ * or REST base as a string - `media`.
305
+ * @param {?string} id ID of the rest resource to check.
295
306
  */
296
307
  export const canUser = (requestedAction, resource, id) => async ({
297
308
  dispatch,
298
309
  registry
299
310
  }) => {
311
+ if (!ALLOWED_RESOURCE_ACTIONS.includes(requestedAction)) {
312
+ throw new Error(`'${requestedAction}' is not a valid action.`);
313
+ }
314
+ let resourcePath = null;
315
+ if (typeof resource === 'object') {
316
+ if (!resource.kind || !resource.name) {
317
+ throw new Error('The entity resource object is not valid.');
318
+ }
319
+ const configs = await dispatch(getOrLoadEntitiesConfig(resource.kind, resource.name));
320
+ const entityConfig = configs.find(config => config.name === resource.name && config.kind === resource.kind);
321
+ if (!entityConfig) {
322
+ return;
323
+ }
324
+ resourcePath = entityConfig.baseURL + (resource.id ? '/' + resource.id : '');
325
+ } else {
326
+ resourcePath = `/wp/v2/${resource}` + (id ? '/' + id : '');
327
+ }
300
328
  const {
301
329
  hasStartedResolution
302
330
  } = registry.select(STORE_NAME);
303
- const resourcePath = id ? `${resource}/${id}` : resource;
304
- const retrievedActions = ['create', 'read', 'update', 'delete'];
305
- if (!retrievedActions.includes(requestedAction)) {
306
- throw new Error(`'${requestedAction}' is not a valid action.`);
307
- }
308
331
 
309
332
  // Prevent resolving the same resource twice.
310
- for (const relatedAction of retrievedActions) {
333
+ for (const relatedAction of ALLOWED_RESOURCE_ACTIONS) {
311
334
  if (relatedAction === requestedAction) {
312
335
  continue;
313
336
  }
@@ -319,7 +342,7 @@ export const canUser = (requestedAction, resource, id) => async ({
319
342
  let response;
320
343
  try {
321
344
  response = await apiFetch({
322
- path: `/wp/v2/${resourcePath}`,
345
+ path: resourcePath,
323
346
  method: 'OPTIONS',
324
347
  parse: false
325
348
  });
@@ -328,25 +351,18 @@ export const canUser = (requestedAction, resource, id) => async ({
328
351
  // 5xx). The previously determined isAllowed value will remain in the store.
329
352
  return;
330
353
  }
331
-
332
- // Optional chaining operator is used here because the API requests don't
333
- // return the expected result in the native version. Instead, API requests
334
- // only return the result, without including response properties like the headers.
335
- const allowHeader = response.headers?.get('allow');
336
- const allowedMethods = allowHeader?.allow || allowHeader || '';
337
- const permissions = {};
338
- const methods = {
339
- create: 'POST',
340
- read: 'GET',
341
- update: 'PUT',
342
- delete: 'DELETE'
343
- };
344
- for (const [actionName, methodName] of Object.entries(methods)) {
345
- permissions[actionName] = allowedMethods.includes(methodName);
346
- }
347
- for (const action of retrievedActions) {
348
- dispatch.receiveUserPermission(`${action}/${resourcePath}`, permissions[action]);
349
- }
354
+ const permissions = getUserPermissionsFromResponse(response);
355
+ registry.batch(() => {
356
+ for (const action of ALLOWED_RESOURCE_ACTIONS) {
357
+ const key = getUserPermissionCacheKey(action, resource, id);
358
+ dispatch.receiveUserPermission(key, permissions[action]);
359
+
360
+ // Mark related action resolutions as finished.
361
+ if (action !== requestedAction) {
362
+ dispatch.finishResolution('canUser', [action, resource, id]);
363
+ }
364
+ }
365
+ });
350
366
  };
351
367
 
352
368
  /**
@@ -360,13 +376,11 @@ export const canUser = (requestedAction, resource, id) => async ({
360
376
  export const canUserEditEntityRecord = (kind, name, recordId) => async ({
361
377
  dispatch
362
378
  }) => {
363
- const configs = await dispatch(getOrLoadEntitiesConfig(kind, name));
364
- const entityConfig = configs.find(config => config.name === name && config.kind === kind);
365
- if (!entityConfig) {
366
- return;
367
- }
368
- const resource = entityConfig.__unstable_rest_base;
369
- await dispatch(canUser('update', resource, recordId));
379
+ await dispatch(canUser('update', {
380
+ kind,
381
+ name,
382
+ id: recordId
383
+ }));
370
384
  };
371
385
 
372
386
  /**
@@ -450,11 +464,16 @@ export const __experimentalGetCurrentGlobalStylesId = () => async ({
450
464
  status: 'active'
451
465
  });
452
466
  const globalStylesURL = activeThemes?.[0]?._links?.['wp:user-global-styles']?.[0]?.href;
453
- if (globalStylesURL) {
454
- const globalStylesObject = await apiFetch({
455
- url: globalStylesURL
456
- });
457
- dispatch.__experimentalReceiveCurrentGlobalStylesId(globalStylesObject.id);
467
+ if (!globalStylesURL) {
468
+ return;
469
+ }
470
+
471
+ // Regex matches the ID at the end of a URL or immediately before
472
+ // the query string.
473
+ const matches = globalStylesURL.match(/\/(\d+)(?:\?|$)/);
474
+ const id = matches ? Number(matches[1]) : null;
475
+ if (id) {
476
+ dispatch.__experimentalReceiveCurrentGlobalStylesId(id);
458
477
  }
459
478
  };
460
479
  export const __experimentalGetCurrentThemeBaseGlobalStyles = () => async ({
@@ -588,7 +607,7 @@ export const getRevisions = (kind, name, recordKey, query = {}) => async ({
588
607
  }) => {
589
608
  const configs = await dispatch(getOrLoadEntitiesConfig(kind, name));
590
609
  const entityConfig = configs.find(config => config.name === name && config.kind === kind);
591
- if (!entityConfig || entityConfig?.__experimentalNoFetch) {
610
+ if (!entityConfig) {
592
611
  return;
593
612
  }
594
613
  if (query._fields) {
@@ -641,16 +660,8 @@ export const getRevisions = (kind, name, recordKey, query = {}) => async ({
641
660
  if (!query?._fields && !query.context) {
642
661
  const key = entityConfig.key || DEFAULT_ENTITY_KEY;
643
662
  const resolutionsArgs = records.filter(record => record[key]).map(record => [kind, name, recordKey, record[key]]);
644
- dispatch({
645
- type: 'START_RESOLUTIONS',
646
- selectorName: 'getRevision',
647
- args: resolutionsArgs
648
- });
649
- dispatch({
650
- type: 'FINISH_RESOLUTIONS',
651
- selectorName: 'getRevision',
652
- args: resolutionsArgs
653
- });
663
+ dispatch.startResolutions('getRevision', resolutionsArgs);
664
+ dispatch.finishResolutions('getRevision', resolutionsArgs);
654
665
  }
655
666
  }
656
667
  };
@@ -674,7 +685,7 @@ export const getRevision = (kind, name, recordKey, revisionKey, query) => async
674
685
  }) => {
675
686
  const configs = await dispatch(getOrLoadEntitiesConfig(kind, name));
676
687
  const entityConfig = configs.find(config => config.name === name && config.kind === kind);
677
- if (!entityConfig || entityConfig?.__experimentalNoFetch) {
688
+ if (!entityConfig) {
678
689
  return;
679
690
  }
680
691
  if (query !== undefined && query._fields) {