Make WordPress Core

Changeset 44172


Ignore:
Timestamp:
12/14/2018 06:01:24 AM (8 years ago)
Author:
pento
Message:

Block Editor: Preload wp/v2/media with OPTIONS for caps check.

Also introduces a block_editor_preload_paths filter for plugins and themes to preload additional data.

Merges [43833] from the 5.0 branch to trunk.

Props imath, mattheu, danielbachhuber.
Fixes #45194.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-admin/edit-form-blocks.php

    r44165 r44172  
    5050        sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
    5151        sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ),
    52 );
     52        array( '/wp/v2/media', 'OPTIONS' ),
     53);
     54
     55/**
     56 * Preload common data by specifying an array of REST API paths that will be preloaded.
     57 *
     58 * Filters the array of paths that will be preloaded.
     59 *
     60 * @since 5.0.0
     61 *
     62 * @param array  $preload_paths Array of paths to preload.
     63 * @param object $post          The post resource data.
     64 */
     65$preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post );
    5366
    5467/*
  • trunk/src/wp-includes/rest-api.php

    r44150 r44172  
    13431343        }
    13441344
     1345        $method = 'GET';
     1346        if ( is_array( $path ) && 2 === count( $path ) ) {
     1347                $method = end( $path );
     1348                $path   = reset( $path );
     1349
     1350                if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) {
     1351                        $method = 'GET';
     1352                }
     1353        }
     1354
    13451355        $path_parts = parse_url( $path );
    13461356        if ( false === $path_parts ) {
     
    13481358        }
    13491359
    1350         $request = new WP_REST_Request( 'GET', $path_parts['path'] );
     1360        $request = new WP_REST_Request( $method, $path_parts['path'] );
    13511361        if ( ! empty( $path_parts['query'] ) ) {
    13521362                parse_str( $path_parts['query'], $query_params );
     
    13671377                }
    13681378
    1369                 $memo[ $path ] = array(
    1370                         'body'    => $data,
    1371                         'headers' => $response->headers,
    1372                 );
     1379                if ( 'OPTIONS' === $method ) {
     1380                        $response = rest_send_allow_header( $response, $server, $request );
     1381
     1382                        $memo[ $method ][ $path ] = array(
     1383                                'body'    => $data,
     1384                                'headers' => $response->headers,
     1385                        );
     1386                } else {
     1387                        $memo[ $path ] = array(
     1388                                'body'    => $data,
     1389                                'headers' => $response->headers,
     1390                        );
     1391                }
    13731392        }
    13741393
  • trunk/tests/phpunit/tests/rest-api.php

    r44123 r44172  
    722722                $this->assertTrue( is_array( rest_preload_api_request( 0, '/' ) ) );
    723723        }
     724
     725        function test_rest_preload_api_request_with_method() {
     726                $rest_server               = $GLOBALS['wp_rest_server'];
     727                $GLOBALS['wp_rest_server'] = null;
     728
     729                $preload_paths = array(
     730                        '/wp/v2/types',
     731                        array( '/wp/v2/media', 'OPTIONS' ),
     732                );
     733
     734                $preload_data = array_reduce(
     735                        $preload_paths,
     736                        'rest_preload_api_request',
     737                        array()
     738                );
     739
     740                $this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS' ) );
     741                $this->assertTrue( isset( $preload_data['OPTIONS']['/wp/v2/media'] ) );
     742
     743                $GLOBALS['wp_rest_server'] = $rest_server;
     744        }
    724745}
Note: See TracChangeset for help on using the changeset viewer.