-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add API to access global settings, styles, and stylesheet #34843
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fd5d5e4
Add functions to interact with global settings & styles
oandregal f5d8d25
Use new functions in block editor settings hook
oandregal 25caf50
Use new functions in layout block supports
oandregal bc02d8c
Remove the settings parameter from gutenberg_get_global_settings
oandregal 39db5c8
Remove the settings parameter from gutenberg_get_global_styles
oandregal 61b6f80
Remove the settings parameter from gutenberg_get_global_stylesheet
oandregal 3ad455b
Get back $consolidated tree
oandregal 17b739c
Get back $theme tree
oandregal 49d7a9d
Format PHP files
oandregal 66dbe4c
Make gutenberg_get_global_settings & gutenberg_get_global_styles syme…
oandregal d23bd12
Revert changes to gutenberg_experimental_global_styles_get_stylesheet
oandregal 6799329
Fix gutenberg_get_global_styles
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
lib/compat/wordpress-5.9/get-global-styles-and-settings.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
/** | ||
* API to interact with global settings & styles. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Function to get the settings resulting of merging core, theme, and user data. | ||
* | ||
* @param array $path Path to the specific setting to retrieve. Optional. | ||
* If empty, will return all settings. | ||
* @param string $block_name Which block to retrieve the settings from. Optional | ||
* If empty, it'll return the settings for the global context. | ||
* @param string $origin Which origin to take data from. Optional. | ||
* It can be 'all' (core, theme, and user) or 'base' (core and theme). | ||
* If empty or unknown, 'all' is used. | ||
* | ||
* @return array The settings to retrieve. | ||
*/ | ||
function gutenberg_get_global_settings( $path = array(), $block_name = '', $origin = 'all' ) { | ||
if ( '' !== $block_name ) { | ||
$path = array_merge( array( 'blocks', $block_name ), $path ); | ||
} | ||
|
||
if ( 'base' === $origin ) { | ||
$origin = 'theme'; | ||
} else { | ||
$origin = 'user'; | ||
} | ||
|
||
$theme_supports = gutenberg_get_default_block_editor_settings(); | ||
$settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $theme_supports, $origin )->get_settings(); | ||
|
||
return _wp_array_get( $settings, $path, $settings ); | ||
} | ||
|
||
/** | ||
* Function to get the styles resulting of merging core, theme, and user data. | ||
* | ||
* @param array $path Path to the specific style to retrieve. Optional. | ||
* If empty, will return all styles. | ||
* @param string $block_name Which block to retrieve the styles from. Optional. | ||
* If empty, it'll return the styles for the global context. | ||
* @param string $origin Which origin to take data from. Optional. | ||
* It can be 'all' (core, theme, and user) or 'base' (core and theme). | ||
* If empty or unknown, 'all' is used. | ||
* | ||
* @return array The styles to retrieve. | ||
*/ | ||
function gutenberg_get_global_styles( $path = array(), $block_name = '', $origin = 'all' ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other question is whether we should support string dotted paths. |
||
if ( '' !== $block_name ) { | ||
$path = array_merge( array( 'blocks', $block_name ), $path ); | ||
} | ||
|
||
if ( 'base' === $origin ) { | ||
$origin = 'theme'; | ||
} else { | ||
$origin = 'user'; | ||
} | ||
|
||
$theme_supports = gutenberg_get_default_block_editor_settings(); | ||
$styles = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $theme_supports, $origin )->get_raw_data()['styles']; | ||
|
||
return _wp_array_get( $styles, $path, $styles ); | ||
} | ||
|
||
/** | ||
* Returns the stylesheet resulting of merging core, theme, and user data. | ||
* | ||
* @param string $type Type of the stylesheet. Optional. | ||
* It accepts 'all', 'block_styles', 'css_variables', 'presets'. | ||
* If empty, it'll resolve to all (theme with theme.json support) | ||
* or 'presets' (theme without theme.json support). | ||
* | ||
* @return string Stylesheet. | ||
*/ | ||
function gutenberg_get_global_stylesheet( $type = '' ) { | ||
// Return cached value if it can be used and exists. | ||
// It's cached by theme to make sure that theme switching clears the cache. | ||
$can_use_cached = ( | ||
( 'all' === $type ) && | ||
( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && | ||
( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && | ||
( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && | ||
! is_admin() | ||
); | ||
$transient_name = 'gutenberg_global_styles_' . get_stylesheet(); | ||
if ( $can_use_cached ) { | ||
$cached = get_transient( $transient_name ); | ||
if ( $cached ) { | ||
return $cached; | ||
} | ||
} | ||
|
||
$supports_theme_json = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support(); | ||
$supports_link_color = get_theme_support( 'experimental-link-color' ); | ||
if ( empty( $type ) && ! $supports_theme_json ) { | ||
$type = 'presets'; | ||
} elseif ( empty( $type ) ) { | ||
$type = 'all'; | ||
} | ||
|
||
$origins = array( 'core', 'theme', 'user' ); | ||
if ( ! $supports_theme_json && ! $supports_link_color ) { | ||
// In this case we only enqueue the core presets (CSS Custom Properties + the classes). | ||
$origins = array( 'core' ); | ||
} elseif ( ! $supports_theme_json && $supports_link_color ) { | ||
// For the legacy link color feauter to work, the CSS Custom Properties | ||
// should be in scope (either the core or the theme ones). | ||
$origins = array( 'core', 'theme' ); | ||
} | ||
|
||
$theme_supports = gutenberg_get_default_block_editor_settings(); | ||
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $theme_supports ); | ||
$stylesheet = $tree->get_stylesheet( $type, $origins ); | ||
|
||
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. | ||
set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS ); | ||
} | ||
|
||
return $stylesheet; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While right now, the $block_name is the only possible "context", I wonder if we'd have new kind of context in the future so wondering if this should be a decision factor for the API shape or if it's fine for now. (One possibility could be to use an object for the last two arguments in order to support more options in the future, but I'm hesitant).
In the frontend, I think the API is still internal so that's why it's not a problem there yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I brought the object approach here and in the client API as well. My understanding was that we wanted to go in this direction instead. Whatever we do, I think it'd be best if the server and the client follow the same pattern.