-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a 'save' option #51
Changes from 5 commits
007a059
e288d4a
9353fa8
914b29b
bba033d
79e0bce
70bd0f4
16de514
edbce49
4f677da
0b30ee4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,45 @@ function create_admin_menu() { | |
add_theme_page( $page_title, $menu_title, 'edit_theme_options', 'create-block-theme', [ $this, 'create_admin_form_page' ] ); | ||
} | ||
|
||
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() { | ||
|
||
// Clear all values in the user theme.json | ||
$user_custom_post_type_id = WP_Theme_JSON_Resolver_Gutenberg::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() ); | ||
|
||
//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 ( $templates as $template ) { | ||
if ( $template->source !== 'custom' ) { | ||
continue; | ||
} | ||
wp_delete_post($template->wp_id, true); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Export activated child theme | ||
*/ | ||
|
@@ -236,11 +275,19 @@ function add_theme_json_to_zip ( $zip, $export_type ) { | |
$theme_json = MY_Theme_JSON_Resolver::export_theme_data( $export_type ); | ||
$zip->addFromString( | ||
'theme.json', | ||
wp_json_encode( $theme_json, JSON_PRETTY_PRINT ) | ||
wp_json_encode( $theme_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) | ||
); | ||
return $zip; | ||
} | ||
|
||
function add_theme_json_to_local ( $export_type ) { | ||
$theme_json = MY_Theme_JSON_Resolver::export_theme_data( $export_type ); | ||
file_put_contents( | ||
get_template_directory() . '/theme.json', | ||
wp_json_encode( $theme_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) | ||
); | ||
} | ||
|
||
function copy_theme_to_zip( $zip, $new_slug, $new_name ) { | ||
|
||
// Get real path for our folder | ||
|
@@ -376,6 +423,10 @@ function add_templates_to_zip( $zip, $export_type, $new_slug ) { | |
|
||
$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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like the original content was encoded, is it maybe this what's happening? Maybe we can use the decode function instead in case there's more than dashes that we should consider? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, very good call. Thank you. That felt pretty shabby when I wrote it. :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried very hard to do this. :( AFAICT this is something that should be addressed in Gutenberg and until it is perhaps this "good enough" solution will be ok? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mentioned it to @scruffian on his latest PR to the export functionality in GB, hopefully there will be a fix for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this actually happens when the template is modified using the site editor... |
||
|
||
if ( $new_slug ) { | ||
$template->content = str_replace( $old_slug, $new_slug, $template->content ); | ||
} | ||
|
@@ -401,6 +452,10 @@ function add_templates_to_zip( $zip, $export_type, $new_slug ) { | |
|
||
$template_part->content = _remove_theme_attribute_in_block_template_content( $template_part->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_part->content = str_replace( '\u002d', '-', $template_part->content ); | ||
|
||
if ( $new_slug ) { | ||
$template_part->content = str_replace( $old_slug, $new_slug, $template_part->content ); | ||
} | ||
|
@@ -414,6 +469,70 @@ function add_templates_to_zip( $zip, $export_type, $new_slug ) { | |
return $zip; | ||
} | ||
|
||
function add_templates_to_local( $export_type ) { | ||
//NOTE: This was heavily copied from add_templates_to_zip | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I agree this needs a refactor ideally. |
||
// and much of this logic can probably be refactored into a common spot | ||
|
||
$templates = gutenberg_get_block_templates(); | ||
$template_parts = gutenberg_get_block_templates( array(), 'wp_template_part' ); | ||
|
||
// 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'] . '/'; | ||
|
||
foreach ( $templates as $template ) { | ||
if ($template->source === 'theme' && $export_type === 'user') { | ||
continue; | ||
} | ||
|
||
if ( | ||
$template->source === 'theme' && | ||
$export_type === 'current' && | ||
! file_exists( $templates_path . $template->slug . '.html' ) | ||
) { | ||
continue; | ||
} | ||
|
||
$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 ); | ||
|
||
file_put_contents( | ||
get_template_directory() . '/templates/' . $template->slug . '.html', | ||
$template->content | ||
); | ||
} | ||
|
||
foreach ( $template_parts as $template_part ) { | ||
if ($template_part->source === 'theme' && $export_type === 'user') { | ||
continue; | ||
} | ||
|
||
if ( | ||
$template_part->source === 'theme' && | ||
$export_type === 'current' && | ||
! file_exists( $parts_path . $template_part->slug . '.html' ) | ||
) { | ||
continue; | ||
} | ||
|
||
|
||
$template_part->content = _remove_theme_attribute_in_block_template_content( $template_part->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_part->content = str_replace( '\u002d', '-', $template_part->content ); | ||
|
||
file_put_contents( | ||
get_template_directory() . '/parts/' . $template_part->slug . '.html', | ||
$template_part->content | ||
); | ||
} | ||
} | ||
|
||
function create_zip( $filename ) { | ||
if ( ! class_exists( 'ZipArchive' ) ) { | ||
return new WP_Error( 'Zip Export not supported.' ); | ||
|
@@ -510,7 +629,7 @@ function create_admin_form_page() { | |
?> | ||
<div class="wrap"> | ||
<h2><?php _e('Create Block Theme', 'create-block-theme'); ?></h2> | ||
<p><?php _e('Save your current block them with changes you made to Templates, Template Parts and Global Styles.', 'create-block-theme'); ?></p> | ||
<p><?php _e('Export your current block them with changes you made to Templates, Template Parts and Global Styles.', 'create-block-theme'); ?></p> | ||
<form method="get"> | ||
|
||
<label><input checked value="export" type="radio" name="theme[type]" class="regular-text code" onchange="document.getElementById('new_theme_metadata_form').setAttribute('hidden', null);" /><?php _e('Export ', 'create-block-theme'); echo wp_get_theme()->get('Name'); ?></label> | ||
|
@@ -525,6 +644,8 @@ function create_admin_form_page() { | |
<label><input value="clone" type="radio" name="theme[type]" class="regular-text code" onchange="document.getElementById('new_theme_metadata_form').removeAttribute('hidden');"/><?php _e('Clone ', 'create-block-theme'); echo wp_get_theme()->get('Name'); ?> | ||
<?php _e('[Create a new theme cloning the activated theme. The resulting theme will have all of the assets of the activated theme as well as user changes.]', 'create-block-theme'); ?></label><br /><br /> | ||
<?php endif; ?> | ||
<label><input value="save" type="radio" name="theme[type]" class="regular-text code" onchange="document.getElementById('new_theme_metadata_form').setAttribute('hidden', null);" /><?php _e('Overwrite ', 'create-block-theme'); echo wp_get_theme()->get('Name'); ?></label> | ||
<?php _e('[Save USER changes as THEME changes and delete the USER changes. Your changes will be saved in the theme on the folder.]', 'create-block-theme'); ?></label><br /><br /> | ||
|
||
<div hidden id="new_theme_metadata_form"> | ||
<label><?php _e('Theme Name', 'create-block-theme'); ?><br /><input type="text" name="theme[name]" class="regular-text" /></label><br /><br /> | ||
|
@@ -535,15 +656,14 @@ function create_admin_form_page() { | |
</div> | ||
<input type="hidden" name="page" value="create-block-theme" /> | ||
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'create_block_theme' ); ?>" /> | ||
<input type="submit" value="<?php _e('Create block theme', 'create-block-theme'); ?>" class="button button-primary" /> | ||
<input type="submit" value="<?php _e('Export theme', 'create-block-theme'); ?>" class="button button-primary" /> | ||
</form> | ||
</div> | ||
<?php | ||
} | ||
|
||
function blockbase_save_theme() { | ||
|
||
// I can't work out how to call the API but this works for now. | ||
if ( ! empty( $_GET['page'] ) && $_GET['page'] === 'create-block-theme' && ! empty( $_GET['theme'] ) ) { | ||
|
||
// Check user capabilities. | ||
|
@@ -556,7 +676,17 @@ function blockbase_save_theme() { | |
return add_action( 'admin_notices', [ $this, 'admin_notice_error' ] ); | ||
} | ||
|
||
if ( is_child_theme() ) { | ||
if ( $_GET['theme']['type'] === 'save' ) { | ||
if ( is_child_theme() ) { | ||
$this->save_theme_locally( 'current' ); | ||
} | ||
else { | ||
$this->save_theme_locally( 'all' ); | ||
} | ||
$this->clear_user_customizations(); | ||
} | ||
|
||
else if ( is_child_theme() ) { | ||
if ( $_GET['theme']['type'] === 'sibling' ) { | ||
$this->create_sibling_theme( $_GET['theme'] ); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this depend on another plugin? That's not ideal...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not another plugin, just this that we had already added previously. All stuff that we had hoped would ultimately eventually be done by Gutenberg... maybe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this suffers from the same issues as the Gutenberg exporter, in that it removes $schema and appearanceTools keys. Feel free to use the code from that PR until it is resolved...