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

Update global styles public API #36610

Merged
merged 4 commits into from Nov 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 32 additions & 26 deletions lib/compat/wordpress-5.9/get-global-styles-and-settings.php
Expand Up @@ -8,25 +8,28 @@
/**
* 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.
* @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(), $block_name = '', $origin = 'all' ) {
if ( '' !== $block_name ) {
$path = array_merge( array( 'blocks', $block_name ), $path );
function gutenberg_get_global_settings( $path = array(), $context = array() ) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we want function_exists checks for core?

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't need them as the current naming stands, but I discussed this with Miguel and I'm going to check how do we want this public API to operate (see). I'll post as a comment any follow-up PR.

if ( ! empty( $context['block_name'] ) ) {
$path = array_merge( array( 'blocks', $context['block_name'] ), $path );
}

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

$settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings();
Expand All @@ -37,25 +40,28 @@ function gutenberg_get_global_settings( $path = array(), $block_name = '', $orig
/**
* 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.
* @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(), $block_name = '', $origin = 'all' ) {
if ( '' !== $block_name ) {
$path = array_merge( array( 'blocks', $block_name ), $path );
function gutenberg_get_global_styles( $path = array(), $context = array() ) {
if ( ! empty( $context['block_name'] ) ) {
$path = array_merge( array( 'blocks', $context['block_name'] ), $path );
}

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

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