From f38c157de6ff9d7d517819af601db8d799dd861e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Mon, 4 May 2020 14:06:47 +0200 Subject: [PATCH 1/4] Expose color palette variables via global styles --- lib/global-styles.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index 6c98d27220297..bb3db97101389 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -153,14 +153,35 @@ function gutenberg_experimental_global_styles_get_core() { } /** - * Return theme's Global Styles. + * Return theme's Global Styles. It also fetches the editor palettes + * declared via add_theme_support. * * @return array Global Styles tree. */ function gutenberg_experimental_global_styles_get_theme() { - return gutenberg_experimental_global_styles_get_from_file( + $global_styles_theme = gutenberg_experimental_global_styles_get_from_file( locate_template( 'experimental-theme.json' ) ); + + // Take colors from declared theme support. + $theme_colors = get_theme_support( 'editor-color-palette' )[ 0 ]; + foreach( $theme_colors as $color ) { + $global_styles_theme['theme']['color'][ $color['slug'] ] = $color['color']; + } + + // Take gradients from declared theme support. + $theme_gradients = get_theme_support( 'editor-gradient-presets' )[ 0 ]; + foreach( $theme_gradients as $gradient ) { + $global_styles_theme['theme']['gradient'][ $gradient['slug'] ] = $gradient['gradient']; + } + + // Take font-sizes from declared theme support. + $theme_font_sizes = get_theme_support( 'editor-font-sizes' )[ 0 ]; + foreach( $theme_font_sizes as $font_size ) { + $global_styles_theme['theme']['font-size'][ $font_size['slug'] ] = $font_size['size']; + } + + return $global_styles_theme; } /** From fa5ce711536e118a828a7c8c10c0a880fd251a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Mon, 4 May 2020 14:12:36 +0200 Subject: [PATCH 2/4] Account for theme not declaring support --- lib/global-styles.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index bb3db97101389..619b38a27eead 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -165,20 +165,26 @@ function gutenberg_experimental_global_styles_get_theme() { // Take colors from declared theme support. $theme_colors = get_theme_support( 'editor-color-palette' )[ 0 ]; - foreach( $theme_colors as $color ) { - $global_styles_theme['theme']['color'][ $color['slug'] ] = $color['color']; + if ( is_array( $theme_colors ) ) { + foreach( $theme_colors as $color ) { + $global_styles_theme['theme']['color'][ $color['slug'] ] = $color['color']; + } } // Take gradients from declared theme support. $theme_gradients = get_theme_support( 'editor-gradient-presets' )[ 0 ]; - foreach( $theme_gradients as $gradient ) { - $global_styles_theme['theme']['gradient'][ $gradient['slug'] ] = $gradient['gradient']; + if( is_array( $theme_gradients ) ) { + foreach( $theme_gradients as $gradient ) { + $global_styles_theme['theme']['gradient'][ $gradient['slug'] ] = $gradient['gradient']; + } } // Take font-sizes from declared theme support. $theme_font_sizes = get_theme_support( 'editor-font-sizes' )[ 0 ]; - foreach( $theme_font_sizes as $font_size ) { - $global_styles_theme['theme']['font-size'][ $font_size['slug'] ] = $font_size['size']; + if( is_array( $theme_font_sizes ) ) { + foreach( $theme_font_sizes as $font_size ) { + $global_styles_theme['theme']['font-size'][ $font_size['slug'] ] = $font_size['size']; + } } return $global_styles_theme; From 953c3d2290f0709ae3921d2b1b2851ced1c74d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Tue, 5 May 2020 19:51:26 +0200 Subject: [PATCH 3/4] Update prefix to preset --- lib/global-styles.php | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index 619b38a27eead..e4cefa8e83025 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -159,15 +159,12 @@ function gutenberg_experimental_global_styles_get_core() { * @return array Global Styles tree. */ function gutenberg_experimental_global_styles_get_theme() { - $global_styles_theme = gutenberg_experimental_global_styles_get_from_file( - locate_template( 'experimental-theme.json' ) - ); - + $theme_supports = array(); // Take colors from declared theme support. $theme_colors = get_theme_support( 'editor-color-palette' )[ 0 ]; if ( is_array( $theme_colors ) ) { foreach( $theme_colors as $color ) { - $global_styles_theme['theme']['color'][ $color['slug'] ] = $color['color']; + $theme_supports['preset']['color'][ $color['slug'] ] = $color['color']; } } @@ -175,7 +172,7 @@ function gutenberg_experimental_global_styles_get_theme() { $theme_gradients = get_theme_support( 'editor-gradient-presets' )[ 0 ]; if( is_array( $theme_gradients ) ) { foreach( $theme_gradients as $gradient ) { - $global_styles_theme['theme']['gradient'][ $gradient['slug'] ] = $gradient['gradient']; + $theme_supports['preset']['gradient'][ $gradient['slug'] ] = $gradient['gradient']; } } @@ -183,11 +180,30 @@ function gutenberg_experimental_global_styles_get_theme() { $theme_font_sizes = get_theme_support( 'editor-font-sizes' )[ 0 ]; if( is_array( $theme_font_sizes ) ) { foreach( $theme_font_sizes as $font_size ) { - $global_styles_theme['theme']['font-size'][ $font_size['slug'] ] = $font_size['size']; + $theme_supports['preset']['font-size'][ $font_size['slug'] ] = $font_size['size']; } } - return $global_styles_theme; + /* + * We want the presets declared in theme.json + * to take precedence over the ones declared via add_theme_support. + * + * However, at the moment, it's not clear how we're going to declare them + * in theme.json until we resolve issues related to i18n and + * unfold the proper theme.json hierarchy. See: + * + * https://github.com/wp-cli/i18n-command/pull/210 + * https://github.com/WordPress/gutenberg/issues/20588 + * + * Hence, for simplicity, we take here the existing presets + * from the add_theme_support, if any. + */ + return array_merge( + gutenberg_experimental_global_styles_get_from_file( + locate_template( 'experimental-theme.json' ) + ), + $theme_supports + ); } /** From 96d92c4e35269409f66619de3945e7ceea0532b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 6 May 2020 14:01:02 +0200 Subject: [PATCH 4/4] Space out to make linter happy --- lib/global-styles.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index e4cefa8e83025..8101bf41707f5 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -161,25 +161,25 @@ function gutenberg_experimental_global_styles_get_core() { function gutenberg_experimental_global_styles_get_theme() { $theme_supports = array(); // Take colors from declared theme support. - $theme_colors = get_theme_support( 'editor-color-palette' )[ 0 ]; + $theme_colors = get_theme_support( 'editor-color-palette' )[0]; if ( is_array( $theme_colors ) ) { - foreach( $theme_colors as $color ) { + foreach ( $theme_colors as $color ) { $theme_supports['preset']['color'][ $color['slug'] ] = $color['color']; } } // Take gradients from declared theme support. - $theme_gradients = get_theme_support( 'editor-gradient-presets' )[ 0 ]; - if( is_array( $theme_gradients ) ) { - foreach( $theme_gradients as $gradient ) { + $theme_gradients = get_theme_support( 'editor-gradient-presets' )[0]; + if ( is_array( $theme_gradients ) ) { + foreach ( $theme_gradients as $gradient ) { $theme_supports['preset']['gradient'][ $gradient['slug'] ] = $gradient['gradient']; } } // Take font-sizes from declared theme support. - $theme_font_sizes = get_theme_support( 'editor-font-sizes' )[ 0 ]; - if( is_array( $theme_font_sizes ) ) { - foreach( $theme_font_sizes as $font_size ) { + $theme_font_sizes = get_theme_support( 'editor-font-sizes' )[0]; + if ( is_array( $theme_font_sizes ) ) { + foreach ( $theme_font_sizes as $font_size ) { $theme_supports['preset']['font-size'][ $font_size['slug'] ] = $font_size['size']; } }