From 14e4f7c64e39ae0fbb675feea033781a735b3e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Wed, 3 Feb 2021 18:15:54 +0100 Subject: [PATCH 1/7] Add pageTemplates field and getter --- lib/class-wp-theme-json.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/class-wp-theme-json.php b/lib/class-wp-theme-json.php index 44b59ceff81d30..d47bd461de84b2 100644 --- a/lib/class-wp-theme-json.php +++ b/lib/class-wp-theme-json.php @@ -103,6 +103,7 @@ class WP_Theme_JSON { * } */ const SCHEMA = array( + 'pageTemplates' => null, 'styles' => array( 'border' => array( 'radius' => null, @@ -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. From c80fbccc70fc5a240d3d27addc4e8845052bc006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Wed, 3 Feb 2021 18:42:42 +0100 Subject: [PATCH 2/7] Makes WP_Theme_JSON_Resolver->get_theme_data public --- lib/class-wp-theme-json-resolver.php | 36 ++++++++++++++++++---------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index d54ae84c8dcd08..b1474feb0b504f 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -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 === $this->theme ) { + $theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) ); + self::translate_presets( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); + $this->theme = new WP_Theme_JSON( $theme_json_data ); + } + + if ( empty( $theme_support_data ) ) { + return $this->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( $this->theme ); - return $this->theme; + return $with_theme_supports; } /** @@ -395,7 +407,7 @@ 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; } @@ -403,7 +415,7 @@ public function get_origin( $theme_support_data = array(), $origin = 'user', $me 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; } @@ -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(); From 89ccea676a765bbccd8bd0b04d41f7b956b9821f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Wed, 3 Feb 2021 18:44:21 +0100 Subject: [PATCH 3/7] Make use of new methods in page-templates.php --- lib/full-site-editing/page-templates.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/full-site-editing/page-templates.php b/lib/full-site-editing/page-templates.php index 89ce31dba143f0..f3a798de1d959f 100644 --- a/lib/full-site-editing/page-templates.php +++ b/lib/full-site-editing/page-templates.php @@ -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 ) ) ) { From 6a7430b92e1f98efb76e4ce9f2f0871220174302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Thu, 4 Feb 2021 12:34:02 +0100 Subject: [PATCH 4/7] Load classes --- lib/load.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/load.php b/lib/load.php index 7772cac90e15e8..ad56b7651e7efe 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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'; @@ -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'; From 854502457aa48ee9edb7af59a16cfa736b53460d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Thu, 4 Feb 2021 14:28:42 +0100 Subject: [PATCH 5/7] Add unit test for page templates --- phpunit/class-wp-theme-json-test.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 34d916a546f49a..6d1b6bc7ce9124 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -736,4 +736,22 @@ 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', + ), + ) ); + } } From acf15433d4a85062ae4a2bd7a1e46f5c40236b7a Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 4 Feb 2021 14:46:22 -0500 Subject: [PATCH 6/7] composer format --- lib/class-wp-theme-json.php | 4 ++-- phpunit/class-wp-theme-json-test.php | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/class-wp-theme-json.php b/lib/class-wp-theme-json.php index d47bd461de84b2..353a4180a1983d 100644 --- a/lib/class-wp-theme-json.php +++ b/lib/class-wp-theme-json.php @@ -104,7 +104,7 @@ class WP_Theme_JSON { */ const SCHEMA = array( 'pageTemplates' => null, - 'styles' => array( + 'styles' => array( 'border' => array( 'radius' => null, 'color' => null, @@ -135,7 +135,7 @@ class WP_Theme_JSON { 'textTransform' => null, ), ), - 'settings' => array( + 'settings' => array( 'border' => array( 'customRadius' => null, 'customColor' => null, diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 6d1b6bc7ce9124..50eee839ea182a 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -738,20 +738,25 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() { } function test_get_page_templates() { - $theme_json = new WP_Theme_JSON( array( - 'pageTemplates' => array( - 'page-home' => array( - 'title' => 'Some title' + $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', - ), - ) ); + $this->assertEqualSetsWithIndex( + $page_templates, + array( + 'page-home' => array( + 'title' => 'Some title', + ), + ) + ); } } From 469cb6c441f6d09a3ba18937299546e105b78c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Fri, 5 Feb 2021 10:41:22 +0100 Subject: [PATCH 7/7] Make $theme static --- lib/class-wp-theme-json-resolver.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index b1474feb0b504f..9ca30a3c8f2965 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -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. @@ -273,14 +273,14 @@ private static function get_core_origin() { * @return WP_Theme_JSON Entity that holds theme data. */ public function get_theme_data( $theme_support_data = array() ) { - if ( null === $this->theme ) { + 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' ) ); - $this->theme = new WP_Theme_JSON( $theme_json_data ); + self::$theme = new WP_Theme_JSON( $theme_json_data ); } if ( empty( $theme_support_data ) ) { - return $this->theme; + return self::$theme; } /* @@ -288,7 +288,7 @@ public function get_theme_data( $theme_support_data = array() ) { * to override the ones declared via add_theme_support. */ $with_theme_supports = new WP_Theme_JSON( $theme_support_data ); - $with_theme_supports->merge( $this->theme ); + $with_theme_supports->merge( self::$theme ); return $with_theme_supports; }