Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Site Editor: Sync export API
  • Loading branch information
Mamaduka committed Nov 26, 2021
commit d630003f06283579088e5ee9a9439eca761deb1c
54 changes: 54 additions & 0 deletions lib/compat/wordpress-5.9/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,3 +891,57 @@ function block_footer_area() {
block_template_part( 'footer' );
}
}

if ( ! function_exists( 'wp_generate_block_templates_export_file' ) ) {
/**
* 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.', '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/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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function permissions_check() {
*/
public function export() {
// Generate the export file.
$filename = wp_generate_edit_site_export_file();
$filename = wp_generate_block_templates_export_file();

if ( is_wp_error( $filename ) ) {
return $filename;
Expand Down
58 changes: 0 additions & 58 deletions lib/compat/wordpress-5.9/edit-site-export.php

This file was deleted.

1 change: 0 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ 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' ) ) {
Expand Down
31 changes: 31 additions & 0 deletions phpunit/class-block-templates-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,35 @@ function test_flatten_blocks() {
$expected = array( $blocks[0] );
$this->assertEquals( $expected, $actual );
}

/**
* Should generate block templates export file.
*/
function test_wp_generate_block_templates_export_file() {
$filename = wp_generate_block_templates_export_file();
$this->assertFileExists( $filename, 'zip file is created at the specified path' );
$this->assertTrue( filesize( $filename ) > 0, 'zip file is larger than 0 bytes' );

// Open ZIP file and make sure the directories exist.
$zip = new ZipArchive();
$zip->open( $filename );
$has_theme_dir = $zip->locateName( 'theme/' ) !== false;
$has_block_templates_dir = $zip->locateName( 'theme/templates/' ) !== false;
$has_block_template_parts_dir = $zip->locateName( 'theme/parts/' ) !== false;
$this->assertTrue( $has_theme_dir, 'theme directory exists' );
$this->assertTrue( $has_block_templates_dir, 'theme/templates directory exists' );
$this->assertTrue( $has_block_template_parts_dir, 'theme/parts directory exists' );

// ZIP file contains at least one HTML file.
$has_html_files = false;
$num_files = $zip->numFiles;
for ( $i = 0; $i < $num_files; $i++ ) {
$filename = $zip->getNameIndex( $i );
if ( '.html' === substr( $filename, -5 ) ) {
$has_html_files = true;
break;
}
}
$this->assertTrue( $has_html_files, 'contains at least one html file' );
}
}
37 changes: 0 additions & 37 deletions phpunit/class-edit-site-export-test.php

This file was deleted.