Skip to content

Commit

Permalink
Update function names for the public global styles API functions (#36907
Browse files Browse the repository at this point in the history
)

* Use wp_* prefix in functions

By doing this, we offer a stable way for plugins to get access
to global styles data, no matter the WordPress version they're using
(e.g. WordPress 5.8) or whether the plugin is active or not.

* Update usage of functions
  • Loading branch information
oandregal authored and noisysocks committed Nov 29, 2021
1 parent 230aafe commit 6714b83
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 106 deletions.
4 changes: 2 additions & 2 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
return $block_content;
}

$block_gap = gutenberg_get_global_settings( array( 'spacing', 'blockGap' ) );
$default_layout = gutenberg_get_global_settings( array( 'layout' ) );
$block_gap = wp_get_global_settings( array( 'spacing', 'blockGap' ) );
$default_layout = wp_get_global_settings( array( 'layout' ) );
$has_block_gap_support = isset( $block_gap ) ? null !== $block_gap : false;
$default_block_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() );
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout;
Expand Down
204 changes: 105 additions & 99 deletions lib/compat/wordpress-5.9/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,124 +5,130 @@
* @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 array $context {
* Metadata to know where to retrieve the $path from. Optional.
*
* @type string $block_name Which block to retrieve the settings from.
* If empty, it'll return the settings for the global context.
* @type string $origin Which origin to take data from.
* Valid values are '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(), $context = array() ) {
if ( ! empty( $context['block_name'] ) ) {
$path = array_merge( array( 'blocks', $context['block_name'] ), $path );
}
if ( ! function_exists( 'wp_get_global_settings' ) ) {
/**
* 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 array $context {
* Metadata to know where to retrieve the $path from. Optional.
*
* @type string $block_name Which block to retrieve the settings from.
* If empty, it'll return the settings for the global context.
* @type string $origin Which origin to take data from.
* Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
* If empty or unknown, 'all' is used.
* }
*
* @return array The settings to retrieve.
*/
function wp_get_global_settings( $path = array(), $context = array() ) {
if ( ! empty( $context['block_name'] ) ) {
$path = array_merge( array( 'blocks', $context['block_name'] ), $path );
}

$origin = 'custom';
if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
$origin = 'theme';
}
$origin = 'custom';
if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
$origin = 'theme';
}

$settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings();
$settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings();

return _wp_array_get( $settings, $path, $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 array $context {
* Metadata to know where to retrieve the $path from. Optional.
*
* @type string $block_name Which block to retrieve the styles from.
* If empty, it'll return the styles for the global context.
* @type string $origin Which origin to take data from.
* Valid values are '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(), $context = array() ) {
if ( ! empty( $context['block_name'] ) ) {
$path = array_merge( array( 'blocks', $context['block_name'] ), $path );
}
if ( ! function_exists( 'wp_get_global_styles' ) ) {
/**
* 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 array $context {
* Metadata to know where to retrieve the $path from. Optional.
*
* @type string $block_name Which block to retrieve the styles from.
* If empty, it'll return the styles for the global context.
* @type string $origin Which origin to take data from.
* Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
* If empty or unknown, 'all' is used.
* }
*
* @return array The styles to retrieve.
*/
function wp_get_global_styles( $path = array(), $context = array() ) {
if ( ! empty( $context['block_name'] ) ) {
$path = array_merge( array( 'blocks', $context['block_name'] ), $path );
}

$origin = 'custom';
if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
$origin = 'theme';
}
$origin = 'custom';
if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
$origin = 'theme';
}

$styles = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_raw_data()['styles'];
$styles = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_raw_data()['styles'];

return _wp_array_get( $styles, $path, $styles );
return _wp_array_get( $styles, $path, $styles );
}
}

/**
* Returns the stylesheet resulting of merging core, theme, and user data.
*
* @param array $types Types of styles to load. Optional.
* It accepts 'variables', 'styles', 'presets' as values.
* If empty, it'll load all for themes with theme.json support
* and only [ 'variables', 'presets' ] for themes without theme.json support.
*
* @return string Stylesheet.
*/
function gutenberg_get_global_stylesheet( $types = array() ) {
// 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 = (
if ( ! function_exists( 'wp_get_global_stylesheet' ) ) {
/**
* Returns the stylesheet resulting of merging core, theme, and user data.
*
* @param array $types Types of styles to load. Optional.
* It accepts 'variables', 'styles', 'presets' as values.
* If empty, it'll load all for themes with theme.json support
* and only [ 'variables', 'presets' ] for themes without theme.json support.
*
* @return string Stylesheet.
*/
function wp_get_global_stylesheet( $types = array() ) {
// 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 = (
( empty( $types ) ) &&
( ! 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;
);
$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( $types ) && ! $supports_theme_json ) {
$types = array( 'variables', 'presets' );
} elseif ( empty( $types ) ) {
$types = array( 'variables', 'styles', 'presets' );
}
$supports_theme_json = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();
$supports_link_color = get_theme_support( 'experimental-link-color' );
if ( empty( $types ) && ! $supports_theme_json ) {
$types = array( 'variables', 'presets' );
} elseif ( empty( $types ) ) {
$types = array( 'variables', 'styles', 'presets' );
}

$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json && ! $supports_link_color ) {
// In this case we only enqueue the core presets (CSS Custom Properties + the classes).
$origins = array( 'default' );
} elseif ( ! $supports_theme_json && $supports_link_color ) {
// For the legacy link color feature to work, the CSS Custom Properties
// should be in scope (either the core or the theme ones).
$origins = array( 'default', 'theme' );
}
$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json && ! $supports_link_color ) {
// In this case we only enqueue the core presets (CSS Custom Properties + the classes).
$origins = array( 'default' );
} elseif ( ! $supports_theme_json && $supports_link_color ) {
// For the legacy link color feature to work, the CSS Custom Properties
// should be in scope (either the core or the theme ones).
$origins = array( 'default', 'theme' );
}

$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data();
$stylesheet = $tree->get_stylesheet( $types, $origins );
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data();
$stylesheet = $tree->get_stylesheet( $types, $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 );
}
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;
return $stylesheet;
}
}
10 changes: 5 additions & 5 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* and enqueues the resulting stylesheet.
*/
function gutenberg_experimental_global_styles_enqueue_assets() {
$stylesheet = gutenberg_get_global_stylesheet();
$stylesheet = wp_get_global_stylesheet();
if ( empty( $stylesheet ) ) {
return;
}
Expand Down Expand Up @@ -52,7 +52,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
}

if ( 'mobile' === $context && WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
$settings['__experimentalStyles'] = gutenberg_get_global_styles();
$settings['__experimentalStyles'] = wp_get_global_styles();
}

if ( 'other' === $context ) {
Expand Down Expand Up @@ -83,7 +83,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
),
);
foreach ( $new_presets as $new_style ) {
$style_css = gutenberg_get_global_stylesheet( array( $new_style['css'] ) );
$style_css = wp_get_global_stylesheet( array( $new_style['css'] ) );
if ( '' !== $style_css ) {
$new_style['css'] = $style_css;
$new_global_styles[] = $new_style;
Expand All @@ -95,7 +95,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
'__unstableType' => 'theme',
);
if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
$style_css = gutenberg_get_global_stylesheet( array( $new_block_classes['css'] ) );
$style_css = wp_get_global_stylesheet( array( $new_block_classes['css'] ) );
if ( '' !== $style_css ) {
$new_block_classes['css'] = $style_css;
$new_global_styles[] = $new_block_classes;
Expand All @@ -106,7 +106,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
}

// Copied from get_block_editor_settings() at wordpress-develop/block-editor.php.
$settings['__experimentalFeatures'] = gutenberg_get_global_settings();
$settings['__experimentalFeatures'] = wp_get_global_settings();

if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) {
$colors_by_origin = $settings['__experimentalFeatures']['color']['palette'];
Expand Down

0 comments on commit 6714b83

Please sign in to comment.