Changeset 43918 for branches/5.0/src/wp-includes/class-wp-block-type.php
- Timestamp:
- 11/21/2018 02:43:33 PM (6 years ago)
- File:
-
- 1 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
Note: See TracChangeset
for help on using the changeset viewer.