Add set_attribute method to Block class (#45523)

This commit is contained in:
Nathan Silveira 2024-03-13 10:13:02 -03:00 committed by GitHub
parent 50beebe3e5
commit 8bef21dbc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Add set_attribute method to Block class

View File

@ -70,6 +70,14 @@ interface BlockInterface {
*/
public function set_attributes( array $attributes );
/**
* Set a block attribute value without replacing the entire attributes object.
*
* @param string $key The attribute key.
* @param mixed $value The attribute value.
*/
public function set_attribute( string $key, $value );
/**
* Get the parent container that the block belongs to.
*/

View File

@ -201,6 +201,16 @@ class AbstractBlock implements BlockInterface {
$this->attributes = $attributes;
}
/**
* Set a block attribute value without replacing the entire attributes object.
*
* @param string $key The attribute key.
* @param mixed $value The attribute value.
*/
public function set_attribute( string $key, $value ) {
$this->attributes[ $key ] = $value;
}
/**
* Get the template that this block belongs to.
*/

View File

@ -657,4 +657,21 @@ class BlockTest extends WC_Unit_Test_Case {
'Failed asserting that the inner blocks are sorted by order.'
);
}
/**
* Test for set_attribute method.
*/
public function test_set_attribute() {
$template = new BlockTemplate();
$block = new Block(
array(
'blockName' => 'test-block-name',
),
$template
);
$block->set_attribute( 'test-attr', 'test-value' );
$this->assertSame( 'test-value', $block->get_attributes()['test-attr'] );
}
}