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

Enable template preview in post editor for non administrators #60447

Merged
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
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;

Comment on lines +154 to +156
Copy link
Member

Choose a reason for hiding this comment

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

Is this check correct? Should it check if a user can edit templates or template parts?

Copy link
Member Author

Choose a reason for hiding this comment

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

@Mamaduka As far as I know they use one singular REST Controler for both. And that lives under templates 🤔

I may of course have missed something

Copy link
Member

Choose a reason for hiding this comment

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

They have different rest bases - templates and template-parts.

I'm asking because I'm working to bulk update canUser selectors, and I would instead use the correct post type for new checks. See #63415.

Copy link
Member

Choose a reason for hiding this comment

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

I've updated to the correct entity in #63415.

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 &&
Copy link
Contributor

Choose a reason for hiding this comment

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

This is potentially the most impactful change because it's not clear exactly what the availability of the template changes in the editor.

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
Loading