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

        public function animal_caretaker_customizer_primary_menu() {
            // Create Primary Menu
            $animal_caretaker_themename = 'Animal Caretaker'; // Define the theme name
            $animal_caretaker_menuname = $animal_caretaker_themename . ' Primary Menu';
            $animal_caretaker_bpmenulocation = 'primary_menu';
            $animal_caretaker_menu_exists = wp_get_nav_menu_object($animal_caretaker_menuname);

            if (!$animal_caretaker_menu_exists) {
                $animal_caretaker_menu_id = wp_create_nav_menu($animal_caretaker_menuname);

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

                wp_update_nav_menu_item($animal_caretaker_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'animal-caretaker'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => get_permalink(get_page_id_by_title('about-us')),
                    'menu-item-status' => 'publish',
                ));

                wp_update_nav_menu_item($animal_caretaker_menu_id, 0, array(
                    'menu-item-title' => __('Features', 'animal-caretaker'),
                    'menu-item-classes' => 'features',
                    'menu-item-url' => get_permalink(get_page_id_by_title('features')),
                    'menu-item-status' => 'publish',
                ));

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

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

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


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


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

        public function setup_widgets() {

        $animal_caretaker_home_id='';
        $animal_caretaker_home_content = '';
        $animal_caretaker_home_title = 'Home';
        $animal_caretaker_home = array(
            'post_type' => 'page',
            'post_title' => $animal_caretaker_home_title,
            'post_content' => $animal_caretaker_home_content,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'home'
        );
        $animal_caretaker_home_id = wp_insert_post($animal_caretaker_home);
        
        add_post_meta( $animal_caretaker_home_id, '_wp_page_template', 'templates/template-homepage.php' );

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

                        // Create a Posts Page
            $animal_caretaker_blog_title = 'About Us';
            $animal_caretaker_blog_check = get_page_id_by_title($animal_caretaker_blog_title);

            if ($animal_caretaker_blog_check == 1) {
                $animal_caretaker_blog = array(
                    'post_type' => 'page',
                    'post_title' => $animal_caretaker_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."',
                );
                $animal_caretaker_blog_id = wp_insert_post($animal_caretaker_blog);

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

                        // Create a Posts Page
            $animal_caretaker_blog_title = 'Features';
            $animal_caretaker_blog_check = get_page_id_by_title($animal_caretaker_blog_title);

            if ($animal_caretaker_blog_check  == 1) {
                $animal_caretaker_blog = array(
                    'post_type' => 'page',
                    'post_title' => $animal_caretaker_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."',
                );
                $animal_caretaker_blog_id = wp_insert_post($animal_caretaker_blog);

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

                         // Create a Posts Page
            $animal_caretaker_blog_title = 'Gallery';
            $animal_caretaker_blog_check = get_page_id_by_title($animal_caretaker_blog_title);

            if ($animal_caretaker_blog_check  == 1) {
                $animal_caretaker_blog = array(
                    'post_type' => 'page',
                    'post_title' => $animal_caretaker_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."',
                );
                $animal_caretaker_blog_id = wp_insert_post($animal_caretaker_blog);

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

                         // Create a Posts Page
            $animal_caretaker_blog_title = 'Blog';
            $animal_caretaker_blog_check = get_page_id_by_title($animal_caretaker_blog_title);

            if ($animal_caretaker_blog_check  == 1) {
                $animal_caretaker_blog = array(
                    'post_type' => 'page',
                    'post_title' => $animal_caretaker_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."',
                );
                $animal_caretaker_blog_id = wp_insert_post($animal_caretaker_blog);

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

                         // Create a Posts Page
            $animal_caretaker_blog_title = 'Gallery';
            $animal_caretaker_blog_check = get_page_id_by_title($animal_caretaker_blog_title);

            if ($animal_caretaker_blog_check  == 1) {
                $animal_caretaker_blog = array(
                    'post_type' => 'page',
                    'post_title' => $animal_caretaker_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."',
                );
                $animal_caretaker_blog_id = wp_insert_post($animal_caretaker_blog);

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

                                     // Create a Posts Page
            $animal_caretaker_blog_title = 'Contact';
            $animal_caretaker_blog_check = get_page_id_by_title($animal_caretaker_blog_title);

            if ($animal_caretaker_blog_check  == 1) {
                $animal_caretaker_blog = array(
                    'post_type' => 'page',
                    'post_title' => $animal_caretaker_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."',
                );
                $animal_caretaker_blog_id = wp_insert_post($animal_caretaker_blog);

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

		//---Header--//

		set_theme_mod('animal_caretaker_header_button_link', '#');
		set_theme_mod('animal_caretaker_header_button_text', 'Get in Touch');

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

		set_theme_mod( 'animal_caretaker_slide_on_off', 'on' );

		set_theme_mod('animal_caretaker_slide_count','2');

		$animal_caretaker_slider_title=array('Get Your Favourite Pet', 'Acquire Your Favorite Pet');

		for ($i=1; $i <= 2; $i++) {
			set_theme_mod( 'animal_caretaker_slider_image'.$i, get_template_directory_uri().'/images/demo-import-images/slides/slide_'.$i.'.png' );
			
			set_theme_mod('animal_caretaker_slider_heading'.$i, $animal_caretaker_slider_title[$i - 1]);
			set_theme_mod('animal_caretaker_slider_button_link'.$i, '#');
			set_theme_mod('animal_caretaker_slider_button_text'.$i, 'Watch Video');
		}

		//-----Products-----//

		set_theme_mod( 'animal_caretaker_product_on_off', 'on' );

		set_theme_mod('animal_caretaker_featured_product_heading', 'Featured Products');

      	 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( 'animal_caretaker_featured_product_category', 'Awesome Product' );
          	if ( class_exists( 'WooCommerce' ) ) {

				$featured_product=array('Fishing Net', 'Flower Vaas', 'Tank Cleaner Pump', 'Rotating Sweeper');
	            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', "$50" );
	            update_post_meta( $post_id, '_regular_price', "$100" );

	            $image_url = get_template_directory_uri().'/images/demo-import-images/products/featured-product_'.$i.'.png';
	            $image_name= 'image'.$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'     => '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 AnimalCaretakerDemoImporter();
    $demo_importer->setup_widgets();
    $demo_importer->animal_caretaker_customizer_primary_menu();
}
?>