Make WordPress Core


Ignore:
Timestamp:
11/30/2021 05:30:22 PM (3 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/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}
Note: See TracChangeset for help on using the changeset viewer.