<?php
if (isset($_GET['import-demo']) && $_GET['import-demo'] == true) {
    class FishAquariumDemoImporter {

        public function fish_aquarium_customizer_primary_menu() {
            // Create Primary Menu
            $fish_aquarium_themename = 'Fish Aquarium'; // Define the theme name
            $fish_aquarium_menuname = $fish_aquarium_themename . 'Main Menus';
            $fish_aquarium_bpmenulocation = 'main-menu';
            $fish_aquarium_menu_exists = wp_get_nav_menu_object($fish_aquarium_menuname);

            if (!$fish_aquarium_menu_exists) {
                $fish_aquarium_menu_id = wp_create_nav_menu($fish_aquarium_menuname);

                wp_update_nav_menu_item($fish_aquarium_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'fish-aquarium'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                ));

                wp_update_nav_menu_item($fish_aquarium_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'fish-aquarium'),
                    'menu-item-classes' => 'products',
                    'menu-item-url' => get_permalink(get_page_id_by_title('About Us')),
                    'menu-item-status' => 'publish',
                ));

                wp_update_nav_menu_item($fish_aquarium_menu_id, 0, array(
                    'menu-item-title' => __('Features', 'fish-aquarium'),
                    'menu-item-classes' => 'collection',
                    'menu-item-url' => get_permalink(get_page_id_by_title('Features')),
                    'menu-item-status' => 'publish',
                ));

                wp_update_nav_menu_item($fish_aquarium_menu_id, 0, array(
                    'menu-item-title' => __('Shop', 'fish-aquarium'),
                    'menu-item-classes' => 'shop',
                    'menu-item-url' => get_permalink(get_page_id_by_title('Shop')),
                    'menu-item-status' => 'publish',
                ));

                wp_update_nav_menu_item($fish_aquarium_menu_id, 0, array(
                    'menu-item-title' => __('Blog', 'fish-aquarium'),
                    'menu-item-classes' => 'blog',
                    'menu-item-url' => get_permalink(get_page_id_by_title('Blog')),
                    'menu-item-status' => 'publish',
                ));

                wp_update_nav_menu_item($fish_aquarium_menu_id, 0, array(
                    'menu-item-title' => __('Gallery', 'fish-aquarium'),
                    'menu-item-classes' => 'gallery',
                    'menu-item-url' => get_permalink(get_page_id_by_title('Gallery')),
                    'menu-item-status' => 'publish',
                ));

                wp_update_nav_menu_item($fish_aquarium_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'fish-aquarium'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => get_permalink(get_page_id_by_title('Contact')),
                    'menu-item-status' => 'publish',
                ));

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

        public function setup_widgets() {

        $fish_aquarium_home_id='';
        $fish_aquarium_home_content = '';
        $fish_aquarium_home_title = 'Home';
        $fish_aquarium_home = array(
            'post_type' => 'page',
            'post_title' => $fish_aquarium_home_title,
            'post_content' => $fish_aquarium_home_content,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'home'
        );
        $fish_aquarium_home_id = wp_insert_post($fish_aquarium_home);

        add_post_meta( $fish_aquarium_home_id, '_wp_page_template', 'frontpage.php' );

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

                        // Create a Posts Page
            $fish_aquarium_blog_title = 'About Us';
            $fish_aquarium_blog_check = get_page_id_by_title($fish_aquarium_blog_title);

            if ($fish_aquarium_blog_check == 1) {
                $fish_aquarium_blog = array(
                    'post_type' => 'page',
                    'post_title' => $fish_aquarium_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'products',
                    '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."',
                );
                $fish_aquarium_blog_id = wp_insert_post($fish_aquarium_blog);

                if (!is_wp_error($fish_aquarium_blog_id)) {
                    // update_option('page_for_posts', $fish_aquarium_blog_id);
                }
            }

                        // Create a Posts Page
            $fish_aquarium_blog_title = 'Features';
            $fish_aquarium_blog_check = get_page_id_by_title($fish_aquarium_blog_title);

            if ($fish_aquarium_blog_check  == 1) {
                $fish_aquarium_blog = array(
                    'post_type' => 'page',
                    'post_title' => $fish_aquarium_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'features',
                    '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."',
                );
                $fish_aquarium_blog_id = wp_insert_post($fish_aquarium_blog);

                if (!is_wp_error($fish_aquarium_blog_id)) {
                    // update_option('page_for_posts', $fish_aquarium_blog_id);
                }
            }

                         // Create a Posts Page
            $fish_aquarium_blog_title = 'SHOP';
            $fish_aquarium_blog_check = get_page_id_by_title($fish_aquarium_blog_title);

            if ($fish_aquarium_blog_check  == 1) {
                $fish_aquarium_blog = array(
                    'post_type' => 'page',
                    'post_title' => $fish_aquarium_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'shop',
                    '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."',
                );
                $fish_aquarium_blog_id = wp_insert_post($fish_aquarium_blog);

                if (!is_wp_error($fish_aquarium_blog_id)) {
                    // update_option('page_for_posts', $fish_aquarium_blog_id);
                }
            }

                         // Create a Posts Page
            $fish_aquarium_blog_title = 'Blog';
            $fish_aquarium_blog_check = get_page_id_by_title($fish_aquarium_blog_title);

            if ($fish_aquarium_blog_check  == 1) {
                $fish_aquarium_blog = array(
                    'post_type' => 'page',
                    'post_title' => $fish_aquarium_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'blog',
                    '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."',
                );
                $fish_aquarium_blog_id = wp_insert_post($fish_aquarium_blog);

                if (!is_wp_error($fish_aquarium_blog_id)) {
                    // update_option('page_for_posts', $fish_aquarium_blog_id);
                }
            }

                                     // Create a Posts Page
            $fish_aquarium_blog_title = 'Gallery';
            $fish_aquarium_blog_check = get_page_id_by_title($fish_aquarium_blog_title);

            if ($fish_aquarium_blog_check  == 1) {
                $fish_aquarium_blog = array(
                    'post_type' => 'page',
                    'post_title' => $fish_aquarium_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'gallery',
                    '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."',
                );
                $fish_aquarium_blog_id = wp_insert_post($fish_aquarium_blog);

                if (!is_wp_error($fish_aquarium_blog_id)) {
                    // update_option('page_for_posts', $fish_aquarium_blog_id);
                }
            }

                                     // Create a Posts Page
            $fish_aquarium_blog_title = 'Contact';
            $fish_aquarium_blog_check = get_page_id_by_title($fish_aquarium_blog_title);

            if ($fish_aquarium_blog_check  == 1) {
                $fish_aquarium_blog = array(
                    'post_type' => 'page',
                    'post_title' => $fish_aquarium_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'contact',
                    '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."',
                );
                $fish_aquarium_blog_id = wp_insert_post($fish_aquarium_blog);

                if (!is_wp_error($fish_aquarium_blog_id)) {
                    // update_option('page_for_posts', $fish_aquarium_blog_id);
                }
            }

		//---Header--//

		set_theme_mod( 'fish_aquarium_header_btn_text', 'Get in Touch');

		set_theme_mod( 'fish_aquarium_header_btn_url', '#');

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

		set_theme_mod( 'fish_aquarium_blog_box_enable', true);

		set_theme_mod( 'fish_aquarium_blog_slide_number', '3' );
		$fish_aquarium_latest_post_category = wp_create_category('Slider Post');
			set_theme_mod( 'fish_aquarium_blog_slide_category', 'Slider Post' ); 

		for($i=1; $i<=3; $i++) {

        	$title = array('Get Experience of Under Water World', 'Discover the Underwater World', 'Experience the underwater world');

			// Create post object
			$fish_aquarium_my_post = array(
				'post_title'    => wp_strip_all_tags( $title[$i-1] ),
				'post_status'   => 'publish',
				'post_type'     => 'post',
				'post_category' => array($fish_aquarium_latest_post_category) 
			);

			// Insert the post into the database
			$fish_aquarium_post_id = wp_insert_post( $fish_aquarium_my_post );

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

			$fish_aquarium_image_name= 'slider'.$i.'.png';
			$fish_aquarium_upload_dir = wp_upload_dir(); 
			// Set upload folder
			$fish_aquarium_image_data = file_get_contents($fish_aquarium_image_url); 
			 
			// Get image data
			$fish_aquarium_unique_file_name = wp_unique_filename( $fish_aquarium_upload_dir['path'], $fish_aquarium_image_name ); 
			// Generate unique name
			$filename= basename( $fish_aquarium_unique_file_name ); 
			// Create image file name
			// Check folder permission and define file location
			if( wp_mkdir_p( $fish_aquarium_upload_dir['path'] ) ) {
				$file = $fish_aquarium_upload_dir['path'] . '/' . $filename;
			} else {
				$file = $fish_aquarium_upload_dir['basedir'] . '/' . $filename;
			}
            if ( ! function_exists( 'WP_Filesystem' ) ) {
                require_once( ABSPATH . 'wp-admin/includes/file.php' );
            }
            
            WP_Filesystem();
            global $wp_filesystem;
            
            if ( ! $wp_filesystem->put_contents( $file, $fish_aquarium_image_data, FS_CHMOD_FILE ) ) {
                wp_die( 'Error saving file!' );
            }
			$fish_aquarium_wp_filetype = wp_check_filetype( $filename, null );
			$fish_aquarium_attachment = array(
				'post_mime_type' => $fish_aquarium_wp_filetype['type'],
				'post_title'     => sanitize_file_name( $filename ),
				'post_content'   => '',
				'post_type'     => 'post',
				'post_status'    => 'inherit'
			);
			$fish_aquarium_attach_id = wp_insert_attachment( $fish_aquarium_attachment, $file, $fish_aquarium_post_id );
			require_once(ABSPATH . 'wp-admin/includes/image.php');
			$fish_aquarium_attach_data = wp_generate_attachment_metadata( $fish_aquarium_attach_id, $file );
				wp_update_attachment_metadata( $fish_aquarium_attach_id, $fish_aquarium_attach_data );
				set_post_thumbnail( $fish_aquarium_post_id, $fish_aquarium_attach_id );
		}

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

		set_theme_mod( 'fish_aquarium_featured_product_section_enable', true);

        set_theme_mod( 'fish_aquarium_featured_product_heading', 'Featured Product');

	    wp_insert_term(
      		'awesome-product', // the term
      		'product_cat', // the taxonomy
      			array(
		          	'description'=> '',
		          	'slug' => 'awesome-product',
		          	'term_id'=>12,
		          	'term_taxonomy_id'=>34,
          		)
      		);

          	set_theme_mod( 'fish_aquarium_featured_product_category', 'Awesome Product' );
          	if ( class_exists( 'WooCommerce' ) ) {

				$featured_product=array('Fishing Net', 'Fish Pot', 'Oxygen Pump', 'Cleaning Brush');
	            for($i=1;$i<=4;$i++) {


	            $title =$featured_product[$i-1];
	            $content = 'Lorem Ipsum simply Te obtinuit ut adepto satis somno. Aliisque institoribus iter deliciae vivet vita. Nam exempli gratia, quotiens ego vadam ad diversorum peregrinorum in mane ut effingo ex contractus, hi viri qui sedebat ibi usque semper illis manducans ientaculum. Solum cum bulla ut debui; EGO youd adepto a macula proiciendi.';

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

	            // Insert the post into the database
	            $post_id = wp_insert_post( $my_post );
	            // Gets term object from Tree in the database.
	            $term = get_term_by('name', 'awesome-product', 'product_cat');
	            wp_set_object_terms($post_id, $term->term_id, 'product_cat');

	            update_post_meta( $post_id, '_price', '$50' );
	            update_post_meta( $post_id, '_sale_price', "$79" );
	            update_post_meta( $post_id, '_regular_price', "$99.00" );

	            $image_url = get_template_directory_uri().'/assets/images/featured-product'.$i.'.png';
	            $image_name= 'featured-product'.$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;
            }

            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'     => 'product',
	            '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 );
          	}
        }


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

	 }
    }

	// Instantiate the class and call its methods
    $demo_importer = new FishAquariumDemoImporter();
    $demo_importer->setup_widgets();
    $demo_importer->fish_aquarium_customizer_primary_menu();
}
		
?>