From 00f745a29b4eb72fbb87077c1378056b485db758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Tue, 24 Nov 2020 12:12:55 +0100 Subject: [PATCH 1/6] Extract WP_Theme_JSON_Resolver class --- lib/class-wp-theme-json-resolver.php | 380 +++++++++++++++++++++++++++ lib/global-styles.php | 292 +------------------- lib/load.php | 1 + 3 files changed, 388 insertions(+), 285 deletions(-) create mode 100644 lib/class-wp-theme-json-resolver.php diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php new file mode 100644 index 00000000000000..a453ce221eae9b --- /dev/null +++ b/lib/class-wp-theme-json-resolver.php @@ -0,0 +1,380 @@ + __( 'Black', 'gutenberg' ), + 'cyan-bluish-gray' => __( 'Cyan bluish gray', 'gutenberg' ), + 'white' => __( 'White', 'gutenberg' ), + 'pale-pink' => __( 'Pale pink', 'gutenberg' ), + 'vivid-red' => __( 'Vivid red', 'gutenberg' ), + 'luminous-vivid-orange' => __( 'Luminous vivid orange', 'gutenberg' ), + 'luminous-vivid-amber' => __( 'Luminous vivid amber', 'gutenberg' ), + 'light-green-cyan' => __( 'Light green cyan', 'gutenberg' ), + 'vivid-green-cyan' => __( 'Vivid green cyan', 'gutenberg' ), + 'pale-cyan-blue' => __( 'Pale cyan blue', 'gutenberg' ), + 'vivid-cyan-blue' => __( 'Vivid cyan blue', 'gutenberg' ), + 'vivid-purple' => __( 'Vivid purple', 'gutenberg' ), + ); + if ( ! empty( $config['global']['settings']['color']['palette'] ) ) { + foreach ( $config['global']['settings']['color']['palette'] as &$color ) { + $color['name'] = $default_colors_i18n[ $color['slug'] ]; + } + } + + $default_gradients_i18n = array( + 'vivid-cyan-blue-to-vivid-purple' => __( 'Vivid cyan blue to vivid purple', 'gutenberg' ), + 'light-green-cyan-to-vivid-green-cyan' => __( 'Light green cyan to vivid green cyan', 'gutenberg' ), + 'luminous-vivid-amber-to-luminous-vivid-orange' => __( 'Luminous vivid amber to luminous vivid orange', 'gutenberg' ), + 'luminous-vivid-orange-to-vivid-red' => __( 'Luminous vivid orange to vivid red', 'gutenberg' ), + 'very-light-gray-to-cyan-bluish-gray' => __( 'Very light gray to cyan bluish gray', 'gutenberg' ), + 'cool-to-warm-spectrum' => __( 'Cool to warm spectrum', 'gutenberg' ), + 'blush-light-purple' => __( 'Blush light purple', 'gutenberg' ), + 'blush-bordeaux' => __( 'Blush bordeaux', 'gutenberg' ), + 'luminous-dusk' => __( 'Luminous dusk', 'gutenberg' ), + 'pale-ocean' => __( 'Pale ocean', 'gutenberg' ), + 'electric-grass' => __( 'Electric grass', 'gutenberg' ), + 'midnight' => __( 'Midnight', 'gutenberg' ), + ); + if ( ! empty( $config['global']['settings']['color']['gradients'] ) ) { + foreach ( $config['global']['settings']['color']['gradients'] as &$gradient ) { + $gradient['name'] = $default_gradients_i18n[ $gradient['slug'] ]; + } + } + + $default_font_sizes_i18n = array( + 'small' => __( 'Small', 'gutenberg' ), + 'normal' => __( 'Normal', 'gutenberg' ), + 'medium' => __( 'Medium', 'gutenberg' ), + 'large' => __( 'Large', 'gutenberg' ), + 'huge' => __( 'Huge', 'gutenberg' ), + ); + if ( ! empty( $config['global']['settings']['typography']['fontSizes'] ) ) { + foreach ( $config['global']['settings']['typography']['fontSizes'] as &$font_size ) { + $font_size['name'] = $default_font_sizes_i18n[ $font_size['slug'] ]; + } + } + + $default_font_styles_i18n = array( + 'normal' => __( 'Regular', 'gutenberg' ), + 'italic' => __( 'Italic', 'gutenberg' ), + 'initial' => __( 'Initial', 'gutenberg' ), + 'inherit' => __( 'Inherit', 'gutenberg' ), + ); + if ( ! empty( $config['global']['settings']['typography']['fontStyles'] ) ) { + foreach ( $config['global']['settings']['typography']['fontStyles'] as &$font_style ) { + $font_style['name'] = $default_font_styles_i18n[ $font_style['slug'] ]; + } + } + + $default_font_weights_i18n = array( + '100' => __( 'Ultralight', 'gutenberg' ), + '200' => __( 'Thin', 'gutenberg' ), + '300' => __( 'Light', 'gutenberg' ), + '400' => __( 'Regular', 'gutenberg' ), + '500' => __( 'Medium', 'gutenberg' ), + '600' => __( 'Semibold', 'gutenberg' ), + '700' => __( 'Bold', 'gutenberg' ), + '800' => __( 'Heavy', 'gutenberg' ), + '900' => __( 'Black', 'gutenberg' ), + 'initial' => __( 'Initial', 'gutenberg' ), + 'inherit' => __( 'Inherit', 'gutenberg' ), + ); + if ( ! empty( $config['global']['settings']['typography']['fontWeights'] ) ) { + foreach ( $config['global']['settings']['typography']['fontWeights'] as &$font_weight ) { + $font_weight['name'] = $default_font_weights_i18n[ $font_weight['slug'] ]; + } + } + // End i18n logic to remove when JSON i18 strings are extracted. + + self::$core = new WP_Theme_JSON( $config ); + + return self::$core; + } + + /** + * Returns the theme's origin config. + * + * It also fetches the existing presets the theme + * may have declared via add_theme_support and + * uses them if the theme hasn't declared any via theme.json. + * + * @param array $settings Existing editor settings. + * + * @return WP_Theme_JSON Entity that holds theme data. + */ + private function get_theme_origin( $settings ) { + $theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); + $theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) ); + + /* + * We want the presets and settings declared in theme.json + * to override the ones declared via add_theme_support. + */ + $this->theme = new WP_Theme_JSON( $theme_support_data ); + $this->theme->merge( new WP_Theme_JSON( $theme_json_data ) ); + + return $this->theme; + } + + /** + * Returns the CPT that contains the user's origin config + * for the current theme or a void array if none found. + * + * It can also create and return a new draft CPT. + * + * @param bool $should_create_cpt Whether a new CPT should be created if no one was found. + * False by default. + * @param array $post_status_filter Filter CPT by post status. + * ['publish'] by default, so it only fetches published posts. + * + * @return array Custom Post Type for the user's origin config. + */ + private static function get_user_data_from_custom_post_type( $should_create_cpt = false, $post_status_filter = array( 'publish' ) ) { + $user_cpt = array(); + $post_type_filter = 'wp_global_styles'; + $post_name_filter = 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() ); + $recent_posts = wp_get_recent_posts( + array( + 'numberposts' => 1, + 'orderby' => 'date', + 'order' => 'desc', + 'post_type' => $post_type_filter, + 'post_status' => $post_status_filter, + 'name' => $post_name_filter, + ) + ); + + if ( is_array( $recent_posts ) && ( count( $recent_posts ) === 1 ) ) { + $user_cpt = $recent_posts[0]; + } elseif ( $should_create_cpt ) { + $cpt_post_id = wp_insert_post( + array( + 'post_content' => '{}', + 'post_status' => 'publish', + 'post_type' => $post_type_filter, + 'post_name' => $post_name_filter, + ), + true + ); + $user_cpt = get_post( $cpt_post_id, ARRAY_A ); + } + + return $user_cpt; + } + + /** + * Returns the user's origin config. + * + * @return WP_Theme_JSON Entity that holds user data. + */ + private static function get_user_origin() { + if ( null !== self::$user ) { + return self::$user; + } + + $config = array(); + $user_cpt = self::get_user_data_from_custom_post_type(); + if ( array_key_exists( 'post_content', $user_cpt ) ) { + $decoded_data = json_decode( $user_cpt['post_content'], true ); + + $json_decoding_error = json_last_error(); + if ( JSON_ERROR_NONE !== $json_decoding_error ) { + error_log( 'Error when decoding user schema: ' . json_last_error_msg() ); + return $config; + } + + if ( is_array( $decoded_data ) ) { + $config = $decoded_data; + } + } + self::$user = new WP_Theme_JSON( $config ); + + return self::$user; + } + + /** + * There are three sources of data for a site: + * core, theme, and user. + * + * The main function of the resolver is to + * merge all this data following this algorithm: + * theme overrides core, and user overrides + * data coming from either theme or core. + * + * user data > theme data > core data + * + * The main use case for the resolver is to return + * the merged data up to the user level.However, + * there are situations in which we need the + * data merged up to a different level (theme) + * or no merged at all. + * + * @param array $settings Existing block editor settings. + * Empty array by default. + * @param string $origin The source of data the consumer wants. + * Valid values are 'core', 'theme', 'user'. + * Default is 'user'. + * @param boolean $merged Whether the data should be merged + * with the previous origins (the default). + * + * @return WP_Theme_JSON + */ + public function get_origin( $settings = [], $origin = 'user', $merged = true ) { + + if ( ( 'user' === $origin ) && $merged ) { + $result = new WP_Theme_JSON(); + $result->merge( self::get_core_origin() ); + $result->merge( $this->get_theme_origin( $settings ) ); + $result->merge( self::get_user_origin() ); + return $result; + } + + if ( ( 'theme' === $origin ) && $merged ) { + $result = new WP_Theme_JSON(); + $result->merge( self::get_core_origin() ); + $result->merge( $this->get_theme_origin( $settings ) ); + return $result; + } + + if ( 'user' === $origin ) { + return self::get_user_origin(); + } + + if ( 'theme' === $origin ) { + return $this->get_theme_origin( $settings ); + } + + return self::get_core_origin(); + } + + /** + * Registers a Custom Post Type to store the user's origin config. + */ + public static function register_user_custom_post_type() { + if ( ! gutenberg_experimental_global_styles_has_theme_json_support() ) { + return; + } + + $args = array( + 'label' => __( 'Global Styles', 'gutenberg' ), + 'description' => 'CPT to store user design tokens', + 'public' => false, + 'show_ui' => false, + 'show_in_rest' => true, + 'rest_base' => '__experimental/global-styles', + 'capabilities' => array( + 'read' => 'edit_theme_options', + 'create_posts' => 'edit_theme_options', + 'edit_posts' => 'edit_theme_options', + 'edit_published_posts' => 'edit_theme_options', + 'delete_published_posts' => 'edit_theme_options', + 'edit_others_posts' => 'edit_theme_options', + 'delete_others_posts' => 'edit_theme_options', + ), + 'map_meta_cap' => true, + 'supports' => array( + 'editor', + 'revisions', + ), + ); + register_post_type( 'wp_global_styles', $args ); + } + + /** + * Returns the ID of the custom post type + * that stores user data. + * + * @return integer + */ + public static function get_user_custom_post_type_id() { + if ( null !== self::$user_custom_post_type_id ) { + return self::$user_custom_post_type_id; + } + + $user_cpt = self::get_user_data_from_custom_post_type( true ); + if ( array_key_exists( 'ID', $user_cpt ) ) { + self::$user_custom_post_type_id = $user_cpt['ID']; + } + + return self::$user_custom_post_type_id; + } + +} diff --git a/lib/global-styles.php b/lib/global-styles.php index b1aeb724588e1b..0270677ce03ea7 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -14,218 +14,6 @@ function gutenberg_experimental_global_styles_has_theme_json_support() { return is_readable( locate_template( 'experimental-theme.json' ) ); } -/** - * Processes a file that adheres to the theme.json - * schema and returns an array with its contents, - * or a void array if none found. - * - * @param string $file_path Path to file. - * @return array Contents that adhere to the theme.json schema. - */ -function gutenberg_experimental_global_styles_get_from_file( $file_path ) { - $config = array(); - if ( file_exists( $file_path ) ) { - $decoded_file = json_decode( - file_get_contents( $file_path ), - true - ); - - $json_decoding_error = json_last_error(); - if ( JSON_ERROR_NONE !== $json_decoding_error ) { - error_log( 'Error when decoding file schema: ' . json_last_error_msg() ); - return $config; - } - - if ( is_array( $decoded_file ) ) { - $config = $decoded_file; - } - } - return $config; -} - -/** - * Returns the user's origin config. - * - * @return WP_Theme_JSON Entity that holds user data. - */ -function gutenberg_experimental_global_styles_get_user() { - $config = array(); - $user_cpt = gutenberg_experimental_global_styles_get_user_cpt(); - if ( array_key_exists( 'post_content', $user_cpt ) ) { - $decoded_data = json_decode( $user_cpt['post_content'], true ); - - $json_decoding_error = json_last_error(); - if ( JSON_ERROR_NONE !== $json_decoding_error ) { - error_log( 'Error when decoding user schema: ' . json_last_error_msg() ); - return $config; - } - - if ( is_array( $decoded_data ) ) { - $config = $decoded_data; - } - } - - return new WP_Theme_JSON( $config ); -} - -/** - * Returns the CPT that contains the user's origin config - * for the current theme or a void array if none found. - * - * It can also create and return a new draft CPT. - * - * @param bool $should_create_cpt Whether a new CPT should be created if no one was found. - * False by default. - * @param array $post_status_filter Filter CPT by post status. - * ['publish'] by default, so it only fetches published posts. - * @return array Custom Post Type for the user's origin config. - */ -function gutenberg_experimental_global_styles_get_user_cpt( $should_create_cpt = false, $post_status_filter = array( 'publish' ) ) { - $user_cpt = array(); - $post_type_filter = 'wp_global_styles'; - $post_name_filter = 'wp-global-styles-' . urlencode( wp_get_theme()->get_stylesheet() ); - $recent_posts = wp_get_recent_posts( - array( - 'numberposts' => 1, - 'orderby' => 'date', - 'order' => 'desc', - 'post_type' => $post_type_filter, - 'post_status' => $post_status_filter, - 'name' => $post_name_filter, - ) - ); - - if ( is_array( $recent_posts ) && ( count( $recent_posts ) === 1 ) ) { - $user_cpt = $recent_posts[0]; - } elseif ( $should_create_cpt ) { - $cpt_post_id = wp_insert_post( - array( - 'post_content' => '{}', - 'post_status' => 'publish', - 'post_type' => $post_type_filter, - 'post_name' => $post_name_filter, - ), - true - ); - $user_cpt = get_post( $cpt_post_id, ARRAY_A ); - } - - return $user_cpt; -} - -/** - * Returns the post ID of the CPT containing the user's origin config. - * - * @return integer - */ -function gutenberg_experimental_global_styles_get_user_cpt_id() { - $user_cpt_id = null; - $user_cpt = gutenberg_experimental_global_styles_get_user_cpt( true ); - if ( array_key_exists( 'ID', $user_cpt ) ) { - $user_cpt_id = $user_cpt['ID']; - } - return $user_cpt_id; -} - -/** - * Return core's origin config. - * - * @return WP_Theme_JSON Entity that holds core data. - */ -function gutenberg_experimental_global_styles_get_core() { - $config = gutenberg_experimental_global_styles_get_from_file( - __DIR__ . '/experimental-default-theme.json' - ); - - // Start i18n logic to remove when JSON i18 strings are extracted. - $default_colors_i18n = array( - 'black' => __( 'Black', 'gutenberg' ), - 'cyan-bluish-gray' => __( 'Cyan bluish gray', 'gutenberg' ), - 'white' => __( 'White', 'gutenberg' ), - 'pale-pink' => __( 'Pale pink', 'gutenberg' ), - 'vivid-red' => __( 'Vivid red', 'gutenberg' ), - 'luminous-vivid-orange' => __( 'Luminous vivid orange', 'gutenberg' ), - 'luminous-vivid-amber' => __( 'Luminous vivid amber', 'gutenberg' ), - 'light-green-cyan' => __( 'Light green cyan', 'gutenberg' ), - 'vivid-green-cyan' => __( 'Vivid green cyan', 'gutenberg' ), - 'pale-cyan-blue' => __( 'Pale cyan blue', 'gutenberg' ), - 'vivid-cyan-blue' => __( 'Vivid cyan blue', 'gutenberg' ), - 'vivid-purple' => __( 'Vivid purple', 'gutenberg' ), - ); - if ( ! empty( $config['global']['settings']['color']['palette'] ) ) { - foreach ( $config['global']['settings']['color']['palette'] as &$color ) { - $color['name'] = $default_colors_i18n[ $color['slug'] ]; - } - } - - $default_gradients_i18n = array( - 'vivid-cyan-blue-to-vivid-purple' => __( 'Vivid cyan blue to vivid purple', 'gutenberg' ), - 'light-green-cyan-to-vivid-green-cyan' => __( 'Light green cyan to vivid green cyan', 'gutenberg' ), - 'luminous-vivid-amber-to-luminous-vivid-orange' => __( 'Luminous vivid amber to luminous vivid orange', 'gutenberg' ), - 'luminous-vivid-orange-to-vivid-red' => __( 'Luminous vivid orange to vivid red', 'gutenberg' ), - 'very-light-gray-to-cyan-bluish-gray' => __( 'Very light gray to cyan bluish gray', 'gutenberg' ), - 'cool-to-warm-spectrum' => __( 'Cool to warm spectrum', 'gutenberg' ), - 'blush-light-purple' => __( 'Blush light purple', 'gutenberg' ), - 'blush-bordeaux' => __( 'Blush bordeaux', 'gutenberg' ), - 'luminous-dusk' => __( 'Luminous dusk', 'gutenberg' ), - 'pale-ocean' => __( 'Pale ocean', 'gutenberg' ), - 'electric-grass' => __( 'Electric grass', 'gutenberg' ), - 'midnight' => __( 'Midnight', 'gutenberg' ), - ); - if ( ! empty( $config['global']['settings']['color']['gradients'] ) ) { - foreach ( $config['global']['settings']['color']['gradients'] as &$gradient ) { - $gradient['name'] = $default_gradients_i18n[ $gradient['slug'] ]; - } - } - - $default_font_sizes_i18n = array( - 'small' => __( 'Small', 'gutenberg' ), - 'normal' => __( 'Normal', 'gutenberg' ), - 'medium' => __( 'Medium', 'gutenberg' ), - 'large' => __( 'Large', 'gutenberg' ), - 'huge' => __( 'Huge', 'gutenberg' ), - ); - if ( ! empty( $config['global']['settings']['typography']['fontSizes'] ) ) { - foreach ( $config['global']['settings']['typography']['fontSizes'] as &$font_size ) { - $font_size['name'] = $default_font_sizes_i18n[ $font_size['slug'] ]; - } - } - - $default_font_styles_i18n = array( - 'normal' => __( 'Regular', 'gutenberg' ), - 'italic' => __( 'Italic', 'gutenberg' ), - 'initial' => __( 'Initial', 'gutenberg' ), - 'inherit' => __( 'Inherit', 'gutenberg' ), - ); - if ( ! empty( $config['global']['settings']['typography']['fontStyles'] ) ) { - foreach ( $config['global']['settings']['typography']['fontStyles'] as &$font_style ) { - $font_style['name'] = $default_font_styles_i18n[ $font_style['slug'] ]; - } - } - - $default_font_weights_i18n = array( - '100' => __( 'Ultralight', 'gutenberg' ), - '200' => __( 'Thin', 'gutenberg' ), - '300' => __( 'Light', 'gutenberg' ), - '400' => __( 'Regular', 'gutenberg' ), - '500' => __( 'Medium', 'gutenberg' ), - '600' => __( 'Semibold', 'gutenberg' ), - '700' => __( 'Bold', 'gutenberg' ), - '800' => __( 'Heavy', 'gutenberg' ), - '900' => __( 'Black', 'gutenberg' ), - 'initial' => __( 'Initial', 'gutenberg' ), - 'inherit' => __( 'Inherit', 'gutenberg' ), - ); - if ( ! empty( $config['global']['settings']['typography']['fontWeights'] ) ) { - foreach ( $config['global']['settings']['typography']['fontWeights'] as &$font_weight ) { - $font_weight['name'] = $default_font_weights_i18n[ $font_weight['slug'] ]; - } - } - // End i18n logic to remove when JSON i18 strings are extracted. - - return new WP_Theme_JSON( $config ); -} - /** * Returns the theme presets registered via add_theme_support, if any. * @@ -322,32 +110,6 @@ function gutenberg_experimental_global_styles_get_theme_support_settings( $setti return $theme_settings; } -/** - * Returns the theme's origin config. - * - * It also fetches the existing presets the theme declared via add_theme_support - * and uses them if the theme hasn't declared any via theme.json. - * - * @param array $settings Existing editor settings. - * - * @return WP_Theme_JSON Entity that holds theme data. - */ -function gutenberg_experimental_global_styles_get_theme( $settings ) { - $theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); - $theme_json_data = gutenberg_experimental_global_styles_get_from_file( - locate_template( 'experimental-theme.json' ) - ); - - /* - * We want the presets and settings declared in theme.json - * to override the ones declared via add_theme_support. - */ - $result = new WP_Theme_JSON( $theme_support_data ); - $result->merge( new WP_Theme_JSON( $theme_json_data ) ); - - return $result; -} - /** * Takes a tree adhering to the theme.json schema and generates * the corresponding stylesheet. @@ -399,9 +161,8 @@ function gutenberg_experimental_global_styles_get_stylesheet( $tree ) { */ function gutenberg_experimental_global_styles_enqueue_assets() { $settings = gutenberg_get_common_block_editor_settings(); - $all = gutenberg_experimental_global_styles_get_core(); - $all->merge( gutenberg_experimental_global_styles_get_theme( $settings ) ); - $all->merge( gutenberg_experimental_global_styles_get_user() ); + $resolver = new WP_Theme_JSON_Resolver(); + $all = $resolver->get_origin( $settings ); $stylesheet = gutenberg_experimental_global_styles_get_stylesheet( $all ); if ( empty( $stylesheet ) ) { @@ -420,15 +181,9 @@ function gutenberg_experimental_global_styles_enqueue_assets() { * @return array New block editor settings */ function gutenberg_experimental_global_styles_settings( $settings ) { - $base = gutenberg_experimental_global_styles_get_core(); - $all = gutenberg_experimental_global_styles_get_core(); - $theme = gutenberg_experimental_global_styles_get_theme( $settings ); - $user = gutenberg_experimental_global_styles_get_user(); - - $base->merge( $theme ); - - $all->merge( $theme ); - $all->merge( $user ); + $resolver = new WP_Theme_JSON_Resolver(); + $all = $resolver->get_origin( $settings ); + $base = $resolver->get_origin( $settings, 'theme' ); // STEP 1: ADD FEATURES // These need to be added to settings always. @@ -454,7 +209,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $screen->id ) && gutenberg_experimental_global_styles_has_theme_json_support() ) { - $settings['__experimentalGlobalStylesUserEntityId'] = gutenberg_experimental_global_styles_get_user_cpt_id(); + $settings['__experimentalGlobalStylesUserEntityId'] = WP_Theme_JSON_Resolver::get_user_custom_post_type_id(); $settings['__experimentalGlobalStylesBaseStyles'] = $base->get_raw_data(); } else { // STEP 3 - OTHERWISE, ADD STYLES @@ -469,39 +224,6 @@ function_exists( 'gutenberg_is_edit_site_page' ) && return $settings; } -/** - * Registers a Custom Post Type to store the user's origin config. - */ -function gutenberg_experimental_global_styles_register_cpt() { - if ( ! gutenberg_experimental_global_styles_has_theme_json_support() ) { - return; - } - - $args = array( - 'label' => __( 'Global Styles', 'gutenberg' ), - 'description' => 'CPT to store user design tokens', - 'public' => false, - 'show_ui' => false, - 'show_in_rest' => true, - 'rest_base' => '__experimental/global-styles', - 'capabilities' => array( - 'read' => 'edit_theme_options', - 'create_posts' => 'edit_theme_options', - 'edit_posts' => 'edit_theme_options', - 'edit_published_posts' => 'edit_theme_options', - 'delete_published_posts' => 'edit_theme_options', - 'edit_others_posts' => 'edit_theme_options', - 'delete_others_posts' => 'edit_theme_options', - ), - 'map_meta_cap' => true, - 'supports' => array( - 'editor', - 'revisions', - ), - ); - register_post_type( 'wp_global_styles', $args ); -} - -add_action( 'init', 'gutenberg_experimental_global_styles_register_cpt' ); +add_action( 'init', [ 'WP_Theme_JSON_Resolver', 'register_user_custom_post_type'] ); add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX ); add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' ); diff --git a/lib/load.php b/lib/load.php index 9227918378a0fd..967c995ee413a8 100644 --- a/lib/load.php +++ b/lib/load.php @@ -124,6 +124,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/navigation-page.php'; require __DIR__ . '/experiments-page.php'; require __DIR__ . '/class-wp-theme-json.php'; +require __DIR__ . '/class-wp-theme-json-resolver.php'; require __DIR__ . '/global-styles.php'; if ( ! class_exists( 'WP_Block_Supports' ) ) { From b41a904d1c2edecbee745964ed6220ff2f2f80bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Tue, 24 Nov 2020 12:44:44 +0100 Subject: [PATCH 2/6] Extract gutenberg_experimental_global_styles_get_theme_support_settings into WP_Theme_JSON_Resolver::extract_legacy_settings --- lib/class-wp-theme-json-resolver.php | 99 ++++++++++++++++++- lib/global-styles.php | 96 ------------------ .../class-theme-json-legacy-settings-test.php | 12 +-- 3 files changed, 104 insertions(+), 103 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index a453ce221eae9b..92d0e7d37ef4ac 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -169,6 +169,103 @@ private static function get_core_origin() { return self::$core; } + /** + * Extracts the legacy theme settings and reorganizes them + * into a structue that adheres to the theme.json schema. + * + * @param array $settings Existing editor settings. + * + * @return array Config that adheres to the theme.json schema. + */ + public static function extract_legacy_settings( $settings ) { + $theme_settings = array(); + $theme_settings['global'] = array(); + $theme_settings['global']['settings'] = array(); + + // Deprecated theme supports. + if ( isset( $settings['disableCustomColors'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['custom'] = ! $settings['disableCustomColors']; + } + + if ( isset( $settings['disableCustomGradients'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['customGradient'] = ! $settings['disableCustomGradients']; + } + + if ( isset( $settings['disableCustomFontSizes'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { + $theme_settings['global']['settings']['typography'] = array(); + } + $theme_settings['global']['settings']['typography']['customFontSize'] = ! $settings['disableCustomFontSizes']; + } + + if ( isset( $settings['enableCustomLineHeight'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { + $theme_settings['global']['settings']['typography'] = array(); + } + $theme_settings['global']['settings']['typography']['customLineHeight'] = $settings['enableCustomLineHeight']; + } + + if ( isset( $settings['enableCustomUnits'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['spacing'] ) ) { + $theme_settings['global']['settings']['spacing'] = array(); + } + $theme_settings['global']['settings']['spacing']['units'] = ( true === $settings['enableCustomUnits'] ) ? + array( 'px', 'em', 'rem', 'vh', 'vw' ) : + $settings['enableCustomUnits']; + } + + if ( isset( $settings['colors'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['palette'] = $settings['colors']; + } + + if ( isset( $settings['gradients'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['gradients'] = $settings['gradients']; + } + + if ( isset( $settings['fontSizes'] ) ) { + $font_sizes = $settings['fontSizes']; + // Back-compatibility for presets without units. + foreach ( $font_sizes as &$font_size ) { + if ( is_numeric( $font_size['size'] ) ) { + $font_size['size'] = $font_size['size'] . 'px'; + } + } + if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { + $theme_settings['global']['settings']['typography'] = array(); + } + $theme_settings['global']['settings']['typography']['fontSizes'] = $font_sizes; + } + + // Things that didn't land in core yet, so didn't have a setting assigned. + if ( current( (array) get_theme_support( 'custom-spacing' ) ) ) { + if ( ! isset( $theme_settings['global']['settings']['spacing'] ) ) { + $theme_settings['global']['settings']['spacing'] = array(); + } + $theme_settings['global']['settings']['spacing']['customPadding'] = true; + } + + if ( current( (array) get_theme_support( 'experimental-link-color' ) ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['link'] = true; + } + + return $theme_settings; + } + /** * Returns the theme's origin config. * @@ -181,7 +278,7 @@ private static function get_core_origin() { * @return WP_Theme_JSON Entity that holds theme data. */ private function get_theme_origin( $settings ) { - $theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); + $theme_support_data = self::extract_legacy_settings( $settings ); $theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) ); /* diff --git a/lib/global-styles.php b/lib/global-styles.php index 0270677ce03ea7..833159e9379adf 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -14,102 +14,6 @@ function gutenberg_experimental_global_styles_has_theme_json_support() { return is_readable( locate_template( 'experimental-theme.json' ) ); } -/** - * Returns the theme presets registered via add_theme_support, if any. - * - * @param array $settings Existing editor settings. - * - * @return array Config that adheres to the theme.json schema. - */ -function gutenberg_experimental_global_styles_get_theme_support_settings( $settings ) { - $theme_settings = array(); - $theme_settings['global'] = array(); - $theme_settings['global']['settings'] = array(); - - // Deprecated theme supports. - if ( isset( $settings['disableCustomColors'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['custom'] = ! $settings['disableCustomColors']; - } - - if ( isset( $settings['disableCustomGradients'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['customGradient'] = ! $settings['disableCustomGradients']; - } - - if ( isset( $settings['disableCustomFontSizes'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { - $theme_settings['global']['settings']['typography'] = array(); - } - $theme_settings['global']['settings']['typography']['customFontSize'] = ! $settings['disableCustomFontSizes']; - } - - if ( isset( $settings['enableCustomLineHeight'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { - $theme_settings['global']['settings']['typography'] = array(); - } - $theme_settings['global']['settings']['typography']['customLineHeight'] = $settings['enableCustomLineHeight']; - } - - if ( isset( $settings['enableCustomUnits'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['spacing'] ) ) { - $theme_settings['global']['settings']['spacing'] = array(); - } - $theme_settings['global']['settings']['spacing']['units'] = ( true === $settings['enableCustomUnits'] ) ? - array( 'px', 'em', 'rem', 'vh', 'vw' ) : - $settings['enableCustomUnits']; - } - - if ( isset( $settings['colors'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['palette'] = $settings['colors']; - } - - if ( isset( $settings['gradients'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['gradients'] = $settings['gradients']; - } - - if ( isset( $settings['fontSizes'] ) ) { - $font_sizes = $settings['fontSizes']; - // Back-compatibility for presets without units. - foreach ( $font_sizes as &$font_size ) { - if ( is_numeric( $font_size['size'] ) ) { - $font_size['size'] = $font_size['size'] . 'px'; - } - } - if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { - $theme_settings['global']['settings']['typography'] = array(); - } - $theme_settings['global']['settings']['typography']['fontSizes'] = $font_sizes; - } - - // Things that didn't land in core yet, so didn't have a setting assigned. - if ( current( (array) get_theme_support( 'custom-spacing' ) ) ) { - if ( ! isset( $theme_settings['global']['settings']['spacing'] ) ) { - $theme_settings['global']['settings']['spacing'] = array(); - } - $theme_settings['global']['settings']['spacing']['customPadding'] = true; - } - - if ( current( (array) get_theme_support( 'experimental-link-color' ) ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['link'] = true; - } - - return $theme_settings; -} - /** * Takes a tree adhering to the theme.json schema and generates * the corresponding stylesheet. diff --git a/phpunit/class-theme-json-legacy-settings-test.php b/phpunit/class-theme-json-legacy-settings-test.php index addadeceeccb06..5ade4792e0a233 100644 --- a/phpunit/class-theme-json-legacy-settings-test.php +++ b/phpunit/class-theme-json-legacy-settings-test.php @@ -48,7 +48,7 @@ function test_legacy_settings_blank() { ), ); - $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); + $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual ); } @@ -73,7 +73,7 @@ function test_legacy_settings_no_theme_support() { ), ); - $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); + $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual ); } @@ -86,7 +86,7 @@ function test_legacy_settings_custom_units_can_be_disabled() { 'units' => array( array() ), ); - $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); + $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual['global']['settings']['spacing'] ); } @@ -99,7 +99,7 @@ function test_legacy_settings_custom_units_can_be_enabled() { 'units' => array( 'px', 'em', 'rem', 'vh', 'vw' ), ); - $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); + $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual['global']['settings']['spacing'] ); } @@ -112,7 +112,7 @@ function test_legacy_settings_custom_units_can_be_filtered() { 'units' => array( 'rem', 'em' ), ); - $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); + $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual['global']['settings']['spacing'] ); } @@ -186,7 +186,7 @@ function test_legacy_settings_filled() { ), ); - $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); + $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual ); } From 992cca1171751989f1e3aeb2b816af615a04d5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Tue, 24 Nov 2020 12:51:33 +0100 Subject: [PATCH 3/6] Make linter happy --- lib/class-wp-theme-json-resolver.php | 37 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index 92d0e7d37ef4ac..82e874d021781a 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -1,10 +1,15 @@ __( 'Vivid cyan blue to vivid purple', 'gutenberg' ), - 'light-green-cyan-to-vivid-green-cyan' => __( 'Light green cyan to vivid green cyan', 'gutenberg' ), + 'vivid-cyan-blue-to-vivid-purple' => __( 'Vivid cyan blue to vivid purple', 'gutenberg' ), + 'light-green-cyan-to-vivid-green-cyan' => __( 'Light green cyan to vivid green cyan', 'gutenberg' ), 'luminous-vivid-amber-to-luminous-vivid-orange' => __( 'Luminous vivid amber to luminous vivid orange', 'gutenberg' ), - 'luminous-vivid-orange-to-vivid-red' => __( 'Luminous vivid orange to vivid red', 'gutenberg' ), - 'very-light-gray-to-cyan-bluish-gray' => __( 'Very light gray to cyan bluish gray', 'gutenberg' ), - 'cool-to-warm-spectrum' => __( 'Cool to warm spectrum', 'gutenberg' ), - 'blush-light-purple' => __( 'Blush light purple', 'gutenberg' ), - 'blush-bordeaux' => __( 'Blush bordeaux', 'gutenberg' ), - 'luminous-dusk' => __( 'Luminous dusk', 'gutenberg' ), - 'pale-ocean' => __( 'Pale ocean', 'gutenberg' ), - 'electric-grass' => __( 'Electric grass', 'gutenberg' ), - 'midnight' => __( 'Midnight', 'gutenberg' ), + 'luminous-vivid-orange-to-vivid-red' => __( 'Luminous vivid orange to vivid red', 'gutenberg' ), + 'very-light-gray-to-cyan-bluish-gray' => __( 'Very light gray to cyan bluish gray', 'gutenberg' ), + 'cool-to-warm-spectrum' => __( 'Cool to warm spectrum', 'gutenberg' ), + 'blush-light-purple' => __( 'Blush light purple', 'gutenberg' ), + 'blush-bordeaux' => __( 'Blush bordeaux', 'gutenberg' ), + 'luminous-dusk' => __( 'Luminous dusk', 'gutenberg' ), + 'pale-ocean' => __( 'Pale ocean', 'gutenberg' ), + 'electric-grass' => __( 'Electric grass', 'gutenberg' ), + 'midnight' => __( 'Midnight', 'gutenberg' ), ); if ( ! empty( $config['global']['settings']['color']['gradients'] ) ) { foreach ( $config['global']['settings']['color']['gradients'] as &$gradient ) { @@ -384,9 +389,9 @@ private static function get_user_origin() { * data merged up to a different level (theme) * or no merged at all. * - * @param array $settings Existing block editor settings. + * @param array $settings Existing block editor settings. * Empty array by default. - * @param string $origin The source of data the consumer wants. + * @param string $origin The source of data the consumer wants. * Valid values are 'core', 'theme', 'user'. * Default is 'user'. * @param boolean $merged Whether the data should be merged @@ -394,7 +399,7 @@ private static function get_user_origin() { * * @return WP_Theme_JSON */ - public function get_origin( $settings = [], $origin = 'user', $merged = true ) { + public function get_origin( $settings = array(), $origin = 'user', $merged = true ) { if ( ( 'user' === $origin ) && $merged ) { $result = new WP_Theme_JSON(); @@ -466,7 +471,7 @@ public static function get_user_custom_post_type_id() { return self::$user_custom_post_type_id; } - $user_cpt = self::get_user_data_from_custom_post_type( true ); + $user_cpt = self::get_user_data_from_custom_post_type( true ); if ( array_key_exists( 'ID', $user_cpt ) ) { self::$user_custom_post_type_id = $user_cpt['ID']; } From 2a21f5603d064c518d45456444830159794a10a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Tue, 24 Nov 2020 14:15:13 +0100 Subject: [PATCH 4/6] Revert "Extract gutenberg_experimental_global_styles_get_theme_support_settings into WP_Theme_JSON_Resolver::extract_legacy_settings" This reverts commit b41a904d1c2edecbee745964ed6220ff2f2f80bf. --- lib/class-wp-theme-json-resolver.php | 99 +------------------ lib/global-styles.php | 96 ++++++++++++++++++ .../class-theme-json-legacy-settings-test.php | 12 +-- 3 files changed, 103 insertions(+), 104 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index 82e874d021781a..d985c58eb71f2c 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -174,103 +174,6 @@ private static function get_core_origin() { return self::$core; } - /** - * Extracts the legacy theme settings and reorganizes them - * into a structue that adheres to the theme.json schema. - * - * @param array $settings Existing editor settings. - * - * @return array Config that adheres to the theme.json schema. - */ - public static function extract_legacy_settings( $settings ) { - $theme_settings = array(); - $theme_settings['global'] = array(); - $theme_settings['global']['settings'] = array(); - - // Deprecated theme supports. - if ( isset( $settings['disableCustomColors'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['custom'] = ! $settings['disableCustomColors']; - } - - if ( isset( $settings['disableCustomGradients'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['customGradient'] = ! $settings['disableCustomGradients']; - } - - if ( isset( $settings['disableCustomFontSizes'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { - $theme_settings['global']['settings']['typography'] = array(); - } - $theme_settings['global']['settings']['typography']['customFontSize'] = ! $settings['disableCustomFontSizes']; - } - - if ( isset( $settings['enableCustomLineHeight'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { - $theme_settings['global']['settings']['typography'] = array(); - } - $theme_settings['global']['settings']['typography']['customLineHeight'] = $settings['enableCustomLineHeight']; - } - - if ( isset( $settings['enableCustomUnits'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['spacing'] ) ) { - $theme_settings['global']['settings']['spacing'] = array(); - } - $theme_settings['global']['settings']['spacing']['units'] = ( true === $settings['enableCustomUnits'] ) ? - array( 'px', 'em', 'rem', 'vh', 'vw' ) : - $settings['enableCustomUnits']; - } - - if ( isset( $settings['colors'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['palette'] = $settings['colors']; - } - - if ( isset( $settings['gradients'] ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['gradients'] = $settings['gradients']; - } - - if ( isset( $settings['fontSizes'] ) ) { - $font_sizes = $settings['fontSizes']; - // Back-compatibility for presets without units. - foreach ( $font_sizes as &$font_size ) { - if ( is_numeric( $font_size['size'] ) ) { - $font_size['size'] = $font_size['size'] . 'px'; - } - } - if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { - $theme_settings['global']['settings']['typography'] = array(); - } - $theme_settings['global']['settings']['typography']['fontSizes'] = $font_sizes; - } - - // Things that didn't land in core yet, so didn't have a setting assigned. - if ( current( (array) get_theme_support( 'custom-spacing' ) ) ) { - if ( ! isset( $theme_settings['global']['settings']['spacing'] ) ) { - $theme_settings['global']['settings']['spacing'] = array(); - } - $theme_settings['global']['settings']['spacing']['customPadding'] = true; - } - - if ( current( (array) get_theme_support( 'experimental-link-color' ) ) ) { - if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { - $theme_settings['global']['settings']['color'] = array(); - } - $theme_settings['global']['settings']['color']['link'] = true; - } - - return $theme_settings; - } - /** * Returns the theme's origin config. * @@ -283,7 +186,7 @@ public static function extract_legacy_settings( $settings ) { * @return WP_Theme_JSON Entity that holds theme data. */ private function get_theme_origin( $settings ) { - $theme_support_data = self::extract_legacy_settings( $settings ); + $theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); $theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) ); /* diff --git a/lib/global-styles.php b/lib/global-styles.php index 833159e9379adf..0270677ce03ea7 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -14,6 +14,102 @@ function gutenberg_experimental_global_styles_has_theme_json_support() { return is_readable( locate_template( 'experimental-theme.json' ) ); } +/** + * Returns the theme presets registered via add_theme_support, if any. + * + * @param array $settings Existing editor settings. + * + * @return array Config that adheres to the theme.json schema. + */ +function gutenberg_experimental_global_styles_get_theme_support_settings( $settings ) { + $theme_settings = array(); + $theme_settings['global'] = array(); + $theme_settings['global']['settings'] = array(); + + // Deprecated theme supports. + if ( isset( $settings['disableCustomColors'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['custom'] = ! $settings['disableCustomColors']; + } + + if ( isset( $settings['disableCustomGradients'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['customGradient'] = ! $settings['disableCustomGradients']; + } + + if ( isset( $settings['disableCustomFontSizes'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { + $theme_settings['global']['settings']['typography'] = array(); + } + $theme_settings['global']['settings']['typography']['customFontSize'] = ! $settings['disableCustomFontSizes']; + } + + if ( isset( $settings['enableCustomLineHeight'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { + $theme_settings['global']['settings']['typography'] = array(); + } + $theme_settings['global']['settings']['typography']['customLineHeight'] = $settings['enableCustomLineHeight']; + } + + if ( isset( $settings['enableCustomUnits'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['spacing'] ) ) { + $theme_settings['global']['settings']['spacing'] = array(); + } + $theme_settings['global']['settings']['spacing']['units'] = ( true === $settings['enableCustomUnits'] ) ? + array( 'px', 'em', 'rem', 'vh', 'vw' ) : + $settings['enableCustomUnits']; + } + + if ( isset( $settings['colors'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['palette'] = $settings['colors']; + } + + if ( isset( $settings['gradients'] ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['gradients'] = $settings['gradients']; + } + + if ( isset( $settings['fontSizes'] ) ) { + $font_sizes = $settings['fontSizes']; + // Back-compatibility for presets without units. + foreach ( $font_sizes as &$font_size ) { + if ( is_numeric( $font_size['size'] ) ) { + $font_size['size'] = $font_size['size'] . 'px'; + } + } + if ( ! isset( $theme_settings['global']['settings']['typography'] ) ) { + $theme_settings['global']['settings']['typography'] = array(); + } + $theme_settings['global']['settings']['typography']['fontSizes'] = $font_sizes; + } + + // Things that didn't land in core yet, so didn't have a setting assigned. + if ( current( (array) get_theme_support( 'custom-spacing' ) ) ) { + if ( ! isset( $theme_settings['global']['settings']['spacing'] ) ) { + $theme_settings['global']['settings']['spacing'] = array(); + } + $theme_settings['global']['settings']['spacing']['customPadding'] = true; + } + + if ( current( (array) get_theme_support( 'experimental-link-color' ) ) ) { + if ( ! isset( $theme_settings['global']['settings']['color'] ) ) { + $theme_settings['global']['settings']['color'] = array(); + } + $theme_settings['global']['settings']['color']['link'] = true; + } + + return $theme_settings; +} + /** * Takes a tree adhering to the theme.json schema and generates * the corresponding stylesheet. diff --git a/phpunit/class-theme-json-legacy-settings-test.php b/phpunit/class-theme-json-legacy-settings-test.php index 5ade4792e0a233..addadeceeccb06 100644 --- a/phpunit/class-theme-json-legacy-settings-test.php +++ b/phpunit/class-theme-json-legacy-settings-test.php @@ -48,7 +48,7 @@ function test_legacy_settings_blank() { ), ); - $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); + $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual ); } @@ -73,7 +73,7 @@ function test_legacy_settings_no_theme_support() { ), ); - $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); + $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual ); } @@ -86,7 +86,7 @@ function test_legacy_settings_custom_units_can_be_disabled() { 'units' => array( array() ), ); - $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); + $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual['global']['settings']['spacing'] ); } @@ -99,7 +99,7 @@ function test_legacy_settings_custom_units_can_be_enabled() { 'units' => array( 'px', 'em', 'rem', 'vh', 'vw' ), ); - $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); + $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual['global']['settings']['spacing'] ); } @@ -112,7 +112,7 @@ function test_legacy_settings_custom_units_can_be_filtered() { 'units' => array( 'rem', 'em' ), ); - $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); + $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual['global']['settings']['spacing'] ); } @@ -186,7 +186,7 @@ function test_legacy_settings_filled() { ), ); - $actual = WP_Theme_JSON_Resolver::extract_legacy_settings( $input ); + $actual = gutenberg_experimental_global_styles_get_theme_support_settings( $input ); $this->assertEqualSetsWithIndex( $expected, $actual ); } From 211f3950b5c5df6a25c824c8eba51426a2541ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Tue, 24 Nov 2020 14:25:22 +0100 Subject: [PATCH 5/6] Extract theme support data in global-styles.php --- lib/class-wp-theme-json-resolver.php | 24 +++++++++++------------- lib/global-styles.php | 23 +++++++++++++---------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index d985c58eb71f2c..76a7507f275362 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -177,17 +177,15 @@ private static function get_core_origin() { /** * Returns the theme's origin config. * - * It also fetches the existing presets the theme - * may have declared via add_theme_support and - * uses them if the theme hasn't declared any via theme.json. + * It uses the theme support data if + * the theme hasn't declared any via theme.json. * - * @param array $settings Existing editor settings. + * @param array $theme_support_data Theme support data in theme.json format. * * @return WP_Theme_JSON Entity that holds theme data. */ - private function get_theme_origin( $settings ) { - $theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); - $theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) ); + private function get_theme_origin( $theme_support_data = array() ) { + $theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) ); /* * We want the presets and settings declared in theme.json @@ -292,8 +290,8 @@ private static function get_user_origin() { * data merged up to a different level (theme) * or no merged at all. * - * @param array $settings Existing block editor settings. - * Empty array by default. + * @param array $theme_support_data Existing block editor settings. + * Empty array by default. * @param string $origin The source of data the consumer wants. * Valid values are 'core', 'theme', 'user'. * Default is 'user'. @@ -302,12 +300,12 @@ private static function get_user_origin() { * * @return WP_Theme_JSON */ - public function get_origin( $settings = array(), $origin = 'user', $merged = true ) { + public function get_origin( $theme_support_data = array(), $origin = 'user', $merged = true ) { if ( ( 'user' === $origin ) && $merged ) { $result = new WP_Theme_JSON(); $result->merge( self::get_core_origin() ); - $result->merge( $this->get_theme_origin( $settings ) ); + $result->merge( $this->get_theme_origin( $theme_support_data ) ); $result->merge( self::get_user_origin() ); return $result; } @@ -315,7 +313,7 @@ public function get_origin( $settings = array(), $origin = 'user', $merged = tru if ( ( 'theme' === $origin ) && $merged ) { $result = new WP_Theme_JSON(); $result->merge( self::get_core_origin() ); - $result->merge( $this->get_theme_origin( $settings ) ); + $result->merge( $this->get_theme_origin( $theme_support_data ) ); return $result; } @@ -324,7 +322,7 @@ public function get_origin( $settings = array(), $origin = 'user', $merged = tru } if ( 'theme' === $origin ) { - return $this->get_theme_origin( $settings ); + return $this->get_theme_origin( $theme_support_data ); } return self::get_core_origin(); diff --git a/lib/global-styles.php b/lib/global-styles.php index 0270677ce03ea7..e9b86e23165972 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -160,9 +160,11 @@ function gutenberg_experimental_global_styles_get_stylesheet( $tree ) { * and enqueues the resulting stylesheet. */ function gutenberg_experimental_global_styles_enqueue_assets() { - $settings = gutenberg_get_common_block_editor_settings(); + $settings = gutenberg_get_common_block_editor_settings(); + $theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); + $resolver = new WP_Theme_JSON_Resolver(); - $all = $resolver->get_origin( $settings ); + $all = $resolver->get_origin( $theme_support_data ); $stylesheet = gutenberg_experimental_global_styles_get_stylesheet( $all ); if ( empty( $stylesheet ) ) { @@ -181,14 +183,7 @@ function gutenberg_experimental_global_styles_enqueue_assets() { * @return array New block editor settings */ function gutenberg_experimental_global_styles_settings( $settings ) { - $resolver = new WP_Theme_JSON_Resolver(); - $all = $resolver->get_origin( $settings ); - $base = $resolver->get_origin( $settings, 'theme' ); - - // STEP 1: ADD FEATURES - // These need to be added to settings always. - // We also need to unset the deprecated settings defined by core. - $settings['__experimentalFeatures'] = $all->get_settings(); + $theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); unset( $settings['colors'] ); unset( $settings['disableCustomColors'] ); unset( $settings['disableCustomFontSizes'] ); @@ -198,6 +193,14 @@ function gutenberg_experimental_global_styles_settings( $settings ) { unset( $settings['fontSizes'] ); unset( $settings['gradients'] ); + $resolver = new WP_Theme_JSON_Resolver(); + $all = $resolver->get_origin( $theme_support_data ); + $base = $resolver->get_origin( $theme_support_data, 'theme' ); + + // STEP 1: ADD FEATURES + // These need to be added to settings always. + $settings['__experimentalFeatures'] = $all->get_settings(); + // STEP 2 - IF EDIT-SITE, ADD DATA REQUIRED FOR GLOBAL STYLES SIDEBAR // The client needs some information to be able to access/update the user styles. // We only do this if the theme has support for theme.json, though, From 1c8ad8b94d7ec459056d9a7fcb48a505208a58d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Tue, 24 Nov 2020 14:45:55 +0100 Subject: [PATCH 6/6] Make linter happy --- lib/global-styles.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index e9b86e23165972..f6f2128326eb6d 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -227,6 +227,6 @@ function_exists( 'gutenberg_is_edit_site_page' ) && return $settings; } -add_action( 'init', [ 'WP_Theme_JSON_Resolver', 'register_user_custom_post_type'] ); +add_action( 'init', array( 'WP_Theme_JSON_Resolver', 'register_user_custom_post_type' ) ); add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX ); add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' );