<?php

class Whizzie {

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

	public function init()
	{
		
	}

	public static function clothing_brand_store_setup_widgets(){


	     /* Create Menu */
            $clothing_brand_store_menuname  = 'Primary Menu';
            $clothing_brand_store_location  = 'primary';

            $clothing_brand_store_menu = wp_get_nav_menu_object( $clothing_brand_store_menuname );

            if ( ! $clothing_brand_store_menu ) {
            $clothing_brand_store_menu_id = wp_create_nav_menu( $clothing_brand_store_menuname );

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

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

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

			if ( $clothing_brand_store_about_id ) {
			wp_update_nav_menu_item( $clothing_brand_store_menu_id, 0, array(
			'menu-item-title'     => 'Pages',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $clothing_brand_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}
             // Create Shop Page
                $clothing_brand_store_about_title = 'Shop';
                $clothing_brand_store_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $clothing_brand_store_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $clothing_brand_store_about_title,
                        'post_content'=> $clothing_brand_store_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $clothing_brand_store_about_id = wp_insert_post($clothing_brand_store_about);
                    } else {
                        $clothing_brand_store_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($clothing_brand_store_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'clothing-brand-store'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $clothing_brand_store_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));

			// Contact Us Page 
			$clothing_brand_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 ( $clothing_brand_store_about_id ) {
			wp_update_nav_menu_item( $clothing_brand_store_menu_id, 0, array(
			'menu-item-title'     => 'Contact Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $clothing_brand_store_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}
            
			/* Assign Menu Location */
			$clothing_brand_store_locations = get_theme_mod( 'nav_menu_locations', array() );
			$clothing_brand_store_locations[ $clothing_brand_store_location ] = $clothing_brand_store_menu_id;
			set_theme_mod( 'nav_menu_locations', $clothing_brand_store_locations );
		}

		// select Post Box
		$clothing_brand_store_title_array = array("Style Meets Comfort", "Elevate Your Wardrobe" , "The Must-Have Outfit This Season");
		for ($clothing_brand_store_i = 1; $clothing_brand_store_i <= 3; $clothing_brand_store_i++) {
			// Create post
			$clothing_brand_store_title = $clothing_brand_store_title_array[$clothing_brand_store_i - 1];
			$clothing_brand_store_content = '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.';            
			$clothing_brand_store_my_post = array(
					'post_title'   => wp_strip_all_tags($clothing_brand_store_title),
					'post_content' => $clothing_brand_store_content,
					'post_status'  => 'publish',
					'post_type'    => 'post',
			);
			$clothing_brand_store_post_id = wp_insert_post($clothing_brand_store_my_post);
			// Get image URL
			$clothing_brand_store_image_url = get_template_directory_uri() . '/images/post/image' . $clothing_brand_store_i . '.png';
			$clothing_brand_store_image_data = wp_remote_get($clothing_brand_store_image_url);
			if (!is_wp_error($clothing_brand_store_image_data)) {
					$clothing_brand_store_image_body = wp_remote_retrieve_body($clothing_brand_store_image_data);
					$clothing_brand_store_upload_dir = wp_upload_dir();
					$clothing_brand_store_image_name = 'image' . $clothing_brand_store_i . '.png';
					$uploaded_file = wp_upload_bits($clothing_brand_store_image_name, null, $clothing_brand_store_image_body);
					if (!$uploaded_file['error']) {
						// Set attachment data
						$attachment = array(
							'post_mime_type' => $uploaded_file['type'],
							'post_title'     => sanitize_file_name($clothing_brand_store_image_name),
							'post_content'   => '',
							'post_status'    => 'inherit',
						);
						$clothing_brand_store_attach_id = wp_insert_attachment($attachment, $uploaded_file['file'], $clothing_brand_store_post_id);
						require_once(ABSPATH . 'wp-admin/includes/image.php');
						$clothing_brand_store_attach_data = wp_generate_attachment_metadata($clothing_brand_store_attach_id, $uploaded_file['file']);
						wp_update_attachment_metadata($clothing_brand_store_attach_id, $clothing_brand_store_attach_data);
						set_post_thumbnail($clothing_brand_store_post_id, $clothing_brand_store_attach_id);
					}
				}
		}

		$clothing_brand_store_product_image_gallery = array();
		$clothing_brand_store_product_ids = array();
	
		$clothing_brand_store_product_category= array(
			'Latest Sleeve Top'       => array(
				'Half Sleeve Ribbed Top',
			),
			'Latest Fit Top'       => array(
				'Basic Relax Fit Top',
			)
		);
	
		$clothing_brand_store_k = 1;
		foreach ( $clothing_brand_store_product_category as $clothing_brand_store_product_cats => $clothing_brand_store_products_name ) { 
		// Insert porduct cats Start
		$content = 'This is sample product category';
		$clothing_brand_store_parent_category	=	wp_insert_term(
		$clothing_brand_store_product_cats, // the term
		'product_cat', // the taxonomy
			array(
			'description'=> $content,
			'slug' => str_replace( ' ', '-', $clothing_brand_store_product_cats)
			)
		);
	
	// -------------- create subcategory END -----------------
	
		$clothing_brand_store_n=1;
		// create Product START
		foreach ( $clothing_brand_store_products_name as $key => $clothing_brand_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
		$clothing_brand_store_my_post = array(
			'post_title'    => wp_strip_all_tags( $clothing_brand_store_product_title ),
			'post_content'  => $content,
			'post_status'   => 'publish',
			'post_type'     => 'product',
			'post_category' => [$clothing_brand_store_parent_category['term_id']]
		);
	
		// Insert the post into the database
	
		$clothing_brand_store_uqpost_id = wp_insert_post($clothing_brand_store_my_post);
		wp_set_object_terms( $clothing_brand_store_uqpost_id, str_replace( ' ', '-', $clothing_brand_store_product_cats), 'product_cat', true );
	
		$clothing_brand_store_product_price = array('$99','$99');
		$clothing_brand_store_product_regular_price = array('$199','$199');
		$clothing_brand_store_product_sale_price = array('$99','$99');
		
		update_post_meta( $clothing_brand_store_uqpost_id, '_regular_price', $clothing_brand_store_product_regular_price[$clothing_brand_store_n-1] );
		update_post_meta( $clothing_brand_store_uqpost_id, '_price', $clothing_brand_store_product_price[$clothing_brand_store_n-1] );
		update_post_meta( $clothing_brand_store_uqpost_id, '_sale_price', $clothing_brand_store_product_sale_price[$clothing_brand_store_n-1] );
		array_push( $clothing_brand_store_product_ids,  $clothing_brand_store_uqpost_id );
	
		// Now replace meta w/ new updated value array
		$clothing_brand_store_image_url = get_template_directory_uri().'/images/product/'.$clothing_brand_store_product_cats.'/' . str_replace(' ', '_', strtolower($clothing_brand_store_product_title)).'.png';
		$clothing_brand_store_image_name  = $clothing_brand_store_product_title.'.png';
		$clothing_brand_store_upload_dir = wp_upload_dir();
		// Set upload folder
		$clothing_brand_store_image_data = file_get_contents(esc_url($clothing_brand_store_image_url));
		// Get image data
		$unique_file_name = wp_unique_filename($clothing_brand_store_upload_dir['path'], $clothing_brand_store_image_name);
		// Generate unique name
		$clothing_brand_store_filename = basename($unique_file_name);
		// Create image file name
		// Check folder permission and define file location
		if (wp_mkdir_p($clothing_brand_store_upload_dir['path'])) {
		$clothing_brand_store_file = $clothing_brand_store_upload_dir['path'].'/'.$clothing_brand_store_filename;
		} else {
		$clothing_brand_store_file = $clothing_brand_store_upload_dir['basedir'].'/'.$clothing_brand_store_filename;
		}
		
		file_put_contents($clothing_brand_store_file, $clothing_brand_store_image_data);
		// Check image file type
		$wp_filetype = wp_check_filetype($clothing_brand_store_filename, null);
		// Set attachment data
		$attachment = array(
			'post_mime_type' => $wp_filetype['type'],
			'post_title'     => sanitize_file_name($clothing_brand_store_filename),
			'post_type'      => 'product',
			'post_status'    => 'inherit',
		);
	
		// Create the attachment
		$clothing_brand_store_attach_id = wp_insert_attachment($attachment, $clothing_brand_store_file, $clothing_brand_store_uqpost_id);
	
		// Define attachment metadata
		$attach_data = wp_generate_attachment_metadata($clothing_brand_store_attach_id, $clothing_brand_store_file);
	
		// Assign metadata to attachment
		wp_update_attachment_metadata($clothing_brand_store_attach_id, $attach_data);
		if ( count( $clothing_brand_store_product_image_gallery ) < 3 ) {
			array_push( $clothing_brand_store_product_image_gallery, $clothing_brand_store_attach_id );
		}
		// // And finally assign featured image to post
		set_post_thumbnail($clothing_brand_store_uqpost_id, $clothing_brand_store_attach_id);
		++$clothing_brand_store_n;
		}
		// Create product END
		++$clothing_brand_store_k;
		}
		// Add Gallery in first simple product and second variable product START
		$clothing_brand_store_product_image_gallery = implode( ',', $clothing_brand_store_product_image_gallery );
		foreach ( $clothing_brand_store_product_ids as $clothing_brand_store_product_id ) {
		update_post_meta( $clothing_brand_store_product_id, 'clothing_brand_store_product_image_gallery', $clothing_brand_store_product_image_gallery );
		}

	}

}
 