Site Editor > Templates: move config to the server#76622
Conversation
|
Size Change: +90 B (0%) Total Size: 7.66 MB
ℹ️ View Unchanged
|
|
Flaky tests detected in 015e3dd. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23534338310
|
d8724eb to
05d6187
Compare
da48b75 to
9cd9fa5
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
There are 4 template screens and 3 sidebars for templates — accounting for the "site editor extensibility" and "template activation" experiments. This PR only migrates the main screen. Additionally, the main screen (no experiments enabled) contents for sidebar and stage are called "legacy", so it's all a bit disorientating. Noted the other ones as follow-ups at #76544 |
| const authorSourceMap = useSelect( ( select ) => { | ||
| const templates = select( coreStore ).getEntityRecords( | ||
| 'postType', | ||
| TEMPLATE_POST_TYPE, | ||
| { per_page: -1 } | ||
| ); | ||
| }, [ records ] ); | ||
| if ( ! templates ) { | ||
| return {}; | ||
| } | ||
| const map = {}; | ||
| for ( const template of templates ) { | ||
| if ( | ||
| template.author_text && | ||
| template.original_source && | ||
| ! map[ template.author_text ] | ||
| ) { | ||
| map[ template.author_text ] = template.original_source; | ||
| } | ||
| } | ||
| return map; | ||
| }, [] ); |
There was a problem hiding this comment.
Like the pages PR, this PR adds the icons client-side. The situation here is different, in that the icon is derived from the source of the view (is the template from an user? from a theme? from a plugin?).
| user: commentAuthorAvatar, | ||
| theme: layout, | ||
| plugin: pluginIcon, | ||
| site: globe, |
There was a problem hiding this comment.
This is using the same icons as useAddedBy hook.
| /** | ||
| * Returns the original source of a template. | ||
| * | ||
| * @param WP_Block_Template $template_object Template instance. | ||
| * @return string The original source ('theme', 'plugin', 'site', or 'user'). | ||
| */ | ||
| private static function get_template_original_source( $template_object ) { | ||
| if ( 'wp_template' === $template_object->type || 'wp_template_part' === $template_object->type ) { | ||
| if ( $template_object->has_theme_file && | ||
| ( 'theme' === $template_object->origin || ( | ||
| empty( $template_object->origin ) && in_array( | ||
| $template_object->source, | ||
| array( | ||
| 'theme', | ||
| 'custom', | ||
| ), | ||
| true | ||
| ) ) | ||
| ) | ||
| ) { | ||
| return 'theme'; | ||
| } | ||
|
|
||
| if ( 'plugin' === $template_object->origin ) { | ||
| return 'plugin'; | ||
| } | ||
|
|
||
| if ( empty( $template_object->has_theme_file ) && 'custom' === $template_object->source && empty( $template_object->author ) ) { | ||
| return 'site'; | ||
| } | ||
| } | ||
|
|
||
| return 'user'; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a human readable text for the author of a template. | ||
| * | ||
| * @param WP_Block_Template $template_object Template instance. | ||
| * @param string $original_source The original source of the template. | ||
| * @return string Human readable text for the author. | ||
| */ | ||
| private static function get_template_author_text( $template_object, $original_source ) { | ||
| switch ( $original_source ) { | ||
| case 'theme': | ||
| $theme_name = wp_get_theme( $template_object->theme )->get( 'Name' ); | ||
| return empty( $theme_name ) ? $template_object->theme : $theme_name; | ||
| case 'plugin': | ||
| if ( ! function_exists( 'get_plugins' ) ) { | ||
| require_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||
| } | ||
| if ( isset( $template_object->plugin ) ) { | ||
| $plugins = wp_get_active_and_valid_plugins(); | ||
|
|
||
| foreach ( $plugins as $plugin_file ) { | ||
| $plugin_basename = plugin_basename( $plugin_file ); | ||
| list( $plugin_slug, ) = explode( '/', $plugin_basename ); | ||
|
|
||
| if ( $plugin_slug === $template_object->plugin ) { | ||
| $plugin_data = get_plugin_data( $plugin_file ); | ||
|
|
||
| if ( ! empty( $plugin_data['Name'] ) ) { | ||
| return $plugin_data['Name']; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $plugins = get_plugins(); | ||
| $plugin_basename = plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) ); | ||
| if ( isset( $plugins[ $plugin_basename ] ) && isset( $plugins[ $plugin_basename ]['Name'] ) ) { | ||
| return $plugins[ $plugin_basename ]['Name']; | ||
| } | ||
| return $template_object->plugin ?? $template_object->theme; | ||
| case 'site': | ||
| return get_bloginfo( 'name' ); | ||
| case 'user': | ||
| $author = get_user_by( 'id', $template_object->author ); | ||
| if ( ! $author ) { | ||
| return __( 'Unknown author', 'gutenberg' ); | ||
| } | ||
| return $author->get( 'display_name' ); | ||
| } | ||
|
|
||
| return ''; | ||
| } |
There was a problem hiding this comment.
These utilities have been copied from the existing templates REST endpoint. It seems this info may need a better way to be encapsulated.
There was a problem hiding this comment.
I believe it would be fine to make get_wp_templates_author_text_field function private and since it's static, call it to get the value without duplicating anything.
There was a problem hiding this comment.
I'm not comfortable making them public since I'm not sure this is the best API.
9ca365a to
d4cf7db
Compare
Co-authored-by: oandregal <oandregal@git.wordpress.org> Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org> Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org> Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Part of #76544
Backported at WordPress/wordpress-develop#11272
What?
Moves the template view configuration (default view, layouts, and view list) from client-side JavaScript (page-templates/view-utils.js) to the server-side REST endpoint (/wp/v2/view-config), continuing the pattern established for pages in #76573
Why?
See #76544
This is part of an ongoing effort to centralize view configuration on the server so that it can be managed, extended, and overridden consistently across entity types.
How?
useViewConfighook.Testing Instructions
Test the Site Editor > Templates screen.
Use of AI Tools
This PR was authored with Claude Code (Claude Opus 4.6).