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

Template Part: Add fallback to the current theme when not provided #55965

Merged
merged 1 commit into from
Nov 14, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ export default function useTemplatePartAreaLabel( clientId ) {
'core/editor'
).__experimentalGetDefaultTemplatePartAreas();
/* eslint-enable @wordpress/data-no-store-string-literals */
const { getEditedEntityRecord } = select( coreStore );
const { getCurrentTheme, getEditedEntityRecord } =
select( coreStore );

for ( const templatePartClientId of parentTemplatePartClientIds ) {
const templatePartBlock = getBlock( templatePartClientId );

// The 'area' usually isn't stored on the block, but instead
// on the entity.
const { theme, slug } = templatePartBlock.attributes;
const { theme = getCurrentTheme()?.stylesheet, slug } =
templatePartBlock.attributes;
const templatePartEntityId = createTemplatePartId(
theme,
slug
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const PatternEdit = ( { attributes, clientId } ) => {
);

const currentThemeStylesheet = useSelect(
( select ) => select( coreStore ).getCurrentTheme()?.stylesheet
( select ) => select( coreStore ).getCurrentTheme()?.stylesheet,
[]
);

const { replaceBlocks, __unstableMarkNextChangeAsNotPersistent } =
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export default function TemplatePartEdit( {
setAttributes,
clientId,
} ) {
const { slug, theme, tagName, layout = {} } = attributes;
const currentTheme = useSelect(
( select ) => select( coreStore ).getCurrentTheme()?.stylesheet,
[]
);
const { slug, theme = currentTheme, tagName, layout = {} } = attributes;
const templatePartId = createTemplatePartId( theme, slug );
const hasAlreadyRendered = useHasRecursion( templatePartId );
const [ isTemplatePartSelectionOpen, setIsTemplatePartSelectionOpen ] =
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/template-part/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export const settings = {
return;
}

const entity = select( coreDataStore ).getEntityRecord(
const { getCurrentTheme, getEntityRecord } = select( coreDataStore );
const entity = getEntityRecord(
'postType',
'wp_template_part',
theme + '//' + slug
( theme || getCurrentTheme()?.stylesheet ) + '//' + slug
);
if ( ! entity ) {
return;
Expand Down
6 changes: 4 additions & 2 deletions packages/block-library/src/template-part/variations.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ export function enhanceTemplatePartVariations( settings, name ) {
// Find a matching variation from the created template part
// by checking the entity's `area` property.
if ( ! slug ) return false;
const entity = select( coreDataStore ).getEntityRecord(
const { getCurrentTheme, getEntityRecord } =
select( coreDataStore );
const entity = getEntityRecord(
'postType',
'wp_template_part',
`${ theme }//${ slug }`
`${ theme || getCurrentTheme()?.stylesheet }//${ slug }`
);

if ( entity?.slug ) {
Expand Down
6 changes: 4 additions & 2 deletions packages/edit-site/src/hooks/template-part-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ function EditTemplatePartMenuItem( { attributes } ) {
const { params } = useLocation();
const templatePart = useSelect(
( select ) => {
return select( coreStore ).getEntityRecord(
const { getCurrentTheme, getEntityRecord } = select( coreStore );

return getEntityRecord(
'postType',
TEMPLATE_PART_POST_TYPE,
// Ideally this should be an official public API.
`${ theme }//${ slug }`
`${ theme || getCurrentTheme()?.stylesheet }//${ slug }`
);
},
[ theme, slug ]
Expand Down
Loading