diff --git a/backport-changelog/6.9/8063.md b/backport-changelog/6.9/8063.md index 8a4613e59e2bd0..d882c897d252e6 100644 --- a/backport-changelog/6.9/8063.md +++ b/backport-changelog/6.9/8063.md @@ -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 diff --git a/lib/compat/wordpress-6.9/template-activate.php b/lib/compat/wordpress-6.9/template-activate.php index fdbbb5c098a958..53595d129c3240 100644 --- a/lib/compat/wordpress-6.9/template-activate.php +++ b/lib/compat/wordpress-6.9/template-activate.php @@ -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. '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', ) ); @@ -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 ); - if ( $active_templates ) { + if ( false !== $active_templates ) { return; } diff --git a/packages/core-data/src/private-selectors.ts b/packages/core-data/src/private-selectors.ts index 560ba6e628686b..d9bc2214c860c0 100644 --- a/packages/core-data/src/private-selectors.ts +++ b/packages/core-data/src/private-selectors.ts @@ -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; } } // If no template is assigned, use the default template. diff --git a/packages/edit-site/src/components/dataviews-actions/index.js b/packages/edit-site/src/components/dataviews-actions/index.js index 826482d3efa205..323d93fa7bff9d 100644 --- a/packages/edit-site/src/components/dataviews-actions/index.js +++ b/packages/edit-site/src/components/dataviews-actions/index.js @@ -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; await editEntityRecord( 'root', 'site', undefined, { active_templates: activeTemplates, } ); diff --git a/packages/editor/src/components/post-template/block-theme.js b/packages/editor/src/components/post-template/block-theme.js index d457cfba91fac6..a6256a4c0151dd 100644 --- a/packages/editor/src/components/post-template/block-theme.js +++ b/packages/editor/src/components/post-template/block-theme.js @@ -31,10 +31,15 @@ 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, @@ -42,6 +47,7 @@ export default function BlockThemeControl( { id } ) { hasGoBack: editorSettings.hasOwnProperty( 'onNavigateToPreviousEntityRecord' ), + hasSpecificTemplate: !! currentPost.template, }; }, [] ); @@ -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 ) @@ -126,11 +134,45 @@ export default function BlockThemeControl( { id } ) { { canCreateTemplate && ( { + 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(); } }