-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Edit Site: Fetch template parts in Template Switcher from REST API #21878
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2,8 +2,8 @@ | |||
* WordPress dependencies | ||||
*/ | ||||
import { __ } from '@wordpress/i18n'; | ||||
import { useSelect } from '@wordpress/data'; | ||||
import { useState } from '@wordpress/element'; | ||||
import { useRegistry, useSelect } from '@wordpress/data'; | ||||
import { useEffect, useState } from '@wordpress/element'; | ||||
import { | ||||
Tooltip, | ||||
DropdownMenu, | ||||
|
@@ -16,6 +16,7 @@ import { Icon, home, plus, undo } from '@wordpress/icons'; | |||
/** | ||||
* Internal dependencies | ||||
*/ | ||||
import { findTemplate } from '../../utils'; | ||||
import TemplatePreview from './template-preview'; | ||||
import ThemePreview from './theme-preview'; | ||||
|
||||
|
@@ -46,11 +47,9 @@ function TemplateLabel( { template, homeId } ) { | |||
} | ||||
|
||||
export default function TemplateSwitcher( { | ||||
templatePartIds, | ||||
page, | ||||
activeId, | ||||
activeTemplatePartId, | ||||
homeId, | ||||
isTemplatePart, | ||||
onActiveIdChange, | ||||
onActiveTemplatePartIdChange, | ||||
|
@@ -70,52 +69,64 @@ export default function TemplateSwitcher( { | |||
setThemePreviewVisible( () => false ); | ||||
}; | ||||
|
||||
const registry = useRegistry(); | ||||
const [ homeId, setHomeId ] = useState(); | ||||
|
||||
useEffect( () => { | ||||
findTemplate( | ||||
'/', | ||||
registry.__experimentalResolveSelect( 'core' ).getEntityRecords | ||||
).then( | ||||
( newHomeId ) => setHomeId( newHomeId ), | ||||
() => setHomeId( null ) | ||||
); | ||||
Comment on lines
+76
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's great that we now have the Not relevant for this PR, but I wanted to point it out in case we'd be willing to reconsider later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could wrap it in a custom hook too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolvers need to be tied to the store state, and it would be awkward to have these temporary values there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't introduce a dedicated resolver for Or we could even modify the entity endpoints for templates and template parts some more to accept a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That makes more sense don't you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly, yeah. I'm not sure if it'd maybe bend the entities abstraction a bit much (if we keep adding rather specific custom args to it that change the query significantly), but maybe that's alright. Think it still makes sense to merge this PR as a first step towards fetching data from the REST API (rather than from a server-passed JS variable)? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather avoid adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Unfortunately, doing the path-to-template resolution on the server side will be non-trivial AFAICS -- the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leave it for later then. |
||||
}, [ registry ] ); | ||||
|
||||
const { currentTheme, template, templateParts } = useSelect( | ||||
( select ) => { | ||||
const { getCurrentTheme, getEntityRecord } = select( 'core' ); | ||||
const { | ||||
getCurrentTheme, | ||||
getEntityRecord, | ||||
getEntityRecords, | ||||
} = select( 'core' ); | ||||
|
||||
const _template = getEntityRecord( | ||||
'postType', | ||||
'wp_template', | ||||
activeId | ||||
); | ||||
|
||||
return { | ||||
currentTheme: getCurrentTheme(), | ||||
template: { | ||||
label: _template ? ( | ||||
<TemplateLabel | ||||
template={ _template } | ||||
homeId={ homeId } | ||||
/> | ||||
) : ( | ||||
__( 'Loading…' ) | ||||
), | ||||
value: activeId, | ||||
slug: _template ? _template.slug : __( 'Loading…' ), | ||||
content: _template?.content, | ||||
}, | ||||
templateParts: templatePartIds.map( ( id ) => { | ||||
const templatePart = getEntityRecord( | ||||
'postType', | ||||
'wp_template_part', | ||||
id | ||||
); | ||||
return { | ||||
label: templatePart ? ( | ||||
<TemplateLabel template={ templatePart } /> | ||||
) : ( | ||||
__( 'Loading…' ) | ||||
), | ||||
value: id, | ||||
slug: templatePart | ||||
? templatePart.slug | ||||
: __( 'Loading…' ), | ||||
}; | ||||
} ), | ||||
template: _template, | ||||
templateParts: _template | ||||
? getEntityRecords( 'postType', 'wp_template_part', { | ||||
resolved: true, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #21878 (comment) |
||||
template: _template.slug, | ||||
} ) | ||||
: null, | ||||
}; | ||||
}, | ||||
[ activeId, templatePartIds, homeId ] | ||||
[ activeId ] | ||||
); | ||||
|
||||
const templateItem = { | ||||
label: template ? ( | ||||
<TemplateLabel template={ template } homeId={ homeId } /> | ||||
) : ( | ||||
__( 'Loading…' ) | ||||
), | ||||
value: activeId, | ||||
slug: template ? template.slug : __( 'Loading…' ), | ||||
content: template?.content, | ||||
}; | ||||
|
||||
const templatePartItems = templateParts?.map( ( templatePart ) => ( { | ||||
label: <TemplateLabel template={ templatePart } />, | ||||
value: templatePart.id, | ||||
slug: templatePart.slug, | ||||
} ) ); | ||||
|
||||
const overwriteSlug = | ||||
TEMPLATE_OVERRIDES[ page.type ] && | ||||
page.slug && | ||||
|
@@ -125,10 +136,10 @@ export default function TemplateSwitcher( { | |||
slug: overwriteSlug, | ||||
title: overwriteSlug, | ||||
status: 'publish', | ||||
content: template.content.raw, | ||||
content: templateItem.content.raw, | ||||
} ); | ||||
const revertToParent = async () => { | ||||
onRemoveTemplate( template.value ); | ||||
onRemoveTemplate( activeId ); | ||||
}; | ||||
return ( | ||||
<> | ||||
|
@@ -141,8 +152,8 @@ export default function TemplateSwitcher( { | |||
label={ __( 'Switch Template' ) } | ||||
toggleProps={ { | ||||
children: ( isTemplatePart | ||||
? templateParts | ||||
: [ template ] | ||||
? templatePartItems | ||||
: [ templateItem ] | ||||
).find( | ||||
( choice ) => | ||||
choice.value === | ||||
|
@@ -156,18 +167,18 @@ export default function TemplateSwitcher( { | |||
<MenuItem | ||||
onClick={ () => onActiveIdChange( activeId ) } | ||||
> | ||||
{ template.label } | ||||
{ templateItem.label } | ||||
</MenuItem> | ||||
{ overwriteSlug && | ||||
overwriteSlug !== template.slug && ( | ||||
overwriteSlug !== templateItem.slug && ( | ||||
<MenuItem | ||||
icon={ plus } | ||||
onClick={ overwriteTemplate } | ||||
> | ||||
{ __( 'Overwrite Template' ) } | ||||
</MenuItem> | ||||
) } | ||||
{ overwriteSlug === template.slug && ( | ||||
{ overwriteSlug === templateItem.slug && ( | ||||
<MenuItem | ||||
icon={ undo } | ||||
onClick={ revertToParent } | ||||
|
@@ -178,7 +189,7 @@ export default function TemplateSwitcher( { | |||
</MenuGroup> | ||||
<MenuGroup label={ __( 'Template Parts' ) }> | ||||
<MenuItemsChoice | ||||
choices={ templateParts } | ||||
choices={ templatePartItems } | ||||
value={ | ||||
isTemplatePart | ||||
? activeTemplatePartId | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REST_REQUEST
isn't defined for internal requests done withrest_do_request
and generally it isn't encouraged. I see that it is being used elsewhere in this function, but I think it'd be better to change this conditional to use a parameter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I was indeed copy-pasting here. If permissible, I'd like to address this in a later iteration/separate PR, since it's pre-existing, and I'd have to read up a bit on this (and would rather not block this PR by that issue 😅 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think it's just something to be looked at some point.