wp/wp-includes/rest-api/class-wp-rest-server.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
--- a/wp/wp-includes/rest-api/class-wp-rest-server.php	Wed Sep 21 18:19:35 2022 +0200
+++ b/wp/wp-includes/rest-api/class-wp-rest-server.php	Tue Sep 27 16:37:53 2022 +0200
@@ -438,7 +438,8 @@
 
 		// Wrap the response in an envelope if asked for.
 		if ( isset( $_GET['_envelope'] ) ) {
-			$result = $this->envelope_response( $result, isset( $_GET['_embed'] ) );
+			$embed  = isset( $_GET['_embed'] ) ? rest_parse_embed_param( $_GET['_embed'] ) : false;
+			$result = $this->envelope_response( $result, $embed );
 		}
 
 		// Send extra data from response objects.
@@ -497,6 +498,7 @@
 			$json_error_message = $this->get_json_last_error();
 
 			if ( $json_error_message ) {
+				$this->set_status( 500 );
 				$json_error_obj = new WP_Error(
 					'rest_encode_error',
 					$json_error_message,
@@ -729,9 +731,10 @@
 	 * data instead.
 	 *
 	 * @since 4.4.0
+	 * @since 6.0.0 The $embed parameter can now contain a list of link relations to include
 	 *
 	 * @param WP_REST_Response $response Response object.
-	 * @param bool             $embed    Whether links should be embedded.
+	 * @param bool|string[]    $embed    Whether to embed all links, a filtered list of link relations, or no links.
 	 * @return WP_REST_Response New response with wrapped data
 	 */
 	public function envelope_response( $response, $embed ) {
@@ -848,7 +851,7 @@
 		 */
 		$endpoints = apply_filters( 'rest_endpoints', $endpoints );
 
-		// Normalise the endpoints.
+		// Normalize the endpoints.
 		$defaults = array(
 			'methods'       => '',
 			'accept_json'   => false,
@@ -1077,8 +1080,8 @@
 	 * @since 5.6.0
 	 *
 	 * @param WP_REST_Request $request  The request object.
+	 * @param string          $route    The matched route regex.
 	 * @param array           $handler  The matched route handler.
-	 * @param string          $route    The matched route regex.
 	 * @param WP_Error|null   $response The current error object if any.
 	 * @return WP_REST_Response
 	 */
@@ -1228,6 +1231,7 @@
 		$response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' );
 		$this->add_active_theme_link_to_index( $response );
 		$this->add_site_logo_to_index( $response );
+		$this->add_site_icon_to_index( $response );
 
 		/**
 		 * Filters the REST API root index data.
@@ -1237,10 +1241,12 @@
 		 * available on the API, and a small amount of data about the site.
 		 *
 		 * @since 4.4.0
+		 * @since 6.0.0 Added `$request` parameter.
 		 *
 		 * @param WP_REST_Response $response Response data.
+		 * @param WP_REST_Request  $request  Request data.
 		 */
-		return apply_filters( 'rest_index', $response );
+		return apply_filters( 'rest_index', $response, $request );
 	}
 
 	/**
@@ -1274,6 +1280,7 @@
 
 	/**
 	 * Exposes the site logo through the WordPress REST API.
+	 *
 	 * This is used for fetching this information when user has no rights
 	 * to update settings.
 	 *
@@ -1282,14 +1289,47 @@
 	 * @param WP_REST_Response $response REST API response.
 	 */
 	protected function add_site_logo_to_index( WP_REST_Response $response ) {
-		$site_logo_id                = get_theme_mod( 'custom_logo' );
-		$response->data['site_logo'] = $site_logo_id;
-		if ( $site_logo_id ) {
+		$site_logo_id = get_theme_mod( 'custom_logo', 0 );
+
+		$this->add_image_to_index( $response, $site_logo_id, 'site_logo' );
+	}
+
+	/**
+	 * Exposes the site icon through the WordPress REST API.
+	 *
+	 * This is used for fetching this information when user has no rights
+	 * to update settings.
+	 *
+	 * @since 5.9.0
+	 *
+	 * @param WP_REST_Response $response REST API response.
+	 */
+	protected function add_site_icon_to_index( WP_REST_Response $response ) {
+		$site_icon_id = get_option( 'site_icon', 0 );
+
+		$this->add_image_to_index( $response, $site_icon_id, 'site_icon' );
+	}
+
+	/**
+	 * Exposes an image through the WordPress REST API.
+	 * This is used for fetching this information when user has no rights
+	 * to update settings.
+	 *
+	 * @since 5.9.0
+	 *
+	 * @param WP_REST_Response $response REST API response.
+	 * @param int              $image_id Image attachment ID.
+	 * @param string           $type     Type of Image.
+	 */
+	protected function add_image_to_index( WP_REST_Response $response, $image_id, $type ) {
+		$response->data[ $type ] = (int) $image_id;
+		if ( $image_id ) {
 			$response->add_link(
 				'https://api.w.org/featuredmedia',
-				rest_url( 'wp/v2/media/' . $site_logo_id ),
+				rest_url( rest_get_route_for_post( $image_id ) ),
 				array(
 					'embeddable' => true,
+					'type'       => $type,
 				)
 			);
 		}
@@ -1361,11 +1401,11 @@
 			}
 
 			/**
-			 * Filters the REST API endpoint data.
+			 * Filters the publicly-visible data for a single REST API route.
 			 *
 			 * @since 4.4.0
 			 *
-			 * @param WP_REST_Request $request Request data. The namespace is passed as the 'namespace' parameter.
+			 * @param array $data Publicly-visible data for the route.
 			 */
 			$available[ $route ] = apply_filters( 'rest_endpoints_description', $data );
 		}
@@ -1402,6 +1442,8 @@
 			'endpoints' => array(),
 		);
 
+		$allow_batch = false;
+
 		if ( isset( $this->route_options[ $route ] ) ) {
 			$options = $this->route_options[ $route ];
 
@@ -1409,6 +1451,8 @@
 				$data['namespace'] = $options['namespace'];
 			}
 
+			$allow_batch = isset( $options['allow_batch'] ) ? $options['allow_batch'] : false;
+
 			if ( isset( $options['schema'] ) && 'help' === $context ) {
 				$data['schema'] = call_user_func( $options['schema'] );
 			}
@@ -1429,6 +1473,12 @@
 				'methods' => array_keys( $callback['methods'] ),
 			);
 
+			$callback_batch = isset( $callback['allow_batch'] ) ? $callback['allow_batch'] : $allow_batch;
+
+			if ( $callback_batch ) {
+				$endpoint_data['allow_batch'] = $callback_batch;
+			}
+
 			if ( isset( $callback['args'] ) ) {
 				$endpoint_data['args'] = array();