Skip to content

Commit

Permalink
Editor: Default attribute value not used with `get_block_wrapper_attr…
Browse files Browse the repository at this point in the history
…ibutes`

Ensures that the default values defined in the schema for block attributes are used when rendering the output of the block with `get_block_wrapper_attributes` helper.

Props gziolo, jonsurrell, youknowriad, ryelle.
Fixes #62114.



git-svn-id: https://develop.svn.wordpress.org/trunk@59093 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
gziolo committed Sep 26, 2024
1 parent b071c28 commit 7d0e751
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-block-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function apply_block_supports() {
}

$block_attributes = array_key_exists( 'attrs', self::$block_to_render ) && is_array( self::$block_to_render['attrs'] )
? self::$block_to_render['attrs']
? $block_type->prepare_attributes_for_render( self::$block_to_render['attrs'] )
: array();

$output = array();
Expand Down
50 changes: 50 additions & 0 deletions tests/phpunit/tests/blocks/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,56 @@ public function test_dynamic_block_rendering() {
);
}

/**
* @ticket 62114
*/
public function test_dynamic_block_with_default_attributes() {
$settings = array(
'attributes' => array(
'content' => array(
'type' => 'string',
'default' => 'Default content.',
),
'align' => array(
'type' => 'string',
'default' => 'full',
),
'backgroundColor' => array(
'type' => 'string',
'default' => 'blueberry-1',
),
'textColor' => array(
'type' => 'string',
'default' => 'white',
),
),
'supports' => array(
'align' => array( 'wide', 'full' ),
'color' => array(
'background' => true,
'text' => true,
),
),
'render_callback' => function ( $attributes ) {
return '<p ' . get_block_wrapper_attributes() . '>' . $attributes['content'] . '</p>';
},
);
register_block_type( 'core/dynamic', $settings );

$post_content =
'before' .
'<!-- wp:core/dynamic --><!-- /wp:core/dynamic -->' .
'after';

$updated_post_content = do_blocks( $post_content );
$this->assertSame(
$updated_post_content,
'before' .
'<p class="alignfull wp-block-dynamic has-text-color has-white-color has-background has-blueberry-1-background-color">Default content.</p>' .
'after'
);
}

/**
* @ticket 45109
*/
Expand Down

0 comments on commit 7d0e751

Please sign in to comment.