mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-22 09:07:59 +01:00
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
This commit is contained in:
parent
5c414db9ad
commit
13c5637263
@ -454,6 +454,39 @@ function _inject_theme_attribute_in_block_template_content( $template_content )
|
||||
return $template_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a block template and removes the theme attribute from each template part.
|
||||
*
|
||||
* @access private
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param string $template_content Serialized block template content.
|
||||
* @return string Updated block template content.
|
||||
*/
|
||||
function _remove_theme_attribute_in_block_template_content( $template_content ) {
|
||||
$has_updated_content = false;
|
||||
$new_content = '';
|
||||
$template_blocks = parse_blocks( $template_content );
|
||||
|
||||
$blocks = _flatten_blocks( $template_blocks );
|
||||
foreach ( $blocks as $key => $block ) {
|
||||
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
|
||||
unset( $blocks[ $key ]['attrs']['theme'] );
|
||||
$has_updated_content = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $has_updated_content ) {
|
||||
return $template_content;
|
||||
}
|
||||
|
||||
foreach ( $template_blocks as $block ) {
|
||||
$new_content .= serialize_block( $block );
|
||||
}
|
||||
|
||||
return $new_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a unified template object based on a theme file.
|
||||
*
|
||||
@ -863,3 +896,55 @@ function block_header_area() {
|
||||
function block_footer_area() {
|
||||
block_template_part( 'footer' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an export of the current templates and
|
||||
* template parts from the site editor at the
|
||||
* specified path in a ZIP file.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @return WP_Error|string Path of the ZIP file or error on failure.
|
||||
*/
|
||||
function wp_generate_block_templates_export_file() {
|
||||
if ( ! class_exists( 'ZipArchive' ) ) {
|
||||
return new WP_Error( __( 'Zip Export not supported.' ) );
|
||||
}
|
||||
|
||||
$obscura = wp_generate_password( 12, false, false );
|
||||
$filename = get_temp_dir() . 'edit-site-export-' . $obscura . '.zip';
|
||||
|
||||
$zip = new ZipArchive();
|
||||
if ( true !== $zip->open( $filename, ZipArchive::CREATE ) ) {
|
||||
return new WP_Error( __( 'Unable to open export file (archive) for writing.' ) );
|
||||
}
|
||||
|
||||
$zip->addEmptyDir( 'theme' );
|
||||
$zip->addEmptyDir( 'theme/templates' );
|
||||
$zip->addEmptyDir( 'theme/parts' );
|
||||
|
||||
// Load templates into the zip file.
|
||||
$templates = get_block_templates();
|
||||
foreach ( $templates as $template ) {
|
||||
$template->content = _remove_theme_attribute_in_block_template_content( $template->content );
|
||||
|
||||
$zip->addFromString(
|
||||
'theme/templates/' . $template->slug . '.html',
|
||||
$template->content
|
||||
);
|
||||
}
|
||||
|
||||
// Load template parts into the zip file.
|
||||
$template_parts = get_block_templates( array(), 'wp_template_part' );
|
||||
foreach ( $template_parts as $template_part ) {
|
||||
$zip->addFromString(
|
||||
'theme/parts/' . $template_part->slug . '.html',
|
||||
$template_part->content
|
||||
);
|
||||
}
|
||||
|
||||
// Save changes to the zip file.
|
||||
$zip->close();
|
||||
|
||||
return $filename;
|
||||
}
|
||||
|
@ -349,6 +349,10 @@ function create_initial_rest_routes() {
|
||||
// Menu Locations.
|
||||
$controller = new WP_REST_Menu_Locations_Controller();
|
||||
$controller->register_routes();
|
||||
|
||||
// Site Editor Export.
|
||||
$controller = new WP_REST_Edit_Site_Export_Controller();
|
||||
$controller->register_routes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '5.9-alpha-52285';
|
||||
$wp_version = '5.9-alpha-52286';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
@ -275,6 +275,7 @@ require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-settings-controller
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-themes-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-plugins-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-directory-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-application-passwords-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-site-health-controller.php';
|
||||
|
Loading…
Reference in New Issue
Block a user