<?php

class Whizzie {

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

	public function init()
	{
	
	}

	public static function tattoo_studio_setup_widgets(){

	$tattoo_studio_product_image_gallery = array();
	$tattoo_studio_product_ids = array();

	$tattoo_studio_product_category= array(
		'Product Category'       => array(
			'SIMPLE SHAPES',
			'SHOW YOUR PERSONALITY',
			'FULL SLEEVE TATTOOS',
			'POWERFUL SOLUTIONS',
		),
	);

	$tattoo_studio_k = 1;
	foreach ( $tattoo_studio_product_category as $tattoo_studio_product_cats => $tattoo_studio_products_name ) { 
	// Insert porduct cats Start
	$content = 'This is sample product category';
	$tattoo_studio_parent_category	=	wp_insert_term(
	$tattoo_studio_product_cats, // the term
	'product_cat', // the taxonomy
		array(
		'description'=> $content,
		'slug' => str_replace( ' ', '-', $tattoo_studio_product_cats)
		)
	);

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

	$tattoo_studio_n=1;
	// create Product START
	foreach ( $tattoo_studio_products_name as $key => $tattoo_studio_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
	$tattoo_studio_my_post = array(
		'post_title'    => wp_strip_all_tags( $tattoo_studio_product_title ),
		'post_content'  => $content,
		'post_status'   => 'publish',
		'post_type'     => 'product',
		'post_category' => [$tattoo_studio_parent_category['term_id']]
	);

	// Insert the post into the database

	$tattoo_studio_uqpost_id = wp_insert_post($tattoo_studio_my_post);
	wp_set_object_terms( $tattoo_studio_uqpost_id, str_replace( ' ', '-', $tattoo_studio_product_cats), 'product_cat', true );

	$tattoo_studio_product_price = array('32.36','32.36','32.36','32.36');
	$tattoo_studio_product_regular_price = array('32.36','32.36','32.36','32.36');
	
	update_post_meta( $tattoo_studio_uqpost_id, '_regular_price', $tattoo_studio_product_regular_price[$tattoo_studio_n-1] );
	update_post_meta( $tattoo_studio_uqpost_id, '_price', $tattoo_studio_product_price[$tattoo_studio_n-1] );
	array_push( $tattoo_studio_product_ids,  $tattoo_studio_uqpost_id );

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

	// Create the attachment
	$tattoo_studio_attach_id = wp_insert_attachment($attachment, $tattoo_studio_file, $tattoo_studio_uqpost_id);

	// Define attachment metadata
	$attach_data = wp_generate_attachment_metadata($tattoo_studio_attach_id, $tattoo_studio_file);

	// Assign metadata to attachment
	wp_update_attachment_metadata($tattoo_studio_attach_id, $attach_data);
	if ( count( $tattoo_studio_product_image_gallery ) < 3 ) {
		array_push( $tattoo_studio_product_image_gallery, $tattoo_studio_attach_id );
	}
	// // And finally assign featured image to post
	set_post_thumbnail($tattoo_studio_uqpost_id, $tattoo_studio_attach_id);
	++$tattoo_studio_n;
	}
	// Create product END
	++$tattoo_studio_k;
	}
	// Add Gallery in first simple product and second variable product START
	$tattoo_studio_product_image_gallery = implode( ',', $tattoo_studio_product_image_gallery );
	foreach ( $tattoo_studio_product_ids as $tattoo_studio_product_id ) {
	update_post_meta( $tattoo_studio_product_id, 'tattoo_studio_product_image_gallery', $tattoo_studio_product_image_gallery );

	}

	/* Create Menu */
            $tattoo_studio_menuname  = 'Main Menus';
            $tattoo_studio_location  = 'main-menu';

            $tattoo_studio_menu = wp_get_nav_menu_object( $tattoo_studio_menuname );

            if ( ! $tattoo_studio_menu ) {
            $tattoo_studio_menu_id = wp_create_nav_menu( $tattoo_studio_menuname );

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

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

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

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

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

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

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

            // Create Shop Page
                $tattoo_studio_about_title = 'Shop';
                $tattoo_studio_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $tattoo_studio_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $tattoo_studio_about_title,
                        'post_content'=> $tattoo_studio_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $tattoo_studio_about_id = wp_insert_post($tattoo_studio_about);
                    } else {
                        $tattoo_studio_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($tattoo_studio_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'tattoo-studio'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $tattoo_studio_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));

			/* Assign Menu Location */
			$tattoo_studio_locations = get_theme_mod( 'nav_menu_locations', array() );
			$tattoo_studio_locations[ $tattoo_studio_location ] = $tattoo_studio_menu_id;
			set_theme_mod( 'nav_menu_locations', $tattoo_studio_locations );
		}
    }
}
 