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

Rename pageTemplates config to customTemplates in theme.json #28830

Merged
merged 2 commits into from
Feb 8, 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
19 changes: 19 additions & 0 deletions docs/designers-developers/developers/themes/theme-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This is documentation for the current direction and work in progress about how t
- Specification
- Settings
- Styles
- Other theme metadata
- FAQ
- The naming schema of CSS Custom Properties
- Why using -- as a separator?
Expand Down Expand Up @@ -476,6 +477,24 @@ These are the current typography properties supported by blocks:

[1] The heading block represents 6 distinct HTML elements: H1-H6. It comes with selectors to target each individual element (ex: core/heading/h1 for H1, etc).


### Other theme metadata
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @nosolosw


There's a growing need to add more theme metadata to the theme.json. This section lists those other fields:

**customTemplates**: within this field themes can list the custom templates present in the `block-templates` folder, the keys should match the custom template name. For example, for a custom template named `my-custom-template.html`, the `theme.json` can declare what post types can use it and what's the title to show the user:

```json
{
"customTemplates": {
"my-custom-template": {
"title": "The template title", /* Mandatory */
"postTypes": [ "page", "post", "my-cpt" ] /* Optional, will only apply to "page" by default. */
}
}
}
```

## Frequently Asked Questions

### The naming schema of CSS Custom Properties
Expand Down
12 changes: 6 additions & 6 deletions lib/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class WP_Theme_JSON {
* }
*/
const SCHEMA = array(
'pageTemplates' => null,
'styles' => array(
'customTemplates' => null,
'styles' => array(
'border' => array(
'radius' => null,
'color' => null,
Expand Down Expand Up @@ -135,7 +135,7 @@ class WP_Theme_JSON {
'textTransform' => null,
),
),
'settings' => array(
'settings' => array(
'border' => array(
'customRadius' => null,
'customColor' => null,
Expand Down Expand Up @@ -1056,11 +1056,11 @@ public function get_settings() {
*
* @return array
*/
public function get_page_templates() {
if ( ! isset( $this->theme_json['pageTemplates'] ) ) {
public function get_custom_templates() {
if ( ! isset( $this->theme_json['customTemplates'] ) ) {
return array();
} else {
return $this->theme_json['pageTemplates'];
return $this->theme_json['customTemplates'];
}
}

Expand Down
22 changes: 11 additions & 11 deletions lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@
* @param array $templates Page templates.
* @param WP_Theme $theme WP_Theme instance.
* @param WP_Post $post The post being edited, provided for context, or null.
* @param string $post_type Post type to get the templates for.
* @return array (Maybe) modified page templates array.
*/
function gutenberg_load_block_page_templates( $templates, $theme, $post ) {
function gutenberg_load_block_page_templates( $templates, $theme, $post, $post_type ) {
if ( ! gutenberg_is_fse_theme() ) {
return $templates;
}

$resolver = new WP_Theme_JSON_Resolver();
$data = $resolver->get_theme_data()->get_page_templates();
$page_templates = array();
$resolver = new WP_Theme_JSON_Resolver();
$data = $resolver->get_theme_data()->get_custom_templates();
$custom_templates = array();
if ( isset( $data ) ) {
foreach ( $data as $key => $page_template ) {
if ( ( ! isset( $page_template['postTypes'] ) && 'page' === $post->post_type ) ||
( isset( $page_template['postTypes'] ) && in_array( $post->post_type, $page_template['postTypes'], true ) )
foreach ( $data as $key => $template ) {
if ( ( ! isset( $template['postTypes'] ) && 'page' === $post_type ) ||
( isset( $template['postTypes'] ) && in_array( $post_type, $template['postTypes'], true ) )
) {
$page_templates[ $key ] = $page_template['title'];
$custom_templates[ $key ] = $template['title'];
}
}
}

return $page_templates;
return $custom_templates;
}
add_filter( 'theme_post_templates', 'gutenberg_load_block_page_templates', 10, 3 );
add_filter( 'theme_page_templates', 'gutenberg_load_block_page_templates', 10, 3 );
add_filter( 'theme_templates', 'gutenberg_load_block_page_templates', 10, 4 );
6 changes: 3 additions & 3 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,18 +738,18 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() {
$this->assertEqualSetsWithIndex( $expected, $result );
}

function test_get_page_templates() {
function test_get_custom_templates() {
$theme_json = new WP_Theme_JSON(
array(
'pageTemplates' => array(
'customTemplates' => array(
'page-home' => array(
'title' => 'Some title',
),
),
)
);

$page_templates = $theme_json->get_page_templates();
$page_templates = $theme_json->get_custom_templates();

$this->assertEqualSetsWithIndex(
$page_templates,
Expand Down