From d335e0e4095a95c2c8c1560c7abff5a68d40baec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Mon, 29 Nov 2021 22:35:17 +0100 Subject: [PATCH] Update the `WP_Theme_JSON_Gutenberg` class to be like the core one (#36973) --- lib/class-wp-theme-json-gutenberg.php | 1311 +++++++++++++------------ 1 file changed, 666 insertions(+), 645 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index fa4eced8949ae..7dd2c94bf3088 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -1,13 +1,18 @@ array( - 'color' => null, - 'radius' => null, - 'style' => null, - 'width' => null, - ), - 'color' => array( - 'background' => null, - 'gradient' => null, - 'text' => null, - ), - 'filter' => array( - 'duotone' => null, - ), - 'spacing' => array( - 'margin' => null, - 'padding' => null, - 'blockGap' => null, - ), - 'typography' => array( - 'fontFamily' => null, - 'fontSize' => null, - 'fontStyle' => null, - 'fontWeight' => null, - 'letterSpacing' => null, - 'lineHeight' => null, - 'textDecoration' => null, - 'textTransform' => null, - ), - ); - - const VALID_SETTINGS = array( - 'appearanceTools' => null, - 'border' => array( - 'color' => null, - 'radius' => null, - 'style' => null, - 'width' => null, - ), - 'color' => array( - 'background' => null, - 'custom' => null, - 'customDuotone' => null, - 'customGradient' => null, - 'defaultGradients' => null, - 'defaultPalette' => null, - 'duotone' => null, - 'gradients' => null, - 'link' => null, - 'palette' => null, - 'text' => null, - ), - 'custom' => null, - 'layout' => array( - 'contentSize' => null, - 'wideSize' => null, - ), - 'spacing' => array( - 'blockGap' => null, - 'margin' => null, - 'padding' => null, - 'units' => null, - ), - 'typography' => array( - 'customFontSize' => null, - 'dropCap' => null, - 'fontFamilies' => null, - 'fontSizes' => null, - 'fontStyle' => null, - 'fontWeight' => null, - 'letterSpacing' => null, - 'lineHeight' => null, - 'textDecoration' => null, - 'textTransform' => null, - ), - ); - /** * Presets are a set of values that serve * to bootstrap some styles: colors, font sizes, etc. @@ -145,21 +70,15 @@ class WP_Theme_JSON_Gutenberg { * This contains the necessary metadata to process them: * * - path => where to find the preset within the settings section - * * - override => whether a theme preset with the same slug as a default preset * can override it - * * - value_key => the key that represents the value - * * - value_func => optionally, instead of value_key, a function to generate * the value that takes a preset as an argument - * - * - css_var => name of the var to generate. The "$slug" substring will be - * replaced by the slug of each preset. For example, - * given a preset for color with two values whose slugs are "black" and "white", - * the string "--wp--preset--color--$slug" will generate two variables: - * "--wp--preset--color--black" and "--wp--preset--color--white". - * + * (either value_key or value_func should be present) + * - css_vars => template string to use in generating the CSS Custom Property. + * Example output: "--wp--preset--duotone--blue: " will generate as many CSS Custom Properties as presets defined + * substituting the $slug for the slug's value for each preset value. * - classes => array containing a structure with the classes to * generate for the presets, where for each array item * the key is the class name and the value the property name. @@ -274,6 +193,112 @@ class WP_Theme_JSON_Gutenberg { 'spacing.blockGap' => array( 'spacing', 'blockGap' ), ); + /** + * The top-level keys a theme.json can have. + * + * @var string[] + */ + const VALID_TOP_LEVEL_KEYS = array( + 'customTemplates', + 'settings', + 'styles', + 'templateParts', + 'version', + ); + + /** + * The valid properties under the settings key. + * + * @var array + */ + const VALID_SETTINGS = array( + 'appearanceTools' => null, + 'border' => array( + 'color' => null, + 'radius' => null, + 'style' => null, + 'width' => null, + ), + 'color' => array( + 'background' => null, + 'custom' => null, + 'customDuotone' => null, + 'customGradient' => null, + 'defaultGradients' => null, + 'defaultPalette' => null, + 'duotone' => null, + 'gradients' => null, + 'link' => null, + 'palette' => null, + 'text' => null, + ), + 'custom' => null, + 'layout' => array( + 'contentSize' => null, + 'wideSize' => null, + ), + 'spacing' => array( + 'blockGap' => null, + 'margin' => null, + 'padding' => null, + 'units' => null, + ), + 'typography' => array( + 'customFontSize' => null, + 'dropCap' => null, + 'fontFamilies' => null, + 'fontSizes' => null, + 'fontStyle' => null, + 'fontWeight' => null, + 'letterSpacing' => null, + 'lineHeight' => null, + 'textDecoration' => null, + 'textTransform' => null, + ), + ); + + /** + * The valid properties under the styles key. + * + * @var array + */ + const VALID_STYLES = array( + 'border' => array( + 'color' => null, + 'radius' => null, + 'style' => null, + 'width' => null, + ), + 'color' => array( + 'background' => null, + 'gradient' => null, + 'text' => null, + ), + 'filter' => array( + 'duotone' => null, + ), + 'spacing' => array( + 'margin' => null, + 'padding' => null, + 'blockGap' => null, + ), + 'typography' => array( + 'fontFamily' => null, + 'fontSize' => null, + 'fontStyle' => null, + 'fontWeight' => null, + 'letterSpacing' => null, + 'lineHeight' => null, + 'textDecoration' => null, + 'textTransform' => null, + ), + ); + + /** + * The valid elements that can be found under styles. + * + * @var string[] + */ const ELEMENTS = array( 'link' => 'a', 'h1' => 'h1', @@ -284,24 +309,29 @@ class WP_Theme_JSON_Gutenberg { 'h6' => 'h6', ); + /** + * The latest version of the schema in use. + * + * @var int + */ const LATEST_SCHEMA = 2; /** * Constructor. * * @param array $theme_json A structure that follows the theme.json schema. - * @param string $origin What source of data this object represents. One of default, theme, or custom. Default: theme. + * @param string $origin Optional. What source of data this object represents. + * One of 'default', 'theme', or 'custom'. Default 'theme'. */ public function __construct( $theme_json = array(), $origin = 'theme' ) { if ( ! in_array( $origin, self::VALID_ORIGINS, true ) ) { $origin = 'theme'; } - $theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json ); - + $this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json ); $valid_block_names = array_keys( self::get_blocks_metadata() ); $valid_element_names = array_keys( self::ELEMENTS ); - $theme_json = self::sanitize( $theme_json, $valid_block_names, $valid_element_names ); + $theme_json = self::sanitize( $this->theme_json, $valid_block_names, $valid_element_names ); $this->theme_json = self::maybe_opt_in_into_settings( $theme_json ); // Internally, presets are keyed by origin. @@ -377,7 +407,6 @@ private static function do_opt_in_into_settings( &$context ) { * @param array $input Structure to sanitize. * @param array $valid_block_names List of valid block names. * @param array $valid_element_names List of valid element names. - * * @return array The sanitized output. */ private static function sanitize( $input, $valid_block_names, $valid_element_names ) { @@ -436,21 +465,24 @@ private static function sanitize( $input, $valid_block_names, $valid_element_nam * * Example: * - * { - * 'core/paragraph': { - * 'selector': 'p' - * }, - * 'core/heading': { - * 'selector': 'h1' - * }, - * 'core/group': { - * 'selector': '.wp-block-group' - * }, - * 'core/cover': { - * 'selector': '.wp-block-cover', - * 'duotone': '> .wp-block-cover__image-background, > .wp-block-cover__video-background' - * } - * } + * { + * 'core/paragraph': { + * 'selector': 'p', + * 'elements': { + * 'link' => 'link selector', + * 'etc' => 'element selector' + * } + * }, + * 'core/heading': { + * 'selector': 'h1', + * 'elements': {} + * }, + * 'core/image': { + * 'selector': '.wp-block-image', + * 'duotone': 'img', + * 'elements': {} + * } + * } * * @return array Block metadata. */ @@ -503,7 +535,6 @@ private static function get_blocks_metadata() { * * @param array $tree Input to process. * @param array $schema Schema to adhere to. - * * @return array Returns the modified $tree. */ private static function remove_keys_not_in_schema( $tree, $schema ) { @@ -529,149 +560,302 @@ private static function remove_keys_not_in_schema( $tree, $schema ) { } /** - * Given a tree, it creates a flattened one - * by merging the keys and binding the leaf values - * to the new keys. - * - * It also transforms camelCase names into kebab-case - * and substitutes '/' by '-'. - * - * This is thought to be useful to generate - * CSS Custom Properties from a tree, - * although there's nothing in the implementation - * of this function that requires that format. - * - * For example, assuming the given prefix is '--wp' - * and the token is '--', for this input tree: - * - * { - * 'some/property': 'value', - * 'nestedProperty': { - * 'sub-property': 'value' - * } - * } - * - * it'll return this output: + * Returns the existing settings for each block. * - * { - * '--wp--some-property': 'value', - * '--wp--nested-property--sub-property': 'value' - * } + * Example: * - * @param array $tree Input tree to process. - * @param string $prefix Prefix to prepend to each variable. '' by default. - * @param string $token Token to use between levels. '--' by default. + * { + * 'root': { + * 'color': { + * 'custom': true + * } + * }, + * 'core/paragraph': { + * 'spacing': { + * 'customPadding': true + * } + * } + * } * - * @return array The flattened tree. + * @return array Settings per block. */ - private static function flatten_tree( $tree, $prefix = '', $token = '--' ) { - $result = array(); - foreach ( $tree as $property => $value ) { - $new_key = $prefix . str_replace( - '/', - '-', - strtolower( preg_replace( '/(?theme_json['settings'] ) ) { + return array(); + } else { + return $this->theme_json['settings']; } - return $result; } /** - * Returns the style property for the given path. + * Returns the stylesheet that results of processing + * the theme.json structure this object represents. * - * It also converts CSS Custom Property stored as - * "var:preset|color|secondary" to the form - * "--wp--preset--color--secondary". - * - * @param array $styles Styles subtree. - * @param array $path Which property to process. + * @param array $types Types of styles to load. Will load all by default. It accepts: + * 'variables': only the CSS Custom Properties for presets & custom ones. + * 'styles': only the styles section in theme.json. + * 'presets': only the classes for the presets. + * @param array $origins A list of origins to include. By default it includes self::VALID_ORIGINS. + * @return string Stylesheet. + */ + public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' ), $origins = self::VALID_ORIGINS ) { + if ( is_string( $types ) ) { + // Dispatch error and map old arguments to new ones. + _deprecated_argument( __FUNCTION__, '5.9' ); + if ( 'block_styles' === $types ) { + $types = array( 'styles', 'presets' ); + } elseif ( 'css_variables' === $types ) { + $types = array( 'variables' ); + } else { + $types = array( 'variables', 'styles', 'presets' ); + } + } + + $blocks_metadata = self::get_blocks_metadata(); + $style_nodes = self::get_style_nodes( $this->theme_json, $blocks_metadata ); + $setting_nodes = self::get_setting_nodes( $this->theme_json, $blocks_metadata ); + + $stylesheet = ''; + + if ( in_array( 'variables', $types, true ) ) { + $stylesheet .= $this->get_css_variables( $setting_nodes, $origins ); + } + + if ( in_array( 'styles', $types, true ) ) { + $stylesheet .= $this->get_block_classes( $style_nodes ); + } + + if ( in_array( 'presets', $types, true ) ) { + $stylesheet .= $this->get_preset_classes( $setting_nodes, $origins ); + } + + return $stylesheet; + } + + /** + * Returns the page templates of the current theme. * - * @return string Style property value. + * @return array */ - private static function get_property_value( $styles, $path ) { - $value = _wp_array_get( $styles, $path, '' ); + public function get_custom_templates() { + $custom_templates = array(); + if ( ! isset( $this->theme_json['customTemplates'] ) || ! is_array( $this->theme_json['customTemplates'] ) ) { + return $custom_templates; + } - if ( '' === $value || is_array( $value ) ) { - return $value; + foreach ( $this->theme_json['customTemplates'] as $item ) { + if ( isset( $item['name'] ) ) { + $custom_templates[ $item['name'] ] = array( + 'title' => isset( $item['title'] ) ? $item['title'] : '', + 'postTypes' => isset( $item['postTypes'] ) ? $item['postTypes'] : array( 'page' ), + ); + } } + return $custom_templates; + } - $prefix = 'var:'; - $prefix_len = strlen( $prefix ); - $token_in = '|'; - $token_out = '--'; - if ( 0 === strncmp( $value, $prefix, $prefix_len ) ) { - $unwrapped_name = str_replace( - $token_in, - $token_out, - substr( $value, $prefix_len ) - ); - $value = "var(--wp--$unwrapped_name)"; + /** + * Returns the template part data of current theme. + * + * @return array + */ + public function get_template_parts() { + $template_parts = array(); + if ( ! isset( $this->theme_json['templateParts'] ) || ! is_array( $this->theme_json['templateParts'] ) ) { + return $template_parts; } - return $value; + foreach ( $this->theme_json['templateParts'] as $item ) { + if ( isset( $item['name'] ) ) { + $template_parts[ $item['name'] ] = array( + 'title' => isset( $item['title'] ) ? $item['title'] : '', + 'area' => isset( $item['area'] ) ? $item['area'] : '', + ); + } + } + return $template_parts; } /** - * Given a styles array, it extracts the style properties - * and adds them to the $declarations array following the format: + * Converts each style section into a list of rulesets + * containing the block styles to be appended to the stylesheet. * - * ```php - * array( - * 'name' => 'property_name', - * 'value' => 'property_value, - * ) - * ``` + * See glossary at https://developer.mozilla.org/en-US/docs/Web/CSS/Syntax * - * @param array $styles Styles to process. - * @param array $settings Theme settings. - * @param array $properties Properties metadata. + * For each section this creates a new ruleset such as: * - * @return array Returns the modified $declarations. + * block-selector { + * style-property-one: value; + * } + * + * @param array $style_nodes Nodes with styles. + * @return string The new stylesheet. */ - private static function compute_style_properties( $styles, $settings = array(), $properties = self::PROPERTIES_METADATA ) { - $declarations = array(); - if ( empty( $styles ) ) { - return $declarations; - } + private function get_block_classes( $style_nodes ) { + $block_rules = ''; - foreach ( $properties as $css_property => $value_path ) { - $value = self::get_property_value( $styles, $value_path ); + foreach ( $style_nodes as $metadata ) { + if ( null === $metadata['selector'] ) { + continue; + } - // Look up protected properties, keyed by value path. - // Skip protected properties that are explicitly set to `null`. - if ( is_array( $value_path ) ) { - $path_string = implode( '.', $value_path ); - if ( - array_key_exists( $path_string, self::PROTECTED_PROPERTIES ) && - _wp_array_get( $settings, self::PROTECTED_PROPERTIES[ $path_string ], null ) === null - ) { - continue; + $node = _wp_array_get( $this->theme_json, $metadata['path'], array() ); + $selector = $metadata['selector']; + $settings = _wp_array_get( $this->theme_json, array( 'settings' ) ); + $declarations = self::compute_style_properties( $node, $settings ); + + // 1. Separate the ones who use the general selector + // and the ones who use the duotone selector. + $declarations_duotone = array(); + foreach ( $declarations as $index => $declaration ) { + if ( 'filter' === $declaration['name'] ) { + unset( $declarations[ $index ] ); + $declarations_duotone[] = $declaration; } } - // Skip if empty and not "0" or value represents array of longhand values. - $has_missing_value = empty( $value ) && ! is_numeric( $value ); - if ( $has_missing_value || is_array( $value ) ) { + // 2. Generate the rules that use the general selector. + $block_rules .= self::to_ruleset( $selector, $declarations ); + + // 3. Generate the rules that use the duotone selector. + if ( isset( $metadata['duotone'] ) && ! empty( $declarations_duotone ) ) { + $selector_duotone = self::scope_selector( $metadata['selector'], $metadata['duotone'] ); + $block_rules .= self::to_ruleset( $selector_duotone, $declarations_duotone ); + } + + if ( self::ROOT_BLOCK_SELECTOR === $selector ) { + $block_rules .= 'body { margin: 0; }'; + $block_rules .= '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }'; + $block_rules .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }'; + $block_rules .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }'; + + $has_block_gap_support = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'blockGap' ) ) !== null; + if ( $has_block_gap_support ) { + $block_rules .= '.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }'; + $block_rules .= '.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }'; + } + } + } + + return $block_rules; + } + + /** + * Creates new rulesets as classes for each preset value such as: + * + * .has-value-color { + * color: value; + * } + * + * .has-value-background-color { + * background-color: value; + * } + * + * .has-value-font-size { + * font-size: value; + * } + * + * .has-value-gradient-background { + * background: value; + * } + * + * p.has-value-gradient-background { + * background: value; + * } + * + * @param array $setting_nodes Nodes with settings. + * @param array $origins List of origins to process presets from. + * @return string The new stylesheet. + */ + private function get_preset_classes( $setting_nodes, $origins ) { + $preset_rules = ''; + + foreach ( $setting_nodes as $metadata ) { + if ( null === $metadata['selector'] ) { continue; } - $declarations[] = array( - 'name' => $css_property, - 'value' => $value, + $selector = $metadata['selector']; + $node = _wp_array_get( $this->theme_json, $metadata['path'], array() ); + $preset_rules .= self::compute_preset_classes( $node, $selector, $origins ); + } + + return $preset_rules; + } + + /** + * Converts each styles section into a list of rulesets + * to be appended to the stylesheet. + * These rulesets contain all the css variables (custom variables and preset variables). + * + * See glossary at https://developer.mozilla.org/en-US/docs/Web/CSS/Syntax + * + * For each section this creates a new ruleset such as: + * + * block-selector { + * --wp--preset--category--slug: value; + * --wp--custom--variable: value; + * } + * + * @param array $nodes Nodes with settings. + * @param array $origins List of origins to process. + * @return string The new stylesheet. + */ + private function get_css_variables( $nodes, $origins ) { + $stylesheet = ''; + foreach ( $nodes as $metadata ) { + if ( null === $metadata['selector'] ) { + continue; + } + + $selector = $metadata['selector']; + + $node = _wp_array_get( $this->theme_json, $metadata['path'], array() ); + $declarations = array_merge( self::compute_preset_vars( $node, $origins ), self::compute_theme_vars( $node ) ); + + $stylesheet .= self::to_ruleset( $selector, $declarations ); + } + + return $stylesheet; + } + + /** + * Given a selector and a declaration list, + * creates the corresponding ruleset. + * + * To help debugging, will add some space + * if SCRIPT_DEBUG is defined and true. + * + * @param string $selector CSS selector. + * @param array $declarations List of declarations. + * + * @return string CSS ruleset. + */ + private static function to_ruleset( $selector, $declarations ) { + if ( empty( $declarations ) ) { + return ''; + } + $ruleset = ''; + + if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { + $declaration_block = array_reduce( + $declarations, + function ( $carry, $element ) { + return $carry .= "\t" . $element['name'] . ': ' . $element['value'] . ";\n"; }, + '' + ); + $ruleset .= $selector . " {\n" . $declaration_block . "}\n"; + } else { + $declaration_block = array_reduce( + $declarations, + function ( $carry, $element ) { + return $carry .= $element['name'] . ': ' . $element['value'] . ';'; }, + '' ); + $ruleset .= $selector . '{' . $declaration_block . '}'; } - return $declarations; + return $ruleset; } /** @@ -683,7 +867,6 @@ private static function compute_style_properties( $styles, $settings = array(), * * @param string $selector Original selector. * @param string $to_append Selector to append. - * * @return string */ private static function append_to_selector( $selector, $to_append ) { @@ -696,6 +879,45 @@ private static function append_to_selector( $selector, $to_append ) { return implode( ',', $new_selectors ); } + /** + * Given a settings array, it returns the generated rulesets + * for the preset classes. + * + * @param array $settings Settings to process. + * @param string $selector Selector wrapping the classes. + * @param array $origins List of origins to process. + * @return string The result of processing the presets. + */ + private static function compute_preset_classes( $settings, $selector, $origins ) { + if ( self::ROOT_BLOCK_SELECTOR === $selector ) { + // Classes at the global level do not need any CSS prefixed, + // and we don't want to increase its specificity. + $selector = ''; + } + + $stylesheet = ''; + foreach ( self::PRESETS_METADATA as $preset_metadata ) { + $slugs = self::get_settings_slugs( $settings, $preset_metadata, $origins ); + foreach ( $preset_metadata['classes'] as $class => $property ) { + foreach ( $slugs as $slug ) { + $css_var = self::replace_slug_in_string( $preset_metadata['css_vars'], $slug ); + $class_name = self::replace_slug_in_string( $class, $slug ); + $stylesheet .= self::to_ruleset( + self::append_to_selector( $selector, $class_name ), + array( + array( + 'name' => $property, + 'value' => 'var(' . $css_var . ') !important', + ), + ) + ); + } + } + } + + return $stylesheet; + } + /** * Function that scopes a selector with another one. This works a bit like * SCSS nesting except the `&` operator isn't supported. @@ -758,7 +980,6 @@ private static function scope_selector( $scope, $selector ) { * @param array $settings Settings to process. * @param array $preset_metadata One of the PRESETS_METADATA values. * @param array $origins List of origins to process. - * * @return array Array of presets where each key is a slug and each value is the preset value. */ private static function get_settings_values_by_slug( $settings, $preset_metadata, $origins ) { @@ -799,7 +1020,6 @@ private static function get_settings_values_by_slug( $settings, $preset_metadata * @param array $settings Settings to process. * @param array $preset_metadata One of the PRESETS_METADATA values. * @param array $origins List of origins to process. - * * @return array Array of presets where the key and value are both the slug. */ private static function get_settings_slugs( $settings, $preset_metadata, $origins = self::VALID_ORIGINS ) { @@ -817,47 +1037,7 @@ private static function get_settings_slugs( $settings, $preset_metadata, $origin $result[ $slug ] = $slug; } } - return $result; - } - - /** - * Given a settings array, it returns the generated rulesets - * for the preset classes. - * - * @param array $settings Settings to process. - * @param string $selector Selector wrapping the classes. - * @param array $origins List of origins to process. - * - * @return string The result of processing the presets. - */ - private static function compute_preset_classes( $settings, $selector, $origins ) { - if ( self::ROOT_BLOCK_SELECTOR === $selector ) { - // Classes at the global level do not need any CSS prefixed, - // and we don't want to increase its specificity. - $selector = ''; - } - - $stylesheet = ''; - foreach ( self::PRESETS_METADATA as $preset_metadata ) { - $slugs = self::get_settings_slugs( $settings, $preset_metadata, $origins ); - foreach ( $preset_metadata['classes'] as $class => $property ) { - foreach ( $slugs as $slug ) { - $css_var = self::replace_slug_in_string( $preset_metadata['css_vars'], $slug ); - $class_name = self::replace_slug_in_string( $class, $slug ); - $stylesheet .= self::to_ruleset( - self::append_to_selector( $selector, $class_name ), - array( - array( - 'name' => $property, - 'value' => 'var(' . $css_var . ') !important', - ), - ) - ); - } - } - } - - return $stylesheet; + return $result; } /** @@ -865,7 +1045,6 @@ private static function compute_preset_classes( $settings, $selector, $origins ) * * @param string $input String to replace. * @param string $slug The slug value to use to generate the custom property. - * * @return string The CSS Custom Property. Something along the lines of --wp--preset--color--black. */ private static function replace_slug_in_string( $input, $slug ) { @@ -886,7 +1065,6 @@ private static function replace_slug_in_string( $input, $slug ) { * * @param array $settings Settings to process. * @param array $origins List of origins to process. - * * @return array Returns the modified $declarations. */ private static function compute_preset_vars( $settings, $origins ) { @@ -917,7 +1095,6 @@ private static function compute_preset_vars( $settings, $origins ) { * ``` * * @param array $settings Settings to process. - * * @return array Returns the modified $declarations. */ private static function compute_theme_vars( $settings ) { @@ -935,281 +1112,216 @@ private static function compute_theme_vars( $settings ) { } /** - * Given a selector and a declaration list, - * creates the corresponding ruleset. - * - * To help debugging, will add some space - * if SCRIPT_DEBUG is defined and true. + * Given a tree, it creates a flattened one + * by merging the keys and binding the leaf values + * to the new keys. * - * @param string $selector CSS selector. - * @param array $declarations List of declarations. + * It also transforms camelCase names into kebab-case + * and substitutes '/' by '-'. * - * @return string CSS ruleset. - */ - private static function to_ruleset( $selector, $declarations ) { - if ( empty( $declarations ) ) { - return ''; - } - $ruleset = ''; - - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { - $declaration_block = array_reduce( - $declarations, - function ( $carry, $element ) { - return $carry .= "\t" . $element['name'] . ': ' . $element['value'] . ";\n"; }, - '' - ); - $ruleset .= $selector . " {\n" . $declaration_block . "}\n"; - } else { - $declaration_block = array_reduce( - $declarations, - function ( $carry, $element ) { - return $carry .= $element['name'] . ': ' . $element['value'] . ';'; }, - '' - ); - $ruleset .= $selector . '{' . $declaration_block . '}'; - } - - return $ruleset; - } - - /** - * Converts each styles section into a list of rulesets - * to be appended to the stylesheet. - * These rulesets contain all the css variables (custom variables and preset variables). + * This is thought to be useful to generate + * CSS Custom Properties from a tree, + * although there's nothing in the implementation + * of this function that requires that format. * - * See glossary at https://developer.mozilla.org/en-US/docs/Web/CSS/Syntax + * For example, assuming the given prefix is '--wp' + * and the token is '--', for this input tree: * - * For each section this creates a new ruleset such as: + * { + * 'some/property': 'value', + * 'nestedProperty': { + * 'sub-property': 'value' + * } + * } * - * block-selector { - * --wp--preset--category--slug: value; - * --wp--custom--variable: value; - * } + * it'll return this output: * - * @param array $nodes Nodes with settings. - * @param array $origins List of origins to process. + * { + * '--wp--some-property': 'value', + * '--wp--nested-property--sub-property': 'value' + * } * - * @return string The new stylesheet. + * @param array $tree Input tree to process. + * @param string $prefix Optional. Prefix to prepend to each variable. Default empty string. + * @param string $token Optional. Token to use between levels. Default '--'. + * @return array The flattened tree. */ - private function get_css_variables( $nodes, $origins ) { - $stylesheet = ''; - foreach ( $nodes as $metadata ) { - if ( null === $metadata['selector'] ) { - continue; - } - - $selector = $metadata['selector']; - - $node = _wp_array_get( $this->theme_json, $metadata['path'], array() ); - $declarations = array_merge( self::compute_preset_vars( $node, $origins ), self::compute_theme_vars( $node ) ); + private static function flatten_tree( $tree, $prefix = '', $token = '--' ) { + $result = array(); + foreach ( $tree as $property => $value ) { + $new_key = $prefix . str_replace( + '/', + '-', + strtolower( preg_replace( '/(? 'property_name', + * 'value' => 'property_value, + * ) + * ``` * - * @return string The new stylesheet. + * @param array $styles Styles to process. + * @param array $settings Theme settings. + * @param array $properties Properties metadata. + * @return array Returns the modified $declarations. */ - private function get_block_classes( $style_nodes ) { - $block_rules = ''; - - foreach ( $style_nodes as $metadata ) { - if ( null === $metadata['selector'] ) { - continue; - } + private static function compute_style_properties( $styles, $settings = array(), $properties = self::PROPERTIES_METADATA ) { + $declarations = array(); + if ( empty( $styles ) ) { + return $declarations; + } - $node = _wp_array_get( $this->theme_json, $metadata['path'], array() ); - $selector = $metadata['selector']; - $settings = _wp_array_get( $this->theme_json, array( 'settings' ) ); - $declarations = self::compute_style_properties( $node, $settings ); + foreach ( $properties as $css_property => $value_path ) { + $value = self::get_property_value( $styles, $value_path ); - // 1. Separate the ones who use the general selector - // and the ones who use the duotone selector. - $declarations_duotone = array(); - foreach ( $declarations as $index => $declaration ) { - if ( 'filter' === $declaration['name'] ) { - unset( $declarations[ $index ] ); - $declarations_duotone[] = $declaration; + // Look up protected properties, keyed by value path. + // Skip protected properties that are explicitly set to `null`. + if ( is_array( $value_path ) ) { + $path_string = implode( '.', $value_path ); + if ( + array_key_exists( $path_string, self::PROTECTED_PROPERTIES ) && + _wp_array_get( $settings, self::PROTECTED_PROPERTIES[ $path_string ], null ) === null + ) { + continue; } } - // 2. Generate the rules that use the general selector. - $block_rules .= self::to_ruleset( $selector, $declarations ); - - // 3. Generate the rules that use the duotone selector. - if ( isset( $metadata['duotone'] ) && ! empty( $declarations_duotone ) ) { - $selector_duotone = self::scope_selector( $metadata['selector'], $metadata['duotone'] ); - $block_rules .= self::to_ruleset( $selector_duotone, $declarations_duotone ); + // Skip if empty and not "0" or value represents array of longhand values. + $has_missing_value = empty( $value ) && ! is_numeric( $value ); + if ( $has_missing_value || is_array( $value ) ) { + continue; } - if ( self::ROOT_BLOCK_SELECTOR === $selector ) { - $block_rules .= 'body { margin: 0; }'; - $block_rules .= '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }'; - $block_rules .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }'; - $block_rules .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }'; - - $has_block_gap_support = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'blockGap' ) ) !== null; - if ( $has_block_gap_support ) { - $block_rules .= '.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }'; - $block_rules .= '.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }'; - } - } + $declarations[] = array( + 'name' => $css_property, + 'value' => $value, + ); } - return $block_rules; + return $declarations; } /** - * Creates new rulesets as classes for each preset value such as: - * - * .has-value-color { - * color: value; - * } - * - * .has-value-background-color { - * background-color: value; - * } - * - * .has-value-font-size { - * font-size: value; - * } - * - * .has-value-gradient-background { - * background: value; - * } + * Returns the style property for the given path. * - * p.has-value-gradient-background { - * background: value; - * } - - * @param array $setting_nodes Nodes with settings. - * @param array $origins List of origins to process presets from. + * It also converts CSS Custom Property stored as + * "var:preset|color|secondary" to the form + * "--wp--preset--color--secondary". * - * @return string The new stylesheet. + * @param array $styles Styles subtree. + * @param array $path Which property to process. + * @return string Style property value. */ - private function get_preset_classes( $setting_nodes, $origins ) { - $preset_rules = ''; - - foreach ( $setting_nodes as $metadata ) { - if ( null === $metadata['selector'] ) { - continue; - } + private static function get_property_value( $styles, $path ) { + $value = _wp_array_get( $styles, $path, '' ); - $selector = $metadata['selector']; - $node = _wp_array_get( $this->theme_json, $metadata['path'], array() ); - $preset_rules .= self::compute_preset_classes( $node, $selector, $origins ); + if ( '' === $value || is_array( $value ) ) { + return $value; } - return $preset_rules; - } - - /** - * Returns the existing settings for each block. - * - * Example: - * - * { - * 'root': { - * 'color': { - * 'custom': true - * } - * }, - * 'core/paragraph': { - * 'typography': { - * 'customFontSize': true - * } - * } - * } - * - * @return array Settings per block. - */ - public function get_settings() { - if ( ! isset( $this->theme_json['settings'] ) ) { - return array(); - } else { - return $this->theme_json['settings']; + $prefix = 'var:'; + $prefix_len = strlen( $prefix ); + $token_in = '|'; + $token_out = '--'; + if ( 0 === strncmp( $value, $prefix, $prefix_len ) ) { + $unwrapped_name = str_replace( + $token_in, + $token_out, + substr( $value, $prefix_len ) + ); + $value = "var(--wp--$unwrapped_name)"; } + + return $value; } /** - * Returns the page templates of the current theme. + * Builds metadata for the setting nodes, which returns in the form of: + * + * [ + * [ + * 'path' => ['path', 'to', 'some', 'node' ], + * 'selector' => 'CSS selector for some node' + * ], + * [ + * 'path' => [ 'path', 'to', 'other', 'node' ], + * 'selector' => 'CSS selector for other node' + * ], + * ] * + * @param array $theme_json The tree to extract setting nodes from. + * @param array $selectors List of selectors per block. * @return array */ - public function get_custom_templates() { - $custom_templates = array(); - if ( ! isset( $this->theme_json['customTemplates'] ) ) { - return $custom_templates; + private static function get_setting_nodes( $theme_json, $selectors = array() ) { + $nodes = array(); + if ( ! isset( $theme_json['settings'] ) ) { + return $nodes; } - foreach ( $this->theme_json['customTemplates'] as $item ) { - if ( isset( $item['name'] ) ) { - $custom_templates[ $item['name'] ] = array( - 'title' => isset( $item['title'] ) ? $item['title'] : '', - 'postTypes' => isset( $item['postTypes'] ) ? $item['postTypes'] : array( 'page' ), - ); - } - } - return $custom_templates; - } + // Top-level. + $nodes[] = array( + 'path' => array( 'settings' ), + 'selector' => self::ROOT_BLOCK_SELECTOR, + ); - /** - * Returns the template part data of current theme. - * - * @return array - */ - public function get_template_parts() { - $template_parts = array(); - if ( ! isset( $this->theme_json['templateParts'] ) ) { - return $template_parts; + // Calculate paths for blocks. + if ( ! isset( $theme_json['settings']['blocks'] ) ) { + return $nodes; } - foreach ( $this->theme_json['templateParts'] as $item ) { - if ( isset( $item['name'] ) ) { - $template_parts[ $item['name'] ] = array( - 'title' => isset( $item['title'] ) ? $item['title'] : '', - 'area' => isset( $item['area'] ) ? $item['area'] : '', - ); + foreach ( $theme_json['settings']['blocks'] as $name => $node ) { + $selector = null; + if ( isset( $selectors[ $name ]['selector'] ) ) { + $selector = $selectors[ $name ]['selector']; } + + $nodes[] = array( + 'path' => array( 'settings', 'blocks', $name ), + 'selector' => $selector, + ); } - return $template_parts; + + return $nodes; } /** * Builds metadata for the style nodes, which returns in the form of: * - * [ - * [ - * 'path' => [ 'path', 'to', 'some', 'node' ], - * 'selector' => 'CSS selector for some node', - * 'duotone' => 'CSS selector for duotone for some node' - * ], - * [ - * 'path' => ['path', 'to', 'other', 'node' ], - * 'selector' => 'CSS selector for other node', - * 'duotone' => null - * ], - * ] + * [ + * [ + * 'path' => [ 'path', 'to', 'some', 'node' ], + * 'selector' => 'CSS selector for some node', + * 'duotone' => 'CSS selector for duotone for some node' + * ], + * [ + * 'path' => ['path', 'to', 'other', 'node' ], + * 'selector' => 'CSS selector for other node', + * 'duotone' => null + * ], + * ] * * @param array $theme_json The tree to extract style nodes from. - * @param array $selectors List of selectors per block. - * + * @param array $selectors List of selectors per block. * @return array */ private static function get_style_nodes( $theme_json, $selectors = array() ) { @@ -1268,91 +1380,6 @@ private static function get_style_nodes( $theme_json, $selectors = array() ) { return $nodes; } - /** - * Builds metadata for the setting nodes, which returns in the form of: - * - * [ - * [ - * 'path' => ['path', 'to', 'some', 'node' ], - * 'selector' => 'CSS selector for some node' - * ], - * [ - * 'path' => [ 'path', 'to', 'other', 'node' ], - * 'selector' => 'CSS selector for other node' - * ], - * ] - * - * @param array $theme_json The tree to extract setting nodes from. - * @param array $selectors List of selectors per block. - * - * @return array - */ - private static function get_setting_nodes( $theme_json, $selectors = array() ) { - $nodes = array(); - if ( ! isset( $theme_json['settings'] ) ) { - return $nodes; - } - - // Top-level. - $nodes[] = array( - 'path' => array( 'settings' ), - 'selector' => self::ROOT_BLOCK_SELECTOR, - ); - - // Calculate paths for blocks. - if ( ! isset( $theme_json['settings']['blocks'] ) ) { - return $nodes; - } - - foreach ( $theme_json['settings']['blocks'] as $name => $node ) { - $selector = null; - if ( isset( $selectors[ $name ]['selector'] ) ) { - $selector = $selectors[ $name ]['selector']; - } - - $nodes[] = array( - 'path' => array( 'settings', 'blocks', $name ), - 'selector' => $selector, - ); - } - - return $nodes; - } - - /** - * Returns the stylesheet that results of processing - * the theme.json structure this object represents. - * - * @param array $types Types of styles to load. Will load all by default. It accepts: - * 'variables': only the CSS Custom Properties for presets & custom ones. - * 'styles': only the styles section in theme.json. - * 'presets': only the classes for the presets. - * @param array $origins A list of origins to include. By default it includes 'default', 'theme', and 'custom'. - * - * @return string Stylesheet. - */ - public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' ), $origins = self::VALID_ORIGINS ) { - $blocks_metadata = self::get_blocks_metadata(); - $style_nodes = self::get_style_nodes( $this->theme_json, $blocks_metadata ); - $setting_nodes = self::get_setting_nodes( $this->theme_json, $blocks_metadata ); - - $stylesheet = ''; - - if ( in_array( 'variables', $types, true ) ) { - $stylesheet .= $this->get_css_variables( $setting_nodes, $origins ); - } - - if ( in_array( 'styles', $types, true ) ) { - $stylesheet .= $this->get_block_classes( $style_nodes ); - } - - if ( in_array( 'presets', $types, true ) ) { - $stylesheet .= $this->get_preset_classes( $setting_nodes, $origins ); - } - - return $stylesheet; - } - /** * Merge new incoming data. * @@ -1420,7 +1447,6 @@ public function merge( $incoming ) { } } } - } /** @@ -1493,12 +1519,68 @@ private static function filter_slugs( $node, $path, $slugs ) { return $new_node; } + /** + * Removes insecure data from theme.json. + * + * @param array $theme_json Structure to sanitize. + * @return array Sanitized structure. + */ + public static function remove_insecure_properties( $theme_json ) { + $sanitized = array(); + + $theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json ); + + $valid_block_names = array_keys( self::get_blocks_metadata() ); + $valid_element_names = array_keys( self::ELEMENTS ); + $theme_json = self::sanitize( $theme_json, $valid_block_names, $valid_element_names ); + + $blocks_metadata = self::get_blocks_metadata(); + $style_nodes = self::get_style_nodes( $theme_json, $blocks_metadata ); + foreach ( $style_nodes as $metadata ) { + $input = _wp_array_get( $theme_json, $metadata['path'], array() ); + if ( empty( $input ) ) { + continue; + } + + $output = self::remove_insecure_styles( $input ); + if ( ! empty( $output ) ) { + _wp_array_set( $sanitized, $metadata['path'], $output ); + } + } + + $setting_nodes = self::get_setting_nodes( $theme_json ); + foreach ( $setting_nodes as $metadata ) { + $input = _wp_array_get( $theme_json, $metadata['path'], array() ); + if ( empty( $input ) ) { + continue; + } + + $output = self::remove_insecure_settings( $input ); + if ( ! empty( $output ) ) { + _wp_array_set( $sanitized, $metadata['path'], $output ); + } + } + + if ( empty( $sanitized['styles'] ) ) { + unset( $theme_json['styles'] ); + } else { + $theme_json['styles'] = $sanitized['styles']; + } + + if ( empty( $sanitized['settings'] ) ) { + unset( $theme_json['settings'] ); + } else { + $theme_json['settings'] = $sanitized['settings']; + } + + return $theme_json; + } + /** * Processes a setting node and returns the same node * without the insecure settings. * * @param array $input Node to process. - * * @return array */ private static function remove_insecure_settings( $input ) { @@ -1554,7 +1636,6 @@ private static function remove_insecure_settings( $input ) { * without the insecure styles. * * @param array $input Node to process. - * * @return array */ private static function remove_insecure_styles( $input ) { @@ -1589,64 +1670,6 @@ private static function is_safe_css_declaration( $property_name, $property_value return ! empty( trim( $filtered ) ); } - /** - * Removes insecure data from theme.json. - * - * @param array $theme_json Structure to sanitize. - * - * @return array Sanitized structure. - */ - public static function remove_insecure_properties( $theme_json ) { - $sanitized = array(); - - $theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json ); - - $valid_block_names = array_keys( self::get_blocks_metadata() ); - $valid_element_names = array_keys( self::ELEMENTS ); - $theme_json = self::sanitize( $theme_json, $valid_block_names, $valid_element_names ); - - $blocks_metadata = self::get_blocks_metadata(); - $style_nodes = self::get_style_nodes( $theme_json, $blocks_metadata ); - foreach ( $style_nodes as $metadata ) { - $input = _wp_array_get( $theme_json, $metadata['path'], array() ); - if ( empty( $input ) ) { - continue; - } - - $output = self::remove_insecure_styles( $input ); - if ( ! empty( $output ) ) { - _wp_array_set( $sanitized, $metadata['path'], $output ); - } - } - - $setting_nodes = self::get_setting_nodes( $theme_json ); - foreach ( $setting_nodes as $metadata ) { - $input = _wp_array_get( $theme_json, $metadata['path'], array() ); - if ( empty( $input ) ) { - continue; - } - - $output = self::remove_insecure_settings( $input ); - if ( ! empty( $output ) ) { - _wp_array_set( $sanitized, $metadata['path'], $output ); - } - } - - if ( empty( $sanitized['styles'] ) ) { - unset( $theme_json['styles'] ); - } else { - $theme_json['styles'] = $sanitized['styles']; - } - - if ( empty( $sanitized['settings'] ) ) { - unset( $theme_json['settings'] ); - } else { - $theme_json['settings'] = $sanitized['settings']; - } - - return $theme_json; - } - /** * Returns the raw data. * @@ -1657,12 +1680,10 @@ public function get_raw_data() { } /** - * * Transforms the given editor settings according the * add_theme_support format to the theme.json format. * * @param array $settings Existing editor settings. - * * @return array Config that adheres to the theme.json schema. */ public static function get_from_editor_settings( $settings ) {