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

        public function realestate_agent_customizer_primary_menu() {
            // Create Primary Menu
            $realestate_agent_themename = 'Realestate Agent'; // Define the theme name
            $realestate_agent_menuname = $realestate_agent_themename . 'Main Menus';
            $realestate_agent_bpmenulocation = 'main-menu';
            $realestate_agent_menu_exists = wp_get_nav_menu_object($realestate_agent_menuname);

            if (!$realestate_agent_menu_exists) {
                $realestate_agent_menu_id = wp_create_nav_menu($realestate_agent_menuname);

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

                wp_update_nav_menu_item($realestate_agent_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'realestate-agent'),
                    '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($realestate_agent_menu_id, 0, array(
                    'menu-item-title' => __('Properties', 'realestate-agent'),
                    'menu-item-classes' => 'properties',
                    'menu-item-url' => get_permalink(get_page_id_by_title('properties')),
                    'menu-item-status' => 'publish',
                ));

                wp_update_nav_menu_item($realestate_agent_menu_id, 0, array(
                    'menu-item-title' => __('Blog', 'realestate-agent'),
                    'menu-item-classes' => 'resources',
                    'menu-item-url' => get_permalink(get_page_id_by_title('resources')),
                    'menu-item-status' => 'publish',
                ));

                wp_update_nav_menu_item($realestate_agent_menu_id, 0, array(
                    'menu-item-title' => __('Contact Us', 'realestate-agent'),
                    'menu-item-classes' => 'contact-us',
                    'menu-item-url' => get_permalink(get_page_id_by_title('contact-us')),
                    'menu-item-status' => 'publish',
                ));

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

        public function setup_widgets() {

        $realestate_agent_home_id='';
        $realestate_agent_home_content = '';
        $realestate_agent_home_title = 'Home';
        $realestate_agent_home = array(
            'post_type' => 'page',
            'post_title' => $realestate_agent_home_title,
            'post_content' => $realestate_agent_home_content,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'home'
        );
        $realestate_agent_home_id = wp_insert_post($realestate_agent_home);

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

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

                        // Create a Posts Page
            $realestate_agent_blog_title = 'About Us';
            $realestate_agent_blog_check = get_page_id_by_title($realestate_agent_blog_title);

            if ($realestate_agent_blog_check == 1) {
                $realestate_agent_blog = array(
                    'post_type' => 'page',
                    'post_title' => $realestate_agent_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."',
                );
                $realestate_agent_blog_id = wp_insert_post($realestate_agent_blog);

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

                        // Create a Posts Page
            $realestate_agent_blog_title = 'Properties';
            $realestate_agent_blog_check = get_page_id_by_title($realestate_agent_blog_title);

            if ($realestate_agent_blog_check  == 1) {
                $realestate_agent_blog = array(
                    'post_type' => 'page',
                    'post_title' => $realestate_agent_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'properties',
                    '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."',
                );
                $realestate_agent_blog_id = wp_insert_post($realestate_agent_blog);

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

                     // Create a Posts Page
            $realestate_agent_blog_title = 'Blog';
            $realestate_agent_blog_check = get_page_id_by_title($realestate_agent_blog_title);

            if ($realestate_agent_blog_check  == 1) {
                $realestate_agent_blog = array(
                    'post_type' => 'page',
                    'post_title' => $realestate_agent_blog_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_name' => 'resources',
                    '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."',
                );
                $realestate_agent_blog_id = wp_insert_post($realestate_agent_blog);

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

                                                 // Create a Posts Page
            $realestate_agent_blog_title = 'Contact Us';
            $realestate_agent_blog_check = get_page_id_by_title($realestate_agent_blog_title);

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

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


		//---Top Header--//

		set_theme_mod( 'realestate_agent_header_phone_number', '+1234567890');
		set_theme_mod( 'realestate_agent_header_email_address', 'info@exampl.com');

		set_theme_mod('realestate_agent_social_links_settings', array(
            array(
                "link_text" => "fab fa-facebook-f",
                "link_url" => "#"
            ),
            array(
                "link_text" => "fab fa-twitter",
                "link_url" => "#"
            ),
            array(
                "link_text" => "fab fa-linkedin",
                "link_url" => "#"
            ),
            array(
                "link_text" => "fab fa-youtube",
                "link_url" => "#"
            ),
            array(
                "link_text" => "fab fa-instagram",
                "link_url" => "#"
            )
        ));

		//---Header--//

		set_theme_mod( 'realestate_agent_header_property_btn_text', 'Add Property');
		set_theme_mod( 'realestate_agent_header_property_btn_link', '#');

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

		set_theme_mod( 'realestate_agent_blog_box_enable', true);

		set_theme_mod( 'realestate_agent_blog_slide_number', '3' );
		$realestate_agent_latest_post_category = wp_create_category('Slider Post');
			set_theme_mod( 'realestate_agent_blog_slide_category', 'Slider Post' ); 

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

			$title =   'Your Gateway to a Richer Life';
			$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';

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

			// Insert the post into the database
			$realestate_agent_post_id = wp_insert_post( $realestate_agent_my_post );

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

			$realestate_agent_image_name = 'slider'.$i.'.png';
			$realestate_agent_upload_dir = wp_upload_dir(); 
			// Set upload folder
			$realestate_agent_image_data = file_get_contents($realestate_agent_image_url); 
			 
			// Get image data
			$realestate_agent_unique_file_name = wp_unique_filename( $realestate_agent_upload_dir['path'], $realestate_agent_image_name ); 
			// Generate unique name
			$filename= basename( $realestate_agent_unique_file_name ); 
			// Create image file name
			// Check folder permission and define file location
			if( wp_mkdir_p( $realestate_agent_upload_dir['path'] ) ) {
				$file = $realestate_agent_upload_dir['path'] . '/' . $filename;
			} else {
				$file = $realestate_agent_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, $realestate_agent_image_data, FS_CHMOD_FILE ) ) {
                        wp_die( 'Error saving file!' );
                    }
			$realestate_agent_wp_filetype = wp_check_filetype( $filename, null );
			$realestate_agent_attachment = array(
				'post_mime_type' => $realestate_agent_wp_filetype['type'],
				'post_title'     => sanitize_file_name( $filename ),
				'post_content'   => '',
				'post_type'     => 'post',
				'post_status'    => 'inherit'
			);
			$realestate_agent_attach_id = wp_insert_attachment( $realestate_agent_attachment, $file, $realestate_agent_post_id );
			require_once(ABSPATH . 'wp-admin/includes/image.php');
			$realestate_agent_attach_data = wp_generate_attachment_metadata( $realestate_agent_attach_id, $file );
				wp_update_attachment_metadata( $realestate_agent_attach_id, $realestate_agent_attach_data );
				set_post_thumbnail( $realestate_agent_post_id, $realestate_agent_attach_id );
		}

		//--------------Properties Section-----------//

		set_theme_mod( 'realestate_agent_your_properties_enable', true);

		set_theme_mod( 'realestate_agent_section_title', 'Find Your Properties');
		set_theme_mod( 'realestate_agent_section_text', 'Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer');

		set_theme_mod( 'realestate_agent_section_btn_text', 'More Locations');
		set_theme_mod( 'realestate_agent_section_btn_link', '#');
		
		$realestate_agent_properties_category = wp_create_category('Properties');
		set_theme_mod( 'realestate_agent_your_properties_category', 'Properties' );
		
        set_theme_mod( 'realestate_agent_your_properties_number', 5);

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

			$title = array('United Arab Emairates', 'San Fransisco', 'Las Vegas', 'United States', 'United Kingdom');
			$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';

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

			// Insert the post into the database
			$realestate_agent_post_id = wp_insert_post( $realestate_agent_my_post );

			$realestate_agent_image_url = get_template_directory_uri().'/assets/images/property'.$i.'.png';

			$realestate_agent_image_name = 'property'.$i.'.png';
			$realestate_agent_upload_dir = wp_upload_dir(); 
			// Set upload folder
			$realestate_agent_image_data_1 = file_get_contents($realestate_agent_image_url);

			$properties_count = array('28', '9', '20', '50', '35');
			 
			update_post_meta( $realestate_agent_post_id, 'realestate_agent_your_properties_count', $properties_count[$i-1]);

			// Get image data
			$realestate_agent_unique_file_name = wp_unique_filename( $realestate_agent_upload_dir['path'], $realestate_agent_image_name ); 
			// Generate unique name
			$filename= basename( $realestate_agent_unique_file_name ); 
			// Create image file name
			// Check folder permission and define file location
			if( wp_mkdir_p( $realestate_agent_upload_dir['path'] ) ) {
				$file = $realestate_agent_upload_dir['path'] . '/' . $filename;
			} else {
				$file = $realestate_agent_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, $realestate_agent_image_data_1, FS_CHMOD_FILE ) ) {
                        wp_die( 'Error saving file!' );
                    }

			$realestate_agent_wp_filetype = wp_check_filetype( $filename, null );
			$realestate_agent_attachment = array(
				'post_mime_type' => $realestate_agent_wp_filetype['type'],
				'post_title'     => sanitize_file_name( $filename ),
				'post_content'   => '',
				'post_type'     => 'post',
				'post_status'    => 'inherit'
			);
			$realestate_agent_attach_id = wp_insert_attachment( $realestate_agent_attachment, $file, $realestate_agent_post_id );
			require_once(ABSPATH . 'wp-admin/includes/image.php');
			$realestate_agent_attach_data = wp_generate_attachment_metadata( $realestate_agent_attach_id, $file );
				wp_update_attachment_metadata( $realestate_agent_attach_id, $realestate_agent_attach_data );
				set_post_thumbnail( $realestate_agent_post_id, $realestate_agent_attach_id );
		}
		    }
    }

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