Repository navigation
Template activation: allow empty array to be set #72011
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
Changes from all commits
cb8e88f
b31bd4e
153a4d8
09be74c
dbfe960
8cff955
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 |
|---|---|---|
|
|
@@ -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 ); | ||
|
Contributor
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. FYI,
Member
Author
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. 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; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Member
Author
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'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. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Member
Author
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. This no longer seems needed either. We'll see if the tests pass. |
||
| await editEntityRecord( 'root', 'site', undefined, { | ||
| active_templates: activeTemplates, | ||
| } ); | ||
|
|
||
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.
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.
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 thought it would only affect the return value in case the option didn't exist.