-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site Editor: Stabilize export endpoint (#36559)
* Site Editor: Stabilize export endpoint * Move '_remove_theme_attribute_from_content' in block template utils Restore 'gutenberg_generate_edit_site_export_file' * Delete temp file after export * Use ZipArchive::open to create export file * Move gutenberg_generate_edit_site_export_file into 5.9 compat dir
- Loading branch information
Showing
9 changed files
with
217 additions
and
142 deletions.
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
83 changes: 83 additions & 0 deletions
83
lib/compat/wordpress-5.9/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,83 @@ | ||
<?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 { | ||
|
||
/** | ||
* Constructs the controller. | ||
*/ | ||
public function __construct() { | ||
$this->namespace = 'wp-block-editor/v1'; | ||
$this->rest_base = 'export'; | ||
} | ||
|
||
/** | ||
* Registers the necessary REST API routes. | ||
*/ | ||
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. | ||
* | ||
* @return WP_Error|bool 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_view_url_details', | ||
__( 'Sorry, you are not allowed to export templates and template parts.', 'gutenberg' ), | ||
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. | ||
* | ||
* @return WP_Error|void | ||
*/ | ||
public function export() { | ||
// Generate the export file. | ||
$filename = wp_generate_edit_site_export_file(); | ||
|
||
if ( is_wp_error( $filename ) ) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Edit Site utility methods. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
if ( ! function_exists( 'wp_generate_edit_site_export_file' ) ) { | ||
/** | ||
* Creates an export of the current templates and | ||
* template parts from the site editor at the | ||
* specified path in a ZIP file. | ||
* | ||
* @return WP_Error|string Path of the ZIP file or error on failure. | ||
*/ | ||
function wp_generate_edit_site_export_file() { | ||
if ( ! class_exists( 'ZipArchive' ) ) { | ||
return new WP_Error( __( 'Zip Export not supported.', 'gutenberg' ) ); | ||
} | ||
|
||
$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.', 'gutenberg' ) ); | ||
} | ||
|
||
$zip->addEmptyDir( 'theme' ); | ||
$zip->addEmptyDir( 'theme/block-templates' ); | ||
$zip->addEmptyDir( 'theme/block-template-parts' ); | ||
|
||
// Load templates into the zip file. | ||
$templates = gutenberg_get_block_templates(); | ||
foreach ( $templates as $template ) { | ||
$template->content = _remove_theme_attribute_in_block_template_content( $template->content ); | ||
|
||
$zip->addFromString( | ||
'theme/block-templates/' . $template->slug . '.html', | ||
$template->content | ||
); | ||
} | ||
|
||
// Load template parts into the zip file. | ||
$template_parts = gutenberg_get_block_templates( array(), 'wp_template_part' ); | ||
foreach ( $template_parts as $template_part ) { | ||
$zip->addFromString( | ||
'theme/block-template-parts/' . $template_part->slug . '.html', | ||
$template_part->content | ||
); | ||
} | ||
|
||
// Save changes to the zip file. | ||
$zip->close(); | ||
|
||
return $filename; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.