Skip to content

Commit 00452c7

Browse files
committed
Themes: Allow extending WP_Theme_JSON and WP_Theme_JSON_Resolver classes.
This change updates methods visibility from `private` to `protected` and adds late static binding. Original PRs from Gutenberg repository: - WordPress/gutenberg#38625 - WordPress/gutenberg#38671 Props oandregal, Mamaduka, kapilpaul. Merges [52744] to the 5.9 branch. Fixes #55178. See #55179. git-svn-id: https://develop.svn.wordpress.org/branches/5.9@52746 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9a7e624 commit 00452c7

File tree

2 files changed

+179
-167
lines changed

2 files changed

+179
-167
lines changed

src/wp-includes/class-wp-theme-json-resolver.php

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,31 @@ class WP_Theme_JSON_Resolver {
2525
* @since 5.8.0
2626
* @var WP_Theme_JSON
2727
*/
28-
private static $core = null;
28+
protected static $core = null;
2929

3030
/**
3131
* Container for data coming from the theme.
3232
*
3333
* @since 5.8.0
3434
* @var WP_Theme_JSON
3535
*/
36-
private static $theme = null;
36+
protected static $theme = null;
3737

3838
/**
3939
* Whether or not the theme supports theme.json.
4040
*
4141
* @since 5.8.0
4242
* @var bool
4343
*/
44-
private static $theme_has_support = null;
44+
protected static $theme_has_support = null;
4545

4646
/**
4747
* Container for data coming from the user.
4848
*
4949
* @since 5.9.0
5050
* @var WP_Theme_JSON
5151
*/
52-
private static $user = null;
52+
protected static $user = null;
5353

5454
/**
5555
* Stores the ID of the custom post type
@@ -58,7 +58,7 @@ class WP_Theme_JSON_Resolver {
5858
* @since 5.9.0
5959
* @var int
6060
*/
61-
private static $user_custom_post_type_id = null;
61+
protected static $user_custom_post_type_id = null;
6262

6363
/**
6464
* Container to keep loaded i18n schema for `theme.json`.
@@ -67,7 +67,7 @@ class WP_Theme_JSON_Resolver {
6767
* @since 5.9.0 Renamed from `$theme_json_i18n` to `$i18n_schema`.
6868
* @var array
6969
*/
70-
private static $i18n_schema = null;
70+
protected static $i18n_schema = null;
7171

7272
/**
7373
* Processes a file that adheres to the theme.json schema
@@ -78,7 +78,7 @@ class WP_Theme_JSON_Resolver {
7878
* @param string $file_path Path to file. Empty if no file.
7979
* @return array Contents that adhere to the theme.json schema.
8080
*/
81-
private static function read_json_file( $file_path ) {
81+
protected static function read_json_file( $file_path ) {
8282
$config = array();
8383
if ( $file_path ) {
8484
$decoded_file = wp_json_file_decode( $file_path, array( 'associative' => true ) );
@@ -113,13 +113,13 @@ public static function get_fields_to_translate() {
113113
* Default 'default'.
114114
* @return array Returns the modified $theme_json_structure.
115115
*/
116-
private static function translate( $theme_json, $domain = 'default' ) {
117-
if ( null === self::$i18n_schema ) {
118-
$i18n_schema = wp_json_file_decode( __DIR__ . '/theme-i18n.json' );
119-
self::$i18n_schema = null === $i18n_schema ? array() : $i18n_schema;
116+
protected static function translate( $theme_json, $domain = 'default' ) {
117+
if ( null === static::$i18n_schema ) {
118+
$i18n_schema = wp_json_file_decode( __DIR__ . '/theme-i18n.json' );
119+
static::$i18n_schema = null === $i18n_schema ? array() : $i18n_schema;
120120
}
121121

122-
return translate_settings_using_i18n_schema( self::$i18n_schema, $theme_json, $domain );
122+
return translate_settings_using_i18n_schema( static::$i18n_schema, $theme_json, $domain );
123123
}
124124

125125
/**
@@ -130,15 +130,15 @@ private static function translate( $theme_json, $domain = 'default' ) {
130130
* @return WP_Theme_JSON Entity that holds core data.
131131
*/
132132
public static function get_core_data() {
133-
if ( null !== self::$core ) {
134-
return self::$core;
133+
if ( null !== static::$core ) {
134+
return static::$core;
135135
}
136136

137-
$config = self::read_json_file( __DIR__ . '/theme.json' );
138-
$config = self::translate( $config );
139-
self::$core = new WP_Theme_JSON( $config, 'default' );
137+
$config = static::read_json_file( __DIR__ . '/theme.json' );
138+
$config = static::translate( $config );
139+
static::$core = new WP_Theme_JSON( $config, 'default' );
140140

141-
return self::$core;
141+
return static::$core;
142142
}
143143

144144
/**
@@ -159,32 +159,32 @@ public static function get_theme_data( $deprecated = array() ) {
159159
if ( ! empty( $deprecated ) ) {
160160
_deprecated_argument( __METHOD__, '5.9.0' );
161161
}
162-
if ( null === self::$theme ) {
163-
$theme_json_data = self::read_json_file( self::get_file_path_from_theme( 'theme.json' ) );
164-
$theme_json_data = self::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
165-
self::$theme = new WP_Theme_JSON( $theme_json_data );
162+
if ( null === static::$theme ) {
163+
$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
164+
$theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
165+
static::$theme = new WP_Theme_JSON( $theme_json_data );
166166

167167
if ( wp_get_theme()->parent() ) {
168168
// Get parent theme.json.
169-
$parent_theme_json_data = self::read_json_file( self::get_file_path_from_theme( 'theme.json', true ) );
170-
$parent_theme_json_data = self::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
169+
$parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) );
170+
$parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
171171
$parent_theme = new WP_Theme_JSON( $parent_theme_json_data );
172172

173173
// Merge the child theme.json into the parent theme.json.
174174
// The child theme takes precedence over the parent.
175-
$parent_theme->merge( self::$theme );
176-
self::$theme = $parent_theme;
175+
$parent_theme->merge( static::$theme );
176+
static::$theme = $parent_theme;
177177
}
178178
}
179179

180180
/*
181181
* We want the presets and settings declared in theme.json
182182
* to override the ones declared via theme supports.
183183
* So we take theme supports, transform it to theme.json shape
184-
* and merge the self::$theme upon that.
184+
* and merge the static::$theme upon that.
185185
*/
186186
$theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_default_block_editor_settings() );
187-
if ( ! self::theme_has_support() ) {
187+
if ( ! static::theme_has_support() ) {
188188
if ( ! isset( $theme_support_data['settings']['color'] ) ) {
189189
$theme_support_data['settings']['color'] = array();
190190
}
@@ -210,7 +210,7 @@ public static function get_theme_data( $deprecated = array() ) {
210210
$theme_support_data['settings']['color']['defaultGradients'] = $default_gradients;
211211
}
212212
$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
213-
$with_theme_supports->merge( self::$theme );
213+
$with_theme_supports->merge( static::$theme );
214214

215215
return $with_theme_supports;
216216
}
@@ -299,12 +299,12 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
299299
* @return WP_Theme_JSON Entity that holds styles for user data.
300300
*/
301301
public static function get_user_data() {
302-
if ( null !== self::$user ) {
303-
return self::$user;
302+
if ( null !== static::$user ) {
303+
return static::$user;
304304
}
305305

306306
$config = array();
307-
$user_cpt = self::get_user_data_from_wp_global_styles( wp_get_theme() );
307+
$user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() );
308308

309309
if ( array_key_exists( 'post_content', $user_cpt ) ) {
310310
$decoded_data = json_decode( $user_cpt['post_content'], true );
@@ -326,9 +326,9 @@ public static function get_user_data() {
326326
$config = $decoded_data;
327327
}
328328
}
329-
self::$user = new WP_Theme_JSON( $config, 'custom' );
329+
static::$user = new WP_Theme_JSON( $config, 'custom' );
330330

331-
return self::$user;
331+
return static::$user;
332332
}
333333

334334
/**
@@ -363,11 +363,11 @@ public static function get_merged_data( $origin = 'custom' ) {
363363
}
364364

365365
$result = new WP_Theme_JSON();
366-
$result->merge( self::get_core_data() );
367-
$result->merge( self::get_theme_data() );
366+
$result->merge( static::get_core_data() );
367+
$result->merge( static::get_theme_data() );
368368

369369
if ( 'custom' === $origin ) {
370-
$result->merge( self::get_user_data() );
370+
$result->merge( static::get_user_data() );
371371
}
372372

373373
return $result;
@@ -382,17 +382,17 @@ public static function get_merged_data( $origin = 'custom' ) {
382382
* @return integer|null
383383
*/
384384
public static function get_user_global_styles_post_id() {
385-
if ( null !== self::$user_custom_post_type_id ) {
386-
return self::$user_custom_post_type_id;
385+
if ( null !== static::$user_custom_post_type_id ) {
386+
return static::$user_custom_post_type_id;
387387
}
388388

389-
$user_cpt = self::get_user_data_from_wp_global_styles( wp_get_theme(), true );
389+
$user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme(), true );
390390

391391
if ( array_key_exists( 'ID', $user_cpt ) ) {
392-
self::$user_custom_post_type_id = $user_cpt['ID'];
392+
static::$user_custom_post_type_id = $user_cpt['ID'];
393393
}
394394

395-
return self::$user_custom_post_type_id;
395+
return static::$user_custom_post_type_id;
396396
}
397397

398398
/**
@@ -404,14 +404,14 @@ public static function get_user_global_styles_post_id() {
404404
* @return bool
405405
*/
406406
public static function theme_has_support() {
407-
if ( ! isset( self::$theme_has_support ) ) {
408-
self::$theme_has_support = (
409-
is_readable( self::get_file_path_from_theme( 'theme.json' ) ) ||
410-
is_readable( self::get_file_path_from_theme( 'theme.json', true ) )
407+
if ( ! isset( static::$theme_has_support ) ) {
408+
static::$theme_has_support = (
409+
is_readable( static::get_file_path_from_theme( 'theme.json' ) ) ||
410+
is_readable( static::get_file_path_from_theme( 'theme.json', true ) )
411411
);
412412
}
413413

414-
return self::$theme_has_support;
414+
return static::$theme_has_support;
415415
}
416416

417417
/**
@@ -426,7 +426,7 @@ public static function theme_has_support() {
426426
* @param bool $template Optional. Use template theme directory. Default false.
427427
* @return string The whole file path or empty if the file doesn't exist.
428428
*/
429-
private static function get_file_path_from_theme( $file_name, $template = false ) {
429+
protected static function get_file_path_from_theme( $file_name, $template = false ) {
430430
$path = $template ? get_template_directory() : get_stylesheet_directory();
431431
$candidate = $path . '/' . $file_name;
432432

@@ -441,12 +441,12 @@ private static function get_file_path_from_theme( $file_name, $template = false
441441
* and `$i18n_schema` variables to reset.
442442
*/
443443
public static function clean_cached_data() {
444-
self::$core = null;
445-
self::$theme = null;
446-
self::$user = null;
447-
self::$user_custom_post_type_id = null;
448-
self::$theme_has_support = null;
449-
self::$i18n_schema = null;
444+
static::$core = null;
445+
static::$theme = null;
446+
static::$user = null;
447+
static::$user_custom_post_type_id = null;
448+
static::$theme_has_support = null;
449+
static::$i18n_schema = null;
450450
}
451451

452452
}

0 commit comments

Comments
 (0)