<?php
class Whizzie {

    public function __construct() {
        add_action( 'wp_ajax_green_nursery_setup_widgets', array( $this, 'green_nursery_setup_widgets' ) );
    }


    public static function green_nursery_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);


    	$green_nurseryhome_content = '';
		$green_nurseryhome_title = 'Home';
		$green_nurseryhome = array(
				'post_type' => 'page',
				'post_title' => $green_nurseryhome_title,
				'post_content'  => $green_nurseryhome_content,
				'post_status' => 'publish',
				'post_author' => 1,
				'post_slug' => 'home'
		);
		$green_nurseryhome_id = wp_insert_post($green_nurseryhome);

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

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

		// Creation of blog page //
		$green_nurseryblog_title = 'Blog';
		$green_nurseryblog_check = get_page_by_path('blog');
		if (!$green_nurseryblog_check) {
			$green_nurseryblog = array(
				'post_type'    => 'page',
				'post_title'   => $green_nurseryblog_title,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'blog'
			);
			$green_nurseryblog_id = wp_insert_post($green_nurseryblog);

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

		// Creation of about page //
		$green_nurseryabout_title = 'About';
		$green_nurseryabout_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.';
		$green_nurseryabout_check = get_page_by_path('about');
		if (!$green_nurseryabout_check) {
			$green_nurseryabout = array(
				'post_type'    => 'page',
				'post_title'   => $green_nurseryabout_title,
				'post_content'   => $green_nurseryabout_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'about'
			);
			wp_insert_post($green_nurseryabout);
		}

		// Creation of services page //
		$green_nurseryservices_title = 'Contact';
		$green_nurseryservices_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.';
		$green_nurseryservices_check = get_page_by_path('services');
		if (!$green_nurseryservices_check) {
			$green_nurseryservices = array(
				'post_type'    => 'page',
				'post_title'   => $green_nurseryservices_title,
				'post_content'   => $green_nurseryservices_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'services'
			);
			wp_insert_post($green_nurseryservices);
		}

		// Creation of pricing page //
		$green_nurserypricing_title = 'Pricing';
		$green_nurserypricing_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.';
		$green_nurserypricing_check = get_page_by_path('pricing');
		if (!$green_nurserypricing_check) {
			$green_nurserypricing = array(
				'post_type'    => 'page',
				'post_title'   => $green_nurserypricing_title,
				'post_content'   => $green_nurserypricing_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'pricing'
			);
			wp_insert_post($green_nurserypricing);
		}

		// Creation of support page //
		$green_nurserysupport_title = 'Support';
		$green_nurserysupport_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.';
		$green_nurserysupport_check = get_page_by_path('support');
		if (!$green_nurserysupport_check) {
			$green_nurserysupport = array(
				'post_type'    => 'page',
				'post_title'   => $green_nurserysupport_title,
				'post_content'   => $green_nurserysupport_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'support'
			);
			wp_insert_post($green_nurserysupport);
		}

		// Creation of features page //
		$green_nurseryfeatures_title = 'Features';
		$green_nurseryfeatures_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.';
		$green_nurseryfeatures_check = get_page_by_path('features');
		if (!$green_nurseryfeatures_check) {
			$green_nurseryfeatures = array(
				'post_type'    => 'page',
				'post_title'   => $green_nurseryfeatures_title,
				'post_content'   => $green_nurseryfeatures_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'features'
			);
			wp_insert_post($green_nurseryfeatures);
		}

		// Creation of services page //
		$green_nurserybirthday_title = 'Products';
		$green_nurserybirthday_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.';
		$green_nurserybirthday_check = get_page_by_path('birthday');
		if (!$green_nurserybirthday_check) {
			$green_nurserybirthday = array(
				'post_type'    => 'page',
				'post_title'   => $green_nurserybirthday_title,
				'post_content'   => $green_nurserybirthday_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'birthday'
			);
			wp_insert_post($green_nurserybirthday);
		}

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


        // Creation of Product //
		$green_nursery_product_image_gallery = array();
		$green_nursery_product_ids = array();

		$green_nursery_product_category= array(
			'All' => array(
				'Emerald Whisper',
				'Urban Fern',
				'Urban Ferm',
				'Emerald Whispers',
			),
		);

		$created_products = array(); 

		foreach ($green_nursery_product_category as $green_nursery_product_cats => $green_nursery_products_name) {

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

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

			foreach ($green_nursery_products_name as $green_nursery_product_title) {

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

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

				$created_products[$green_nursery_product_title] = $post_id;

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

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

				$green_nursery_product_ids[] = $post_id;


				$image_url = get_template_directory_uri().'/assets/images/product/'.strtolower($green_nursery_product_cats).'/' . str_replace(' ', '_', strtolower($green_nursery_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(' ', '-', $green_nursery_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);
				}
			}
		}

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

		foreach ($green_nursery_product_ids as $green_nursery_product_id) {
			update_post_meta($green_nursery_product_id, 'green_nursery_product_image_gallery', $green_nursery_product_image_gallery);
		}

        /* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- Green Nursery Primary Menu -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*/

		$green_nurserythemename = 'Green Nursery';
		$green_nurserymenuname = $green_nurserythemename . ' Primary Menu';
		$green_nurserymenulocation = 'green-nursery-primary-menu';
		$green_nurserymenu_exists = wp_get_nav_menu_object($green_nurserymenuname);

		if (!$green_nurserymenu_exists) {
			$green_nurserymenu_id = wp_create_nav_menu($green_nurserymenuname);

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

			// Products
			$green_nurserypage_about = get_page_by_path('birthday');
			if($green_nurserypage_about){
				wp_update_nav_menu_item($green_nurserymenu_id, 0, array(
					'menu-item-title' => __('Shop Plants', 'green-nursery'),
					'menu-item-classes' => 'c',
					'menu-item-url' => get_permalink($green_nurserypage_about),
					'menu-item-status' => 'publish'
				));
			}

			// features
			$green_nurserypages_services = get_page_by_path('About');
			if($green_nurserypages_services){
				wp_update_nav_menu_item($green_nurserymenu_id, 0, array(
					'menu-item-title' => __('About', 'green-nursery'),
					'menu-item-classes' => 'About',
					'menu-item-url' => get_permalink($green_nurserypages_services),
					'menu-item-status' => 'publish'
				));
			}

			// pricing
			$green_nurserypages = get_page_by_path('Contact');
			if($green_nurserypages){
				wp_update_nav_menu_item($green_nurserymenu_id, 0, array(
					'menu-item-title' => __('Contact', 'green-nursery'),
					'menu-item-classes' => 'Contact',
					'menu-item-url' => get_permalink($green_nurserypages_contact),
					'menu-item-status' => 'publish'
				));
			}

			// Support
			$green_nurserypage_blog = get_page_by_path('blog');
			if($green_nurserypage_blog){
				wp_update_nav_menu_item($green_nurserymenu_id, 0, array(
					'menu-item-title' => __('blog', 'green-nursery'),
					'menu-item-classes' => 'blog',
					'menu-item-url' => get_permalink($green_nurserypage_blog),
					'menu-item-status' => 'publish'
				));
			}

			if (!has_nav_menu($green_nurserymenulocation)) {
				$green_nurserylocations = get_theme_mod('nav_menu_locations');
				$green_nurserylocations[$green_nurserymenulocation] = $green_nurserymenu_id;
				set_theme_mod('nav_menu_locations', $green_nurserylocations);
			}
		}
    }
}