2021-11-30 12:32:02 -05:00
|
|
|
<?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
|
|
|
|
*
|
Docs: Correct `@return` values for a few REST API class methods.
Includes listing the expected type first, instead of `WP_Error`.
Follow-up to [39031], [39033], [46696], [49927], [49929], [50993], [51286], [51973], [52079], [52286], [53152], [56415].
Props antonvlasenko.
See #61593.
Built from https://develop.svn.wordpress.org/trunk@58704
git-svn-id: http://core.svn.wordpress.org/trunk@58106 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-07-10 07:12:16 -04:00
|
|
|
* @return true|WP_Error True if the request has access, or WP_Error object.
|
2021-11-30 12:32:02 -05:00
|
|
|
*/
|
|
|
|
public function permissions_check() {
|
2022-04-11 06:38:00 -04:00
|
|
|
if ( current_user_can( 'edit_theme_options' ) ) {
|
|
|
|
return true;
|
2021-11-30 12:32:02 -05:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:38:00 -04:00
|
|
|
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() )
|
|
|
|
);
|
2021-11-30 12:32:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
Docs: Correct `@return` values for a few REST API class methods.
Includes listing the expected type first, instead of `WP_Error`.
Follow-up to [39031], [39033], [46696], [49927], [49929], [50993], [51286], [51973], [52079], [52286], [53152], [56415].
Props antonvlasenko.
See #61593.
Built from https://develop.svn.wordpress.org/trunk@58704
git-svn-id: http://core.svn.wordpress.org/trunk@58106 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-07-10 07:12:16 -04:00
|
|
|
* @return void|WP_Error
|
2021-11-30 12:32:02 -05:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:10:14 -04:00
|
|
|
$theme_name = basename( get_stylesheet() );
|
2021-11-30 12:32:02 -05:00
|
|
|
header( 'Content-Type: application/zip' );
|
2022-04-11 06:38:00 -04:00
|
|
|
header( 'Content-Disposition: attachment; filename=' . $theme_name . '.zip' );
|
2021-11-30 12:32:02 -05:00
|
|
|
header( 'Content-Length: ' . filesize( $filename ) );
|
|
|
|
flush();
|
|
|
|
readfile( $filename );
|
|
|
|
unlink( $filename );
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|