Check ZipArchive class before zip export (#609)

This commit is contained in:
Aki Hamano 2024-05-01 01:10:57 +09:00 committed by GitHub
parent b0c48cdfbc
commit 9830e9de79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 27 additions and 6 deletions

View file

@ -462,6 +462,9 @@ class Create_Block_Theme_Admin {
$this->create_blank_theme( $_POST['theme'], $_FILES['screenshot'] );

add_action( 'admin_notices', array( 'Form_Messages', 'admin_notice_blank_success' ) );
} elseif ( ! class_exists( 'ZipArchive' ) ) {
// Avoid running if ZipArchive is not enabled.
add_action( 'admin_notices', array( 'Form_Messages', 'admin_notice_error_unsupported_zip_archive' ) );
} elseif ( is_child_theme() ) {
if ( 'sibling' === $_POST['theme']['type'] ) {
if ( '' === $_POST['theme']['name'] ) {

View file

@ -103,4 +103,13 @@ class Form_Messages {
</div>
<?php
}

public static function admin_notice_error_unsupported_zip_archive() {
$themes_dir = get_theme_root();
?>
<div class="notice notice-error">
<p><?php _e( 'Unable to create a zip file. ZipArchive not available.', 'create-block-theme' ); ?></p>
</div>
<?php
}
}

View file

@ -367,6 +367,13 @@ class Create_Block_Theme_API {
* Export the theme as a ZIP file.
*/
function rest_export_theme( $request ) {
if ( ! class_exists( 'ZipArchive' ) ) {
return new WP_Error(
'missing_zip_package',
__( 'Unable to create a zip file. ZipArchive not available.', 'create-block-theme' ),
);
}

$theme_slug = wp_get_theme()->get( 'TextDomain' );

// Create ZIP file in the temporary directory.

View file

@ -46,6 +46,7 @@ import { CreateVariationPanel } from './editor-sidebar/create-variation-panel';
import { ThemeMetadataEditorModal } from './editor-sidebar/metadata-editor-modal';
import ScreenHeader from './editor-sidebar/screen-header';
import { downloadExportedTheme } from './resolvers';
import { downloadFile } from './utils';

const CreateBlockThemePlugin = () => {
const [ isEditorOpen, setIsEditorOpen ] = useState( false );
@ -56,8 +57,12 @@ const CreateBlockThemePlugin = () => {

const { createErrorNotice } = useDispatch( noticesStore );

const handleExportClick = () => {
downloadExportedTheme().catch( ( error ) => {
const handleExportClick = async () => {
try {
const response = await downloadExportedTheme();
downloadFile( response );
} catch ( errorResponse ) {
const error = await errorResponse.json();
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
@ -66,7 +71,7 @@ const CreateBlockThemePlugin = () => {
'create-block-theme'
);
createErrorNotice( errorMessage, { type: 'snackbar' } );
} );
}
};

return (

View file

@ -1,5 +1,4 @@
import apiFetch from '@wordpress/api-fetch';
import { downloadFile } from './utils';

export async function fetchThemeJson() {
const fetchOptions = {
@ -57,7 +56,5 @@ export async function downloadExportedTheme() {
'Content-Type': 'application/json',
},
parse: false,
} ).then( ( response ) => {
downloadFile( response );
} );
}