From 2c05f20040ccd1e68f3260365f2d41027e34caf5 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 17 Nov 2021 11:16:26 +0400 Subject: [PATCH 1/5] Site Editor: Stabilize export endpoint --- ...ss-wp-rest-edit-site-export-controller.php | 111 ++++++++++++++++++ lib/full-site-editing/edit-site-export.php | 76 ------------ lib/load.php | 4 + lib/rest-api.php | 11 ++ packages/edit-site/src/plugins/index.js | 2 +- 5 files changed, 127 insertions(+), 77 deletions(-) create mode 100644 lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php diff --git a/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php new file mode 100644 index 00000000000000..22d8d8cfa19b61 --- /dev/null +++ b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php @@ -0,0 +1,111 @@ +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() { + if ( ! class_exists( 'ZipArchive' ) ) { + return new WP_Error( __( 'Zip Export not supported.', 'gutenberg' ) ); + } + + // Create ZIP file in the temporary directory. + $filename = tempnam( get_temp_dir(), 'edit-site-export' ); + + $zip = new ZipArchive(); + $zip->open( $filename, ZipArchive::OVERWRITE ); + $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_from_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(); + + header( 'Content-Type: application/zip' ); + header( 'Content-Disposition: attachment; filename=edit-site-export.zip' ); + header( 'Content-Length: ' . filesize( $filename ) ); + flush(); + readfile( $filename ); + exit; + } +} diff --git a/lib/full-site-editing/edit-site-export.php b/lib/full-site-editing/edit-site-export.php index 6a29c235434951..7914f83206220d 100644 --- a/lib/full-site-editing/edit-site-export.php +++ b/lib/full-site-editing/edit-site-export.php @@ -36,79 +36,3 @@ function _remove_theme_attribute_from_content( $template_content ) { return $template_content; } - -/** - * Creates an export of the current templates and - * template parts from the site editor at the - * specified path in a ZIP file. - * - * @param string $filename path of the ZIP file. - */ -function gutenberg_edit_site_export_create_zip( $filename ) { - if ( ! class_exists( 'ZipArchive' ) ) { - return new WP_Error( 'Zip Export not supported.' ); - } - - $zip = new ZipArchive(); - $zip->open( $filename, ZipArchive::OVERWRITE ); - $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_from_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(); -} - -/** - * Output a ZIP file with an export of the current templates - * and template parts from the site editor, and close the connection. - */ -function gutenberg_edit_site_export() { - // Create ZIP file in the temporary directory. - $filename = tempnam( get_temp_dir(), 'edit-site-export' ); - gutenberg_edit_site_export_create_zip( $filename ); - - header( 'Content-Type: application/zip' ); - header( 'Content-Disposition: attachment; filename=edit-site-export.zip' ); - header( 'Content-Length: ' . filesize( $filename ) ); - flush(); - echo readfile( $filename ); - die(); -} - -add_action( - 'rest_api_init', - function () { - register_rest_route( - '__experimental/edit-site/v1', - '/export', - array( - 'methods' => 'GET', - 'callback' => 'gutenberg_edit_site_export', - 'permission_callback' => function () { - return current_user_can( 'edit_theme_options' ); - }, - ) - ); - } -); diff --git a/lib/load.php b/lib/load.php index cec6a89012da32..2d61d3d6d8bb8e 100644 --- a/lib/load.php +++ b/lib/load.php @@ -71,6 +71,10 @@ function gutenberg_is_experiment_enabled( $name ) { require_once __DIR__ . '/class-wp-rest-url-details-controller.php'; } + if ( ! class_exists( 'WP_REST_Edit_Site_Export_Controller' ) ) { + require_once __DIR__ . '/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php'; + } + require __DIR__ . '/rest-api.php'; } diff --git a/lib/rest-api.php b/lib/rest-api.php index 3e2e2b09fd1d02..49f82e2eb4b0e1 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -242,3 +242,14 @@ function gutenberg_register_global_styles_endpoints() { $editor_settings->register_routes(); } add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' ); + +/** + * Registers the Edit Site's Export REST API routes. + * + * @return void + */ +function gutenberg_register_edit_site_export_endpoint() { + $editor_settings = new WP_REST_Edit_Site_Export_Controller(); + $editor_settings->register_routes(); +} +add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_endpoint' ); diff --git a/packages/edit-site/src/plugins/index.js b/packages/edit-site/src/plugins/index.js index 9c74d64010380c..2c0403a8d9470d 100644 --- a/packages/edit-site/src/plugins/index.js +++ b/packages/edit-site/src/plugins/index.js @@ -28,7 +28,7 @@ registerPlugin( 'edit-site', { icon={ download } onClick={ () => apiFetch( { - path: '/__experimental/edit-site/v1/export', + path: '/wp-block-editor/v1/export', parse: false, } ) .then( ( res ) => res.blob() ) From 4780fd5bc3faeb4325d35a84ddf407171572473b Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 17 Nov 2021 12:57:59 +0400 Subject: [PATCH 2/5] Move '_remove_theme_attribute_from_content' in block template utils Restore 'gutenberg_generate_edit_site_export_file' --- .../wordpress-5.9/block-template-utils.php | 34 +++++++++++ ...ss-wp-rest-edit-site-export-controller.php | 34 ++--------- lib/full-site-editing/edit-site-export.php | 58 ++++++++++++------- phpunit/class-block-templates-test.php | 22 +++++++ phpunit/class-edit-site-export-test.php | 24 +------- 5 files changed, 97 insertions(+), 75 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index ccbfd566b332c3..9f189d83e8b610 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -429,6 +429,40 @@ function _inject_theme_attribute_in_block_template_content( $template_content ) } } +if ( ! function_exists( '_remove_theme_attribute_in_block_template_content' ) ) { + /** + * Parses wp_template content and removes the theme attribute from + * each wp_template_part + * + * @param string $template_content serialized wp_template content. + * + * @return string Updated wp_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 ) { + foreach ( $template_blocks as $block ) { + $new_content .= serialize_block( $block ); + } + + return $new_content; + } + + return $template_content; + } +} + if ( ! function_exists( '_build_block_template_result_from_file' ) ) { /** * Build a unified template object based on a theme file. diff --git a/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php index 22d8d8cfa19b61..8509b1bef55449 100644 --- a/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php +++ b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php @@ -65,42 +65,16 @@ public function permissions_check() { * @return WP_Error|void */ public function export() { - if ( ! class_exists( 'ZipArchive' ) ) { - return new WP_Error( __( 'Zip Export not supported.', 'gutenberg' ) ); - } - // Create ZIP file in the temporary directory. $filename = tempnam( get_temp_dir(), 'edit-site-export' ); - $zip = new ZipArchive(); - $zip->open( $filename, ZipArchive::OVERWRITE ); - $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_from_content( $template->content ); + // Generate the export file. + $success = gutenberg_generate_edit_site_export_file( $filename ); - $zip->addFromString( - 'theme/block-templates/' . $template->slug . '.html', - $template->content - ); + if ( is_wp_error( $success ) ) { + return $success; } - // 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(); - header( 'Content-Type: application/zip' ); header( 'Content-Disposition: attachment; filename=edit-site-export.zip' ); header( 'Content-Length: ' . filesize( $filename ) ); diff --git a/lib/full-site-editing/edit-site-export.php b/lib/full-site-editing/edit-site-export.php index 7914f83206220d..563ab880f387c7 100644 --- a/lib/full-site-editing/edit-site-export.php +++ b/lib/full-site-editing/edit-site-export.php @@ -1,38 +1,52 @@ $block ) { - if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) { - unset( $blocks[ $key ]['attrs']['theme'] ); - $has_updated_content = true; - } +function gutenberg_generate_edit_site_export_file( $filename ) { + if ( ! class_exists( 'ZipArchive' ) ) { + return new WP_Error( 'Zip Export not supported.' ); } - if ( $has_updated_content ) { - foreach ( $template_blocks as $block ) { - $new_content .= serialize_block( $block ); - } + $zip = new ZipArchive(); + $zip->open( $filename, ZipArchive::OVERWRITE ); + $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 ); - return $new_content; + $zip->addFromString( + 'theme/block-templates/' . $template->slug . '.html', + $template->content + ); } - return $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 true; } diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 7d0cfa3b3bc6c2..15c8f20b1f185c 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -191,6 +191,28 @@ function test_inject_theme_attribute_in_block_template_content() { $this->assertEquals( $content_with_no_template_part, $template_content ); } + function test_remove_theme_attribute_in_block_template_content() { + $content_with_existing_theme_attribute = ''; + $template_content = _remove_theme_attribute_in_block_template_content( $content_with_existing_theme_attribute ); + $expected = ''; + $this->assertEquals( $expected, $template_content ); + + $content_with_existing_theme_attribute_nested = ''; + $template_content = _remove_theme_attribute_in_block_template_content( $content_with_existing_theme_attribute_nested ); + $expected = ''; + $this->assertEquals( $expected, $template_content ); + + // Does not modify content when there is no existing theme attribute. + $content_without_theme_attribute = ''; + $template_content = _remove_theme_attribute_in_block_template_content( $content_without_theme_attribute ); + $this->assertEquals( $content_without_theme_attribute, $template_content ); + + // Does not remove theme when there is no template part. + $content_with_no_template_part = ''; + $template_content = _remove_theme_attribute_in_block_template_content( $content_with_no_template_part ); + $this->assertEquals( $content_with_no_template_part, $template_content ); + } + /** * Should retrieve the template from the theme files. */ diff --git a/phpunit/class-edit-site-export-test.php b/phpunit/class-edit-site-export-test.php index 8d83cb51e86341..12300114d145ac 100644 --- a/phpunit/class-edit-site-export-test.php +++ b/phpunit/class-edit-site-export-test.php @@ -6,31 +6,9 @@ */ class Edit_Site_Export_Test extends WP_UnitTestCase { - function test_remove_theme_attribute_from_content() { - $content_with_existing_theme_attribute = ''; - $template_content = _remove_theme_attribute_from_content( $content_with_existing_theme_attribute ); - $expected = ''; - $this->assertEquals( $expected, $template_content ); - - $content_with_existing_theme_attribute_nested = ''; - $template_content = _remove_theme_attribute_from_content( $content_with_existing_theme_attribute_nested ); - $expected = ''; - $this->assertEquals( $expected, $template_content ); - - // Does not modify content when there is no existing theme attribute. - $content_without_theme_attribute = ''; - $template_content = _remove_theme_attribute_from_content( $content_without_theme_attribute ); - $this->assertEquals( $content_without_theme_attribute, $template_content ); - - // Does not remove theme when there is no template part. - $content_with_no_template_part = ''; - $template_content = _remove_theme_attribute_from_content( $content_with_no_template_part ); - $this->assertEquals( $content_with_no_template_part, $template_content ); - } - function test_gutenberg_edit_site_export() { $filename = tempnam( get_temp_dir(), 'edit-site-export' ); - gutenberg_edit_site_export_create_zip( $filename ); + gutenberg_generate_edit_site_export_file( $filename ); $this->assertTrue( file_exists( $filename ), 'zip file is created at the specified path' ); $this->assertTrue( filesize( $filename ) > 0, 'zip file is larger than 0 bytes' ); From 1616a7f34ffad584058821eaec38b9c0160d211d Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 17 Nov 2021 13:00:58 +0400 Subject: [PATCH 3/5] Delete temp file after export --- .../wordpress-5.9/class-wp-rest-edit-site-export-controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php index 8509b1bef55449..e02902527b3bf1 100644 --- a/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php +++ b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php @@ -80,6 +80,7 @@ public function export() { header( 'Content-Length: ' . filesize( $filename ) ); flush(); readfile( $filename ); + unlink( $filename ); exit; } } From 182cd2060410bcdc4be9f5846cf670f4c84db7a5 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 17 Nov 2021 17:12:19 +0400 Subject: [PATCH 4/5] Use ZipArchive::open to create export file --- ...ass-wp-rest-edit-site-export-controller.php | 9 +++------ lib/full-site-editing/edit-site-export.php | 18 +++++++++++------- phpunit/class-edit-site-export-test.php | 3 +-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php index e02902527b3bf1..a9ffde09895fdf 100644 --- a/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php +++ b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php @@ -65,14 +65,11 @@ public function permissions_check() { * @return WP_Error|void */ public function export() { - // Create ZIP file in the temporary directory. - $filename = tempnam( get_temp_dir(), 'edit-site-export' ); - // Generate the export file. - $success = gutenberg_generate_edit_site_export_file( $filename ); + $filename = gutenberg_generate_edit_site_export_file(); - if ( is_wp_error( $success ) ) { - return $success; + if ( is_wp_error( $filename ) ) { + return $filename; } header( 'Content-Type: application/zip' ); diff --git a/lib/full-site-editing/edit-site-export.php b/lib/full-site-editing/edit-site-export.php index 563ab880f387c7..45001fdce92d85 100644 --- a/lib/full-site-editing/edit-site-export.php +++ b/lib/full-site-editing/edit-site-export.php @@ -10,17 +10,21 @@ * template parts from the site editor at the * specified path in a ZIP file. * - * @param string $filename path of the ZIP file. - * - * @return WP_Error|bool + * @return WP_Error|string Path of the ZIP file or error on failure. */ -function gutenberg_generate_edit_site_export_file( $filename ) { +function gutenberg_generate_edit_site_export_file() { if ( ! class_exists( 'ZipArchive' ) ) { - return new WP_Error( 'Zip Export not supported.' ); + 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(); - $zip->open( $filename, ZipArchive::OVERWRITE ); + 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' ); @@ -48,5 +52,5 @@ function gutenberg_generate_edit_site_export_file( $filename ) { // Save changes to the zip file. $zip->close(); - return true; + return $filename; } diff --git a/phpunit/class-edit-site-export-test.php b/phpunit/class-edit-site-export-test.php index 12300114d145ac..e7b197e6827a84 100644 --- a/phpunit/class-edit-site-export-test.php +++ b/phpunit/class-edit-site-export-test.php @@ -7,8 +7,7 @@ class Edit_Site_Export_Test extends WP_UnitTestCase { function test_gutenberg_edit_site_export() { - $filename = tempnam( get_temp_dir(), 'edit-site-export' ); - gutenberg_generate_edit_site_export_file( $filename ); + $filename = gutenberg_generate_edit_site_export_file(); $this->assertTrue( file_exists( $filename ), 'zip file is created at the specified path' ); $this->assertTrue( filesize( $filename ) > 0, 'zip file is larger than 0 bytes' ); From a995e8b9eee9806ffa7e60e75bd105b60f62f50b Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 17 Nov 2021 19:13:49 +0400 Subject: [PATCH 5/5] Move gutenberg_generate_edit_site_export_file into 5.9 compat dir --- ...ss-wp-rest-edit-site-export-controller.php | 2 +- lib/compat/wordpress-5.9/edit-site-export.php | 58 +++++++++++++++++++ lib/full-site-editing/edit-site-export.php | 56 ------------------ lib/load.php | 2 +- phpunit/class-edit-site-export-test.php | 6 +- 5 files changed, 63 insertions(+), 61 deletions(-) create mode 100644 lib/compat/wordpress-5.9/edit-site-export.php delete mode 100644 lib/full-site-editing/edit-site-export.php diff --git a/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php index a9ffde09895fdf..be3d020060c5fe 100644 --- a/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php +++ b/lib/compat/wordpress-5.9/class-wp-rest-edit-site-export-controller.php @@ -66,7 +66,7 @@ public function permissions_check() { */ public function export() { // Generate the export file. - $filename = gutenberg_generate_edit_site_export_file(); + $filename = wp_generate_edit_site_export_file(); if ( is_wp_error( $filename ) ) { return $filename; diff --git a/lib/compat/wordpress-5.9/edit-site-export.php b/lib/compat/wordpress-5.9/edit-site-export.php new file mode 100644 index 00000000000000..825766cffcab7d --- /dev/null +++ b/lib/compat/wordpress-5.9/edit-site-export.php @@ -0,0 +1,58 @@ +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; + } +} diff --git a/lib/full-site-editing/edit-site-export.php b/lib/full-site-editing/edit-site-export.php deleted file mode 100644 index 45001fdce92d85..00000000000000 --- a/lib/full-site-editing/edit-site-export.php +++ /dev/null @@ -1,56 +0,0 @@ -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; -} diff --git a/lib/load.php b/lib/load.php index 2d61d3d6d8bb8e..d2cb603bb3f4ce 100644 --- a/lib/load.php +++ b/lib/load.php @@ -97,6 +97,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.9/get-global-styles-and-settings.php'; require __DIR__ . '/compat/wordpress-5.9/json-file-decode.php'; require __DIR__ . '/compat/wordpress-5.9/translate-settings-using-i18n-schema.php'; +require __DIR__ . '/compat/wordpress-5.9/edit-site-export.php'; require __DIR__ . '/editor-settings.php'; if ( ! class_exists( 'WP_Block_Template' ) ) { @@ -118,7 +119,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/full-site-editing/template-parts.php'; require __DIR__ . '/full-site-editing/template-loader.php'; require __DIR__ . '/full-site-editing/edit-site-page.php'; -require __DIR__ . '/full-site-editing/edit-site-export.php'; require __DIR__ . '/compat/wordpress-5.9/default-theme-supports.php'; require __DIR__ . '/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php'; require __DIR__ . '/compat/wordpress-5.9/rest-active-global-styles.php'; diff --git a/phpunit/class-edit-site-export-test.php b/phpunit/class-edit-site-export-test.php index e7b197e6827a84..ae56f11f283de9 100644 --- a/phpunit/class-edit-site-export-test.php +++ b/phpunit/class-edit-site-export-test.php @@ -1,13 +1,13 @@ assertTrue( file_exists( $filename ), 'zip file is created at the specified path' ); $this->assertTrue( filesize( $filename ) > 0, 'zip file is larger than 0 bytes' );