diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php
index 3388078d05..926bcec4b2 100644
a
|
b
|
class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { |
54 | 54 | 'description' => __( 'Attributes for the block' ), |
55 | 55 | 'type' => 'object', |
56 | 56 | 'default' => array(), |
| 57 | 'sanitize_callback' => static function ( $value, $request ) { |
| 58 | $block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] ); |
| 59 | |
| 60 | if ( ! $block ) { |
| 61 | // This will get rejected in ::get_item(). |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | $schema = array( |
| 66 | 'type' => 'object', |
| 67 | 'properties' => $block->get_attributes(), |
| 68 | 'additionalProperties' => false, |
| 69 | ); |
| 70 | |
| 71 | return rest_sanitize_value_from_schema( $value, $schema ); |
| 72 | }, |
57 | 73 | 'validate_callback' => static function ( $value, $request ) { |
58 | 74 | $block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] ); |
59 | 75 | |
diff --git a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php
index 423d4a4947..c047fa268e 100644
a
|
b
|
class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca |
55 | 55 | */ |
56 | 56 | protected static $non_dynamic_block_name = 'core/non-dynamic'; |
57 | 57 | |
| 58 | /** |
| 59 | * Dynamic block with boolean attributes block name. |
| 60 | * |
| 61 | * @since 5.5.0 |
| 62 | * |
| 63 | * @var string |
| 64 | */ |
| 65 | protected static $dynamic_block_with_boolean_attributes_block_name = 'core/dynamic-block-with-boolean-attributes'; |
| 66 | |
58 | 67 | /** |
59 | 68 | * Test API user's ID. |
60 | 69 | * |
… |
… |
class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca |
127 | 136 | $this->register_test_block(); |
128 | 137 | $this->register_post_context_test_block(); |
129 | 138 | $this->register_non_dynamic_block(); |
| 139 | $this->register_dynamic_block_with_boolean_attributes(); |
130 | 140 | parent::setUp(); |
131 | 141 | } |
132 | 142 | |
… |
… |
class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca |
139 | 149 | WP_Block_Type_Registry::get_instance()->unregister( self::$block_name ); |
140 | 150 | WP_Block_Type_Registry::get_instance()->unregister( self::$context_block_name ); |
141 | 151 | WP_Block_Type_Registry::get_instance()->unregister( self::$non_dynamic_block_name ); |
| 152 | WP_Block_Type_Registry::get_instance()->unregister( self::$dynamic_block_with_boolean_attributes_block_name ); |
142 | 153 | parent::tearDown(); |
143 | 154 | } |
144 | 155 | |
… |
… |
class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca |
195 | 206 | register_block_type( self::$non_dynamic_block_name ); |
196 | 207 | } |
197 | 208 | |
| 209 | /** |
| 210 | * Registers the dynamic with boolean attributes block name. |
| 211 | * |
| 212 | * @since 5.5.0 |
| 213 | */ |
| 214 | protected function register_dynamic_block_with_boolean_attributes() { |
| 215 | register_block_type( |
| 216 | self::$dynamic_block_with_boolean_attributes_block_name, |
| 217 | array( |
| 218 | 'attributes' => array( |
| 219 | 'boolean_true_attribute' => array( |
| 220 | 'type' => 'boolean', |
| 221 | 'default' => true, |
| 222 | ), |
| 223 | 'boolean_false_attribute' => array( |
| 224 | 'type' => 'boolean', |
| 225 | 'default' => false, |
| 226 | ), |
| 227 | ), |
| 228 | 'render_callback' => array( $this, 'render_test_block' ), |
| 229 | ) |
| 230 | ); |
| 231 | } |
| 232 | |
198 | 233 | /** |
199 | 234 | * Test render callback. |
200 | 235 | * |
… |
… |
class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca |
522 | 557 | $this->assertErrorResponse( 'block_invalid', $response, 404 ); |
523 | 558 | } |
524 | 559 | |
| 560 | /** |
| 561 | * @ticket 50620 |
| 562 | */ |
| 563 | public function test_get_sanitized_attributes_for_dynamic_block_with_boolean_attributes() { |
| 564 | wp_set_current_user( self::$user_id ); |
| 565 | |
| 566 | $request = new WP_REST_Request( 'GET', self::$rest_api_route . self::$dynamic_block_with_boolean_attributes_block_name ); |
| 567 | |
| 568 | $attributes = array( |
| 569 | 'boolean_true_attribute' => 'true', |
| 570 | 'boolean_false_attribute' => 'false', |
| 571 | ); |
| 572 | |
| 573 | $expected_attributes = $attributes; |
| 574 | $expected_attributes['boolean_true_attribute'] = filter_var( $attributes['boolean_true_attribute'], FILTER_VALIDATE_BOOLEAN ); |
| 575 | $expected_attributes['boolean_false_attribute'] = filter_var( $attributes['boolean_false_attribute'], FILTER_VALIDATE_BOOLEAN ); |
| 576 | |
| 577 | $request->set_param( 'context', 'edit' ); |
| 578 | $request->set_param( 'attributes', $attributes ); |
| 579 | $response = rest_get_server()->dispatch( $request ); |
| 580 | $this->assertEquals( 200, $response->get_status() ); |
| 581 | $data = $response->get_data(); |
| 582 | |
| 583 | $this->assertEquals( $expected_attributes, json_decode( $data['rendered'], true ) ); |
| 584 | } |
| 585 | |
525 | 586 | /** |
526 | 587 | * Get item schema. |
527 | 588 | * |