Skip to content

Commit

Permalink
removed export type conditions, child theme conditions and reused som…
Browse files Browse the repository at this point in the history
…e code from zip export
  • Loading branch information
draganescu committed Apr 29, 2022
1 parent 3df23b1 commit bd72aed
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 291 deletions.
26 changes: 14 additions & 12 deletions lib/compat/wordpress-6.0/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ function gutenberg_generate_block_templates_export_file() {
);
}

// Add the theme.json file to the zip.
$zip->addFromString(
'theme.json',
gutenberg_export_theme_json()
);

// Save changes to the zip file.
$zip->close();

return $filename;
}

function gutenberg_export_theme_json() {
// Load theme.json into the zip file.
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data( array(), array( 'with_supports' => false ) );
// Merge with user data.
Expand All @@ -117,16 +130,5 @@ function gutenberg_generate_block_templates_export_file() {
$theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

// Replace 4 spaces with a tab.
$theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded );

// Add the theme.json file to the zip.
$zip->addFromString(
'theme.json',
$theme_json_tabbed
);

// Save changes to the zip file.
$zip->close();

return $filename;
return preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded );
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*/

/**
* Controller which provides REST endpoint for exporting current templates
* and template parts.
* Controller which provides REST endpoints for exporting current templates
* and template parts either as a zip bundle or as edits to the current enabled
* theme's files.
*
* @since 5.9.0
*
Expand All @@ -17,18 +18,14 @@
class Gutenberg_REST_Edit_Site_Export_Controller_6_1 extends Gutenberg_REST_Edit_Site_Export_Controller {



/**
* Registers the necessary REST API routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/update_theme',
'/' . $this->rest_base . '/export_to_theme_files',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'update_theme' ),
'callback' => array( $this, 'export_to_theme_files' ),
'permission_callback' => array( $this, 'permissions_check' ),
),
),
Expand All @@ -39,178 +36,124 @@ public function register_routes() {
}

/**
* 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_export_templates',
__( 'Sorry, you are not allowed to export templates and template parts.', 'gutenberg' ),
array( 'status' => rest_authorization_required_code() )
);
}

/**
* Update the files of a block theme with the template customisations
* made in the site editor.
* Update the files of the currently enabled block theme with the template and
* template part customisations and additions made in the site editor.
*
* @return WP_Error|void
*/
public function update_theme() {
if ( is_child_theme() ) {
$this->save_theme_locally( 'current' );
}
else {
$this->save_theme_locally( 'all' );
}
public function export_to_theme_files() {
$this->update_template_and_parts_files();
$this->update_theme_json_file();
$this->clear_user_customizations();
return rest_ensure_response( array( "update_theme" => true ) );
}

function save_theme_locally( $export_type ) {
$this->add_templates_to_local( $export_type );
$this->add_theme_json_to_local( $export_type );
}

function clear_user_customizations() {
protected function update_template_and_parts_files() {

// Clear all values in the user theme.json
$user_custom_post_type_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
$global_styles_controller = new Gutenberg_REST_Global_Styles_Controller();
$update_request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' );
$update_request->set_param( 'id', $user_custom_post_type_id );
$update_request->set_param( 'settings', [] );
$update_request->set_param( 'styles', [] );
$updated_global_styles = $global_styles_controller->update_item( $update_request );
delete_transient( 'global_styles' );
delete_transient( 'global_styles_' . get_stylesheet() );
delete_transient( 'gutenberg_global_styles' );
delete_transient( 'gutenberg_global_styles_' . get_stylesheet() );
$user_customisations = $this->get_user_customisations();

//remove all user templates (they have been saved in the theme)
$templates = gutenberg_get_block_templates();
$template_parts = gutenberg_get_block_templates( array(), 'wp_template_part' );
foreach ( $template_parts as $template ) {
if ( $template->source !== 'custom' ) {
continue;
}
wp_delete_post($template->wp_id, true);
foreach ( $user_customisations['templates'] as $template ) {
file_put_contents(
get_template_directory() . '/templates/' . $template->slug . '.html',
$template->content
);
}

foreach ( $templates as $template ) {
if ( $template->source !== 'custom' ) {
continue;
}
wp_delete_post($template->wp_id, true);
foreach ( $user_customisations['template_parts'] as $template_part ) {
file_put_contents(
get_template_directory() . '/parts/' . $template_part->slug . '.html',
$template_part->content
);
}

}

/*
* Filter a template out (return false) based on the export_type expected and the templates origin.
* Templates not filtered out are modified based on the slug information provided and cleaned up
* to have the expected exported value.
*/
function filter_theme_template( $template, $export_type, $path, $old_slug, $new_slug ) {
if ($template->source === 'theme' && $export_type === 'user') {
return false;
}
if (
$template->source === 'theme' &&
$export_type === 'current' &&
! file_exists( $path . $template->slug . '.html' )
) {
return false;
}

$template->content = _remove_theme_attribute_in_block_template_content( $template->content );

// NOTE: Dashes are encoded as \u002d in the content that we get (noteably in things like css variables used in templates)
// This replaces that with dashes again. We should consider decoding the entire string but that is proving difficult.
$template->content = str_replace( '\u002d', '-', $template->content );

if ( $new_slug ) {
$template->content = str_replace( $old_slug, $new_slug, $template->content );
}

return $template;
protected function update_theme_json_file() {
file_put_contents(
get_template_directory() . '/theme.json',
gutenberg_export_theme_json()
);
}

function get_theme_templates( $export_type, $new_slug ) {
protected function get_user_customisations() {

$old_slug = wp_get_theme()->get( 'TextDomain' );
$templates = gutenberg_get_block_templates();
$template_parts = gutenberg_get_block_templates ( array(), 'wp_template_part' );
$exported_templates = [];
$exported_parts = [];

// build collection of templates/parts in currently activated theme
$templates_paths = get_block_theme_folders();
$templates_path = get_stylesheet_directory() . '/' . $templates_paths['wp_template'] . '/';
$parts_path = get_stylesheet_directory() . '/' . $templates_paths['wp_template_part'] . '/';
$exported_templates = [];
$exported_template_parts = [];

foreach ( $templates as $template ) {
$template = $this->filter_theme_template(
$template,
$export_type,
$templates_path,
$old_slug,
$new_slug
);
if ( $template ) {
$exported_templates[] = $template;
if ( $template->source !== 'custom' ) {
continue;
}
$template = $this->clean_template( $template );
$exported_templates[] = $template;
}

foreach ( $template_parts as $template ) {
$template = $this->filter_theme_template(
$template,
$export_type,
$parts_path,
$old_slug,
$new_slug

);
if ( $template ) {
$exported_parts[] = $template;
foreach ( $template_parts as $template_part ) {
if ( $template_part->source !== 'custom' ) {
continue;
}
$template_part = $this->clean_template( $template_part );
$exported_template_parts[] = $template_part;
}

return (object)[
return array(
'templates'=>$exported_templates,
'parts'=>$exported_parts
];
'template_parts'=>$exported_template_parts
);

}

function add_templates_to_local( $export_type ) {

$theme_templates = $this->get_theme_templates( $export_type, null );
/*
* Clean a template or template part by reversing unicode escaping
* and removing the theme attribute from the content
*/
function clean_template( $template ) {
$template->content = _remove_theme_attribute_in_block_template_content(
$template->content
);

foreach ( $theme_templates->templates as $template ) {
file_put_contents(
get_template_directory() . '/templates/' . $template->slug . '.html',
$template->content
);
}
// `serialize_block_attributes` has "unicode escape sequence
// substitution for characters which might otherwise interfere with embedding
// the result in an HTML comment".
// This reverses that escaping for saving back to files.
// @see `wp-includes/blocks.php`
$template->content = str_replace( '\\u002d\\u002d', '--', $template->content );
$template->content = str_replace( '\\u003c', '<', $template->content );
$template->content = str_replace( '\\u003e', '>', $template->content );
$template->content = str_replace( '\\u0026', '&', $template->content );
$template->content = str_replace( '\\u0022', '"', $template->content );

foreach ( $theme_templates->parts as $template_part ) {
file_put_contents(
get_template_directory() . '/parts/' . $template_part->slug . '.html',
$template_part->content
);
}
return $template;
}

function add_theme_json_to_local ( $export_type ) {
file_put_contents(
get_template_directory() . '/theme.json',
WP_Theme_JSON_Resolver_6_1::export_theme_data( $export_type )
function clear_user_customizations() {

// clear global styles
$global_styles_controller = new Gutenberg_REST_Global_Styles_Controller();
$update_request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' );
$update_request->set_param(
'id',
WP_Theme_JSON_Resolver::get_user_global_styles_post_id()
);
$update_request->set_param( 'settings', [] );
$update_request->set_param( 'styles', [] );
$global_styles_controller->update_item( $update_request );

// delete global styles transients
delete_transient( 'global_styles' );
delete_transient( 'global_styles_' . get_stylesheet() );
delete_transient( 'gutenberg_global_styles' );
delete_transient( 'gutenberg_global_styles_' . get_stylesheet() );

// remove all user templates and template parts
$user_customisations = $this->get_user_customisations();
foreach ( $user_customisations['templates'] as $template ) {
wp_delete_post($template->wp_id, true);
}
foreach ( $user_customisations['template_parts'] as $template_part ) {
wp_delete_post($template->wp_id, true);
}
}
}
Loading

0 comments on commit bd72aed

Please sign in to comment.