Ticket #49615: 49615-core-blocks.diff
File 49615-core-blocks.diff, 3.8 KB (added by , 4 years ago) |
---|
-
src/wp-includes/blocks/index.php
65 65 } 66 66 } 67 67 add_action( 'init', 'register_core_block_types_from_metadata' ); 68 69 /** 70 * Given a registered block type settings array, assigns default attributes 71 * based on the block supports configuration. It mirrors the same behavior 72 * applied on the client using `blocks.registerBlockType` filter. 73 * 74 * @since 5.5.0 75 * 76 * @param array $args Block type settings. 77 * @return array Block type settings with default attributes applied. 78 */ 79 function register_block_type_add_default_attributes( $args ) { 80 // This check accounts for a special case when `attributes` is not set 81 // or the value provided is invalid. 82 if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { 83 return $args; 84 } 85 86 $attributes = $args['attributes']; 87 $supports = isset( $args['supports'] ) ? $args['supports'] : array(); 88 89 if ( ! empty( $supports['align'] ) && empty( $attributes['align']['type'] ) ) { 90 $args['attributes']['align'] = array( 91 'type' => 'string', 92 ); 93 } 94 95 if ( ! empty( $supports['anchor'] ) && empty( $attributes['anchor']['type'] ) ) { 96 $args['attributes']['anchor'] = array( 97 'type' => 'string', 98 'source' => 'attribute', 99 'attribute' => 'id', 100 'selector' => '*', 101 ); 102 } 103 104 if ( 105 ( ! isset( $supports['customClassName'] ) || false !== $supports['customClassName'] ) && 106 empty( $attributes['className']['type'] ) 107 ) { 108 $args['attributes']['className'] = array( 109 'type' => 'string', 110 ); 111 } 112 113 return $args; 114 } 115 add_filter( 'register_block_type_args', 'register_block_type_add_default_attributes' ); -
tests/phpunit/tests/blocks/block.php
74 74 'default' => 10, 75 75 ), 76 76 ), 77 'supports' => array( 78 'customClassName' => false, 79 ), 77 80 ); 78 81 $this->registry->register( 'core/example', $block_type_settings ); 79 82 -
tests/phpunit/tests/blocks/register.php
294 294 $this->assertEqualSets( array( 'alert', 'message' ), $result->keywords ); 295 295 $this->assertEquals( 296 296 array( 297 'message' => array(297 'message' => array( 298 298 'type' => 'string', 299 299 'source' => 'html', 300 300 'selector' => '.message', 301 301 ), 302 'align' => array( 303 'type' => 'string', 304 ), 305 'className' => array( 306 'type' => 'string', 307 ), 302 308 ), 303 309 $result->attributes 304 310 ); … … 398 404 $block_type = $registry->get_registered( 'core/test-filtered' ); 399 405 $this->assertEquals( 'boolean', $block_type->attributes['core/test-filtered']['type'] ); 400 406 } 407 408 /** 409 * @ticket 49615 410 */ 411 function test_register_add_default_attributes_filter() { 412 $block_name = 'core/test-attributes'; 413 register_block_type( 414 $block_name, 415 array( 416 'attributes' => array(), 417 'supports' => array( 418 'align' => true, 419 'anchor' => true, 420 ), 421 ) 422 ); 423 424 $registry = WP_Block_Type_Registry::get_instance(); 425 $block_type = $registry->get_registered( $block_name ); 426 $this->assertEquals( 427 array( 428 'align' => array( 429 'type' => 'string', 430 ), 431 'anchor' => array( 432 'type' => 'string', 433 'source' => 'attribute', 434 'attribute' => 'id', 435 'selector' => '*', 436 ), 437 'className' => array( 438 'type' => 'string', 439 ), 440 ), 441 $block_type->attributes 442 ); 443 } 444 401 445 }