<?php
/**
 * @package Demo Import
 * @since 1.0.0
 */

class ThemeWhizzie {

	protected $version = '1.1.0';

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

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

	/** @var array Demo Import steps set by user. */
	protected $config_steps = array();
	public $parent_slug;
	/**
	 * Constructor
	 *
	 * @param $news_current_affair_config	Our config parameters
	*/
	public function __construct( $news_current_affair_config ) {
		$this->set_vars( $news_current_affair_config );
		$this->init();
	}

	/**
	 * Set some settings
	 * @since 1.0.0
	 * @param $news_current_affair_config	Our config parameters
	*/
	public function set_vars( $news_current_affair_config ) {
		if( isset( $news_current_affair_config['page_slug'] ) ) {
			$this->page_slug = esc_attr( $news_current_affair_config['page_slug'] );
		}
		if( isset( $news_current_affair_config['page_title'] ) ) {
			$this->page_title = esc_attr( $news_current_affair_config['page_title'] );
		}
		if( isset( $news_current_affair_config['steps'] ) ) {
			$this->config_steps = $news_current_affair_config['steps'];
		}

		$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 . '-demoimport' );
		$this->parent_slug = apply_filters( $this->theme_name . '_theme_setup_wizard_parent_slug', '' );
	}

	/**
	 * Hooks and filters
	 * @since 1.0.0
	*/
	public function init() {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		add_action( 'admin_menu', array( $this, 'menu_page' ) );
		add_action( 'wp_ajax_setup_widgets', array( $this, 'setup_widgets' ) );
	}
	
	public function enqueue_scripts() {
		wp_enqueue_style( 'demo-import-style', get_template_directory_uri() . '/demo-import/assets/css/demo-import-style.css');
		wp_register_script( 'demo-import-script', get_template_directory_uri() . '/demo-import/assets/js/demo-import-script.js', array( 'jquery' ), time() );
		wp_localize_script(
			'demo-import-script',
			'news_current_affair_whizzie_params',
			array(
				'ajaxurl' 		=> admin_url( 'admin-ajax.php' ),
				'wpnonce' 		=> wp_create_nonce( 'whizzie_nonce' ),
				'verify_text'	=> esc_html( 'verifying', 'news-current-affair' )
			)
		);
		wp_enqueue_script( 'demo-import-script' );
	}

	/**  Make a modal screen for the wizard **/
	public function menu_page() {
		add_menu_page( esc_html( $this->page_title ), esc_html( $this->page_title ), 'manage_options', $this->page_slug, array( $this, 'news_current_affair_guide' ) ,'',40);
	}

	/** Make an interface for the wizard **/
	public function wizard_page() {
		/* 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
				$steps = $this->get_steps();
				echo '<ul class="whizzie-nav wizard-icon-nav">';?>
				<?php
					$stepI=1;
					foreach( $steps as $step ) {
						$stepAct=($stepI ==1)? 1 : 0;
						if( isset( $step['icon_text'] ) && $step['icon_text'] ) {
							echo '<li class="commom-cls nav-step-' . esc_attr( $step['id'] ) . '" wizard-steps="step-'.esc_attr( $step['id'] ).'" data-enable="'.$stepAct.'">
							<p>'.esc_attr( $step['icon_text'] ).'</p>
							</li>';
						}
					$stepI++;}
			 	echo '</ul>';
				echo '<ul class="whizzie-menu wizard-menu-page">';
				foreach( $steps as $step ) {
					$class = 'step step-' . esc_attr( $step['id'] );
					echo '<li data-step="' . esc_attr( $step['id'] ) . '" class="' . esc_attr( $class ) . '" >';
						$content = call_user_func( array( $this, $step['view'] ) );
						printf('<div class="wizard-button-wrapper">');
							if( isset( $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( $step['button_text_one'] )
								);
							}
						printf('</div>');
					echo '</li>';
				}
				echo '</ul>';
				?>
				<div class="step-loading"><span class="spinner">
					<img src="<?php echo esc_url(get_template_directory_uri().'/demo-import/assets/images/Spinner-Animaion.gif'); ?>">
				</span></div>
			<?php echo '</div>';?>
		</div>
	<?php }

	/**
	 * Set options for the steps
	 * @return Array
	*/
	public function get_steps() {
		if ( get_option('news_current_affair_demo_imported') ) {
			return array(
				'done' => array(
					'id'        => 'done',
					'title'     => __( 'All Done', 'news-current-affair' ),
					'icon'      => 'yes',
					'view'      => 'get_step_done',
					'callback'  => '',
					'icon_text' => 'View Site'
				)
			);
		}
		$dev_steps = $this->config_steps;
		$steps = array(
			'widgets' => array(
				'id'			=> 'widgets',
				'title'			=> __( 'Customizer', 'news-current-affair' ),
				'icon'			=> 'welcome-widgets-menus',
				'view'			=> 'get_step_widgets',
				'callback'		=> 'install_widgets',
				'button_text_one'	=> __( 'Import Demo', 'news-current-affair' ),
				'can_skip'		=> true,
				'icon_text'      => 'Import Demo'
			),
			'done' => array(
				'id'			=> 'done',
				'title'			=> __( 'All Done', 'news-current-affair' ),
				'icon'			=> 'yes',
				'view'			=> 'get_step_done',
				'callback'		=> '',
				'icon_text'      => 'Done'
			)
		);
		// 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_importer() { ?>
		<div class="summary">
			<p>
				<?php esc_html_e('Thank you for choosing this News Current Affair 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.','news-current-affair'); ?>
			</p>
		</div>
	<?php }

	/**   Print the content for the widgets step   **/
	public function get_step_widgets() { ?>
		<div class="summary">
			<p>
				<?php esc_html_e('This theme allows you to import demo content and add widgets. Install them using the button below. You can also update or deactivate them using the Customizer.','news-current-affair'); ?>
			</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=news-current-affair-getstart-page' ) ); ?>" class="wz-btn-customizer" target="_blank"><?php esc_html_e('About News Current Affair','news-current-affair') ?></a>
				<a href="<?php echo esc_url(admin_url('/customize.php')); ?>" class="wz-btn-customizer" target="_blank"><?php esc_html_e('Customize Your Demo','news-current-affair') ?></a>
				<a href="<?php echo esc_url(home_url()); ?>" class="wz-btn-visit-site" target="_blank"><?php esc_html_e('Visit Your Site','news-current-affair'); ?></a>
			</div>
			<div class="finish-buttons">
				<a href="<?php echo esc_url(admin_url()); ?>" class="button button-primary"><?php esc_html_e('Finish','news-current-affair'); ?></a>
			</div>
		</div>

	<?php }


	public function news_current_affair_customizer_nav_menu() {
		// ------- Create Primary Menu --------
		$news_current_affair_themename = 'News Current Affair'; // Ensure the theme name is set
		$news_current_affair_menuname = $news_current_affair_themename . ' Primary Menu';
		$news_current_affair_menulocation = 'menu-1';
		$news_current_affair_menu_exists = wp_get_nav_menu_object($news_current_affair_menuname);

		if (!$news_current_affair_menu_exists) {
			$news_current_affair_menu_id = wp_create_nav_menu($news_current_affair_menuname);

			// Home
			wp_update_nav_menu_item($news_current_affair_menu_id, 0, array(
				'menu-item-title' => __('Home', 'news-current-affair'),
				'menu-item-classes' => 'home',
				'menu-item-url' => home_url('/'),
				'menu-item-status' => 'publish'
			));

			// About
			$news_current_affair_page_about = get_page_by_path('about');
			if($news_current_affair_page_about){
				wp_update_nav_menu_item($news_current_affair_menu_id, 0, array(
					'menu-item-title' => __('About', 'news-current-affair'),
					'menu-item-classes' => 'about',
					'menu-item-url' => get_permalink($news_current_affair_page_about),
					'menu-item-status' => 'publish'
				));
			}

			// Services
			$news_current_affair_page_services = get_page_by_path('services');
			if($news_current_affair_page_services){
				wp_update_nav_menu_item($news_current_affair_menu_id, 0, array(
					'menu-item-title' => __('Services', 'news-current-affair'),
					'menu-item-classes' => 'services',
					'menu-item-url' => get_permalink($news_current_affair_page_services),
					'menu-item-status' => 'publish'
				));
			}

			// Blog
			$news_current_affair_page_blog = get_page_by_path('blog');
			if($news_current_affair_page_blog){
				wp_update_nav_menu_item($news_current_affair_menu_id, 0, array(
					'menu-item-title' => __('Blog', 'news-current-affair'),
					'menu-item-classes' => 'blog',
					'menu-item-url' => get_permalink($news_current_affair_page_blog),
					'menu-item-status' => 'publish'
				));
			}

			// Contact Us
			$news_current_affair_page_contact = get_page_by_path('contact');
			if($news_current_affair_page_contact){
				wp_update_nav_menu_item($news_current_affair_menu_id, 0, array(
					'menu-item-title' => __('Contact Us', 'news-current-affair'),
					'menu-item-classes' => 'contact',
					'menu-item-url' => get_permalink($news_current_affair_page_contact),
					'menu-item-status' => 'publish'
				));
			}

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

	public function news_current_affair_social_menu() {

		// ------- Create Social Menu --------
		$news_current_affair_menuname = $news_current_affair_themename . 'Social Menu';
		$news_current_affair_menulocation = 'social-menu';
		$news_current_affair_menu_exists = wp_get_nav_menu_object( $news_current_affair_menuname );

		if( !$news_current_affair_menu_exists){
			$news_current_affair_menu_id = wp_create_nav_menu($news_current_affair_menuname);

			wp_update_nav_menu_item( $news_current_affair_menu_id, 0, array(
				'menu-item-title'  => __( 'Facebook', 'news-current-affair' ),
				'menu-item-url'    => 'https://www.facebook.com',
				'menu-item-status' => 'publish',
			) );

			wp_update_nav_menu_item( $news_current_affair_menu_id, 0, array(
				'menu-item-title'  => __( 'Pinterest', 'news-current-affair' ),
				'menu-item-url'    => 'https://www.pinterest.com',
				'menu-item-status' => 'publish',
			) );
	
			wp_update_nav_menu_item( $news_current_affair_menu_id, 0, array(
				'menu-item-title'  => __( 'Twitter', 'news-current-affair' ),
				'menu-item-url'    => 'https://www.twitter.com',
				'menu-item-status' => 'publish',
			) );
	
			wp_update_nav_menu_item( $news_current_affair_menu_id, 0, array(
				'menu-item-title'  => __( 'Youtube', 'news-current-affair' ),
				'menu-item-url'    => 'https://www.youtube.com',
				'menu-item-status' => 'publish',
			) );

			wp_update_nav_menu_item( $news_current_affair_menu_id, 0, array(
				'menu-item-title'  => __( 'Instagram', 'news-current-affair' ),
				'menu-item-url'    => 'https://www.instagram.com',
				'menu-item-status' => 'publish',
			) );

			if( !has_nav_menu( $news_current_affair_menulocation ) ){
					$locations = get_theme_mod('nav_menu_locations');
					$locations[$news_current_affair_menulocation] = $news_current_affair_menu_id;
					set_theme_mod( 'nav_menu_locations', $locations );
			}
		}
	}

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

		//................................................. MENU PAGES .................................................//
		
		$news_current_affair_home_id='';
		$news_current_affair_home_content = '';

		$news_current_affair_home_title = 'Home';
		$news_current_affair_home = array(
				'post_type' => 'page',
				'post_title' => $news_current_affair_home_title,
				'post_content'  => $news_current_affair_home_content,
				'post_status' => 'publish',
				'post_author' => 1,
				'post_slug' => 'home'
		);
		$news_current_affair_home_id = wp_insert_post($news_current_affair_home);

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

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


		// Create a posts page and assign the template
		$news_current_affair_blog_title = 'Blog';
		$news_current_affair_blog_check = get_page_by_path('blog');
		if (!$news_current_affair_blog_check) {
			$news_current_affair_blog = array(
				'post_type'    => 'page',
				'post_title'   => $news_current_affair_blog_title,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'blog' // Unique slug for the blog page
			);
			$news_current_affair_blog_id = wp_insert_post($news_current_affair_blog);

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

		// Create a Contact Us page and assign the template
		$news_current_affair_contact_title = 'Contact Us';
		$news_current_affair_contact_check = get_page_by_path('contact');
		if (!$news_current_affair_contact_check) {
			$news_current_affair_contact = array(
				'post_type'    => 'page',
				'post_title'   => $news_current_affair_contact_title,
				'post_content'   => '"More About The Free News Current Affair Theme"
										The Free News WordPress Theme is designed to cater to both beginner users and seasoned news publishers, making it an ideal choice for anyone looking to build a professional and content-rich online news platform. With its intuitive interface and ready-to-use design elements, it simplifies the process of creating a site focused on the latest news, current events, or in-depth analysis. Whether you plan to highlight national news, business updates, political coverage, or entertainment stories, this theme adjusts seamlessly to your editorial focus. The widget-friendly layout supports weather widgets, trending news tickers, and social media integration to enhance engagement. This theme is also perfect for community-based reporting, user-generated content, or citizen journalism, providing a reliable and visually impressive structure at no cost while maintaining performance, responsiveness, and flexibility.

										As the digital news landscape rapidly expands, having a reliable, stylish, and functional theme is more important than ever. The Free News WordPress Theme rises to this challenge by supporting multiple post formats, including text, audio, and video, making it ideal for modern multimedia storytelling. Whether you\'re a seasoned journalist, an aspiring reporter, or part of a grassroots news team, this theme empowers you to share compelling stories in a visually appealing format. It features editorial sections for opinion pieces and in-depth columns, while also enabling the quick upload of breaking news, live updates, or visually rich photo essays. The Free News WordPress Theme even supports podcast episodes and video-based content, allowing you to turn your platform into a truly dynamic, all-in-one digital news hub.

										The Free News WordPress Theme also stands out for its impressive speed and high functionality, making it a top choice for modern news publishers. Built with lightweight, clean code, it delivers fast load times—crucial for keeping visitors engaged in a high-traffic, fast-paced news environment. This performance-optimized theme is fully responsive and mobile-friendly, offering swipeable carousels, intuitive dropdown menus, and collapsible sidebars that enhance user navigation on smartphones and tablets. Users can access trending news, breaking headlines, and featured stories with zero delays. It’s compatible with AMP and popular caching plugins, which further improve site speed and overall performance. Whether you’re running an independent news blog, a regional news portal, or a full-fledged digital media outlet, this theme provides the essential tools to inform, engage, and build a loyal audience in today’s competitive digital news landscape.',
					'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'contact' // Unique slug for the Contact Us page
			);
			wp_insert_post($news_current_affair_contact);
		}

		// Create a About page and assign the template
		$news_current_affair_about_title = 'About';
		$news_current_affair_about_check = get_page_by_path('about');
		if (!$news_current_affair_about_check) {
			$news_current_affair_about = array(
				'post_type'    => 'page',
				'post_title'   => $news_current_affair_about_title,
				'post_content'   => '"More About The Free News Current Affair Theme"
										The Free News WordPress Theme is designed to cater to both beginner users and seasoned news publishers, making it an ideal choice for anyone looking to build a professional and content-rich online news platform. With its intuitive interface and ready-to-use design elements, it simplifies the process of creating a site focused on the latest news, current events, or in-depth analysis. Whether you plan to highlight national news, business updates, political coverage, or entertainment stories, this theme adjusts seamlessly to your editorial focus. The widget-friendly layout supports weather widgets, trending news tickers, and social media integration to enhance engagement. This theme is also perfect for community-based reporting, user-generated content, or citizen journalism, providing a reliable and visually impressive structure at no cost while maintaining performance, responsiveness, and flexibility.

										As the digital news landscape rapidly expands, having a reliable, stylish, and functional theme is more important than ever. The Free News WordPress Theme rises to this challenge by supporting multiple post formats, including text, audio, and video, making it ideal for modern multimedia storytelling. Whether you\'re a seasoned journalist, an aspiring reporter, or part of a grassroots news team, this theme empowers you to share compelling stories in a visually appealing format. It features editorial sections for opinion pieces and in-depth columns, while also enabling the quick upload of breaking news, live updates, or visually rich photo essays. The Free News WordPress Theme even supports podcast episodes and video-based content, allowing you to turn your platform into a truly dynamic, all-in-one digital news hub.

										The Free News WordPress Theme also stands out for its impressive speed and high functionality, making it a top choice for modern news publishers. Built with lightweight, clean code, it delivers fast load times—crucial for keeping visitors engaged in a high-traffic, fast-paced news environment. This performance-optimized theme is fully responsive and mobile-friendly, offering swipeable carousels, intuitive dropdown menus, and collapsible sidebars that enhance user navigation on smartphones and tablets. Users can access trending news, breaking headlines, and featured stories with zero delays. It’s compatible with AMP and popular caching plugins, which further improve site speed and overall performance. Whether you’re running an independent news blog, a regional news portal, or a full-fledged digital media outlet, this theme provides the essential tools to inform, engage, and build a loyal audience in today’s competitive digital news landscape.',
					'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'about' // Unique slug for the About page
			);
			wp_insert_post($news_current_affair_about);
		}

		// Create a Services page and assign the template
		$news_current_affair_services_title = 'Services';
		$news_current_affair_services_check = get_page_by_path('services');
		if (!$news_current_affair_services_check) {
			$news_current_affair_services = array(
				'post_type'    => 'page',
				'post_title'   => $news_current_affair_services_title,
				'post_content'   => '"More About The Free News Current Affair Theme"
										The Free News WordPress Theme is designed to cater to both beginner users and seasoned news publishers, making it an ideal choice for anyone looking to build a professional and content-rich online news platform. With its intuitive interface and ready-to-use design elements, it simplifies the process of creating a site focused on the latest news, current events, or in-depth analysis. Whether you plan to highlight national news, business updates, political coverage, or entertainment stories, this theme adjusts seamlessly to your editorial focus. The widget-friendly layout supports weather widgets, trending news tickers, and social media integration to enhance engagement. This theme is also perfect for community-based reporting, user-generated content, or citizen journalism, providing a reliable and visually impressive structure at no cost while maintaining performance, responsiveness, and flexibility.

										As the digital news landscape rapidly expands, having a reliable, stylish, and functional theme is more important than ever. The Free News WordPress Theme rises to this challenge by supporting multiple post formats, including text, audio, and video, making it ideal for modern multimedia storytelling. Whether you\'re a seasoned journalist, an aspiring reporter, or part of a grassroots news team, this theme empowers you to share compelling stories in a visually appealing format. It features editorial sections for opinion pieces and in-depth columns, while also enabling the quick upload of breaking news, live updates, or visually rich photo essays. The Free News WordPress Theme even supports podcast episodes and video-based content, allowing you to turn your platform into a truly dynamic, all-in-one digital news hub.

										The Free News WordPress Theme also stands out for its impressive speed and high functionality, making it a top choice for modern news publishers. Built with lightweight, clean code, it delivers fast load times—crucial for keeping visitors engaged in a high-traffic, fast-paced news environment. This performance-optimized theme is fully responsive and mobile-friendly, offering swipeable carousels, intuitive dropdown menus, and collapsible sidebars that enhance user navigation on smartphones and tablets. Users can access trending news, breaking headlines, and featured stories with zero delays. It’s compatible with AMP and popular caching plugins, which further improve site speed and overall performance. Whether you’re running an independent news blog, a regional news portal, or a full-fledged digital media outlet, this theme provides the essential tools to inform, engage, and build a loyal audience in today’s competitive digital news landscape.',
					'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'services' // Unique slug for the Services page
			);
			wp_insert_post($news_current_affair_services);
		}


		// ------------------------------------------ Sections -------------------------------------- //

			set_theme_mod('news_current_affair_service_show_hide',1);
			set_theme_mod('news_current_affair_enable_slider',1);
			set_theme_mod('news_current_affair_enable_slider',true);
			

		// ------------------------------------------ Slider Section -------------------------------------- //

			wp_delete_post(1);
			$blog_title = array(
				'Global Leaders Unite to Address Climate Crisis at Historic Summit in Geneva',
				'Economy Rebounds with g Strong Growth',
				'Global Warming Threatens Ecosystem Stabilit'
			);
			
			for($i=1;$i<=3;$i++){
				$title =$blog_title[$i-1];
				$content = 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.';

				// Create post object
				$my_post = array(
					'post_title'    => wp_strip_all_tags( $title ),
					'post_content'  => $content,
					'post_status'   => 'publish',
					'post_type'     => 'post',
				);

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

				$image_url = get_template_directory_uri().'/revolution/assets/images/about'.$i.'.png';

				$image_name= 'about'.$i.'.png';
				$upload_dir       = wp_upload_dir();
				// Set upload folder
				$image_data       = file_get_contents($image_url);
				// Get image data
				$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name );
				// Generate unique name
				$filename= basename( $unique_file_name );
				// Create image file name
				// Check folder permission and define file location
				if( wp_mkdir_p( $upload_dir['path'] ) ) {
					$file = $upload_dir['path'] . '/' . $filename;
				} else {
					$file = $upload_dir['basedir'] . '/' . $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( $file, $image_data, FS_CHMOD_FILE ) ) {
					wp_die( 'Error saving file!' );
				}
				// Check image file type
				$wp_filetype = wp_check_filetype( $filename, null );
				// Set attachment data
				$attachment = array(
				'post_mime_type' => $wp_filetype['type'],
				'post_title'     => sanitize_file_name( $filename ),
				'post_content'   => '',
				'post_type'     => 'post',
				'post_status'    => 'inherit'
				);
				// Create the attachment
				$attach_id = wp_insert_attachment( $attachment, $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, $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 );
			}

		// ------------------------------------------ Chef Section -------------------------------------- //

			// Define tab names (categories)
			$categories = array('All', 'Entertainment', 'Fashion', 'Food and Drinks');

			// Set how many posts per category
			$posts_per_category = 1;

			// Define titles for each category
			$post_titles = array(
				'All' => array(
					'Economic Growth Surges Beyond Expectations',
				),
				'Entertainment' => array(
					'Best Laptops Under $1000',
				),
				'Fashion' => array(
					'iPhone 15 Pro Max Hands-on',
				),
				'Food and Drinks' => array(
					'Top Smartwatches in Market',
				)
			);

			// Set theme mods and create categories
			foreach ($categories as $i => $cat_name) {
				$index = $i + 1;
				set_theme_mod('news_current_affair_projetcs_text' . $index, $cat_name);
				$cat_id = wp_create_category($cat_name);
				set_theme_mod('news_current_affair_projetcs_category' . $index, $cat_name);
			}

			set_theme_mod('news_current_affair_latest_post_heading', 'LATEST POST');
			set_theme_mod('news_current_affair_latest_post_tab_number', count($categories));

			// Now insert posts for each category
			foreach ($categories as $cat_name) {
				$cat_id = get_cat_ID($cat_name);
				$titles = $post_titles[$cat_name];

				foreach ($titles as $j => $title) {
					$content = 'This post is under category: ' . $cat_name . '.  Lorem Ipsum has been the industrys 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.';

					$post_id = wp_insert_post(array(
						'post_title'    => $title,
						'post_content'  => $content,
						'post_status'   => 'publish',
						'post_type'     => 'post',
						'post_category' => array($cat_id),
					));

					// Build image URL: /revolution/assets/images/category-name/image1.png
					$image_index = $j + 1;
					$folder_slug = sanitize_title($cat_name); // Converts "Smart Watches" => "smart-watches"
					$image_url = get_template_directory_uri() . '/revolution/assets/images/' . $folder_slug . '/image' . $image_index . '.png';

					$image_name = $folder_slug . '-image' . $image_index . '.png';
					$upload_dir = wp_upload_dir();
					$image_data = @file_get_contents($image_url);

					if ($image_data) {
						$unique_file_name = wp_unique_filename($upload_dir['path'], $image_name);
						$filename = basename($unique_file_name);
						$file = wp_mkdir_p($upload_dir['path']) ? $upload_dir['path'] . '/' . $filename : $upload_dir['basedir'] . '/' . $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( $file, $image_data, FS_CHMOD_FILE ) ) {
							wp_die( 'Error saving file!' );
						}

						$wp_filetype = wp_check_filetype($filename, null);
						$attachment = array(
							'post_mime_type' => $wp_filetype['type'],
							'post_title'     => sanitize_file_name($filename),
							'post_content'   => '',
							'post_status'    => 'inherit'
						);

						$attach_id = wp_insert_attachment($attachment, $file, $post_id);
						require_once(ABSPATH . 'wp-admin/includes/image.php');
						$attach_data = wp_generate_attachment_metadata($attach_id, $file);
						wp_update_attachment_metadata($attach_id, $attach_data);
						set_post_thumbnail($post_id, $attach_id);
					}
				}
			}


  	 	$this->news_current_affair_customizer_nav_menu();
  	 	$this->news_current_affair_social_menu();
		update_option('news_current_affair_demo_imported', true);
	}

	public function news_current_affair_guide() {
		$display_string = '';
		$return = add_query_arg( array()) ;
		$theme = wp_get_theme( 'news-current-affair' );
		?>
		<div class="wrapper-info get-stared-page-wrap">
			<div class="wrapper-info-content">
				<div class="buynow__">
					<h2><?php esc_html_e( 'Welcome to News Current Affair', 'news-current-affair' ); ?> <span class="version">Version: <?php echo esc_html($theme['Version']);?></span></h2>
					<p><?php esc_html_e('The quick setup wizard will assist you in configuring your new website. This wizard will import the demo content.', 'news-current-affair'); ?></p>
				</div>
				<div class="buynow_">
					<a target="_blank" class="buynow_themepage" href="<?php echo esc_url('https://www.revolutionwp.com/products/latest-news-wordpress-theme'); ?>"><?php echo esc_html__('Go Premium Now', 'news-current-affair'); ?></a>
				</div>
			</div>
			<div class="tab-sec theme-option-tab">
				<div id="demo_offer" class="tabcontent open">
					<?php $this->wizard_page(); ?>
				</div>
			</div>
		</div>
	<?php }
}