Skip to content

Commit

Permalink
Enable template preview in post editor for non administrators (#60447)
Browse files Browse the repository at this point in the history
Co-authored-by: fabiankaegy <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: noisysocks <[email protected]>
  • Loading branch information
4 people authored Apr 17, 2024
1 parent b9a29cd commit e44ba47
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 39 deletions.
35 changes: 21 additions & 14 deletions packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export default function TemplatePartEdit( {
area,
onNavigateToEntityRecord,
title,
canEditTemplate,
} = useSelect(
( select ) => {
const { getEditedEntityRecord, hasFinishedResolution } =
Expand All @@ -150,6 +151,9 @@ export default function TemplatePartEdit( {
)
: false;

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

return {
hasInnerBlocks: getBlockCount( clientId ) > 0,
isResolved: hasResolvedEntity,
Expand All @@ -161,6 +165,7 @@ export default function TemplatePartEdit( {
onNavigateToEntityRecord:
getSettings().onNavigateToEntityRecord,
title: entityRecord?.title,
canEditTemplate: _canEditTemplate,
};
},
[ templatePartId, attributes.area, clientId ]
Expand Down Expand Up @@ -228,20 +233,22 @@ export default function TemplatePartEdit( {
return (
<>
<RecursionProvider uniqueId={ templatePartId }>
{ isEntityAvailable && onNavigateToEntityRecord && (
<BlockControls group="other">
<ToolbarButton
onClick={ () =>
onNavigateToEntityRecord( {
postId: templatePartId,
postType: 'wp_template_part',
} )
}
>
{ __( 'Edit' ) }
</ToolbarButton>
</BlockControls>
) }
{ isEntityAvailable &&
onNavigateToEntityRecord &&
canEditTemplate && (
<BlockControls group="other">
<ToolbarButton
onClick={ () =>
onNavigateToEntityRecord( {
postId: templatePartId,
postType: 'wp_template_part',
} )
}
>
{ __( 'Edit' ) }
</ToolbarButton>
</BlockControls>
) }
<InspectorControls group="advanced">
<TemplatePartAdvancedControls
tagName={ tagName }
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ function Editor( {
getEditorSettings().supportsTemplateMode;
const isViewable =
getPostType( currentPost.postType )?.viewable ?? false;
const canEditTemplate = canUser( 'create', 'templates' );
const canViewTemplate = canUser( 'read', 'templates' );
return {
template:
supportsTemplateMode &&
isViewable &&
canEditTemplate &&
canViewTemplate &&
currentPost.postType !== 'wp_template'
? getEditedPostTemplate()
: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { __experimentalConfirmDialog as ConfirmDialog } from '@wordpress/components';
Expand Down Expand Up @@ -37,10 +38,19 @@ export default function EditTemplateBlocksNotification( { contentRef } ) {
};
}, [] );

const canEditTemplate = useSelect(
( select ) =>
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;
}
Expand All @@ -52,7 +62,11 @@ export default function EditTemplateBlocksNotification( { contentRef } ) {
return () => {
canvas?.removeEventListener( 'dblclick', handleDblClick );
};
}, [ contentRef ] );
}, [ contentRef, canEditTemplate ] );

if ( ! canEditTemplate ) {
return null;
}

return (
<ConfirmDialog
Expand Down
53 changes: 31 additions & 22 deletions packages/editor/src/components/post-template/block-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEntityRecord } from '@wordpress/core-data';
import { useEntityRecord, store as coreStore } from '@wordpress/core-data';
import { check } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';

Expand Down Expand Up @@ -51,6 +51,11 @@ export default function BlockThemeControl( { id } ) {
const { createSuccessNotice } = useDispatch( noticesStore );
const { setRenderingMode } = useDispatch( editorStore );

const canCreateTemplate = useSelect(
( select ) =>
select( coreStore ).canUser( 'create', 'templates' ) ?? false
);

if ( ! hasResolved ) {
return null;
}
Expand Down Expand Up @@ -81,30 +86,34 @@ export default function BlockThemeControl( { id } ) {
{ ( { onClose } ) => (
<>
<MenuGroup>
<MenuItem
onClick={ () => {
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' ) }
</MenuItem>
{ canCreateTemplate && (
<MenuItem
onClick={ () => {
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' ) }
</MenuItem>
) }

<SwapTemplateButton onClick={ onClose } />
<ResetDefaultTemplate onClick={ onClose } />
<CreateNewTemplate onClick={ onClose } />
{ canCreateTemplate && (
<CreateNewTemplate onClick={ onClose } />
) }
</MenuGroup>
<MenuGroup>
<MenuItem
Expand Down

1 comment on commit e44ba47

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in e44ba47.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8720496017
📝 Reported issues:

Please sign in to comment.