function build_and_save_theme($options, $new_theme = true)
 {
     global $extm;
     if (!function_exists('WP_Filesystem')) {
         require_once ABSPATH . 'wp-admin/includes/file.php';
     }
     WP_Filesystem();
     global $wp_filesystem;
     // extract tags from string
     $options['Tags'] = explode(' ', $options['Tags']);
     // set template to runway-framework
     $options['Template'] = 'runway-framework';
     // if theme folder unknown name folder like theme name
     if (!isset($options['Folder']) || empty($options['Folder'])) {
         $options['Folder'] = $this->make_theme_folder_from_name($options['Name']);
     } else {
         $options['Folder'] = $this->make_theme_folder_from_name($options['Folder']);
     }
     // check form mode new or edit(duplicate)
     $this->mode = isset($this->mode) ? $this->mode : '';
     if ($this->mode == 'new') {
         if (file_exists($this->themes_path . '/' . $options['Folder'])) {
             return false;
         }
         mkdir($this->themes_path . '/' . $options['Folder']);
     } else {
         // change theme folder
         if (!file_exists($this->themes_path . '/' . $options['Folder'])) {
             rename($this->themes_path . '/' . $_REQUEST['name'], $this->themes_path . '/' . $options['Folder']);
             // change file names into changed theme folder
             $this->rename_history_packages($options['Folder']);
         }
     }
     if (!file_exists($this->themes_path . '/' . $options['Folder'] . '/data')) {
         mkdir($this->themes_path . '/' . $options['Folder'] . '/data');
     }
     // check if have new screenshot and if true move file to theme folder
     if ($_FILES['theme_options']['name']['Screenshot'] != '') {
         imagepng(imagecreatefromstring($wp_filesystem->get_contents($_FILES['theme_options']['tmp_name']['Screenshot'])), $this->themes_path . '/' . $options['Folder'] . '/screenshot.png');
         $options['Screenshot'] = true;
     }
     // check if have new custom icon and if true move file to theme folder
     if ($_FILES['theme_options']['name']['CustomIcon'] != '') {
         imagepng(imagecreatefromstring($wp_filesystem->get_contents($_FILES['theme_options']['tmp_name']['CustomIcon'])), $this->themes_path . '/' . $options['Folder'] . '/custom-icon.png');
         $image = wp_get_image_editor($_FILES['theme_options']['tmp_name']['CustomIcon']);
         if (!is_wp_error($image)) {
             $image->resize(36, 36);
             $image->save($this->themes_path . '/' . $options['Folder'] . '/custom-icon.png');
         }
         $options['CustomIcon'] = true;
     }
     if (file_exists($this->themes_path . '/' . $options['Folder'] . '/custom-icon.png')) {
         $options['CustomIcon'] = true;
     }
     // If no custom screenshot copy default
     if (file_exists($this->themes_path . '/' . $options['Folder'] . '/screenshot.png')) {
         $options['Screenshot'] = true;
     } else {
         copy($this->themes_path . '/' . $options['Template'] . '/screenshot.png', $this->themes_path . '/' . $options['Folder'] . '/screenshot.png');
         $options['Screenshot'] = true;
     }
     // save settings to JSON
     $theme_prefix = get_theme_prefix($options['Folder']);
     if ($this->mode == 'new') {
         $options['ThemeID'] = create_theme_ID();
     } else {
         $options['ThemeID'] = empty($theme_prefix) ? create_theme_ID() : $theme_prefix;
     }
     $this->save_settings($options['Folder'], $options);
     if ($new_theme) {
         // Add functions.php
         $functions = '';
         if ($this->themes_path . '/' . $options['Template'] . '/functions.php') {
             $functions = '<?php /* child theme functions */ ?>';
         }
         $wp_filesystem->put_contents($this->themes_path . '/' . $options['Folder'] . '/functions.php', $functions, FS_CHMOD_FILE);
         // save settings into wordpress style.css
         $wp_filesystem->put_contents($this->themes_path . '/' . $options['Folder'] . '/style.css', $this->build_theme_css($options), FS_CHMOD_FILE);
     } else {
         $matches = array();
         $css = $wp_filesystem->get_contents($this->themes_path . '/' . $options['Folder'] . '/style.css');
         if (preg_match('/^\\s*\\/\\*\\*!/i', $css)) {
             $is_sass = true;
         } else {
             $is_sass = false;
         }
         $css = preg_replace('/\\/\\*\\*?!?([^\\*]*)\\*?\\*\\//i', '', $css);
         $new_css = $this->build_theme_css($options, false, $is_sass) . $css;
         // save settings into wordpress style.css
         $wp_filesystem->put_contents($this->themes_path . '/' . $options['Folder'] . '/style.css', $new_css, FS_CHMOD_FILE);
     }
     // return settings to enable activate theme popup
     return $options;
 }
function check_theme_ID($folder = false)
{
    global $shortname;
    if (!function_exists('WP_Filesystem')) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
    }
    WP_Filesystem();
    global $wp_filesystem;
    $settings = get_settings_json($folder);
    if (isset($settings['Name'])) {
        $themeInfo = rw_get_theme_data();
        $theme_name_stylecss = $themeInfo['Name'];
        if (isset($settings['ThemeID'])) {
            $theme_prefix_old = isset($settings['ThemeID']) ? $settings['ThemeID'] : $shortname;
            if (change_theme_prefix($theme_prefix_old, $settings['ThemeID'])) {
                $wp_filesystem->put_contents(get_stylesheet_directory() . '/data/settings.json', json_encode($settings), FS_CHMOD_FILE);
            }
        }
        if (empty($theme_name_stylecss)) {
            $theme_name_stylecss = __('Unknown Theme (Theme Name in style.css is empty)', 'framework');
        }
        if (isset($settings['Name']) && $theme_name_stylecss != $settings['Name']) {
            $link = IS_CHILD ? admin_url('admin.php?page=themes') : admin_url('themes.php');
            if (isset($_GET['create-theme-id'])) {
                if ($_GET['create-theme-id']) {
                    $theme_prefix_old = isset($settings['ThemeID']) ? $settings['ThemeID'] : $shortname;
                    $theme_id = create_theme_ID();
                    $settings['ThemeID'] = $theme_id;
                    if (change_theme_prefix($theme_prefix_old, $settings['ThemeID'])) {
                        $settings['Name'] = $theme_name_stylecss;
                        $wp_filesystem->put_contents(get_stylesheet_directory() . '/data/settings.json', json_encode($settings), FS_CHMOD_FILE);
                    }
                } else {
                    $settings['Name'] = $theme_name_stylecss;
                    $wp_filesystem->put_contents(get_stylesheet_directory() . '/data/settings.json', json_encode($settings), FS_CHMOD_FILE);
                }
                $redirect = '<script type="text/javascript">window.location = "' . esc_url($link) . '";</script>';
                echo $redirect;
                // escaped above
            }
            add_action('admin_notices', 'ask_new_theme', 10, 3);
            do_action('admin_notices', $settings['Name'], $theme_name_stylecss, $link);
            remove_action('admin_notices', 'ask_new_theme');
        } else {
            return true;
        }
    }
}
function check_theme_ID($folder = false)
{
    global $shortname;
    if (!function_exists('WP_Filesystem')) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
    }
    WP_Filesystem();
    global $wp_filesystem;
    $settings = get_settings_json($folder);
    if (isset($settings['Name'])) {
        $themeInfo = rw_get_theme_data();
        $theme_name_stylecss = $themeInfo['Name'];
        // $is_prefix_ID = ( isset($settings['ThemeID']) && isset($settings['isPrefixID']) )? $settings['isPrefixID'] : false;
        if (isset($settings['ThemeID'])) {
            //if( !isset($settings['isPrefixID']) || (isset($settings['isPrefixID']) && !$settings['isPrefixID']) ) {
            $theme_prefix_old = isset($settings['ThemeID']) ? $settings['ThemeID'] : $shortname;
            if (change_theme_prefix($theme_prefix_old, $settings['ThemeID'])) {
                //$settings['isPrefixID'] = true;
                $wp_filesystem->put_contents(get_stylesheet_directory() . '/data/settings.json', json_encode($settings), FS_CHMOD_FILE);
                //file_put_contents( get_stylesheet_directory() . '/data/settings.json', json_encode($settings) );
            }
            //}
        }
        if (isset($settings['Name']) && $theme_name_stylecss != $settings['Name']) {
            $link = admin_url('admin.php?page=themes');
            //$link = home_url().$_SERVER['REQUEST_URI'];
            if (isset($_GET['create-theme-id'])) {
                if ($_GET['create-theme-id']) {
                    //$theme_prefix_old = $settings['ThemeID'];
                    $theme_prefix_old = isset($settings['ThemeID']) ? $settings['ThemeID'] : $shortname;
                    $theme_id = create_theme_ID();
                    $settings['ThemeID'] = $theme_id;
                    if (change_theme_prefix($theme_prefix_old, $settings['ThemeID'])) {
                        $settings['Name'] = $theme_name_stylecss;
                        $wp_filesystem->put_contents(get_stylesheet_directory() . '/data/settings.json', json_encode($settings), FS_CHMOD_FILE);
                        //file_put_contents( get_stylesheet_directory() . '/data/settings.json', json_encode($settings) );
                    }
                } else {
                    $settings['Name'] = $theme_name_stylecss;
                    $wp_filesystem->put_contents(get_stylesheet_directory() . '/data/settings.json', json_encode($settings), FS_CHMOD_FILE);
                    //file_put_contents( get_stylesheet_directory() . '/data/settings.json', json_encode($settings) );
                }
                $redirect = '<script type="text/javascript">window.location = "' . $link . '";</script>';
                echo $redirect;
            }
            add_action('admin_notices', 'ask_new_theme', 10, 3);
            do_action('admin_notices', $settings['Name'], $theme_name_stylecss, $link);
            remove_action('admin_notices', 'ask_new_theme');
        } else {
            return true;
        }
    }
}