Make WordPress Core


Ignore:
Timestamp:
11/30/2021 05:30:22 PM (2 years ago)
Author:
spacedmonkey
Message:

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 .

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-template-utils.php

    r52266 r52286  
    191191
    192192    /**
     193     * @ticket 54448
     194     *
     195     * @dataProvider data_remove_theme_attribute_in_block_template_content
     196     */
     197    function test_remove_theme_attribute_in_block_template_content( $template_content, $expected ) {
     198        $this->assertEquals( $expected, _remove_theme_attribute_in_block_template_content( $template_content ) );
     199    }
     200
     201    function data_remove_theme_attribute_in_block_template_content() {
     202        return array(
     203            array(
     204                '<!-- wp:template-part {"slug":"header","theme":"tt1-blocks","align":"full","tagName":"header","className":"site-header"} /-->',
     205                '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /-->',
     206            ),
     207            array(
     208                '<!-- wp:group --><!-- wp:template-part {"slug":"header","theme":"tt1-blocks","align":"full","tagName":"header","className":"site-header"} /--><!-- /wp:group -->',
     209                '<!-- wp:group --><!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /--><!-- /wp:group -->',
     210            ),
     211            // Does not modify content when there is no existing theme attribute.
     212            array(
     213                '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /-->',
     214                '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /-->',
     215            ),
     216            // Does not remove theme when there is no template part.
     217            array(
     218                '<!-- wp:post-content /-->',
     219                '<!-- wp:post-content /-->',
     220            ),
     221        );
     222    }
     223
     224    /**
    193225     * Should retrieve the template from the theme files.
    194226     */
     
    312344        $this->assertSame( $expected, $actual );
    313345    }
     346
     347    /**
     348     * Should generate block templates export file.
     349     *
     350     * @ticket 54448
     351     */
     352    function test_wp_generate_block_templates_export_file() {
     353        $filename = wp_generate_block_templates_export_file();
     354        $this->assertFileExists( $filename, 'zip file is created at the specified path' );
     355        $this->assertTrue( filesize( $filename ) > 0, 'zip file is larger than 0 bytes' );
     356
     357        // Open ZIP file and make sure the directories exist.
     358        $zip = new ZipArchive();
     359        $zip->open( $filename );
     360        $has_theme_dir                = $zip->locateName( 'theme/' ) !== false;
     361        $has_block_templates_dir      = $zip->locateName( 'theme/templates/' ) !== false;
     362        $has_block_template_parts_dir = $zip->locateName( 'theme/parts/' ) !== false;
     363        $this->assertTrue( $has_theme_dir, 'theme directory exists' );
     364        $this->assertTrue( $has_block_templates_dir, 'theme/templates directory exists' );
     365        $this->assertTrue( $has_block_template_parts_dir, 'theme/parts directory exists' );
     366
     367        // ZIP file contains at least one HTML file.
     368        $has_html_files = false;
     369        $num_files      = $zip->numFiles;
     370        for ( $i = 0; $i < $num_files; $i++ ) {
     371            $filename = $zip->getNameIndex( $i );
     372            if ( '.html' === substr( $filename, -5 ) ) {
     373                $has_html_files = true;
     374                break;
     375            }
     376        }
     377        $this->assertTrue( $has_html_files, 'contains at least one html file' );
     378    }
    314379}
Note: See TracChangeset for help on using the changeset viewer.