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

Try generating styles for classic themes by filtering theme.json data #44268

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function update_with( $new_data ) {
return $this;
}

public function create_with( $new_data ) {
Copy link
Member Author

@oandregal oandregal Sep 21, 2022

Choose a reason for hiding this comment

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

Turns out we don't have a great way to clear existing data through the merge process if it's not overwrite it. This creates a new object from scratch. May need to revisit the ability to clear data but this works for now.

$this->theme_json = new WP_Theme_JSON_Gutenberg( $new_data, $this->origin );
}

/**
* Returns the underlying data.
*
Expand Down
37 changes: 2 additions & 35 deletions lib/compat/wordpress-6.1/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,42 +82,9 @@ function gutenberg_get_global_stylesheet( $types = array() ) {
return $cached;
}
}
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data();
$supports_theme_json = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();
if ( empty( $types ) && ! $supports_theme_json ) {
$types = array( 'variables', 'presets', 'base-layout-styles' );
} elseif ( empty( $types ) ) {
$types = array( 'variables', 'styles', 'presets' );
}

/*
* If variables are part of the stylesheet,
* we add them for all origins (default, theme, user).
* This is so themes without a theme.json still work as before 5.9:
* they can override the default presets.
* See https://core.trac.wordpress.org/ticket/54782
*/
$styles_variables = '';
if ( in_array( 'variables', $types, true ) ) {
$styles_variables = $tree->get_stylesheet( array( 'variables' ) );
$types = array_diff( $types, array( 'variables' ) );
}
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data();
$stylesheet = $tree->get_stylesheet( $types );

/*
* For the remaining types (presets, styles), we do consider origins:
*
* - themes without theme.json: only the classes for the presets defined by core
* - themes with theme.json: the presets and styles classes, both from core and the theme
*/
$styles_rest = '';
if ( ! empty( $types ) ) {
$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json ) {
$origins = array( 'default' );
}
$styles_rest = $tree->get_stylesheet( $types, $origins );
}
$stylesheet = $styles_variables . $styles_rest;
if ( $can_use_cached ) {
// Cache for a minute.
// This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites.
Expand Down
54 changes: 54 additions & 0 deletions lib/compat/wordpress-6.1/hook-theme-json-for-classic-themes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

function theme_json_current_theme_has_support( ){
return WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();
}

function theme_json_default_filter_for_classic_themes( $theme_json_data ) {
if ( ! theme_json_current_theme_has_support() ) {
$new_data = array(
'version' => 2,
'settings' => array(
/*
* We should be able to remove this.
* Without it, it breaks.
*/
"spacing" => array(
Copy link
Member Author

@oandregal oandregal Sep 21, 2022

Choose a reason for hiding this comment

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

Something breaks if I create a WP_Theme_JSON object without settings. It seems that it's this bit that fails. I haven't been able to look at why, but it should be possible to create an object with empty settings, correct? cc @glendaviesnz

"spacingScale" => array(
"operator" => "*",
"increment" => 1.5,
"steps" => 7,
"mediumStep" => 1.5,
"unit" => "rem"
),
),
),
'styles' => array(
'elements' => array(
'button' => array(
'color' => array(
'text' => '#fff',
'background' => '#32373c',
),
'spacing' => array(
'padding' =>'calc(0.667em + 2px) calc(1.333em + 2px)',
),
'typography' => array(
'fontSize' => 'inherit',
'fontFamily' => 'inherit',
'lineHeight' => 'inherit',
'textDecoration' => 'none',
),
'border' => array(
'width' => '0',
),
),
),
),
);
$theme_json_data->create_with( $new_data );
}

return $theme_json_data;
}
add_filter( 'theme_json_default', 'theme_json_default_filter_for_classic_themes');
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.1/block-editor-settings.php';
require __DIR__ . '/compat/wordpress-6.1/persisted-preferences.php';
require __DIR__ . '/compat/wordpress-6.1/get-global-styles-and-settings.php';
require __DIR__ . '/compat/wordpress-6.1/hook-theme-json-for-classic-themes.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-6-1.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php';
Expand Down