-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add an 'area' term for Template Parts. #28410
Changes from all commits
fa05b02
0682937
299124b
45451b3
264279a
90a2641
3a55346
34f7bd9
dd2e647
f4c2540
60bb168
d0823f3
64e3a60
503382f
a691b4a
07a862c
744a1aa
524abc0
6d21411
7dc515a
bd35f4c
8ab0d88
3125a40
ff5e4ca
cf73932
dfd1bfa
fa668f6
9e5cee0
193c66f
89553a8
be7856c
9691842
34d4724
9db03de
a975810
a41d6c8
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 |
---|---|---|
|
@@ -49,12 +49,17 @@ function _gutenberg_get_template_file( $template_type, $slug ) { | |
foreach ( $themes as $theme_slug => $theme_dir ) { | ||
$file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; | ||
if ( file_exists( $file_path ) ) { | ||
return array( | ||
$new_template_item = array( | ||
'slug' => $slug, | ||
'path' => $file_path, | ||
'theme' => $theme_slug, | ||
'type' => $template_type, | ||
); | ||
|
||
if ( 'wp_template_part' === $template_type ) { | ||
return _gutenberg_add_template_part_area_info( $new_template_item ); | ||
} | ||
return $new_template_item; | ||
} | ||
} | ||
|
||
|
@@ -93,18 +98,45 @@ function _gutenberg_get_template_files( $template_type ) { | |
// Subtract ending '.html'. | ||
-5 | ||
); | ||
$template_files[] = array( | ||
$new_template_item = array( | ||
'slug' => $template_slug, | ||
'path' => $template_file, | ||
'theme' => $theme_slug, | ||
'type' => $template_type, | ||
); | ||
|
||
if ( 'wp_template_part' === $template_type ) { | ||
$template_files[] = _gutenberg_add_template_part_area_info( $new_template_item ); | ||
} else { | ||
$template_files[] = $new_template_item; | ||
} | ||
} | ||
} | ||
|
||
return $template_files; | ||
} | ||
|
||
/** | ||
* Attempts to add the template part's area information to the input template. | ||
* | ||
* @param array $template_info Template to add information to (requires 'type' and 'slug' fields). | ||
* | ||
* @return array Template. | ||
*/ | ||
function _gutenberg_add_template_part_area_info( $template_info ) { | ||
if ( WP_Theme_JSON_Resolver::theme_has_support() ) { | ||
$theme_data = WP_Theme_JSON_Resolver::get_theme_data()->get_template_parts(); | ||
} | ||
|
||
if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { | ||
$template_info['area'] = gutenberg_filter_template_part_area_type( $theme_data[ $template_info['slug'] ]['area'] ); | ||
} else { | ||
$template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; | ||
} | ||
|
||
return $template_info; | ||
} | ||
|
||
/** | ||
* Parses wp_template content and injects the current theme's | ||
* stylesheet as a theme attribute into each wp_template_part | ||
|
@@ -171,6 +203,10 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t | |
$template->title = $default_template_types[ $template_file['slug'] ]['title']; | ||
} | ||
|
||
if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) { | ||
$template->area = $template_file['area']; | ||
} | ||
|
||
return $template; | ||
} | ||
|
||
|
@@ -206,6 +242,13 @@ function _gutenberg_build_template_result_from_post( $post ) { | |
$template->title = $post->post_title; | ||
$template->status = $post->post_status; | ||
|
||
if ( 'wp_template_part' === $post->post_type ) { | ||
$type_terms = get_the_terms( $post, 'wp_template_part_area' ); | ||
if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { | ||
$template->area = $type_terms[0]->name; | ||
} | ||
} | ||
|
||
return $template; | ||
} | ||
|
||
|
@@ -237,6 +280,15 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t | |
), | ||
); | ||
|
||
if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) { | ||
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.
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. Certainly! My apologies for the oversight there earlier - #32746 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. Not at all, thank you! |
||
$wp_query_args['tax_query'][] = array( | ||
Addison-Stavlo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'taxonomy' => 'wp_template_part_area', | ||
'field' => 'name', | ||
'terms' => $query['area'], | ||
); | ||
$wp_query_args['tax_query']['relation'] = 'AND'; | ||
} | ||
|
||
if ( isset( $query['slug__in'] ) ) { | ||
$wp_query_args['post_name__in'] = $query['slug__in']; | ||
} | ||
|
@@ -261,14 +313,16 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t | |
if ( ! isset( $query['wp_id'] ) ) { | ||
$template_files = _gutenberg_get_template_files( $template_type ); | ||
foreach ( $template_files as $template_file ) { | ||
$is_custom = array_search( | ||
$is_not_custom = false === array_search( | ||
wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], | ||
array_column( $query_result, 'id' ), | ||
true | ||
); | ||
$should_include = false === $is_custom && ( | ||
! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ) | ||
); | ||
$fits_slug_query = | ||
! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ); | ||
$fits_area_query = | ||
! isset( $query['area'] ) || $template_file['area'] === $query['area']; | ||
$should_include = $is_not_custom && $fits_slug_query && $fits_area_query; | ||
if ( $should_include ) { | ||
$query_result[] = _gutenberg_build_template_result_from_file( $template_file, $template_type ); | ||
} | ||
|
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.
There's some inconsistency in different parts of the code base for instance in this function name
gutenberg_filter_template_part_area_type
we refer to the "area" as "type" which can be confusing especially since we already have a "type" property on the template and template area objects.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.
Id say here we refer to 'area' as 'area', and 'type' more refers to the different allowed values for that area term. Perhaps
gutenberg_filter_template_part_area_value
would make more sense?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 like value better, but why not just
gutenberg_filter_template_part_area
?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 works too, names are hard 😅
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.
#29679