WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/21/2018 02:43:33 PM (11 months ago)
Author:
danielbachhuber
Message:

REST API: Preserve unknown, respect null in server-side block rendering.

  • Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to render_callback. Notably, this resolves an issue where render_callback cannot receive a block's align and customClassName attribute values, since these are defined as a client-side filter.
  • Validates null as a proper value in its own right. Previously, a client implementation of a block could track {"attribute":null} as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.

Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/class-wp-block-type.php

    r43742 r43918  
    124124    /**
    125125     * Validates attributes against the current block schema, populating
    126      * defaulted and missing values, and omitting unknown attributes.
     126     * defaulted and missing values.
    127127     *
    128128     * @since 5.0.0
     
    132132     */
    133133    public function prepare_attributes_for_render( $attributes ) {
     134        // If there are no attribute definitions for the block type, skip
     135        // processing and return vebatim.
    134136        if ( ! isset( $this->attributes ) ) {
    135137            return $attributes;
    136138        }
    137139
    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;
    148145            }
    149146
    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 ] );
    152156            }
    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;
    158169    }
    159170
Note: See TracChangeset for help on using the changeset viewer.