<?php

class Whizzie {

	public function __construct() {
		$this->init();
	}

	public function init()
	{
	
	}

	public static function optical_store_setup_widgets(){

		require_once ABSPATH . 'wp-admin/includes/image.php';
		require_once ABSPATH . 'wp-admin/includes/file.php';
		require_once ABSPATH . 'wp-admin/includes/media.php';

	set_theme_mod( 'optical_store_header_login_url', '#' );
	set_theme_mod( 'optical_store_header_quote_url', '#' );
	set_theme_mod( 'optical_store_header_quote_text', 'Get A Quote' );
	set_theme_mod(	'optical_store_social_links_settings',
	array(
		array(
				"link_text" => "fab fa-instagram",
				"link_url" => "www.instagram.com"
		),
		array(
				"link_text" => "fab fa-twitter",
				"link_url" => "www.twitter.com"
		),
		array(
				"link_text" => "fab fa-youtube",
				"link_url" => "www.youtube.com"
		),
		array(
				"link_text" => "fab fa-linkedin-in",
				"link_url" => "www.linkedin.com"
		)
	));

	$optical_store_product_image_gallery = array();
	$optical_store_product_ids = array();

	$optical_store_product_category= array(
		'Products'       => array(
			'Vision Lens',
			'Optic Spectra',
			'Clearview Glasses',
			'Eyefocus Optics',
			'Crystal Vision',
		),
	);
	$optical_store_k = 1;
	foreach ( $optical_store_product_category as $optical_store_product_cats => $optical_store_products_name ) { 
	
	$content = 'This is sample product category';
	$optical_store_parent_category	=	wp_insert_term(
	$optical_store_product_cats, 
	'product_cat', 
		array(
		'description'=> $content,
		'slug' => str_replace( ' ', '-', $optical_store_product_cats)
		)
	);

	$optical_store_n=1;
	foreach ( $optical_store_products_name as $key => $optical_store_product_title ) {
	$content = '
		<div class="main_content">
		<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s 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.</p>
		</div>';

	$optical_store_my_post = array(
		'post_title'    => wp_strip_all_tags( $optical_store_product_title ),
		'post_content'  => $content,
		'post_status'   => 'publish',
		'post_type'     => 'product',
		'post_category' => [$optical_store_parent_category['term_id']]
	);

	$optical_store_uqpost_id = wp_insert_post($optical_store_my_post);
	wp_set_object_terms( $optical_store_uqpost_id, str_replace( ' ', '-', $optical_store_product_cats), 'product_cat', true );

	$optical_store_product_price = array('100','100','100','100','100');
	
	update_post_meta( $optical_store_uqpost_id, '_price', $optical_store_product_price[$optical_store_n-1] );
	array_push( $optical_store_product_ids,  $optical_store_uqpost_id );

	$optical_store_image_url = get_template_directory_uri().'/assets/images/product/'.$optical_store_product_cats.'/' . str_replace(' ', '_', strtolower($optical_store_product_title)).'.png';
	$optical_store_image_name  = $optical_store_product_title.'.png';
	$optical_store_upload_dir = wp_upload_dir();
	
	$optical_store_image_data = file_get_contents(esc_url($optical_store_image_url));
	
	$unique_file_name = wp_unique_filename($optical_store_upload_dir['path'], $optical_store_image_name);
	
	$optical_store_filename = basename($unique_file_name);

	if (wp_mkdir_p($optical_store_upload_dir['path'])) {
	$optical_store_file = $optical_store_upload_dir['path'].'/'.$optical_store_filename;
	} else {
	$optical_store_file = $optical_store_upload_dir['basedir'].'/'.$optical_store_filename;
	}
	
	file_put_contents($optical_store_file, $optical_store_image_data);
	
	$wp_filetype = wp_check_filetype($optical_store_filename, null);
	
	$attachment = array(
	'post_mime_type' => $wp_filetype['type'],
	'post_title'     => sanitize_file_name($optical_store_filename),
	'post_type'      => 'product',
	'post_status'    => 'inherit',
	);

	$optical_store_attach_id = wp_insert_attachment($attachment, $optical_store_file, $optical_store_uqpost_id);

	$attach_data = wp_generate_attachment_metadata($optical_store_attach_id, $optical_store_file);

	wp_update_attachment_metadata($optical_store_attach_id, $attach_data);
	if ( count( $optical_store_product_image_gallery ) < 3 ) {
		array_push( $optical_store_product_image_gallery, $optical_store_attach_id );
	}

	set_post_thumbnail($optical_store_uqpost_id, $optical_store_attach_id);
	++$optical_store_n;
	}
	
	++$optical_store_k;
	}
	
	$optical_store_product_image_gallery = implode( ',', $optical_store_product_image_gallery );
	foreach ( $optical_store_product_ids as $optical_store_product_id ) {
	update_post_meta( $optical_store_product_id, 'optical_store_product_image_gallery', $optical_store_product_image_gallery );
	}

	/* Create Menu */
            $optical_store_menuname  = 'Main Menus';
            $optical_store_location  = 'main-menu';

            $optical_store_menu = wp_get_nav_menu_object( $optical_store_menuname );

            if ( ! $optical_store_menu ) {
            $optical_store_menu_id = wp_create_nav_menu( $optical_store_menuname );

           // Home Page 
			wp_update_nav_menu_item( $optical_store_menu_id, 0, array(
				'menu-item-title'     => __( 'Home', 'optical-store' ),
				'menu-item-url'       => home_url( '/' ),
				'menu-item-type'      => 'custom',
				'menu-item-status'    => 'publish',
			) );

			// About Us Page 
			$optical_store_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'About Us',
			'post_status' => 'publish',
			) );

			if ( $optical_store_about_id ) {
			wp_update_nav_menu_item( $optical_store_menu_id, 0, array(
			'menu-item-title'     => 'About Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $optical_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Testimonials Page 
			$optical_store_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Testimonials',
			'post_status' => 'publish',
			) );

			if ( $optical_store_about_id ) {
			wp_update_nav_menu_item( $optical_store_menu_id, 0, array(
			'menu-item-title'     => 'Testimonials',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $optical_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Contact Us Page 
			$optical_store_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Contact Us',
			'post_status' => 'publish',
			) );

			if ( $optical_store_about_id ) {
			wp_update_nav_menu_item( $optical_store_menu_id, 0, array(
			'menu-item-title'     => 'contact Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $optical_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

            // Create Shop Page
                $optical_store_about_title = 'Shop';
                $optical_store_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $optical_store_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $optical_store_about_title,
                        'post_content'=> $optical_store_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $optical_store_about_id = wp_insert_post($optical_store_about);
                    } else {
                        $optical_store_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($optical_store_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'optical-store'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $optical_store_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
            
			/* Assign Menu Location */
			$optical_store_locations = get_theme_mod( 'nav_menu_locations', array() );
			$optical_store_locations[ $optical_store_location ] = $optical_store_menu_id;
			set_theme_mod( 'nav_menu_locations', $optical_store_locations );
		}
    }
}
 