<?php

class Whizzie {

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

	public function init()
	{
	
	}

	public static function hardware_dealer_setup_widgets(){

	set_theme_mod( 'hardware_dealer_header_phone_number', '1800 1200 110' );
	set_theme_mod( 'hardware_dealer_header_advertisement_text', 'Summer-Season Sale UP To 25% OFF Use Coupon Code "Hardware1123"' );
	set_theme_mod( 'hardware_dealer_location_text', 'Location1245new York' );
	set_theme_mod( 'hardware_dealer_location_link', '#' );
	set_theme_mod( 'hardware_dealer_header_button_text', 'Book An Handyman' );
	set_theme_mod( 'hardware_dealer_header_button_url', '#' );
	set_theme_mod( 'hardware_dealer_phone_number_text', 'Call Us An Time' );
	set_theme_mod( 'hardware_dealer_phone_number', '+123456789' );

	$hardware_dealer_product_image_gallery = array();
	$hardware_dealer_product_ids = array();

	$hardware_dealer_product_category= array(
		'Screwdriver'       => array(
			'Open Wrench',
			'Ractcheting Wrench',
			'Lug Wrench',
			'Pipe Wrench'
		),
		'Wrench'       => array(
			'Pipe Wrench',
			'Open Wrench',
			'Ractcheting Wrench',
			'Lug Wrench',
		),
		'Hammers'       => array(
			'Lug Wrench',
			'Pipe Wrench',
			'Open Wrench',
			'Ractcheting Wrench',
		),
		'Power Tools'       => array(
			'Ractcheting Wrench',
			'Lug Wrench',
			'Pipe Wrench',
			'Open Wrench',
		)
	);
	$hardware_dealer_k = 1;
	foreach ( $hardware_dealer_product_category as $hardware_dealer_product_cats => $hardware_dealer_products_name ) { 
	// Insert porduct cats Start
	$content = 'This is sample product category';
	$hardware_dealer_parent_category	=	wp_insert_term(
	$hardware_dealer_product_cats, // the term
	'product_cat', // the taxonomy
		array(
		'description'=> $content,
		'slug' => str_replace( ' ', '-', $hardware_dealer_product_cats)
		)
	);

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

	$hardware_dealer_n=1;
	// create Product START
	foreach ( $hardware_dealer_products_name as $key => $hardware_dealer_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
	$hardware_dealer_my_post = array(
		'post_title'    => wp_strip_all_tags( $hardware_dealer_product_title ),
		'post_content'  => $content,
		'post_status'   => 'publish',
		'post_type'     => 'product',
		'post_category' => [$hardware_dealer_parent_category['term_id']]
	);

	// Insert the post into the database

	$hardware_dealer_uqpost_id = wp_insert_post($hardware_dealer_my_post);
	wp_set_object_terms( $hardware_dealer_uqpost_id, str_replace( ' ', '-', $hardware_dealer_product_cats), 'product_cat', true );

	$hardware_dealer_product_price = array('$5.00','$5.00','$5.00' ,'$5.00');
	$hardware_dealer_product_regular_price = array('$5.00','$5.00','$5.00');
	
	update_post_meta( $hardware_dealer_uqpost_id, '_regular_price', $hardware_dealer_product_regular_price[$hardware_dealer_n-1] );
	update_post_meta( $hardware_dealer_uqpost_id, '_price', $hardware_dealer_product_price[$hardware_dealer_n-1] );
	array_push( $hardware_dealer_product_ids,  $hardware_dealer_uqpost_id );

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

	// Create the attachment
	$hardware_dealer_attach_id = wp_insert_attachment($attachment, $hardware_dealer_file, $hardware_dealer_uqpost_id);

	// Define attachment metadata
	$attach_data = wp_generate_attachment_metadata($hardware_dealer_attach_id, $hardware_dealer_file);

	// Assign metadata to attachment
	wp_update_attachment_metadata($hardware_dealer_attach_id, $attach_data);
	if ( count( $hardware_dealer_product_image_gallery ) < 3 ) {
		array_push( $hardware_dealer_product_image_gallery, $hardware_dealer_attach_id );
	}
	// // And finally assign featured image to post
	set_post_thumbnail($hardware_dealer_uqpost_id, $hardware_dealer_attach_id);
	++$hardware_dealer_n;
	}
	// Create product END
	++$hardware_dealer_k;
	}
	// Add Gallery in first simple product and second variable product START
	$hardware_dealer_product_image_gallery = implode( ',', $hardware_dealer_product_image_gallery );
	foreach ( $hardware_dealer_product_ids as $hardware_dealer_product_id ) {
	update_post_meta( $hardware_dealer_product_id, 'hardware_dealer_product_image_gallery', $hardware_dealer_product_image_gallery );
	}

	/* Create Menu */
            $hardware_dealer_menuname  = 'Main Menus';
            $hardware_dealer_location  = 'main-menu';

            $hardware_dealer_menu = wp_get_nav_menu_object( $hardware_dealer_menuname );

            if ( ! $hardware_dealer_menu ) {
            $hardware_dealer_menu_id = wp_create_nav_menu( $hardware_dealer_menuname );

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

			// About Us Page 
			$hardware_dealer_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 ( $hardware_dealer_about_id ) {
			wp_update_nav_menu_item( $hardware_dealer_menu_id, 0, array(
			'menu-item-title'     => 'About Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $hardware_dealer_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

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

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

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

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

			// Contact Us Page 
			$hardware_dealer_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 ( $hardware_dealer_about_id ) {
			wp_update_nav_menu_item( $hardware_dealer_menu_id, 0, array(
			'menu-item-title'     => 'Contact Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $hardware_dealer_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

          // Create Shop Page
                $hardware_dealer_about_title = 'Shop';
                $hardware_dealer_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $hardware_dealer_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $hardware_dealer_about_title,
                        'post_content'=> $hardware_dealer_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $hardware_dealer_about_id = wp_insert_post($hardware_dealer_about);
                    } else {
                        $hardware_dealer_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($hardware_dealer_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'hardware-dealer'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $hardware_dealer_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
         
			/* Assign Menu Location */
			$hardware_dealer_locations = get_theme_mod( 'nav_menu_locations', array() );
			$hardware_dealer_locations[ $hardware_dealer_location ] = $hardware_dealer_menu_id;
			set_theme_mod( 'nav_menu_locations', $hardware_dealer_locations );
		}
    }
}
 