<?php

class Whizzie {

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

	public function init()
	{
	}

	public static function bookstore_exhibition_setup_widgets(){

	$bookstore_exhibition_product_image_gallery = array();
	$bookstore_exhibition_product_ids = array();

	$bookstore_exhibition_product_category= array(
		'Most Popular Books'       => array(
			'Product Name 01',
			'Product Name 02',
			'Product Name 03',
			'Product Name 04',
		),
	);
	
	$bookstore_exhibition_k = 1;
	foreach ( $bookstore_exhibition_product_category as $bookstore_exhibition_product_cats => $bookstore_exhibition_products_name ) { 
	// Insert porduct cats Start
	$content = 'This is sample product category';
	$bookstore_exhibition_parent_category	=	wp_insert_term(
	$bookstore_exhibition_product_cats, // the term
	'product_cat', // the taxonomy
		array(
		'description'=> $content,
		'slug' => str_replace( ' ', '-', $bookstore_exhibition_product_cats)
		)
	);

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

	$bookstore_exhibition_n=1;
	// create Product START
	foreach ( $bookstore_exhibition_products_name as $key => $bookstore_exhibition_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
	$bookstore_exhibition_my_post = array(
		'post_title'    => wp_strip_all_tags( $bookstore_exhibition_product_title ),
		'post_content'  => $content,
		'post_status'   => 'publish',
		'post_type'     => 'product',
		'post_category' => [$bookstore_exhibition_parent_category['term_id']]
	);

	// Insert the post into the database

	$bookstore_exhibition_uqpost_id = wp_insert_post($bookstore_exhibition_my_post);
	wp_set_object_terms( $bookstore_exhibition_uqpost_id, str_replace( ' ', '-', $bookstore_exhibition_product_cats), 'product_cat', true );

	$bookstore_exhibition_product_price = array('$20.46','$20.46','$20.46','$20.46');
	$bookstore_exhibition_product_regular_price = array('$20.46','$20.46','$20.46','$20.46');
	// $bookstore_exhibition_product_sale_price = array('49','49','49','49');
	
	update_post_meta( $bookstore_exhibition_uqpost_id, '_regular_price', $bookstore_exhibition_product_regular_price[$bookstore_exhibition_n-1] );
	update_post_meta( $bookstore_exhibition_uqpost_id, '_price', $bookstore_exhibition_product_price[$bookstore_exhibition_n-1] );
	// update_post_meta( $bookstore_exhibition_uqpost_id, '_sale_price', $bookstore_exhibition_product_sale_price[$bookstore_exhibition_n-1] );
	array_push( $bookstore_exhibition_product_ids,  $bookstore_exhibition_uqpost_id );

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

	// Create the attachment
	$bookstore_exhibition_attach_id = wp_insert_attachment($attachment, $bookstore_exhibition_file, $bookstore_exhibition_uqpost_id);

	// Define attachment metadata
	$attach_data = wp_generate_attachment_metadata($bookstore_exhibition_attach_id, $bookstore_exhibition_file);

	// Assign metadata to attachment
	wp_update_attachment_metadata($bookstore_exhibition_attach_id, $attach_data);
	if ( count( $bookstore_exhibition_product_image_gallery ) < 3 ) {
		array_push( $bookstore_exhibition_product_image_gallery, $bookstore_exhibition_attach_id );
	}
	// // And finally assign featured image to post
	set_post_thumbnail($bookstore_exhibition_uqpost_id, $bookstore_exhibition_attach_id);
	++$bookstore_exhibition_n;
	}
	// Create product END
	++$bookstore_exhibition_k;
	}
	// Add Gallery in first simple product and second variable product START
	$bookstore_exhibition_product_image_gallery = implode( ',', $bookstore_exhibition_product_image_gallery );
	foreach ( $bookstore_exhibition_product_ids as $bookstore_exhibition_product_id ) {
	update_post_meta( $bookstore_exhibition_product_id, 'bookstore_exhibition_product_image_gallery', $bookstore_exhibition_product_image_gallery );
	}

	/* Create Menu */
            $bookstore_exhibition_menuname  = 'Primary Menu';
            $bookstore_exhibition_location  = 'primary-menu';

            $bookstore_exhibition_menu = wp_get_nav_menu_object( $bookstore_exhibition_menuname );

            if ( ! $bookstore_exhibition_menu ) {
            $bookstore_exhibition_menu_id = wp_create_nav_menu( $bookstore_exhibition_menuname );

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

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

			// Pages Page 
			$bookstore_exhibition_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 ( $bookstore_exhibition_about_id ) {
			wp_update_nav_menu_item( $bookstore_exhibition_menu_id, 0, array(
			'menu-item-title'     => 'Pages',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $bookstore_exhibition_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Books Page 
			$bookstore_exhibition_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'  => 'Man & Women',
			'post_status' => 'publish',
			) );

			if ( $bookstore_exhibition_about_id ) {
			wp_update_nav_menu_item( $bookstore_exhibition_menu_id, 0, array(
			'menu-item-title'     => 'Man & Women',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $bookstore_exhibition_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

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

             // Create Shop Page
                $bookstore_exhibition_about_title = 'Shop';
                $bookstore_exhibition_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $bookstore_exhibition_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $bookstore_exhibition_about_title,
                        'post_content'=> $bookstore_exhibition_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $bookstore_exhibition_about_id = wp_insert_post($bookstore_exhibition_about);
                    } else {
                        $bookstore_exhibition_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($bookstore_exhibition_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'bookstore-exhibition'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $bookstore_exhibition_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
          
			/* Assign Menu Location */
			$bookstore_exhibition_locations = get_theme_mod( 'nav_menu_locations', array() );
			$bookstore_exhibition_locations[ $bookstore_exhibition_location ] = $bookstore_exhibition_menu_id;
			set_theme_mod( 'nav_menu_locations', $bookstore_exhibition_locations );
		}

        $bookstore_exhibition_options = get_theme_mod('theme_options', []);
        $bookstore_exhibition_options['discount_text'] = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ';
        $bookstore_exhibition_options['discount_link'] = '#';
		$bookstore_exhibition_options['facebook_link'] = 'www.facebook.com';
		$bookstore_exhibition_options['twitter_link'] = 'www.twitter.com';
		$bookstore_exhibition_options['instagram_link'] = 'www.instagram.com';
		$bookstore_exhibition_options['youtube_link'] = 'www.youtube.com';
        set_theme_mod('theme_options', $bookstore_exhibition_options);
    }
}
 