From e44ba473daa2821d5ae38fc83e1023282d404661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20K=C3=A4gy?= Date: Wed, 17 Apr 2024 12:40:21 +0200 Subject: [PATCH] Enable template preview in post editor for non administrators (#60447) Co-authored-by: fabiankaegy Co-authored-by: youknowriad Co-authored-by: noisysocks --- .../src/template-part/edit/index.js | 35 +++++++----- packages/edit-post/src/editor.js | 4 +- .../edit-template-blocks-notification.js | 16 +++++- .../components/post-template/block-theme.js | 53 +++++++++++-------- 4 files changed, 69 insertions(+), 39 deletions(-) diff --git a/packages/block-library/src/template-part/edit/index.js b/packages/block-library/src/template-part/edit/index.js index 5af3ede02b677e..52d740735eaf61 100644 --- a/packages/block-library/src/template-part/edit/index.js +++ b/packages/block-library/src/template-part/edit/index.js @@ -128,6 +128,7 @@ export default function TemplatePartEdit( { area, onNavigateToEntityRecord, title, + canEditTemplate, } = useSelect( ( select ) => { const { getEditedEntityRecord, hasFinishedResolution } = @@ -150,6 +151,9 @@ export default function TemplatePartEdit( { ) : false; + const _canEditTemplate = + select( coreStore ).canUser( 'create', 'templates' ) ?? false; + return { hasInnerBlocks: getBlockCount( clientId ) > 0, isResolved: hasResolvedEntity, @@ -161,6 +165,7 @@ export default function TemplatePartEdit( { onNavigateToEntityRecord: getSettings().onNavigateToEntityRecord, title: entityRecord?.title, + canEditTemplate: _canEditTemplate, }; }, [ templatePartId, attributes.area, clientId ] @@ -228,20 +233,22 @@ export default function TemplatePartEdit( { return ( <> - { isEntityAvailable && onNavigateToEntityRecord && ( - - - onNavigateToEntityRecord( { - postId: templatePartId, - postType: 'wp_template_part', - } ) - } - > - { __( 'Edit' ) } - - - ) } + { isEntityAvailable && + onNavigateToEntityRecord && + canEditTemplate && ( + + + onNavigateToEntityRecord( { + postId: templatePartId, + postType: 'wp_template_part', + } ) + } + > + { __( 'Edit' ) } + + + ) } + select( coreStore ).canUser( 'create', 'templates' ) ?? false + ); + const [ isDialogOpen, setIsDialogOpen ] = useState( false ); useEffect( () => { const handleDblClick = ( event ) => { + if ( ! canEditTemplate ) { + return; + } + if ( ! event.target.classList.contains( 'is-root-container' ) ) { return; } @@ -52,7 +62,11 @@ export default function EditTemplateBlocksNotification( { contentRef } ) { return () => { canvas?.removeEventListener( 'dblclick', handleDblClick ); }; - }, [ contentRef ] ); + }, [ contentRef, canEditTemplate ] ); + + if ( ! canEditTemplate ) { + return null; + } return ( + select( coreStore ).canUser( 'create', 'templates' ) ?? false + ); + if ( ! hasResolved ) { return null; } @@ -81,30 +86,34 @@ export default function BlockThemeControl( { id } ) { { ( { onClose } ) => ( <> - { - onNavigateToEntityRecord( { - postId: template.id, - postType: 'wp_template', - } ); - onClose(); - createSuccessNotice( - __( - 'Editing template. Changes made here affect all posts and pages that use the template.' - ), - { - type: 'snackbar', - actions: notificationAction, - } - ); - } } - > - { __( 'Edit template' ) } - + { canCreateTemplate && ( + { + onNavigateToEntityRecord( { + postId: template.id, + postType: 'wp_template', + } ); + onClose(); + createSuccessNotice( + __( + 'Editing template. Changes made here affect all posts and pages that use the template.' + ), + { + type: 'snackbar', + actions: notificationAction, + } + ); + } } + > + { __( 'Edit template' ) } + + ) } - + { canCreateTemplate && ( + + ) }