diff --git a/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/class-wp-rest-batch-controller.php b/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/class-wp-rest-batch-controller.php index 72e2592f1d7d5..e674491d7fdee 100644 --- a/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/class-wp-rest-batch-controller.php +++ b/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/class-wp-rest-batch-controller.php @@ -78,9 +78,9 @@ public function handle_batch_requests( $request ) { // Foreach request specified in the requests param, run the endpoint. foreach ( $request['requests'] as $request_params ) { - $response = $this->handle_request( $request_params ); - $key = $request_params['method'] . ' ' . $request_params['route']; - $data[ $key ] = $this->prepare_for_collection( $response ); + $response = $this->handle_request( $request_params ); + $key = $request_params['method'] . ' ' . $request_params['route']; + $data[ $key ][] = $this->prepare_for_collection( $response ); } return rest_ensure_response( $data ); diff --git a/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/class-wp-rest-sideload-image-controller.php b/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/class-wp-rest-sideload-image-controller.php index 01f97d99c39f9..6bf90eddb5067 100644 --- a/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/class-wp-rest-sideload-image-controller.php +++ b/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/class-wp-rest-sideload-image-controller.php @@ -57,7 +57,7 @@ public function register_routes() { * @return WP_Error|WP_REST_Response Response object on success, WP_Error object on failure. */ public function create_item( $request ) { - if ( in_array( get_post_type( $request->get_param( 'post_id' ) ), [ 'revision', 'attachment' ], true ) ) { + if ( ! empty( $request['post_id'] ) && in_array( get_post_type( $request['post_id'] ), [ 'revision', 'attachment' ], true ) ) { return new WP_Error( 'rest_invalid_param', __( 'Invalid parent type.' ), [ 'status' => 400 ] ); } diff --git a/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/page-template-modal/utils/ensure-assets.js b/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/page-template-modal/utils/ensure-assets.js index 98627c75d5967..02d04591b176b 100644 --- a/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/page-template-modal/utils/ensure-assets.js +++ b/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/page-template-modal/utils/ensure-assets.js @@ -3,6 +3,12 @@ */ import { reduce, isEmpty, forEach, set } from 'lodash'; +/** + * WordPress dependencies + */ +import apiFetch from '@wordpress/api-fetch'; +import { addQueryArgs } from '@wordpress/url'; + /** * A full asset URL. * @typedef {String} URL @@ -133,28 +139,33 @@ const findAssetsInBlock = ( session, block ) => { * @returns {Promise} Promise that resoves into an object with URLs as keys and fetch results as values. */ const fetchAssets = async assets => { - return new Promise( resolve => { - // Simulate API call delay. - setTimeout( () => { - // TODO: Get this from API response. - const mockImage = { - id: 66, - url: - 'http://test.local/wp-content/uploads/2019/07/Screenshot-2019-07-16-at-17.30.52-1021x1024.png', - }; - - resolve( - reduce( - assets, - ( fetched, asset ) => ( { - ...fetched, - [ asset.url ]: mockImage, - } ), - {} - ) - ); - }, 1000 ); + const requests = []; + + forEach( assets, ( { url } ) => { + requests.push( { + method: 'POST', + route: '/fse/v1/sideload/image', + params: { url }, + } ); } ); + + return await apiFetch( { + method: 'POST', + path: addQueryArgs( '/fse/v1/batch', { requests } ), + } ).then( response => + reduce( + assets, + ( fetched, asset ) => { + const { id, source_url } = response[ 'POST /fse/v1/sideload/image' ].shift(); + + return { + ...fetched, + [ asset.url ]: { id, url: source_url }, + }; + }, + {} + ) + ); }; /** diff --git a/apps/full-site-editing/package.json b/apps/full-site-editing/package.json index 0e98ff492de1e..acb9e0883ee58 100644 --- a/apps/full-site-editing/package.json +++ b/apps/full-site-editing/package.json @@ -29,7 +29,7 @@ "build": "rimraf dist && npm-run-all --parallel build:*" }, "dependencies": { - "@wordpress/api-fetch": "^3.1.2", + "@wordpress/api-fetch": "^3.3.0", "@wordpress/blocks": "^6.2.3", "@wordpress/components": "^7.3.0", "@wordpress/compose": "^3.2.0",