Skip to content

Commit

Permalink
Clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Apr 6, 2021
1 parent 37d2e0c commit 862c86c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 201 deletions.
2 changes: 1 addition & 1 deletion lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static function get_core_data() {
return self::$core;
}

$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
$all_blocks = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;
$config = self::read_json_file( __DIR__ . '/experimental-default-theme.json' );
$config = self::translate( $config );

Expand Down
34 changes: 24 additions & 10 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 @@ -152,14 +166,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 +185,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 +237,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
2 changes: 1 addition & 1 deletion lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @return array Config that adheres to the theme.json schema.
*/
function gutenberg_experimental_global_styles_get_theme_support_settings( $settings ) {
$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
$all_blocks = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;
$theme_settings = array();
$theme_settings['settings'] = array();
$theme_settings['settings'][ $all_blocks ] = array();
Expand Down
12 changes: 6 additions & 6 deletions phpunit/class-wp-theme-json-legacy-settings-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function get_editor_settings_no_theme_support() {
}

function test_legacy_settings_blank() {
$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
$all_blocks = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;
$input = array();
$expected = array(
'settings' => array(
Expand All @@ -55,7 +55,7 @@ function test_legacy_settings_blank() {
}

function test_legacy_settings_no_theme_support() {
$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
$all_blocks = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;
$input = $this->get_editor_settings_no_theme_support();
$expected = array(
'settings' => array(
Expand All @@ -81,7 +81,7 @@ function test_legacy_settings_no_theme_support() {
}

function test_legacy_settings_custom_units_can_be_disabled() {
$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
$all_blocks = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;
add_theme_support( 'custom-units', array() );
$input = gutenberg_get_common_block_editor_settings();

Expand All @@ -96,7 +96,7 @@ function test_legacy_settings_custom_units_can_be_disabled() {
}

function test_legacy_settings_custom_units_can_be_enabled() {
$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
$all_blocks = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;
add_theme_support( 'custom-units' );
$input = gutenberg_get_common_block_editor_settings();

Expand All @@ -111,7 +111,7 @@ function test_legacy_settings_custom_units_can_be_enabled() {
}

function test_legacy_settings_custom_units_can_be_filtered() {
$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
$all_blocks = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;
add_theme_support( 'custom-units', 'rem', 'em' );
$input = gutenberg_get_common_block_editor_settings();

Expand All @@ -126,7 +126,7 @@ function test_legacy_settings_custom_units_can_be_filtered() {
}

function test_legacy_settings_filled() {
$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
$all_blocks = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME;
$input = array(
'disableCustomColors' => true,
'disableCustomGradients' => true,
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 862c86c

Please sign in to comment.