<?php
class Whizzie {

    public function __construct() {
        add_action( 'wp_ajax_local_supermarket_setup_widgets', array( $this, 'local_supermarket_setup_widgets' ) );
    }

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

    	$local_supermarkethome_content = '';
		$local_supermarkethome_title = 'Home';
		$local_supermarkethome = array(
				'post_type' => 'page',
				'post_title' => $local_supermarkethome_title,
				'post_content'  => $local_supermarkethome_content,
				'post_status' => 'publish',
				'post_author' => 1,
				'post_slug' => 'home'
		);
		$local_supermarkethome_id = wp_insert_post($local_supermarkethome);

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

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

		// Creation of blog page //
		$local_supermarketblog_title = 'Blog';
		$local_supermarketblog_check = get_page_by_path('blog');
		if (!$local_supermarketblog_check) {
			$local_supermarketblog = array(
				'post_type'    => 'page',
				'post_title'   => $local_supermarketblog_title,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'blog'
			);
			$local_supermarketblog_id = wp_insert_post($local_supermarketblog);

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

		// Creation of about page //
		$local_supermarketabout_title = 'About';
		$local_supermarketabout_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.';
		$local_supermarketabout_check = get_page_by_path('about');
		if (!$local_supermarketabout_check) {
			$local_supermarketabout = array(
				'post_type'    => 'page',
				'post_title'   => $local_supermarketabout_title,
				'post_content'   => $local_supermarketabout_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'about'
			);
			wp_insert_post($local_supermarketabout);
		}

		// Creation of services page //
		$local_supermarketservices_title = 'Contact';
		$local_supermarketservices_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.';
		$local_supermarketservices_check = get_page_by_path('services');
		if (!$local_supermarketservices_check) {
			$local_supermarketservices = array(
				'post_type'    => 'page',
				'post_title'   => $local_supermarketservices_title,
				'post_content'   => $local_supermarketservices_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'services'
			);
			wp_insert_post($local_supermarketservices);
		}

		// Creation of pricing page //
		$local_supermarketpricing_title = 'Pricing';
		$local_supermarketpricing_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.';
		$local_supermarketpricing_check = get_page_by_path('pricing');
		if (!$local_supermarketpricing_check) {
			$local_supermarketpricing = array(
				'post_type'    => 'page',
				'post_title'   => $local_supermarketpricing_title,
				'post_content'   => $local_supermarketpricing_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'pricing'
			);
			wp_insert_post($local_supermarketpricing);
		}

		// Creation of support page //
		$local_supermarketsupport_title = 'Support';
		$local_supermarketsupport_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.';
		$local_supermarketsupport_check = get_page_by_path('support');
		if (!$local_supermarketsupport_check) {
			$local_supermarketsupport = array(
				'post_type'    => 'page',
				'post_title'   => $local_supermarketsupport_title,
				'post_content'   => $local_supermarketsupport_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'support'
			);
			wp_insert_post($local_supermarketsupport);
		}

		// Creation of features page //
		$local_supermarketgallery_title = 'Gallery';
		$local_supermarketgallery_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.';
		$local_supermarketgallery_check = get_page_by_path('gallery');
		if (!$local_supermarketgallery_check) {
			$local_supermarketgallery = array(
				'post_type'    => 'page',
				'post_title'   => $local_supermarketgallery_title,
				'post_content'   => $local_supermarketgallery_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'gallery'
			);
			wp_insert_post($local_supermarket_gallery);
		}

		// Creation of services page //
		$local_supermarketbirthday_title = 'Products';
		$local_supermarketbirthday_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.';
		$local_supermarketbirthday_check = get_page_by_path('birthday');
		if (!$local_supermarketbirthday_check) {
			$local_supermarketbirthday = array(
				'post_type'    => 'page',
				'post_title'   => $local_supermarketbirthday_title,
				'post_content'   => $local_supermarketbirthday_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'birthday'
			);
			wp_insert_post($local_supermarketbirthday);
		}

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


        // Creation of Product //
		$local_supermarket_product_image_gallery = array();
		$local_supermarket_product_ids = array();

		$local_supermarket_product_category= array(
			'All' => array(
				'Original Butter',
				'Rice Bag',
				'Fresh Apples',
			),
			'Groceries' => array(
				'Fresh Apples',
				'Cheddar Cheese',
				'Rice Bag',
			),
			'Fruits and Vegetables' => array(
				'Cheddar Cheese',
				'Fresh Apples',
				'Original Butter',
			),
			'Dairy and Bakery' => array(
				'Original Butter',
				'Fresh Apples',
				'Rice Bag',
			),
			'Snacks' => array(
				'Rice Bag',
				'Cheddar Cheese',
				'Fresh Apples',
			),
		);

		$created_products = array(); 

		foreach ($local_supermarket_product_category as $local_supermarket_product_cats => $local_supermarket_products_name) {

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

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

			foreach ($local_supermarket_products_name as $local_supermarket_product_title) {

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

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

				$created_products[$local_supermarket_product_title] = $post_id;

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

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

				$local_supermarket_product_ids[] = $post_id;


				$category_folder = sanitize_title($local_supermarket_product_cats);

				$image_url = get_template_directory_uri() . '/assets/images/product/' . $category_folder . '/' . sanitize_title($local_supermarket_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(' ', '-', $local_supermarket_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);
				}
			}
		}

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

		foreach ($local_supermarket_product_ids as $local_supermarket_product_id) {
			update_post_meta($local_supermarket_product_id, 'local_supermarket_product_image_gallery', $local_supermarket_product_image_gallery);
		}

        /* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- Local Supermarket Primary Menu -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*/

		$local_supermarketthemename = 'Local Supermarket';
		$local_supermarketmenuname = $local_supermarketthemename . ' Primary Menu';
		$local_supermarketmenulocation = 'local-supermarket-primary-menu';
		$local_supermarketmenu_exists = wp_get_nav_menu_object($local_supermarketmenuname);

		if (!$local_supermarketmenu_exists) {
			$local_supermarketmenu_id = wp_create_nav_menu($local_supermarketmenuname);

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

			// About
			$local_supermarketpage_about = get_page_by_path('about');
			if($local_supermarketpage_about){
				wp_update_nav_menu_item($local_supermarketmenu_id, 0, array(
					'menu-item-title' => __('About', 'local-supermarket'),
					'menu-item-classes' => 'birthday',
					'menu-item-url' => get_permalink($local_supermarketpage_about),
					'menu-item-status' => 'publish'
				));
			}

			// features
			$local_supermarketpages_services = get_page_by_path('services');
			if($local_supermarketpages_services){
				wp_update_nav_menu_item($local_supermarketmenu_id, 0, array(
					'menu-item-title' => __('Services', 'local-supermarket'),
					'menu-item-classes' => 'features',
					'menu-item-url' => get_permalink($local_supermarketpages_services),
					'menu-item-status' => 'publish'
				));
			}

			// pricing
			$local_supermarketpages = get_page_by_path('gallery');
			if($local_supermarketpages_gallery){
				wp_update_nav_menu_item($local_supermarketmenu_id, 0, array(
					'menu-item-title' => __('Gallery', 'local-supermarket'),
					'menu-item-classes' => 'pricing',
					'menu-item-url' => get_permalink($local_supermarketpages_gallery),
					'menu-item-status' => 'publish'
				));
			}

			// Support
			$local_supermarketpage_blog = get_page_by_path('blog');
			if($local_supermarketpage_blog){
				wp_update_nav_menu_item($local_supermarketmenu_id, 0, array(
					'menu-item-title' => __('Blog', 'local-supermarket'),
					'menu-item-classes' => 'blog',
					'menu-item-url' => get_permalink($local_supermarketpage_blog),
					'menu-item-status' => 'publish'
				));
			}

			if (!has_nav_menu($local_supermarketmenulocation)) {
				$local_supermarketlocations = get_theme_mod('nav_menu_locations');
				$local_supermarketlocations[$local_supermarketmenulocation] = $local_supermarketmenu_id;
				set_theme_mod('nav_menu_locations', $local_supermarketlocations);
			}
		}
    }
}