<?php

class Whizzie {

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

	public function init()
	{
		
	}

	public static function medical_store_setup_widgets(){

	set_theme_mod( 'medical_store_header_button_url', '#' );
	set_theme_mod( 'medical_store_phone_number', '1234567890' );
	set_theme_mod( 'medical_store_phone_number_text', 'Call US' );

	$medical_store_product_image_gallery = array();
	$medical_store_product_ids = array();

	$medical_store_product_category= array(
		'Product Category'       => array( 
			'Doctors Choice Face Mask',
			'Pharmo Pharmacy Digital Thermometer',
			'baconil TTS20 14mg',
			'a Neem Tulsi Face Wash',
		),
	);
	$medical_store_k = 1;
	foreach ( $medical_store_product_category as $medical_store_product_cats => $medical_store_products_name ) { 
	// Insert porduct cats Start
	$content = 'This is sample product category';
	$medical_store_parent_category	=	wp_insert_term(
	$medical_store_product_cats, // the term
	'product_cat', // the taxonomy
		array(
		'description'=> $content,
		'slug' => str_replace( ' ', '-', $medical_store_product_cats)
		)
	);

// -------------- create subcategory END -----------------

	$medical_store_n=1;
	// create Product START
	foreach ( $medical_store_products_name as $key => $medical_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>';

	// Create post object
	$medical_store_my_post = array(
		'post_title'    => wp_strip_all_tags( $medical_store_product_title ),
		'post_content'  => $content,
		'post_status'   => 'publish',
		'post_type'     => 'product',
		'post_category' => [$medical_store_parent_category['term_id']]
	);

	// Insert the post into the database

	$medical_store_uqpost_id = wp_insert_post($medical_store_my_post);
	wp_set_object_terms( $medical_store_uqpost_id, str_replace( ' ', '-', $medical_store_product_cats), 'product_cat', true );

	$medical_store_product_price = array('$20.46','$20.46','$20.46','$20.46');
	$medical_store_product_regular_price = array('$20.46','$20.46','$20.46','$20.46');
	// $medical_store_product_sale_price = array('49','49','49','49');
	
	update_post_meta( $medical_store_uqpost_id, '_regular_price', $medical_store_product_regular_price[$medical_store_n-1] );
	update_post_meta( $medical_store_uqpost_id, '_price', $medical_store_product_price[$medical_store_n-1] );
	// update_post_meta( $medical_store_uqpost_id, '_sale_price', $medical_store_product_sale_price[$medical_store_n-1] );
	array_push( $medical_store_product_ids,  $medical_store_uqpost_id );

	// Now replace meta w/ new updated value array
	$medical_store_image_url = get_template_directory_uri().'/assets/images/product/'.$medical_store_product_cats.'/' . str_replace(' ', '_', strtolower($medical_store_product_title)).'.png';
	$medical_store_image_name  = $medical_store_product_title.'.png';
	$medical_store_upload_dir = wp_upload_dir();
	// Set upload folder
	$medical_store_image_data = file_get_contents(esc_url($medical_store_image_url));
	// Get image data
	$unique_file_name = wp_unique_filename($medical_store_upload_dir['path'], $medical_store_image_name);
	// Generate unique name
	$medical_store_filename = basename($unique_file_name);
	// Create image file name
	// Check folder permission and define file location
	if (wp_mkdir_p($medical_store_upload_dir['path'])) {
	$medical_store_file = $medical_store_upload_dir['path'].'/'.$medical_store_filename;
	} else {
	$medical_store_file = $medical_store_upload_dir['basedir'].'/'.$medical_store_filename;
	}
	
	file_put_contents($medical_store_file, $medical_store_image_data);
	// Check image file type
	$wp_filetype = wp_check_filetype($medical_store_filename, null);
	// Set attachment data
	$attachment = array(
		'post_mime_type' => $wp_filetype['type'],
		'post_title'     => sanitize_file_name($medical_store_filename),
		'post_type'      => 'product',
		'post_status'    => 'inherit',
	);

	// Create the attachment
	$medical_store_attach_id = wp_insert_attachment($attachment, $medical_store_file, $medical_store_uqpost_id);

	// Define attachment metadata
	$attach_data = wp_generate_attachment_metadata($medical_store_attach_id, $medical_store_file);

	// Assign metadata to attachment
	wp_update_attachment_metadata($medical_store_attach_id, $attach_data);
	if ( count( $medical_store_product_image_gallery ) < 3 ) {
		array_push( $medical_store_product_image_gallery, $medical_store_attach_id );
	}
	// // And finally assign featured image to post
	set_post_thumbnail($medical_store_uqpost_id, $medical_store_attach_id);
	++$medical_store_n;
	}
	// Create product END
	++$medical_store_k;
	}
	// Add Gallery in first simple product and second variable product START
	$medical_store_product_image_gallery = implode( ',', $medical_store_product_image_gallery );
	foreach ( $medical_store_product_ids as $medical_store_product_id ) {
	update_post_meta( $medical_store_product_id, 'medical_store_product_image_gallery', $medical_store_product_image_gallery );
	}

	/* Create Menu */
            $medical_store_menuname  = 'Main Menus';
            $medical_store_location  = 'main-menu';

            $medical_store_menu = wp_get_nav_menu_object( $medical_store_menuname );

            if ( ! $medical_store_menu ) {
            $medical_store_menu_id = wp_create_nav_menu( $medical_store_menuname );

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

			// Pharma Products Page 
			$medical_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'  => 'Pharma Products',
			'post_status' => 'publish',
			) );

			if ( $medical_store_about_id ) {
			wp_update_nav_menu_item( $medical_store_menu_id, 0, array(
			'menu-item-title'     => 'Pharma Products',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $medical_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Baby Care Page 
			$medical_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'  => 'Baby Care',
			'post_status' => 'publish',
			) );

			if ( $medical_store_about_id ) {
			wp_update_nav_menu_item( $medical_store_menu_id, 0, array(
			'menu-item-title'     => 'Baby Care',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $medical_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Personal Care Page 
			$medical_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'  => 'Personal Care',
			'post_status' => 'publish',
			) );

			if ( $medical_store_about_id ) {
			wp_update_nav_menu_item( $medical_store_menu_id, 0, array(
			'menu-item-title'     => 'Personal Care',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $medical_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Health Device Page 
			$medical_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'  => 'Health Device',
			'post_status' => 'publish',
			) );

			if ( $medical_store_about_id ) {
			wp_update_nav_menu_item( $medical_store_menu_id, 0, array(
			'menu-item-title'     => 'Health Device',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $medical_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Ayurveda Page 
			$medical_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'  => 'Ayurveda',
			'post_status' => 'publish',
			) );

			if ( $medical_store_about_id ) {
			wp_update_nav_menu_item( $medical_store_menu_id, 0, array(
			'menu-item-title'     => 'Ayurveda',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $medical_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}
     
            // Create Shop Page
                $medical_store_about_title = 'Shop';
                $medical_store_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $medical_store_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $medical_store_about_title,
                        'post_content'=> $medical_store_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $medical_store_about_id = wp_insert_post($medical_store_about);
                    } else {
                        $medical_store_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($medical_store_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'medical-store'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $medical_store_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));

			/* Assign Menu Location */
			$medical_store_locations = get_theme_mod( 'nav_menu_locations', array() );
			$medical_store_locations[ $medical_store_location ] = $medical_store_menu_id;
			set_theme_mod( 'nav_menu_locations', $medical_store_locations );
		}
}

}
 