From 7b4d297abbffc172baf9873576fdf98dbdd869fb Mon Sep 17 00:00:00 2001 From: Jorge Date: Tue, 25 May 2021 22:25:08 +0100 Subject: [PATCH] Fix: Allow themes to disable the color and gradients palette. --- lib/class-wp-theme-json-gutenberg.php | 37 ++++++++++++++++++++++++++- lib/global-styles.php | 16 ++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 5a7765ef62bedb..00f3ced24c5cb0 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -18,6 +18,13 @@ class WP_Theme_JSON_Gutenberg { */ private $theme_json = null; + /** + * Container of append paths that were merged with a non null value before. + * + * @var array + */ + private $merged_append_paths_with_data = array(); + /** * Holds block metadata extracted from block.json * to be shared among all instances so we don't @@ -1119,13 +1126,41 @@ public function merge( $incoming, $update_or_remove = 'remove' ) { } foreach ( $to_append as $path_to_append ) { $path = array_merge( $metadata['path'], $path_to_append ); - $incoming_node = _wp_array_get( $incoming_data, $path, array() ); + $incoming_node = _wp_array_get( $incoming_data, $path, null ); $existing_node = _wp_array_get( $existing_data, $path, array() ); if ( empty( $incoming_node ) && empty( $existing_node ) ) { continue; } + // If we are merging with something that has no values + // core origin should be treated as if the values were present + // and we can remove the origin. + if ( null === $incoming_node ) { + if ( _wp_array_get( $this->merged_append_paths_with_data, $path, false ) ) { + continue; + } + $all_nodes_are_core_origin = true; + foreach ( $existing_node as $value ) { + if ( ! ( isset( $value['origin'] ) && 'core' === $value['origin'] ) ) { + $all_nodes_are_core_origin = false; + break; + } + } + if ( $all_nodes_are_core_origin ) { + $merged = array(); + foreach ( $existing_node as $value ) { + unset( $value['origin'] ); + $merged[] = $value; + } + gutenberg_experimental_set( $this->theme_json, $path, $merged ); + } + continue; + } + if ( ! empty( $existing_node ) ) { + gutenberg_experimental_set( $this->merged_append_paths_with_data, $path, true ); + } + $index_table = array(); $existing_slugs = array(); $merged = array(); diff --git a/lib/global-styles.php b/lib/global-styles.php index a5243bbdc5cf48..b9aafd1dbca67f 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -177,6 +177,22 @@ function_exists( 'gutenberg_is_edit_site_page' ) && unset( $settings['__experimentalFeatures']['spacing']['customPadding'] ); } + // On the site-editor for now we still need the core origin colors. + if ( 'site-editor' !== $context ) { + $settings_with_core_origin = array( 'colors', 'gradients' ); + foreach ( $settings_with_core_origin as $setting_key ) { + if ( isset( $settings[ $setting_key ] ) ) { + $new_setting = array(); + foreach ( $settings[ $setting_key ] as $node ) { + if ( ! ( isset( $node['origin'] ) && 'core' === $node['origin'] ) ) { + $new_setting[] = $node; + } + } + $settings[ $setting_key ] = $new_setting; + } + } + } + return $settings; }