REST API: Ensure that the export template endpoint returns a valid WP_Error object.

Ensure that the export template endpoint returns a WP_Error object, including code and message, so that the site editor can display an error message. 
Add some basic unit tests to ensure that permission checks are working as expected. 

Follow-up to [52286].

Props Spacedmonkey, dlh, hellofromTonya , Mamaduka, TimothyBlynJacobs.
Fixes #54448.


Built from https://develop.svn.wordpress.org/trunk@52340


git-svn-id: http://core.svn.wordpress.org/trunk@51932 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2021-12-07 18:38:02 +00:00
parent 153e27f625
commit 40b22d1209
3 changed files with 5 additions and 5 deletions

View File

@ -913,7 +913,7 @@ function block_footer_area() {
*/
function wp_generate_block_templates_export_file() {
if ( ! class_exists( 'ZipArchive' ) ) {
return new WP_Error( __( 'Zip Export not supported.' ) );
return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.' ) );
}
$obscura = wp_generate_password( 12, false, false );
@ -921,7 +921,7 @@ function wp_generate_block_templates_export_file() {
$zip = new ZipArchive();
if ( true !== $zip->open( $filename, ZipArchive::CREATE ) ) {
return new WP_Error( __( 'Unable to open export file (archive) for writing.' ) );
return new WP_Error( 'unable_to_create_zip', __( 'Unable to open export file (archive) for writing.' ) );
}
$zip->addEmptyDir( 'theme' );

View File

@ -54,8 +54,8 @@ class WP_REST_Edit_Site_Export_Controller extends WP_REST_Controller {
*/
public function permissions_check() {
if ( ! current_user_can( 'edit_theme_options' ) ) {
new WP_Error(
'rest_cannot_view_url_details',
return new WP_Error(
'rest_cannot_export_templates',
__( 'Sorry, you are not allowed to export templates and template parts.' ),
array( 'status' => rest_authorization_required_code() )
);

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-beta1-52339';
$wp_version = '5.9-beta1-52340';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.