diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 07:56:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 07:56:49 +0000 |
commit | a415c29efee45520ae252d2aa28f1083a521cd7b (patch) | |
tree | f4ade4b6668ecc0765de7e1424f7c1427ad433ff /wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php | |
parent | Initial commit. (diff) | |
download | wordpress-a415c29efee45520ae252d2aa28f1083a521cd7b.tar.xz wordpress-a415c29efee45520ae252d2aa28f1083a521cd7b.zip |
Adding upstream version 6.4.3+dfsg1.upstream/6.4.3+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php')
-rw-r--r-- | wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php new file mode 100644 index 0000000..6917608 --- /dev/null +++ b/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php @@ -0,0 +1,94 @@ +<?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' ) ) { + return true; + } + + 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() ) + ); + } + + /** + * 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; + } + + $theme_name = basename( get_stylesheet() ); + header( 'Content-Type: application/zip' ); + header( 'Content-Disposition: attachment; filename=' . $theme_name . '.zip' ); + header( 'Content-Length: ' . filesize( $filename ) ); + flush(); + readfile( $filename ); + unlink( $filename ); + exit; + } +} |