Skip to content
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
1 change: 1 addition & 0 deletions backport-changelog/6.9/8063.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ https://github.com/WordPress/wordpress-develop/pull/8063
* https://github.com/WordPress/gutenberg/pull/71811
* https://github.com/WordPress/gutenberg/pull/72029
* https://github.com/WordPress/gutenberg/pull/72049
* https://github.com/WordPress/gutenberg/pull/72011
10 changes: 6 additions & 4 deletions lib/compat/wordpress-6.9/template-activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ function gutenberg_setup_static_template() {
'active_templates',
array(
'type' => 'object',
// Do not set the default value to an empty array! For some reason
// that will prevent the option from being set to an empty array.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm guessing it's like the "block defaults", when the value you want to set equals the default value it's not persisted at all.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, I thought it would only affect the return value in case the option didn't exist.

'show_in_rest' => array(
'schema' => array(
'type' => 'object',
// properties can be integers or false (deactivated).
// Properties can be integers, strings, or false
// (deactivated).
'additionalProperties' => true,
),
),
'default' => array(),
'label' => 'Active Templates',
)
);
Expand Down Expand Up @@ -438,9 +440,9 @@ function gutenberg_set_active_template_theme( $changes, $request ) {
// is active.
add_action( 'init', 'gutenberg_migrate_existing_templates' );
function gutenberg_migrate_existing_templates() {
$active_templates = get_option( 'active_templates' );
$active_templates = get_option( 'active_templates', false );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

FYI, false is already the default value for that argument.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I saw, but I wanted to be explicit so it's clearly relating to the condition below


if ( $active_templates ) {
if ( false !== $active_templates ) {
return;
}

Expand Down
37 changes: 32 additions & 5 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,38 @@ export const getTemplateId = createRegistrySelector(
// First see if the post/page has an assigned template and fetch it.
const currentTemplateSlug = editedEntity.template;
if ( currentTemplateSlug ) {
const currentTemplate = select( STORE_NAME ).getDefaultTemplateId( {
slug: currentTemplateSlug,
} );
if ( currentTemplate ) {
return currentTemplate;
const userTemplates = select( STORE_NAME ).getEntityRecords(
'postType',
'wp_template',
{ per_page: -1 }
);
if ( ! userTemplates ) {
return;
}
const userTemplateWithSlug = userTemplates.find(
( { slug } ) => slug === currentTemplateSlug
);

if ( userTemplateWithSlug ) {
return userTemplateWithSlug.id;
}

const registeredTemplates = select( STORE_NAME ).getEntityRecords(
'postType',
'wp_registered_template',
{ per_page: -1 }
);

if ( ! registeredTemplates ) {
return;
}

const registeredTemplateWithSlug = registeredTemplates.find(
( { slug } ) => slug === currentTemplateSlug
);

if ( registeredTemplateWithSlug ) {
return registeredTemplateWithSlug.id;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm reverting what I did in https://github.com/WordPress/gutenberg/pull/72049/files#diff-166c46de8d08a1bf4740eae54f4a8fa2296684af9f318ec0baa31f248a9c44be, which was wrong. We shouldn't try to resolve a template for this slug. We should check the list of templates we have, and due to the changes we need to check both registered and user templates. In the long term we should really add a way to query the endpoint by slug.

}
}
// If no template is assigned, use the default template.
Expand Down
4 changes: 0 additions & 4 deletions packages/edit-site/src/components/dataviews-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ export const useSetActiveTemplateAction = () => {
activeTemplates[ item.slug ] = item.id;
}
}
// To do: figure out why the REST API deletes the option when
// it's set to an empty object. That would trigger the migration
// function, which will make all templates in the database active.
activeTemplates.__preventCollapse = 0;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This no longer seems needed either. We'll see if the tests pass.

await editEntityRecord( 'root', 'site', undefined, {
active_templates: activeTemplates,
} );
Expand Down
48 changes: 45 additions & 3 deletions packages/editor/src/components/post-template/block-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ export default function BlockThemeControl( { id } ) {
onNavigateToEntityRecord,
getEditorSettings,
hasGoBack,
hasSpecificTemplate,
} = useSelect( ( select ) => {
const { getRenderingMode, getEditorSettings: _getEditorSettings } =
unlock( select( editorStore ) );
const {
getRenderingMode,
getEditorSettings: _getEditorSettings,
getCurrentPost,
} = unlock( select( editorStore ) );
const editorSettings = _getEditorSettings();
const currentPost = getCurrentPost();
return {
isTemplateHidden: getRenderingMode() === 'post-only',
onNavigateToEntityRecord: editorSettings.onNavigateToEntityRecord,
getEditorSettings: _getEditorSettings,
hasGoBack: editorSettings.hasOwnProperty(
'onNavigateToPreviousEntityRecord'
),
hasSpecificTemplate: !! currentPost.template,
};
}, [] );

Expand All @@ -52,6 +58,8 @@ export default function BlockThemeControl( { id } ) {
'wp_template',
id
);
const { getEntityRecord } = useSelect( coreStore );
const { editEntityRecord } = useDispatch( coreStore );
const { createSuccessNotice } = useDispatch( noticesStore );
const { setRenderingMode, setDefaultRenderingMode } = unlock(
useDispatch( editorStore )
Expand Down Expand Up @@ -126,11 +134,45 @@ export default function BlockThemeControl( { id } ) {
<MenuGroup>
{ canCreateTemplate && (
<MenuItem
onClick={ () => {
onClick={ async () => {
onNavigateToEntityRecord( {
postId: template.id,
postType: 'wp_template',
} );
// When editing a global template,
// activate the auto-draft. This is not
// immediately live (we're not saving
// site options), and when nothing is
// saved, the setting will be ignored.
// In the future, we should make the
// duplication explicit, so there
// wouldn't be an "edit" button for
// static theme templates.
if ( ! hasSpecificTemplate ) {
const activeTemplates =
await getEntityRecord(
'root',
'site'
).active_templates;
if (
activeTemplates[
template.slug
] !== template.id
) {
editEntityRecord(
'root',
'site',
undefined,
{
active_templates: {
...activeTemplates,
[ template.slug ]:
template.id,
},
}
);
}
}
onClose();
mayShowTemplateEditNotice();
} }
Expand Down
Loading