From 7779a218f161ebff7c4a24f3d15118a425e66faa Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 5 Dec 2019 17:30:56 -0500 Subject: [PATCH 1/3] Core Data: Add support for fetching permissions of custom actions --- packages/core-data/src/resolvers.js | 60 +++++++++++++++--------- packages/core-data/src/test/resolvers.js | 26 ++++++++++ 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 670cb4adf2a750..377ddf6d6ce91e 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -127,8 +127,8 @@ export function* hasUploadPermissions() { * Checks whether the current user can perform the given action on the given * REST resource. * - * @param {string} action Action to check. One of: 'create', 'read', 'update', - * 'delete'. + * @param {string} action Action to check. One of 'create', 'read', 'update', + * 'delete', or a JSON Hyper Schema targetSchema key. * @param {string} resource REST resource to check, e.g. 'media' or 'posts'. * @param {?string} id ID of the rest resource to check. */ @@ -140,23 +140,35 @@ export function* canUser( action, resource, id ) { delete: 'DELETE', }; - const method = methods[ action ]; - if ( ! method ) { - throw new Error( `'${ action }' is not a valid action.` ); + const isCustomAction = ! methods.hasOwnProperty( action ); + + let path = '/wp/v2/' + resource; + + if ( id ) { + path += '/' + id; } - const path = id ? `/wp/v2/${ resource }/${ id }` : `/wp/v2/${ resource }`; + if ( isCustomAction ) { + path = addQueryArgs( path, { context: 'edit' } ); + } let response; try { response = yield apiFetch( { path, - // Ideally this would always be an OPTIONS request, but unfortunately there's - // a bug in the REST API which causes the Allow header to not be sent on - // OPTIONS requests to /posts/:id routes. - // https://core.trac.wordpress.org/ticket/45753 - method: id ? 'GET' : 'OPTIONS', - parse: false, + // Ideally, this should only be a GET request if requesting details + // of a custom action. Until required WordPress support reaches 5.3 + // or newer, the `id` condition must be included, due to a previous + // bug in the REST API where the Allow header was not on OPTIONS + // requests to /posts/:id routes. + // + // See: https://core.trac.wordpress.org/ticket/45753 + method: id || isCustomAction ? 'GET' : 'OPTIONS', + + // Only parse response as JSON if requesting permissions of a custom + // actions. Non-custom actions derive permissions from the response + // headers, which aren't available on the parsed result. + parse: isCustomAction, } ); } catch ( error ) { // Do nothing if our OPTIONS request comes back with an API error (4xx or @@ -164,19 +176,25 @@ export function* canUser( action, resource, id ) { return; } - let allowHeader; - if ( hasIn( response, [ 'headers', 'get' ] ) ) { - // If the request is fetched using the fetch api, the header can be - // retrieved using the 'get' method. - allowHeader = response.headers.get( 'allow' ); + let isAllowed; + if ( isCustomAction ) { + isAllowed = hasIn( response, [ '_links', 'wp:action-' + action ] ); } else { - // If the request was preloaded server-side and is returned by the - // preloading middleware, the header will be a simple property. - allowHeader = get( response, [ 'headers', 'Allow' ], '' ); + let allowHeader; + if ( hasIn( response, [ 'headers', 'get' ] ) ) { + // If the request is fetched using the fetch api, the header can be + // retrieved using the 'get' method. + allowHeader = response.headers.get( 'allow' ); + } else { + // If the request was preloaded server-side and is returned by the + // preloading middleware, the header will be a simple property. + allowHeader = get( response, [ 'headers', 'Allow' ], '' ); + } + + isAllowed = includes( allowHeader, methods[ action ] ); } const key = compact( [ action, resource, id ] ).join( '/' ); - const isAllowed = includes( allowHeader, method ); yield receiveUserPermission( key, isAllowed ); } diff --git a/packages/core-data/src/test/resolvers.js b/packages/core-data/src/test/resolvers.js index c91babdfb59562..7e73d6e283d9a3 100644 --- a/packages/core-data/src/test/resolvers.js +++ b/packages/core-data/src/test/resolvers.js @@ -158,6 +158,32 @@ describe( 'canUser', () => { expect( received.done ).toBe( true ); expect( received.value ).toBeUndefined(); } ); + + it( 'receives custom actions', () => { + const generator = canUser( 'publish', 'posts' ); + + let received = generator.next(); + expect( received.done ).toBe( false ); + expect( received.value ).toEqual( apiFetch( { + path: '/wp/v2/posts?context=edit', + method: 'GET', + parse: true, + } ) ); + + received = generator.next( { + _links: { + 'wp:action-publish': [ + { href: 'http://localhost:8888/wp-json/wp/v2/posts' }, + ], + }, + } ); + expect( received.done ).toBe( false ); + expect( received.value ).toEqual( receiveUserPermission( 'publish/posts', true ) ); + + received = generator.next(); + expect( received.done ).toBe( true ); + expect( received.value ).toBeUndefined(); + } ); } ); describe( 'getAutosaves', () => { From 9fbfd97fab1b78d194d58fc1034bee5d6f1e667e Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 5 Dec 2019 17:32:13 -0500 Subject: [PATCH 2/3] Editor: Use canUser to derive user capabilities --- .../header/post-publish-button-or-toggle.js | 11 +---------- packages/editor/src/components/post-author/check.js | 9 ++------- .../src/components/post-pending-status/check.js | 9 ++------- .../src/components/post-publish-button/index.js | 6 +++--- .../src/components/post-publish-button/label.js | 10 +++------- .../src/components/post-publish-panel/index.js | 6 +++--- .../src/components/post-publish-panel/prepublish.js | 10 +++------- .../editor/src/components/post-saved-state/index.js | 9 ++++----- .../editor/src/components/post-schedule/check.js | 10 +++------- packages/editor/src/components/post-sticky/check.js | 12 ++++-------- .../components/post-taxonomies/flat-term-selector.js | 9 +++++---- .../post-taxonomies/hierarchical-term-selector.js | 9 +++++---- .../editor/src/components/post-visibility/check.js | 10 +++------- packages/editor/src/components/provider/index.js | 4 ++-- 14 files changed, 43 insertions(+), 81 deletions(-) diff --git a/packages/edit-post/src/components/header/post-publish-button-or-toggle.js b/packages/edit-post/src/components/header/post-publish-button-or-toggle.js index e483e3ee270bbd..c9cc6afd2c6875 100644 --- a/packages/edit-post/src/components/header/post-publish-button-or-toggle.js +++ b/packages/edit-post/src/components/header/post-publish-button-or-toggle.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -76,11 +71,7 @@ export function PostPublishButtonOrToggle( { export default compose( withSelect( ( select ) => ( { - hasPublishAction: get( - select( 'core/editor' ).getCurrentPost(), - [ '_links', 'wp:action-publish' ], - false - ), + hasPublishAction: select( 'core' ).canUser( 'publish', 'posts', select( 'core/editor' ).getCurrentPostId() ), isBeingScheduled: select( 'core/editor' ).isEditedPostBeingScheduled(), isPending: select( 'core/editor' ).isCurrentPostPending(), isPublished: select( 'core/editor' ).isCurrentPostPublished(), diff --git a/packages/editor/src/components/post-author/check.js b/packages/editor/src/components/post-author/check.js index 4f725e792a44f9..5004c81ce4cfbb 100644 --- a/packages/editor/src/components/post-author/check.js +++ b/packages/editor/src/components/post-author/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -24,9 +19,9 @@ export function PostAuthorCheck( { hasAssignAuthorAction, authors, children } ) export default compose( [ withSelect( ( select ) => { - const post = select( 'core/editor' ).getCurrentPost(); + const postId = select( 'core/editor' ).getCurrentPostId(); return { - hasAssignAuthorAction: get( post, [ '_links', 'wp:action-assign-author' ], false ), + hasAssignAuthorAction: select( 'core' ).canUser( 'assign-author', 'posts', postId ), postType: select( 'core/editor' ).getCurrentPostType(), authors: select( 'core' ).getAuthors(), }; diff --git a/packages/editor/src/components/post-pending-status/check.js b/packages/editor/src/components/post-pending-status/check.js index 2c6c19b928042b..591f14210190ea 100644 --- a/packages/editor/src/components/post-pending-status/check.js +++ b/packages/editor/src/components/post-pending-status/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -19,9 +14,9 @@ export function PostPendingStatusCheck( { hasPublishAction, isPublished, childre export default compose( withSelect( ( select ) => { - const { isCurrentPostPublished, getCurrentPostType, getCurrentPost } = select( 'core/editor' ); + const { isCurrentPostPublished, getCurrentPostType, getCurrentPostId } = select( 'core/editor' ); return { - hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ), + hasPublishAction: select( 'core' ).canUser( 'publish', 'posts', getCurrentPostId() ), isPublished: isCurrentPostPublished(), postType: getCurrentPostType(), }; diff --git a/packages/editor/src/components/post-publish-button/index.js b/packages/editor/src/components/post-publish-button/index.js index de7b3069d17265..eb625481a06431 100644 --- a/packages/editor/src/components/post-publish-button/index.js +++ b/packages/editor/src/components/post-publish-button/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { noop, get } from 'lodash'; +import { noop } from 'lodash'; import classnames from 'classnames'; import memoize from 'memize'; import EquivalentKeyMap from 'equivalent-key-map'; @@ -201,11 +201,11 @@ export default compose( [ isEditedPostSaveable, isEditedPostPublishable, isPostSavingLocked, - getCurrentPost, getCurrentPostType, getCurrentPostId, hasNonPostEntityChanges, } = select( 'core/editor' ); + const { canUser } = select( 'core' ); return { isSaving: isSavingPost(), isBeingScheduled: isEditedPostBeingScheduled(), @@ -214,7 +214,7 @@ export default compose( [ isPostSavingLocked: isPostSavingLocked(), isPublishable: isEditedPostPublishable(), isPublished: isCurrentPostPublished(), - hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ), + hasPublishAction: canUser( 'publish', 'posts', getCurrentPostId() ), postType: getCurrentPostType(), postId: getCurrentPostId(), hasNonPostEntityChanges: hasNonPostEntityChanges(), diff --git a/packages/editor/src/components/post-publish-button/label.js b/packages/editor/src/components/post-publish-button/label.js index 5373d39322558f..a69d1c0059120c 100644 --- a/packages/editor/src/components/post-publish-button/label.js +++ b/packages/editor/src/components/post-publish-button/label.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -47,16 +42,17 @@ export default compose( [ isEditedPostBeingScheduled, isSavingPost, isPublishingPost, - getCurrentPost, + getCurrentPostId, getCurrentPostType, isAutosavingPost, } = select( 'core/editor' ); + const { canUser } = select( 'core' ); return { isPublished: isCurrentPostPublished(), isBeingScheduled: isEditedPostBeingScheduled(), isSaving: forceIsSaving || isSavingPost(), isPublishing: isPublishingPost(), - hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ), + hasPublishAction: canUser( 'publish', 'posts', getCurrentPostId() ), postType: getCurrentPostType(), isAutosaving: isAutosavingPost(), }; diff --git a/packages/editor/src/components/post-publish-panel/index.js b/packages/editor/src/components/post-publish-panel/index.js index b6c5f5612c787c..71eba8b3361df1 100644 --- a/packages/editor/src/components/post-publish-panel/index.js +++ b/packages/editor/src/components/post-publish-panel/index.js @@ -111,9 +111,9 @@ export class PostPublishPanel extends Component { export default compose( [ withSelect( ( select ) => { - const { getPostType } = select( 'core' ); + const { canUser, getPostType } = select( 'core' ); const { - getCurrentPost, + getCurrentPostId, getEditedPostAttribute, isCurrentPostPublished, isCurrentPostScheduled, @@ -125,7 +125,7 @@ export default compose( [ const postType = getPostType( getEditedPostAttribute( 'type' ) ); return { - hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ), + hasPublishAction: canUser( 'publish', 'posts', getCurrentPostId() ), isPostTypeViewable: get( postType, [ 'viewable' ], false ), isBeingScheduled: isEditedPostBeingScheduled(), isDirty: isEditedPostDirty(), diff --git a/packages/editor/src/components/post-publish-panel/prepublish.js b/packages/editor/src/components/post-publish-panel/prepublish.js index 9368db03008db2..384022f9f5d2f5 100644 --- a/packages/editor/src/components/post-publish-panel/prepublish.js +++ b/packages/editor/src/components/post-publish-panel/prepublish.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -68,11 +63,12 @@ function PostPublishPanelPrepublish( { export default withSelect( ( select ) => { const { - getCurrentPost, + getCurrentPostId, isEditedPostBeingScheduled, } = select( 'core/editor' ); + const { canUser } = select( 'core' ); return { - hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ), + hasPublishAction: canUser( 'publish', 'posts', getCurrentPostId() ), isBeingScheduled: isEditedPostBeingScheduled(), }; } diff --git a/packages/editor/src/components/post-saved-state/index.js b/packages/editor/src/components/post-saved-state/index.js index 0fabd1c0f80fed..ca0722afd673a1 100644 --- a/packages/editor/src/components/post-saved-state/index.js +++ b/packages/editor/src/components/post-saved-state/index.js @@ -2,7 +2,6 @@ * External dependencies */ import classnames from 'classnames'; -import { get } from 'lodash'; /** * WordPress dependencies @@ -44,7 +43,7 @@ export class PostSavedState extends Component { render() { const { - post, + hasPublishAction, isNew, isScheduled, isPublished, @@ -96,7 +95,6 @@ export class PostSavedState extends Component { // Once the post has been submitted for review this button // is not needed for the contributor role. - const hasPublishAction = get( post, [ '_links', 'wp:action-publish' ], false ); if ( ! hasPublishAction && isPending ) { return null; } @@ -136,12 +134,13 @@ export default compose( [ isEditedPostDirty, isSavingPost, isEditedPostSaveable, - getCurrentPost, + getCurrentPostId, isAutosavingPost, getEditedPostAttribute, } = select( 'core/editor' ); + const { canUser } = select( 'core' ); return { - post: getCurrentPost(), + hasPublishAction: canUser( 'publish', 'posts', getCurrentPostId() ), isNew: isEditedPostNew(), isPublished: isCurrentPostPublished(), isScheduled: isCurrentPostScheduled(), diff --git a/packages/editor/src/components/post-schedule/check.js b/packages/editor/src/components/post-schedule/check.js index 7495f216b014c5..e1a77e129a80fa 100644 --- a/packages/editor/src/components/post-schedule/check.js +++ b/packages/editor/src/components/post-schedule/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -19,9 +14,10 @@ export function PostScheduleCheck( { hasPublishAction, children } ) { export default compose( [ withSelect( ( select ) => { - const { getCurrentPost, getCurrentPostType } = select( 'core/editor' ); + const { getCurrentPostId, getCurrentPostType } = select( 'core/editor' ); + const { canUser } = select( 'core' ); return { - hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ), + hasPublishAction: canUser( 'publish', 'posts', getCurrentPostId() ), postType: getCurrentPostType(), }; } ), diff --git a/packages/editor/src/components/post-sticky/check.js b/packages/editor/src/components/post-sticky/check.js index 949c18cf43efdf..153eccc3fd6f4f 100644 --- a/packages/editor/src/components/post-sticky/check.js +++ b/packages/editor/src/components/post-sticky/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -22,10 +17,11 @@ export function PostStickyCheck( { hasStickyAction, postType, children } ) { export default compose( [ withSelect( ( select ) => { - const post = select( 'core/editor' ).getCurrentPost(); + const { canUser } = select( 'core' ); + const { getCurrentPostId, getCurrentPostType } = select( 'core/editor' ); return { - hasStickyAction: get( post, [ '_links', 'wp:action-sticky' ], false ), - postType: select( 'core/editor' ).getCurrentPostType(), + hasStickyAction: canUser( 'sticky', 'posts', getCurrentPostId() ), + postType: getCurrentPostType(), }; } ), ] )( PostStickyCheck ); diff --git a/packages/editor/src/components/post-taxonomies/flat-term-selector.js b/packages/editor/src/components/post-taxonomies/flat-term-selector.js index da6b0782966d4b..b69cf5cd1a208e 100644 --- a/packages/editor/src/components/post-taxonomies/flat-term-selector.js +++ b/packages/editor/src/components/post-taxonomies/flat-term-selector.js @@ -244,12 +244,13 @@ class FlatTermSelector extends Component { export default compose( withSelect( ( select, { slug } ) => { - const { getCurrentPost } = select( 'core/editor' ); - const { getTaxonomy } = select( 'core' ); + const { getCurrentPostId } = select( 'core/editor' ); + const { getTaxonomy, canUser } = select( 'core' ); const taxonomy = getTaxonomy( slug ); + const postId = getCurrentPostId(); return { - hasCreateAction: taxonomy ? get( getCurrentPost(), [ '_links', 'wp:action-create-' + taxonomy.rest_base ], false ) : false, - hasAssignAction: taxonomy ? get( getCurrentPost(), [ '_links', 'wp:action-assign-' + taxonomy.rest_base ], false ) : false, + hasCreateAction: taxonomy ? canUser( 'create-' + taxonomy.rest_base, 'posts', postId ) : false, + hasAssignAction: taxonomy ? canUser( 'assign-' + taxonomy.rest_base, 'posts', postId ) : false, terms: taxonomy ? select( 'core/editor' ).getEditedPostAttribute( taxonomy.rest_base ) : [], taxonomy, }; diff --git a/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js b/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js index e7563ba296a3db..6d4e7fd192c177 100644 --- a/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js +++ b/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js @@ -453,12 +453,13 @@ class HierarchicalTermSelector extends Component { export default compose( [ withSelect( ( select, { slug } ) => { - const { getCurrentPost } = select( 'core/editor' ); - const { getTaxonomy } = select( 'core' ); + const { getCurrentPostId } = select( 'core/editor' ); + const { getTaxonomy, canUser } = select( 'core' ); + const postId = getCurrentPostId(); const taxonomy = getTaxonomy( slug ); return { - hasCreateAction: taxonomy ? get( getCurrentPost(), [ '_links', 'wp:action-create-' + taxonomy.rest_base ], false ) : false, - hasAssignAction: taxonomy ? get( getCurrentPost(), [ '_links', 'wp:action-assign-' + taxonomy.rest_base ], false ) : false, + hasCreateAction: taxonomy ? canUser( 'create-' + taxonomy.rest_base, 'posts', postId ) : false, + hasAssignAction: taxonomy ? canUser( 'assign-' + taxonomy.rest_base, 'posts', postId ) : false, terms: taxonomy ? select( 'core/editor' ).getEditedPostAttribute( taxonomy.rest_base ) : [], taxonomy, }; diff --git a/packages/editor/src/components/post-visibility/check.js b/packages/editor/src/components/post-visibility/check.js index 134694b142916b..e7b3c65b05c4df 100644 --- a/packages/editor/src/components/post-visibility/check.js +++ b/packages/editor/src/components/post-visibility/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -16,9 +11,10 @@ export function PostVisibilityCheck( { hasPublishAction, render } ) { export default compose( [ withSelect( ( select ) => { - const { getCurrentPost, getCurrentPostType } = select( 'core/editor' ); + const { canUser } = select( 'core' ); + const { getCurrentPostId, getCurrentPostType } = select( 'core/editor' ); return { - hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ), + hasPublishAction: canUser( 'publish', 'posts', getCurrentPostId() ), postType: getCurrentPostType(), }; } ), diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index 1d3714e5c09885..577de980e392b6 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -204,7 +204,7 @@ export default compose( [ withRegistryProvider, withSelect( ( select ) => { const { - canUserUseUnfilteredHTML, + getCurrentPostId, __unstableIsEditorReady: isEditorReady, getEditorBlocks, getEditorSelectionStart, @@ -214,7 +214,7 @@ export default compose( [ const { canUser } = select( 'core' ); return { - canUserUseUnfilteredHTML: canUserUseUnfilteredHTML(), + canUserUseUnfilteredHTML: canUser( 'unfiltered-html', 'posts', getCurrentPostId() ), isReady: isEditorReady(), blocks: getEditorBlocks(), selectionStart: getEditorSelectionStart(), From d088dec3a93976b8cdd03acea9b6acad03758d78 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 5 Dec 2019 17:32:28 -0500 Subject: [PATCH 3/3] Editor: Deprecate canUserUseUnfilteredHTML selector --- .../developers/data/data-core-editor.md | 12 -------- packages/editor/src/store/selectors.js | 30 +++++++++++-------- packages/editor/src/store/test/selectors.js | 22 -------------- 3 files changed, 18 insertions(+), 46 deletions(-) diff --git a/docs/designers-developers/developers/data/data-core-editor.md b/docs/designers-developers/developers/data/data-core-editor.md index babe2b12d1c96d..790a6417428ad1 100644 --- a/docs/designers-developers/developers/data/data-core-editor.md +++ b/docs/designers-developers/developers/data/data-core-editor.md @@ -12,18 +12,6 @@ _Related_ - canInsertBlockType in core/block-editor store. -# **canUserUseUnfilteredHTML** - -Returns whether or not the user has the unfiltered_html capability. - -_Parameters_ - -- _state_ `Object`: Editor state. - -_Returns_ - -- `boolean`: Whether the user can or can't post unfiltered HTML. - # **didPostSaveRequestFail** Returns true if a previous post save was attempted but failed, or false diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index d8777cb5b590a4..2f334619cf27e7 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -4,7 +4,6 @@ import { find, get, - has, map, pick, mapValues, @@ -1215,17 +1214,6 @@ export function getActivePostLock( state ) { return state.postLock.activePostLock; } -/** - * Returns whether or not the user has the unfiltered_html capability. - * - * @param {Object} state Editor state. - * - * @return {boolean} Whether the user can or can't post unfiltered HTML. - */ -export function canUserUseUnfilteredHTML( state ) { - return has( getCurrentPost( state ), [ '_links', 'wp:action-unfiltered-html' ] ); -} - /** * Returns whether the pre-publish panel should be shown * or skipped when the user clicks the "publish" button. @@ -1307,6 +1295,24 @@ export function getEditorSettings( state ) { * Backward compatibility */ +/** + * Returns whether or not the user has the unfiltered_html capability. + * + * @deprecated + * @private + * + * @param {Object} state Editor state. + * + * @return {boolean} Whether the user can or can't post unfiltered HTML. + */ +export const canUserUseUnfilteredHTML = createRegistrySelector( ( select ) => ( state ) => { + deprecated( '`wp.data.select( \'core/editor\' ).canUserUseUnfilteredHTML`', { + alternative: '`wp.data.select( \'core\' ).canUser( \'unfiltered-html\', \'posts\', wp.data.select( \'core/editor\' ).getCurrentPostId() )`', + } ); + + return select( 'core' ).canUser( 'unfiltered-html', 'posts', getCurrentPostId( state ) ); +} ); + function getBlockEditorSelector( name ) { return createRegistrySelector( ( select ) => ( state, ...args ) => { deprecated( '`wp.data.select( \'core/editor\' ).' + name + '`', { diff --git a/packages/editor/src/store/test/selectors.js b/packages/editor/src/store/test/selectors.js index a6608f28b6cd68..d337b72b511da1 100644 --- a/packages/editor/src/store/test/selectors.js +++ b/packages/editor/src/store/test/selectors.js @@ -167,7 +167,6 @@ const { getPermalinkParts, isPostSavingLocked, isPostAutosavingLocked, - canUserUseUnfilteredHTML, } = selectors; describe( 'selectors', () => { @@ -2841,25 +2840,4 @@ describe( 'selectors', () => { expect( getPermalinkParts( state ) ).toBeNull(); } ); } ); - - describe( 'canUserUseUnfilteredHTML', () => { - it( 'should return true if the _links object contains the property wp:action-unfiltered-html', () => { - const state = { - currentPost: { - _links: { - 'wp:action-unfiltered-html': [], - }, - }, - }; - expect( canUserUseUnfilteredHTML( state ) ).toBe( true ); - } ); - it( 'should return false if the _links object doesnt contain the property wp:action-unfiltered-html', () => { - const state = { - currentPost: { - _links: {}, - }, - }; - expect( canUserUseUnfilteredHTML( state ) ).toBe( false ); - } ); - } ); } );