Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.