<?php

class Whizzie {

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

	public function init()
	{
	
	}

	public static function printing_press_setup_widgets(){


	$printing_press_product_image_gallery = array();
	$printing_press_product_ids = array();

	$printing_press_product_category= array(
		'Product Category'       => array(
			'Packaging Boxes',
			'Business Cards',
			'Custom Paper Bags',
			'Book Covers',
		),
	);

	$printing_press_k = 1;
	foreach ( $printing_press_product_category as $printing_press_product_cats => $printing_press_products_name ) { 
	// Insert porduct cats Start
	$content = 'This is sample product category';
	$printing_press_parent_category	=	wp_insert_term(
	$printing_press_product_cats, // the term
	'product_cat', // the taxonomy
		array(
		'description'=> $content,
		'slug' => str_replace( ' ', '-', $printing_press_product_cats)
		)
	);

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

	$printing_press_n=1;
	// create Product START
	foreach ( $printing_press_products_name as $key => $printing_press_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
	$printing_press_my_post = array(
		'post_title'    => wp_strip_all_tags( $printing_press_product_title ),
		'post_content'  => $content,
		'post_status'   => 'publish',
		'post_type'     => 'product',
		'post_category' => [$printing_press_parent_category['term_id']]
	);

	// Insert the post into the database

	$printing_press_uqpost_id = wp_insert_post($printing_press_my_post);
	wp_set_object_terms( $printing_press_uqpost_id, str_replace( ' ', '-', $printing_press_product_cats), 'product_cat', true );

	$printing_press_product_price = array('32.36','32.36','32.36','32.36');
	
	update_post_meta( $printing_press_uqpost_id, '_regular_price', $printing_press_product_price[$printing_press_n-1] );
	update_post_meta( $printing_press_uqpost_id, '_price', $printing_press_product_price[$printing_press_n-1] );
	array_push( $printing_press_product_ids,  $printing_press_uqpost_id );

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

	// Create the attachment
	$printing_press_attach_id = wp_insert_attachment($attachment, $printing_press_file, $printing_press_uqpost_id);

	// Define attachment metadata
	$attach_data = wp_generate_attachment_metadata($printing_press_attach_id, $printing_press_file);

	// Assign metadata to attachment
	wp_update_attachment_metadata($printing_press_attach_id, $attach_data);
	if ( count( $printing_press_product_image_gallery ) < 3 ) {
		array_push( $printing_press_product_image_gallery, $printing_press_attach_id );
	}
	// // And finally assign featured image to post
	set_post_thumbnail($printing_press_uqpost_id, $printing_press_attach_id);
	++$printing_press_n;
	}
	// Create product END
	++$printing_press_k;
	}
	// Add Gallery in first simple product and second variable product START
	$printing_press_product_image_gallery = implode( ',', $printing_press_product_image_gallery );
	foreach ( $printing_press_product_ids as $printing_press_product_id ) {
	update_post_meta( $printing_press_product_id, 'printing_press_product_image_gallery', $printing_press_product_image_gallery );
	}

	/* Create Menu */
            $printing_press_menuname  = 'Main Menus';
            $printing_press_location  = 'main-menu';

            $printing_press_menu = wp_get_nav_menu_object( $printing_press_menuname );

            if ( ! $printing_press_menu ) {
            $printing_press_menu_id = wp_create_nav_menu( $printing_press_menuname );

           // Home Page 
			wp_update_nav_menu_item( $printing_press_menu_id, 0, array(
				'menu-item-title'     => __( 'Home', 'printing-press' ),
				'menu-item-url'       => home_url( '/' ),
				'menu-item-type'      => 'custom',
				'menu-item-status'    => 'publish',
				'menu-item-position'  => 1,
			) );

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

			// Services Page 
			$printing_press_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 ( $printing_press_about_id ) {
			wp_update_nav_menu_item( $printing_press_menu_id, 0, array(
			'menu-item-title'     => 'Services',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $printing_press_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
            'menu-item-position'  => 3,
			) );
			}

			// Portfolio Page 
			$printing_press_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 ( $printing_press_about_id ) {
			wp_update_nav_menu_item( $printing_press_menu_id, 0, array(
			'menu-item-title'     => 'Portfolio',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $printing_press_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
            'menu-item-position'  => 4,
			) );
			}

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

		   // Create Shop Page
                $printing_press_about_title = 'Shop';
                $printing_press_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $printing_press_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $printing_press_about_title,
                        'post_content'=> $printing_press_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $printing_press_about_id = wp_insert_post($printing_press_about);
                    } else {
                        $printing_press_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($printing_press_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'printing-press'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $printing_press_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
            
			/* ---------- Assign Menu Location ---------- */
			$printing_press_locations = get_theme_mod( 'nav_menu_locations', array() );
			$printing_press_locations[ $printing_press_location ] = $printing_press_menu_id;
			set_theme_mod( 'nav_menu_locations', $printing_press_locations );
		}
    }
}
 