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

Global Styles: absorb editor settings transformation in WP_Theme_JSON #30610

Merged
merged 1 commit into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/class-wp-theme-json-resolver.php
Expand Up @@ -242,7 +242,6 @@ public static function get_core_data() {
return self::$core;
}

$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a leftover I found, so I thought I'd remove it here as well.

$config = self::read_json_file( __DIR__ . '/experimental-default-theme.json' );
$config = self::translate( $config );
self::$core = new WP_Theme_JSON( $config );
Expand Down
100 changes: 100 additions & 0 deletions lib/class-wp-theme-json.php
Expand Up @@ -1138,4 +1138,104 @@ public function get_raw_data() {
return $this->theme_json;
}

/**
*
* Transforms the given editor settings according the
* add_theme_support format to the theme.json format.
*
* @param array $settings Existing editor settings.
*
* @return array Config that adheres to the theme.json schema.
*/
public static function get_from_editor_settings( $settings ) {
$theme_settings = array( 'settings' => array() );

// Deprecated theme supports.
if ( isset( $settings['disableCustomColors'] ) ) {
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color']['custom'] = ! $settings['disableCustomColors'];
}

if ( isset( $settings['disableCustomGradients'] ) ) {
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color']['customGradient'] = ! $settings['disableCustomGradients'];
}

if ( isset( $settings['disableCustomFontSizes'] ) ) {
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['typography'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['typography'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['typography']['customFontSize'] = ! $settings['disableCustomFontSizes'];
}

if ( isset( $settings['enableCustomLineHeight'] ) ) {
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['typography'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['typography'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['typography']['customLineHeight'] = $settings['enableCustomLineHeight'];
}

if ( isset( $settings['enableCustomUnits'] ) ) {
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['spacing'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['spacing'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['spacing']['units'] = ( true === $settings['enableCustomUnits'] ) ?
array( 'px', 'em', 'rem', 'vh', 'vw' ) :
$settings['enableCustomUnits'];
}

if ( isset( $settings['colors'] ) ) {
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color']['palette'] = $settings['colors'];
}

if ( isset( $settings['gradients'] ) ) {
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color']['gradients'] = $settings['gradients'];
}

if ( isset( $settings['fontSizes'] ) ) {
$font_sizes = $settings['fontSizes'];
// Back-compatibility for presets without units.
foreach ( $font_sizes as $key => $font_size ) {
if ( is_numeric( $font_size['size'] ) ) {
$font_sizes[ $key ]['size'] = $font_size['size'] . 'px';
}
}
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['typography'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['typography'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['typography']['fontSizes'] = $font_sizes;
}

// This allows to make the plugin work with WordPress 5.7 beta
// as well as lower versions. The second check can be removed
// as soon as the minimum WordPress version for the plugin
// is bumped to 5.7.
if ( isset( $settings['enableCustomSpacing'] ) ) {
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['spacing'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['spacing'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['spacing']['customPadding'] = $settings['enableCustomSpacing'];
}

// Things that didn't land in core yet, so didn't have a setting assigned.
if ( current( (array) get_theme_support( 'experimental-link-color' ) ) ) {
if ( ! isset( $theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] ) ) {
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color'] = array();
}
$theme_settings['settings'][ self::ALL_BLOCKS_NAME ]['color']['link'] = true;
}

return $theme_settings;
}

}
105 changes: 2 additions & 103 deletions lib/global-styles.php
Expand Up @@ -5,107 +5,6 @@
* @package gutenberg
*/

/**
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been moved verbatim to WP_Theme_JSON::get_from_editor_settings, except for the first 4 lines:

$all_blocks                                = WP_Theme_JSON::ALL_BLOCKS_NAME;
$theme_settings                            = array();
$theme_settings['settings']                = array();
$theme_settings['settings'][ $all_blocks ] = array();

that were collapsed into one:

$theme_settings = array( 'settings' => array() );

* 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 ) {
$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME;
$theme_settings = array();
$theme_settings['settings'] = array();
$theme_settings['settings'][ $all_blocks ] = array();

// Deprecated theme supports.
if ( isset( $settings['disableCustomColors'] ) ) {
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) {
$theme_settings['settings'][ $all_blocks ]['color'] = array();
}
$theme_settings['settings'][ $all_blocks ]['color']['custom'] = ! $settings['disableCustomColors'];
}

if ( isset( $settings['disableCustomGradients'] ) ) {
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) {
$theme_settings['settings'][ $all_blocks ]['color'] = array();
}
$theme_settings['settings'][ $all_blocks ]['color']['customGradient'] = ! $settings['disableCustomGradients'];
}

if ( isset( $settings['disableCustomFontSizes'] ) ) {
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['typography'] ) ) {
$theme_settings['settings'][ $all_blocks ]['typography'] = array();
}
$theme_settings['settings'][ $all_blocks ]['typography']['customFontSize'] = ! $settings['disableCustomFontSizes'];
}

if ( isset( $settings['enableCustomLineHeight'] ) ) {
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['typography'] ) ) {
$theme_settings['settings'][ $all_blocks ]['typography'] = array();
}
$theme_settings['settings'][ $all_blocks ]['typography']['customLineHeight'] = $settings['enableCustomLineHeight'];
}

if ( isset( $settings['enableCustomUnits'] ) ) {
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['spacing'] ) ) {
$theme_settings['settings'][ $all_blocks ]['spacing'] = array();
}
$theme_settings['settings'][ $all_blocks ]['spacing']['units'] = ( true === $settings['enableCustomUnits'] ) ?
array( 'px', 'em', 'rem', 'vh', 'vw' ) :
$settings['enableCustomUnits'];
}

if ( isset( $settings['colors'] ) ) {
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) {
$theme_settings['settings'][ $all_blocks ]['color'] = array();
}
$theme_settings['settings'][ $all_blocks ]['color']['palette'] = $settings['colors'];
}

if ( isset( $settings['gradients'] ) ) {
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) {
$theme_settings['settings'][ $all_blocks ]['color'] = array();
}
$theme_settings['settings'][ $all_blocks ]['color']['gradients'] = $settings['gradients'];
}

if ( isset( $settings['fontSizes'] ) ) {
$font_sizes = $settings['fontSizes'];
// Back-compatibility for presets without units.
foreach ( $font_sizes as $key => $font_size ) {
if ( is_numeric( $font_size['size'] ) ) {
$font_sizes[ $key ]['size'] = $font_size['size'] . 'px';
}
}
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['typography'] ) ) {
$theme_settings['settings'][ $all_blocks ]['typography'] = array();
}
$theme_settings['settings'][ $all_blocks ]['typography']['fontSizes'] = $font_sizes;
}

// This allows to make the plugin work with WordPress 5.7 beta
// as well as lower versions. The second check can be removed
// as soon as the minimum WordPress version for the plugin
// is bumped to 5.7.
if ( isset( $settings['enableCustomSpacing'] ) ) {
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['spacing'] ) ) {
$theme_settings['settings'][ $all_blocks ]['spacing'] = array();
}
$theme_settings['settings'][ $all_blocks ]['spacing']['customPadding'] = $settings['enableCustomSpacing'];
}

// Things that didn't land in core yet, so didn't have a setting assigned.
if ( current( (array) get_theme_support( 'experimental-link-color' ) ) ) {
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) {
$theme_settings['settings'][ $all_blocks ]['color'] = array();
}
$theme_settings['settings'][ $all_blocks ]['color']['link'] = true;
}

return $theme_settings;
}

/**
* Takes a tree adhering to the theme.json schema and generates
* the corresponding stylesheet.
Expand Down Expand Up @@ -165,7 +64,7 @@ function gutenberg_experimental_global_styles_enqueue_assets() {
}

$settings = gutenberg_get_common_block_editor_settings();
$theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings );
$theme_support_data = WP_Theme_JSON::get_from_editor_settings( $settings );

$all = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data );

Expand All @@ -186,7 +85,7 @@ function gutenberg_experimental_global_styles_enqueue_assets() {
* @return array New block editor settings
*/
function gutenberg_experimental_global_styles_settings( $settings ) {
$theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings );
$theme_support_data = WP_Theme_JSON::get_from_editor_settings( $settings );
unset( $settings['colors'] );
unset( $settings['disableCustomColors'] );
unset( $settings['disableCustomFontSizes'] );
Expand Down