| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstraps Global Styles. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Whether the current theme has a theme.json file. |
| 10 |
* |
| 11 |
* @return boolean |
| 12 |
*/ |
| 13 |
function gutenberg_experimental_global_styles_has_theme_json_support() { |
| 14 |
return is_readable( locate_template( 'experimental-theme.json' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Given a tree, it creates a flattened one |
| 19 |
* by merging the keys and binding the leaf values |
| 20 |
* to the new keys. |
| 21 |
* |
| 22 |
* This is thought to be useful to generate |
| 23 |
* CSS Custom Properties from a tree, |
| 24 |
* although there's nothing in the implementation |
| 25 |
* of this function that requires that format. |
| 26 |
* |
| 27 |
* For example, assuming the given prefix is '--wp' |
| 28 |
* and the token is '--', for this input tree: |
| 29 |
* |
| 30 |
* { |
| 31 |
* 'property': 'value', |
| 32 |
* 'nested-property': { |
| 33 |
* 'sub-property': 'value' |
| 34 |
* } |
| 35 |
* } |
| 36 |
* |
| 37 |
* it'll return this output: |
| 38 |
* |
| 39 |
* { |
| 40 |
* '--wp--property': 'value', |
| 41 |
* '--wp--nested-property--sub-property': 'value' |
| 42 |
* } |
| 43 |
* |
| 44 |
* @param array $tree Input tree to process. |
| 45 |
* @param string $prefix Prefix to prepend to each variable. '' by default. |
| 46 |
* @param string $token Token to use between levels. '--' by default. |
| 47 |
* |
| 48 |
* @return array The flattened tree. |
| 49 |
*/ |
| 50 |
function gutenberg_experimental_global_styles_get_css_vars( $tree, $prefix = '', $token = '--' ) { |
| 51 |
$result = array(); |
| 52 |
foreach ( $tree as $property => $value ) { |
| 53 |
$new_key = $prefix . str_replace( '/', '-', $property ); |
| 54 |
|
| 55 |
if ( is_array( $value ) ) { |
| 56 |
$new_prefix = $new_key . $token; |
| 57 |
$result = array_merge( |
| 58 |
$result, |
| 59 |
gutenberg_experimental_global_styles_get_css_vars( $value, $new_prefix, $token ) |
| 60 |
); |
| 61 |
} else { |
| 62 |
$result[ $new_key ] = $value; |
| 63 |
} |
| 64 |
} |
| 65 |
return $result; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Processes a file that adheres to the theme.json |
| 70 |
* schema and returns an array with its contents, |
| 71 |
* or a void array if none found. |
| 72 |
* |
| 73 |
* @param string $file_path Path to file. |
| 74 |
* @return array Contents that adhere to the theme.json schema. |
| 75 |
*/ |
| 76 |
function gutenberg_experimental_global_styles_get_from_file( $file_path ) { |
| 77 |
$config = array(); |
| 78 |
if ( file_exists( $file_path ) ) { |
| 79 |
$decoded_file = json_decode( |
| 80 |
file_get_contents( $file_path ), |
| 81 |
true |
| 82 |
); |
| 83 |
|
| 84 |
$json_decoding_error = json_last_error(); |
| 85 |
if ( JSON_ERROR_NONE !== $json_decoding_error ) { |
| 86 |
error_log( 'Error when decoding file schema: ' . json_last_error_msg() ); |
| 87 |
return $config; |
| 88 |
} |
| 89 |
|
| 90 |
if ( is_array( $decoded_file ) ) { |
| 91 |
$config = $decoded_file; |
| 92 |
} |
| 93 |
} |
| 94 |
return $config; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the user's origin config. |
| 99 |
* |
| 100 |
* @return array Config that adheres to the theme.json schema. |
| 101 |
*/ |
| 102 |
function gutenberg_experimental_global_styles_get_user() { |
| 103 |
$config = array(); |
| 104 |
$user_cpt = gutenberg_experimental_global_styles_get_user_cpt(); |
| 105 |
if ( array_key_exists( 'post_content', $user_cpt ) ) { |
| 106 |
$decoded_data = json_decode( $user_cpt['post_content'], true ); |
| 107 |
|
| 108 |
$json_decoding_error = json_last_error(); |
| 109 |
if ( JSON_ERROR_NONE !== $json_decoding_error ) { |
| 110 |
error_log( 'Error when decoding user schema: ' . json_last_error_msg() ); |
| 111 |
return $config; |
| 112 |
} |
| 113 |
|
| 114 |
if ( is_array( $decoded_data ) ) { |
| 115 |
$config = $decoded_data; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return $config; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the CPT that contains the user's origin config |
| 124 |
* for the current theme or a void array if none found. |
| 125 |
* |
| 126 |
* It can also create and return a new draft CPT. |
| 127 |
* |
| 128 |
* @param bool $should_create_cpt Whether a new CPT should be created if no one was found. |
| 129 |
* False by default. |
| 130 |
* @param array $post_status_filter Filter CPT by post status. |
| 131 |
* ['publish'] by default, so it only fetches published posts. |
| 132 |
* @return array Custom Post Type for the user's origin config. |
| 133 |
*/ |
| 134 |
function gutenberg_experimental_global_styles_get_user_cpt( $should_create_cpt = false, $post_status_filter = array( 'publish' ) ) { |
| 135 |
$user_cpt = array(); |
| 136 |
$post_type_filter = 'wp_global_styles'; |
| 137 |
$post_name_filter = 'wp-global-styles-' . strtolower( wp_get_theme()->get( 'TextDomain' ) ); |
| 138 |
$recent_posts = wp_get_recent_posts( |
| 139 |
array( |
| 140 |
'numberposts' => 1, |
| 141 |
'orderby' => 'date', |
| 142 |
'order' => 'desc', |
| 143 |
'post_type' => $post_type_filter, |
| 144 |
'post_status' => $post_status_filter, |
| 145 |
'name' => $post_name_filter, |
| 146 |
) |
| 147 |
); |
| 148 |
|
| 149 |
if ( is_array( $recent_posts ) && ( count( $recent_posts ) === 1 ) ) { |
| 150 |
$user_cpt = $recent_posts[0]; |
| 151 |
} elseif ( $should_create_cpt ) { |
| 152 |
$cpt_post_id = wp_insert_post( |
| 153 |
array( |
| 154 |
'post_content' => '{}', |
| 155 |
'post_status' => 'publish', |
| 156 |
'post_type' => $post_type_filter, |
| 157 |
'post_name' => $post_name_filter, |
| 158 |
), |
| 159 |
true |
| 160 |
); |
| 161 |
$user_cpt = get_post( $cpt_post_id, ARRAY_A ); |
| 162 |
} |
| 163 |
|
| 164 |
return $user_cpt; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns the post ID of the CPT containing the user's origin config. |
| 169 |
* |
| 170 |
* @return integer |
| 171 |
*/ |
| 172 |
function gutenberg_experimental_global_styles_get_user_cpt_id() { |
| 173 |
$user_cpt_id = null; |
| 174 |
$user_cpt = gutenberg_experimental_global_styles_get_user_cpt( true ); |
| 175 |
if ( array_key_exists( 'ID', $user_cpt ) ) { |
| 176 |
$user_cpt_id = $user_cpt['ID']; |
| 177 |
} |
| 178 |
return $user_cpt_id; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Return core's origin config. |
| 183 |
* |
| 184 |
* @return array Config that adheres to the theme.json schema. |
| 185 |
*/ |
| 186 |
function gutenberg_experimental_global_styles_get_core() { |
| 187 |
$config = gutenberg_experimental_global_styles_get_from_file( |
| 188 |
__DIR__ . '/experimental-default-theme.json' |
| 189 |
); |
| 190 |
|
| 191 |
return $config; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Returns the theme presets registered via add_theme_support, if any. |
| 196 |
* |
| 197 |
* @return array Config that adheres to the theme.json schema. |
| 198 |
*/ |
| 199 |
function gutenberg_experimental_global_styles_get_theme_presets() { |
| 200 |
$theme_presets = array(); |
| 201 |
|
| 202 |
$theme_colors = gutenberg_experimental_get( get_theme_support( 'editor-color-palette' ), array( '0' ) ); |
| 203 |
foreach ( $theme_colors as $color ) { |
| 204 |
$theme_presets['global']['presets']['color'][] = array( |
| 205 |
'slug' => $color['slug'], |
| 206 |
'value' => $color['color'], |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
$theme_gradients = gutenberg_experimental_get( get_theme_support( 'editor-gradient-presets' ), array( '0' ) ); |
| 211 |
foreach ( $theme_gradients as $gradient ) { |
| 212 |
$theme_presets['global']['presets']['gradient'][] = array( |
| 213 |
'slug' => $gradient['slug'], |
| 214 |
'value' => $gradient['gradient'], |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
$theme_font_sizes = gutenberg_experimental_get( get_theme_support( 'editor-font-sizes' ), array( '0' ) ); |
| 219 |
foreach ( $theme_font_sizes as $font_size ) { |
| 220 |
$theme_presets['global']['presets']['font-size'][] = array( |
| 221 |
'slug' => $font_size['slug'], |
| 222 |
'value' => $font_size['size'], |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
return $theme_presets; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Returns the theme's origin config. |
| 231 |
* |
| 232 |
* It also fetches the existing presets the theme declared via add_theme_support |
| 233 |
* and uses them if the theme hasn't declared any via theme.json. |
| 234 |
* |
| 235 |
* @return array Config that adheres to the theme.json schema. |
| 236 |
*/ |
| 237 |
function gutenberg_experimental_global_styles_get_theme() { |
| 238 |
$theme_presets = gutenberg_experimental_global_styles_get_theme_presets(); |
| 239 |
$theme_config = gutenberg_experimental_global_styles_get_from_file( |
| 240 |
locate_template( 'experimental-theme.json' ) |
| 241 |
); |
| 242 |
|
| 243 |
/* |
| 244 |
* We want the presets declared in theme.json |
| 245 |
* to take precedence over the ones declared via add_theme_support. |
| 246 |
* |
| 247 |
* Note that merging happens at the preset category level. Example: |
| 248 |
* |
| 249 |
* - if the theme declares a color palette via add_theme_support & |
| 250 |
* a set of font sizes via theme.json, both will be included in the output. |
| 251 |
* |
| 252 |
* - if the theme declares a color palette both via add_theme_support & |
| 253 |
* via theme.json, the later takes precedence. |
| 254 |
* |
| 255 |
*/ |
| 256 |
$theme_config = gutenberg_experimental_global_styles_merge_trees( |
| 257 |
$theme_presets, |
| 258 |
$theme_config |
| 259 |
); |
| 260 |
|
| 261 |
return $theme_config; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Returns the style features a particular block supports. |
| 266 |
* |
| 267 |
* @param array $supports The block supports array. |
| 268 |
* |
| 269 |
* @return array Style features supported by the block. |
| 270 |
*/ |
| 271 |
function gutenberg_experimental_global_styles_get_supported_styles( $supports ) { |
| 272 |
$style_features = array( |
| 273 |
'--wp--style--color--link' => array( '__experimentalColor', 'linkColor' ), |
| 274 |
'background-color' => array( '__experimentalColor' ), |
| 275 |
'background' => array( '__experimentalColor', 'gradients' ), |
| 276 |
'color' => array( '__experimentalColor' ), |
| 277 |
'font-size' => array( '__experimentalFontSize' ), |
| 278 |
'line-height' => array( '__experimentalLineHeight' ), |
| 279 |
); |
| 280 |
|
| 281 |
$supported_features = array(); |
| 282 |
foreach ( $style_features as $style_feature => $path ) { |
| 283 |
if ( gutenberg_experimental_get( $supports, $path ) ) { |
| 284 |
$supported_features[] = $style_feature; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
return $supported_features; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Retrieves the block data (selector/supports). |
| 293 |
* |
| 294 |
* @return array |
| 295 |
*/ |
| 296 |
function gutenberg_experimental_global_styles_get_block_data() { |
| 297 |
$block_data = array(); |
| 298 |
|
| 299 |
$registry = WP_Block_Type_Registry::get_instance(); |
| 300 |
$blocks = array_merge( |
| 301 |
$registry->get_all_registered(), |
| 302 |
array( |
| 303 |
'global' => new WP_Block_Type( |
| 304 |
'global', |
| 305 |
array( |
| 306 |
'supports' => array( |
| 307 |
'__experimentalSelector' => ':root', |
| 308 |
'__experimentalFontSize' => true, |
| 309 |
'__experimentalColor' => array( |
| 310 |
'linkColor' => true, |
| 311 |
'gradients' => true, |
| 312 |
), |
| 313 |
), |
| 314 |
) |
| 315 |
), |
| 316 |
) |
| 317 |
); |
| 318 |
foreach ( $blocks as $block_name => $block_type ) { |
| 319 |
if ( ! property_exists( $block_type, 'supports' ) || empty( $block_type->supports ) || ! is_array( $block_type->supports ) ) { |
| 320 |
continue; |
| 321 |
} |
| 322 |
|
| 323 |
$supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); |
| 324 |
if ( empty( $supports ) ) { |
| 325 |
continue; |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
* Assign the selector for the block. |
| 330 |
* |
| 331 |
* Some blocks can declare multiple selectors: |
| 332 |
* |
| 333 |
* - core/heading represents the H1-H6 HTML elements |
| 334 |
* - core/list represents the UL and OL HTML elements |
| 335 |
* - core/group is meant to represent DIV and other HTML elements |
| 336 |
* |
| 337 |
* Some other blocks don't provide a selector, |
| 338 |
* so we generate a class for them based on their name: |
| 339 |
* |
| 340 |
* - 'core/group' => '.wp-block-group' |
| 341 |
* - 'my-custom-library/block-name' => '.wp-block-my-custom-library-block-name' |
| 342 |
* |
| 343 |
* Note that, for core blocks, we don't add the `core/` prefix to its class name. |
| 344 |
* This is for historical reasons, as they come with a class without that infix. |
| 345 |
* |
| 346 |
*/ |
| 347 |
if ( |
| 348 |
isset( $block_type->supports['__experimentalSelector'] ) && |
| 349 |
is_string( $block_type->supports['__experimentalSelector'] ) |
| 350 |
) { |
| 351 |
$block_data[ $block_name ] = array( |
| 352 |
'selector' => $block_type->supports['__experimentalSelector'], |
| 353 |
'supports' => $supports, |
| 354 |
); |
| 355 |
} elseif ( |
| 356 |
isset( $block_type->supports['__experimentalSelector'] ) && |
| 357 |
is_array( $block_type->supports['__experimentalSelector'] ) |
| 358 |
) { |
| 359 |
foreach ( $block_type->supports['__experimentalSelector'] as $key => $selector ) { |
| 360 |
$block_data[ $key ] = array( |
| 361 |
'selector' => $selector, |
| 362 |
'supports' => $supports, |
| 363 |
); |
| 364 |
} |
| 365 |
} else { |
| 366 |
$block_data[ $block_name ] = array( |
| 367 |
'selector' => '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name ) ), |
| 368 |
'supports' => $supports, |
| 369 |
); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
return $block_data; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Given an array contain the styles shape returns the css for this styles. |
| 378 |
* A similar function exists on the client at /packages/block-editor/src/hooks/style.js. |
| 379 |
* |
| 380 |
* @param array $styles Array containing the styles shape from global styles. |
| 381 |
* |
| 382 |
* @return array Containing a set of css rules. |
| 383 |
*/ |
| 384 |
function gutenberg_experimental_global_styles_flatten_styles_tree( $styles ) { |
| 385 |
$mappings = array( |
| 386 |
'line-height' => array( 'typography', 'lineHeight' ), |
| 387 |
'font-size' => array( 'typography', 'fontSize' ), |
| 388 |
'background' => array( 'color', 'gradient' ), |
| 389 |
'background-color' => array( 'color', 'background' ), |
| 390 |
'color' => array( 'color', 'text' ), |
| 391 |
'--wp--style--color--link' => array( 'color', 'link' ), |
| 392 |
); |
| 393 |
|
| 394 |
$result = array(); |
| 395 |
|
| 396 |
foreach ( $mappings as $key => $path ) { |
| 397 |
$value = gutenberg_experimental_get( $styles, $path, null ); |
| 398 |
if ( null !== $value ) { |
| 399 |
$result[ $key ] = $value; |
| 400 |
} |
| 401 |
} |
| 402 |
return $result; |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Takes a tree adhering to the theme.json schema and generates |
| 408 |
* the corresponding stylesheet. |
| 409 |
* |
| 410 |
* @param array $tree Input tree. |
| 411 |
* |
| 412 |
* @return string Stylesheet. |
| 413 |
*/ |
| 414 |
function gutenberg_experimental_global_styles_resolver( $tree ) { |
| 415 |
$stylesheet = ''; |
| 416 |
$block_data = gutenberg_experimental_global_styles_get_block_data(); |
| 417 |
foreach ( array_keys( $tree ) as $block_name ) { |
| 418 |
if ( |
| 419 |
! array_key_exists( $block_name, $block_data ) || |
| 420 |
! array_key_exists( 'selector', $block_data[ $block_name ] ) || |
| 421 |
! array_key_exists( 'supports', $block_data[ $block_name ] ) |
| 422 |
) { |
| 423 |
// Skip blocks that haven't declared support, |
| 424 |
// because we don't know to process them. |
| 425 |
continue; |
| 426 |
} |
| 427 |
|
| 428 |
// Extract the relevant preset info before converting them to CSS Custom Properties. |
| 429 |
foreach ( array_keys( $tree[ $block_name ]['presets'] ) as $preset_category ) { |
| 430 |
$flattened_values = array(); |
| 431 |
foreach ( $tree[ $block_name ]['presets'][ $preset_category ] as $preset_value ) { |
| 432 |
$flattened_values[ $preset_value['slug'] ] = $preset_value['value']; |
| 433 |
} |
| 434 |
$tree[ $block_name ]['presets'][ $preset_category ] = $flattened_values; |
| 435 |
} |
| 436 |
|
| 437 |
$token = '--'; |
| 438 |
$prefix = '--wp--preset' . $token; |
| 439 |
$css_variables = gutenberg_experimental_global_styles_get_css_vars( $tree[ $block_name ]['presets'], $prefix, $token ); |
| 440 |
|
| 441 |
$stylesheet .= gutenberg_experimental_global_styles_resolver_styles( |
| 442 |
$block_data[ $block_name ]['selector'], |
| 443 |
$block_data[ $block_name ]['supports'], |
| 444 |
array_merge( |
| 445 |
gutenberg_experimental_global_styles_flatten_styles_tree( $tree[ $block_name ]['styles'] ), |
| 446 |
$css_variables |
| 447 |
) |
| 448 |
); |
| 449 |
} |
| 450 |
|
| 451 |
if ( gutenberg_experimental_global_styles_has_theme_json_support() ) { |
| 452 |
// To support all themes, we added in the block-library stylesheet |
| 453 |
// a style rule such as .has-link-color a { color: var(--wp--style--color--link, #00e); } |
| 454 |
// so that existing link colors themes used didn't break. |
| 455 |
// We add this here to make it work for themes that opt-in to theme.json |
| 456 |
// In the future, we may do this differently. |
| 457 |
$stylesheet .= 'a { color: var(--wp--style--color--link, #00e); }'; |
| 458 |
} |
| 459 |
|
| 460 |
return $stylesheet; |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Generates CSS declarations for a block. |
| 465 |
* |
| 466 |
* @param string $block_selector CSS selector for the block. |
| 467 |
* @param array $block_supports A list of properties supported by the block. |
| 468 |
* @param array $block_styles The list of properties/values to be converted to CSS. |
| 469 |
* |
| 470 |
* @return string The corresponding CSS rule. |
| 471 |
*/ |
| 472 |
function gutenberg_experimental_global_styles_resolver_styles( $block_selector, $block_supports, $block_styles ) { |
| 473 |
$css_rule = ''; |
| 474 |
$css_declarations = ''; |
| 475 |
|
| 476 |
foreach ( $block_styles as $property => $value ) { |
| 477 |
// Only convert to CSS: |
| 478 |
// |
| 479 |
// 1) The style attributes the block has declared support for. |
| 480 |
// 2) Any CSS custom property attached to the node. |
| 481 |
if ( in_array( $property, $block_supports, true ) || strstr( $property, '--' ) ) { |
| 482 |
$css_declarations .= "\t" . $property . ': ' . $value . ";\n"; |
| 483 |
} |
| 484 |
} |
| 485 |
if ( '' !== $css_declarations ) { |
| 486 |
$css_rule .= $block_selector . " {\n"; |
| 487 |
$css_rule .= $css_declarations; |
| 488 |
$css_rule .= "}\n"; |
| 489 |
} |
| 490 |
|
| 491 |
return $css_rule; |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Helper function that merges trees that adhere to the theme.json schema. |
| 496 |
* |
| 497 |
* @param array $core Core origin. |
| 498 |
* @param array $theme Theme origin. |
| 499 |
* @param array $user User origin. An empty array by default. |
| 500 |
* |
| 501 |
* @return array The merged result. |
| 502 |
*/ |
| 503 |
function gutenberg_experimental_global_styles_merge_trees( $core, $theme, $user = array() ) { |
| 504 |
$core = gutenberg_experimental_global_styles_normalize_schema( $core ); |
| 505 |
$theme = gutenberg_experimental_global_styles_normalize_schema( $theme ); |
| 506 |
$user = gutenberg_experimental_global_styles_normalize_schema( $user ); |
| 507 |
$result = gutenberg_experimental_global_styles_normalize_schema( array() ); |
| 508 |
|
| 509 |
foreach ( array_keys( $core ) as $block_name ) { |
| 510 |
foreach ( array( 'presets', 'features' ) as $subtree ) { |
| 511 |
$result[ $block_name ][ $subtree ] = array_merge( |
| 512 |
$core[ $block_name ][ $subtree ], |
| 513 |
$theme[ $block_name ][ $subtree ], |
| 514 |
$user[ $block_name ][ $subtree ] |
| 515 |
); |
| 516 |
} |
| 517 |
foreach ( array_keys( $core[ $block_name ]['styles'] ) as $subtree ) { |
| 518 |
$result[ $block_name ]['styles'][ $subtree ] = array_merge( |
| 519 |
$core[ $block_name ]['styles'][ $subtree ], |
| 520 |
$theme[ $block_name ]['styles'][ $subtree ], |
| 521 |
$user[ $block_name ]['styles'][ $subtree ] |
| 522 |
); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
return $result; |
| 527 |
} |
| 528 |
|
| 529 |
/** |
| 530 |
* Given a tree, it normalizes it to the expected schema. |
| 531 |
* |
| 532 |
* @param array $tree Source tree to normalize. |
| 533 |
* |
| 534 |
* @return array Normalized tree. |
| 535 |
*/ |
| 536 |
function gutenberg_experimental_global_styles_normalize_schema( $tree ) { |
| 537 |
$block_schema = array( |
| 538 |
'styles' => array( |
| 539 |
'typography' => array(), |
| 540 |
'color' => array(), |
| 541 |
), |
| 542 |
'features' => array(), |
| 543 |
'presets' => array(), |
| 544 |
); |
| 545 |
|
| 546 |
$normalized_tree = array(); |
| 547 |
$block_data = gutenberg_experimental_global_styles_get_block_data(); |
| 548 |
foreach ( array_keys( $block_data ) as $block_name ) { |
| 549 |
$normalized_tree[ $block_name ] = $block_schema; |
| 550 |
} |
| 551 |
|
| 552 |
$tree = array_merge_recursive( |
| 553 |
$normalized_tree, |
| 554 |
$tree |
| 555 |
); |
| 556 |
|
| 557 |
return $tree; |
| 558 |
} |
| 559 |
|
| 560 |
/** |
| 561 |
* Takes data from the different origins (core, theme, and user) |
| 562 |
* and returns the merged result. |
| 563 |
* |
| 564 |
* @return array Merged trees. |
| 565 |
*/ |
| 566 |
function gutenberg_experimental_global_styles_get_merged_origins() { |
| 567 |
$core = gutenberg_experimental_global_styles_get_core(); |
| 568 |
$theme = gutenberg_experimental_global_styles_get_theme(); |
| 569 |
$user = gutenberg_experimental_global_styles_get_user(); |
| 570 |
$merged = gutenberg_experimental_global_styles_merge_trees( $core, $theme, $user ); |
| 571 |
|
| 572 |
return $merged; |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* Fetches the preferences for each origin (core, theme, user) |
| 577 |
* and enqueues the resulting stylesheet. |
| 578 |
*/ |
| 579 |
function gutenberg_experimental_global_styles_enqueue_assets() { |
| 580 |
$merged = gutenberg_experimental_global_styles_get_merged_origins(); |
| 581 |
$stylesheet = gutenberg_experimental_global_styles_resolver( $merged ); |
| 582 |
if ( empty( $stylesheet ) ) { |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
wp_register_style( 'global-styles', false, array(), true, true ); |
| 587 |
wp_add_inline_style( 'global-styles', $stylesheet ); |
| 588 |
wp_enqueue_style( 'global-styles' ); |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* Returns the default config for editor features, |
| 593 |
* or an empty array if none found. |
| 594 |
* |
| 595 |
* @param array $config Config to extract values from. |
| 596 |
* @return array Default features config for the editor. |
| 597 |
*/ |
| 598 |
function gutenberg_experimental_global_styles_get_editor_features( $config ) { |
| 599 |
$features = array(); |
| 600 |
foreach ( array_keys( $config ) as $context ) { |
| 601 |
if ( |
| 602 |
empty( $config[ $context ]['features'] ) || |
| 603 |
! is_array( $config[ $context ]['features'] ) |
| 604 |
) { |
| 605 |
$features[ $context ] = array(); |
| 606 |
} else { |
| 607 |
$features[ $context ] = $config[ $context ]['features']; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
// Deprecated theme supports. |
| 612 |
if ( get_theme_support( 'disable-custom-colors' ) ) { |
| 613 |
if ( ! isset( $features['global']['color'] ) ) { |
| 614 |
$features['global']['color'] = array(); |
| 615 |
} |
| 616 |
$features['global']['color']['custom'] = false; |
| 617 |
} |
| 618 |
|
| 619 |
return $features; |
| 620 |
} |
| 621 |
|
| 622 |
/** |
| 623 |
* Adds the necessary data for the Global Styles client UI to the block settings. |
| 624 |
* |
| 625 |
* @param array $settings Existing block editor settings. |
| 626 |
* @return array New block editor settings |
| 627 |
*/ |
| 628 |
function gutenberg_experimental_global_styles_settings( $settings ) { |
| 629 |
|
| 630 |
if ( gutenberg_experimental_global_styles_has_theme_json_support() ) { |
| 631 |
$settings['__experimentalGlobalStylesUserEntityId'] = gutenberg_experimental_global_styles_get_user_cpt_id(); |
| 632 |
|
| 633 |
$global_styles = gutenberg_experimental_global_styles_merge_trees( |
| 634 |
gutenberg_experimental_global_styles_get_core(), |
| 635 |
gutenberg_experimental_global_styles_get_theme() |
| 636 |
); |
| 637 |
|
| 638 |
$settings['__experimentalGlobalStylesBase'] = $global_styles; |
| 639 |
} |
| 640 |
|
| 641 |
// Add the styles for the editor via the settings |
| 642 |
// so they get processed as if they were added via add_editor_styles: |
| 643 |
// they will get the editor wrapper class. |
| 644 |
$merged = gutenberg_experimental_global_styles_get_merged_origins(); |
| 645 |
$stylesheet = gutenberg_experimental_global_styles_resolver( $merged ); |
| 646 |
$settings['styles'][] = array( 'css' => $stylesheet ); |
| 647 |
|
| 648 |
$settings['__experimentalFeatures'] = gutenberg_experimental_global_styles_get_editor_features( $merged ); |
| 649 |
// Unsetting deprecated settings defined by Core. |
| 650 |
unset( $settings['disableCustomColors'] ); |
| 651 |
|
| 652 |
return $settings; |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* Registers a Custom Post Type to store the user's origin config. |
| 657 |
*/ |
| 658 |
function gutenberg_experimental_global_styles_register_cpt() { |
| 659 |
if ( ! gutenberg_experimental_global_styles_has_theme_json_support() ) { |
| 660 |
return; |
| 661 |
} |
| 662 |
|
| 663 |
$args = array( |
| 664 |
'label' => __( 'Global Styles', 'gutenberg' ), |
| 665 |
'description' => 'CPT to store user design tokens', |
| 666 |
'public' => false, |
| 667 |
'show_ui' => false, |
| 668 |
'show_in_rest' => true, |
| 669 |
'rest_base' => '__experimental/global-styles', |
| 670 |
'capabilities' => array( |
| 671 |
'read' => 'edit_theme_options', |
| 672 |
'create_posts' => 'edit_theme_options', |
| 673 |
'edit_posts' => 'edit_theme_options', |
| 674 |
'edit_published_posts' => 'edit_theme_options', |
| 675 |
'delete_published_posts' => 'edit_theme_options', |
| 676 |
'edit_others_posts' => 'edit_theme_options', |
| 677 |
'delete_others_posts' => 'edit_theme_options', |
| 678 |
), |
| 679 |
'map_meta_cap' => true, |
| 680 |
'supports' => array( |
| 681 |
'editor', |
| 682 |
'revisions', |
| 683 |
), |
| 684 |
); |
| 685 |
register_post_type( 'wp_global_styles', $args ); |
| 686 |
} |
| 687 |
|
| 688 |
add_action( 'init', 'gutenberg_experimental_global_styles_register_cpt' ); |
| 689 |
add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings' ); |
| 690 |
add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' ); |
| 691 |
|