• Resolved Elango

    (@steelthemeshelp)


    Hi ,

    I’m currently creating a theme wizard setup for my WordPress theme.

    I want to include the demo content import feature within the theme wizard setup instead of having it under Appearance. However, I tried implementing it, but it’s not working as expected.

    Could you please help me resolve this issue? I would really appreciate any guidance or suggestions.


    <?php

    /**

    * Theme Setup Wizard with Integrated Plugin Installation and Demo Import

    *

    * This file handles the theme setup wizard functionality with inline plugin installation

    * and demo content import without redirecting to separate pages

    * Place this file in your theme's includes/main/Demo/ directory

    */

    // Prevent direct access

    if (!defined('ABSPATH')) {

    exit;

    }

    // Include dependencies

    require_once get_template_directory() . '/includes/main/Demo/server-capability-check.php';

    /**

    * Theme Setup Wizard Class

    */

    class Integrated_Theme_Setup_Wizard {

    /**

    * Current step

    */

    private $step = '';

    /**

    * Steps for the setup wizard

    */

    private $steps = array();

    /**

    * Theme name

    */

    private $theme_name = '';

    /**

    * Required plugins

    */

    private $required_plugins = array();

    /**

    * Constructor

    */

    /**

    * Constructor

    */

    public function __construct() {

    $this->theme_name = wp_get_theme()->get('Name');

    // Define the wizard steps

    $this->steps = array(

    'welcome' => array(

    'name' => __('Welcome', 'coreit'),

    'view' => array($this, 'welcome_step'),

    'handler' => '',

    ),

    'plugins' => array(

    'name' => __('Plugins', 'coreit'),

    'view' => array($this, 'plugins_step'),

    'handler' => '',

    ),

    'demo_import' => array(

    'name' => __('Demo Import', 'coreit'),

    'view' => array($this, 'demo_import_step'),

    'handler' => '',

    ),

    'done' => array(

    'name' => __('Done', 'coreit'),

    'view' => array($this, 'done_step'),

    'handler' => '',

    ),

    );

    // Get required plugins

    $this->get_required_plugins();

    // Add admin menu

    add_action('admin_menu', array($this, 'add_admin_menu'));

    // Add admin scripts

    add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));

    // AJAX handlers

    add_action('wp_ajax_install_child_theme', array($this, 'ajax_install_child_theme'));

    add_action('wp_ajax_install_plugin', array($this, 'ajax_install_plugin'));

    add_action('wp_ajax_activate_plugin', array($this, 'ajax_activate_plugin'));

    // Check current step

    if (isset($_GET['step'])) {

    $this->step = sanitize_key($_GET['step']);

    } else {

    $this->step = array_keys($this->steps)[0];

    }

    // Handle form submissions

    if (!empty($_POST) && isset($_POST['wizard_nonce'])) {

    check_admin_referer('wizard_nonce', 'wizard_nonce');

    if (isset($_POST['save_step']) && isset($this->steps[$this->step]['handler'])) {

    call_user_func($this->steps[$this->step]['handler']);

    }

    wp_redirect(esc_url_raw($this->get_next_step_link()));

    exit;

    }

    }

    /**

    * Get required plugins from the existing TGMPA configuration

    */

    private function get_required_plugins() {

    // Extract plugins from your existing code

    $this->required_plugins = array(

    array(

    'name' => 'Elementor',

    'slug' => 'elementor',

    'source' => '',

    'required' => false,

    ),

    array(

    'name' => 'One Click Demo Import',

    'slug' => 'one-click-demo-import',

    'source' => '',

    'required' => true,

    ),

    array(

    'name' => 'WooCommerce',

    'slug' => 'woocommerce',

    'source' => '',

    'required' => false,

    ),

    array(

    'name' => 'Contact Form 7',

    'slug' => 'contact-form-7',

    'source' => '',

    'required' => false,

    ),

    array(

    'name' => 'CoreIt Addons',

    'slug' => 'coreit-addons',

    'source' => get_template_directory() . '/includes/admin/dashboard/pluigns/files/coreit-addons.zip',

    'required' => false,

    ),

    array(

    'name' => 'The MailChimp for WordPress',

    'slug' => 'mailchimp-for-wp',

    'source' => '',

    'required' => false,

    ),

    array(

    'name' => 'Amelia Appointment Booking',

    'slug' => 'ameliabooking',

    'source' => '',

    'required' => false,

    ),

    array(

    'name' => 'Translate WordPress with GTranslate',

    'slug' => 'gtranslate',

    'source' => '',

    'required' => false,

    ),

    array(

    'name' => 'WP Job Manager',

    'slug' => 'wp-job-manager',

    'source' => '',

    'required' => false,

    ),

    array(

    'name' => 'CoreIt Template Importer',

    'slug' => 'coreit-template-importer',

    'source' => get_template_directory() . '/includes/admin/dashboard/pluigns/files/coreit-template-importer.zip',

    'required' => false,

    ),

    );

    }

    /**

    * Add admin menu

    */

    public function add_admin_menu() {

    add_menu_page(

    sprintf(__('%s Setup', 'coreit'), $this->theme_name),

    sprintf(__('%s', 'coreit'), $this->theme_name),

    'manage_options',

    'coreit',

    array($this, 'setup_wizard'),

    'dashicons-admin-appearance',

    2

    );

    }

    /**

    * Enqueue scripts and styles

    */

    public function enqueue_scripts() {

    $screen = get_current_screen();

    // Check for the coreit page ID

    if ($screen->id !== 'toplevel_page_coreit') {

    return;

    }

    wp_enqueue_style('theme-setup-wizard', get_template_directory_uri() . '/includes/main/Demo/assets/css/wizard.css', array(), '1.0.0');

    wp_enqueue_script('theme-setup-wizard', get_template_directory_uri() . '/includes/main/Demo/assets/js/wizard.js', array('jquery'), '1.0.0', true);

    wp_localize_script('theme-setup-wizard', 'theme_setup_wizard', array(

    'ajaxurl' => admin_url('admin-ajax.php'),

    'nonce' => wp_create_nonce('theme_setup_wizard'),

    'admin_url' => admin_url('admin.php?page=coreit'),

    'plugin_texts' => array(

    'installing' => __('Installing...', 'coreit'),

    'installed' => __('Installed', 'coreit'),

    'install_failed' => __('Installation Failed', 'coreit'),

    'activating' => __('Activating...', 'coreit'),

    'activated' => __('Activated', 'coreit'),

    'activation_failed' => __('Activation Failed', 'coreit'),

    ),

    ));

    }

    /**

    * Get the next step link

    */

    public function get_next_step_link() {

    $keys = array_keys($this->steps);

    $current_step_index = array_search($this->step, $keys);

    if ($current_step_index < count($keys) - 1) {

    $next_step = $keys[$current_step_index + 1];

    return add_query_arg('step', $next_step, admin_url('admin.php?page=coreit'));

    }

    return admin_url();

    }

    /**

    * Get the previous step link

    */

    public function get_prev_step_link() {

    $keys = array_keys($this->steps);

    $current_step_index = array_search($this->step, $keys);

    if ($current_step_index > 0) {

    $prev_step = $keys[$current_step_index - 1];

    return add_query_arg('step', $prev_step, admin_url('admin.php?page=coreit'));

    }

    return admin_url();

    }

    /**

    * Setup wizard page

    */

    public function setup_wizard() {

    $this->setup_wizard_header();

    $this->setup_wizard_steps();

    $this->setup_wizard_content();

    $this->setup_wizard_footer();

    }

    /**

    * Setup wizard header

    */

    public function setup_wizard_header() {

    ?>

    <!DOCTYPE html>

    <html <?php language_attributes(); ?>>

    <head>

    <meta charset="<?php bloginfo('charset'); ?>">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title><?php printf(__('%s &rsaquo; Setup Wizard', 'coreit'), $this->theme_name); ?></title>

    <?php wp_print_styles(); ?>

    <?php do_action('admin_print_styles'); ?>

    <?php do_action('admin_head'); ?>

    </head>

    <body class="theme-setup-wizard-body">

    <div class="theme-setup-wizard">

    <header class="wizard-header">

    <h1><?php printf(__('%s Setup', 'coreit'), $this->theme_name); ?></h1>

    </header>

    <?php

    }

    /**

    * Setup wizard steps

    */

    public function setup_wizard_steps() {

    $current_step = $this->step;

    ?>

    <ul class="wizard-steps">

    <?php foreach ($this->steps as $step_key => $step) : ?>

    <li class="<?php echo $step_key === $current_step ? 'active' : ($this->is_step_completed($step_key) ? 'done' : ''); ?>">

    <?php echo esc_html($step['name']); ?>

    </li>

    <?php endforeach; ?>

    </ul>

    <?php

    }

    /**

    * Check if step is completed

    */

    private function is_step_completed($step) {

    $keys = array_keys($this->steps);

    $current_step_index = array_search($this->step, $keys);

    $step_index = array_search($step, $keys);

    return $step_index < $current_step_index;

    }

    /**

    * Setup wizard content

    */

    public function setup_wizard_content() {

    echo '<div class="wizard-content">';

    if (!empty($this->steps[$this->step]['view'])) {

    call_user_func($this->steps[$this->step]['view']);

    }

    echo '</div>';

    }

    /**

    * Setup wizard footer

    */

    public function setup_wizard_footer() {

    ?>

    <footer class="wizard-footer">

    <div class="wizard-footer-links">

    <?php if ($this->step !== array_keys($this->steps)[0]) : ?>

    <a class="button button-secondary" href="<?php echo esc_url($this->get_prev_step_link()); ?>"><?php _e('Back', 'coreit'); ?></a>

    <?php endif; ?>

    <?php if ($this->step !== array_keys($this->steps)[count($this->steps) - 1]) : ?>

    <a class="button button-primary next-step" href="<?php echo esc_url($this->get_next_step_link()); ?>"><?php _e('Next', 'coreit'); ?></a>

    <?php endif; ?>

    </div>

    </footer>

    </div>

    <?php wp_print_scripts(); ?>

    <?php do_action('admin_print_scripts'); ?>

    <?php do_action('admin_footer'); ?>

    </body>

    </html>

    <?php

    exit;

    }

    /**

    * Welcome step

    */

    public function welcome_step() {

    // Get parent theme info for child theme creation

    $parent_theme = wp_get_theme();

    $parent_name = $parent_theme->get('Name');

    $parent_slug = $parent_theme->get_stylesheet();

    $child_slug = $parent_slug . '-child';

    // Check if child theme already exists

    $child_theme_exists = wp_get_theme($child_slug)->exists();

    $child_theme_active = (wp_get_theme()->get_stylesheet() === $child_slug);

    ?>

    <div class="wizard-step-content">

    <h2><?php printf(__('Welcome to %s Setup Wizard', 'coreit'), $this->theme_name); ?></h2>

    <p><?php _e('Thank you for choosing our theme. This wizard will help you set up your website quickly and easily.', 'coreit'); ?></p>

    <p><?php _e('This wizard will guide you through:', 'coreit'); ?></p>

    <ul>

    <li><?php _e('Installing and activating essential plugins', 'coreit'); ?></li>

    <li><?php _e('Creating a child theme (optional but recommended)', 'coreit'); ?></li>

    <li><?php _e('Importing demo content to match our theme demos', 'coreit'); ?></li>

    </ul>

    <p><?php _e('The setup should only take a few minutes to complete.', 'coreit'); ?></p>

    <div class="notice notice-info">

    <p><?php _e('Using a child theme is recommended if you plan to customize your site. Demo content import is optional and can be skipped if you prefer to start with a blank site.', 'coreit'); ?></p>

    </div>

    <?php

    // Display server capability check

    Theme_Server_Capability_Check::display_server_capability_check();

    ?>

    <!-- Child Theme Option Section -->

    <div class="setup-section child-theme-section">

    <h3><?php _e('Child Theme (Optional)', 'coreit'); ?></h3>

    <p><?php _e('A child theme allows you to make changes to the theme without losing your modifications when the theme is updated.', 'coreit'); ?></p>

    <?php if ($child_theme_exists && $child_theme_active) : ?>

    <div class="notice notice-success">

    <p><?php _e('Child theme is already installed and active.', 'coreit'); ?></p>

    </div>

    <p><button class="button button-secondary" disabled><?php _e('Child Theme Activated', 'coreit'); ?></button></p>

    <?php elseif ($child_theme_exists && !$child_theme_active) : ?>

    <div class="notice notice-info">

    <p><?php _e('Child theme is installed but not active. Click the button below to activate it.', 'coreit'); ?></p>

    </div>

    <p><button class="button button-primary install-child-theme" data-action="activate"><?php _e('Activate Child Theme', 'coreit'); ?></button></p>

    <?php else : ?>

    <p><button class="button button-primary install-child-theme" data-action="install"><?php _e('Install & Activate Child Theme', 'coreit'); ?></button></p>

    <?php endif; ?>

    <div class="child-theme-response"></div>

    </div>

    <hr />

    <p><a class="button button-primary button-hero" href="<?php echo esc_url($this->get_next_step_link()); ?>"><?php _e('Let\'s Get Started', 'coreit'); ?></a></p>

    </div>

    <?php

    }

    public function plugins_step() {

    ?>

    <div class="wizard-step-content">

    <h2><?php _e('Install Required Plugins', 'coreit'); ?></h2>

    <p><?php _e('Select the plugins you want to install. Required plugins are mandatory.', 'coreit'); ?></p>

    <div class="plugin-installation">

    <table class="plugin-table">

    <thead>

    <tr>

    <th><input type="checkbox" id="select-all-plugins"></th>

    <th><?php _e('Plugin', 'coreit'); ?></th>

    <th><?php _e('Status', 'coreit'); ?></th>

    <th><?php _e('Action', 'coreit'); ?></th>

    </tr>

    </thead>

    <tbody>

    <?php foreach ($this->required_plugins as $plugin) :

    $plugin_path = $this->get_plugin_path($plugin['slug']);

    $is_installed = file_exists(WP_PLUGIN_DIR . '/' . $plugin_path);

    $is_active = is_plugin_active($plugin_path);

    $plugin_status = 'not-installed';

    if ($is_installed) {

    $plugin_status = $is_active ? 'active' : 'inactive';

    }

    $button_text = '';

    $button_class = '';

    $button_action = '';

    if ($plugin_status === 'not-installed') {

    $button_text = __('Install', 'coreit');

    $button_class = 'install-plugin';

    $button_action = 'install';

    } elseif ($plugin_status === 'inactive') {

    $button_text = __('Activate', 'coreit');

    $button_class = 'activate-plugin';

    $button_action = 'activate';

    } else {

    $button_text = __('Active', 'coreit');

    $button_class = 'button-disabled';

    $button_action = '';

    }

    ?>

    <tr data-plugin-slug="<?php echo esc_attr($plugin['slug']); ?>"

    data-plugin-source="<?php echo esc_attr($plugin['source']); ?>"

    data-plugin-path="<?php echo esc_attr($plugin_path); ?>">

    <td>

    <input type="checkbox"

    class="plugin-checkbox"

    <?php echo $plugin['required'] ? 'checked disabled' : ''; ?>

    data-required="<?php echo $plugin['required'] ? '1' : '0'; ?>">

    </td>

    <td>

    <strong><?php echo esc_html($plugin['name']); ?></strong>

    <?php if ($plugin['required']) : ?>

    <span class="required-label"><?php _e('(Required)', 'coreit'); ?></span>

    <?php else : ?>

    <span class="recommended-label"><?php _e('(Recommended)', 'coreit'); ?></span>

    <?php endif; ?>

    </td>

    <td class="plugin-status <?php echo esc_attr($plugin_status); ?>">

    <?php

    if ($plugin_status === 'active') {

    _e('Active', 'coreit');

    } elseif ($plugin_status === 'inactive') {

    _e('Installed', 'coreit');

    } else {

    _e('Not Installed', 'coreit');

    }

    ?>

    </td>

    <td>

    <?php if ($plugin_status !== 'active') : ?>

    <button

    class="button <?php echo esc_attr($button_class); ?>"

    data-action="<?php echo esc_attr($button_action); ?>">

    <?php echo esc_html($button_text); ?>

    </button>

    <?php else : ?>

    <span class="dashicons dashicons-yes"></span>

    <?php endif; ?>

    </td>

    </tr>

    <?php endforeach; ?>

    </tbody>

    </table>

    <div class="plugin-installation-status">

    <p class="plugin-installation-progress"></p>

    </div>

    <div class="plugin-installation-actions">

    <button class="button button-primary install-selected-plugins"><?php _e('Install Selected Plugins', 'coreit'); ?></button>

    </div>

    </div>

    </div>

    <?php

    }

    public function demo_import_step() {

    // Define available demo imports based on the OCDI import files

    $demos = array(

    array(

    'id' => 0,

    'name' => 'CoreIT Demo Import V1',

    'description' => __('Home Page 1 to 6 and all the inner pages', 'coreit'),

    'screenshot' => get_template_directory_uri() . '/screenshot.jpg',

    'preview_url' => 'https://coreit.themepanthers.com/demo',

    'import_file' => get_parent_theme_file_path('/demo-import/demos/v1/demo-content1.xml'),

    ),

    // Add more demos if needed following the same structure

    );

    ?>

    <div class="wizard-step-content">

    <h2><?php _e('Demo Content Import', 'coreit'); ?></h2>

    <p><?php _e('Choose a demo layout to quickly set up your website.', 'coreit'); ?></p>

    <div class="demo-import-section">

    <?php

    // Check if One Click Demo Import plugin is installed and active

    $ocdi_plugin_path = 'one-click-demo-import/one-click-demo-import.php';

    $is_ocdi_active = is_plugin_active($ocdi_plugin_path);

    if ($is_ocdi_active) : ?>

    <div class="notice notice-info">

    <p><?php _e('One Click Demo Import plugin is installed and ready.', 'coreit'); ?></p>

    </div>

    <div class="demo-selection">

    <?php foreach ($demos as $demo) : ?>

    <div class="demo-item">

    <h3><?php echo esc_html($demo['name']); ?></h3>

    <div class="demo-preview">

    <?php if (!empty($demo['screenshot'])) : ?>

    <img src="<?php echo esc_url($demo['screenshot']); ?>"

    alt="<?php echo esc_attr($demo['name']); ?> Screenshot"

    class="demo-screenshot">

    <?php endif; ?>

    <div class="demo-actions">

    <?php if (!empty($demo['preview_url'])) : ?>

    <a href="<?php echo esc_url($demo['preview_url']); ?>"

    class="button button-secondary"

    target="_blank">

    <?php _e('Preview', 'coreit'); ?>

    </a>

    <?php endif; ?>

    <a href="#"

    class="button button-primary import-demo-button"

    data-demo-id="<?php echo esc_attr($demo['id']); ?>">

    <?php _e('Import Demo', 'coreit'); ?>

    </a>

    </div>

    </div>

    <p><?php echo esc_html($demo['description']); ?></p>

    </div>

    <?php endforeach; ?>

    </div>

    <script>

    jQuery(document).ready(function($) {

    $('.import-demo-button').on('click', function(e) {

    e.preventDefault();

    var demoId = $(this).data('demo-id');

    var importUrl = '<?php echo esc_url(admin_url('themes.php')); ?>?page=one-click-demo-import&step=import&import=' + demoId;

    // Open the import URL in a new tab

    window.open(importUrl, '_blank');

    });

    });

    </script>

    <style>

    .demo-selection {

    display: flex;

    flex-wrap: wrap;

    gap: 20px;

    }

    .demo-item {

    border: 1px solid #ddd;

    padding: 15px;

    width: 300px;

    text-align: center;

    }

    .demo-screenshot {

    max-width: 100%;

    height: auto;

    margin-bottom: 15px;

    }

    .demo-actions {

    display: flex;

    justify-content: center;

    gap: 10px;

    margin-top: 10px;

    }

    </style>

    <?php else : ?>

    <div class="notice notice-warning">

    <p><?php _e('One Click Demo Import plugin is not active. Please install and activate it first.', 'coreit'); ?></p>

    </div>

    <div class="demo-import-actions">

    <a href="<?php echo esc_url(admin_url('admin.php?page=coreit&step=plugins')); ?>"

    class="button button-primary button-hero">

    <?php _e('Go to Plugins Step', 'coreit'); ?>

    </a>

    </div>

    <?php endif; ?>

    </div>

    <div class="demo-import-note">

    <p><?php _e('Note: Importing demo content will add pages, posts, images, and theme settings to your site.', 'coreit'); ?></p>

    <p><?php _e('We recommend doing this on a fresh WordPress installation.', 'coreit'); ?></p>

    </div>

    </div>

    <?php

    }

    /**

    * Child theme handler

    */

    public function child_theme_handler() {

    // This method is intentionally left empty as we use AJAX for child theme installation

    }

    /**

    * Done step

    */

    public function done_step() {

    ?>

    <div class="wizard-step-content">

    <h2><?php _e('Setup Complete!', 'coreit'); ?></h2>

    <p><?php printf(__('Congratulations! %s has been set up successfully.', 'coreit'), $this->theme_name); ?></p>

    <p><?php _e('You can now start customizing your site.', 'coreit'); ?></p>

    <div class="wizard-done-buttons">

    <a class="button button-primary" href="<?php echo esc_url(admin_url('customize.php')); ?>"><?php _e('Customize Theme', 'coreit'); ?></a>

    <a class="button button-secondary" href="<?php echo esc_url(admin_url()); ?>"><?php _e('Go to Dashboard', 'coreit'); ?></a>

    </div>

    </div>

    <?php

    }

    /**

    * Get plugin path based on slug

    */

    private function get_plugin_path($plugin_slug) {

    switch ($plugin_slug) {

    case 'elementor':

    return 'elementor/elementor.php';

    case 'one-click-demo-import':

    return 'one-click-demo-import/one-click-demo-import.php';

    case 'woocommerce':

    return 'woocommerce/woocommerce.php';

    case 'contact-form-7':

    return 'contact-form-7/wp-contact-form-7.php';

    case 'coreit-addons':

    return 'coreit-addons/class-coreit-addons.php';

    case 'mailchimp-for-wp':

    return 'mailchimp-for-wp/mailchimp-for-wp.php';

    case 'ameliabooking':

    return 'ameliabooking/ameliabooking.php';

    case 'gtranslate':

    return 'gtranslate/gtranslate.php';

    case 'wp-job-manager':

    return 'wp-job-manager/wp-job-manager.php';

    case 'coreit-template-importer':

    return 'coreit-template-importer/coreit-template-importer.php';

    default:

    return $plugin_slug . '/' . $plugin_slug . '.php';

    }

    }

    /**

    * AJAX handler for plugin installation

    */

    public function ajax_install_plugin() {

    // Check nonce

    check_ajax_referer('theme_setup_wizard', 'nonce');

    // Check permissions

    if (!current_user_can('install_plugins')) {

    wp_send_json_error(array('message' => __('You do not have permission to install plugins.', 'coreit')));

    }

    // Get plugin slug

    $plugin_slug = isset($_POST['slug']) ? sanitize_text_field($_POST['slug']) : '';

    $plugin_source = isset($_POST['source']) ? sanitize_text_field($_POST['source']) : '';

    if (empty($plugin_slug)) {

    wp_send_json_error(array('message' => __('Plugin slug is required.', 'coreit')));

    }

    // Include required files for plugin installation

    require_once ABSPATH . 'wp-admin/includes/plugin.php';

    require_once ABSPATH . 'wp-admin/includes/plugin-install.php';

    require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

    require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';

    // Check if plugin is already installed

    $plugin_path = $this->get_plugin_path($plugin_slug);

    if (file_exists(WP_PLUGIN_DIR . '/' . $plugin_path)) {

    wp_send_json_success(array(

    'message' => __('Plugin is already installed.', 'coreit'),

    'slug' => $plugin_slug,

    'path' => $plugin_path

    ));

    }

    // Set up the upgrader

    $skin = new WP_Ajax_Upgrader_Skin();

    $upgrader = new Plugin_Upgrader($skin);

    // Install from custom source (bundled plugin) or WordPress repository

    if (!empty($plugin_source)) {

    // Install from local source

    $result = $upgrader->install($plugin_source);

    } else {

    // Get plugin info from repository

    $api = plugins_api('plugin_information', array(

    'slug' => $plugin_slug,

    'fields' => array(

    'short_description' => false,

    'sections' => false,

    'requires' => false,

    'rating' => false,

    'ratings' => false,

    'downloaded' => false,

    'last_updated' => false,

    'added' => false,

    'tags' => false,

    'compatibility' => false,

    'homepage' => false,

    'donate_link' => false,

    ),

    ));

    if (is_wp_error($api)) {

    wp_send_json_error(array('message' => $api->get_error_message()));

    }

    // Install from WordPress repository

    $result = $upgrader->install($api->download_link);

    }

    // Check for installation errors

    if (is_wp_error($result)) {

    wp_send_json_error(array('message' => $result->get_error_message()));

    } elseif (is_wp_error($skin->result)) {

    wp_send_json_error(array('message' => $skin->result->get_error_message()));

    } elseif ($skin->get_errors()->has_errors()) {

    wp_send_json_error(array('message' => $skin->get_error_messages()));

    } elseif (is_null($result)) {

    wp_send_json_error(array('message' => __('Plugin installation failed for an unknown reason.', 'coreit')));

    }

    // Installation was successful, return success response

    wp_send_json_success(array(

    'message' => __('Plugin installed successfully.', 'coreit'),

    'slug' => $plugin_slug,

    'path' => $plugin_path

    ));

    }

    /**

    * AJAX handler for plugin activation

    */

    public function ajax_activate_plugin() {

    // Check nonce

    check_ajax_referer('theme_setup_wizard', 'nonce');

    // Check permissions

    if (!current_user_can('activate_plugins')) {

    wp_send_json_error(array('message' => __('You do not have permission to activate plugins.', 'coreit')));

    }

    // Get plugin path

    $plugin_path = isset($_POST['path']) ? sanitize_text_field($_POST['path']) : '';

    if (empty($plugin_path)) {

    wp_send_json_error(array('message' => __('Plugin path is required.', 'coreit')));

    }

    // Include required files

    require_once ABSPATH . 'wp-admin/includes/plugin.php';

    // Check if plugin is already active

    if (is_plugin_active($plugin_path)) {

    wp_send_json_success(array(

    'message' => __('Plugin is already active.', 'coreit'),

    'path' => $plugin_path

    ));

    }

    // Activate the plugin

    $result = activate_plugin($plugin_path);

    // Check for activation errors

    if (is_wp_error($result)) {

    wp_send_json_error(array('message' => $result->get_error_message()));

    }

    // Activation was successful, return success response

    wp_send_json_success(array(

    'message' => __('Plugin activated successfully.', 'coreit'),

    'path' => $plugin_path

    ));

    }

    /**

    * AJAX handler for child theme installation

    */

    public function ajax_install_child_theme() {

    // Check nonce

    check_ajax_referer('theme_setup_wizard', 'nonce');

    // Check permissions

    if (!current_user_can('install_themes')) {

    wp_send_json_error(array('message' => __('You do not have permission to install themes.', 'coreit')));

    }

    // Get parent theme info

    $parent_theme = wp_get_theme();

    $parent_name = $parent_theme->get('Name');

    $parent_slug = $parent_theme->get_stylesheet();

    $child_slug = $parent_slug . '-child';

    // Check if child theme already exists and activate it

    if (wp_get_theme($child_slug)->exists() && isset($_POST['action_type']) && $_POST['action_type'] === 'activate') {

    // Activate the child theme WITHOUT SWITCHING THE CURRENT SESSION

    update_option('stylesheet', $child_slug);

    update_option('template', $parent_slug);

    // Send success response without redirect

    wp_send_json_success(array(

    'message' => sprintf(

    __('%s child theme has been activated successfully. Your changes will be visible after completing the wizard or refreshing.', 'coreit'),

    $parent_name

    ),

    'redirect' => false,

    'status' => 'activated'

    ));

    return;

    }

    // Create child theme directory

    $child_dir = WP_CONTENT_DIR . '/themes/' . $child_slug;

    if (!wp_mkdir_p($child_dir)) {

    wp_send_json_error(array('message' => __('Could not create child theme directory.', 'coreit')));

    return;

    }

    // Create style.css with CORRECT parent theme reference

    $css = "/*

    Theme Name: {$parent_name} Child

    Theme URI: https://themeforest.net/category/wordpress?term=steelthemes&view=grid#content

    Description: This is a child theme of {$parent_name}

    Author: SteelThemes

    Author URI: https://themeforest.net/category/wordpress?term=steelthemes&view=grid#content

    Template: {$parent_slug}

    Version: 1.0.0

    License: GNU General Public License v2 or later

    License URI: http://www.gnu.org/licenses/gpl-2.0.html

    Text Domain: {$parent_slug}-child

    */

    /* Add your custom styles below this line */

    ";

    // Write style.css

    $wrote_css = file_put_contents($child_dir . '/style.css', $css);

    if (!$wrote_css) {

    wp_send_json_error(array('message' => __('Could not create style.css file.', 'coreit')));

    return;

    }

    // Create functions.php with proper parent theme enqueuing

    $php = "<?php

    /**

    * {$parent_name} Child Theme functions and definitions

    */

    // Enqueue parent and child theme styles correctly

    function {$parent_slug}_child_enqueue_styles() {

    // First enqueue the parent theme style

    wp_enqueue_style('{$parent_slug}-style',

    get_template_directory_uri() . '/style.css',

    array(),

    wp_get_theme('{$parent_slug}')->get('Version')

    );

    // Then enqueue the child theme style

    wp_enqueue_style('{$parent_slug}-child-style',

    get_stylesheet_directory_uri() . '/style.css',

    array('{$parent_slug}-style'),

    wp_get_theme()->get('Version')

    );

    }

    add_action('wp_enqueue_scripts', '{$parent_slug}_child_enqueue_styles');

    /**

    * Add your custom functions below this line

    */

    ";

    // Write functions.php

    $wrote_php = file_put_contents($child_dir . '/functions.php', $php);

    if (!$wrote_php) {

    wp_send_json_error(array('message' => __('Could not create functions.php file.', 'coreit')));

    return;

    }

    // Create screenshot.png by copying parent theme's screenshot

    if (file_exists(get_template_directory() . '/screenshot.png')) {

    $copied_screenshot = copy(

    get_template_directory() . '/screenshot.png',

    $child_dir . '/screenshot.png'

    );

    }

    // Activate the child theme for the site, but NOT for the current session

    update_option('stylesheet', $child_slug);

    update_option('template', $parent_slug);

    wp_send_json_success(array(

    'message' => sprintf(

    __('%s child theme has been installed and activated successfully. Your changes will be visible after completing the wizard or refreshing.', 'coreit'),

    $parent_name

    ),

    'redirect' => false,

    'status' => 'installed_activated'

    ));

    }

    }

    // Initialize the class

    new Integrated_Theme_Setup_Wizard();



    Thanks
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Darshana

    (@darshanaw)

    Hi @steelthemeshelp,

    Thank you for reaching out. While we don’t directly support custom development requests, we can guide you towards finding solutions if you are experiencing specific issues.

    In order to assist you better, could you please provide us with more detail regarding the issue you have experienced? If there are error messages showing up, please include them. Additionally, sharing the contents of any relevant error logs will help us to check with our developers.

    Thank you!

    Plugin Support Darshana

    (@darshanaw)

    Hi @steelthemeshelp,

    We haven’t heard from you in a while, so I’m going to go ahead and close out this thread for now. If you’re still having trouble, feel free to respond here at your convenience and we’ll be happy to help.

    Have a great day!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Help With once click’ is closed to new replies.