<?php

/**
 * Wizard
 *
 * @package Whizzie
 * @author misbahwp
 * @since 1.0.0
 */

class Whizzie {

	protected $version = '1.1.0';

	/** @var string Current theme name, used as namespace in actions. */
	protected $theme_name = '';
	protected $theme_title = '';

	protected $plugin_path = '';
	protected $parent_slug = '';

	/** @var string Wizard page slug and title. */
	protected $page_slug = '';
	protected $page_title = '';

	/** @var array Wizard steps set by user. */
	protected $config_steps = array();

	/**
	 * Relative plugin url for this plugin folder
	 * @since 1.0.0
	 * @var string
	 */
	protected $plugin_url = '';

	/**
	 * TGMPA instance storage
	 *
	 * @var object
	 */
	protected $tgmpa_instance;

	/**
	 * TGMPA Menu slug
	 *
	 * @var string
	 */
	protected $tgmpa_menu_slug = 'tgmpa-install-plugins';

	/**
	 * TGMPA Menu url
	 *
	 * @var string
	 */
	protected $tgmpa_url = 'themes.php?page=tgmpa-install-plugins';

	// Where to find the widget.wie file
	protected $widget_file_url = '';

	/**
	 * Constructor
	 *
	 * @param $config	Our config parameters
	 */
	public function __construct( $config ) {
		$this->set_vars( $config );
		$this->init();
	}

	/**
	 * Set some settings
	 * @since 1.0.0
	 * @param $config	Our config parameters
	 */
	public function set_vars( $config ) {

		// require_once trailingslashit( WHIZZIE_DIR ) . 'tgm/class-tgm-plugin-activation.php';
		require_once trailingslashit( WHIZZIE_DIR ) . 'tgm/tgm.php';
		// require_once trailingslashit( WHIZZIE_DIR ) . 'widgets/class-ti-widget-importer.php';

		if( isset( $config['page_slug'] ) ) {
			$this->page_slug = esc_attr( $config['page_slug'] );
		}
		if( isset( $config['page_title'] ) ) {
			$this->page_title = esc_attr( $config['page_title'] );
		}
		if( isset( $config['steps'] ) ) {
			$this->config_steps = $config['steps'];
		}

		$this->plugin_path = trailingslashit( dirname( __FILE__ ) );
		$relative_url = str_replace( get_template_directory(), '', $this->plugin_path );
		$this->plugin_url = trailingslashit( get_template_directory_uri() . $relative_url );
		$current_theme = wp_get_theme();
		$this->theme_title = $current_theme->get( 'Name' );
		$this->theme_name = strtolower( preg_replace( '#[^a-zA-Z]#', '', $current_theme->get( 'Name' ) ) );
		$this->page_slug = apply_filters( $this->theme_name . '_theme_setup_wizard_page_slug', $this->theme_name . '-wizard' );
		$this->parent_slug = apply_filters( $this->theme_name . '_theme_setup_wizard_parent_slug', '' );

	}

	public function redirect_to_wizard() {
        update_option( 'dismissed-get_started', false );
    }

	/*
	 * Hooks and filters
	 * @since 1.0.0
	 */
	public function init() {

		add_action( 'after_switch_theme', array( $this, 'redirect_to_wizard' ) );
		if ( class_exists( 'TGM_Plugin_Activation' ) && isset( $GLOBALS['tgmpa'] ) ) {
			add_action( 'init', array( $this, 'get_tgmpa_instance' ), 30 );
			add_action( 'init', array( $this, 'set_tgmpa_url' ), 40 );
		}
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		add_action( 'admin_menu', array( $this, 'menu_page' ) );
		add_action( 'admin_init', array( $this, 'get_plugins' ), 30 );
		add_filter( 'tgmpa_load', array( $this, 'tgmpa_load' ), 10, 1 );
		add_action( 'wp_ajax_setup_plugins', array( $this, 'setup_plugins' ) );
		add_action( 'wp_ajax_setup_widgets', array( $this, 'setup_widgets' ) );

	}

	public function enqueue_scripts($hook) {

		wp_enqueue_style( 'importer-style', get_template_directory_uri() . '/inc/importer/assets/css/importer-style.css');

		wp_register_script( 'importer-script', get_template_directory_uri() . '/inc/importer/assets/js/importer-script.js', array( 'jquery' ), time() );
		wp_localize_script(
			'importer-script',
			'coupon_flash_deals_whizzie_params',
			array(
				'ajaxurl' 		=> admin_url( 'admin-ajax.php' ),
				'wpnonce' 		=> wp_create_nonce( 'whizzie_nonce' ),
				'verify_text'	=> esc_html( 'verifying', 'coupon-flash-deals' )
			)
		);
		wp_enqueue_script( 'importer-script' );

	}

	public static function get_instance() {
		if ( ! self::$instance ) {
			self::$instance = new self;
		}
		return self::$instance;
	}

	public function tgmpa_load( $status ) {
		return is_admin() || current_user_can( 'install_themes' );
	}

	/**
	 * Get configured TGMPA instance
	 *
	 * @access public
	 * @since 1.1.2
	 */
	public function get_tgmpa_instance() {
		$this->tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
	}

	/**
	 * Update $tgmpa_menu_slug and $tgmpa_parent_slug from TGMPA instance
	 *
	 * @access public
	 * @since 1.1.2
	 */
	public function set_tgmpa_url() {
		$this->tgmpa_menu_slug = ( property_exists( $this->tgmpa_instance, 'menu' ) ) ? $this->tgmpa_instance->menu : $this->tgmpa_menu_slug;
		$this->tgmpa_menu_slug = apply_filters( $this->theme_name . '_theme_setup_wizard_tgmpa_menu_slug', $this->tgmpa_menu_slug );
		$tgmpa_parent_slug = ( property_exists( $this->tgmpa_instance, 'parent_slug' ) && $this->tgmpa_instance->parent_slug !== 'themes.php' ) ? 'admin.php' : 'themes.php';
		$this->tgmpa_url = apply_filters( $this->theme_name . '_theme_setup_wizard_tgmpa_url', $tgmpa_parent_slug . '?page=' . $this->tgmpa_menu_slug );
	}

	/**
	 * Make a modal screen for the wizard
	 */
	public function menu_page() {
		add_menu_page( 
			esc_html( $this->page_title ), // Page title
			esc_html( $this->page_title ), // Menu title
			'manage_options', // Capability
			$this->page_slug, // Menu slug
			array( $this, 'coupon_flash_deals_setup_wizard' ) , // Function that renders the page
			'dashicons-admin-generic', // Icon
			2 // Position in the dashboard menu
		);
	}

	/**
	 * Make an interface for the wizard
	 */
	public function wizard_page() {
		tgmpa_load_bulk_installer();
		// install plugins with TGM.
		if ( ! class_exists( 'TGM_Plugin_Activation' ) || ! isset( $GLOBALS['tgmpa'] ) ) {
			die( 'Failed to find TGM' );
		}
		$url = wp_nonce_url( add_query_arg( array( 'plugins' => 'go' ) ), 'whizzie-setup' );

		// copied from TGM
		$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
		$fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.
		if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) {
			return true; // Stop the normal page form from displaying, credential request form will be shown.
		}
		// Now we have some credentials, setup WP_Filesystem.
		if ( ! WP_Filesystem( $creds ) ) {
			// Our credentials were no good, ask the user for them again.
			request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );
			return true;
		}
		/* If we arrive here, we have the filesystem */ ?>
		<div class="wrap">
			<?php printf('<h1>%s</h1>', esc_html($this->page_title));
			echo '<div class="card importer-wrap">';
				// The wizard is a list with only one item visible at a time
				$steps = $this->get_steps();
				echo '<ul class="importer-menu">';
				foreach( $steps as $step ) {
					$class = 'step step-' . esc_attr( $step['id'] );
					echo '<li data-step="' . esc_attr( $step['id'] ) . '" class="' . esc_attr( $class ) . '">';
						printf( '<h2>%s</h2>', esc_html( $step['title'] ) );
						// $content is split into summary and detail
						$content = call_user_func( array( $this, $step['view'] ) );
						if( isset( $content['summary'] ) ) {
							printf(
								'<div class="summary">%s</div>',
								wp_kses_post( $content['summary'] )
							);
						}
						if( isset( $content['detail'] ) ) {
							// Add a link to see more detail
							printf( '<p><a href="#" class="more-info">%s</a></p>', __( 'More Info', 'coupon-flash-deals' ) );
							printf(
								'<div class="detail">%s</div>',
								$content['detail'] // Need to escape this
							);
						}
						// The next button

						if( isset( $step['button_text'] ) && $step['button_text'] ) {
							printf(
								'<div class="button-wrap"><a href="#" class="button button-primary do-it" data-callback="%s" data-step="%s">%s</a></div>',
								esc_attr( $step['callback'] ),
								esc_attr( $step['id'] ),
								esc_html( $step['button_text'] )
							);
						}

						// The skip button
						if( isset( $step['can_skip'] ) && $step['can_skip'] ) {
							printf(
								'<div class="button-wrap" style="margin-left: 0.5em;"><a href="#" class="button button-secondary do-it" data-callback="%s" data-step="%s">%s</a></div>',
								'do_next_step',
								esc_attr( $step['id'] ),
								__( 'Skip', 'coupon-flash-deals' )
							);
						}

					echo '</li>';
				}
				echo '</ul>';
				?>
				<div class="destiny-going"><span class="spinner"></span></div>
			</div><!-- .importer-wrap -->

		</div><!-- .wrap -->
	<?php }

	public function wz_activate_coupon_flash_deals() {

		if ( is_wp_error( $response ) ) {
			$response = array('status' => false, 'msg' => 'Something Went Wrong!');
			wp_send_json($response);
			exit;
		} else {
			$response_body = wp_remote_retrieve_body( $response );
			$response_body = json_decode($response_body);

			if ( $response_body->is_suspended == 1 ) {
			} else {
			}

			if ($response_body->status === false) {
				$response = array('status' => false, 'msg' => $response_body->msg);
				wp_send_json($response);
				exit;
			} else {
				$response = array('status' => true, 'msg' => 'Theme Activated Successfully!');
				wp_send_json($response);
				exit;
			}
		}
	}

	public function coupon_flash_deals_setup_wizard() {
		?>
			<div class="wrapper-info get-stared-page-wrap">
				<div id="demo_offer">
					<?php $this->wizard_page(); ?>
				</div>
			</div>
		<?php
	}

	/**
	 * Set options for the steps
	 * Incorporate any options set by the theme dev
	 * Return the array for the steps
	 * @return Array
	 */
	public function get_steps() {
		$dev_steps = $this->config_steps;
		$steps = array(
			'intro' => array(
				'id'			=> 'intro',
				'title'			=> __( 'Welcome to ', 'coupon-flash-deals' ) . $this->theme_title,
				'icon'			=> 'dashboard',
				'view'			=> 'get_step_intro', // Callback for content
				'callback'		=> 'do_next_step', // Callback for JS
				'button_text'	=> __( 'START NOW', 'coupon-flash-deals' ),
				'can_skip'		=> false // Show a skip button?
			),
			'plugins' => array(
				'id'			=> 'plugins',
				'title'			=> __( 'Plugins', 'coupon-flash-deals' ),
				'icon'			=> 'admin-plugins',
				'view'			=> 'get_step_plugins',
				'callback'		=> 'install_plugins',
				'button_text'	=> __( 'INSTALL PLUGINS', 'coupon-flash-deals' ),
				'can_skip'		=> true
			),
			'widgets' => array(
				'id'			=> 'widgets',
				'title'			=> __( 'Demo Importer', 'coupon-flash-deals' ),
				'icon'			=> 'welcome-widgets-menus',
				'view'			=> 'get_step_widgets',
				'callback'		=> 'install_widgets',
				'button_text'	=> __( 'IMPORT DEMO', 'coupon-flash-deals' ),
				'can_skip'		=> true
			),
			'done' => array(
				'id'			=> 'done',
				'title'			=> __( 'All Done', 'coupon-flash-deals' ),
				'icon'			=> 'yes',
				'view'			=> 'get_step_done',
				'callback'		=> ''
			)
		);

		// Iterate through each step and replace with dev config values
		if( $dev_steps ) {
			// Configurable elements - these are the only ones the dev can update from config.php
			$can_config = array( 'title', 'icon', 'button_text', 'can_skip' );
			foreach( $dev_steps as $dev_step ) {
				// We can only proceed if an ID exists and matches one of our IDs
				if( isset( $dev_step['id'] ) ) {
					$id = $dev_step['id'];
					if( isset( $steps[$id] ) ) {
						foreach( $can_config as $element ) {
							if( isset( $dev_step[$element] ) ) {
								$steps[$id][$element] = $dev_step[$element];
							}
						}
					}
				}
			}
		}
		return $steps;
	}

	/**
	 * Print the content for the intro step
	 */
		public function get_step_intro() { ?>
			<div class="summary">
				<p>
					<?php
					printf(
						esc_html__('Thank you for choosing this %s Theme. Using this quick setup wizard, you will be able to configure your new website and get it running in just a few minutes. Just follow these simple steps mentioned in the wizard and get started with your website.', 'coupon-flash-deals'),
						$this->theme_title
					);
					?>
				</p>
				<p>
					<?php esc_html_e('You may even skip the steps and get back to the dashboard if you have no time at the present moment. You can come back any time if you change your mind.','coupon-flash-deals'); ?>
				</p>
			</div>
		<?php }

	/**
	 * Get the content for the plugins step
	 * @return $content Array
	 */
	public function get_step_plugins() {
		$plugins = $this->get_plugins();
		$content = array(); ?>
			<div class="summary">
				<p>
					<?php esc_html_e('Additional plugins always make your website exceptional. Install these plugins by clicking the install button. You may also deactivate them from the dashboard.', 'coupon-flash-deals') ?>
				</p>
			</div>
		<?php
		$content['detail'] = '<ul class="whizzie-do-plugins">';
		foreach( $plugins['all'] as $slug=>$plugin ) {
			$content['detail'] .= '<li data-slug="' . esc_attr( $slug ) . '">' . '<span class="left-span">' .esc_html( $plugin['name'] ) .'</span>'. '<span class="right-span">';
			$keys = array();
			if ( isset( $plugins['install'][ $slug ] ) ) {
			    $keys[] = 'Installation';
			}
			if ( isset( $plugins['update'][ $slug ] ) ) {
			    $keys[] = 'Update';
			}
			if ( isset( $plugins['activate'][ $slug ] ) ) {
			    $keys[] = 'Activation';
			}
			$content['detail'] .= implode( ' and ', $keys ) . ' required';
			$content['detail'] .= '</span></li>';
		}
		$content['detail'] .= '</ul>';
		
		return $content;
	}

	function moveArrayPosition(&$array, $key, $new_position) {
	    if (!array_key_exists($key, $array)) {
	        return $array;
	    }
	    $item = $array[$key];
	    unset($array[$key]);
	    $result = [];
	    $position_added = false;

	    foreach ($array as $current_key => $current_value) {
	        if (!$position_added && $new_position === count($result)) {
	            $result[$key] = $item;
	            $position_added = true;
	        }
	        $result[$current_key] = $current_value;
	    }
	    if (!$position_added) {
	        $result[$key] = $item;
	    }
	    $array = $result;
	    return $array;
	}

	/**
	 * Print the content for the widgets step
	 * @since 1.1.0
	 */
	public function get_step_widgets() { ?>
		<div class="summary">
			<p>
				<?php esc_html_e('This theme supports importing the demo content and adding widgets. Get them installed with the below button. Using the Customizer, it is possible to update or even deactivate them','coupon-flash-deals'); ?>
			</p>
		</div>
	<?php }

	/**
	 * Print the content for the final step
	 */
	public function get_step_done() { ?>
		<div id="mi-demo-wizard-guide" class="summary">
			<p><?php esc_html_e('Awesome! You have Imported Your Website Successfully.','coupon-flash-deals'); ?></p>
			<div class="mi-wizard-finish">				
				<a target="_blank" href="<?php echo esc_url(admin_url()); ?>" class="button btnPush btnBlueGreen"><?php esc_html_e('FINISH', 'coupon-flash-deals'); ?></a>
				<a target="_blank" href="<?php echo esc_url(home_url()); ?>" class="button btnPush btnBlueGreen"><?php esc_html_e('VISIT SITE', 'coupon-flash-deals'); ?></a>
			</div>
		</div>		
		
	<?php }

	/**
	 * Get the plugins registered with TGMPA
	 */
	public function get_plugins() {
		$instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
		$plugins = array(
			'all' 		=> array(),
			'install'	=> array(),
			'update'	=> array(),
			'activate'	=> array()
		);
		foreach( $instance->plugins as $slug=>$plugin ) {
			if( $instance->is_plugin_active( $slug ) && false === $instance->does_plugin_have_update( $slug ) ) {
				// Plugin is installed and up to date
				continue;
			} else {
				$plugins['all'][$slug] = $plugin;
				if( ! $instance->is_plugin_installed( $slug ) ) {
					$plugins['install'][$slug] = $plugin;
				} else {
					if( false !== $instance->does_plugin_have_update( $slug ) ) {
						$plugins['update'][$slug] = $plugin;
					}
					if( $instance->can_plugin_activate( $slug ) ) {
						$plugins['activate'][$slug] = $plugin;
					}
				}
			}
		}
		return $plugins;
	}

	/**
	 * Get the widgets.wie file from the /content folder
	 * @return Mixed	Either the file or false
	 * @since 1.1.0
	 */

	public function setup_plugins() {
		if ( ! check_ajax_referer( 'whizzie_nonce', 'wpnonce' ) || empty( $_POST['slug'] ) ) {
			wp_send_json_error( array( 'error' => 1, 'message' => esc_html__( 'No Slug Found','coupon-flash-deals' ) ) );
		}
		$json = array();
		// send back some json we use to hit up TGM
		$plugins = $this->get_plugins();

		// what are we doing with this plugin?
		foreach ( $plugins['activate'] as $slug => $plugin ) {
			if ( $_POST['slug'] == $slug ) {
				$json = array(
					'url'           => admin_url( $this->tgmpa_url ),
					'plugin'        => array( $slug ),
					'tgmpa-page'    => $this->tgmpa_menu_slug,
					'plugin_status' => 'all',
					'_wpnonce'      => wp_create_nonce( 'bulk-plugins' ),
					'action'        => 'tgmpa-bulk-activate',
					'action2'       => - 1,
					'message'       => esc_html__( 'Activating Plugin','coupon-flash-deals' ),
				);
				break;
			}
		}
		foreach ( $plugins['update'] as $slug => $plugin ) {
			if ( $_POST['slug'] == $slug ) {
				$json = array(
					'url'           => admin_url( $this->tgmpa_url ),
					'plugin'        => array( $slug ),
					'tgmpa-page'    => $this->tgmpa_menu_slug,
					'plugin_status' => 'all',
					'_wpnonce'      => wp_create_nonce( 'bulk-plugins' ),
					'action'        => 'tgmpa-bulk-update',
					'action2'       => - 1,
					'message'       => esc_html__( 'Updating Plugin','coupon-flash-deals' ),
				);
				break;
			}
		}
		foreach ( $plugins['install'] as $slug => $plugin ) {
			if ( $_POST['slug'] == $slug ) {
				$json = array(
					'url'           => admin_url( $this->tgmpa_url ),
					'plugin'        => array( $slug ),
					'tgmpa-page'    => $this->tgmpa_menu_slug,
					'plugin_status' => 'all',
					'_wpnonce'      => wp_create_nonce( 'bulk-plugins' ),
					'action'        => 'tgmpa-bulk-install',
					'action2'       => - 1,
					'message'       => esc_html__( 'Installing Plugin','coupon-flash-deals' ),
				);
				break;
			}
		}
		if ( $json ) {
			$json['hash'] = md5( serialize( $json ) ); // used for checking if duplicates happen, move to next plugin
			wp_send_json( $json );
		} else {
			wp_send_json( array( 'done' => 1, 'message' => esc_html__( 'Success','coupon-flash-deals' ) ) );
		}
		exit;
	}

	public static function get_page_id_by_title($pagename){

		$args = array(
			'post_type' => 'page',
			'posts_per_page' => 1,
			'post_status' => 'publish',
			'title' => $pagename
		);
		$query = new WP_Query( $args );
		
		$page_id = '1';
		if (isset($query->post->ID)) {
			$page_id = $query->post->ID;
		}
		
		return $page_id;
	}

public function create_theme_nav_menu() {

    // ------- Create Nav Menu --------
    $menuname = 'Primary Menu';
    $bpmenulocation = 'primary_menu';
    $menu_exists = wp_get_nav_menu_object($menuname);

    if (!$menu_exists) {
        $menu_id = wp_create_nav_menu($menuname);

        // Create or fetch 'Blogs' page
        $blog_page = get_page_by_title('Blogs');
        if (!$blog_page) {
            $blog_page_id = wp_insert_post(array(
                'post_title'   => 'Blogs',
                'post_type'    => 'page',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_name'    => 'blogs',
                'post_content' => 'This is the blog listing page.'
            ));
        } else {
            $blog_page_id = $blog_page->ID;
        }

        // Set the Posts page
        update_option('page_for_posts', $blog_page_id);

        // Add Home
        wp_update_nav_menu_item($menu_id, 0, array(
            'menu-item-title'   => __('Home','coupon-flash-deals'),
            'menu-item-classes' => 'home-page',
            'menu-item-url'     => home_url('/'),
            'menu-item-status'  => 'publish',
        ));

        // Add Blogs (Posts Page)
        if ($blog_page_id) {
            wp_update_nav_menu_item($menu_id, 0, array(
                'menu-item-title'   => __('Blogs', 'coupon-flash-deals'),
                'menu-item-classes' => 'blogs',
                'menu-item-object'  => 'page',
                'menu-item-object-id' => $blog_page_id,
                'menu-item-type'    => 'post_type',
                'menu-item-status'  => 'publish',
            ));
        }

        // Add other pages
        wp_update_nav_menu_item($menu_id, 0, array(
            'menu-item-title'   => __('Pages','coupon-flash-deals'),
            'menu-item-classes' => 'pages',
            'menu-item-url'     => get_permalink(Whizzie::get_page_id_by_title('Pages')),
            'menu-item-status'  => 'publish',
        ));

        wp_update_nav_menu_item($menu_id, 0, array(
            'menu-item-title'   => __('About Us','coupon-flash-deals'),
            'menu-item-classes' => 'about-us',
            'menu-item-url'     => get_permalink(Whizzie::get_page_id_by_title('About Us')),
            'menu-item-status'  => 'publish',
        ));

        wp_update_nav_menu_item($menu_id, 0, array(
            'menu-item-title'   => __('Contact Us','coupon-flash-deals'),
            'menu-item-classes' => 'contact-us',
            'menu-item-url'     => get_permalink(Whizzie::get_page_id_by_title('Contact Us')),
            'menu-item-status'  => 'publish',
        ));

        wp_update_nav_menu_item($menu_id, 0, array(
            'menu-item-title'   => __('Advance Search','coupon-flash-deals'),
            'menu-item-classes' => 'advance-search',
            'menu-item-url'     => get_permalink(Whizzie::get_page_id_by_title('Advance Search')),
            'menu-item-status'  => 'publish',
        ));

        // Assign menu to location
        if (!has_nav_menu($bpmenulocation)) {
            $locations = get_theme_mod('nav_menu_locations');
            $locations[$bpmenulocation] = $menu_id;
            set_theme_mod('nav_menu_locations', $locations);
        }
    }
}

	public function setup_widgets() {

		 $coupon_flash_deals_home_id='';
        $coupon_flash_deals_home_content = '';
        $coupon_flash_deals_home_title = 'Home';
        $coupon_flash_deals_home = array(
            'post_type' => 'page',
            'post_title' => $coupon_flash_deals_home_title,
            'post_content' => $coupon_flash_deals_home_content,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'home'
        );
        $coupon_flash_deals_home_id = wp_insert_post($coupon_flash_deals_home);

        add_post_meta( $coupon_flash_deals_home_id, '_wp_page_template', 'templates/template-homepage.php' );

        update_option( 'page_on_front', $coupon_flash_deals_home_id );
        update_option( 'show_on_front', 'page' );


            // Create a Posts Page
            $coupon_flash_deals_blog_title = 'Blogs';
            $coupon_flash_deals_blog_check = get_page_id_by_title($coupon_flash_deals_blog_title);

            if ($coupon_flash_deals_blog_check  == 1) {
                $coupon_flash_deals_blog = array(
                    'post_type' => 'page',
                    'post_title' => $coupon_flash_deals_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'blogs',
                    'post_content' => '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."',
                );
                $coupon_flash_deals_blog_id = wp_insert_post($coupon_flash_deals_blog);
            }

            // Create a Posts Page
            $coupon_flash_deals_blog_title = 'Pages';
            $coupon_flash_deals_blog_check = get_page_id_by_title($coupon_flash_deals_blog_title);

            if ($coupon_flash_deals_blog_check == 1) {
                $coupon_flash_deals_pages = array(
                    'post_type' => 'page',
                    'post_title' => $coupon_flash_deals_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'pages',
                    'post_content' => '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."',
                );
                $coupon_flash_deals_blog_id = wp_insert_post($coupon_flash_deals_pages);
            }

            // Create a Posts Page
            $coupon_flash_deals_blog_title = 'About Us';
            $coupon_flash_deals_blog_check = get_page_id_by_title($coupon_flash_deals_blog_title);

            if ($coupon_flash_deals_blog_check  == 1) {
                $coupon_flash_deals_about_us = array(
                    'post_type' => 'page',
                    'post_title' => $coupon_flash_deals_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'about-us',
                    'post_content' => '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."',
                );
                $coupon_flash_deals_blog_id = wp_insert_post($coupon_flash_deals_about_us);
            }

            // Create a Posts Page
			$coupon_flash_deals_search_title = 'Advance Search';
			$coupon_flash_deals_search_check = get_page_id_by_title($coupon_flash_deals_search_title);

			if ($coupon_flash_deals_search_check == 1) {

			    $coupon_flash_deals_about_us = array(
			        'post_type'    => 'page',
			        'post_title'   => $coupon_flash_deals_search_title,
			        'post_status'  => 'publish',
			        'post_author'  => 1,
			        'post_name'    => 'advance-search',
			        'post_content' => '"Lorem ipsum dolor sit amet, consectetur adipiscing elit..."',
			    );

			    // Insert the page
			    $coupon_flash_deals_blog_id = wp_insert_post($coupon_flash_deals_about_us);

			    if($coupon_flash_deals_blog_id) {

			        // Assign template to the page
			        update_post_meta($coupon_flash_deals_blog_id, '_wp_page_template', 'templates/advance-search.php');

			        // Add page to menu
			        $menu_name = 'Main Menu'; // Replace with your menu name
			        $menu = wp_get_nav_menu_object($menu_name);
			        if($menu) {
			            $menu_id = $menu->term_id;

			            // Add menu item
			            wp_update_nav_menu_item($menu_id, 0, array(
			                'menu-item-title'   => __('Advance Search', 'coupon-flash-deals'),
			                'menu-item-classes' => 'advance-search',
			                'menu-item-url'     => get_permalink($coupon_flash_deals_blog_id), // <-- Page URL
			                'menu-item-status'  => 'publish',
			            ));
			        }
			    }
			}

            // Create a Posts Page
            $coupon_flash_deals_blog_title = 'Contact Us';
            $coupon_flash_deals_blog_check = get_page_id_by_title($coupon_flash_deals_blog_title);

            if ($coupon_flash_deals_blog_check  == 1) {
                $coupon_flash_deals_contact = array(
                    'post_type' => 'page',
                    'post_title' => $coupon_flash_deals_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'contact-us',
                    'post_content' => '"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."',
                );
                $coupon_flash_deals_blog_id = wp_insert_post($coupon_flash_deals_contact);
            }

		//---Header--//

        // set_theme_mod( 'coupon_flash_deals_site_title_setting', false);

		set_theme_mod('coupon_flash_deals_header_wishlist_link','#');
		
		set_theme_mod('coupon_flash_deals_header_phone_number','1234567890');
		set_theme_mod('coupon_flash_deals_header_email_address','info@example.com');
		set_theme_mod('coupon_flash_deals_header_location','Boatway & Morris st, New York');

		//-----Slider-----//

		set_theme_mod( 'coupon_flash_deals_slide_on_off', 'on' );

		set_theme_mod('coupon_flash_deals_slide_count', '2');

		for ($i=0; $i <=2 ; $i++) { 
			set_theme_mod('coupon_flash_deals_slider_sub_heading'.$i, ' Only the best discounts & coupons');
			set_theme_mod('coupon_flash_deals_slider_heading'.$i, 'Enjoy discount & coupon codes!');
			set_theme_mod('coupon_flash_deals_slider_content'.$i, 'Lorem ipsum dolor sit amet This is Photoshop’s version of Lorem Ipsn gravida. Ing business like this takes much more effort than doing your own.');
			set_theme_mod( 'coupon_flash_deals_slider_image'.$i , get_template_directory_uri().'/images/slider.png' );
		}

		//--------------------------------------//

		$this->create_theme_nav_menu();

		$this->coupon_flash_deals_setup_coupons_cpt();
		$this->coupon_flash_deals_import_demo_coupons();

	}

	/**
	 * Register the "coupons" post type + "couponcategory" taxonomy the same
	 * way the Simple CPT plugin's own admin UI would: by creating the
	 * "simple_cpt" / "simple_tax" definition posts it reads on `init`.
	 *
	 * Powers the "We Would Suggest You" homepage section.
	 *
	 * @since 1.1.0
	 */
	private function coupon_flash_deals_setup_coupons_cpt() {

		// Nothing to register against if Simple CPT isn't active.
		if ( ! post_type_exists( 'simple_cpt' ) || ! post_type_exists( 'simple_tax' ) ) {
			return;
		}

		// Prefer the functional check: if "coupons" is already a registered
		// post type, Simple CPT already has a working definition post for it
		// (its meta drives that registration on every `init`). Checking the
		// definition post's meta directly is a weaker signal - it goes stale
		// if something else (e.g. a site reset tool) wipes postmeta without
		// touching post types/rows, which would otherwise cause a duplicate
		// definition post to be created on every re-import.
		$coupon_flash_deals_existing_cpt = post_type_exists( 'coupons' ) ? array( true ) : get_posts( array(
			'post_type'      => 'simple_cpt',
			'post_status'    => 'publish',
			'meta_key'       => 'simple_cpt_name',
			'meta_value'     => 'coupons',
			'posts_per_page' => 1,
			'fields'         => 'ids',
		) );

		if ( empty( $coupon_flash_deals_existing_cpt ) ) {
			$coupon_flash_deals_cpt_id = wp_insert_post( array(
				'post_type'   => 'simple_cpt',
				'post_title'  => 'Coupons',
				'post_status' => 'publish',
				'post_author' => 1,
			) );

			if ( $coupon_flash_deals_cpt_id && ! is_wp_error( $coupon_flash_deals_cpt_id ) ) {
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_name', 'coupons' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_label', 'Coupons' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_singular_name', 'Coupon' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_description', 'Coupon deals used in Advance Search and the homepage "We Would Suggest You" section.' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_icon_slug', 'dashicons-tickets-alt' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_public', '1' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_show_ui', '1' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_has_archive', '1' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_exclude_from_search', '0' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_capability_type', 'post' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_hierarchical', '0' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_rewrite', '1' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_custom_rewrite_slug', 'coupons' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_withfront', '1' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_query_var', '1' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_show_in_rest', '1' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_publicly_queryable', '1' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_show_in_menu', '1' );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_supports', array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields' ) );
				update_post_meta( $coupon_flash_deals_cpt_id, 'simple_cpt_builtin_tax', array() );
			}
		}

		$coupon_flash_deals_existing_tax = taxonomy_exists( 'couponcategory' ) ? array( true ) : get_posts( array(
			'post_type'      => 'simple_tax',
			'post_status'    => 'publish',
			'meta_key'       => 'simple_cpt_tax_name',
			'meta_value'     => 'couponcategory',
			'posts_per_page' => 1,
			'fields'         => 'ids',
		) );

		if ( empty( $coupon_flash_deals_existing_tax ) ) {
			$coupon_flash_deals_tax_id = wp_insert_post( array(
				'post_type'   => 'simple_tax',
				'post_title'  => 'Coupon Category',
				'post_status' => 'publish',
				'post_author' => 1,
			) );

			if ( $coupon_flash_deals_tax_id && ! is_wp_error( $coupon_flash_deals_tax_id ) ) {
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_name', 'couponcategory' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_label', 'Coupon Categories' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_singular_name', 'Coupon Category' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_show_ui', '1' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_hierarchical', '1' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_rewrite', '1' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_custom_rewrite_slug', 'coupon-category' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_query_var', '1' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_show_in_rest', '1' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_show_admin_column', '1' );
				update_post_meta( $coupon_flash_deals_tax_id, 'simple_cpt_tax_post_types', array( 'coupons' ) );
			}
		}

		// Make the post type/taxonomy available immediately in this request
		// (Simple CPT normally only registers these on the next `init`).
		if ( class_exists( 'Simple_Cpt_Admin' ) ) {
			$coupon_flash_deals_simple_cpt_admin = new Simple_Cpt_Admin( 'simple-cpt', defined( 'SIMPLE_CPT_VERSION' ) ? SIMPLE_CPT_VERSION : '1.1.1' );
			$coupon_flash_deals_simple_cpt_admin->register_simple_cpts();
		}

		update_option( 'simple_cpt_flush_needed', true );
		flush_rewrite_rules();
	}

	/**
	 * Copy a theme image into the media library and attach it to a post.
	 *
	 * @param string $file_path      Absolute path to the source image.
	 * @param int    $parent_post_id Post to attach the image to.
	 * @return int Attachment ID, or 0 on failure.
	 * @since 1.1.0
	 */
	private function coupon_flash_deals_import_local_image( $file_path, $parent_post_id = 0 ) {

		if ( ! file_exists( $file_path ) ) {
			return 0;
		}

		require_once ABSPATH . 'wp-admin/includes/file.php';
		require_once ABSPATH . 'wp-admin/includes/image.php';
		require_once ABSPATH . 'wp-admin/includes/media.php';

		$coupon_flash_deals_upload_dir = wp_upload_dir();
		if ( ! empty( $coupon_flash_deals_upload_dir['error'] ) ) {
			return 0;
		}

		$coupon_flash_deals_filename = wp_unique_filename( $coupon_flash_deals_upload_dir['path'], basename( $file_path ) );
		$coupon_flash_deals_new_file = trailingslashit( $coupon_flash_deals_upload_dir['path'] ) . $coupon_flash_deals_filename;

		if ( ! @copy( $file_path, $coupon_flash_deals_new_file ) ) {
			return 0;
		}

		$coupon_flash_deals_filetype = wp_check_filetype( $coupon_flash_deals_filename, null );

		$coupon_flash_deals_attach_id = wp_insert_attachment(
			array(
				'post_mime_type' => $coupon_flash_deals_filetype['type'],
				'post_title'     => sanitize_file_name( pathinfo( $coupon_flash_deals_filename, PATHINFO_FILENAME ) ),
				'post_content'   => '',
				'post_status'    => 'inherit',
			),
			$coupon_flash_deals_new_file,
			$parent_post_id
		);

		if ( ! $coupon_flash_deals_attach_id || is_wp_error( $coupon_flash_deals_attach_id ) ) {
			return 0;
		}

		$coupon_flash_deals_attach_data = wp_generate_attachment_metadata( $coupon_flash_deals_attach_id, $coupon_flash_deals_new_file );
		wp_update_attachment_metadata( $coupon_flash_deals_attach_id, $coupon_flash_deals_attach_data );

		return $coupon_flash_deals_attach_id;
	}

	/**
	 * Seed demo "coupons" posts (title, excerpt, ACF-backed discount/expiry
	 * fields, category and featured image) for the "We Would Suggest You"
	 * homepage section, and switch that section on via the Customizer.
	 *
	 * @since 1.1.0
	 */
	private function coupon_flash_deals_import_demo_coupons() {

		if ( ! post_type_exists( 'coupons' ) ) {
			return;
		}

		$coupon_flash_deals_categories = array(
			'spa-wellness' => 'Spa & Wellness',
			'dining'       => 'Dining',
			'travel'       => 'Travel',
			'beauty'       => 'Beauty',
		);

		if ( taxonomy_exists( 'couponcategory' ) ) {
			foreach ( $coupon_flash_deals_categories as $coupon_flash_deals_slug => $coupon_flash_deals_name ) {
				if ( ! term_exists( $coupon_flash_deals_slug, 'couponcategory' ) ) {
					wp_insert_term( $coupon_flash_deals_name, 'couponcategory', array( 'slug' => $coupon_flash_deals_slug ) );
				}
			}
		}

		$coupon_flash_deals_demo_coupons = array(
			array(
				'title'    => 'SPA Saloon – This Month Only 45% Off',
				'excerpt'  => 'Platea rutrum. Enim quam montes',
				'amount'   => '45%',
				'days'     => 39,
				'image'    => 'coupons1.png',
				'category' => 'spa-wellness',
			),
			array(
				'title'    => 'Great Dinner For Two – 15% Off',
				'excerpt'  => 'Platea rutrum. Enim quam montes',
				'amount'   => '15%',
				'days'     => 39,
				'image'    => 'coupons2.png',
				'category' => 'dining',
			),
			array(
				'title'    => 'Travel Holiday Up To 70% Discount',
				'excerpt'  => 'Platea rutrum. Enim quam montes',
				'amount'   => '70%',
				'days'     => 39,
				'image'    => 'coupons3.png',
				'category' => 'travel',
			),
			array(
				'title'    => 'Spray Tans – Body Bronze 50% Off',
				'excerpt'  => 'Platea rutrum. Enim quam montes',
				'amount'   => '50%',
				'days'     => 39,
				'image'    => 'coupons4.png',
				'category' => 'beauty',
			),
		);

		foreach ( $coupon_flash_deals_demo_coupons as $coupon_flash_deals_demo_coupon ) {

			$coupon_flash_deals_existing_coupon = get_posts( array(
				'post_type'      => 'coupons',
				'post_status'    => array( 'publish', 'draft' ),
				'title'          => $coupon_flash_deals_demo_coupon['title'],
				'posts_per_page' => 1,
				'fields'         => 'ids',
			) );

			if ( ! empty( $coupon_flash_deals_existing_coupon ) ) {
				$coupon_flash_deals_coupon_id = $coupon_flash_deals_existing_coupon[0];
			} else {
				$coupon_flash_deals_coupon_id = wp_insert_post( array(
					'post_type'    => 'coupons',
					'post_title'   => $coupon_flash_deals_demo_coupon['title'],
					'post_excerpt' => $coupon_flash_deals_demo_coupon['excerpt'],
					'post_content' => $coupon_flash_deals_demo_coupon['excerpt'],
					'post_status'  => 'publish',
					'post_author'  => 1,
				) );
			}

			if ( ! $coupon_flash_deals_coupon_id || is_wp_error( $coupon_flash_deals_coupon_id ) ) {
				continue;
			}

			// Re-running the importer also heals a coupon that already exists
			// but is missing its fields/image/thumbnail (e.g. a site reset
			// tool wiped postmeta without removing the posts themselves).
			if ( '' === get_post_meta( $coupon_flash_deals_coupon_id, 'coupon_amount', true ) ) {
				update_post_meta( $coupon_flash_deals_coupon_id, 'coupon_amount', $coupon_flash_deals_demo_coupon['amount'] );
			}
			if ( '' === get_post_meta( $coupon_flash_deals_coupon_id, 'start_date', true ) ) {
				update_post_meta( $coupon_flash_deals_coupon_id, 'start_date', date( 'Ymd' ) );
			}
			if ( '' === get_post_meta( $coupon_flash_deals_coupon_id, 'date_expires', true ) ) {
				update_post_meta( $coupon_flash_deals_coupon_id, 'date_expires', date( 'Ymd', strtotime( '+' . intval( $coupon_flash_deals_demo_coupon['days'] ) . ' days' ) ) );
			}

			if ( taxonomy_exists( 'couponcategory' ) ) {
				$coupon_flash_deals_existing_terms = wp_get_object_terms( $coupon_flash_deals_coupon_id, 'couponcategory', array( 'fields' => 'ids' ) );
				if ( empty( $coupon_flash_deals_existing_terms ) ) {
					wp_set_object_terms( $coupon_flash_deals_coupon_id, $coupon_flash_deals_demo_coupon['category'], 'couponcategory' );
				}
			}

			if ( ! has_post_thumbnail( $coupon_flash_deals_coupon_id ) ) {
				$coupon_flash_deals_image_path = trailingslashit( get_template_directory() ) . 'images/' . $coupon_flash_deals_demo_coupon['image'];
				$coupon_flash_deals_attach_id  = $this->coupon_flash_deals_import_local_image( $coupon_flash_deals_image_path, $coupon_flash_deals_coupon_id );

				if ( $coupon_flash_deals_attach_id ) {
					set_post_thumbnail( $coupon_flash_deals_coupon_id, $coupon_flash_deals_attach_id );
				}
			}
		}

		set_theme_mod( 'coupon_flash_deals_suggest_on_off', 'on' );
		set_theme_mod( 'coupon_flash_deals_suggest_heading', 'We Would Suggest You' );
		set_theme_mod( 'coupon_flash_deals_suggest_count', 4 );
	}
}