Skip to content

Commit

Permalink
Save a theme variation (#90)
Browse files Browse the repository at this point in the history
* save the variation locally

* generate a zip with the variation

* shanged wording, added script to toggle between forms

* toggle both when we don't want any form

* typo

* save theme with chosen name, alert when none was entered

* clear user customizations

* fix copy + paste mistake

* fix success message

* wording and paths

* duplicate variations with the same name instead of replacing them
  • Loading branch information
MaggieCabrera authored Jul 29, 2022
1 parent 0b589f4 commit f6bc1ff
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 14 deletions.
109 changes: 95 additions & 14 deletions admin/class-create-block-theme-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ function create_admin_menu() {
$page_title=_x('Create Block Theme', 'UI String', 'create-block-theme');
$menu_title=_x('Create Block Theme', 'UI String', 'create-block-theme');
add_theme_page( $page_title, $menu_title, 'edit_theme_options', 'create-block-theme', [ $this, 'create_admin_form_page' ] );
add_action('admin_enqueue_scripts', [ $this, 'form_script' ] );
}

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

function save_variation ( $export_type, $theme ) {
$this->add_theme_json_variation_to_local( $export_type, $theme );
}

function clear_user_customizations() {

// Clear all values in the user theme.json
Expand Down Expand Up @@ -331,6 +336,31 @@ function add_theme_json_to_local ( $export_type ) {
);
}

function add_theme_json_variation_to_local ( $export_type, $theme ) {
$variation_slug = sanitize_title( $theme['variation'] );
$variation_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'styles' . DIRECTORY_SEPARATOR;
$file_counter = 0;

if ( ! file_exists( $variation_path ) ) {
mkdir( $variation_path, 0755, true );
}

if ( file_exists( $variation_path . $variation_slug . '.json' ) ) {
$file_counter++;
while ( file_exists( $variation_path . $variation_slug . '_' . $file_counter . '.json' ) ) {
$file_counter++;
}
$variation_slug = $variation_slug . '_' . $file_counter;
}

$_GET['theme']['variation_slug'] = $variation_slug;

file_put_contents(
$variation_path . $variation_slug . '.json',
MY_Theme_JSON_Resolver::export_theme_data( $export_type )
);
}

function copy_theme_to_zip( $zip, $new_slug, $new_name ) {

// Get real path for our folder
Expand Down Expand Up @@ -663,14 +693,14 @@ function create_admin_form_page() {
<p><?php printf( esc_html__('Export your current block theme (%1$s) with changes you made to Templates, Template Parts and Global Styles.', 'create-block-theme'), esc_html( wp_get_theme()->get('Name') ) ); ?></p>

<label>
<input checked value="export" type="radio" name="theme[type]" class="regular-text code" onchange="document.getElementById('new_theme_metadata_form').setAttribute('hidden', null);" />
<input checked value="export" type="radio" name="theme[type]" class="regular-text code" onchange="toggleForm( 'new_theme_metadata_form', true );toggleForm( 'new_variation_metadata_form', true );" />
<?php _e('Export ', 'create-block-theme'); echo wp_get_theme()->get('Name'); ?><br />
<?php _e('[Export the activated theme with user changes]', 'create-block-theme'); ?>
</label>
<br /><br />
<?php if ( is_child_theme() ): ?>
<label>
<input value="sibling" type="radio" name="theme[type]" class="regular-text code" onchange="document.getElementById('new_theme_metadata_form').removeAttribute('hidden');"/>
<input value="sibling" type="radio" name="theme[type]" class="regular-text code" onchange="toggleForm( 'new_theme_metadata_form', false );"/>
<?php _e('Create sibling of ', 'create-block-theme'); echo wp_get_theme()->get('Name'); ?>
</label>
<br />
Expand All @@ -679,29 +709,35 @@ function create_admin_form_page() {
<br />
<?php else: ?>
<label>
<input value="child" type="radio" name="theme[type]" class="regular-text code" onchange="document.getElementById('new_theme_metadata_form').removeAttribute('hidden');"/>
<input value="child" type="radio" name="theme[type]" class="regular-text code" onchange="toggleForm( 'new_theme_metadata_form', false );"/>
<?php _e('Create child of ', 'create-block-theme'); echo wp_get_theme()->get('Name'); ?>
</label>
<br />
<?php _e('[Create a new child theme. The currently activated theme will be the parent theme.]', 'create-block-theme'); ?>
<br /><br />
<label>
<input value="clone" type="radio" name="theme[type]" class="regular-text code" onchange="document.getElementById('new_theme_metadata_form').removeAttribute('hidden');"/>
<input value="clone" type="radio" name="theme[type]" class="regular-text code" onchange="toggleForm( 'new_theme_metadata_form', false );"/>
<?php _e('Clone ', 'create-block-theme'); echo wp_get_theme()->get('Name'); ?><br />
<?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);" />
<input value="save" type="radio" name="theme[type]" class="regular-text code" onchange="toggleForm( 'new_theme_metadata_form', true );toggleForm( 'new_variation_metadata_form', true );" />
<?php _e('Overwrite ', 'create-block-theme'); echo wp_get_theme()->get('Name'); ?><br />
<?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 />
<label>
<input value="blank" type="radio" name="theme[type]" class="regular-text code" onchange="document.getElementById('new_theme_metadata_form').removeAttribute('hidden', null);" />
<input value="blank" type="radio" name="theme[type]" class="regular-text code" onchange="toggleForm( 'new_theme_metadata_form', false );" />
<?php _e('Create blank theme ', 'create-block-theme'); ?><br />
<?php _e('[Generates a boilerplate "empty" theme inside of this site\'s themes directory.]', 'create-block-theme'); ?>
<?php _e('[Generate a boilerplate "empty" theme inside of this site\'s themes directory.]', 'create-block-theme'); ?>
</label>
<br /><br />
<label>
<input value="variation" type="radio" name="theme[type]" class="regular-text code" onchange="toggleForm( 'new_variation_metadata_form', false );" />
<?php _e('Create a style variation ', 'create-block-theme'); ?><br />
<?php printf( esc_html__('[Save user changes as a style variation of %1$s.]', 'create-block-theme'), esc_html( wp_get_theme()->get('Name') ) ); ?>
</label>
<br /><br />

Expand All @@ -711,6 +747,12 @@ function create_admin_form_page() {
</div>
<div id="col-right">
<div class="col-wrap">
<div hidden id="new_variation_metadata_form">
<label>
<?php _e('Variation Name (*):', 'create-block-theme'); ?><br />
<input placeholder="<?php _e('Variation Name', 'create-block-theme'); ?>" type="text" name="theme[variation]" class="large-text" />
</label>
</div>
<div hidden id="new_theme_metadata_form">
<label>
<?php _e('Theme Name (*):', 'create-block-theme'); ?><br />
Expand Down Expand Up @@ -751,18 +793,22 @@ function create_admin_form_page() {
<?php
}

function form_script() {
wp_enqueue_script('form-script', plugin_dir_url(__FILE__) . '/js/form-script.js');
}

function blockbase_save_theme() {

if ( ! empty( $_GET['page'] ) && $_GET['page'] === 'create-block-theme' && ! empty( $_GET['theme'] ) ) {

// Check user capabilities.
if ( ! current_user_can( 'edit_theme_options' ) ) {
return add_action( 'admin_notices', [ $this, 'admin_notice_error' ] );
return add_action( 'admin_notices', [ $this, 'admin_notice_error_theme_name' ] );
}

// Check nonce
if ( ! wp_verify_nonce( $_GET['nonce'], 'create_block_theme' ) ) {
return add_action( 'admin_notices', [ $this, 'admin_notice_error' ] );
return add_action( 'admin_notices', [ $this, 'admin_notice_error_theme_name' ] );
}

if ( $_GET['theme']['type'] === 'save' ) {
Expand All @@ -777,9 +823,26 @@ function blockbase_save_theme() {
add_action( 'admin_notices', [ $this, 'admin_notice_save_success' ] );
}

else if ( $_GET['theme']['type'] === 'variation' ) {

if ( $_GET['theme']['variation'] === '' ) {
return add_action( 'admin_notices', [ $this, 'admin_notice_error_variation_name' ] );
}

if ( is_child_theme() ) {
$this->save_variation( 'current', $_GET['theme'] );
}
else {
$this->save_variation( 'all', $_GET['theme'] );
}
$this->clear_user_customizations();

add_action( 'admin_notices', [ $this, 'admin_notice_variation_success' ] );
}

else if ( $_GET['theme']['type'] === 'blank' ) {
if ( $_GET['theme']['name'] === '' ) {
return add_action( 'admin_notices', [ $this, 'admin_notice_error' ] );
return add_action( 'admin_notices', [ $this, 'admin_notice_error_theme_name' ] );
}
$this->create_blank_theme( $_GET['theme'] );

Expand All @@ -789,7 +852,7 @@ function blockbase_save_theme() {
else if ( is_child_theme() ) {
if ( $_GET['theme']['type'] === 'sibling' ) {
if ( $_GET['theme']['name'] === '' ) {
return add_action( 'admin_notices', [ $this, 'admin_notice_error' ] );
return add_action( 'admin_notices', [ $this, 'admin_notice_error_theme_name' ] );
}
$this->create_sibling_theme( $_GET['theme'] );
}
Expand All @@ -800,13 +863,13 @@ function blockbase_save_theme() {
} else {
if( $_GET['theme']['type'] === 'child' ) {
if ( $_GET['theme']['name'] === '' ) {
return add_action( 'admin_notices', [ $this, 'admin_notice_error' ] );
return add_action( 'admin_notices', [ $this, 'admin_notice_error_theme_name' ] );
}
$this->create_child_theme( $_GET['theme'] );
}
else if( $_GET['theme']['type'] === 'clone' ) {
if ( $_GET['theme']['name'] === '' ) {
return add_action( 'admin_notices', [ $this, 'admin_notice_error' ] );
return add_action( 'admin_notices', [ $this, 'admin_notice_error_theme_name' ] );
}
$this->clone_theme( $_GET['theme'] );
}
Expand All @@ -819,13 +882,20 @@ function blockbase_save_theme() {
}
}

function admin_notice_error() {
function admin_notice_error_theme_name() {
$class = 'notice notice-error';
$message = __( 'Please specify a theme name.', 'create-block-theme' );

printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
}

function admin_notice_error_variation_name() {
$class = 'notice notice-error';
$message = __( 'Please specify a variation name.', 'create-block-theme' );

printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
}

function admin_notice_export_success() {
?>
<div class="notice notice-success is-dismissible">
Expand All @@ -852,4 +922,15 @@ function admin_notice_blank_success() {
<?php
}

function admin_notice_variation_success() {
$theme_name = wp_get_theme()->get( 'Name' );
$variation_name = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'styles' . DIRECTORY_SEPARATOR . $_GET['theme']['variation_slug'] .'.json';

?>
<div class="notice notice-success is-dismissible">
<p><?php printf( esc_html__( 'Your variation of %1$s has been created successfully. The new variation file is in %2$s', 'create-block-theme' ), esc_html( $theme_name ) , esc_html( $variation_name ) ); ?></p>
</div>
<?php
}

}
9 changes: 9 additions & 0 deletions admin/js/form-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function toggleForm( formID, hide ){
if( formID == 'new_theme_metadata_form' && !hide ) {
toggleForm( 'new_variation_metadata_form', true );
}
if( formID == 'new_variation_metadata_form' && !hide ) {
toggleForm( 'new_theme_metadata_form', true );
}
document.getElementById( formID ).toggleAttribute('hidden', hide);
}

0 comments on commit f6bc1ff

Please sign in to comment.