Making WordPress.org

Changeset 7580


Ignore:
Timestamp:
08/02/2018 12:55:50 AM (8 years ago)
Author:
dd32
Message:

Gutenberg: Preload some REST API endpoints with mock/static data to avoid REST API failure errors.

See #3703.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/gutenberg/functions.php

    r7579 r7580  
    3232                wp_enqueue_style( 'l10n' );
    3333                wp_enqueue_style( 'buttons' );
     34
     35                // Use a middleware provider to intercept and modify API calls. Short-circuit POST requests, bound queries, allow media, etc.
    3436                wp_add_inline_script( 'wp-api-fetch',
    3537                        sprintf(
     
    4042                                        );
    4143
     44                                        // Prevent non-whitelisted non-GET requests (ie. POST) to prevent errors
    4245                                        if ( options.method && options.method !== "GET" && ! isWhitelistedEndpoint ) {
    43                                                 return Promise.resolve( options.data ); // This works in enough cases to be the default return value.
     46                                                // This works in enough cases to be the default return value.
     47                                                return Promise.resolve( options.data );
    4448                                        }
     49
     50                                        // Add limits to all GET queries which attempt unbound queries
    4551                                        options.path = options.path.replace( "per_page=-1", "per_page=10" );
     52
     53                                        // Load images with the view context, seems to work
     54                                        if ( lodash.startsWith( options.path, "/wp/v2/media/" ) ) {
     55                                                options.path = options.path.replace( "context=edit", "context=view" );
     56                                        }
    4657
    4758                                        return next( options );
     
    5061                        'after'
    5162                );
     63
     64                // Use a middleware preloader to handle the "types" API endpoints with minimal data needed
     65                wp_add_inline_script( 'wp-api-fetch',
     66                        'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( {
     67                                "/wp/v2/types?context=edit": { "body": {
     68                                        "page": {
     69                                                "rest_base": "pages",
     70                                                "supports": {}
     71                                        },
     72                                        "wp_block": {
     73                                                "rest_base": "blocks",
     74                                                "supports": {}
     75                                        }
     76                                } },
     77                                "/wp/v2/types/page?context=edit": { "body": {
     78                                        "rest_base": "pages",
     79                                        "supports": {}
     80                                } },
     81                                "/wp/v2/types/wp_block?context=edit": { "body": {
     82                                        "rest_base": "blocks",
     83                                        "supports": {}
     84                                } }
     85                        } ) );',
     86                        'after'
     87                );
     88
     89                // Use a middleware preloader to load the custom post content:
     90                $frontendburg_content = include __DIR__ . '/gutenfront-content.php';
     91                wp_add_inline_script( 'wp-api-fetch',
     92                        sprintf(
     93                                'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
     94                                wp_json_encode( array(
     95                                        "/wp/v2/pages/" . get_post()->ID . "?context=edit" => [ 'body' => [
     96                                                'id' => get_post()->ID,
     97                                                'title' => [ 'raw' => $frontendburg_content['title'] ],
     98                                                'content' => [ 'block_format' => 1, 'raw' => $frontendburg_content['content'] ],
     99                                                'excerpt' => [ 'raw' => '' ],
     100                                                'date' => '', 'date_gmt' => '', 'modified' => '', 'modified_gmt' => '',
     101                                                'link' => home_url('/'), 'guid' => [],
     102                                                'parent' => 0, 'menu_order' => 0, 'author' => 0, 'featured_media' => 0,
     103                                                'comment_status' => 'closed', 'ping_status' => 'closed', 'template' => '', 'meta' => [], '_links' => [],
     104                                                'type' => 'page', 'status' => 'draft',
     105                                                'slug' => '', 'generated_slug' => '', 'permalink_template' => home_url('/'),
     106                                        ] ]
     107                                ) )
     108                        ),
     109                        'after'
     110                );
     111
     112                // Add a middleware provider which intercepts all uploads and stores them within the browser
    52113                wp_add_inline_script( 'wp-api-fetch',
    53114                        sprintf(
     
    200261}
    201262add_action( 'wp_ajax_nopriv_query-attachments', 'frontenberg_wp_ajax_nopriv_query_attachments' );
    202 
    203 // Override the WP API to give Gutenberg the REST API responses it wants.
    204 function frontenberg_override_rest_api( $null, $wp_rest_server, $request ) {
    205         if ( is_admin() || preg_match( '!/wp-json/!', $_SERVER['REQUEST_URI'] ) ) {
    206                 return $null; // Don't modify an actual API responses..
    207         }
    208 
    209         if ( '/wp/v2/types' == $request->get_route() ) {
    210                 // Minimal data required by Gutenberg for pages.
    211                 return new WP_REST_Response( array(
    212                         'page' => array(
    213                                 'rest_base' => 'pages',
    214                         )
    215                 ), 200 );
    216         }
    217 
    218         if ( preg_match( '!^/wp/v2/pages/(\d+)$!', $request->get_route(), $m ) ) {
    219                 $content = include __DIR__ . '/gutenfront-content.php';
    220 
    221                 return new WP_REST_Response( array(
    222                         'id' => $m[1],
    223                         'date' => gmdate( 'c' ), 'date_gmt' => gmdate( 'c' ),
    224                         'modified' => gmdate( 'c' ), 'modified_gmt' => gmdate( 'c' ),
    225                         'link' => 'https://wordpress.org/gutenberg/', 'guid' => array(),
    226                         'parent' => 0, 'menu_order' => 0, 'author' => 0, 'featured_media' => 0,
    227                         'comment_status' => 'closed', 'ping_status' => 'closed', 'template' => '', 'meta' => [], '_links' => [],
    228                         'type' => 'page', 'status' => 'draft',
    229                         'slug' => '', 'generated_slug' => '', 'permalink_template' => home_url('/'),
    230                         'excerpt' => array( 'raw' => '' ),
    231                         'title' => array(
    232                                 'raw' => $content['title'],
    233                         ),
    234                         'content' => array(
    235                                 'block_format' => 1,
    236                                 'raw' => $content['content'],
    237                         ),
    238                 ), 200 );
    239         }
    240 
    241         return $null;
    242 }
    243 add_filter( 'rest_pre_dispatch', 'frontenberg_override_rest_api', 10, 3 );
    244 
    245263
    246264if ( ! function_exists( 'gutenbergtheme_setup' ) ) :
Note: See TracChangeset for help on using the changeset viewer.