<?php

class Whizzie {

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

	public function init()
	{
	
	}

	public static function comic_ebook_setup_widgets(){

	set_theme_mod( 'comic_ebook_header_email', 'info@example.com' );

	/* Create Menu */
            $comic_ebook_menuname  = 'Main Menus';
            $comic_ebook_location  = 'main-menu';

            $comic_ebook_menu = wp_get_nav_menu_object( $comic_ebook_menuname );

            if ( ! $comic_ebook_menu ) {
            $comic_ebook_menu_id = wp_create_nav_menu( $comic_ebook_menuname );

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

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

			// Contact Us Page 
			$comic_ebook_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',
			'post_status' => 'publish',
			) );

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

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

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

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

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

            // Create Shop Page
                $comic_ebook_about_title = 'Shop';
                $comic_ebook_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $comic_ebook_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $comic_ebook_about_title,
                        'post_content'=> $comic_ebook_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $comic_ebook_about_id = wp_insert_post($comic_ebook_about);
                    } else {
                        $comic_ebook_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($comic_ebook_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'comic-ebook'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $comic_ebook_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
            
			/* Assign Menu Location */
			$comic_ebook_locations = get_theme_mod( 'nav_menu_locations', array() );
			$comic_ebook_locations[ $comic_ebook_location ] = $comic_ebook_menu_id;
			set_theme_mod( 'nav_menu_locations', $comic_ebook_locations );
		}

	$comic_ebook_product_image_gallery = array();
	$comic_ebook_product_ids = array();

	$comic_ebook_product_category= array(
		'Product Category'       => array( 
			'Shadow Blossom',
			'Echoes of the Samurai',
			'Neon Destiny Saga',
			'Petal Storm The Silent Blade',
		),
	);
	
	$comic_ebook_k = 1;
	foreach ( $comic_ebook_product_category as $comic_ebook_product_cats => $comic_ebook_products_name ) { 
	// Insert porduct cats Start
	$content = 'This is sample product category';
	$comic_ebook_parent_category	=	wp_insert_term(
	$comic_ebook_product_cats, // the term
	'product_cat', // the taxonomy
		array(
		'description'=> $content,
		'slug' => str_replace( ' ', '-', $comic_ebook_product_cats)
		)
	);

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

	$comic_ebook_n=1;
	// create Product START
	foreach ( $comic_ebook_products_name as $key => $comic_ebook_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
	$comic_ebook_my_post = array(
		'post_title'    => wp_strip_all_tags( $comic_ebook_product_title ),
		'post_content'  => $content,
		'post_status'   => 'publish',
		'post_type'     => 'product',
		'post_category' => [$comic_ebook_parent_category['term_id']]
	);

	// Insert the post into the database

	$comic_ebook_uqpost_id = wp_insert_post($comic_ebook_my_post);
	wp_set_object_terms( $comic_ebook_uqpost_id, str_replace( ' ', '-', $comic_ebook_product_cats), 'product_cat', true );

	$comic_ebook_product_price = array('$20.46','$20.46','$20.46','$20.46');
	$comic_ebook_product_regular_price = array('$20.46','$20.46','$20.46','$20.46');
	// $comic_ebook_product_sale_price = array('49','49','49','49');
	
	update_post_meta( $comic_ebook_uqpost_id, '_regular_price', $comic_ebook_product_regular_price[$comic_ebook_n-1] );
	update_post_meta( $comic_ebook_uqpost_id, '_price', $comic_ebook_product_price[$comic_ebook_n-1] );
	// update_post_meta( $comic_ebook_uqpost_id, '_sale_price', $comic_ebook_product_sale_price[$comic_ebook_n-1] );
	array_push( $comic_ebook_product_ids,  $comic_ebook_uqpost_id );

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

	// Create the attachment
	$comic_ebook_attach_id = wp_insert_attachment($attachment, $comic_ebook_file, $comic_ebook_uqpost_id);

	// Define attachment metadata
	$attach_data = wp_generate_attachment_metadata($comic_ebook_attach_id, $comic_ebook_file);

	// Assign metadata to attachment
	wp_update_attachment_metadata($comic_ebook_attach_id, $attach_data);
	if ( count( $comic_ebook_product_image_gallery ) < 3 ) {
		array_push( $comic_ebook_product_image_gallery, $comic_ebook_attach_id );
	}
	// // And finally assign featured image to post
	set_post_thumbnail($comic_ebook_uqpost_id, $comic_ebook_attach_id);
	++$comic_ebook_n;
	}
	// Create product END
	++$comic_ebook_k;
	}
	// Add Gallery in first simple product and second variable product START
	$comic_ebook_product_image_gallery = implode( ',', $comic_ebook_product_image_gallery );
	foreach ( $comic_ebook_product_ids as $comic_ebook_product_id ) {
	update_post_meta( $comic_ebook_product_id, 'comic_ebook_product_image_gallery', $comic_ebook_product_image_gallery );
	}
}

}
 