Skip to content
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

Themes: Support title in templateParts #35626

Merged
merged 5 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/how-to-guides/themes/theme-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ Within this field themes can list the template parts present in the `block-templ
Currently block variations exist for "header" and "footer" values of the area term, any other values and template parts not defined in the json will default to the general template part block. Variations will be denoted by specific icons within the editor's interface, will default to the corresponding semantic HTML element for the wrapper (this can also be overridden by the `tagName` attribute set on the template part block), and will contextualize the template part allowing more custom flows in future editor improvements.

- name: mandatory.
- title: optional, translatable.
- area: optional, will be set to `uncategorized` by default and trigger no block variation.

```json
Expand All @@ -957,6 +958,7 @@ Currently block variations exist for "header" and "footer" values of the area te
"templateParts": [
{
"name": "my-template-part",
"title": "Header",
"area": "header"
}
]
Expand Down
5 changes: 3 additions & 2 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ private static function compute_preset_classes( $settings, $selector, $origins )
/**
* Transform a slug into a CSS Custom Property.
*
* @param array $input String to replace.
* @param string $input String to replace.
* @param string $slug The slug value to use to generate the custom property.
*
* @return string The CSS Custom Property. Something along the lines of --wp--preset--color--black.
Expand Down Expand Up @@ -1121,7 +1121,8 @@ public function get_template_parts() {
foreach ( $this->theme_json['templateParts'] as $item ) {
if ( isset( $item['name'] ) ) {
$template_parts[ $item['name'] ] = array(
'area' => isset( $item['area'] ) ? $item['area'] : '',
'title' => isset( $item['title'] ) ? $item['title'] : '',
'area' => isset( $item['area'] ) ? $item['area'] : '',
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ function _gutenberg_add_template_part_area_info( $template_info ) {
}

if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) {
$template_info['area'] = gutenberg_filter_template_part_area( $theme_data[ $template_info['slug'] ]['area'] );
$template_info['title'] = $theme_data[ $template_info['slug'] ]['title'];
$template_info['area'] = gutenberg_filter_template_part_area( $theme_data[ $template_info['slug'] ]['area'] );
} else {
$template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
}
Expand Down Expand Up @@ -222,7 +223,7 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t
$template->slug = $template_file['slug'];
$template->source = 'theme';
$template->type = $template_type;
$template->title = $template_file['slug'];
$template->title = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug'];
$template->status = 'publish';
$template->has_theme_file = true;

Expand Down
5 changes: 5 additions & 0 deletions lib/theme-i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,10 @@
{
"title": "Custom template name"
}
],
"templateParts": [
{
"title": "Template part name"
}
]
}
14 changes: 14 additions & 0 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ function test_fields_are_extracted() {
'key' => 'title',
'context' => 'Custom template name',
),
array(
'path' => array( 'templateParts' ),
'key' => 'title',
'context' => 'Template part name',
),
);

$this->assertEquals( $expected, $actual );
Expand Down Expand Up @@ -197,6 +202,15 @@ function test_translations_are_applied() {
),
)
);
$this->assertSame(
$actual->get_template_parts(),
array(
'small-header' => array(
'title' => 'Mały nagłówek',
'area' => 'header',
),
)
);
}

function test_switching_themes_recalculates_data() {
Expand Down
8 changes: 5 additions & 3 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1525,8 +1525,9 @@ function test_get_template_parts() {
array(
'templateParts' => array(
array(
'name' => 'small-header',
'area' => 'header',
'name' => 'small-header',
'title' => 'Small Header',
'area' => 'header',
),
),
)
Expand All @@ -1538,7 +1539,8 @@ function test_get_template_parts() {
$template_parts,
array(
'small-header' => array(
'area' => 'header',
'title' => 'Small Header',
'area' => 'header',
),
)
);
Expand Down
Binary file modified phpunit/data/languages/themes/fse-pl_PL.mo
Binary file not shown.
4 changes: 4 additions & 0 deletions phpunit/data/languages/themes/fse-pl_PL.po
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ msgctxt "Custom template name"
msgid "Homepage template"
msgstr "Szablon strony głównej"

msgctxt "Template part name"
msgid "Small Header"
msgstr "Mały nagłówek"

msgctxt "Color name"
msgid "Light"
msgstr "Jasny"
Expand Down
1 change: 1 addition & 0 deletions phpunit/data/themedir1/fse/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"templateParts": [
{
"name": "small-header",
"title": "Small Header",
"area": "header"
}
]
Expand Down