Skip to content

Commit

Permalink
WP_Theme_JSON: absorb editor settings transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Apr 7, 2021
1 parent 562c715 commit 0c4d08a
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 305 deletions.
97 changes: 97 additions & 0 deletions lib/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -1256,4 +1256,101 @@ public function get_raw_data() {
return $this->theme_json;
}

/**
*
* @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']['color'] ) ) {
$theme_settings['settings']['color'] = array();
}
$theme_settings['settings']['color']['custom'] = ! $settings['disableCustomColors'];
}

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

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

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

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

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

if ( isset( $settings['gradients'] ) ) {
if ( ! isset( $theme_settings['settings']['color'] ) ) {
$theme_settings['settings']['color'] = array();
}
$theme_settings['settings']['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']['typography'] ) ) {
$theme_settings['settings']['typography'] = array();
}
$theme_settings['settings']['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']['spacing'] ) ) {
$theme_settings['settings']['spacing'] = array();
}
$theme_settings['settings']['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']['color'] ) ) {
$theme_settings['settings']['color'] = array();
}
$theme_settings['settings']['color']['link'] = true;
}

return $theme_settings;
}

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

/**
* 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_Schema_V0::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
Loading

0 comments on commit 0c4d08a

Please sign in to comment.