Make WordPress Core

Ticket #47620: 47620.2.diff

File 47620.2.diff, 12.1 KB (added by spacedmonkey, 5 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2/**
     3 * REST API: WP_REST_Block_Types_Controller class
     4 *
     5 * @since      5.4.0
     6 * @subpackage REST_API
     7 * @package    WordPress
     8 */
     9
     10/**
     11 * Core class used to access block types via the REST API.
     12 *
     13 * @since 5.4.0
     14 *
     15 * @see   WP_REST_Controller
     16 */
     17class WP_REST_Block_Types_Controller extends WP_REST_Controller {
     18
     19        /**
     20         * Constructor.
     21         *
     22         * @since 5.4.0
     23         */
     24        public function __construct() {
     25                $this->namespace = 'wp/v2';
     26                $this->rest_base = 'block-types';
     27        }
     28
     29        /**
     30         * Registers the routes for the objects of the controller.
     31         *
     32         * @since 5.4.0
     33         *
     34         * @see   register_rest_route()
     35         */
     36        public function register_routes() {
     37
     38                register_rest_route(
     39                        $this->namespace,
     40                        '/' . $this->rest_base,
     41                        array(
     42                                array(
     43                                        'methods'             => WP_REST_Server::READABLE,
     44                                        'callback'            => array( $this, 'get_items' ),
     45                                        'permission_callback' => array( $this, 'get_items_permissions_check' ),
     46                                        'args'                => $this->get_collection_params(),
     47                                ),
     48                                'schema' => array( $this, 'get_public_item_schema' ),
     49                        )
     50                );
     51
     52                register_rest_route(
     53                        $this->namespace,
     54                        '/' . $this->rest_base . '/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)',
     55                        array(
     56                                'args'   => array(
     57                                        'name'      => array(
     58                                                'description' => __( '' ),
     59                                                'type'        => 'string',
     60                                        ),
     61                                        'namespace' => array(
     62                                                'description' => __( '' ),
     63                                                'type'        => 'string',
     64                                        ),
     65                                ),
     66                                array(
     67                                        'methods'             => WP_REST_Server::READABLE,
     68                                        'callback'            => array( $this, 'get_item' ),
     69                                        'permission_callback' => array( $this, 'get_item_permissions_check' ),
     70                                        'args'                => array(
     71                                                'context' => $this->get_context_param( array( 'default' => 'view' ) ),
     72                                        ),
     73                                ),
     74                                'schema' => array( $this, 'get_public_item_schema' ),
     75                        )
     76                );
     77        }
     78
     79        /**
     80         * Checks whether a given request has permission to read post block types.
     81         *
     82         * @param WP_REST_Request $request Full details about the request.
     83         *
     84         * @since 5.4.0
     85         *
     86         * @return WP_Error|bool True if the request has read access, WP_Error object otherwise.
     87         */
     88        public function get_items_permissions_check( $request) {
     89                if ( ! current_user_can( 'edit_posts' ) ) {
     90                        return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage block types.' ), array( 'status' => rest_authorization_required_code() ) );
     91                }
     92
     93                return true;
     94        }
     95
     96        /**
     97         * Retrieves all post block types, depending on user context.
     98         *
     99         * @param WP_REST_Request $request Full details about the request.
     100         *
     101         * @since 5.4.0
     102         *
     103         * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
     104         */
     105        public function get_items( $request ) {
     106                $data        = array();
     107                $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
     108
     109                // Retrieve the list of registered collection query parameters.
     110                $registered = $this->get_collection_params();
     111                $namespace = '';
     112                if ( isset( $registered['namespace'] ) && ! empty( $request['namespace'] ) ) {
     113                        $namespace = $request['namespace'];
     114                }
     115
     116                foreach ( $block_types as $slug => $obj ) {
     117                        $ret = $this->check_read_permission( $obj );
     118
     119                        if ( ! $ret ) {
     120                                continue;
     121                        }
     122
     123                        $block_type         = $this->prepare_item_for_response( $obj, $request );
     124
     125                        if ( $namespace ){
     126                                $pieces = explode('/', $obj->name);
     127                                $block_namespace = array_shift($pieces);
     128                                if( $namespace !== $block_namespace ){
     129                                        continue;
     130                                }
     131                        }
     132
     133                        $data[ $obj->name ] = $this->prepare_response_for_collection( $block_type );
     134                }
     135
     136                return rest_ensure_response( $data );
     137        }
     138
     139        /**
     140         * Checks if a given request has access to read a block type.
     141         *
     142         * @param WP_REST_Request $request Full details about the request.
     143         *
     144         * @since 5.4.0
     145         *
     146         * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise.
     147         */
     148        public function get_item_permissions_check( $request ) {
     149                $block_name = $request['namespace'] . '/' . $request['name'];
     150                $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
     151
     152                if ( empty( $block_type ) ) {
     153                        return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.' ), array( 'status' => 404 ) );
     154                }
     155
     156                $check = $this->check_read_permission( $block_type );
     157
     158                if ( ! $check ) {
     159                        return new WP_Error( 'rest_cannot_read_block_type', __( 'Cannot view block type.' ), array( 'status' => rest_authorization_required_code() ) );
     160                }
     161
     162                return true;
     163        }
     164
     165        /**
     166         * Checks whether a given block type should be visible.
     167         *
     168         * @param object $block_type block type.
     169         *
     170         * @since 5.4.0
     171         *
     172         * @return WP_Error|bool True if the block type is visible, otherwise false.
     173         */
     174        protected function check_read_permission( $block_type ) {
     175                if ( ! current_user_can( 'edit_posts' ) ) {
     176                        return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage block types.' ), array( 'status' => rest_authorization_required_code() ) );
     177                }
     178
     179                return true;
     180        }
     181
     182        /**
     183         * Retrieves a specific block type.
     184         *
     185         * @param WP_REST_Request $request Full details about the request.
     186         *
     187         * @since 5.4.0
     188         *
     189         * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
     190         */
     191        public  function get_item( $request ) {
     192                $block_name = $request['namespace'] . '/' . $request['name'];
     193                $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
     194
     195                if ( empty( $block_type ) ) {
     196                        return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.' ), array( 'status' => 404 ) );
     197                }
     198
     199                $data = $this->prepare_item_for_response( $block_type, $request );
     200
     201                return rest_ensure_response( $data );
     202        }
     203
     204        /**
     205         * Prepares a block type object for serialization.
     206         *
     207         * @param stdClass        $block_type block type data.
     208         * @param WP_REST_Request $request    Full details about the request.
     209         *
     210         * @since 5.4.0
     211         *
     212         * @return WP_REST_Response block type data.
     213         */
     214        public function prepare_item_for_response( $block_type, $request ) {
     215
     216                $fields = $this->get_fields_for_response( $request );
     217                $data   = array();
     218                if ( in_array( 'name', $fields, true ) ) {
     219                        $data['name'] = $block_type->name;
     220                }
     221
     222                if ( in_array( 'attributes', $fields, true ) ) {
     223                        $data['attributes'] = $block_type->get_attributes();
     224                }
     225
     226                if ( in_array( 'is_dynamic', $fields, true ) ) {
     227                        $data['is_dynamic'] = $block_type->is_dynamic();
     228                }
     229
     230                $extra_fields = array( 'editor_script', 'script', 'editor_style', 'style' );
     231                foreach ( $extra_fields as $extra_field ) {
     232                        if ( in_array( $extra_field, $fields, true ) ) {
     233                                $data[ $extra_field ] = (string) $block_type->$extra_field;
     234                        }
     235                }
     236
     237
     238                $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
     239                $data    = $this->add_additional_fields_to_object( $data, $request );
     240                $data    = $this->filter_response_by_context( $data, $context );
     241
     242                $response = rest_ensure_response( $data );
     243
     244                $response->add_links(
     245                        array(
     246                                'collection'              => array(
     247                                        'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
     248                                ),
     249                                'https://api.w.org/items' => array(
     250                                        'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ),
     251                                ),
     252                        )
     253                );
     254
     255
     256                /**
     257                 * Filters a block type returned from the REST API.
     258                 *
     259                 * Allows modification of the block type data right before it is returned.
     260                 *
     261                 * @param WP_REST_Response $response   The response object.
     262                 * @param object           $block_type The original block type object.
     263                 * @param WP_REST_Request  $request    Request used to generate the response.
     264                 *
     265                 * @since 5.4.0
     266                 *
     267                 */
     268                return apply_filters( 'rest_prepare_block_type', $response, $block_type, $request );
     269        }
     270
     271        /**
     272         * Retrieves the block type' schema, conforming to JSON Schema.
     273         *
     274         * @since 5.4.0
     275         *
     276         * @return array Item schema data.
     277         */
     278        public function get_item_schema() {
     279                if ( $this->schema ) {
     280                        return $this->add_additional_fields_schema( $this->schema );
     281                }
     282
     283                $schema = array(
     284                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
     285                        'title'      => 'block_type',
     286                        'type'       => 'object',
     287                        'properties' => array(
     288                                'name'          => array(
     289                                        'description' => __( 'The title for the block type.' ),
     290                                        'type'        => 'string',
     291                                        'context'     => array( 'embed', 'view', 'edit' ),
     292                                        'readonly'    => true,
     293                                ),
     294                                'attributes'    => array(
     295                                        'description' => __( '' ),
     296                                        'type'        => 'object',
     297                                        'context'     => array( 'embed', 'view', 'edit' ),
     298                                        'readonly'    => true,
     299                                ),
     300                                'is_dynamic'    => array(
     301                                        'description' => __( '' ),
     302                                        'type'        => 'boolean',
     303                                        'context'     => array( 'embed', 'view', 'edit' ),
     304                                        'readonly'    => true,
     305                                ),
     306                                'editor_script' => array(
     307                                        'description' => __( '' ),
     308                                        'type'        => 'string',
     309                                        'context'     => array( 'embed', 'view', 'edit' ),
     310                                        'readonly'    => true,
     311                                ),
     312                                'script'        => array(
     313                                        'description' => __( '' ),
     314                                        'type'        => 'string',
     315                                        'context'     => array( 'embed', 'view', 'edit' ),
     316                                        'readonly'    => true,
     317                                ),
     318                                'editor_style'  => array(
     319                                        'description' => __( '' ),
     320                                        'type'        => 'string',
     321                                        'context'     => array( 'embed', 'view', 'edit' ),
     322                                        'readonly'    => true,
     323                                ),
     324                                'style'         => array(
     325                                        'description' => __( '' ),
     326                                        'type'        => 'string',
     327                                        'context'     => array( 'embed', 'view', 'edit' ),
     328                                        'readonly'    => true,
     329                                ),
     330                        ),
     331                );
     332
     333                $this->schema = $schema;
     334
     335                return $this->add_additional_fields_schema( $this->schema );
     336        }
     337
     338        /**
     339         * Retrieves the query params for collections.
     340         *
     341         * @since 5.4.0
     342         *
     343         * @return array Collection parameters.
     344         */
     345        public function get_collection_params() {
     346                $new_params            = array();
     347                $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
     348                $new_params['namespace']    = array(
     349                        'description' => __( '' ),
     350                        'type'        => 'string',
     351                );
     352                return $new_params;
     353        }
     354
     355}
  • src/wp-includes/rest-api.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    264264        $controller = new WP_REST_Block_Renderer_Controller;
    265265        $controller->register_routes();
    266266
     267        // Block Types.
     268        $controller = new WP_REST_Block_Types_Controller()
     269        $controller->register_routes();
     270
    267271        // Settings.
    268272        $controller = new WP_REST_Settings_Controller;
    269273        $controller->register_routes();
  • src/wp-settings.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    246246require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-search-controller.php' );
    247247require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-blocks-controller.php' );
    248248require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-renderer-controller.php' );
     249require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-types-controller.php' );
    249250require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-settings-controller.php' );
    250251require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-themes-controller.php' );
    251252require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-meta-fields.php' );