From 6ae94ab259ecea71fff8e7a374a6f871da98b8fd Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 15 Jun 2021 14:05:12 +0100 Subject: [PATCH 1/4] Fix Post Content block regression --- packages/block-library/src/post-content/edit.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 4d1ae859d6d5f5..498b3c4a3db4ec 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -38,7 +38,8 @@ function ReadOnlyContent( { userCanEdit, postType, postId } ) { ); } -function EditableContent( { layout, postType, postId } ) { +function EditableContent( { layout, context = {} } ) { + const { postType, postId } = context; const themeSupportsLayout = useSelect( ( select ) => { const { getSettings } = select( blockEditorStore ); return getSettings()?.supportsLayout; @@ -65,6 +66,7 @@ function EditableContent( { layout, postType, postId } ) { postType, { id: postId } ); + const props = useInnerBlocksProps( useBlockProps( { className: 'entry-content' } ), { @@ -87,6 +89,7 @@ function Content( props ) { postId ); const isEditable = userCanEdit && ! isDescendentOfQueryLoop; + return isEditable ? ( ) : ( From 08b693cf1e7a939681b08b6878c7745f869ddd1a Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 15 Jun 2021 14:54:29 +0100 Subject: [PATCH 2/4] fix canUserEdit selector --- .../block-library/src/post-content/edit.js | 7 +----- .../block-library/src/post-excerpt/edit.js | 7 +----- packages/block-library/src/post-title/edit.js | 7 +----- packages/block-library/src/utils/hooks.js | 12 +++------- packages/core-data/README.md | 1 - packages/core-data/src/entities.js | 1 + packages/core-data/src/resolvers.js | 22 +++++++++---------- packages/core-data/src/selectors.js | 17 ++++++++------ .../specs/performance/site-editor.test.js | 3 +++ 9 files changed, 30 insertions(+), 47 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 498b3c4a3db4ec..9283b120057b36 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -82,12 +82,7 @@ function EditableContent( { layout, context = {} } ) { function Content( props ) { const { context: { queryId, postType, postId } = {} } = props; const isDescendentOfQueryLoop = !! queryId; - const userCanEdit = useCanEditEntity( - 'root', - 'postType', - postType, - postId - ); + const userCanEdit = useCanEditEntity( 'postType', postType, postId ); const isEditable = userCanEdit && ! isDescendentOfQueryLoop; return isEditable ? ( diff --git a/packages/block-library/src/post-excerpt/edit.js b/packages/block-library/src/post-excerpt/edit.js index 0d7648903495ea..0df53eb1879235 100644 --- a/packages/block-library/src/post-excerpt/edit.js +++ b/packages/block-library/src/post-excerpt/edit.js @@ -52,12 +52,7 @@ export default function PostExcerptEditor( { context: { postId, postType, queryId }, } ) { const isDescendentOfQueryLoop = !! queryId; - const userCanEdit = useCanEditEntity( - 'root', - 'postType', - postType, - postId - ); + const userCanEdit = useCanEditEntity( 'postType', postType, postId ); const isEditable = userCanEdit && ! isDescendentOfQueryLoop; const [ rawExcerpt, diff --git a/packages/block-library/src/post-title/edit.js b/packages/block-library/src/post-title/edit.js index 77f5dbbd64f2d4..259f12b19632bb 100644 --- a/packages/block-library/src/post-title/edit.js +++ b/packages/block-library/src/post-title/edit.js @@ -31,12 +31,7 @@ export default function PostTitleEdit( { } ) { const TagName = 0 === level ? 'p' : 'h' + level; const isDescendentOfQueryLoop = !! queryId; - const userCanEdit = useCanEditEntity( - 'root', - 'postType', - postType, - postId - ); + const userCanEdit = useCanEditEntity( 'postType', postType, postId ); const [ rawTitle = '', setTitle, fullTitle ] = useEntityProp( 'postType', postType, diff --git a/packages/block-library/src/utils/hooks.js b/packages/block-library/src/utils/hooks.js index 1a71c4b0e3f7be..4b64c529299467 100644 --- a/packages/block-library/src/utils/hooks.js +++ b/packages/block-library/src/utils/hooks.js @@ -9,19 +9,13 @@ import { store as coreStore } from '@wordpress/core-data'; * * @param {string} kind Entity kind. * @param {string} name Entity name. - * @param {number} key Record's key. * @param {string} recordId Record's id. */ -export function useCanEditEntity( kind, name, key, recordId ) { +export function useCanEditEntity( kind, name, recordId ) { return useSelect( ( select ) => - select( coreStore ).canUserEditEntityRecord( - kind, - name, - key, - recordId - ), - [ kind, name, key, recordId ] + select( coreStore ).canUserEditEntityRecord( kind, name, recordId ), + [ kind, name, recordId ] ); } diff --git a/packages/core-data/README.md b/packages/core-data/README.md index 2e8e58fee25952..3c5d3ff07a6b9a 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -282,7 +282,6 @@ _Parameters_ - _state_ `Object`: Data state. - _kind_ `string`: Entity kind. - _name_ `string`: Entity name. -- _key_ `number`: Record's key. - _recordId_ `string`: Record's id. _Returns_ diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 8e07b4153ab7fc..85f0cf5797f162 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -189,6 +189,7 @@ function* loadPostTypeEntities() { record?.title || ( isTemplate ? startCase( record.slug ) : String( record.id ) ), __unstablePrePersist: isTemplate ? undefined : prePersistPostType, + __unstable_rest_base: postType.rest_base, }; } ); } diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index edb4cbdb7c1a5d..48fd962c028442 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -347,20 +347,18 @@ export function* canUser( action, resource, id ) { * Checks whether the current user can perform the given action on the given * REST resource. * - * @param {string} kind Entity kind. - * @param {string} name Entity name. - * @param {number} key Record's key. + * @param {string} kind Entity kind. + * @param {string} name Entity name. * @param {string} recordId Record's id. */ -export function* canUserEditEntityRecord( kind, name, key, recordId ) { - const entity = yield controls.select( - coreStoreName, - 'getEntityRecord', - kind, - name, - key - ); - const resource = entity?.rest_base || ''; +export function* canUserEditEntityRecord( kind, name, recordId ) { + const entities = yield getKindEntities( kind ); + const entity = find( entities, { kind, name } ); + if ( ! entity ) { + return; + } + + const resource = entity.__unstable_rest_base; yield canUser( 'update', resource, recordId ); } diff --git a/packages/core-data/src/selectors.js b/packages/core-data/src/selectors.js index 320519c3fcb502..5b28c1df5146c1 100644 --- a/packages/core-data/src/selectors.js +++ b/packages/core-data/src/selectors.js @@ -646,17 +646,20 @@ export function canUser( state, action, resource, id ) { * * https://developer.wordpress.org/rest-api/reference/ * - * @param {Object} state Data state. - * @param {string} kind Entity kind. - * @param {string} name Entity name. - * @param {number} key Record's key. + * @param {Object} state Data state. + * @param {string} kind Entity kind. + * @param {string} name Entity name. * @param {string} recordId Record's id. * @return {boolean|undefined} Whether or not the user can edit, * or `undefined` if the OPTIONS request is still being made. */ -export function canUserEditEntityRecord( state, kind, name, key, recordId ) { - const entity = getEntityRecord( state, kind, name, key ); - const resource = entity?.rest_base || ''; +export function canUserEditEntityRecord( state, kind, name, recordId ) { + const entity = getEntity( state, kind, name ); + if ( ! entity ) { + return false; + } + const resource = entity.__unstable_rest_base; + return canUser( state, 'update', resource, recordId ); } diff --git a/packages/e2e-tests/specs/performance/site-editor.test.js b/packages/e2e-tests/specs/performance/site-editor.test.js index b13c9c6f8e1db3..21451db3961879 100644 --- a/packages/e2e-tests/specs/performance/site-editor.test.js +++ b/packages/e2e-tests/specs/performance/site-editor.test.js @@ -88,6 +88,9 @@ describe( 'Site Editor Performance', () => { } // Measuring typing performance inside the post content. + await canvas().waitForSelector( + '[data-type="core/post-content"] [data-type="core/paragraph"]' + ); await canvas().click( '[data-type="core/post-content"] [data-type="core/paragraph"]' ); From f814f671bee9f07777392ab32ec746ee689c0f92 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 15 Jun 2021 15:04:40 +0100 Subject: [PATCH 3/4] Fix unit tests --- packages/core-data/src/test/selectors.js | 52 +++++++----------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/packages/core-data/src/test/selectors.js b/packages/core-data/src/test/selectors.js index afc8448d39362b..507ac5bb7e6177 100644 --- a/packages/core-data/src/test/selectors.js +++ b/packages/core-data/src/test/selectors.js @@ -446,29 +446,36 @@ describe( 'canUser', () => { } ); describe( 'canUserEditEntityRecord', () => { - it( 'returns undefined by default', () => { + it( 'returns false by default', () => { const state = deepFreeze( { userPermissions: {}, entities: { data: {} }, } ); - expect( - canUserEditEntityRecord( state, 'root', 'postType', 'post' ) - ).toBe( undefined ); + expect( canUserEditEntityRecord( state, 'postType', 'post' ) ).toBe( + false + ); } ); it( 'returns whether the user can edit', () => { const state = deepFreeze( { userPermissions: { 'create/posts': false, - 'update/posts': true, + 'update/posts/1': true, }, entities: { + config: [ + { + kind: 'postType', + name: 'post', + __unstable_rest_base: 'posts' + } + ], data: { root: { postType: { queriedData: { items: { - post: { slug: 'post', rest_base: 'posts' }, + post: { slug: 'post', __unstable: 'posts' }, }, itemIsComplete: { post: true, @@ -481,38 +488,7 @@ describe( 'canUserEditEntityRecord', () => { }, } ); expect( - canUserEditEntityRecord( state, 'root', 'postType', 'post' ) - ).toBe( true ); - } ); - - it( 'returns whether whether the user can edit a given resource', () => { - const state = deepFreeze( { - userPermissions: { - 'create/posts': false, - 'update/pages': false, - 'update/pages/2010': true, - 'update/posts/2010': false, - }, - entities: { - data: { - root: { - postType: { - queriedData: { - items: { - page: { slug: 'page', rest_base: 'pages' }, - }, - itemIsComplete: { - page: true, - }, - queries: {}, - }, - }, - }, - }, - }, - } ); - expect( - canUserEditEntityRecord( state, 'root', 'postType', 'page', 2010 ) + canUserEditEntityRecord( state, 'postType', 'post', '1' ) ).toBe( true ); } ); } ); From 8ce1ec349477cc9f08b53aff5cc65496e9fdd7c3 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 15 Jun 2021 15:20:15 +0100 Subject: [PATCH 4/4] Fix small linting issue --- packages/core-data/src/test/selectors.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core-data/src/test/selectors.js b/packages/core-data/src/test/selectors.js index 507ac5bb7e6177..cce4293885f32d 100644 --- a/packages/core-data/src/test/selectors.js +++ b/packages/core-data/src/test/selectors.js @@ -467,8 +467,8 @@ describe( 'canUserEditEntityRecord', () => { { kind: 'postType', name: 'post', - __unstable_rest_base: 'posts' - } + __unstable_rest_base: 'posts', + }, ], data: { root: {