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: Don't allow selection of template parts that the user doesn't have the privileges to edit #60812

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,14 +3,14 @@
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';

const CONTENT_ONLY_BLOCKS = applyFilters( 'editor.postContentBlockTypes', [
'core/post-title',
'core/post-featured-image',
'core/post-content',
'core/template-part',
] );

/**
Expand All @@ -21,6 +21,14 @@ export default function DisableNonPageContentBlocks() {
const contentOnlyIds = useSelect( ( select ) => {
const { getBlocksByName, getBlockParents, getBlockName } =
select( blockEditorStore );

const canEditTemplate =
select( coreStore ).canUser( 'create', 'templates' ) ?? false;

if ( canEditTemplate ) {
CONTENT_ONLY_BLOCKS.push( 'core/template-part' );
}

return getBlocksByName( CONTENT_ONLY_BLOCKS ).filter( ( clientId ) =>
getBlockParents( clientId ).every( ( parentClientId ) => {
const parentBlockName = getBlockName( parentClientId );
Expand Down
Expand Up @@ -40,34 +40,44 @@ describe( 'DisableNonPageContentBlocks', () => {
type: 'UNSET_BLOCK_EDITING_MODE',
} ) );

const registry = createRegistry( {
'core/block-editor': {
reducer: () => {},
selectors: {
getBlocksByName( state, blockNames ) {
return Object.keys( testBlocks ).filter( ( clientId ) =>
blockNames.includes( testBlocks[ clientId ] )
);
},
getBlockParents( state, clientId ) {
return clientId.slice( 0, -1 ).split( '' );
},
getBlockName( state, clientId ) {
return testBlocks[ clientId ];
},
getBlockOrder( state, rootClientId ) {
return Object.keys( testBlocks ).filter(
( clientId ) =>
clientId.startsWith( rootClientId ) &&
clientId !== rootClientId
);
},
const registry = createRegistry();

registry.registerStore( 'core/block-editor', {
reducer: () => {},
selectors: {
getBlocksByName( state, blockNames ) {
return Object.keys( testBlocks ).filter( ( clientId ) =>
blockNames.includes( testBlocks[ clientId ] )
);
},
getBlockParents( state, clientId ) {
return clientId.slice( 0, -1 ).split( '' );
},
getBlockName( state, clientId ) {
return testBlocks[ clientId ];
},
getBlockOrder( state, rootClientId ) {
return Object.keys( testBlocks ).filter(
( clientId ) =>
clientId.startsWith( rootClientId ) &&
clientId !== rootClientId
);
},
actions: {
setBlockEditingMode,
unsetBlockEditingMode,
},
actions: {
setBlockEditingMode,
unsetBlockEditingMode,
},
} );

registry.registerStore( 'core', {
reducer: () => {},
selectors: {
canUser() {
return true;
},
},
actions: {},
} );

const { unmount } = render(
Expand Down