Make WordPress Core

Changeset 43918


Ignore:
Timestamp:
11/21/2018 02:43:33 PM (8 years 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.

Location:
branches/5.0
Files:
5 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
  • branches/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php

    r43806 r43918  
    6262                                                                'additionalProperties' => false,
    6363                                                                'properties'           => $block_type->get_attributes(),
     64                                                                'default'              => array(),
    6465                                                        ),
    6566                                                        'post_id'    => array(
  • branches/5.0/tests/phpunit/tests/blocks/block-type.php

    r43770 r43918  
    169169                        'wrongTypeDefaulted' => 5,
    170170                        /* missingDefaulted */
    171                         'undefined'          => 'omit',
     171                        'undefined'          => 'include',
     172                        'intendedNull'       => null,
    172173                );
    173174
    … …  
    190191                                                'default' => 'define',
    191192                                        ),
     193                                        'intendedNull'       => array(
     194                                                'type'    => array( 'string', 'null' ),
     195                                                'default' => 'wrong',
     196                                        ),
    192197                                ),
    193198                        )
    … …  
    199204                        array(
    200205                                'correct'            => 'include',
    201                                 'wrongType'          => null,
     206                                /* wrongType */
    202207                                'wrongTypeDefaulted' => 'defaulted',
    203208                                'missingDefaulted'   => 'define',
     209                                'undefined'          => 'include',
     210                                'intendedNull'       => null,
    204211                        ),
    205212                        $prepared_attributes
    206213                );
     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 );
    207227        }
    208228
  • branches/5.0/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php

    r43805 r43918  
    320320                $defaults   = array();
    321321                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                        }
    323325                }
    324326
  • branches/5.0/tests/qunit/fixtures/wp-api-generated.js

    r43897 r43918  
    43154315                        "attributes": {
    43164316                            "required": false,
     4317                            "default": [],
    43174318                            "description": "Attributes for core/block block",
    43184319                            "type": "object"
    … …  
    43544355                        "attributes": {
    43554356                            "required": false,
     4357                            "default": [],
    43564358                            "description": "Attributes for core/latest-comments block",
    43574359                            "type": "object"
    … …  
    43934395                        "attributes": {
    43944396                            "required": false,
     4397                            "default": [],
    43954398                            "description": "Attributes for core/archives block",
    43964399                            "type": "object"
    … …  
    44324435                        "attributes": {
    44334436                            "required": false,
     4437                            "default": [],
    44344438                            "description": "Attributes for core/categories block",
    44354439                            "type": "object"
    … …  
    44714475                        "attributes": {
    44724476                            "required": false,
     4477                            "default": [],
    44734478                            "description": "Attributes for core/latest-posts block",
    44744479                            "type": "object"
    … …  
    45104515                        "attributes": {
    45114516                            "required": false,
     4517                            "default": [],
    45124518                            "description": "Attributes for core/shortcode block",
    45134519                            "type": "object"
Note: See TracChangeset for help on using the changeset viewer.