Changeset 44269
- Timestamp:
- 12/17/2018 05:59:44 PM (6 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/5.0 merged: 43918
- Property svn:mergeinfo changed
-
trunk/src/wp-includes/class-wp-block-type.php
r44108 r44269 124 124 /** 125 125 * Validates attributes against the current block schema, populating 126 * defaulted and missing values , and omitting unknown attributes.126 * defaulted and missing values. 127 127 * 128 128 * @since 5.0.0 … … 132 132 */ 133 133 public function prepare_attributes_for_render( $attributes ) { 134 // If there are no attribute definitions for the block type, skip 135 // processing and return vebatim. 134 136 if ( ! isset( $this->attributes ) ) { 135 137 return $attributes; 136 138 } 137 139 138 $prepared_attributes = array(); 139 140 foreach ( $this->attributes as $attribute_name => $schema ) { 141 $value = null; 142 143 if ( isset( $attributes[ $attribute_name ] ) ) { 144 $is_valid = rest_validate_value_from_schema( $attributes[ $attribute_name ], $schema ); 145 if ( ! is_wp_error( $is_valid ) ) { 146 $value = rest_sanitize_value_from_schema( $attributes[ $attribute_name ], $schema ); 147 } 140 foreach ( $attributes as $attribute_name => $value ) { 141 // If the attribute is not defined by the block type, it cannot be 142 // validated. 143 if ( ! isset( $this->attributes[ $attribute_name ] ) ) { 144 continue; 148 145 } 149 146 150 if ( is_null( $value ) && isset( $schema['default'] ) ) { 151 $value = $schema['default']; 147 $schema = $this->attributes[ $attribute_name ]; 148 149 // Validate value by JSON schema. An invalid value should revert to 150 // its default, if one exists. This occurs by virtue of the missing 151 // attributes loop immediately following. If there is not a default 152 // assigned, the attribute value should remain unset. 153 $is_valid = rest_validate_value_from_schema( $value, $schema ); 154 if ( is_wp_error( $is_valid ) ) { 155 unset( $attributes[ $attribute_name ] ); 152 156 } 153 154 $prepared_attributes[ $attribute_name ] = $value; 155 } 156 157 return $prepared_attributes; 157 } 158 159 // Populate values of any missing attributes for which the block type 160 // defines a default. 161 $missing_schema_attributes = array_diff_key( $this->attributes, $attributes ); 162 foreach ( $missing_schema_attributes as $attribute_name => $schema ) { 163 if ( isset( $schema['default'] ) ) { 164 $attributes[ $attribute_name ] = $schema['default']; 165 } 166 } 167 168 return $attributes; 158 169 } 159 170 -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php
r44150 r44269 62 62 'additionalProperties' => false, 63 63 'properties' => $block_type->get_attributes(), 64 'default' => array(), 64 65 ), 65 66 'post_id' => array( -
trunk/tests/phpunit/tests/blocks/block-type.php
r44127 r44269 169 169 'wrongTypeDefaulted' => 5, 170 170 /* missingDefaulted */ 171 'undefined' => 'omit', 171 'undefined' => 'include', 172 'intendedNull' => null, 172 173 ); 173 174 … … 190 191 'default' => 'define', 191 192 ), 193 'intendedNull' => array( 194 'type' => array( 'string', 'null' ), 195 'default' => 'wrong', 196 ), 192 197 ), 193 198 ) … … 199 204 array( 200 205 'correct' => 'include', 201 'wrongType' => null,206 /* wrongType */ 202 207 'wrongTypeDefaulted' => 'defaulted', 203 208 'missingDefaulted' => 'define', 209 'undefined' => 'include', 210 'intendedNull' => null, 204 211 ), 205 212 $prepared_attributes 206 213 ); 214 } 215 216 /** 217 * @ticket 45145 218 */ 219 function test_prepare_attributes_none_defined() { 220 $attributes = array( 'exists' => 'keep' ); 221 222 $block_type = new WP_Block_Type( 'core/dummy', array() ); 223 224 $prepared_attributes = $block_type->prepare_attributes_for_render( $attributes ); 225 226 $this->assertEquals( $attributes, $prepared_attributes ); 207 227 } 208 228 -
trunk/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php
r44150 r44269 320 320 $defaults = array(); 321 321 foreach ( $block_type->attributes as $key => $attribute ) { 322 $defaults[ $key ] = isset( $attribute['default'] ) ? $attribute['default'] : null; 322 if ( isset( $attribute['default'] ) ) { 323 $defaults[ $key ] = $attribute['default']; 324 } 323 325 } 324 326 -
trunk/tests/qunit/fixtures/wp-api-generated.js
r44250 r44269 4318 4318 "attributes": { 4319 4319 "required": false, 4320 "default": [], 4320 4321 "description": "Attributes for core/block block", 4321 4322 "type": "object" … … 4357 4358 "attributes": { 4358 4359 "required": false, 4360 "default": [], 4359 4361 "description": "Attributes for core/latest-comments block", 4360 4362 "type": "object" … … 4396 4398 "attributes": { 4397 4399 "required": false, 4400 "default": [], 4398 4401 "description": "Attributes for core/archives block", 4399 4402 "type": "object" … … 4435 4438 "attributes": { 4436 4439 "required": false, 4440 "default": [], 4437 4441 "description": "Attributes for core/categories block", 4438 4442 "type": "object" … … 4474 4478 "attributes": { 4475 4479 "required": false, 4480 "default": [], 4476 4481 "description": "Attributes for core/latest-posts block", 4477 4482 "type": "object" … … 4513 4518 "attributes": { 4514 4519 "required": false, 4520 "default": [], 4515 4521 "description": "Attributes for core/shortcode block", 4516 4522 "type": "object"
Note: See TracChangeset
for help on using the changeset viewer.