Changeset 58394
- Timestamp:
- 06/12/2024 07:15:06 AM (2 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-supports/block-style-variations.php
r58264 r58394 214 214 /** 215 215 * Collects block style variation data for merging with theme.json data. 216 * As each block style variation is processed it is registered if it hasn't217 * been already. This registration is required for later sanitization of218 * theme.json data.219 216 * 220 217 * @since 6.6.0 … … 225 222 * @return array Block variations data to be merged under `styles.blocks`. 226 223 */ 227 function wp_resolve_ and_register_block_style_variations( $variations ) {224 function wp_resolve_block_style_variations( $variations ) { 228 225 $variations_data = array(); 229 226 230 227 if ( empty( $variations ) ) { 231 228 return $variations_data; 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 232 420 } 233 421 … … 270 458 ); 271 459 } 272 273 // Add block style variation data under current block type.274 $path = array( $block_type, 'variations', $variation_name );275 _wp_array_set( $variations_data, $path, $variation_data );276 460 } 277 461 } 278 279 return $variations_data; 280 } 281 282 /** 283 * Merges variations data with existing theme.json data ensuring that the 284 * current theme.json data values take precedence. 285 * 286 * @since 6.6.0 287 * @access private 288 * 289 * @param array $variations_data Block style variations data keyed by block type. 290 * @param WP_Theme_JSON_Data $theme_json Current theme.json data. 291 * @param string $origin Origin for the theme.json data. 292 * 293 * @return WP_Theme_JSON The merged theme.json data. 294 */ 295 function wp_merge_block_style_variations_data( $variations_data, $theme_json, $origin = 'theme' ) { 296 if ( empty( $variations_data ) ) { 297 return $theme_json; 298 } 299 300 $variations_theme_json_data = array( 301 'version' => WP_Theme_JSON::LATEST_SCHEMA, 302 'styles' => array( 'blocks' => $variations_data ), 303 ); 304 305 $variations_theme_json = new WP_Theme_JSON_Data( $variations_theme_json_data, $origin ); 462 } 463 464 /** 465 * Register shared block style variations defined by the theme. 466 * 467 * These can come in three forms: 468 * - the theme's theme.json 469 * - the theme's partials (standalone files in `/styles` that only define block style variations) 470 * - the user's theme.json (for example, theme style variations the user selected) 471 * 472 * @since 6.6.0 473 * @access private 474 */ 475 function wp_register_block_style_variations_from_theme() { 476 // Partials from `/styles`. 477 $variations_partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' ); 478 wp_register_block_style_variations_from_theme_json_data( $variations_partials ); 306 479 307 480 /* 308 * Merge the current theme.json data over shared variation data so that 309 * any explicit per block variation values take precedence. 481 * Pull the data from the specific origin instead of the merged data. 482 * This is because, for 6.6, we only support registering block style variations 483 * for the 'theme' and 'custom' origins but not for 'default' (core theme.json) 484 * or 'custom' (theme.json in a block). 485 * 486 * When/If we add support for every origin, we should switch to using the public API 487 * instead, e.g.: wp_get_global_styles( array( 'blocks', 'variations' ) ). 310 488 */ 311 return $variations_theme_json->update_with( $theme_json->get_data() ); 312 } 313 314 /** 315 * Merges any shared block style variation definitions from a theme style 316 * variation into their appropriate block type within theme json styles. Any 317 * custom user selections already made will take precedence over the shared 318 * style variation value. 319 * 320 * @since 6.6.0 321 * @access private 322 * 323 * @param WP_Theme_JSON_Data $theme_json Current theme.json data. 324 * 325 * @return WP_Theme_JSON_Data 326 */ 327 function wp_resolve_block_style_variations_from_theme_style_variation( $theme_json ) { 328 $theme_json_data = $theme_json->get_data(); 329 $shared_variations = $theme_json_data['styles']['blocks']['variations'] ?? array(); 330 $variations_data = wp_resolve_and_register_block_style_variations( $shared_variations ); 331 332 return wp_merge_block_style_variations_data( $variations_data, $theme_json, 'user' ); 333 } 334 335 /** 336 * Merges block style variation data sourced from standalone partial 337 * theme.json files. 338 * 339 * @since 6.6.0 340 * @access private 341 * 342 * @param WP_Theme_JSON_Data $theme_json Current theme.json data. 343 * 344 * @return WP_Theme_JSON_Data 345 */ 346 function wp_resolve_block_style_variations_from_theme_json_partials( $theme_json ) { 347 $block_style_variations = WP_Theme_JSON_Resolver::get_style_variations( 'block' ); 348 $variations_data = wp_resolve_and_register_block_style_variations( $block_style_variations ); 349 350 return wp_merge_block_style_variations_data( $variations_data, $theme_json ); 351 } 352 353 /** 354 * Merges shared block style variations registered within the 355 * `styles.blocks.variations` property of the primary theme.json file. 356 * 357 * @since 6.6.0 358 * @access private 359 * 360 * @param WP_Theme_JSON_Data $theme_json Current theme.json data. 361 * 362 * @return WP_Theme_JSON_Data 363 */ 364 function wp_resolve_block_style_variations_from_primary_theme_json( $theme_json ) { 365 $theme_json_data = $theme_json->get_data(); 366 $block_style_variations = $theme_json_data['styles']['blocks']['variations'] ?? array(); 367 $variations_data = wp_resolve_and_register_block_style_variations( $block_style_variations ); 368 369 return wp_merge_block_style_variations_data( $variations_data, $theme_json ); 370 } 371 372 /** 373 * Merges block style variations registered via the block styles registry with a 374 * style object, under their appropriate block types within theme.json styles. 375 * Any variation values defined within the theme.json specific to a block type 376 * will take precedence over these shared definitions. 377 * 378 * @since 6.6.0 379 * @access private 380 * 381 * @param WP_Theme_JSON_Data $theme_json Current theme.json data. 382 * 383 * @return WP_Theme_JSON_Data 384 */ 385 function wp_resolve_block_style_variations_from_styles_registry( $theme_json ) { 386 $registry = WP_Block_Styles_Registry::get_instance(); 387 $styles = $registry->get_all_registered(); 388 $variations_data = array(); 389 390 foreach ( $styles as $block_type => $variations ) { 391 foreach ( $variations as $variation_name => $variation ) { 392 if ( ! empty( $variation['style_data'] ) ) { 393 $path = array( $block_type, 'variations', $variation_name ); 394 _wp_array_set( $variations_data, $path, $variation['style_data'] ); 395 } 396 } 397 } 398 399 return wp_merge_block_style_variations_data( $variations_data, $theme_json ); 400 } 401 402 /** 403 * Enqueues styles for block style variations. 404 * 405 * @since 6.6.0 406 * @access private 407 */ 408 function wp_enqueue_block_style_variation_styles() { 409 wp_enqueue_style( 'block-style-variation-styles' ); 410 } 411 412 // Register the block support. 413 WP_Block_Supports::get_instance()->register( 'block-style-variation', array() ); 414 415 add_filter( 'render_block_data', 'wp_render_block_style_variation_support_styles', 10, 2 ); 416 add_filter( 'render_block', 'wp_render_block_style_variation_class_name', 10, 2 ); 417 add_action( 'wp_enqueue_scripts', 'wp_enqueue_block_style_variation_styles', 1 ); 418 419 // Resolve block style variations from all their potential sources. The order here is deliberate. 420 add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_primary_theme_json', 10, 1 ); 421 add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_theme_json_partials', 10, 1 ); 422 add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_styles_registry', 10, 1 ); 423 424 add_filter( 'wp_theme_json_data_user', 'wp_resolve_block_style_variations_from_theme_style_variation', 10, 1 ); 489 490 // theme.json of the theme. 491 $theme_json_theme = WP_Theme_JSON_Resolver::get_theme_data(); 492 $variations_theme = $theme_json_theme->get_data()['styles']['blocks']['variations'] ?? array(); 493 wp_register_block_style_variations_from_theme_json_data( $variations_theme ); 494 495 // User data linked for this theme. 496 $theme_json_user = WP_Theme_JSON_Resolver::get_user_data(); 497 $variations_user = $theme_json_user->get_data()['styles']['blocks']['variations'] ?? array(); 498 wp_register_block_style_variations_from_theme_json_data( $variations_user ); 499 } 500 add_action( 'init', 'wp_register_block_style_variations_from_theme' ); -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php
r58262 r58394 232 232 * @since 5.9.0 233 233 * @since 6.2.0 Added validation of styles.css property. 234 234 235 * 235 236 * @param WP_REST_Request $request Request object. … … 264 265 $config['styles'] = $existing_config['styles']; 265 266 } 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 266 286 if ( isset( $request['settings'] ) ) { 267 287 $config['settings'] = $request['settings']; -
trunk/tests/phpunit/tests/block-supports/block-style-variations.php
r58264 r58394 66 66 switch_theme( 'block-theme' ); 67 67 68 69 70 71 72 73 74 68 75 $variation_styles_data = array( 69 76 'color' => array( -
trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php
r58328 r58394 604 604 605 605 /** 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 606 655 * @doesNotPerformAssertions 607 656 */
Note: See TracChangeset
for help on using the changeset viewer.