<?php
class Whizzie {

    public function __construct() {
        add_action( 'wp_ajax_air_ticket_booking_setup_widgets', array( $this, 'air_ticket_booking_setup_widgets' ) );
    }


    public static function air_ticket_booking_setup_widgets() {

    	$shoplentor_options = array (
	        'wishlist' => 'on',
	        'compare' => 'off',
	        'ajaxsearch' => 'off',
	        'ajaxcart_singleproduct' => 'off',
	        'postduplicator' => 'off',
	        'loadproductlimit' => 20.0,
	    );
	    update_option('woolentor_others_tabs', $shoplentor_options);


    	$air_ticket_bookinghome_content = '';
		$air_ticket_bookinghome_title = 'Home';
		$air_ticket_bookinghome = array(
				'post_type' => 'page',
				'post_title' => $air_ticket_bookinghome_title,
				'post_content'  => $air_ticket_bookinghome_content,
				'post_status' => 'publish',
				'post_author' => 1,
				'post_slug' => 'home'
		);
		$air_ticket_bookinghome_id = wp_insert_post($air_ticket_bookinghome);

		add_post_meta( $air_ticket_bookinghome_id, '_wp_page_template', 'frontpage.php' );

		$air_ticket_bookinghome = get_page_by_path( 'Home' );
		update_option( 'page_on_front', $air_ticket_bookinghome->ID );
		update_option( 'show_on_front', 'page' );

		// Creation of blog page //
		$air_ticket_bookingblog_title = 'Blog';
		$air_ticket_bookingblog_check = get_page_by_path('blog');
		if (!$air_ticket_bookingblog_check) {
			$air_ticket_bookingblog = array(
				'post_type'    => 'page',
				'post_title'   => $air_ticket_bookingblog_title,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'blog'
			);
			$air_ticket_bookingblog_id = wp_insert_post($air_ticket_bookingblog);

			if (!is_wp_error($air_ticket_bookingblog_id)) {
				update_option('page_for_posts', $air_ticket_bookingblog_id);
			}
		}

		// Creation of about page //
		$air_ticket_bookingabout_title = 'About';
		$air_ticket_bookingabout_content = 'What is Lorem Ipsum?
													Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys 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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
													&nbsp;
													Why do we use it?
													It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
													&nbsp;
													Where does it come from?
													There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
													&nbsp;
													Why do we use it?
													It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
													&nbsp;
													Where does it come from?
													There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
		$air_ticket_bookingabout_check = get_page_by_path('about');
		if (!$air_ticket_bookingabout_check) {
			$air_ticket_bookingabout = array(
				'post_type'    => 'page',
				'post_title'   => $air_ticket_bookingabout_title,
				'post_content'   => $air_ticket_bookingabout_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'about'
			);
			wp_insert_post($air_ticket_bookingabout);
		}

		// Creation of services page //
		$air_ticket_bookingservices_title = 'Contact';
		$air_ticket_bookingservices_content = 'What is Lorem Ipsum?
													Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys 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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
													&nbsp;
													Why do we use it?
													It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
													&nbsp;
													Where does it come from?
													There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
													&nbsp;
													Why do we use it?
													It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
													&nbsp;
													Where does it come from?
													There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
		$air_ticket_bookingservices_check = get_page_by_path('services');
		if (!$air_ticket_bookingservices_check) {
			$air_ticket_bookingservices = array(
				'post_type'    => 'page',
				'post_title'   => $air_ticket_bookingservices_title,
				'post_content'   => $air_ticket_bookingservices_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'services'
			);
			wp_insert_post($air_ticket_bookingservices);
		}

		// Creation of destination page //
		$air_ticket_bookingdestination_title = 'Destination';
		$air_ticket_bookingdestination_content = 'What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys 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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
		$air_ticket_bookingdestination_check = get_page_by_path('destination');
		if (!$air_ticket_bookingdestination_check) {
			$air_ticket_bookingdestination = array(
				'post_type'    => 'page',
				'post_title'   => $air_ticket_bookingdestination_title,
				'post_content'   => $air_ticket_bookingdestination_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'destination'
			);
			wp_insert_post($air_ticket_bookingdestination);
		}

		// Creation of support page //
		$air_ticket_bookingsupport_title = 'Support';
		$air_ticket_bookingsupport_content = 'What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys 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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
		$air_ticket_bookingsupport_check = get_page_by_path('support');
		if (!$air_ticket_bookingsupport_check) {
			$air_ticket_bookingsupport = array(
				'post_type'    => 'page',
				'post_title'   => $air_ticket_bookingsupport_title,
				'post_content'   => $air_ticket_bookingsupport_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'support'
			);
			wp_insert_post($air_ticket_bookingsupport);
		}

		// Creation of features page //
		$air_ticket_bookingfeatures_title = 'Features';
		$air_ticket_bookingfeatures_content = 'What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys 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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
		$air_ticket_bookingfeatures_check = get_page_by_path('features');
		if (!$air_ticket_bookingfeatures_check) {
			$air_ticket_bookingfeatures = array(
				'post_type'    => 'page',
				'post_title'   => $air_ticket_bookingfeatures_title,
				'post_content'   => $air_ticket_bookingfeatures_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'features'
			);
			wp_insert_post($air_ticket_bookingfeatures);
		}

		// Creation of services page //
		$air_ticket_bookingbirthday_title = 'Products';
		$air_ticket_bookingbirthday_content = 'What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys 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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
		$air_ticket_bookingbirthday_check = get_page_by_path('birthday');
		if (!$air_ticket_bookingbirthday_check) {
			$air_ticket_bookingbirthday = array(
				'post_type'    => 'page',
				'post_title'   => $air_ticket_bookingbirthday_title,
				'post_content'   => $air_ticket_bookingbirthday_content,
				'post_status'  => 'publish',
				'post_author'  => 1,
				'post_name'    => 'birthday'
			);
			wp_insert_post($air_ticket_bookingbirthday);
		}

		// Creation of Pages //
		$air_ticket_bookingnotfound_title = 'Pages';
		$air_ticket_bookingnotfound = array(
			'post_type'   => 'pages',
			'post_title'  => $air_ticket_bookingnotfound_title,
			'post_status' => 'publish',
			'post_author' => 1,
			'post_slug'   => 'pages'
		);
		$air_ticket_bookingnotfound_id = wp_insert_post($air_ticket_bookingnotfound);
		add_post_meta($air_ticket_bookingnotfound_id, '_wp_page_template', 'pages.php');

		/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- Air Ticket Booking Primary Menu -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*/

		$air_ticket_bookingthemename = 'Air Ticket Booking';
		$air_ticket_bookingmenuname = $air_ticket_bookingthemename . ' Primary Menu';
		$air_ticket_bookingmenulocation = 'air-ticket-booking-primary-menu';
		$air_ticket_bookingmenu_exists = wp_get_nav_menu_object($air_ticket_bookingmenuname);

		if (!$air_ticket_bookingmenu_exists) {

			$air_ticket_bookingmenu_id = wp_create_nav_menu($air_ticket_bookingmenuname);

			// Home
			wp_update_nav_menu_item($air_ticket_bookingmenu_id, 0, array(
				'menu-item-title'  => __('Home', 'air-ticket-booking'),
				'menu-item-url'    => home_url('/'),
				'menu-item-status' => 'publish'
			));

			// About
			$air_ticket_bookingpage_about = get_page_by_path('about');
			if($air_ticket_bookingpage_about){
				wp_update_nav_menu_item($air_ticket_bookingmenu_id, 0, array(
					'menu-item-title'     => __('About', 'air-ticket-booking'),
					'menu-item-object-id' => $air_ticket_bookingpage_about->ID,
					'menu-item-object'    => 'page',
					'menu-item-type'      => 'post_type',
					'menu-item-status'    => 'publish'
				));
			}

			// Services
			$air_ticket_bookingpage_services = get_page_by_path('services');
			if($air_ticket_bookingpage_services){
				wp_update_nav_menu_item($air_ticket_bookingmenu_id, 0, array(
					'menu-item-title'     => __('Services', 'air-ticket-booking'),
					'menu-item-object-id' => $air_ticket_bookingpage_services->ID,
					'menu-item-object'    => 'page',
					'menu-item-type'      => 'post_type',
					'menu-item-status'    => 'publish'
				));
			}

			// Destination
			$air_ticket_bookingpage_destination = get_page_by_path('destination');
			if($air_ticket_bookingpage_destination){
				wp_update_nav_menu_item($air_ticket_bookingmenu_id, 0, array(
					'menu-item-title'     => __('Destination', 'air-ticket-booking'),
					'menu-item-object-id' => $air_ticket_bookingpage_destination->ID,
					'menu-item-object'    => 'page',
					'menu-item-type'      => 'post_type',
					'menu-item-status'    => 'publish'
				));
			}

			// Blog
			$air_ticket_bookingpage_blog = get_page_by_path('blog');
			if($air_ticket_bookingpage_blog){
				wp_update_nav_menu_item($air_ticket_bookingmenu_id, 0, array(
					'menu-item-title'     => __('Blog', 'air-ticket-booking'),
					'menu-item-object-id' => $air_ticket_bookingpage_blog->ID,
					'menu-item-object'    => 'page',
					'menu-item-type'      => 'post_type',
					'menu-item-status'    => 'publish'
				));
			}

			if (!has_nav_menu($air_ticket_bookingmenulocation)) {
				$air_ticket_bookinglocations = get_theme_mod('nav_menu_locations');
				$air_ticket_bookinglocations[$air_ticket_bookingmenulocation] = $air_ticket_bookingmenu_id;
				set_theme_mod('nav_menu_locations', $air_ticket_bookinglocations);
			}
		}

		// ======================== destinations start ==========================

		// Create destinations and two itineraries per destination
		$parent_category_arr = array(
		'Turkey' => array( 'Brihadisvara Temple' ),
		'France' => array( 'Canggu Temple' ),
		'Netherland' => array( 'Barkhor Street' ),
		'German' => array( "Tigers Nest Blitltan" ),
	   );

		// Map simple trip types per destination. Adjust values as needed.
		$trip_type_map = array(
			'Turkey' => 'City',
			'France' => 'City Break',
			'Netherland' => 'Beach & Adventure',
			'German' => 'Trekking',
		);

		function flex_attach_image_from_theme( $image_url, $image_name, $post_parent = 0 ) {

			if ( empty( $image_url ) ) {
				return false;
			}

			$upload_dir = wp_upload_dir();
			$image_data = @file_get_contents( esc_url( $image_url ) );

			if ( $image_data === false ) {
				return false;
			}

			$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name );
			$filename = basename( $unique_file_name );

			if ( wp_mkdir_p( $upload_dir['path'] ) ) {
				$file = $upload_dir['path'] . '/' . $filename;
			} else {
				$file = $upload_dir['basedir'] . '/' . $filename;
			}

			file_put_contents( $file, $image_data );

			$wp_filetype = wp_check_filetype( $filename, null );

			$attachment = array(
				'post_mime_type' => $wp_filetype['type'],
				'post_title'     => sanitize_file_name( $filename ),
				'post_content'   => '',
				'post_status'    => 'inherit',
			);

			$attach_id = wp_insert_attachment( $attachment, $file, $post_parent );

			require_once ABSPATH . 'wp-admin/includes/image.php';

			$attach_data = wp_generate_attachment_metadata( $attach_id, $file );

			wp_update_attachment_metadata( $attach_id, $attach_data );

			return array(
				'id' => $attach_id,
				'url' => wp_get_attachment_image_url( $attach_id )
			);
		}

		// Loop destinations
		foreach ( $parent_category_arr as $destination_name => $itineraries ) {

			$existing = term_exists( $destination_name, 'travel_locations' );

			if ( $existing === 0 || $existing === null ) {

				$term = wp_insert_term( $destination_name, 'travel_locations', array(
					'description' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
				) );

				if ( is_wp_error( $term ) ) {
					continue;
				}

				$dest_id = $term['term_id'];

			} else {

				$dest_id = is_array( $existing ) ? $existing['term_id'] : $existing;

			}

			// destination image
			$existing_image = get_term_meta( $dest_id, 'destination_image', true );

			if ( empty( $existing_image ) ) {

				$dest_image_url = get_template_directory_uri() . '/assets/images/destination/' . str_replace( ' ', '-', $destination_name ) . '.png';

				$image_name = $destination_name . '.png';

				$attached = flex_attach_image_from_theme( $dest_image_url, $image_name );

				if ( $attached && ! empty( $attached['url'] ) ) {
					update_term_meta( $dest_id, 'destination_image', $attached['url'] );
				}

			}

			$count = 0;

			foreach ( $itineraries as $title ) {

				if ( $count >= 4 ) {
					break;
				}

				$post_arr = array(
					'post_title'   => wp_strip_all_tags( $title ),
					'post_content' => 'Auto-generated content for ' . $title,
					'post_status'  => 'publish',
					'post_type'    => 'itineraries',
				);

				$post_id = wp_insert_post( $post_arr );

				if ( ! $post_id || is_wp_error( $post_id ) ) {
					continue;
				}

				wp_set_object_terms( $post_id, intval( $dest_id ), 'travel_locations', true );

				$trip_type = isset( $trip_type_map[ $destination_name ] ) ? $trip_type_map[ $destination_name ] : 'General';

				update_post_meta( $post_id, 'itinerary_types', $trip_type );

				update_post_meta( $post_id, 'wp_travel_trip_price', '100' );
				update_post_meta( $post_id, 'wp_travel_trip_duration', '3' );
				update_post_meta( $post_id, 'wp_travel_trip_duration_night', '2' );

				// itinerary image
				$itinerary_image_url = get_template_directory_uri() . '/assets/images/itineraries/' . str_replace( ' ', '-', $title ) . '.png';

				$it_image_name = $title . '.png';

				$attached_it = flex_attach_image_from_theme( $itinerary_image_url, $it_image_name, $post_id );

				if ( $attached_it && ! empty( $attached_it['id'] ) ) {
					set_post_thumbnail( $post_id, $attached_it['id'] );
				}

				$count++;

			}
		}

		// ======================== destinations end ==========================
    }
}