Make WordPress Core

Changeset 52286


Ignore:
Timestamp:
11/30/2021 05:30:22 PM (5 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 .

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r52275 r52286  
    456456
    457457/**
     458 * Parses a block template and removes the theme attribute from each template part.
     459 *
     460 * @access private
     461 * @since 5.9.0
     462 *
     463 * @param string $template_content Serialized block template content.
     464 * @return string Updated block template content.
     465 */
     466function _remove_theme_attribute_in_block_template_content( $template_content ) {
     467        $has_updated_content = false;
     468        $new_content         = '';
     469        $template_blocks     = parse_blocks( $template_content );
     470
     471        $blocks = _flatten_blocks( $template_blocks );
     472        foreach ( $blocks as $key => $block ) {
     473                if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
     474                        unset( $blocks[ $key ]['attrs']['theme'] );
     475                        $has_updated_content = true;
     476                }
     477        }
     478
     479        if ( ! $has_updated_content ) {
     480                return $template_content;
     481        }
     482
     483        foreach ( $template_blocks as $block ) {
     484                $new_content .= serialize_block( $block );
     485        }
     486
     487        return $new_content;
     488}
     489
     490/**
    458491 * Build a unified template object based on a theme file.
    459492 *
     
    864897        block_template_part( 'footer' );
    865898}
     899
     900/**
     901 * Creates an export of the current templates and
     902 * template parts from the site editor at the
     903 * specified path in a ZIP file.
     904 *
     905 * @since 5.9.0
     906 *
     907 * @return WP_Error|string Path of the ZIP file or error on failure.
     908 */
     909function wp_generate_block_templates_export_file() {
     910        if ( ! class_exists( 'ZipArchive' ) ) {
     911                return new WP_Error( __( 'Zip Export not supported.' ) );
     912        }
     913
     914        $obscura  = wp_generate_password( 12, false, false );
     915        $filename = get_temp_dir() . 'edit-site-export-' . $obscura . '.zip';
     916
     917        $zip = new ZipArchive();
     918        if ( true !== $zip->open( $filename, ZipArchive::CREATE ) ) {
     919                return new WP_Error( __( 'Unable to open export file (archive) for writing.' ) );
     920        }
     921
     922        $zip->addEmptyDir( 'theme' );
     923        $zip->addEmptyDir( 'theme/templates' );
     924        $zip->addEmptyDir( 'theme/parts' );
     925
     926        // Load templates into the zip file.
     927        $templates = get_block_templates();
     928        foreach ( $templates as $template ) {
     929                $template->content = _remove_theme_attribute_in_block_template_content( $template->content );
     930
     931                $zip->addFromString(
     932                        'theme/templates/' . $template->slug . '.html',
     933                        $template->content
     934                );
     935        }
     936
     937        // Load template parts into the zip file.
     938        $template_parts = get_block_templates( array(), 'wp_template_part' );
     939        foreach ( $template_parts as $template_part ) {
     940                $zip->addFromString(
     941                        'theme/parts/' . $template_part->slug . '.html',
     942                        $template_part->content
     943                );
     944        }
     945
     946        // Save changes to the zip file.
     947        $zip->close();
     948
     949        return $filename;
     950}
  • trunk/src/wp-includes/rest-api.php

    r52272 r52286  
    349349        // Menu Locations.
    350350        $controller = new WP_REST_Menu_Locations_Controller();
     351        $controller->register_routes();
     352
     353        // Site Editor Export.
     354        $controller = new WP_REST_Edit_Site_Export_Controller();
    351355        $controller->register_routes();
    352356}
  • trunk/src/wp-settings.php

    r52272 r52286  
    276276require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-plugins-controller.php';
    277277require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-directory-controller.php';
     278require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php';
    278279require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php';
    279280require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-application-passwords-controller.php';
  • 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}
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r52275 r52286  
    1064310643                }
    1064410644            ]
     10645        },
     10646        "/wp-block-editor/v1/export": {
     10647            "namespace": "wp-block-editor/v1",
     10648            "methods": [
     10649                "GET"
     10650            ],
     10651            "endpoints": [
     10652                {
     10653                    "methods": [
     10654                        "GET"
     10655                    ],
     10656                    "args": []
     10657                }
     10658            ],
     10659            "_links": {
     10660                "self": [
     10661                    {
     10662                        "href": "http://example.org/index.php?rest_route=/wp-block-editor/v1/export"
     10663                    }
     10664                ]
     10665            }
    1064510666        }
    1064610667    },
Note: See TracChangeset for help on using the changeset viewer.