Changeset 43918 for branches/5.0
- Timestamp:
- 11/21/2018 02:43:33 PM (6 years ago)
- Location:
- branches/5.0
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/5.0/src/wp-includes/class-wp-block-type.php
r43742 r43918 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 -
branches/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php
r43806 r43918 62 62 'additionalProperties' => false, 63 63 'properties' => $block_type->get_attributes(), 64 'default' => array(), 64 65 ), 65 66 'post_id' => array( -
branches/5.0/tests/phpunit/tests/blocks/block-type.php
r43770 r43918 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 -
branches/5.0/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php
r43805 r43918 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 -
branches/5.0/tests/qunit/fixtures/wp-api-generated.js
r43897 r43918 4315 4315 "attributes": { 4316 4316 "required": false, 4317 "default": [], 4317 4318 "description": "Attributes for core/block block", 4318 4319 "type": "object" … … 4354 4355 "attributes": { 4355 4356 "required": false, 4357 "default": [], 4356 4358 "description": "Attributes for core/latest-comments block", 4357 4359 "type": "object" … … 4393 4395 "attributes": { 4394 4396 "required": false, 4397 "default": [], 4395 4398 "description": "Attributes for core/archives block", 4396 4399 "type": "object" … … 4432 4435 "attributes": { 4433 4436 "required": false, 4437 "default": [], 4434 4438 "description": "Attributes for core/categories block", 4435 4439 "type": "object" … … 4471 4475 "attributes": { 4472 4476 "required": false, 4477 "default": [], 4473 4478 "description": "Attributes for core/latest-posts block", 4474 4479 "type": "object" … … 4510 4515 "attributes": { 4511 4516 "required": false, 4517 "default": [], 4512 4518 "description": "Attributes for core/shortcode block", 4513 4519 "type": "object"
Note: See TracChangeset
for help on using the changeset viewer.