<?php
class Whizzie {

    public function __construct() {
        add_action( 'wp_ajax_spa_parlor_setup_widgets', array( $this, 'spa_parlor_setup_widgets' ) );
    }


    public static function spa_parlor_setup_widgets() {

    	$shoplentor_options = array (
	        'wishlist' => 'on',
	        'compare' => 'off',
	        'ajaxsearch' => 'off',
	        'ajaxcart_singleproduct' => 'off',
	        'postduplicator' => 'off',
	        'loadproductlimit' => 20.0,
	    );
	    update_option('woolentor_others_tabs', $shoplentor_options);


    	$spa_parlorhome_content = '';
		$spa_parlorhome_title = 'Home';
		$spa_parlorhome = array(
				'post_type' => 'page',
				'post_title' => $spa_parlorhome_title,
				'post_content'  => $spa_parlorhome_content,
				'post_status' => 'publish',
				'post_author' => 1,
				'post_slug' => 'home'
		);
		$spa_parlorhome_id = wp_insert_post($spa_parlorhome);

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

		$spa_parlorhome = get_page_by_path( 'Home' );
		update_option( 'page_on_front', $spa_parlorhome->ID );
		update_option( 'show_on_front', 'page' );

		// Creation of blog page //
		$spa_parlorblog_title = 'Blog';
		$spa_parlorblog_check = get_page_by_path('blog');
		if (!$spa_parlorblog_check) {
			$spa_parlorblog = array(
				'post_type'    => 'page',
				'post_title'   => $spa_parlorblog_title,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'blog'
			);
			$spa_parlorblog_id = wp_insert_post($spa_parlorblog);

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

		// Creation of about page //
		$spa_parlorabout_title = 'About';
		$spa_parlorabout_content = 'What is Lorem Ipsum?
													Lorem Ipsum is simply dummy text of the printing and typesetting industry. 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. 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.
													&nbsp;
													Why do we use it?
													It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
													&nbsp;
													Where does it come from?
													There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
													&nbsp;
													Why do we use it?
													It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
													&nbsp;
													Where does it come from?
													There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
		$spa_parlorabout_check = get_page_by_path('about');
		if (!$spa_parlorabout_check) {
			$spa_parlorabout = array(
				'post_type'    => 'page',
				'post_title'   => $spa_parlorabout_title,
				'post_content'   => $spa_parlorabout_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'about'
			);
			wp_insert_post($spa_parlorabout);
		}

		// Creation of services page //
		$spa_parlorservices_title = 'Contact';
		$spa_parlorservices_content = 'What is Lorem Ipsum?
													Lorem Ipsum is simply dummy text of the printing and typesetting industry. 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. 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.
													&nbsp;
													Why do we use it?
													It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
													&nbsp;
													Where does it come from?
													There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
													&nbsp;
													Why do we use it?
													It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
													&nbsp;
													Where does it come from?
													There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
		$spa_parlorservices_check = get_page_by_path('services');
		if (!$spa_parlorservices_check) {
			$spa_parlorservices = array(
				'post_type'    => 'page',
				'post_title'   => $spa_parlorservices_title,
				'post_content'   => $spa_parlorservices_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'services'
			);
			wp_insert_post($spa_parlorservices);
		}

		// Creation of pricing page //
		$spa_parlorpricing_title = 'Pricing';
		$spa_parlorpricing_content = 'What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. 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. 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.';
		$spa_parlorpricing_check = get_page_by_path('pricing');
		if (!$spa_parlorpricing_check) {
			$spa_parlorpricing = array(
				'post_type'    => 'page',
				'post_title'   => $spa_parlorpricing_title,
				'post_content'   => $spa_parlorpricing_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'pricing'
			);
			wp_insert_post($spa_parlorpricing);
		}

		// Creation of support page //
		$spa_parlorsupport_title = 'Support';
		$spa_parlorsupport_content = 'What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. 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. 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.';
		$spa_parlorsupport_check = get_page_by_path('support');
		if (!$spa_parlorsupport_check) {
			$spa_parlorsupport = array(
				'post_type'    => 'page',
				'post_title'   => $spa_parlorsupport_title,
				'post_content'   => $spa_parlorsupport_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'support'
			);
			wp_insert_post($spa_parlorsupport);
		}

		// Creation of features page //
		$spa_parlorfeatures_title = 'Features';
		$spa_parlorfeatures_content = 'What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. 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. 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.';
		$spa_parlorfeatures_check = get_page_by_path('features');
		if (!$spa_parlorfeatures_check) {
			$spa_parlorfeatures = array(
				'post_type'    => 'page',
				'post_title'   => $spa_parlorfeatures_title,
				'post_content'   => $spa_parlorfeatures_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'features'
			);
			wp_insert_post($spa_parlorfeatures);
		}

		// Creation of services page //
		$spa_parlorbirthday_title = 'Products';
		$spa_parlorbirthday_content = 'What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. 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. 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.';
		$spa_parlorbirthday_check = get_page_by_path('birthday');
		if (!$spa_parlorbirthday_check) {
			$spa_parlorbirthday = array(
				'post_type'    => 'page',
				'post_title'   => $spa_parlorbirthday_title,
				'post_content'   => $spa_parlorbirthday_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'birthday'
			);
			wp_insert_post($spa_parlorbirthday);
		}

		// Creation of Pages //
		$spa_parlornotfound_title = 'Pages';
		$spa_parlornotfound = array(
			'post_type'   => 'pages',
			'post_title'  => $spa_parlornotfound_title,
			'post_status' => 'publish',
			'post_author' => 1,
			'post_slug'   => 'pages'
		);
		$spa_parlornotfound_id = wp_insert_post($spa_parlornotfound);
		add_post_meta($spa_parlornotfound_id, '_wp_page_template', 'pages.php');


        // Creation of Product //
		$spa_parlor_product_image_gallery = array();
		$spa_parlor_product_ids = array();

		$spa_parlor_product_category= array(
			'All' => array(
				'Body Oil',
				'Facewash',
				'Lipstick',
				'Lotion',
			),
			'Oils' => array(
				'Body Oil',
				'Lipgloss',
				'moisture',
			),
			'Scrubs' => array(
				'Facewash',
				'Lipstick',
				'Lotion',
			),
			'Body' => array(
				'Body Oil',
				'Facewash',
				'Lotion',
			),
			'Face' => array(
				'Lipstick',
				'Lotion',
				'Lipgloss',
			),
		);

		$created_products = array(); 

		foreach ($spa_parlor_product_category as $spa_parlor_product_cats => $spa_parlor_products_name) {

			
			$spa_parlor_parent_category = wp_insert_term(
				$spa_parlor_product_cats,
				'product_cat',
				array(
					'description'=> 'This is sample product category',
					'slug' => sanitize_title($spa_parlor_product_cats)
				)
			);

			if (is_wp_error($spa_parlor_parent_category)) {
				$term = get_term_by('name', $spa_parlor_product_cats, 'product_cat');
				$term_id = $term->term_id;
			} else {
				$term_id = $spa_parlor_parent_category['term_id'];
			}

			foreach ($spa_parlor_products_name as $spa_parlor_product_title) {

				if (isset($created_products[$spa_parlor_product_title])) {
					wp_set_object_terms($created_products[$spa_parlor_product_title], $term_id, 'product_cat', true);
					continue;
				}

				$post_id = wp_insert_post(array(
					'post_title'   => wp_strip_all_tags($spa_parlor_product_title),
					'post_content' => '<p>Lorem Ipsum text</p>',
					'post_status'  => 'publish',
					'post_type'    => 'product',
				));

				$created_products[$spa_parlor_product_title] = $post_id;

				
				wp_set_object_terms($post_id, $term_id, 'product_cat');

				update_post_meta($post_id, '_price', '02.00');

				$spa_parlor_product_ids[] = $post_id;


				$image_url = get_template_directory_uri().'/assets/images/product/'.strtolower($spa_parlor_product_cats).'/' . str_replace(' ', '_', strtolower($spa_parlor_product_title)).'.png';

				$upload_dir = wp_upload_dir();
				$image_data = @file_get_contents($image_url);

				if ($image_data) {

					$unique_name = 'prod-' . time() . '-' . strtolower(str_replace(' ', '-', $spa_parlor_product_title)) . '.png';

					$filename = wp_unique_filename($upload_dir['path'], $unique_name);

					if (wp_mkdir_p($upload_dir['path'])) {
						$file = $upload_dir['path'].'/'.$filename;
					} else {
						$file = $upload_dir['basedir'].'/'.$filename;
					}

					file_put_contents($file, $image_data);

					$wp_filetype = wp_check_filetype($filename, null);

					$attachment = array(
						'post_mime_type' => $wp_filetype['type'],
						'post_title'     => sanitize_file_name($filename),
						'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);
				}
			}
		}

		$spa_parlor_product_image_gallery = implode(',', $spa_parlor_product_image_gallery);

		foreach ($spa_parlor_product_ids as $spa_parlor_product_id) {
			update_post_meta($spa_parlor_product_id, 'spa_parlor_product_image_gallery', $spa_parlor_product_image_gallery);
		}

        /* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- Spa parlor Primary Menu -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*/

		$spa_parlorthemename = 'Spa parlor';
		$spa_parlormenuname = $spa_parlorthemename . ' Primary Menu';
		$spa_parlormenulocation = 'spa-parlor-primary-menu';
		$spa_parlormenu_exists = wp_get_nav_menu_object($spa_parlormenuname);

		if (!$spa_parlormenu_exists) {
			$spa_parlormenu_id = wp_create_nav_menu($spa_parlormenuname);

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

			// Products
			$spa_parlorpage_about = get_page_by_path('birthday');
			if($spa_parlorpage_about){
				wp_update_nav_menu_item($spa_parlormenu_id, 0, array(
					'menu-item-title' => __('Products', 'spa-parlor'),
					'menu-item-classes' => 'birthday',
					'menu-item-url' => get_permalink($spa_parlorpage_about),
					'menu-item-status' => 'publish'
				));
			}

			// features
			$spa_parlorpages_services = get_page_by_path('features');
			if($spa_parlorpages_services){
				wp_update_nav_menu_item($spa_parlormenu_id, 0, array(
					'menu-item-title' => __('Features', 'spa-parlor'),
					'menu-item-classes' => 'features',
					'menu-item-url' => get_permalink($spa_parlorpages_services),
					'menu-item-status' => 'publish'
				));
			}

			// pricing
			$spa_parlorpages = get_page_by_path('pricing');
			if($spa_parlorpages){
				wp_update_nav_menu_item($spa_parlormenu_id, 0, array(
					'menu-item-title' => __('Pricing', 'spa-parlor'),
					'menu-item-classes' => 'pricing',
					'menu-item-url' => get_permalink($spa_parlorpages),
					'menu-item-status' => 'publish'
				));
			}

			// Support
			$spa_parlorpage_blog = get_page_by_path('support');
			if($spa_parlorpage_blog){
				wp_update_nav_menu_item($spa_parlormenu_id, 0, array(
					'menu-item-title' => __('Support', 'spa-parlor'),
					'menu-item-classes' => 'blog',
					'menu-item-url' => get_permalink($spa_parlorpage_blog),
					'menu-item-status' => 'publish'
				));
			}

			if (!has_nav_menu($spa_parlormenulocation)) {
				$spa_parlorlocations = get_theme_mod('nav_menu_locations');
				$spa_parlorlocations[$spa_parlormenulocation] = $spa_parlormenu_id;
				set_theme_mod('nav_menu_locations', $spa_parlorlocations);
			}
		}
    }
}