<?php
/**
 * @package Free Demo Content
 * @since 1.0.0
 */

class GrocerySupermarketChainThemeWhizzie {

	protected $version = '1.1.0';

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

	/** @var string Free Demo Content page slug and title. */
	protected $grocery_supermarket_chain_page_slug = '';
	protected $grocery_supermarket_chain_page_title = '';

	/** @var array Free Demo Content steps set by user. */
	protected $grocery_supermarket_chain_config_steps = array();

	/**
	 * Relative plugin url for this plugin folder
	 * @since 1.0.0
	 * @var string
	*/
	protected $grocery_supermarket_chain_plugin_url = '';
	protected $grocery_supermarket_chain_plugin_path = '';
	public $grocery_supermarket_chain_parent_slug;
	/**
	 * TGMPA instance storage
	 *
	 * @var object
	*/
	protected $grocery_supermarket_chain_tgmpa_instance;

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

	/**
	 * TGMPA Menu url
	 *
	 * @var string
	*/
	protected $grocery_supermarket_chain_tgmpa_url = 'themes.php?page=tgmpa-install-plugins';
	/**
	 * Constructor
	 *
	 * @param $grocery_supermarket_chain_config	Our config parameters
	*/
	public function __construct( $grocery_supermarket_chain_config ) {
		$this->set_vars( $grocery_supermarket_chain_config );
		$this->init();

		include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	}

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

		require_once trailingslashit( WHIZZIE_DIR ) . 'tgm/tgm.php';

		if( isset( $grocery_supermarket_chain_config['grocery_supermarket_chain_page_slug'] ) ) {
			$this->grocery_supermarket_chain_page_slug = esc_attr( $grocery_supermarket_chain_config['grocery_supermarket_chain_page_slug'] );
		}
		if( isset( $grocery_supermarket_chain_config['grocery_supermarket_chain_page_title'] ) ) {
			$this->grocery_supermarket_chain_page_title = esc_attr( $grocery_supermarket_chain_config['grocery_supermarket_chain_page_title'] );
		}
		if( isset( $grocery_supermarket_chain_config['steps'] ) ) {
			$this->grocery_supermarket_chain_config_steps = $grocery_supermarket_chain_config['steps'];
		}

		$this->grocery_supermarket_chain_plugin_path = trailingslashit( dirname( __FILE__ ) );
		$relative_url = str_replace( get_template_directory(), '', $this->grocery_supermarket_chain_plugin_path );
		$this->grocery_supermarket_chain_plugin_url = trailingslashit( get_template_directory_uri() . $relative_url );
		$current_theme = wp_get_theme();
		$this->grocery_supermarket_chain_theme_title = $current_theme->get( 'Name' );
		$this->grocery_supermarket_chain_theme_name = strtolower( preg_replace( '#[^a-zA-Z]#', '', $current_theme->get( 'Name' ) ) );
		$this->grocery_supermarket_chain_page_slug = apply_filters( $this->grocery_supermarket_chain_theme_name . '_theme_setup_wizard_grocery_supermarket_chain_page_slug', $this->grocery_supermarket_chain_theme_name . '-freedemocontent' );
		$this->grocery_supermarket_chain_parent_slug = apply_filters( $this->grocery_supermarket_chain_theme_name . '_theme_setup_wizard_parent_slug', '' );
	}

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

		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() {
		wp_enqueue_style( 'grocery-supermarket-chain-free-demo-content-style', get_template_directory_uri() . '/inc/free-demo-content/css-and-js/free-demo-content-style.css');
		wp_register_script( 'grocery-supermarket-chain-free-demo-content-script', get_template_directory_uri() . '/inc/free-demo-content/css-and-js/free-demo-content-script.js', array( 'jquery' ), time() );
		wp_localize_script(
			'grocery-supermarket-chain-free-demo-content-script',
			'grocery_supermarket_chain_whizzie_params',
			array(
				'ajaxurl' 		=> admin_url( 'admin-ajax.php' ),
				'wpnonce' 		=> wp_create_nonce( 'whizzie_nonce' ),
				'verify_text'	=> esc_html( 'verifying', 'grocery-supermarket-chain' )
			)
		);
		wp_enqueue_script( 'grocery-supermarket-chain-free-demo-content-script' );
	}

	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->grocery_supermarket_chain_tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
	}

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


	/**  Make a modal screen for the wizard **/
	// public function menu_page() {
	// 	add_theme_page( esc_html( $this->grocery_supermarket_chain_page_title ), esc_html( $this->grocery_supermarket_chain_page_title ), 'manage_options', $this->grocery_supermarket_chain_page_slug, array( $this, 'grocery_supermarket_chain_guide' ) );
	// }

	/*** Make an interface for the wizard ***/
	public function Grocery_Supermarket_Chain_Demo_Content_Page() {

		tgmpa_load_bulk_installer();

		// install plugins with TGM.
		if ( ! class_exists( 'TGM_Plugin_Activation' ) || ! isset( $GLOBALS['tgmpa'] ) ) {
			die( 'Failed to find TGM' );
		}
		$grocery_supermarket_chain_url = wp_nonce_url( add_query_arg( array( 'plugins' => 'go' ) ), 'whizzie-setup' );

		// copied from TGM
		$grocery_supermarket_chain_method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
		$grocery_supermarket_chain_fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.
		if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $grocery_supermarket_chain_url ), $grocery_supermarket_chain_method, false, false, $grocery_supermarket_chain_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( $grocery_supermarket_chain_url ), $grocery_supermarket_chain_method, true, false, $grocery_supermarket_chain_fields );
			return true;
		}

		/* If we arrive here, we have the filesystem */ ?>
		<div class="wrap">
			<?php echo '<div class="whizzie-wrap">';
				// The wizard is a list with only one item visible at a time
				$grocery_supermarket_chain_steps = $this->get_steps();
				echo '<div class="abcd">';

					echo '<ul class="whizzie-nav wizard-icon-nav nav-tab-wrapper clearfix">';?>

					<?php
						$grocery_supermarket_chain_stepI=1;
						foreach( $grocery_supermarket_chain_steps as $grocery_supermarket_chain_step ) {
							$grocery_supermarket_chain_stepAct=($grocery_supermarket_chain_stepI ==1)? 1 : 0;
							if( isset( $grocery_supermarket_chain_step['icon_text'] ) && $grocery_supermarket_chain_step['icon_text'] ) {
								echo '<li class="commom-cls nav-step-' . esc_attr( $grocery_supermarket_chain_step['id'] ) . '" wizard-steps="step-'.esc_attr( $grocery_supermarket_chain_step['id'] ).'" data-enable="'.$grocery_supermarket_chain_stepAct.'">
								<a class="nav-tab upgrade-button">'.esc_attr( $grocery_supermarket_chain_step['icon_text'] ).'</a>
								</li>';
							}
						$grocery_supermarket_chain_stepI++;}

					echo '</ul>';
			 	echo '</div>';

				echo '<div class="second-div">';
					echo '<ul class="whizzie-menu wizard-menu-page theme-details">';
					foreach( $grocery_supermarket_chain_steps as $grocery_supermarket_chain_step ) {
						$grocery_supermarket_chain_class = 'step step-' . esc_attr( $grocery_supermarket_chain_step['id'] );
						echo '<li data-step="' . esc_attr( $grocery_supermarket_chain_step['id'] ) . '" class="' . esc_attr( $grocery_supermarket_chain_class ) . '" >';

							$grocery_supermarket_chain_content = call_user_func( array( $this, $grocery_supermarket_chain_step['view'] ) );
							if( isset( $grocery_supermarket_chain_content['summary'] ) ) {
								printf(
									'<div class="summary">%s</div>',
									wp_kses_post( $grocery_supermarket_chain_content['summary'] )
								);
							}
							if( isset( $grocery_supermarket_chain_content['detail'] ) ) {
								// Add a link to see more detail
								printf( '<div class="wz-require-plugins">');
								printf(
									'<div class="detail">%s</div>',
									$grocery_supermarket_chain_content['detail'] // Need to escape this
								);
								printf('</div>');
							}
							printf('<div class="wizard-button-wrapper">');
								// The next button
								if( isset( $grocery_supermarket_chain_step['button_text'] ) && $grocery_supermarket_chain_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( $grocery_supermarket_chain_step['callback'] ),
										esc_attr( $grocery_supermarket_chain_step['id'] ),
										esc_html( $grocery_supermarket_chain_step['button_text'] )
									);
								}

								if( isset( $grocery_supermarket_chain_step['button_text_one'] )) {
									printf(
										'<div class="button-wrap button-wrap-one">
											<a href="#" class="button button-primary do-it" data-callback="install_widgets" data-step="widgets"><p class="demo-type-text">%s</p></a>
										</div>',
										esc_html( $grocery_supermarket_chain_step['button_text_one'] )
									);
								}
							printf('</div>');
						echo '</li>';
					}
					echo '</ul>';

				echo '</div>';
				?>
			<?php echo '</div>';?>
		</div>
	<?php }

	/**
	 * Set options for the steps
	 * @return Array
	*/
	public function get_steps() {
		$grocery_supermarket_chain_dev_steps = $this->grocery_supermarket_chain_config_steps;
		$grocery_supermarket_chain_steps = array(
			'plugins' => array(
				'id'			=> 'plugins',
				'title'			=> __( 'Plugins', 'grocery-supermarket-chain' ),
				'icon'			=> 'admin-plugins',
				'view'			=> 'get_step_plugins',
				'callback'		=> 'install_plugins',
				'button_text'	=> __( 'Install Plugins', 'grocery-supermarket-chain' ),
				'can_skip'		=> true,
				'icon_text'      => 'Plugins'
			),
			'widgets' => array(
				'id'			=> 'widgets',
				'title'			=> __( 'Customizer', 'grocery-supermarket-chain' ),
				'icon'			=> 'welcome-widgets-menus',
				'view'			=> 'get_step_widgets',
				'callback'		=> 'install_widgets',
				'button_text_one'	=> __( 'Import Demo Content', 'grocery-supermarket-chain' ),

				'can_skip'		=> true,
				'icon_text'      => 'Import Demo Content'
			),
			'done' => array(
				'id'			=> 'done',
				'title'			=> __( 'All Done', 'grocery-supermarket-chain' ),
				'icon'			=> 'yes',
				'view'			=> 'get_step_done',
				'callback'		=> '',
				'icon_text'      => 'Done'
			)
		);

		// Iterate through each step and replace with dev config values
		if( $grocery_supermarket_chain_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( $grocery_supermarket_chain_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( $grocery_supermarket_chain_steps[$id] ) ) {
						foreach( $can_config as $element ) {
							if( isset( $dev_step[$element] ) ) {
								$grocery_supermarket_chain_steps[$id][$element] = $dev_step[$element];
							}
						}
					}
				}
			}
		}
		return $grocery_supermarket_chain_steps;
	}

	/*** Print the content for the intro step ***/
		public function get_step_importer() { ?>
		<div class="summary">
			<p>
				<?php esc_html_e('Thank you for choosing this Grocery Supermarket Chain 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.','grocery-supermarket-chain'); ?>
			</p>
		</div>
	<?php }

	/**
	 * Get the content for the plugins step
	 * @return $grocery_supermarket_chain_content Array
	*/
	public function get_step_plugins() {
		$plugins = $this->get_plugins();
		$grocery_supermarket_chain_content = array(); ?>
			<div class="summary">
				<p>
					<?php esc_html_e('Install Recommended Plugins:	','grocery-supermarket-chain') ?>
				</p>
			</div>
		<?php // The detail element is initially hidden from the user
		$grocery_supermarket_chain_content['detail'] = '<span class="wizard-plugin-count">'.count($plugins['all']).'</span><ul class="whizzie-do-plugins">';
		// Add each plugin into a list
		foreach( $plugins['all'] as $slug=>$plugin ) {
			$grocery_supermarket_chain_content['detail'] .= '<li data-slug="' . esc_attr( $slug ) . '">' . esc_html( $plugin['name'] ) . '<div class="wizard-plugin-title">';

			$grocery_supermarket_chain_content['detail'] .= '<span class="wizard-plugin-status">Installation Required</span><i class="spinner"></i></div></li>';
		}
		$grocery_supermarket_chain_content['detail'] .= '</ul>';

		return $grocery_supermarket_chain_content;
	}

	/**    Print the content for the intro step     **/
	public function get_step_widgets() { ?>
		<div class="summary">
			<p>
				<?php esc_html_e('This theme allows you to Import Demo Content content and add widgets. Install them using the button below. You can also update or deactivate them using the Customizer.','grocery-supermarket-chain'); ?>
			</p>
		</div>
	<?php }

	/***  Print the content for the final step  ***/
	public function get_step_done() { ?>

		<div class="setup-finish">
			<p>
				<?php echo esc_html('Your demo content has been imported successfully. Click the finish button for more information.'); ?>
			</p>
			<div class="finish-buttons">
				<a href="<?php echo esc_url( admin_url( 'themes.php?page=grocery-supermarket-chain-themeinfo-page' ) ); ?>" class="wz-btn-customizer" target="_blank"><?php esc_html_e('Grocery Supermarket Chain Info','grocery-supermarket-chain') ?></a>
				<a href="<?php echo esc_url(admin_url('/customize.php')); ?>" class="wz-btn-customizer" target="_blank"><?php esc_html_e('Customizer Settings','grocery-supermarket-chain') ?></a>
				<a href="" class="wz-btn-builder" target="_blank"><?php esc_html_e('Customize Your Demo','grocery-supermarket-chain'); ?></a>
				<a href="<?php echo esc_url(home_url()); ?>" class="wz-btn-visit-site" target="_blank"><?php esc_html_e('Visit Your Site','grocery-supermarket-chain'); ?></a>
			</div>
			<div class="finish-buttons">
				<a href="<?php echo esc_url(admin_url()); ?>" class="button button-primary"><?php esc_html_e('Finish','grocery-supermarket-chain'); ?></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;
	}

	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','grocery-supermarket-chain' ) ) );
		}
		$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->grocery_supermarket_chain_tgmpa_url ),
					'plugin'        => array( $slug ),
					'tgmpa-page'    => $this->grocery_supermarket_chain_tgmpa_menu_slug,
					'plugin_status' => 'all',
					'_wpnonce'      => wp_create_nonce( 'bulk-plugins' ),
					'action'        => 'tgmpa-bulk-activate',
					'action2'       => - 1,
					'message'       => esc_html__( 'Activating Plugin','grocery-supermarket-chain' ),
				);
				break;
			}
		}
		foreach ( $plugins['update'] as $slug => $plugin ) {
			if ( $_POST['slug'] == $slug ) {
				$json = array(
					'url'           => admin_url( $this->grocery_supermarket_chain_tgmpa_url ),
					'plugin'        => array( $slug ),
					'tgmpa-page'    => $this->grocery_supermarket_chain_tgmpa_menu_slug,
					'plugin_status' => 'all',
					'_wpnonce'      => wp_create_nonce( 'bulk-plugins' ),
					'action'        => 'tgmpa-bulk-update',
					'action2'       => - 1,
					'message'       => esc_html__( 'Updating Plugin','grocery-supermarket-chain' ),
				);
				break;
			}
		}
		foreach ( $plugins['install'] as $slug => $plugin ) {
			if ( $_POST['slug'] == $slug ) {
				$json = array(
					'url'           => admin_url( $this->grocery_supermarket_chain_tgmpa_url ),
					'plugin'        => array( $slug ),
					'tgmpa-page'    => $this->grocery_supermarket_chain_tgmpa_menu_slug,
					'plugin_status' => 'all',
					'_wpnonce'      => wp_create_nonce( 'bulk-plugins' ),
					'action'        => 'tgmpa-bulk-install',
					'action2'       => - 1,
					'message'       => esc_html__( 'Installing Plugin','grocery-supermarket-chain' ),
				);
				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','grocery-supermarket-chain' ) ) );
		}
		exit;
	}


	//. - . - . - . - . - . - . - . - . - . - . - . - . MENUS . - . - . - . - . - . - . - . - . - . - . - . - .//
	
	public function Grocery_Supermarket_Chain_Customizer_Header_Menu() {
		// ------- Create Primary Menu --------
		$grocery_supermarket_chain_themename = 'Grocery Supermarket Chain'; // Ensure the theme name is set
		$grocery_supermarket_chain_menuname = $grocery_supermarket_chain_themename . ' Primary Menu';
		$grocery_supermarket_chain_menulocation = 'menu-1';
		$grocery_supermarket_chain_menu_exists = wp_get_nav_menu_object($grocery_supermarket_chain_menuname);

		if (!$grocery_supermarket_chain_menu_exists) {
			$grocery_supermarket_chain_menu_id = wp_create_nav_menu($grocery_supermarket_chain_menuname);

			// Home
			wp_update_nav_menu_item($grocery_supermarket_chain_menu_id, 0, array(
				'menu-item-title' => __('Home', 'grocery-supermarket-chain'),
				'menu-item-classes' => 'home',
				'menu-item-url' => home_url('/'),
				'menu-item-status' => 'publish'
			));

			// About
			$grocery_supermarket_chain_page_about = get_page_by_path('about');
			if($grocery_supermarket_chain_page_about){
				wp_update_nav_menu_item($grocery_supermarket_chain_menu_id, 0, array(
					'menu-item-title' => __('About', 'grocery-supermarket-chain'),
					'menu-item-classes' => 'about',
					'menu-item-url' => get_permalink($grocery_supermarket_chain_page_about),
					'menu-item-status' => 'publish'
				));
			}

			// Page
			$grocery_supermarket_chain_page_Page = get_page_by_path('Page');
			if($grocery_supermarket_chain_page_Page){
				wp_update_nav_menu_item($grocery_supermarket_chain_menu_id, 0, array(
					'menu-item-title' => __('Page', 'grocery-supermarket-chain'),
					'menu-item-classes' => 'Page',
					'menu-item-url' => get_permalink($grocery_supermarket_chain_page_Page),
					'menu-item-status' => 'publish'
				));
			}

			// Blog
			$grocery_supermarket_chain_page_blog = get_page_by_path('blog');
			if($grocery_supermarket_chain_page_blog){
				wp_update_nav_menu_item($grocery_supermarket_chain_menu_id, 0, array(
					'menu-item-title' => __('Blog', 'grocery-supermarket-chain'),
					'menu-item-classes' => 'blog',
					'menu-item-url' => get_permalink($grocery_supermarket_chain_page_blog),
					'menu-item-status' => 'publish'
				));
			}

			// Contact Us
			$grocery_supermarket_chain_page_contact = get_page_by_path('contact');
			if($grocery_supermarket_chain_page_contact){
				wp_update_nav_menu_item($grocery_supermarket_chain_menu_id, 0, array(
					'menu-item-title' => __('Contact Us', 'grocery-supermarket-chain'),
					'menu-item-classes' => 'contact',
					'menu-item-url' => get_permalink($grocery_supermarket_chain_page_contact),
					'menu-item-status' => 'publish'
				));
			}

			// Assign menu to location if not set
			if (!has_nav_menu($grocery_supermarket_chain_menulocation)) {
				$grocery_supermarket_chain_locations = get_theme_mod('nav_menu_locations');
				$grocery_supermarket_chain_locations[$grocery_supermarket_chain_menulocation] = $grocery_supermarket_chain_menu_id; // Use $grocery_supermarket_chain_menu_id here
				set_theme_mod('nav_menu_locations', $grocery_supermarket_chain_locations);
			}
		}
	}


	/**
	* Imports the Demo Content
	* @since 1.1.0
	*/
	public function setup_widgets() {

		//. - . - . - . - . - . - . - . - . - . - . - . - . MENU PAGES . - . - . - . - . - . - . - . - . - . - . - . - .//
		
			$grocery_supermarket_chain_home_id='';
			$grocery_supermarket_chain_home_content = '';

			$grocery_supermarket_chain_home_title = 'Home';
			$grocery_supermarket_chain_home = array(
					'post_type' => 'page',
					'post_title' => $grocery_supermarket_chain_home_title,
					'post_content'  => $grocery_supermarket_chain_home_content,
					'post_status' => 'publish',
					'post_author' => 1,
					'post_slug' => 'home'
			);
			$grocery_supermarket_chain_home_id = wp_insert_post($grocery_supermarket_chain_home);

			//Set the home page template
			add_post_meta( $grocery_supermarket_chain_home_id, '_wp_page_template', 'default-home.php' );

			//Set the static front page
			$grocery_supermarket_chain_home = get_page_by_title( 'Home' );
			update_option( 'page_on_front', $grocery_supermarket_chain_home->ID );
			update_option( 'show_on_front', 'page' );


			// Create a posts page and assign the template
			$grocery_supermarket_chain_blog_title = 'Blog';
			$grocery_supermarket_chain_blog_check = get_page_by_path('blog');
			if (!$grocery_supermarket_chain_blog_check) {
				$grocery_supermarket_chain_blog = array(
					'post_type'    => 'page',
					'post_title'   => $grocery_supermarket_chain_blog_title,
					'post_status'  => 'publish',
					'post_author'  => 1,
					'post_name'    => 'blog'
				);
				$grocery_supermarket_chain_blog_id = wp_insert_post($grocery_supermarket_chain_blog);

				// Set the posts page
				if (!is_wp_error($grocery_supermarket_chain_blog_id)) {
					update_option('page_for_posts', $grocery_supermarket_chain_blog_id);
				}
			}

			// Create a Contact Us page and assign the template
			$grocery_supermarket_chain_contact_title = 'Contact Us';
			$grocery_supermarket_chain_contact_check = get_page_by_path('contact');
			if (!$grocery_supermarket_chain_contact_check) {
				$grocery_supermarket_chain_contact = array(
					'post_type'    => 'page',
					'post_title'   => $grocery_supermarket_chain_contact_title,
					'post_status'  => 'publish',
					'post_content'   => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
					'post_author'  => 1,
					'post_name'    => 'contact'
				);
				wp_insert_post($grocery_supermarket_chain_contact);
			}

			// Create a About page and assign the template
			$grocery_supermarket_chain_about_title = 'About';
			$grocery_supermarket_chain_about_check = get_page_by_path('about');
			if (!$grocery_supermarket_chain_about_check) {
				$grocery_supermarket_chain_about = array(
					'post_type'    => 'page',
					'post_title'   => $grocery_supermarket_chain_about_title,
					'post_status'  => 'publish',
					'post_content'   => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
					'post_author'  => 1,
					'post_name'    => 'about'
				);
				wp_insert_post($grocery_supermarket_chain_about);
			}

			// Create a Page page and assign the template
			$grocery_supermarket_chain_Page_title = 'Page';
			$grocery_supermarket_chain_Page_check = get_page_by_path('Page');
			if (!$grocery_supermarket_chain_Page_check) {
				$grocery_supermarket_chain_Page = array(
					'post_type'    => 'page',
					'post_title'   => $grocery_supermarket_chain_Page_title,
					'post_status'  => 'publish',
					'post_content'   => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
					'post_author'  => 1,
					'post_name'    => 'Page'
				);
				wp_insert_post($grocery_supermarket_chain_Page);
			}

			
		//. - . - . - . - . - . - . - . - . - . - . - . - . FRONTPAGE CONTENT . - . - . - . - . - . - . - . - . - . - . - . - .//

			set_theme_mod('grocery_supermarket_chain_email_address','supermarket@example.com');
			set_theme_mod('grocery_supermarket_chain_phone_number','00 12321545648');

			set_theme_mod('grocery_supermarket_chain_facebook_url','#');
			set_theme_mod('grocery_supermarket_chain_twitter_url','#');
			set_theme_mod('grocery_supermarket_chain_instagram_url','#');
			set_theme_mod('grocery_supermarket_chain_youtube_url','#');
			set_theme_mod('grocery_supermarket_chain_whatsapp_url','#');
			
			set_theme_mod('grocery_supermarket_chain_banner_section_on_off_setting',true);
			set_theme_mod('grocery_supermarket_chain_product_section_on_off_setting',true);

			set_theme_mod('grocery_supermarket_chain_slider',3);
	
			$grocery_supermarket_chain_diff_headings = array(
				'We Are Delivered Organic Fresh Fruits',
				'Freshness You Can Taste, Quality You Can Trust',
				'Making Grocery Shopping Easier Every Day'
			);

			for($grocery_supermarket_chain_i=1;$grocery_supermarket_chain_i<=3;$grocery_supermarket_chain_i++){
				set_theme_mod( 'grocery_supermarket_chain_banner_short_heading'.$grocery_supermarket_chain_i, 'Welcome To Supermarket Store' );
				set_theme_mod('grocery_supermarket_chain_banner_heading'.$grocery_supermarket_chain_i, $grocery_supermarket_chain_diff_headings[$grocery_supermarket_chain_i - 1]);
				set_theme_mod( 'grocery_supermarket_chain_banner_content'.$grocery_supermarket_chain_i, 'Unlock a world of knowledge and skills. Start your learning journey today!' );
				set_theme_mod( 'grocery_supermarket_chain_banner_btn'.$grocery_supermarket_chain_i, 'Shop Now' );
				set_theme_mod( 'grocery_supermarket_chain_banner_button_link'.$grocery_supermarket_chain_i, '#' );
				set_theme_mod( 'grocery_supermarket_chain_banner_image'.$grocery_supermarket_chain_i,get_template_directory_uri().'/assets/images/slider.png' );
			}
			
			set_theme_mod('grocery_supermarket_chain_product_heading','Deal Of The Week');

			$grocery_supermarket_chain_product_category= array(
				'Vegetable' => array(
							'Apple',
							'Red Pepper',
							'Coke',
							'Egg',
							'Pastry',
							'Meat'
				),
				'Dairy' => array( ),
				'Icecream' => array( ),
				'Grocery' => array( ),
			);
			$grocery_supermarket_chain_k = 1;
			foreach ( $grocery_supermarket_chain_product_category as $grocery_supermarket_chain_product_cats => $grocery_supermarket_chain_products_name ) {

				// Insert porduct cats Start
				$grocery_supermarket_chain_content = 'Lorem Ipsum is simply dummy text of the printing and setting.';
				$grocery_supermarket_chain_parent_category	=	wp_insert_term(
				$grocery_supermarket_chain_product_cats, // the term
				'product_cat', // the taxonomy
				array(
					'description'=> $grocery_supermarket_chain_content,
					'slug' => 'product_cat'.$grocery_supermarket_chain_k
				));

				$grocery_supermarket_chain_image_url = get_template_directory_uri().'/assets/images/slider.png';

				$grocery_supermarket_chain_image_name= 'img.png';
				$upload_dir       = wp_upload_dir();
				// Set upload folder
				$grocery_supermarket_chain_image_data= file_get_contents($grocery_supermarket_chain_image_url);
				// Get image data
				$grocery_supermarket_chain_unique_file_name = wp_unique_filename( $upload_dir['path'], $grocery_supermarket_chain_image_name );
				// Generate unique name
				$grocery_supermarket_chain_filename= basename( $grocery_supermarket_chain_unique_file_name );
				// Create image file name

				// Check folder permission and define file location
				if( wp_mkdir_p( $upload_dir['path'] ) ) {
				$grocery_supermarket_chain_file = $upload_dir['path'] . '/' . $grocery_supermarket_chain_filename;
				} else {
				$grocery_supermarket_chain_file = $upload_dir['basedir'] . '/' . $grocery_supermarket_chain_filename;
				}

				// Create the image  file on the server
				if ( ! function_exists( 'WP_Filesystem' ) ) {
					require_once( ABSPATH . 'wp-admin/includes/file.php' );
				}
				
				WP_Filesystem();
				global $wp_filesystem;
				
				if ( ! $wp_filesystem->put_contents( $grocery_supermarket_chain_file, $grocery_supermarket_chain_image_data, FS_CHMOD_FILE ) ) {
					wp_die( 'Error saving file!' );
				}
				
				// Check image file type
				$wp_filetype = wp_check_filetype( $grocery_supermarket_chain_filename, null );

				// Set attachment data
				$attachment = array(
				'post_mime_type' => $wp_filetype['type'],
				'post_title'     => sanitize_file_name( $grocery_supermarket_chain_filename ),
				'post_content'   => '',
				'post_type'     => 'product',
				'post_status'    => 'inherit'
				);

				// Create the attachment
				$attach_id = wp_insert_attachment( $attachment, $grocery_supermarket_chain_file, $post_id );

				// Include image.php
				require_once(ABSPATH . 'wp-admin/includes/image.php');

				// Define attachment metadata
				$attach_data = wp_generate_attachment_metadata( $attach_id, $grocery_supermarket_chain_file );

				// Assign metadata to attachment
				wp_update_attachment_metadata( $attach_id, $attach_data );

				update_woocommerce_term_meta( $grocery_supermarket_chain_parent_category['term_id'], 'thumbnail_id', $attach_id );

				// create Product START
				foreach ( $grocery_supermarket_chain_products_name as $grocery_supermarket_chain_key => $product_title ) {

					$grocery_supermarket_chain_content = 'Lorem Ipsum is simply dummy text of the printing and setting.';
					// Create post object
					$grocery_supermarket_chain_my_post = array(
						'post_title'    => wp_strip_all_tags( $product_title ),
						'post_content'  => $grocery_supermarket_chain_content,
						'post_status'   => 'publish',
						'post_type'     => 'product',
					);

					// Insert the post into the database
					$post_id    = wp_insert_post($grocery_supermarket_chain_my_post);

					wp_set_object_terms( $post_id, 'product_cat' . $grocery_supermarket_chain_k, 'product_cat', true );

					update_post_meta($post_id, '_regular_price', '140.00'); // Set regular price	
					update_post_meta($post_id, '_sale_price', '120.00'); // Set sale price
					update_post_meta($post_id, '_price', '120.00'); // Set current price (sale price is applied)

					// Now replace meta w/ new updated value array
					$grocery_supermarket_chain_image_url = get_template_directory_uri().'/assets/images/'.str_replace( " ", "-", $product_title).'.png';

					echo $grocery_supermarket_chain_image_url . "<br>";

					$grocery_supermarket_chain_image_name       = $product_title.'.png';
					$upload_dir = wp_upload_dir();
					// Set upload folder
					$grocery_supermarket_chain_image_data = file_get_contents(esc_url($grocery_supermarket_chain_image_url));

					// Get image data
					$grocery_supermarket_chain_unique_file_name = wp_unique_filename($upload_dir['path'], $grocery_supermarket_chain_image_name);
					// Generate unique name
					$grocery_supermarket_chain_filename = basename($grocery_supermarket_chain_unique_file_name);
					// Create image file name

					// Check folder permission and define file location
					if (wp_mkdir_p($upload_dir['path'])) {
						$grocery_supermarket_chain_file = $upload_dir['path'].'/'.$grocery_supermarket_chain_filename;
					} else {
						$grocery_supermarket_chain_file = $upload_dir['basedir'].'/'.$grocery_supermarket_chain_filename;
					}

					// Create the image  file on the server
					if ( ! function_exists( 'WP_Filesystem' ) ) {
						require_once( ABSPATH . 'wp-admin/includes/file.php' );
					}
					
					WP_Filesystem();
					global $wp_filesystem;
					
					if ( ! $wp_filesystem->put_contents( $grocery_supermarket_chain_file, $grocery_supermarket_chain_image_data, FS_CHMOD_FILE ) ) {
						wp_die( 'Error saving file!' );
					}

					// Check image file type
					$wp_filetype = wp_check_filetype($grocery_supermarket_chain_filename, null);

					// Set attachment data
					$attachment = array(
						'post_mime_type' => $wp_filetype['type'],
						'post_title'     => sanitize_file_name($grocery_supermarket_chain_filename),
						'post_type'      => 'product',
						'post_status'    => 'inherit',
					);

					// Create the attachment
					$attach_id = wp_insert_attachment($attachment, $grocery_supermarket_chain_file, $post_id);

					// Include image.php
					require_once (ABSPATH.'wp-admin/includes/image.php');

					// Define attachment metadata
					$attach_data = wp_generate_attachment_metadata($attach_id, $grocery_supermarket_chain_file);

					// Assign metadata to attachment
					wp_update_attachment_metadata($attach_id, $attach_data);

					// And finally assign featured image to post
					set_post_thumbnail($post_id, $attach_id);
				}
				// Create product END
				++$grocery_supermarket_chain_k;
			}

		$this->Grocery_Supermarket_Chain_Customizer_Header_Menu();
	}

	//guidline for about theme
	public function grocery_supermarket_chain_guide() {
		$display_string = '';
		//custom function about theme customizer
		$return = add_query_arg( array()) ;
		$theme = wp_get_theme( 'grocery-supermarket-chain' );
		?>
		<div class="wrapper-info wrap about-wrap access-wrap">
			<div class="tab-sec theme-option-tab">
				<div id="demo_offer" class="tabcontent open">
					<?php $this->Grocery_Supermarket_Chain_Demo_Content_Page(); ?>
				</div>
			</div>
		</div>
		
	<?php }
}