Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Tweaks and add comments.
  • Loading branch information
spacedmonkey committed Nov 30, 2021
commit 7c78e4fa10548ae3e8a7c0d4b9cfdfcd101e8ba8
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@
class WP_REST_Edit_Site_Export_Controller extends WP_REST_Controller {

/**
* Constructs the controller.
* Constructor.
*
* @since 5.9.0
*/
public function __construct() {
$this->namespace = 'wp-block-editor/v1';
$this->rest_base = 'export';
}

/**
* Registers the necessary REST API routes.
* Registers the site export route.
*
* @since 5.9.0
*/
public function register_routes() {
register_rest_route(
Expand All @@ -44,31 +48,37 @@ public function register_routes() {
/**
* Checks whether a given request has permission to export.
*
* @return WP_Error|bool True if the request has access, or WP_Error object.
* @since 5.9.0
*
* @return WP_Error|true True if the request has access, or WP_Error object.
*/
public function permissions_check() {
if ( current_user_can( 'edit_theme_options' ) ) {
return true;
if ( ! current_user_can( 'edit_theme_options' ) ) {
new WP_Error(
'rest_cannot_view_url_details',
__( 'Sorry, you are not allowed to export templates and template parts.' ),
array( 'status' => rest_authorization_required_code() )
);
}

return new WP_Error(
'rest_cannot_view_url_details',
__( 'Sorry, you are not allowed to export templates and template parts.' ),
array( 'status' => rest_authorization_required_code() )
);
return true;
}

/**
* Output a ZIP file with an export of the current templates
* and template parts from the site editor, and close the connection.
*
* @since 5.9.0
*
* @return WP_Error|void
*/
public function export() {
// Generate the export file.
$filename = wp_generate_block_templates_export_file();

if ( is_wp_error( $filename ) ) {
$filename->add_data( array( 'status' => 500 ) );

return $filename;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ function test_flatten_blocks() {

/**
* Should generate block templates export file.
*
* @ticket 54448
*/
function test_wp_generate_block_templates_export_file() {
$filename = wp_generate_block_templates_export_file();
Expand Down