WordPress.org

Make WordPress Core


Ignore:
Timestamp:
12/17/2018 05:21:05 PM (3 years ago)
Author:
desrosj
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.

Merges [43917] to trunk.

See #45145 for the patch, #45098 for the original ticket.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php

    r44150 r44268  
    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}
Note: See TracChangeset for help on using the changeset viewer.