Skip to content

Commit

Permalink
Clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Apr 8, 2021
1 parent 373f9f4 commit f450ed5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 196 deletions.
45 changes: 32 additions & 13 deletions lib/class-wp-theme-json-schema-v0.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
*/
class WP_Theme_JSON_Schema_V0 {

/**
* How to address all the blocks
* in the theme.json file.
*/
const ALL_BLOCKS_NAME = 'defaults';

/**
* How to address the root block
* in the theme.json file.
*
* @var string
*/
const ROOT_BLOCK_NAME = 'root';

/**
* Data schema of each block within a theme.json.
*
Expand Down Expand Up @@ -132,9 +146,14 @@ public static function to_v1( $old, $version ) {
);

// Overwrite the things that change.
$new['settings'] = self::process_settings( $old['settings'], $blocks_to_consolidate );
$new['styles'] = self::process_styles( $old['styles'], $blocks_to_consolidate );
$new['version'] = $version;
if ( isset( $old['settings' ] ) ) {
$new['settings'] = self::process_settings( $old['settings'], $blocks_to_consolidate );
}
if ( isset( $old['styles'] ) ) {
$new['styles'] = self::process_styles( $old['styles'], $blocks_to_consolidate );
}

$new['version'] = $version;

return $new;
}
Expand All @@ -152,14 +171,14 @@ private static function process_settings( $settings, $blocks_to_consolidate ) {
);

// 'defaults' settings become top-level.
if ( isset( $settings[ WP_Theme_JSON::ALL_BLOCKS_NAME] ) ) {
$new = $settings[ WP_Theme_JSON::ALL_BLOCKS_NAME ];
unset( $settings[ WP_Theme_JSON::ALL_BLOCKS_NAME ] );
if ( isset( $settings[ self::ALL_BLOCKS_NAME] ) ) {
$new = $settings[ self::ALL_BLOCKS_NAME ];
unset( $settings[ self::ALL_BLOCKS_NAME ] );
}

// 'root' settings override 'defaults'.
if ( isset( $settings[ WP_Theme_JSON::ROOT_BLOCK_NAME ] ) ) {
$new = array_replace_recursive( $new, $settings[ WP_Theme_JSON::ROOT_BLOCK_NAME ] );
if ( isset( $settings[ self::ROOT_BLOCK_NAME ] ) ) {
$new = array_replace_recursive( $new, $settings[ self::ROOT_BLOCK_NAME ] );

// The array_replace_recursive algorithm merges at the leaf level.
// This means that when a leaf value is an array,
Expand All @@ -171,15 +190,15 @@ private static function process_settings( $settings, $blocks_to_consolidate ) {
foreach( $paths_to_override as $path ) {
$root_value = _wp_array_get(
$settings,
array_merge( array( WP_Theme_JSON::ROOT_BLOCK_NAME), $path ),
array_merge( array( self::ROOT_BLOCK_NAME), $path ),
null
);
if ( null !== $root_value ) {
gutenberg_experimental_set( $new, $path, $root_value );
}
}

unset( $settings[ WP_Theme_JSON::ROOT_BLOCK_NAME ] );
unset( $settings[ self::ROOT_BLOCK_NAME ] );
}

if ( empty( $settings ) ) {
Expand Down Expand Up @@ -223,9 +242,9 @@ private static function process_styles( $styles, $blocks_to_consolidate ) {
$new = array();

// Styles within root become top-level.
if ( isset( $styles[ WP_Theme_JSON::ROOT_BLOCK_NAME ] ) ) {
$new = $styles[ WP_Theme_JSON::ROOT_BLOCK_NAME ];
unset( $styles[ WP_Theme_JSON::ROOT_BLOCK_NAME ] );
if ( isset( $styles[ self::ROOT_BLOCK_NAME ] ) ) {
$new = $styles[ self::ROOT_BLOCK_NAME ];
unset( $styles[ self::ROOT_BLOCK_NAME ] );

// Transform root.styles.color.link into elements.link.color.text.
if ( isset( $new['color']['link'] ) ) {
Expand Down
39 changes: 5 additions & 34 deletions lib/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,12 @@ class WP_Theme_JSON {
*/
private static $blocks_metadata = null;

/**
* How to address all the blocks
* in the theme.json file.
*/
const ALL_BLOCKS_NAME = 'defaults';

/**
* The CSS selector for the * block,
* only using to generate presets.
*
* @var string
*/
const ALL_BLOCKS_SELECTOR = ':root';

/**
* How to address the root block
* in the theme.json file.
*
* @var string
*/
const ROOT_BLOCK_NAME = 'root';

/**
* The CSS selector for the root block.
*
* @var string
*/
const ROOT_BLOCK_SELECTOR = ':root';
const ROOT_SELECTOR = ':root';

const VALID_TOP_LEVEL_KEYS = array(
'version',
Expand Down Expand Up @@ -383,14 +361,7 @@ private static function get_blocks_metadata() {
return self::$blocks_metadata;
}

self::$blocks_metadata = array(
self::ROOT_BLOCK_NAME => array(
'selector' => self::ROOT_BLOCK_SELECTOR,
),
self::ALL_BLOCKS_NAME => array(
'selector' => self::ALL_BLOCKS_SELECTOR,
),
);
self::$blocks_metadata = array();

$registry = WP_Block_Type_Registry::get_instance();
$blocks = $registry->get_all_registered();
Expand Down Expand Up @@ -611,7 +582,7 @@ private static function compute_style_properties( $input, $output ) {
private static function get_presets_of_node( $node, $selector ) {
$output = '';

if ( WP_Theme_JSON::ROOT_BLOCK_SELECTOR === $selector ) {
if ( self::ROOT_SELECTOR === $selector ) {
// Classes at the global level do not need any CSS prefixed,
// and we don't want to increase its specificity.
$selector = '';
Expand Down Expand Up @@ -905,7 +876,7 @@ public function get_stylesheet( $type = 'all' ) {
if ( isset( $input['settings'] ) ) {
$settings_paths[] = array(
'path' => array('settings'),
'selector' => self::ROOT_BLOCK_SELECTOR,
'selector' => self::ROOT_SELECTOR,
);
}
if ( isset( $input['settings']['blocks'] ) ) {
Expand All @@ -925,7 +896,7 @@ public function get_stylesheet( $type = 'all' ) {
if ( isset( $input['styles'] ) ) {
$styles_paths[] = array(
'path' => array('styles'),
'selector' => self::ROOT_BLOCK_SELECTOR,
'selector' => self::ROOT_SELECTOR,
'elements' => self::ELEMENTS,
);
}
Expand Down
146 changes: 1 addition & 145 deletions phpunit/class-wp-theme-json-schema-v0-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,152 +8,8 @@

class WP_Theme_JSON_Schema_V0_Test extends WP_UnitTestCase {

function test_schema_sanitization_subtree_is_removed_if_key_invalid() {
$actual = WP_Theme_JSON_Schema_V0::sanitize(
array(
'invalid/key' => 'content',
'styles' => array(
'invalid/key' => array(
'color' => array(
'custom' => 'false',
),
),
'core/group' => array(
'invalid/key' => array(
'custom' => false,
'background' => 'red',
),
'color' => array(
'invalid/key' => true,
'background' => 'red',
),
'spacing' => array(
'padding' => array(
'invalid/key' => false,
'top' => '10px',
),
),
),
),
),
array(
'core/group' => array()
)
);

$expected = array(
'styles' => array(
'core/group' => array(
'color' => array(
'background' => 'red',
),
'spacing' => array(
'padding' => array(
'top' => '10px',
),
),
),
),
);

$this->assertEqualSetsWithIndex( $expected, $actual );
}

function test_schema_sanitization_subtree_is_removed_if_not_array() {
$root_name = WP_Theme_JSON::ROOT_BLOCK_NAME;
$actual = WP_Theme_JSON_Schema_V0::sanitize(
array(
'settings' => 'invalid/not/array',
'styles' => array(
$root_name => 'invalid/not/array',
'core/paragraph' => array(
'invalid/not/array' => false,
),
'core/group' => array(
'invalid/not/array' => false,
'color' => array(
'link' => 'pink',
),
'typography' => array(
'invalid/key' => false,
),
'spacing' => array(
'padding' => array(
'invalid/key' => '10px',
),
),
),
),
),
array(
'core/paragraph' => array(),
'core/group' => array(),
)
);

$expected = array(
'styles' => array(
'core/group' => array(
'color' => array(
'link' => 'pink',
),
),
),
);

$this->assertEqualSetsWithIndex( $expected, $actual );
}

function test_schema_sanitization_subtree_is_removed_if_empty() {
$root_name = WP_Theme_JSON::ROOT_BLOCK_NAME;
$actual = WP_Theme_JSON_Schema_V0::sanitize(
array(
'settings' => array(
'invalid/key' => array(
'color' => array(
'custom' => false,
),
),
$root_name => array(
'invalid/key' => false,
),
),
'styles' => array(
$root_name => array(
'color' => array(
'link' => 'blue',
),
'typography' => array(
'invalid/key' => false,
),
'spacing' => array(
'padding' => array(
'invalid/key' => '10px',
),
),
),
),
),
array(
$root_name => array(),
)
);

$expected = array(
'styles' => array(
$root_name => array(
'color' => array(
'link' => 'blue',
),
),
),
);

$this->assertEqualSetsWithIndex( $expected, $actual );
}

function test_schema_to_v1() {
$root_name = WP_Theme_JSON::ROOT_BLOCK_NAME;
$root_name = WP_Theme_JSON_Schema_V0::ROOT_BLOCK_NAME;
$theme_json = new WP_Theme_JSON(
array(
'invalid/key' => 'content',
Expand Down
8 changes: 4 additions & 4 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
class WP_Theme_JSON_Test extends WP_UnitTestCase {

function test_get_settings_v0() {
$defaults = WP_Theme_JSON::ALL_BLOCKS_NAME;
$root = WP_Theme_JSON::ROOT_BLOCK_NAME;
$defaults = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;
$root = WP_Theme_JSON_Schema_V0::ROOT_BLOCK_NAME;
$theme_json = new WP_Theme_JSON(
array(
'settings' => array(
Expand Down Expand Up @@ -231,8 +231,8 @@ function test_get_settings() {
}

function test_get_stylesheet_v0() {
$root_name = WP_Theme_JSON::ROOT_BLOCK_NAME;
$all_blocks_name = WP_Theme_JSON::ALL_BLOCKS_NAME;
$root_name = WP_Theme_JSON_Schema_V0::ROOT_BLOCK_NAME;
$all_blocks_name = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;

$theme_json = new WP_Theme_JSON(
array(
Expand Down

0 comments on commit f450ed5

Please sign in to comment.