Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Post Content block regression #32693

Merged
merged 4 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -65,6 +66,7 @@ function EditableContent( { layout, postType, postId } ) {
postType,
{ id: postId }
);

const props = useInnerBlocksProps(
useBlockProps( { className: 'entry-content' } ),
{
Expand All @@ -80,13 +82,9 @@ function EditableContent( { layout, postType, postId } ) {
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 ? (
<EditableContent { ...props } />
) : (
Expand Down
7 changes: 1 addition & 6 deletions packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 3 additions & 9 deletions packages/block-library/src/utils/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
);
}

Expand Down
1 change: 0 additions & 1 deletion packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this should be something that is available in all entities (and not just post one) but also ideally, it could potentially be better though, basically we need this properly to match with the "permissions" array, we could have a canEdit function per entity or a getResource or something like that. But this will do for now.

};
} );
}
Expand Down
22 changes: 10 additions & 12 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
17 changes: 10 additions & 7 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
52 changes: 14 additions & 38 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,63 +446,39 @@ 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: {
data: {
root: {
postType: {
queriedData: {
items: {
post: { slug: 'post', rest_base: 'posts' },
},
itemIsComplete: {
post: true,
},
queries: {},
},
},
config: [
{
kind: 'postType',
name: 'post',
__unstable_rest_base: 'posts',
},
},
},
} );
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' },
post: { slug: 'post', __unstable: 'posts' },
},
itemIsComplete: {
page: true,
post: true,
},
queries: {},
},
Expand All @@ -512,7 +488,7 @@ describe( 'canUserEditEntityRecord', () => {
},
} );
expect(
canUserEditEntityRecord( state, 'root', 'postType', 'page', 2010 )
canUserEditEntityRecord( state, 'postType', 'post', '1' )
).toBe( true );
} );
} );
Expand Down
3 changes: 3 additions & 0 deletions packages/e2e-tests/specs/performance/site-editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]'
);
Expand Down