Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add ability to opt out of Core color palette #36447

Closed
2 changes: 2 additions & 0 deletions docs/how-to-guides/themes/theme-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ The settings section has the following structure:
"custom": true,
"customDuotone": true,
"customGradient": true,
"corePalette": true,
"coreGradients": true,
"duotone": [],
"gradients": [],
"link": false,
Expand Down
44 changes: 44 additions & 0 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class WP_Theme_JSON_Gutenberg {
'custom' => null,
'customDuotone' => null,
'customGradient' => null,
'corePalette' => null,
'coreGradients' => null,
'duotone' => null,
'gradients' => null,
'link' => null,
Expand Down Expand Up @@ -1320,6 +1322,48 @@ public function merge( $incoming ) {
}
}

$this->maybe_remove_core_color_palette_and_gradients();
}

/**
* Removes the Core Palette and Gradients if the Theme
* opts out of them using the `corePalette` and `coreGradients` value
*
* @return void
*/
public function maybe_remove_core_color_palette_and_gradients() {

if ( isset( $this->theme_json['settings']['color']['corePalette'] ) ) {
if ( false === $this->theme_json['settings']['color']['corePalette'] ) {
$this->theme_json['settings']['color']['palette']['core'] = array();
}
}

if ( isset( $this->theme_json['settings']['blocks'] ) && is_array( $this->theme_json['settings']['blocks'] ) ) {
foreach ( $this->theme_json['settings']['blocks'] as &$block ) {
if ( isset( $block['color']['corePalette'] ) ) {
if ( false === $block['color']['corePalette'] ) {
$block['color']['palette']['core'] = array();
}
}
}
}

if ( isset( $this->theme_json['settings']['color']['coreGradients'] ) ) {
if ( false === $this->theme_json['settings']['color']['coreGradients'] ) {
$this->theme_json['settings']['color']['gradients']['core'] = array();
}
}

if ( isset( $this->theme_json['settings']['blocks'] ) && is_array( $this->theme_json['settings']['blocks'] ) ) {
foreach ( $this->theme_json['settings']['blocks'] as &$block ) {
if ( isset( $block['color']['coreGradients'] ) ) {
if ( false === $block['color']['coreGradients'] ) {
$block['color']['gradients']['core'] = array();
}
}
}
}
}

/**
Expand Down
257 changes: 257 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,263 @@ public function test_merge_incoming_data_null_presets() {
$this->assertEqualSetsWithIndex( $expected, $actual );
}

public function test_maybe_remove_core_palette_and_gradients() {
$theme_json = new WP_Theme_JSON_Gutenberg();

$core_theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'palette' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
'gradients' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
),
),
),
'core'
);

$theme_theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'corePalette' => false,
'coreGradients' => false,
'palette' => array(
array(
'slug' => 'green',
'color' => 'green',
),
),
),
),
)
);

$theme_json->merge( $core_theme_json );
$theme_json->merge( $theme_theme_json );

$actual = $theme_json->get_raw_data();
$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'corePalette' => false,
'coreGradients' => false,
'palette' => array(
'core' => array(),
'theme' => array(
array(
'slug' => 'green',
'color' => 'green',
),
),
),
'gradients' => array(
'core' => array(),
),
),
),
);

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

public function test_maybe_remove_core_palette_and_gradients_inverse() {
$theme_json = new WP_Theme_JSON_Gutenberg();

$core_theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'palette' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
'gradients' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
),
),
),
'core'
);

$theme_theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'corePalette' => true,
'coreGradients' => true,
'palette' => array(
array(
'slug' => 'green',
'color' => 'green',
),
),
),
),
)
);

$theme_json->merge( $core_theme_json );
$theme_json->merge( $theme_theme_json );

$actual = $theme_json->get_raw_data();
$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'corePalette' => true,
'coreGradients' => true,
'palette' => array(
'core' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
'theme' => array(
array(
'slug' => 'green',
'color' => 'green',
),
),
),
'gradients' => array(
'core' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
),
),
),
);

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

public function test_maybe_remove_core_palette_and_gradients_blocks() {
$theme_json = new WP_Theme_JSON_Gutenberg();

$core_theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'palette' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
'gradients' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
),
),
),
'core'
);

$theme_theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'palette' => array(
array(
'slug' => 'green',
'color' => 'green',
),
),
),
'blocks' => array(
'core/heading' => array(
'color' => array(
'corePalette' => false,
'coreGradients' => false,
),
),
),
),
)
);

$theme_json->merge( $core_theme_json );
$theme_json->merge( $theme_theme_json );

$actual = $theme_json->get_raw_data();
$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'palette' => array(
'core' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
'theme' => array(
array(
'slug' => 'green',
'color' => 'green',
),
),
),
'gradients' => array(
'core' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
),
),
'blocks' => array(
'core/heading' => array(
'color' => array(
'corePalette' => false,
'coreGradients' => false,
'palette' => array(
'core' => array(),
),
'gradients' => array(
'core' => array(),
),
),
),
),
),
);

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

function test_remove_insecure_properties_removes_unsafe_styles() {
$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties(
array(
Expand Down
10 changes: 10 additions & 0 deletions schemas/json/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
"type": "boolean",
"default": true
},
"corePalette": {
"description": "Allow users to choose colors from the Core palette. \nGutenberg plugin required.",
"type": "boolean",
"default": true
},
"coreGradients": {
"description": "Allow users to choose colors from the Core gradients. \nGutenberg plugin required.",
"type": "boolean",
"default": true
},
"custom": {
"description": "Allow users to select custom colors.\nSince 5.8.",
"type": "boolean",
Expand Down