~canonical-sysadmins/wordpress/5.1.1

« back to all changes in this revision

Viewing changes to wp-includes/rest-api.php

  • Committer: Barry Price
  • Date: 2018-12-12 05:08:33 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: barry.price@canonical.com-20181212050833-y090hmrbmlxy37aa
Merge WP5.0 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
193
193
                        $revisions_controller = new WP_REST_Revisions_Controller( $post_type->name );
194
194
                        $revisions_controller->register_routes();
195
195
                }
 
196
 
 
197
                if ( 'attachment' !== $post_type->name ) {
 
198
                        $autosaves_controller = new WP_REST_Autosaves_Controller( $post_type->name );
 
199
                        $autosaves_controller->register_routes();
 
200
                }
 
201
 
196
202
        }
197
203
 
198
204
        // Post types.
230
236
        $controller = new WP_REST_Comments_Controller;
231
237
        $controller->register_routes();
232
238
 
 
239
        /**
 
240
         * Filters the search handlers to use in the REST search controller.
 
241
         *
 
242
         * @since 5.0.0
 
243
         *
 
244
         * @param array $search_handlers List of search handlers to use in the controller. Each search
 
245
         *                               handler instance must extend the `WP_REST_Search_Handler` class.
 
246
         *                               Default is only a handler for posts.
 
247
         */
 
248
        $search_handlers = apply_filters( 'wp_rest_search_handlers', array( new WP_REST_Post_Search_Handler() ) );
 
249
 
 
250
        $controller = new WP_REST_Search_Controller( $search_handlers );
 
251
        $controller->register_routes();
 
252
 
 
253
        // Block Renderer.
 
254
        $controller = new WP_REST_Block_Renderer_Controller;
 
255
        $controller->register_routes();
 
256
 
233
257
        // Settings.
234
258
        $controller = new WP_REST_Settings_Controller;
235
259
        $controller->register_routes();
 
260
 
 
261
        // Themes.
 
262
        $controller = new WP_REST_Themes_Controller;
 
263
        $controller->register_routes();
 
264
 
236
265
}
237
266
 
238
267
/**
307
336
                $path = '/';
308
337
        }
309
338
 
 
339
        $path = '/' . ltrim( $path, '/' );
 
340
 
310
341
        if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) {
311
342
                global $wp_rewrite;
312
343
 
316
347
                        $url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme );
317
348
                }
318
349
 
319
 
                $url .= '/' . ltrim( $path, '/' );
 
350
                $url .= $path;
320
351
        } else {
321
352
                $url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );
322
353
                // nginx only allows HTTP/1.0 methods when redirecting from / to /index.php
325
356
                        $url .= 'index.php';
326
357
                }
327
358
 
328
 
                $path = '/' . ltrim( $path, '/' );
329
 
 
330
359
                $url = add_query_arg( 'rest_route', $path, $url );
331
360
        }
332
361
 
1293
1322
 
1294
1323
        return $value;
1295
1324
}
 
1325
 
 
1326
/**
 
1327
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
 
1328
 * Expected to be called in the context of `array_reduce`.
 
1329
 *
 
1330
 * @since 5.0.0
 
1331
 *
 
1332
 * @param  array  $memo Reduce accumulator.
 
1333
 * @param  string $path REST API path to preload.
 
1334
 * @return array        Modified reduce accumulator.
 
1335
 */
 
1336
function rest_preload_api_request( $memo, $path ) {
 
1337
        // array_reduce() doesn't support passing an array in PHP 5.2, so we need to make sure we start with one.
 
1338
        if ( ! is_array( $memo ) ) {
 
1339
                $memo = array();
 
1340
        }
 
1341
 
 
1342
        if ( empty( $path ) ) {
 
1343
                return $memo;
 
1344
        }
 
1345
 
 
1346
        $method = 'GET';
 
1347
        if ( is_array( $path ) && 2 === count( $path ) ) {
 
1348
                $method = end( $path );
 
1349
                $path   = reset( $path );
 
1350
 
 
1351
                if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) {
 
1352
                        $method = 'GET';
 
1353
                }
 
1354
        }
 
1355
 
 
1356
        $path_parts = parse_url( $path );
 
1357
        if ( false === $path_parts ) {
 
1358
                return $memo;
 
1359
        }
 
1360
 
 
1361
        $request = new WP_REST_Request( $method, $path_parts['path'] );
 
1362
        if ( ! empty( $path_parts['query'] ) ) {
 
1363
                parse_str( $path_parts['query'], $query_params );
 
1364
                $request->set_query_params( $query_params );
 
1365
        }
 
1366
 
 
1367
        $response = rest_do_request( $request );
 
1368
        if ( 200 === $response->status ) {
 
1369
                $server = rest_get_server();
 
1370
                $data   = (array) $response->get_data();
 
1371
                $links  = $server->get_compact_response_links( $response );
 
1372
                if ( ! empty( $links ) ) {
 
1373
                        $data['_links'] = $links;
 
1374
                }
 
1375
 
 
1376
                if ( 'OPTIONS' === $method ) {
 
1377
                        $response = rest_send_allow_header( $response, $server, $request );
 
1378
 
 
1379
                        $memo[ $method ][ $path ] = array(
 
1380
                                'body'    => $data,
 
1381
                                'headers' => $response->headers,
 
1382
                        );
 
1383
                } else {
 
1384
                        $memo[ $path ] = array(
 
1385
                                'body'    => $data,
 
1386
                                'headers' => $response->headers,
 
1387
                        );
 
1388
                }
 
1389
        }
 
1390
 
 
1391
        return $memo;
 
1392
}