WordPress.org

Make WordPress Core

Changeset 43917


Ignore:
Timestamp:
11/21/2018 02:34:09 PM (10 months ago)
Author:
danielbachhuber
Message:

REST API: Always include title.raw/content.raw for Blocks in context=view.

Demarcations for reusable blocks are always expected to be accessible by clients.

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

Location:
branches/5.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php

    r43805 r43917  
    3737        return parent::check_read_permission( $post );
    3838    }
     39
     40    /**
     41     * Filters a response based on the context defined in the schema.
     42     *
     43     * @since 5.0.0
     44     *
     45     * @param array  $data    Response data to fiter.
     46     * @param string $context Context defined in the schema.
     47     * @return array Filtered response.
     48     */
     49    public function filter_response_by_context( $data, $context ) {
     50        $data = parent::filter_response_by_context( $data, $context );
     51
     52        /*
     53         * Remove `title.rendered` and `content.rendered` from the response. It
     54         * doesn't make sense for a reusable block to have rendered content on its
     55         * own, since rendering a block requires it to be inside a post or a page.
     56         */
     57        unset( $data['title']['rendered'] );
     58        unset( $data['content']['rendered'] );
     59
     60        return $data;
     61    }
     62
     63    /**
     64     * Retrieves the block's schema, conforming to JSON Schema.
     65     *
     66     * @since 5.0.0
     67     *
     68     * @return array Item schema data.
     69     */
     70    public function get_item_schema() {
     71        $schema = parent::get_item_schema();
     72
     73        /*
     74         * Allow all contexts to access `title.raw` and `content.raw`. Clients always
     75         * need the raw markup of a reusable block to do anything useful, e.g. parse
     76         * it or display it in an editor.
     77         */
     78        $schema['properties']['title']['properties']['raw']['context']   = array( 'view', 'edit' );
     79        $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );
     80
     81        /*
     82         * Remove `title.rendered` and `content.rendered` from the schema. It doesn’t
     83         * make sense for a reusable block to have rendered content on its own, since
     84         * rendering a block requires it to be inside a post or a page.
     85         */
     86        unset( $schema['properties']['title']['properties']['rendered'] );
     87        unset( $schema['properties']['content']['properties']['rendered'] );
     88
     89        return $schema;
     90    }
     91
    3992}
  • branches/5.0/tests/phpunit/tests/rest-api/rest-blocks-controller.php

    r43805 r43917  
    3030
    3131    /**
    32      * Our fake user's ID.
    33      *
    34      * @since 5.0.0
    35      *
    36      * @var int
    37      */
    38     protected static $user_id;
     32     * Our fake user IDs, keyed by their role.
     33     *
     34     * @since 5.0.0
     35     *
     36     * @var array
     37     */
     38    protected static $user_ids;
    3939
    4040    /**
     
    5151                'post_status'  => 'publish',
    5252                'post_title'   => 'My cool block',
    53                 'post_content' => '<!-- wp:core/paragraph --><p>Hello!</p><!-- /wp:core/paragraph -->',
     53                'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
    5454            )
    5555        );
    5656
    57         self::$user_id = $factory->user->create(
    58             array(
    59                 'role' => 'editor',
    60             )
     57        self::$user_ids = array(
     58            'editor'      => $factory->user->create( array( 'role' => 'editor' ) ),
     59            'author'      => $factory->user->create( array( 'role' => 'author' ) ),
     60            'contributor' => $factory->user->create( array( 'role' => 'contributor' ) ),
    6161        );
    6262    }
     
    7070        wp_delete_post( self::$post_id );
    7171
    72         self::delete_user( self::$user_id );
     72        foreach ( self::$user_ids as $user_id ) {
     73            self::delete_user( $user_id );
     74        }
    7375    }
    7476
     
    115117    public function test_capabilities( $action, $role, $expected_status ) {
    116118        if ( $role ) {
    117             $user_id = $this->factory->user->create( array( 'role' => $role ) );
     119            $user_id = self::$user_ids[ $role ];
    118120            wp_set_current_user( $user_id );
    119121        } else {
     
    127129                    array(
    128130                        'title'   => 'Test',
    129                         'content' => '<!-- wp:core/paragraph --><p>Test</p><!-- /wp:core/paragraph -->',
     131                        'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->',
    130132                    )
    131133                );
     
    150152                        'post_status'  => 'publish',
    151153                        'post_title'   => 'My cool block',
    152                         'post_content' => '<!-- wp:core/paragraph --><p>Hello!</p><!-- /wp:core/paragraph -->',
     154                        'post_content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
    153155                        'post_author'  => $user_id,
    154156                    )
     
    159161                    array(
    160162                        'title'   => 'Test',
    161                         'content' => '<!-- wp:core/paragraph --><p>Test</p><!-- /wp:core/paragraph -->',
     163                        'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->',
    162164                    )
    163165                );
     
    180182                    array(
    181183                        'title'   => 'Test',
    182                         'content' => '<!-- wp:core/paragraph --><p>Test</p><!-- /wp:core/paragraph -->',
     184                        'content' => '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->',
    183185                    )
    184186                );
     
    197199                $this->fail( "'$action' is not a valid action." );
    198200        }
    199 
    200         if ( isset( $user_id ) ) {
    201             self::delete_user( $user_id );
    202         }
     201    }
     202
     203    /**
     204     * Check that the raw title and content of a block can be accessed when there
     205     * is no set schema, and that the rendered content of a block is not included
     206     * in the response.
     207     */
     208    public function test_content() {
     209        wp_set_current_user( self::$user_ids['author'] );
     210
     211        $request  = new WP_REST_Request( 'GET', '/wp/v2/blocks/' . self::$post_id );
     212        $response = rest_get_server()->dispatch( $request );
     213        $data     = $response->get_data();
     214
     215        $this->assertEquals(
     216            array(
     217                'raw' => 'My cool block',
     218            ),
     219            $data['title']
     220        );
     221        $this->assertEquals(
     222            array(
     223                'raw'       => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->',
     224                'protected' => false,
     225            ),
     226            $data['content']
     227        );
    203228    }
    204229}
Note: See TracChangeset for help on using the changeset viewer.