forked from WordPress/WordPress
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site Editor: Add site export REST API endpoint.
Add a REST API to export site templates and template part as html files. When the REST API is requested, it responds by downloading a single ZIP file and exits early, without completing full request. To create the exported zip, the ZipArchive class is required. If this class is not present then the export will gracefully fail, returning a `WP_Error` object and 500 status error code. Props spacedmonkey, youknowriad, Mamaduka, walbo, peterwilsoncc. Fixes #54448 . Built from https://develop.svn.wordpress.org/trunk@52286 git-svn-id: http://core.svn.wordpress.org/trunk@51878 1a063a9b-81f0-0310-95a4-ce76da25c4cd
- Loading branch information
1 parent
5c414db
commit 13c5637
Showing
5 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* REST API: WP_REST_Edit_Site_Export_Controller class | ||
* | ||
* @package WordPress | ||
* @subpackage REST_API | ||
*/ | ||
|
||
/** | ||
* Controller which provides REST endpoint for exporting current templates | ||
* and template parts. | ||
* | ||
* @since 5.9.0 | ||
* | ||
* @see WP_REST_Controller | ||
*/ | ||
class WP_REST_Edit_Site_Export_Controller extends WP_REST_Controller { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since 5.9.0 | ||
*/ | ||
public function __construct() { | ||
$this->namespace = 'wp-block-editor/v1'; | ||
$this->rest_base = 'export'; | ||
} | ||
|
||
/** | ||
* Registers the site export route. | ||
* | ||
* @since 5.9.0 | ||
*/ | ||
public function register_routes() { | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base, | ||
array( | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'export' ), | ||
'permission_callback' => array( $this, 'permissions_check' ), | ||
), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Checks whether a given request has permission to export. | ||
* | ||
* @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' ) ) { | ||
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; | ||
} | ||
|
||
header( 'Content-Type: application/zip' ); | ||
header( 'Content-Disposition: attachment; filename=edit-site-export.zip' ); | ||
header( 'Content-Length: ' . filesize( $filename ) ); | ||
flush(); | ||
readfile( $filename ); | ||
unlink( $filename ); | ||
exit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters