Skip to content

Commit

Permalink
Adapt Gutenberg code to work after global styles lands in core (#32059)
Browse files Browse the repository at this point in the history
Co-authored-by: ntsekouras <[email protected]>
  • Loading branch information
nosolosw and ntsekouras authored May 24, 2021
1 parent e6e0ea8 commit 1743b38
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 148 deletions.
4 changes: 2 additions & 2 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@
/lib @timothybjacobs @spacedmonkey
/lib/global-styles.php @timothybjabocs @spacedmonkey @nosolosw
/lib/experimental-default-theme.json @timothybjabocs @spacedmonkey @nosolosw
/lib/class-wp-theme-json.php @timothybjabocs @spacedmonkey @nosolosw
/lib/class-wp-theme-json-resolver.php @timothybjabocs @spacedmonkey @nosolosw
/lib/class-wp-theme-json-gutenberg.php @timothybjabocs @spacedmonkey @nosolosw
/lib/class-wp-theme-json-resolver-gutenberg.php @timothybjabocs @spacedmonkey @nosolosw
/phpunit/class-wp-theme-json-test.php @nosolosw

# Native (Unowned)
Expand Down
2 changes: 1 addition & 1 deletion lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function gutenberg_render_elements_support( $block_content, $block ) {
/*
* For now we only care about link color.
* This code in the future when we have a public API
* should take advantage of WP_Theme_JSON::compute_style_properties
* should take advantage of WP_Theme_JSON_Gutenberg::compute_style_properties
* and work for any element and style.
*/
if ( null === $link_color ) {
Expand Down
4 changes: 2 additions & 2 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {

$used_layout = $block['attrs']['layout'];
if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) {
$tree = WP_Theme_JSON_Resolver::get_merged_data( array(), 'theme' );
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( array(), 'theme' );
$default_layout = _wp_array_get( $tree->get_settings(), array( 'layout' ) );
if ( ! $default_layout ) {
return $block_content;
Expand Down Expand Up @@ -112,7 +112,7 @@ function gutenberg_restore_group_inner_container( $block_content, $block ) {

if (
'core/group' !== $block['blockName'] ||
WP_Theme_JSON_Resolver::theme_has_support() ||
WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ||
1 === preg_match( $group_with_inner_container_regex, $block_content )
) {
return $block_content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Class that encapsulates the processing of
* structures that adhere to the theme.json spec.
*/
class WP_Theme_JSON {
class WP_Theme_JSON_Gutenberg {

/**
* Container of data in theme.json format.
Expand Down Expand Up @@ -1075,9 +1075,9 @@ public function get_stylesheet( $type = 'all' ) {
/**
* Merge new incoming data.
*
* @param WP_Theme_JSON $incoming Data to merge.
* @param string $update_or_remove Whether update or remove existing colors
* for which the incoming data has a duplicated slug.
* @param WP_Theme_JSON_Gutenberg $incoming Data to merge.
* @param string $update_or_remove Whether update or remove existing colors
* for which the incoming data has a duplicated slug.
*/
public function merge( $incoming, $update_or_remove = 'remove' ) {
$incoming_data = $incoming->get_raw_data();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
* Class that abstracts the processing
* of the different data sources.
*/
class WP_Theme_JSON_Resolver {
class WP_Theme_JSON_Resolver_Gutenberg {

/**
* Container for data coming from core.
*
* @var WP_Theme_JSON
* @var WP_Theme_JSON_Gutenberg
*/
private static $core = null;

/**
* Container for data coming from the theme.
*
* @var WP_Theme_JSON
* @var WP_Theme_JSON_Gutenberg
*/
private static $theme = null;

Expand All @@ -36,7 +36,7 @@ class WP_Theme_JSON_Resolver {
/**
* Container for data coming from the user.
*
* @var WP_Theme_JSON
* @var WP_Theme_JSON_Gutenberg
*/
private static $user = null;

Expand Down Expand Up @@ -241,7 +241,7 @@ private static function translate( $theme_json, $domain = 'default' ) {
/**
* Return core's origin config.
*
* @return WP_Theme_JSON Entity that holds core data.
* @return WP_Theme_JSON_Gutenberg Entity that holds core data.
*/
public static function get_core_data() {
if ( null !== self::$core ) {
Expand All @@ -250,7 +250,7 @@ public static function get_core_data() {

$config = self::read_json_file( __DIR__ . '/experimental-default-theme.json' );
$config = self::translate( $config );
self::$core = new WP_Theme_JSON( $config );
self::$core = new WP_Theme_JSON_Gutenberg( $config );

return self::$core;
}
Expand All @@ -268,7 +268,7 @@ public static function get_core_data() {
*
* @param array $theme_support_data Theme support data in theme.json format.
*
* @return WP_Theme_JSON Entity that holds theme data.
* @return WP_Theme_JSON_Gutenberg Entity that holds theme data.
*/
public static function get_theme_data( $theme_support_data = array() ) {
if ( null === self::$theme ) {
Expand All @@ -278,7 +278,7 @@ public static function get_theme_data( $theme_support_data = array() ) {
$theme_json_data = self::read_json_file( self::get_file_path_from_theme( 'experimental-theme.json' ) );
}
$theme_json_data = self::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
self::$theme = new WP_Theme_JSON( $theme_json_data );
self::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data );
}

if ( empty( $theme_support_data ) ) {
Expand All @@ -289,7 +289,7 @@ public static function get_theme_data( $theme_support_data = array() ) {
* We want the presets and settings declared in theme.json
* to override the ones declared via add_theme_support.
*/
$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
$with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data );
$with_theme_supports->merge( self::$theme );

return $with_theme_supports;
Expand Down Expand Up @@ -333,7 +333,7 @@ private static function get_user_data_from_custom_post_type( $should_create_cpt
} elseif ( $should_create_cpt ) {
$cpt_post_id = wp_insert_post(
array(
'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
'post_content' => '{"version": ' . WP_Theme_JSON_Gutenberg::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
'post_status' => 'publish',
'post_title' => __( 'Custom Styles', 'default' ),
'post_type' => $post_type_filter,
Expand All @@ -353,7 +353,7 @@ private static function get_user_data_from_custom_post_type( $should_create_cpt
/**
* Returns the user's origin config.
*
* @return WP_Theme_JSON Entity that holds user data.
* @return WP_Theme_JSON_Gutenberg Entity that holds user data.
*/
public static function get_user_data() {
if ( null !== self::$user ) {
Expand All @@ -368,7 +368,7 @@ public static function get_user_data() {
$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 new WP_Theme_JSON( $config );
return new WP_Theme_JSON_Gutenberg( $config );
}

// Very important to verify if the flag isGlobalStylesUserThemeJSON is true.
Expand All @@ -382,7 +382,7 @@ public static function get_user_data() {
$config = $decoded_data;
}
}
self::$user = new WP_Theme_JSON( $config );
self::$user = new WP_Theme_JSON_Gutenberg( $config );

return self::$user;
}
Expand All @@ -409,12 +409,12 @@ public static function get_user_data() {
* Valid values are 'theme' or 'user'.
* Default is 'user'.
*
* @return WP_Theme_JSON
* @return WP_Theme_JSON_Gutenberg
*/
public static function get_merged_data( $settings = array(), $origin = 'user' ) {
$theme_support_data = WP_Theme_JSON::get_from_editor_settings( $settings );
$theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( $settings );

$result = new WP_Theme_JSON();
$result = new WP_Theme_JSON_Gutenberg();
$result->merge( self::get_core_data() );
$result->merge( self::get_theme_data( $theme_support_data ) );

Expand Down Expand Up @@ -532,4 +532,4 @@ public static function clean_cached_data() {

}

add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );
add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) );
2 changes: 1 addition & 1 deletion lib/class-wp-theme-json-schema-v0.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static function parse( $old ) {
$new['styles'] = self::process_styles( $old['styles'] );
}

$new['version'] = WP_Theme_JSON::LATEST_SCHEMA;
$new['version'] = WP_Theme_JSON_Gutenberg::LATEST_SCHEMA;

return $new;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ function gutenberg_register_packages_styles( $styles ) {
);

// Only load the default layout and margin styles for themes without theme.json file.
if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
$wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
}

Expand Down Expand Up @@ -663,7 +663,7 @@ function gutenberg_extend_block_editor_styles( $settings ) {
}

// Remove the default font editor styles for FSE themes.
if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
foreach ( $settings['styles'] as $j => $style ) {
if ( 0 === strpos( $style['css'], 'body { font-family:' ) ) {
unset( $settings['styles'][ $j ] );
Expand Down Expand Up @@ -693,7 +693,7 @@ function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings )
$settings['supportsTemplateMode'] = gutenberg_supports_block_templates();

// Enable the new layout options for themes with a theme.json file.
$settings['supportsLayout'] = WP_Theme_JSON_Resolver::theme_has_support();
$settings['supportsLayout'] = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();

return $settings;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ function _gutenberg_get_template_files( $template_type ) {
* @return array Template.
*/
function _gutenberg_add_template_part_area_info( $template_info ) {
if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
$theme_data = WP_Theme_JSON_Resolver::get_theme_data()->get_template_parts();
if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
$theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_template_parts();
}

if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function gutenberg_register_template_post_type() {
* Registers block editor 'wp_theme' taxonomy.
*/
function gutenberg_register_wp_theme_taxonomy() {
if ( ! gutenberg_supports_block_templates() && ! WP_Theme_JSON_Resolver::theme_has_support() ) {
if ( ! gutenberg_supports_block_templates() && ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
return;
}

Expand Down
Loading

0 comments on commit 1743b38

Please sign in to comment.