Make WordPress Core

Changeset 53268


Ignore:
Timestamp:
04/26/2022 09:48:20 AM (4 years ago)
Author:
gziolo
Message:

Editor: Register 'lock' attribute for every block on the server

Backports changes from https://github.com/WordPress/gutenberg/pull/40468.

The lock attribute needs to be supported by every block, but currently, it is only done on the client site. As a result, it was causing block rendered API requests to fail when blocks are locked.

Props mamaduka, peterwilsoncc.
See #55567.

Location:
trunk
Files:
6 edited

Legend:

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

    r53084 r53268  
    204204         */
    205205        public $style = null;
     206
     207        /**
     208         * Attributes supported by every block.
     209         *
     210         * @since 6.0.0
     211         * @var array
     212         */
     213        const GLOBAL_ATTRIBUTES = array(
     214                'lock' => array( 'type' => 'object' ),
     215        );
    206216
    207217        /**
     
    356366                $args['name'] = $this->name;
    357367
     368                // Setup attributes if needed.
     369                if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
     370                        $args['attributes'] = array();
     371                }
     372
     373                // Register core attributes.
     374                foreach ( static::GLOBAL_ATTRIBUTES as $attr_key => $attr_schema ) {
     375                        if ( ! array_key_exists( $attr_key, $args['attributes'] ) ) {
     376                                $args['attributes'][ $attr_key ] = $attr_schema;
     377                        }
     378                }
     379
    358380                /**
    359381                 * Filters the arguments for registering a block type.
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r53084 r53268  
    845845                                'description' => '',
    846846                                'icon'        => 'text',
     847                                'attributes'  => array(
     848                                        'lock' => array( 'type' => 'object' ),
     849                                ),
    847850                                'usesContext' => array(),
    848851                                'category'    => 'common',
  • trunk/tests/phpunit/tests/blocks/register.php

    r53091 r53268  
    393393                                        'selector' => '.message',
    394394                                ),
     395                                'lock'    => array( 'type' => 'object' ),
    395396                        ),
    396397                        $result->attributes
  • trunk/tests/phpunit/tests/blocks/wpBlock.php

    r53152 r53268  
    8383
    8484                $this->assertInstanceOf( WP_Block_Type::class, $block->block_type );
    85                 $this->assertSame(
    86                         $block_type_settings['attributes'],
     85                $this->assertSameSetsWithIndex(
     86                        array(
     87                                'defaulted' => array(
     88                                        'type'    => 'number',
     89                                        'default' => 10,
     90                                ),
     91                                'lock'      => array( 'type' => 'object' ),
     92                        ),
    8793                        $block->block_type->attributes
    8894                );
  • trunk/tests/phpunit/tests/blocks/wpBlockType.php

    r52010 r53268  
    8383                $this->assertSame( $args['render_callback'], $block_type->render_callback );
    8484                $this->assertSame( $args['foo'], $block_type->foo );
     85        }
     86
     87        /*
     88         * @ticket 55567
     89         * @covers WP_Block_Type::set_props
     90         */
     91        public function test_core_attributes() {
     92                $block_type = new WP_Block_Type( 'core/fake', array() );
     93
     94                $this->assertSameSetsWithIndex(
     95                        array(
     96                                'lock' => array( 'type' => 'object' ),
     97                        ),
     98                        $block_type->attributes
     99                );
     100        }
     101
     102        /*
     103         * @ticket 55567
     104         * @covers WP_Block_Type::set_props
     105         */
     106        public function test_core_attributes_matches_custom() {
     107                $block_type = new WP_Block_Type(
     108                        'core/fake',
     109                        array(
     110                                'attributes' => array(
     111                                        'lock' => array(
     112                                                'type' => 'string',
     113                                        ),
     114                                ),
     115                        )
     116                );
     117
     118                // Backward compatibility: Don't override attributes with the same name.
     119                $this->assertSameSetsWithIndex(
     120                        array(
     121                                'lock' => array( 'type' => 'string' ),
     122                        ),
     123                        $block_type->attributes
     124                );
    85125        }
    86126
  • trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php

    r53084 r53268  
    244244                $this->assertNull( $data['style'] );
    245245                $this->assertSameSets( array(), $data['provides_context'] );
    246                 $this->assertSameSets( array(), $data['attributes'] );
     246                $this->assertSameSetsWithIndex(
     247                        array(
     248                                'lock' => array( 'type' => 'object' ),
     249                        ),
     250                        $data['attributes']
     251                );
    247252                $this->assertSameSets( array( 'invalid_uses_context' ), $data['uses_context'] );
    248253                $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] );
     
    300305                $this->assertNull( $data['editor_style'] );
    301306                $this->assertNull( $data['style'] );
    302                 $this->assertSameSets( array(), $data['attributes'] );
     307                $this->assertSameSetsWithIndex(
     308                        array(
     309                                'lock' => array( 'type' => 'object' ),
     310                        ),
     311                        $data['attributes']
     312                );
    303313                $this->assertSameSets( array(), $data['provides_context'] );
    304314                $this->assertSameSets( array(), $data['uses_context'] );
Note: See TracChangeset for help on using the changeset viewer.