From 58fed318c7bd8044f8b1a78497181a91320e4927 Mon Sep 17 00:00:00 2001 From: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Date: Mon, 10 Jun 2024 23:14:51 +0900 Subject: [PATCH] Quality: Fix warning error when exporting theme (#671) --- includes/class-create-block-theme-api.php | 1 + src/utils/download-file.js | 27 +++++------------------ 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/includes/class-create-block-theme-api.php b/includes/class-create-block-theme-api.php index 889be29b..604fa75b 100644 --- a/includes/class-create-block-theme-api.php +++ b/includes/class-create-block-theme-api.php @@ -308,6 +308,7 @@ function rest_export_theme( $request ) { header( 'Content-Length: ' . filesize( $filename ) ); flush(); echo readfile( $filename ); + exit; } /** diff --git a/src/utils/download-file.js b/src/utils/download-file.js index 5e159b78..75332acc 100644 --- a/src/utils/download-file.js +++ b/src/utils/download-file.js @@ -1,3 +1,8 @@ +/** + * WordPress dependencies + */ +import { downloadBlob } from '@wordpress/blob'; + /* * Download a file from in a browser. * @@ -9,25 +14,5 @@ export default async function downloadFile( response ) { const filename = response.headers .get( 'Content-Disposition' ) .split( 'filename=' )[ 1 ]; - - // Check if the browser supports navigator.msSaveBlob or navigator.saveBlob - if ( window.navigator.msSaveBlob || window.navigator.saveBlob ) { - const saveBlob = - window.navigator.msSaveBlob || window.navigator.saveBlob; - saveBlob.call( window.navigator, blob, filename ); - } else { - // Fall back to creating an object URL and triggering a download using an anchor element - const url = URL.createObjectURL( blob ); - - const a = document.createElement( 'a' ); - a.href = url; - a.download = filename; - document.body.appendChild( a ); - a.click(); - document.body.removeChild( a ); - - setTimeout( () => { - URL.revokeObjectURL( url ); - }, 100 ); - } + downloadBlob( filename, blob, 'application/zip' ); }