Make WordPress Core


Ignore:
Timestamp:
02/17/2022 09:02:10 AM (5 years ago)
Author:
audrasjb
Message:

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:

Props oandregal, Mamaduka, kapilpaul.
Fixes #55178.
See #55179.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json-resolver.php

    r52610 r52744  
    2626         * @var WP_Theme_JSON
    2727         */
    28         private static $core = null;
     28        protected static $core = null;
    2929
    3030        /**
     
    3434         * @var WP_Theme_JSON
    3535         */
    36         private static $theme = null;
     36        protected static $theme = null;
    3737
    3838        /**
     
    4242         * @var bool
    4343         */
    44         private static $theme_has_support = null;
     44        protected static $theme_has_support = null;
    4545
    4646        /**
     
    5050         * @var WP_Theme_JSON
    5151         */
    52         private static $user = null;
     52        protected static $user = null;
    5353
    5454        /**
     
    5959         * @var int
    6060         */
    61         private static $user_custom_post_type_id = null;
     61        protected static $user_custom_post_type_id = null;
    6262
    6363        /**
     
    6868         * @var array
    6969         */
    70         private static $i18n_schema = null;
     70        protected static $i18n_schema = null;
    7171
    7272        /**
     
    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 ) {
     
    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;
    120                 }
    121 
    122                 return translate_settings_using_i18n_schema( self::$i18n_schema, $theme_json, $domain );
     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;
     120                }
     121
     122                return translate_settings_using_i18n_schema( static::$i18n_schema, $theme_json, $domain );
    123123        }
    124124
     
    131131         */
    132132        public static function get_core_data() {
    133                 if ( null !== self::$core ) {
    134                         return self::$core;
    135                 }
    136 
    137                 $config     = self::read_json_file( __DIR__ . '/theme.json' );
    138                 $config     = self::translate( $config );
    139                 self::$core = new WP_Theme_JSON( $config, 'default' );
    140 
    141                 return self::$core;
     133                if ( null !== static::$core ) {
     134                        return static::$core;
     135                }
     136
     137                $config       = static::read_json_file( __DIR__ . '/theme.json' );
     138                $config       = static::translate( $config );
     139                static::$core = new WP_Theme_JSON( $config, 'default' );
     140
     141                return static::$core;
    142142        }
    143143
     
    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                }
     
    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();
     
    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;
     
    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 ) ) {
     
    327327                        }
    328328                }
    329                 self::$user = new WP_Theme_JSON( $config, 'custom' );
    330 
    331                 return self::$user;
     329                static::$user = new WP_Theme_JSON( $config, 'custom' );
     330
     331                return static::$user;
    332332        }
    333333
     
    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
     
    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;
    387                 }
    388 
    389                 $user_cpt = self::get_user_data_from_wp_global_styles( wp_get_theme(), true );
     385                if ( null !== static::$user_custom_post_type_id ) {
     386                        return static::$user_custom_post_type_id;
     387                }
     388
     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'];
    393                 }
    394 
    395                 return self::$user_custom_post_type_id;
     392                        static::$user_custom_post_type_id = $user_cpt['ID'];
     393                }
     394
     395                return static::$user_custom_post_type_id;
    396396        }
    397397
     
    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
     
    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;
     
    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
Note: See TracChangeset for help on using the changeset viewer.