| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP_Theme_JSON_Resolver_Gutenberg class |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class that abstracts the processing of the different data sources |
| 10 |
* for site-level config and offers an API to work with them. |
| 11 |
* |
| 12 |
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes). |
| 13 |
* This is a low-level API that may need to do breaking changes. Please, |
| 14 |
* use get_global_settings, get_global_styles, and get_global_stylesheet instead. |
| 15 |
* |
| 16 |
* @access private |
| 17 |
*/ |
| 18 |
class WP_Theme_JSON_Resolver_Gutenberg extends WP_Theme_JSON_Resolver_6_1 { |
| 19 |
/** |
| 20 |
* Returns the theme's data. |
| 21 |
* |
| 22 |
* Data from theme.json will be backfilled from existing |
| 23 |
* theme supports, if any. Note that if the same data |
| 24 |
* is present in theme.json and in theme supports, |
| 25 |
* the theme.json takes precedence. |
| 26 |
* |
| 27 |
* @param array $deprecated Deprecated argument. |
| 28 |
* @param array $settings Contains a key called with_supports to determine whether to include theme supports in the data. |
| 29 |
* @return WP_Theme_JSON_Gutenberg Entity that holds theme data. |
| 30 |
*/ |
| 31 |
public static function get_theme_data( $deprecated = array(), $settings = array( 'with_supports' => true ) ) { |
| 32 |
if ( ! empty( $deprecated ) ) { |
| 33 |
_deprecated_argument( __METHOD__, '5.9' ); |
| 34 |
} |
| 35 |
|
| 36 |
if ( null === static::$theme ) { |
| 37 |
$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); |
| 38 |
$theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); |
| 39 |
$theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_data ); |
| 40 |
static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data ); |
| 41 |
|
| 42 |
if ( wp_get_theme()->parent() ) { |
| 43 |
// Get parent theme.json. |
| 44 |
$parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); |
| 45 |
$parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); |
| 46 |
$parent_theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $parent_theme_json_data ); |
| 47 |
$parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data ); |
| 48 |
|
| 49 |
// Merge the child theme.json into the parent theme.json. |
| 50 |
// The child theme takes precedence over the parent. |
| 51 |
$parent_theme->merge( static::$theme ); |
| 52 |
static::$theme = $parent_theme; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! $settings['with_supports'] ) { |
| 57 |
return static::$theme; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* We want the presets and settings declared in theme.json |
| 62 |
* to override the ones declared via theme supports. |
| 63 |
* So we take theme supports, transform it to theme.json shape |
| 64 |
* and merge the static::$theme upon that. |
| 65 |
*/ |
| 66 |
$theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( get_default_block_editor_settings() ); |
| 67 |
if ( ! static::theme_has_support() ) { |
| 68 |
if ( ! isset( $theme_support_data['settings']['color'] ) ) { |
| 69 |
$theme_support_data['settings']['color'] = array(); |
| 70 |
} |
| 71 |
|
| 72 |
$default_palette = false; |
| 73 |
if ( current_theme_supports( 'default-color-palette' ) ) { |
| 74 |
$default_palette = true; |
| 75 |
} |
| 76 |
if ( ! isset( $theme_support_data['settings']['color']['palette'] ) ) { |
| 77 |
// If the theme does not have any palette, we still want to show the core one. |
| 78 |
$default_palette = true; |
| 79 |
} |
| 80 |
$theme_support_data['settings']['color']['defaultPalette'] = $default_palette; |
| 81 |
|
| 82 |
$default_gradients = false; |
| 83 |
if ( current_theme_supports( 'default-gradient-presets' ) ) { |
| 84 |
$default_gradients = true; |
| 85 |
} |
| 86 |
if ( ! isset( $theme_support_data['settings']['color']['gradients'] ) ) { |
| 87 |
// If the theme does not have any gradients, we still want to show the core ones. |
| 88 |
$default_gradients = true; |
| 89 |
} |
| 90 |
$theme_support_data['settings']['color']['defaultGradients'] = $default_gradients; |
| 91 |
|
| 92 |
// Classic themes without a theme.json don't support global duotone. |
| 93 |
$theme_support_data['settings']['color']['defaultDuotone'] = false; |
| 94 |
} |
| 95 |
$with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data ); |
| 96 |
$with_theme_supports->merge( static::$theme ); |
| 97 |
|
| 98 |
return $with_theme_supports; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Gets the styles for blocks from the block.json file. |
| 103 |
* |
| 104 |
* @return WP_Theme_JSON |
| 105 |
*/ |
| 106 |
public static function get_block_data() { |
| 107 |
$registry = WP_Block_Type_Registry::get_instance(); |
| 108 |
$blocks = $registry->get_all_registered(); |
| 109 |
$config = array( 'version' => 1 ); |
| 110 |
foreach ( $blocks as $block_name => $block_type ) { |
| 111 |
if ( isset( $block_type->supports['__experimentalStyle'] ) ) { |
| 112 |
$config['styles']['blocks'][ $block_name ] = static::remove_JSON_comments( $block_type->supports['__experimentalStyle'] ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( |
| 116 |
isset( $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ) && |
| 117 |
null === _wp_array_get( $config, array( 'styles', 'blocks', $block_name, 'spacing', 'blockGap' ), null ) |
| 118 |
) { |
| 119 |
// Ensure an empty placeholder value exists for the block, if it provides a default blockGap value. |
| 120 |
// The real blockGap value to be used will be determined when the styles are rendered for output. |
| 121 |
$config['styles']['blocks'][ $block_name ]['spacing']['blockGap'] = null; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Core here means it's the lower level part of the styles chain. |
| 126 |
// It can be a core or a third-party block. |
| 127 |
return new WP_Theme_JSON_Gutenberg( $config, 'core' ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* When given an array, this will remove any keys with the name `//`. |
| 132 |
* |
| 133 |
* @param array $array The array to filter. |
| 134 |
* @return array The filtered array. |
| 135 |
*/ |
| 136 |
private static function remove_JSON_comments( $array ) { |
| 137 |
unset( $array['//'] ); |
| 138 |
foreach ( $array as $k => $v ) { |
| 139 |
if ( is_array( $v ) ) { |
| 140 |
$array[ $k ] = static::remove_JSON_comments( $v ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $array; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the data merged from multiple origins. |
| 149 |
* |
| 150 |
* There are three sources of data (origins) for a site: |
| 151 |
* default, theme, and custom. The custom's has higher priority |
| 152 |
* than the theme's, and the theme's higher than default's. |
| 153 |
* |
| 154 |
* Unlike the getters {@link get_core_data}, |
| 155 |
* {@link get_theme_data}, and {@link get_user_data}, |
| 156 |
* this method returns data after it has been merged |
| 157 |
* with the previous origins. This means that if the same piece of data |
| 158 |
* is declared in different origins (user, theme, and core), |
| 159 |
* the last origin overrides the previous. |
| 160 |
* |
| 161 |
* For example, if the user has set a background color |
| 162 |
* for the paragraph block, and the theme has done it as well, |
| 163 |
* the user preference wins. |
| 164 |
* |
| 165 |
* @since 5.8.0 |
| 166 |
* @since 5.9.0 Added user data, removed the `$settings` parameter, |
| 167 |
* added the `$origin` parameter. |
| 168 |
* |
| 169 |
* @param string $origin Optional. To what level should we merge data. |
| 170 |
* Valid values are 'theme' or 'custom'. Default 'custom'. |
| 171 |
* @return WP_Theme_JSON |
| 172 |
*/ |
| 173 |
public static function get_merged_data( $origin = 'custom' ) { |
| 174 |
if ( is_array( $origin ) ) { |
| 175 |
_deprecated_argument( __FUNCTION__, '5.9' ); |
| 176 |
} |
| 177 |
|
| 178 |
$result = new WP_Theme_JSON_Gutenberg(); |
| 179 |
$result->merge( static::get_core_data() ); |
| 180 |
$result->merge( static::get_block_data() ); |
| 181 |
$result->merge( static::get_theme_data() ); |
| 182 |
if ( 'custom' === $origin ) { |
| 183 |
$result->merge( static::get_user_data() ); |
| 184 |
} |
| 185 |
// Generate the default spacing sizes presets. |
| 186 |
$result->set_spacing_sizes(); |
| 187 |
|
| 188 |
return $result; |
| 189 |
} |
| 190 |
} |
| 191 |
|