-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Edit Site: Add multiple template loading #19141
Changes from all commits
a5eb4ec
5a076af
b0734e0
dc02ce6
69ed750
27230c8
00d58b6
6d11ca6
fca9562
1ea222d
a74c4aa
16b2d88
0248569
0123540
88d8259
2abbb67
2ec8c5a
6c2ed22
a7db5ee
b326082
4c824f6
fd09ea0
66da25e
52a2906
4cf04ba
0f706ac
97e4846
858dc18
802909f
b4500e5
c6c16e4
c177284
54f012d
acc17a7
8d4cbcb
c78142b
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!-- wp:paragraph --> | ||
<p>Header Template Part</p> | ||
<!-- /wp:paragraph --> |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -28,7 +28,12 @@ class="edit-site" | |||||||||||||||||||||||||||||||||||||||
* @param string $hook Page. | ||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||
function gutenberg_edit_site_init( $hook ) { | ||||||||||||||||||||||||||||||||||||||||
global $_wp_current_template_id; | ||||||||||||||||||||||||||||||||||||||||
global | ||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_id, | ||||||||||||||||||||||||||||||||||||||||
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. Curious why do we need the globals here, given that these are not coming from core WP, but are just something that's been introduced for site editor (in 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. It's how we hijack template resolution to resolve block templates. |
||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_name, | ||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_content, | ||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_hierarchy, | ||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_part_ids; | ||||||||||||||||||||||||||||||||||||||||
if ( 'gutenberg_page_gutenberg-edit-site' !== $hook ) { | ||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
@@ -74,11 +79,53 @@ function gutenberg_edit_site_init( $hook ) { | |||||||||||||||||||||||||||||||||||||||
$settings['fontSizes'] = $font_sizes; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// Get root template by trigerring `./template-loader.php`'s logic. | ||||||||||||||||||||||||||||||||||||||||
// Get all templates by triggering `./template-loader.php`'s logic. | ||||||||||||||||||||||||||||||||||||||||
$template_getters = array( | ||||||||||||||||||||||||||||||||||||||||
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. Are we intentionally missing gutenberg/lib/template-loader.php Lines 23 to 41 in 56c043c
In which case the call below becomes something like: call_user_func( 'get_' . $template_type . '_template' ); 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 needs to match the code from Core template resolution; I think it would be premature to abstract it like that right now. |
||||||||||||||||||||||||||||||||||||||||
'get_embed_template', | ||||||||||||||||||||||||||||||||||||||||
'get_404_template', | ||||||||||||||||||||||||||||||||||||||||
'get_search_template', | ||||||||||||||||||||||||||||||||||||||||
'get_home_template', | ||||||||||||||||||||||||||||||||||||||||
'get_privacy_policy_template', | ||||||||||||||||||||||||||||||||||||||||
'get_post_type_archive_template', | ||||||||||||||||||||||||||||||||||||||||
'get_taxonomy_template', | ||||||||||||||||||||||||||||||||||||||||
'get_attachment_template', | ||||||||||||||||||||||||||||||||||||||||
'get_single_template', | ||||||||||||||||||||||||||||||||||||||||
'get_page_template', | ||||||||||||||||||||||||||||||||||||||||
'get_singular_template', | ||||||||||||||||||||||||||||||||||||||||
'get_category_template', | ||||||||||||||||||||||||||||||||||||||||
'get_tag_template', | ||||||||||||||||||||||||||||||||||||||||
'get_author_template', | ||||||||||||||||||||||||||||||||||||||||
'get_date_template', | ||||||||||||||||||||||||||||||||||||||||
'get_archive_template', | ||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||
$template_ids = array(); | ||||||||||||||||||||||||||||||||||||||||
$template_part_ids = array(); | ||||||||||||||||||||||||||||||||||||||||
foreach ( $template_getters as $template_getter ) { | ||||||||||||||||||||||||||||||||||||||||
call_user_func( $template_getter ); | ||||||||||||||||||||||||||||||||||||||||
apply_filters( 'template_include', null ); | ||||||||||||||||||||||||||||||||||||||||
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. Should we document this filter according to WordPress documentation standards? It might make this logic easier to grok. 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. Sure 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. But we are not following that convention in the rest of the plugin, are we? 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. Now that you mention it, I actually don't remember seeing it around. |
||||||||||||||||||||||||||||||||||||||||
if ( isset( $_wp_current_template_id ) ) { | ||||||||||||||||||||||||||||||||||||||||
$template_ids[ $_wp_current_template_name ] = $_wp_current_template_id; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
if ( isset( $_wp_current_template_part_ids ) ) { | ||||||||||||||||||||||||||||||||||||||||
$template_part_ids = $template_part_ids + $_wp_current_template_part_ids; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_id = null; | ||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_name = null; | ||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_content = null; | ||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_hierarchy = null; | ||||||||||||||||||||||||||||||||||||||||
$_wp_current_template_part_ids = null; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
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. It would be cool to extract all the logic here into a utility function 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. Good idea. It might be useful for plugin developers as well. 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. Should we do this as a follow-up? 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. Yes |
||||||||||||||||||||||||||||||||||||||||
get_front_page_template(); | ||||||||||||||||||||||||||||||||||||||||
get_index_template(); | ||||||||||||||||||||||||||||||||||||||||
apply_filters( 'template_include', null ); | ||||||||||||||||||||||||||||||||||||||||
$settings['templateId'] = $_wp_current_template_id; | ||||||||||||||||||||||||||||||||||||||||
$template_ids[ $_wp_current_template_name ] = $_wp_current_template_id; | ||||||||||||||||||||||||||||||||||||||||
if ( isset( $_wp_current_template_part_ids ) ) { | ||||||||||||||||||||||||||||||||||||||||
$template_part_ids = $template_part_ids + $_wp_current_template_part_ids; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
$settings['templateId'] = $_wp_current_template_id; | ||||||||||||||||||||||||||||||||||||||||
$settings['templateType'] = 'wp_template'; | ||||||||||||||||||||||||||||||||||||||||
$settings['templateIds'] = array_values( $template_ids ); | ||||||||||||||||||||||||||||||||||||||||
$settings['templatePartIds'] = array_values( $template_part_ids ); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// This is so other parts of the code can hook their own settings. | ||||||||||||||||||||||||||||||||||||||||
// Example: Global Styles. | ||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,24 +81,44 @@ function gutenberg_override_query_template( $template, $type, array $templates = | |
* @param array $block The root block to start traversing from. | ||
*/ | ||
function create_auto_draft_for_template_part_block( $block ) { | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( 'core/template-part' === $block['blockName'] && ! isset( $block['attrs']['id'] ) ) { | ||
$template_part_file_path = | ||
get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html'; | ||
if ( ! file_exists( $template_part_file_path ) ) { | ||
return; | ||
global $_wp_current_template_part_ids; | ||
|
||
if ( 'core/template-part' === $block['blockName'] ) { | ||
if ( ! isset( $block['attrs']['postId'] ) ) { | ||
$template_part_file_path = | ||
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. Do you think it would be worthwhile to extract this to 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. The demo block template code is temporary until we have a proper block-based theme; this will flatten once we can remove it. |
||
get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html'; | ||
if ( ! file_exists( $template_part_file_path ) ) { | ||
if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ) ) { | ||
$template_part_file_path = | ||
dirname( __FILE__ ) . '/demo-block-template-parts/' . $block['attrs']['slug'] . '.html'; | ||
if ( ! file_exists( $template_part_file_path ) ) { | ||
return; | ||
} | ||
} else { | ||
return; | ||
} | ||
} | ||
$template_part_id = wp_insert_post( | ||
array( | ||
'post_content' => file_get_contents( $template_part_file_path ), | ||
'post_title' => $block['attrs']['slug'], | ||
'post_status' => 'auto-draft', | ||
'post_type' => 'wp_template_part', | ||
'post_name' => $block['attrs']['slug'], | ||
'meta_input' => array( | ||
'theme' => $block['attrs']['theme'], | ||
), | ||
) | ||
); | ||
} else { | ||
$template_part_id = $block['attrs']['postId']; | ||
} | ||
|
||
if ( isset( $_wp_current_template_part_ids ) ) { | ||
$_wp_current_template_part_ids[ $block['attrs']['slug'] ] = $template_part_id; | ||
} else { | ||
$_wp_current_template_part_ids = array( $block['attrs']['slug'] => $template_part_id ); | ||
} | ||
wp_insert_post( | ||
array( | ||
'post_content' => file_get_contents( $template_part_file_path ), | ||
'post_title' => ucfirst( $block['attrs']['slug'] ), | ||
'post_status' => 'auto-draft', | ||
'post_type' => 'wp_template_part', | ||
'post_name' => $block['attrs']['slug'], | ||
'meta_input' => array( | ||
'theme' => $block['attrs']['theme'], | ||
), | ||
) | ||
); | ||
} | ||
|
||
foreach ( $block['innerBlocks'] as $inner_block ) { | ||
|
@@ -114,7 +134,7 @@ function create_auto_draft_for_template_part_block( $block ) { | |
* @return string Path to the canvas file to include. | ||
*/ | ||
function gutenberg_find_template( $template_file ) { | ||
global $_wp_current_template_id, $_wp_current_template_content, $_wp_current_template_hierarchy; | ||
global $_wp_current_template_id, $_wp_current_template_name, $_wp_current_template_content, $_wp_current_template_hierarchy; | ||
|
||
// Bail if no relevant template hierarchy was determined, or if the template file | ||
// was overridden another way. | ||
|
@@ -173,7 +193,7 @@ function gutenberg_find_template( $template_file ) { | |
$post_name = basename( $higher_priority_block_template_path, '.html' ); | ||
$current_template_post = array( | ||
'post_content' => file_get_contents( $higher_priority_block_template_path ), | ||
'post_title' => ucfirst( $post_name ), | ||
'post_title' => $post_name, | ||
'post_status' => 'auto-draft', | ||
'post_type' => 'wp_template', | ||
'post_name' => $post_name, | ||
|
@@ -192,13 +212,22 @@ function gutenberg_find_template( $template_file ) { | |
} | ||
} | ||
|
||
if ( isset( $_GET['_wp-find-template'] ) ) { | ||
if ( $current_template_post ) { | ||
wp_send_json_success( $current_template_post ); | ||
} else { | ||
wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) ); | ||
} | ||
} | ||
|
||
if ( $current_template_post ) { | ||
if ( is_admin() ) { | ||
foreach ( parse_blocks( $current_template_post->post_content ) as $block ) { | ||
create_auto_draft_for_template_part_block( $block ); | ||
} | ||
} | ||
$_wp_current_template_id = $current_template_post->ID; | ||
$_wp_current_template_name = $current_template_post->post_name; | ||
$_wp_current_template_content = empty( $current_template_post->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template_post->post_content; | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Do we need the theme slug here? It'd be good if it could be implicit to the "current theme" as set in options.
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.
It's so that customized template parts can persist after switching themes. Different themes could both define headers.
Do you mean to make it default to the current theme if not set? We can do that, but we would need to save it on customized templates so that they don't load a different theme's template parts after a theme switch. It seems like a lot of additional implicit behaviors that might confuse people. I'm not sure saving one attribute in theme files is worth it.
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.
It's a weird requirement to have just for the sake of switching themes. It should be possible to handle it at the time of switching themes as well (also depending on what the UX looks like for what you want to preserve and what you want to replace) since we know both the previous and after contexts. Also not even sure that we want the concept of mixed parts between themes, I'd lean more towards a split of just
current theme
anduser templates
.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.
That is the implicit behavior I mentioned could be confusing.
It would be confusing if, after switching themes, those
user templates
started referencing different template parts, hence why we require the attribute after customization.We could default to the theme that originally created each
user template
, but then how does the user add new template parts to existinguser templates
.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 also didn't expect to see the theme slug here, since I interpreted these demo templates as a generic fallback that should work with any given theme.
Having said that, I do agree with this part in general. It just didn't seem necessary to me in this particular case. Would it make sense to persist it only once the demo template part has been modified?
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.
What if the user modifies the template, without touching the part, switches themes, and then ends up with a different template part reference. There are too many edge cases like this where we lose with nothing to win.