Skip to content

Commit

Permalink
Load page templates via theme.json abstractions (#28700)
Browse files Browse the repository at this point in the history
  • Loading branch information
nosolosw authored Feb 5, 2021
1 parent b5c5801 commit a863239
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
38 changes: 25 additions & 13 deletions lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class WP_Theme_JSON_Resolver {
*
* @var WP_Theme_JSON
*/
private $theme = null;
private static $theme = null;

/**
* Container for data coming from the user.
Expand Down Expand Up @@ -258,27 +258,39 @@ private static function get_core_origin() {
}

/**
* Returns the theme's origin config.
* Returns the theme's data.
*
* It uses the theme support data if
* the theme hasn't declared any via theme.json.
* Data from theme.json can be augmented via the
* $theme_support_data variable. This is useful, for example,
* to backfill the gaps in theme.json that a theme has declared
* via add_theme_supports.
*
* Note that if the same data is present in theme.json
* and in $theme_support_data, the theme.json's is not overwritten.
*
* @param array $theme_support_data Theme support data in theme.json format.
*
* @return WP_Theme_JSON Entity that holds theme data.
*/
private function get_theme_origin( $theme_support_data = array() ) {
$theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) );
self::translate_presets( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
public function get_theme_data( $theme_support_data = array() ) {
if ( null === self::$theme ) {
$theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) );
self::translate_presets( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
self::$theme = new WP_Theme_JSON( $theme_json_data );
}

if ( empty( $theme_support_data ) ) {
return self::$theme;
}

/*
* We want the presets and settings declared in theme.json
* to override the ones declared via add_theme_support.
*/
$this->theme = new WP_Theme_JSON( $theme_support_data );
$this->theme->merge( new WP_Theme_JSON( $theme_json_data ) );
$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
$with_theme_supports->merge( self::$theme );

return $this->theme;
return $with_theme_supports;
}

/**
Expand Down Expand Up @@ -395,15 +407,15 @@ public function get_origin( $theme_support_data = array(), $origin = 'user', $me
if ( ( 'user' === $origin ) && $merged ) {
$result = new WP_Theme_JSON();
$result->merge( self::get_core_origin() );
$result->merge( $this->get_theme_origin( $theme_support_data ) );
$result->merge( $this->get_theme_data( $theme_support_data ) );
$result->merge( self::get_user_origin() );
return $result;
}

if ( ( 'theme' === $origin ) && $merged ) {
$result = new WP_Theme_JSON();
$result->merge( self::get_core_origin() );
$result->merge( $this->get_theme_origin( $theme_support_data ) );
$result->merge( $this->get_theme_data( $theme_support_data ) );
return $result;
}

Expand All @@ -412,7 +424,7 @@ public function get_origin( $theme_support_data = array(), $origin = 'user', $me
}

if ( 'theme' === $origin ) {
return $this->get_theme_origin( $theme_support_data );
return $this->get_theme_data( $theme_support_data );
}

return self::get_core_origin();
Expand Down
18 changes: 16 additions & 2 deletions lib/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ class WP_Theme_JSON {
* }
*/
const SCHEMA = array(
'styles' => array(
'pageTemplates' => null,
'styles' => array(
'border' => array(
'radius' => null,
'color' => null,
Expand Down Expand Up @@ -134,7 +135,7 @@ class WP_Theme_JSON {
'textTransform' => null,
),
),
'settings' => array(
'settings' => array(
'border' => array(
'customRadius' => null,
'customColor' => null,
Expand Down Expand Up @@ -1050,6 +1051,19 @@ public function get_settings() {
}
}

/**
* Returns the page templates of the current theme.
*
* @return array
*/
public function get_page_templates() {
if ( ! isset( $this->theme_json['pageTemplates'] ) ) {
return array();
} else {
return $this->theme_json['pageTemplates'];
}
}

/**
* Returns the stylesheet that results of processing
* the theme.json structure this object represents.
Expand Down
15 changes: 5 additions & 10 deletions lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ function gutenberg_load_block_page_templates( $templates, $theme, $post ) {
if ( ! gutenberg_is_fse_theme() ) {
return $templates;
}
$config_file = locate_template( 'experimental-theme.json' );
if ( ! file_exists( $config_file ) ) {
return $templates;
}
$data = json_decode(
file_get_contents( $config_file ),
true
);

$resolver = new WP_Theme_JSON_Resolver();
$data = $resolver->get_theme_data()->get_page_templates();
$page_templates = array();
if ( isset( $data['pageTemplates'] ) ) {
foreach ( $data['pageTemplates'] as $key => $page_template ) {
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 ) )
) {
Expand Down
8 changes: 6 additions & 2 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ function gutenberg_is_experiment_enabled( $name ) {
if ( ! class_exists( 'WP_Block_Template ' ) ) {
require __DIR__ . '/full-site-editing/class-wp-block-template.php';
}

// These are used by some FSE features
// as well as global styles.
require __DIR__ . '/class-wp-theme-json.php';
require __DIR__ . '/class-wp-theme-json-resolver.php';

require __DIR__ . '/full-site-editing/full-site-editing.php';
require __DIR__ . '/full-site-editing/block-templates.php';
require __DIR__ . '/full-site-editing/default-template-types.php';
Expand All @@ -109,8 +115,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/navigation.php';
require __DIR__ . '/navigation-page.php';
require __DIR__ . '/experiments-page.php';
require __DIR__ . '/class-wp-theme-json.php';
require __DIR__ . '/class-wp-theme-json-resolver.php';
require __DIR__ . '/global-styles.php';
require __DIR__ . '/query-utils.php';

Expand Down
23 changes: 23 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,27 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() {
);
$this->assertEqualSetsWithIndex( $expected, $result );
}

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

$page_templates = $theme_json->get_page_templates();

$this->assertEqualSetsWithIndex(
$page_templates,
array(
'page-home' => array(
'title' => 'Some title',
),
)
);
}
}

0 comments on commit a863239

Please sign in to comment.