436 */ |
436 */ |
437 $result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $request ); |
437 $result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $request ); |
438 |
438 |
439 // Wrap the response in an envelope if asked for. |
439 // Wrap the response in an envelope if asked for. |
440 if ( isset( $_GET['_envelope'] ) ) { |
440 if ( isset( $_GET['_envelope'] ) ) { |
441 $result = $this->envelope_response( $result, isset( $_GET['_embed'] ) ); |
441 $embed = isset( $_GET['_embed'] ) ? rest_parse_embed_param( $_GET['_embed'] ) : false; |
|
442 $result = $this->envelope_response( $result, $embed ); |
442 } |
443 } |
443 |
444 |
444 // Send extra data from response objects. |
445 // Send extra data from response objects. |
445 $headers = $result->get_headers(); |
446 $headers = $result->get_headers(); |
446 $this->send_headers( $headers ); |
447 $this->send_headers( $headers ); |
495 $result = wp_json_encode( $result ); |
496 $result = wp_json_encode( $result ); |
496 |
497 |
497 $json_error_message = $this->get_json_last_error(); |
498 $json_error_message = $this->get_json_last_error(); |
498 |
499 |
499 if ( $json_error_message ) { |
500 if ( $json_error_message ) { |
|
501 $this->set_status( 500 ); |
500 $json_error_obj = new WP_Error( |
502 $json_error_obj = new WP_Error( |
501 'rest_encode_error', |
503 'rest_encode_error', |
502 $json_error_message, |
504 $json_error_message, |
503 array( 'status' => 500 ) |
505 array( 'status' => 500 ) |
504 ); |
506 ); |
727 * The enveloping technique is used to work around browser/client |
729 * The enveloping technique is used to work around browser/client |
728 * compatibility issues. Essentially, it converts the full HTTP response to |
730 * compatibility issues. Essentially, it converts the full HTTP response to |
729 * data instead. |
731 * data instead. |
730 * |
732 * |
731 * @since 4.4.0 |
733 * @since 4.4.0 |
|
734 * @since 6.0.0 The $embed parameter can now contain a list of link relations to include |
732 * |
735 * |
733 * @param WP_REST_Response $response Response object. |
736 * @param WP_REST_Response $response Response object. |
734 * @param bool $embed Whether links should be embedded. |
737 * @param bool|string[] $embed Whether to embed all links, a filtered list of link relations, or no links. |
735 * @return WP_REST_Response New response with wrapped data |
738 * @return WP_REST_Response New response with wrapped data |
736 */ |
739 */ |
737 public function envelope_response( $response, $embed ) { |
740 public function envelope_response( $response, $embed ) { |
738 $envelope = array( |
741 $envelope = array( |
739 'body' => $this->response_to_data( $response, $embed ), |
742 'body' => $this->response_to_data( $response, $embed ), |
846 * `'/path/regex' => array( $callback, $bitmask )` or |
849 * `'/path/regex' => array( $callback, $bitmask )` or |
847 * `'/path/regex' => array( array( $callback, $bitmask ). |
850 * `'/path/regex' => array( array( $callback, $bitmask ). |
848 */ |
851 */ |
849 $endpoints = apply_filters( 'rest_endpoints', $endpoints ); |
852 $endpoints = apply_filters( 'rest_endpoints', $endpoints ); |
850 |
853 |
851 // Normalise the endpoints. |
854 // Normalize the endpoints. |
852 $defaults = array( |
855 $defaults = array( |
853 'methods' => '', |
856 'methods' => '', |
854 'accept_json' => false, |
857 'accept_json' => false, |
855 'accept_raw' => false, |
858 'accept_raw' => false, |
856 'show_in_index' => true, |
859 'show_in_index' => true, |
1075 * |
1078 * |
1076 * @access private |
1079 * @access private |
1077 * @since 5.6.0 |
1080 * @since 5.6.0 |
1078 * |
1081 * |
1079 * @param WP_REST_Request $request The request object. |
1082 * @param WP_REST_Request $request The request object. |
|
1083 * @param string $route The matched route regex. |
1080 * @param array $handler The matched route handler. |
1084 * @param array $handler The matched route handler. |
1081 * @param string $route The matched route regex. |
|
1082 * @param WP_Error|null $response The current error object if any. |
1085 * @param WP_Error|null $response The current error object if any. |
1083 * @return WP_REST_Response |
1086 * @return WP_REST_Response |
1084 */ |
1087 */ |
1085 protected function respond_to_request( $request, $route, $handler, $response ) { |
1088 protected function respond_to_request( $request, $route, $handler, $response ) { |
1086 /** |
1089 /** |
1226 |
1229 |
1227 $response = new WP_REST_Response( $available ); |
1230 $response = new WP_REST_Response( $available ); |
1228 $response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' ); |
1231 $response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' ); |
1229 $this->add_active_theme_link_to_index( $response ); |
1232 $this->add_active_theme_link_to_index( $response ); |
1230 $this->add_site_logo_to_index( $response ); |
1233 $this->add_site_logo_to_index( $response ); |
|
1234 $this->add_site_icon_to_index( $response ); |
1231 |
1235 |
1232 /** |
1236 /** |
1233 * Filters the REST API root index data. |
1237 * Filters the REST API root index data. |
1234 * |
1238 * |
1235 * This contains the data describing the API. This includes information |
1239 * This contains the data describing the API. This includes information |
1236 * about supported authentication schemes, supported namespaces, routes |
1240 * about supported authentication schemes, supported namespaces, routes |
1237 * available on the API, and a small amount of data about the site. |
1241 * available on the API, and a small amount of data about the site. |
1238 * |
1242 * |
1239 * @since 4.4.0 |
1243 * @since 4.4.0 |
|
1244 * @since 6.0.0 Added `$request` parameter. |
1240 * |
1245 * |
1241 * @param WP_REST_Response $response Response data. |
1246 * @param WP_REST_Response $response Response data. |
1242 */ |
1247 * @param WP_REST_Request $request Request data. |
1243 return apply_filters( 'rest_index', $response ); |
1248 */ |
|
1249 return apply_filters( 'rest_index', $response, $request ); |
1244 } |
1250 } |
1245 |
1251 |
1246 /** |
1252 /** |
1247 * Adds a link to the active theme for users who have proper permissions. |
1253 * Adds a link to the active theme for users who have proper permissions. |
1248 * |
1254 * |
1272 } |
1278 } |
1273 } |
1279 } |
1274 |
1280 |
1275 /** |
1281 /** |
1276 * Exposes the site logo through the WordPress REST API. |
1282 * Exposes the site logo through the WordPress REST API. |
|
1283 * |
1277 * This is used for fetching this information when user has no rights |
1284 * This is used for fetching this information when user has no rights |
1278 * to update settings. |
1285 * to update settings. |
1279 * |
1286 * |
1280 * @since 5.8.0 |
1287 * @since 5.8.0 |
1281 * |
1288 * |
1282 * @param WP_REST_Response $response REST API response. |
1289 * @param WP_REST_Response $response REST API response. |
1283 */ |
1290 */ |
1284 protected function add_site_logo_to_index( WP_REST_Response $response ) { |
1291 protected function add_site_logo_to_index( WP_REST_Response $response ) { |
1285 $site_logo_id = get_theme_mod( 'custom_logo' ); |
1292 $site_logo_id = get_theme_mod( 'custom_logo', 0 ); |
1286 $response->data['site_logo'] = $site_logo_id; |
1293 |
1287 if ( $site_logo_id ) { |
1294 $this->add_image_to_index( $response, $site_logo_id, 'site_logo' ); |
|
1295 } |
|
1296 |
|
1297 /** |
|
1298 * Exposes the site icon through the WordPress REST API. |
|
1299 * |
|
1300 * This is used for fetching this information when user has no rights |
|
1301 * to update settings. |
|
1302 * |
|
1303 * @since 5.9.0 |
|
1304 * |
|
1305 * @param WP_REST_Response $response REST API response. |
|
1306 */ |
|
1307 protected function add_site_icon_to_index( WP_REST_Response $response ) { |
|
1308 $site_icon_id = get_option( 'site_icon', 0 ); |
|
1309 |
|
1310 $this->add_image_to_index( $response, $site_icon_id, 'site_icon' ); |
|
1311 } |
|
1312 |
|
1313 /** |
|
1314 * Exposes an image through the WordPress REST API. |
|
1315 * This is used for fetching this information when user has no rights |
|
1316 * to update settings. |
|
1317 * |
|
1318 * @since 5.9.0 |
|
1319 * |
|
1320 * @param WP_REST_Response $response REST API response. |
|
1321 * @param int $image_id Image attachment ID. |
|
1322 * @param string $type Type of Image. |
|
1323 */ |
|
1324 protected function add_image_to_index( WP_REST_Response $response, $image_id, $type ) { |
|
1325 $response->data[ $type ] = (int) $image_id; |
|
1326 if ( $image_id ) { |
1288 $response->add_link( |
1327 $response->add_link( |
1289 'https://api.w.org/featuredmedia', |
1328 'https://api.w.org/featuredmedia', |
1290 rest_url( 'wp/v2/media/' . $site_logo_id ), |
1329 rest_url( rest_get_route_for_post( $image_id ) ), |
1291 array( |
1330 array( |
1292 'embeddable' => true, |
1331 'embeddable' => true, |
|
1332 'type' => $type, |
1293 ) |
1333 ) |
1294 ); |
1334 ); |
1295 } |
1335 } |
1296 } |
1336 } |
1297 |
1337 |
1400 'namespace' => '', |
1440 'namespace' => '', |
1401 'methods' => array(), |
1441 'methods' => array(), |
1402 'endpoints' => array(), |
1442 'endpoints' => array(), |
1403 ); |
1443 ); |
1404 |
1444 |
|
1445 $allow_batch = false; |
|
1446 |
1405 if ( isset( $this->route_options[ $route ] ) ) { |
1447 if ( isset( $this->route_options[ $route ] ) ) { |
1406 $options = $this->route_options[ $route ]; |
1448 $options = $this->route_options[ $route ]; |
1407 |
1449 |
1408 if ( isset( $options['namespace'] ) ) { |
1450 if ( isset( $options['namespace'] ) ) { |
1409 $data['namespace'] = $options['namespace']; |
1451 $data['namespace'] = $options['namespace']; |
1410 } |
1452 } |
|
1453 |
|
1454 $allow_batch = isset( $options['allow_batch'] ) ? $options['allow_batch'] : false; |
1411 |
1455 |
1412 if ( isset( $options['schema'] ) && 'help' === $context ) { |
1456 if ( isset( $options['schema'] ) && 'help' === $context ) { |
1413 $data['schema'] = call_user_func( $options['schema'] ); |
1457 $data['schema'] = call_user_func( $options['schema'] ); |
1414 } |
1458 } |
1415 } |
1459 } |
1426 |
1470 |
1427 $data['methods'] = array_merge( $data['methods'], array_keys( $callback['methods'] ) ); |
1471 $data['methods'] = array_merge( $data['methods'], array_keys( $callback['methods'] ) ); |
1428 $endpoint_data = array( |
1472 $endpoint_data = array( |
1429 'methods' => array_keys( $callback['methods'] ), |
1473 'methods' => array_keys( $callback['methods'] ), |
1430 ); |
1474 ); |
|
1475 |
|
1476 $callback_batch = isset( $callback['allow_batch'] ) ? $callback['allow_batch'] : $allow_batch; |
|
1477 |
|
1478 if ( $callback_batch ) { |
|
1479 $endpoint_data['allow_batch'] = $callback_batch; |
|
1480 } |
1431 |
1481 |
1432 if ( isset( $callback['args'] ) ) { |
1482 if ( isset( $callback['args'] ) ) { |
1433 $endpoint_data['args'] = array(); |
1483 $endpoint_data['args'] = array(); |
1434 |
1484 |
1435 foreach ( $callback['args'] as $key => $opts ) { |
1485 foreach ( $callback['args'] as $key => $opts ) { |