Make WordPress Core

Changeset 52746


Ignore:
Timestamp:
02/17/2022 11:13:09 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.
Merges [52744] to the 5.9 branch.
Fixes #55178.
See #55179.

Location:
branches/5.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.9

  • branches/5.9/src/wp-includes/class-wp-theme-json-resolver.php

    r52372 r52746  
    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
  • branches/5.9/src/wp-includes/class-wp-theme-json.php

    r52434 r52746  
    2525         * @var array
    2626         */
    27         private $theme_json = null;
     27        protected $theme_json = null;
    2828
    2929        /**
    … …  
    3535         * @var array
    3636         */
    37         private static $blocks_metadata = null;
     37        protected static $blocks_metadata = null;
    3838
    3939        /**
    … …  
    367367         */
    368368        public function __construct( $theme_json = array(), $origin = 'theme' ) {
    369                 if ( ! in_array( $origin, self::VALID_ORIGINS, true ) ) {
     369                if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
    370370                        $origin = 'theme';
    371371                }
    372372
    373373                $this->theme_json    = WP_Theme_JSON_Schema::migrate( $theme_json );
    374                 $valid_block_names   = array_keys( self::get_blocks_metadata() );
    375                 $valid_element_names = array_keys( self::ELEMENTS );
    376                 $theme_json          = self::sanitize( $this->theme_json, $valid_block_names, $valid_element_names );
    377                 $this->theme_json    = self::maybe_opt_in_into_settings( $theme_json );
     374                $valid_block_names   = array_keys( static::get_blocks_metadata() );
     375                $valid_element_names = array_keys( static::ELEMENTS );
     376                $theme_json          = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names );
     377                $this->theme_json    = static::maybe_opt_in_into_settings( $theme_json );
    378378
    379379                // Internally, presets are keyed by origin.
    380                 $nodes = self::get_setting_nodes( $this->theme_json );
     380                $nodes = static::get_setting_nodes( $this->theme_json );
    381381                foreach ( $nodes as $node ) {
    382                         foreach ( self::PRESETS_METADATA as $preset_metadata ) {
     382                        foreach ( static::PRESETS_METADATA as $preset_metadata ) {
    383383                                $path   = array_merge( $node['path'], $preset_metadata['path'] );
    384384                                $preset = _wp_array_get( $this->theme_json, $path, null );
    … …  
    401401         * @return array The modified theme.json structure.
    402402         */
    403         private static function maybe_opt_in_into_settings( $theme_json ) {
     403        protected static function maybe_opt_in_into_settings( $theme_json ) {
    404404                $new_theme_json = $theme_json;
    405405
    … …  
    408408                        true === $new_theme_json['settings']['appearanceTools']
    409409                ) {
    410                         self::do_opt_in_into_settings( $new_theme_json['settings'] );
     410                        static::do_opt_in_into_settings( $new_theme_json['settings'] );
    411411                }
    412412
    … …  
    414414                        foreach ( $new_theme_json['settings']['blocks'] as &$block ) {
    415415                                if ( isset( $block['appearanceTools'] ) && ( true === $block['appearanceTools'] ) ) {
    416                                         self::do_opt_in_into_settings( $block );
     416                                        static::do_opt_in_into_settings( $block );
    417417                                }
    418418                        }
    … …  
    429429         * @param array $context The context to which the settings belong.
    430430         */
    431         private static function do_opt_in_into_settings( &$context ) {
     431        protected static function do_opt_in_into_settings( &$context ) {
    432432                $to_opt_in = array(
    433433                        array( 'border', 'color' ),
    … …  
    464464         * @return array The sanitized output.
    465465         */
    466         private static function sanitize( $input, $valid_block_names, $valid_element_names ) {
     466        protected static function sanitize( $input, $valid_block_names, $valid_element_names ) {
    467467                $output = array();
    468468
    … …  
    471471                }
    472472
    473                 $output = array_intersect_key( $input, array_flip( self::VALID_TOP_LEVEL_KEYS ) );
     473                $output = array_intersect_key( $input, array_flip( static::VALID_TOP_LEVEL_KEYS ) );
    474474
    475475                // Some styles are only meant to be available at the top-level (e.g.: blockGap),
    476476                // hence, the schema for blocks & elements should not have them.
    477                 $styles_non_top_level = self::VALID_STYLES;
     477                $styles_non_top_level = static::VALID_STYLES;
    478478                foreach ( array_keys( $styles_non_top_level ) as $section ) {
    479479                        foreach ( array_keys( $styles_non_top_level[ $section ] ) as $prop ) {
    … …  
    493493                $schema_settings_blocks = array();
    494494                foreach ( $valid_block_names as $block ) {
    495                         $schema_settings_blocks[ $block ]           = self::VALID_SETTINGS;
     495                        $schema_settings_blocks[ $block ]           = static::VALID_SETTINGS;
    496496                        $schema_styles_blocks[ $block ]             = $styles_non_top_level;
    497497                        $schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements;
    498498                }
    499                 $schema['styles']             = self::VALID_STYLES;
     499                $schema['styles']             = static::VALID_STYLES;
    500500                $schema['styles']['blocks']   = $schema_styles_blocks;
    501501                $schema['styles']['elements'] = $schema_styles_elements;
    502                 $schema['settings']           = self::VALID_SETTINGS;
     502                $schema['settings']           = static::VALID_SETTINGS;
    503503                $schema['settings']['blocks'] = $schema_settings_blocks;
    504504
    … …  
    514514                        }
    515515
    516                         $result = self::remove_keys_not_in_schema( $input[ $subtree ], $schema[ $subtree ] );
     516                        $result = static::remove_keys_not_in_schema( $input[ $subtree ], $schema[ $subtree ] );
    517517
    518518                        if ( empty( $result ) ) {
    … …  
    555555         * @return array Block metadata.
    556556         */
    557         private static function get_blocks_metadata() {
    558                 if ( null !== self::$blocks_metadata ) {
    559                         return self::$blocks_metadata;
    560                 }
    561 
    562                 self::$blocks_metadata = array();
     557        protected static function get_blocks_metadata() {
     558                if ( null !== static::$blocks_metadata ) {
     559                        return static::$blocks_metadata;
     560                }
     561
     562                static::$blocks_metadata = array();
    563563
    564564                $registry = WP_Block_Type_Registry::get_instance();
    … …  
    569569                                is_string( $block_type->supports['__experimentalSelector'] )
    570570                        ) {
    571                                 self::$blocks_metadata[ $block_name ]['selector'] = $block_type->supports['__experimentalSelector'];
     571                                static::$blocks_metadata[ $block_name ]['selector'] = $block_type->supports['__experimentalSelector'];
    572572                        } else {
    573                                 self::$blocks_metadata[ $block_name ]['selector'] = '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name ) );
     573                                static::$blocks_metadata[ $block_name ]['selector'] = '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name ) );
    574574                        }
    575575
    … …  
    578578                                is_string( $block_type->supports['color']['__experimentalDuotone'] )
    579579                        ) {
    580                                 self::$blocks_metadata[ $block_name ]['duotone'] = $block_type->supports['color']['__experimentalDuotone'];
     580                                static::$blocks_metadata[ $block_name ]['duotone'] = $block_type->supports['color']['__experimentalDuotone'];
    581581                        }
    582582
    … …  
    584584                        // If the block selector is compounded, will append the element to each
    585585                        // individual block selector.
    586                         $block_selectors = explode( ',', self::$blocks_metadata[ $block_name ]['selector'] );
    587                         foreach ( self::ELEMENTS as $el_name => $el_selector ) {
     586                        $block_selectors = explode( ',', static::$blocks_metadata[ $block_name ]['selector'] );
     587                        foreach ( static::ELEMENTS as $el_name => $el_selector ) {
    588588                                $element_selector = array();
    589589                                foreach ( $block_selectors as $selector ) {
    590590                                        $element_selector[] = $selector . ' ' . $el_selector;
    591591                                }
    592                                 self::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = implode( ',', $element_selector );
    593                         }
    594                 }
    595 
    596                 return self::$blocks_metadata;
     592                                static::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = implode( ',', $element_selector );
     593                        }
     594                }
     595
     596                return static::$blocks_metadata;
    597597        }
    598598
    … …  
    608608         * @return array Returns the modified $tree.
    609609         */
    610         private static function remove_keys_not_in_schema( $tree, $schema ) {
     610        protected static function remove_keys_not_in_schema( $tree, $schema ) {
    611611                $tree = array_intersect_key( $tree, $schema );
    612612
    … …  
    617617
    618618                        if ( is_array( $schema[ $key ] ) && is_array( $tree[ $key ] ) ) {
    619                                 $tree[ $key ] = self::remove_keys_not_in_schema( $tree[ $key ], $schema[ $key ] );
     619                                $tree[ $key ] = static::remove_keys_not_in_schema( $tree[ $key ], $schema[ $key ] );
    620620
    621621                                if ( empty( $tree[ $key ] ) ) {
    … …  
    671671         *                       - `styles`: only the styles section in theme.json.
    672672         *                       - `presets`: only the classes for the presets.
    673          * @param array $origins A list of origins to include. By default it includes `self::VALID_ORIGINS`.
     673         * @param array $origins A list of origins to include. By default it includes VALID_ORIGINS.
    674674         * @return string Stylesheet.
    675675         */
    676         public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' ), $origins = self::VALID_ORIGINS ) {
     676        public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' ), $origins = null ) {
     677                if ( null === $origins ) {
     678                        $origins = static::VALID_ORIGINS;
     679                }
     680
    677681                if ( is_string( $types ) ) {
    678682                        // Dispatch error and map old arguments to new ones.
    … …  
    687691                }
    688692
    689                 $blocks_metadata = self::get_blocks_metadata();
    690                 $style_nodes     = self::get_style_nodes( $this->theme_json, $blocks_metadata );
    691                 $setting_nodes   = self::get_setting_nodes( $this->theme_json, $blocks_metadata );
     693                $blocks_metadata = static::get_blocks_metadata();
     694                $style_nodes     = static::get_style_nodes( $this->theme_json, $blocks_metadata );
     695                $setting_nodes   = static::get_setting_nodes( $this->theme_json, $blocks_metadata );
    692696
    693697                $stylesheet = '';
    … …  
    776780         * @return string The new stylesheet.
    777781         */
    778         private function get_block_classes( $style_nodes ) {
     782        protected function get_block_classes( $style_nodes ) {
    779783                $block_rules = '';
    780784
    … …  
    787791                        $selector     = $metadata['selector'];
    788792                        $settings     = _wp_array_get( $this->theme_json, array( 'settings' ) );
    789                         $declarations = self::compute_style_properties( $node, $settings );
     793                        $declarations = static::compute_style_properties( $node, $settings );
    790794
    791795                        // 1. Separate the ones who use the general selector
    … …  
    807811                         * @link https://github.com/WordPress/gutenberg/issues/36147.
    808812                         */
    809                         if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
     813                        if ( static::ROOT_BLOCK_SELECTOR === $selector ) {
    810814                                $block_rules .= 'body { margin: 0; }';
    811815                        }
    812816
    813817                        // 2. Generate the rules that use the general selector.
    814                         $block_rules .= self::to_ruleset( $selector, $declarations );
     818                        $block_rules .= static::to_ruleset( $selector, $declarations );
    815819
    816820                        // 3. Generate the rules that use the duotone selector.
    817821                        if ( isset( $metadata['duotone'] ) && ! empty( $declarations_duotone ) ) {
    818                                 $selector_duotone = self::scope_selector( $metadata['selector'], $metadata['duotone'] );
    819                                 $block_rules     .= self::to_ruleset( $selector_duotone, $declarations_duotone );
    820                         }
    821 
    822                         if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
     822                                $selector_duotone = static::scope_selector( $metadata['selector'], $metadata['duotone'] );
     823                                $block_rules     .= static::to_ruleset( $selector_duotone, $declarations_duotone );
     824                        }
     825
     826                        if ( static::ROOT_BLOCK_SELECTOR === $selector ) {
    823827                                $block_rules .= '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }';
    824828                                $block_rules .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }';
    … …  
    865869         * @return string The new stylesheet.
    866870         */
    867         private function get_preset_classes( $setting_nodes, $origins ) {
     871        protected function get_preset_classes( $setting_nodes, $origins ) {
    868872                $preset_rules = '';
    869873
    … …  
    875879                        $selector      = $metadata['selector'];
    876880                        $node          = _wp_array_get( $this->theme_json, $metadata['path'], array() );
    877                         $preset_rules .= self::compute_preset_classes( $node, $selector, $origins );
     881                        $preset_rules .= static::compute_preset_classes( $node, $selector, $origins );
    878882                }
    879883
    … …  
    902906         * @return string The new stylesheet.
    903907         */
    904         private function get_css_variables( $nodes, $origins ) {
     908        protected function get_css_variables( $nodes, $origins ) {
    905909                $stylesheet = '';
    906910                foreach ( $nodes as $metadata ) {
    … …  
    912916
    913917                        $node         = _wp_array_get( $this->theme_json, $metadata['path'], array() );
    914                         $declarations = array_merge( self::compute_preset_vars( $node, $origins ), self::compute_theme_vars( $node ) );
    915 
    916                         $stylesheet .= self::to_ruleset( $selector, $declarations );
     918                        $declarations = array_merge( static::compute_preset_vars( $node, $origins ), static::compute_theme_vars( $node ) );
     919
     920                        $stylesheet .= static::to_ruleset( $selector, $declarations );
    917921                }
    918922
    … …  
    930934         * @return string CSS ruleset.
    931935         */
    932         private static function to_ruleset( $selector, $declarations ) {
     936        protected static function to_ruleset( $selector, $declarations ) {
    933937                if ( empty( $declarations ) ) {
    934938                        return '';
    … …  
    958962         * @return string
    959963         */
    960         private static function append_to_selector( $selector, $to_append ) {
     964        protected static function append_to_selector( $selector, $to_append ) {
    961965                $new_selectors = array();
    962966                $selectors     = explode( ',', $selector );
    … …  
    980984         * @return string The result of processing the presets.
    981985         */
    982         private static function compute_preset_classes( $settings, $selector, $origins ) {
    983                 if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
     986        protected static function compute_preset_classes( $settings, $selector, $origins ) {
     987                if ( static::ROOT_BLOCK_SELECTOR === $selector ) {
    984988                        // Classes at the global level do not need any CSS prefixed,
    985989                        // and we don't want to increase its specificity.
    … …  
    988992
    989993                $stylesheet = '';
    990                 foreach ( self::PRESETS_METADATA as $preset_metadata ) {
    991                         $slugs = self::get_settings_slugs( $settings, $preset_metadata, $origins );
     994                foreach ( static::PRESETS_METADATA as $preset_metadata ) {
     995                        $slugs = static::get_settings_slugs( $settings, $preset_metadata, $origins );
    992996                        foreach ( $preset_metadata['classes'] as $class => $property ) {
    993997                                foreach ( $slugs as $slug ) {
    994                                         $css_var     = self::replace_slug_in_string( $preset_metadata['css_vars'], $slug );
    995                                         $class_name  = self::replace_slug_in_string( $class, $slug );
    996                                         $stylesheet .= self::to_ruleset(
    997                                                 self::append_to_selector( $selector, $class_name ),
     998                                        $css_var     = static::replace_slug_in_string( $preset_metadata['css_vars'], $slug );
     999                                        $class_name  = static::replace_slug_in_string( $class, $slug );
     1000                                        $stylesheet .= static::to_ruleset(
     1001                                                static::append_to_selector( $selector, $class_name ),
    9981002                                                array(
    9991003                                                        array(
    … …  
    10271031         * @return string Scoped selector.
    10281032         */
    1029         private static function scope_selector( $scope, $selector ) {
     1033        protected static function scope_selector( $scope, $selector ) {
    10301034                $scopes    = explode( ',', $scope );
    10311035                $selectors = explode( ',', $selector );
    … …  
    10771081         * @return array Array of presets where each key is a slug and each value is the preset value.
    10781082         */
    1079         private static function get_settings_values_by_slug( $settings, $preset_metadata, $origins ) {
     1083        protected static function get_settings_values_by_slug( $settings, $preset_metadata, $origins ) {
    10801084                $preset_per_origin = _wp_array_get( $settings, $preset_metadata['path'], array() );
    10811085
    … …  
    11191123         * @return array Array of presets where the key and value are both the slug.
    11201124         */
    1121         private static function get_settings_slugs( $settings, $preset_metadata, $origins = self::VALID_ORIGINS ) {
     1125        protected static function get_settings_slugs( $settings, $preset_metadata, $origins = null ) {
     1126                if ( null === $origins ) {
     1127                        $origins = static::VALID_ORIGINS;
     1128                }
     1129
    11221130                $preset_per_origin = _wp_array_get( $settings, $preset_metadata['path'], array() );
    11231131
    … …  
    11461154         * @return string The CSS Custom Property. Something along the lines of `--wp--preset--color--black`.
    11471155         */
    1148         private static function replace_slug_in_string( $input, $slug ) {
     1156        protected static function replace_slug_in_string( $input, $slug ) {
    11491157                return strtr( $input, array( '$slug' => $slug ) );
    11501158        }
    … …  
    11671175         * @return array Returns the modified $declarations.
    11681176         */
    1169         private static function compute_preset_vars( $settings, $origins ) {
     1177        protected static function compute_preset_vars( $settings, $origins ) {
    11701178                $declarations = array();
    1171                 foreach ( self::PRESETS_METADATA as $preset_metadata ) {
    1172                         $values_by_slug = self::get_settings_values_by_slug( $settings, $preset_metadata, $origins );
     1179                foreach ( static::PRESETS_METADATA as $preset_metadata ) {
     1180                        $values_by_slug = static::get_settings_values_by_slug( $settings, $preset_metadata, $origins );
    11731181                        foreach ( $values_by_slug as $slug => $value ) {
    11741182                                $declarations[] = array(
    1175                                         'name'  => self::replace_slug_in_string( $preset_metadata['css_vars'], $slug ),
     1183                                        'name'  => static::replace_slug_in_string( $preset_metadata['css_vars'], $slug ),
    11761184                                        'value' => $value,
    11771185                                );
    … …  
    11971205         * @return array Returns the modified $declarations.
    11981206         */
    1199         private static function compute_theme_vars( $settings ) {
     1207        protected static function compute_theme_vars( $settings ) {
    12001208                $declarations  = array();
    12011209                $custom_values = _wp_array_get( $settings, array( 'custom' ), array() );
    1202                 $css_vars      = self::flatten_tree( $custom_values );
     1210                $css_vars      = static::flatten_tree( $custom_values );
    12031211                foreach ( $css_vars as $key => $value ) {
    12041212                        $declarations[] = array(
    … …  
    12481256         * @return array The flattened tree.
    12491257         */
    1250         private static function flatten_tree( $tree, $prefix = '', $token = '--' ) {
     1258        protected static function flatten_tree( $tree, $prefix = '', $token = '--' ) {
    12511259                $result = array();
    12521260                foreach ( $tree as $property => $value ) {
    … …  
    12611269                                $result     = array_merge(
    12621270                                        $result,
    1263                                         self::flatten_tree( $value, $new_prefix, $token )
     1271                                        static::flatten_tree( $value, $new_prefix, $token )
    12641272                                );
    12651273                        } else {
    … …  
    12871295         * @return array Returns the modified $declarations.
    12881296         */
    1289         private static function compute_style_properties( $styles, $settings = array(), $properties = self::PROPERTIES_METADATA ) {
     1297        protected static function compute_style_properties( $styles, $settings = array(), $properties = null ) {
     1298                if ( null === $properties ) {
     1299                        $properties = static::PROPERTIES_METADATA;
     1300                }
     1301
    12901302                $declarations = array();
    12911303                if ( empty( $styles ) ) {
    … …  
    12941306
    12951307                foreach ( $properties as $css_property => $value_path ) {
    1296                         $value = self::get_property_value( $styles, $value_path );
     1308                        $value = static::get_property_value( $styles, $value_path );
    12971309
    12981310                        // Look up protected properties, keyed by value path.
    … …  
    13011313                                $path_string = implode( '.', $value_path );
    13021314                                if (
    1303                                         array_key_exists( $path_string, self::PROTECTED_PROPERTIES ) &&
    1304                                         _wp_array_get( $settings, self::PROTECTED_PROPERTIES[ $path_string ], null ) === null
     1315                                        array_key_exists( $path_string, static::PROTECTED_PROPERTIES ) &&
     1316                                        _wp_array_get( $settings, static::PROTECTED_PROPERTIES[ $path_string ], null ) === null
    13051317                                ) {
    13061318                                        continue;
    … …  
    13371349         * @return string|array Style property value.
    13381350         */
    1339         private static function get_property_value( $styles, $path ) {
     1351        protected static function get_property_value( $styles, $path ) {
    13401352                $value = _wp_array_get( $styles, $path, '' );
    13411353
    … …  
    13801392         * @return array
    13811393         */
    1382         private static function get_setting_nodes( $theme_json, $selectors = array() ) {
     1394        protected static function get_setting_nodes( $theme_json, $selectors = array() ) {
    13831395                $nodes = array();
    13841396                if ( ! isset( $theme_json['settings'] ) ) {
    … …  
    13891401                $nodes[] = array(
    13901402                        'path'     => array( 'settings' ),
    1391                         'selector' => self::ROOT_BLOCK_SELECTOR,
     1403                        'selector' => static::ROOT_BLOCK_SELECTOR,
    13921404                );
    13931405
    … …  
    14341446         * @return array
    14351447         */
    1436         private static function get_style_nodes( $theme_json, $selectors = array() ) {
     1448        protected static function get_style_nodes( $theme_json, $selectors = array() ) {
    14371449                $nodes = array();
    14381450                if ( ! isset( $theme_json['styles'] ) ) {
    … …  
    14431455                $nodes[] = array(
    14441456                        'path'     => array( 'styles' ),
    1445                         'selector' => self::ROOT_BLOCK_SELECTOR,
     1457                        'selector' => static::ROOT_BLOCK_SELECTOR,
    14461458                );
    14471459
    … …  
    14501462                                $nodes[] = array(
    14511463                                        'path'     => array( 'styles', 'elements', $element ),
    1452                                         'selector' => self::ELEMENTS[ $element ],
     1464                                        'selector' => static::ELEMENTS[ $element ],
    14531465                                );
    14541466                        }
    … …  
    15241536                 * we remove it from the theme presets.
    15251537                 */
    1526                 $nodes        = self::get_setting_nodes( $incoming_data );
    1527                 $slugs_global = self::get_default_slugs( $this->theme_json, array( 'settings' ) );
     1538                $nodes        = static::get_setting_nodes( $incoming_data );
     1539                $slugs_global = static::get_default_slugs( $this->theme_json, array( 'settings' ) );
    15281540                foreach ( $nodes as $node ) {
    1529                         $slugs_node = self::get_default_slugs( $this->theme_json, $node['path'] );
     1541                        $slugs_node = static::get_default_slugs( $this->theme_json, $node['path'] );
    15301542                        $slugs      = array_merge_recursive( $slugs_global, $slugs_node );
    15311543
    … …  
    15381550
    15391551                        // Replace the presets.
    1540                         foreach ( self::PRESETS_METADATA as $preset ) {
    1541                                 $override_preset = self::should_override_preset( $this->theme_json, $node['path'], $preset['override'] );
    1542 
    1543                                 foreach ( self::VALID_ORIGINS as $origin ) {
     1552                        foreach ( static::PRESETS_METADATA as $preset ) {
     1553                                $override_preset = static::should_override_preset( $this->theme_json, $node['path'], $preset['override'] );
     1554
     1555                                foreach ( static::VALID_ORIGINS as $origin ) {
    15441556                                        $base_path = array_merge( $node['path'], $preset['path'] );
    15451557                                        $path      = array_merge( $base_path, array( $origin ) );
    … …  
    15521564                                                foreach ( $content as &$item ) {
    15531565                                                        if ( ! array_key_exists( 'name', $item ) ) {
    1554                                                                 $name = self::get_name_from_defaults( $item['slug'], $base_path );
     1566                                                                $name = static::get_name_from_defaults( $item['slug'], $base_path );
    15551567                                                                if ( null !== $name ) {
    15561568                                                                        $item['name'] = $name;
    … …  
    15671579                                        } else {
    15681580                                                $slugs_for_preset = _wp_array_get( $slugs, $preset['path'], array() );
    1569                                                 $content          = self::filter_slugs( $content, $slugs_for_preset );
     1581                                                $content          = static::filter_slugs( $content, $slugs_for_preset );
    15701582                                                _wp_array_set( $this->theme_json, $path, $content );
    15711583                                        }
    … …  
    15851597         * @return boolean
    15861598         */
    1587         private static function should_override_preset( $theme_json, $path, $override ) {
     1599        protected static function should_override_preset( $theme_json, $path, $override ) {
    15881600                if ( is_bool( $override ) ) {
    15891601                        return $override;
    … …  
    16371649         * @return array
    16381650         */
    1639         private static function get_default_slugs( $data, $node_path ) {
     1651        protected static function get_default_slugs( $data, $node_path ) {
    16401652                $slugs = array();
    16411653
    1642                 foreach ( self::PRESETS_METADATA as $metadata ) {
     1654                foreach ( static::PRESETS_METADATA as $metadata ) {
    16431655                        $path   = array_merge( $node_path, $metadata['path'], array( 'default' ) );
    16441656                        $preset = _wp_array_get( $data, $path, null );
    … …  
    16691681         * @return string|null
    16701682         */
    1671         private function get_name_from_defaults( $slug, $base_path ) {
     1683        protected function get_name_from_defaults( $slug, $base_path ) {
    16721684                $path            = array_merge( $base_path, array( 'default' ) );
    16731685                $default_content = _wp_array_get( $this->theme_json, $path, null );
    … …  
    16921704         * @return array The new node.
    16931705         */
    1694         private static function filter_slugs( $node, $slugs ) {
     1706        protected static function filter_slugs( $node, $slugs ) {
    16951707                if ( empty( $slugs ) ) {
    16961708                        return $node;
    … …  
    17201732                $theme_json = WP_Theme_JSON_Schema::migrate( $theme_json );
    17211733
    1722                 $valid_block_names   = array_keys( self::get_blocks_metadata() );
    1723                 $valid_element_names = array_keys( self::ELEMENTS );
    1724                 $theme_json          = self::sanitize( $theme_json, $valid_block_names, $valid_element_names );
    1725 
    1726                 $blocks_metadata = self::get_blocks_metadata();
    1727                 $style_nodes     = self::get_style_nodes( $theme_json, $blocks_metadata );
     1734                $valid_block_names   = array_keys( static::get_blocks_metadata() );
     1735                $valid_element_names = array_keys( static::ELEMENTS );
     1736                $theme_json          = static::sanitize( $theme_json, $valid_block_names, $valid_element_names );
     1737
     1738                $blocks_metadata = static::get_blocks_metadata();
     1739                $style_nodes     = static::get_style_nodes( $theme_json, $blocks_metadata );
    17281740                foreach ( $style_nodes as $metadata ) {
    17291741                        $input = _wp_array_get( $theme_json, $metadata['path'], array() );
    … …  
    17321744                        }
    17331745
    1734                         $output = self::remove_insecure_styles( $input );
     1746                        $output = static::remove_insecure_styles( $input );
    17351747                        if ( ! empty( $output ) ) {
    17361748                                _wp_array_set( $sanitized, $metadata['path'], $output );
    … …  
    17381750                }
    17391751
    1740                 $setting_nodes = self::get_setting_nodes( $theme_json );
     1752                $setting_nodes = static::get_setting_nodes( $theme_json );
    17411753                foreach ( $setting_nodes as $metadata ) {
    17421754                        $input = _wp_array_get( $theme_json, $metadata['path'], array() );
    … …  
    17451757                        }
    17461758
    1747                         $output = self::remove_insecure_settings( $input );
     1759                        $output = static::remove_insecure_settings( $input );
    17481760                        if ( ! empty( $output ) ) {
    17491761                                _wp_array_set( $sanitized, $metadata['path'], $output );
    … …  
    17751787         * @return array
    17761788         */
    1777         private static function remove_insecure_settings( $input ) {
     1789        protected static function remove_insecure_settings( $input ) {
    17781790                $output = array();
    1779                 foreach ( self::PRESETS_METADATA as $preset_metadata ) {
    1780                         foreach ( self::VALID_ORIGINS as $origin ) {
     1791                foreach ( static::PRESETS_METADATA as $preset_metadata ) {
     1792                        foreach ( static::VALID_ORIGINS as $origin ) {
    17811793                                $path_with_origin = array_merge( $preset_metadata['path'], array( $origin ) );
    17821794                                $presets          = _wp_array_get( $input, $path_with_origin, null );
    … …  
    18031815                                                $preset_is_valid = true;
    18041816                                                foreach ( $preset_metadata['properties'] as $property ) {
    1805                                                         if ( ! self::is_safe_css_declaration( $property, $value ) ) {
     1817                                                        if ( ! static::is_safe_css_declaration( $property, $value ) ) {
    18061818                                                                $preset_is_valid = false;
    18071819                                                                break;
    … …  
    18321844         * @return array
    18331845         */
    1834         private static function remove_insecure_styles( $input ) {
     1846        protected static function remove_insecure_styles( $input ) {
    18351847                $output       = array();
    1836                 $declarations = self::compute_style_properties( $input );
     1848                $declarations = static::compute_style_properties( $input );
    18371849
    18381850                foreach ( $declarations as $declaration ) {
    1839                         if ( self::is_safe_css_declaration( $declaration['name'], $declaration['value'] ) ) {
    1840                                 $path = self::PROPERTIES_METADATA[ $declaration['name'] ];
     1851                        if ( static::is_safe_css_declaration( $declaration['name'], $declaration['value'] ) ) {
     1852                                $path = static::PROPERTIES_METADATA[ $declaration['name'] ];
    18411853
    18421854                                // Check the value isn't an array before adding so as to not
    … …  
    18601872         * @return bool
    18611873         */
    1862         private static function is_safe_css_declaration( $property_name, $property_value ) {
     1874        protected static function is_safe_css_declaration( $property_name, $property_value ) {
    18631875                $style_to_validate = $property_name . ': ' . $property_value;
    18641876                $filtered          = esc_html( safecss_filter_attr( $style_to_validate ) );
    … …  
    18881900        public static function get_from_editor_settings( $settings ) {
    18891901                $theme_settings = array(
    1890                         'version'  => self::LATEST_SCHEMA,
     1902                        'version'  => static::LATEST_SCHEMA,
    18911903                        'settings' => array(),
    18921904                );
Note: See TracChangeset for help on using the changeset viewer.