<?php
/**
 * Demo importer AJAX functions
 *
 * @package Meridian One
 * @since 1.0
 */

/**
 * Table Of Contents
 *
 * meridian_one_importer_ajax_install_nav_menus ( Install navigation menus )
 * meridian_one_importer_ajax_install_home_page ( Set up homepage )
 * meridian_one_importer_ajax_install_customizer ( Set up customizer values )
 * meridian_one_importer_ajax_install_widgets ( Set up widgets )
 */

/**
 * Install navigation menus
 *
 * @since 1.0
 */
function meridian_one_importer_ajax_install_nav_menus() {

	// Allowed to do this?
	if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {

		$response = array();
		$response['status'] = 'success';

		// get locations
		$locations = get_theme_mod('nav_menu_locations');

		/**
		 * Header
		 */

		// Check if the menu exists
		$menu_exists = wp_get_nav_menu_object( esc_html__( 'Header', 'meridian-one' ) );
		$home_url = esc_url( home_url( '/' ) );

		// If it doesn't exist, let's create it.
		if ( ! $menu_exists ) {

			$menu_id = wp_create_nav_menu( esc_html__( 'Header', 'meridian-one' ) );		
			$locations['menu-1'] = $menu_id;

			wp_update_nav_menu_item( $menu_id, 0, array(
				'menu-item-title' => esc_html__( 'Features', 'meridian-one' ),
				'menu-item-url' => $home_url . '#features',
				'menu-item-status' => 'publish',
			));

			wp_update_nav_menu_item( $menu_id, 0, array(
				'menu-item-title' => esc_html__( 'About', 'meridian-one' ),
				'menu-item-url' => $home_url . '#about',
				'menu-item-status' => 'publish',
			));

			wp_update_nav_menu_item( $menu_id, 0, array(
				'menu-item-title' => esc_html__( 'Services', 'meridian-one' ),
				'menu-item-url' => $home_url . '#services',
				'menu-item-status' => 'publish',
			));

			wp_update_nav_menu_item( $menu_id, 0, array(
				'menu-item-title' => esc_html__( 'Testimonials', 'meridian-one' ),
				'menu-item-url' => $home_url . '#testimonials',
				'menu-item-status' => 'publish',
			));

			wp_update_nav_menu_item( $menu_id, 0, array(
				'menu-item-title' => esc_html__( 'Team', 'meridian-one' ),
				'menu-item-url' => $home_url . '#team',
				'menu-item-status' => 'publish',
			));

			wp_update_nav_menu_item( $menu_id, 0, array(
				'menu-item-title' => esc_html__( 'News', 'meridian-one' ),
				'menu-item-url' => $home_url . '#news',
				'menu-item-status' => 'publish',
			));

			wp_update_nav_menu_item( $menu_id, 0, array(
				'menu-item-title' => esc_html__( 'Contact', 'meridian-one' ),
				'menu-item-url' => $home_url . '#contact',
				'menu-item-status' => 'publish',
			));

		}

		// Set new locations
		set_theme_mod( 'nav_menu_locations', $locations );

		// Encode response
		$response_json = json_encode( $response );	

		// AJAX phone home
		header( "Content-Type: application/json" );
		echo $response_json;

		// Asta la vista
		exit;

	}
			
} add_action( 'wp_ajax_meridian-one-ajax-install-nav-menus', 'meridian_one_importer_ajax_install_nav_menus' );

/**
 * Set up homepage
 *
 * @since 1.0
 */
function meridian_one_importer_ajax_install_home_page() {

	// Allowed to do this?
	if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {

		$response = array();
		$response['status'] = 'success';

		// Create post object
		$the_post = array(
			'post_title' => esc_html__( 'Home', 'meridian-one' ),
			'post_status' => 'publish',
			'post_type' => 'page',
		);

		// Insert the post into the database
		$post_id = wp_insert_post( $the_post );

		// If post added
		if ( $post_id ) {

			// set homepage template
			update_post_meta( $post_id, '_wp_page_template', 'template-home.php' );

			// Set as front page			
			update_option( 'page_on_front', $post_id );
			update_option( 'show_on_front', 'page' );

		} else {
			$response['status'] = 'fail';
		}

		// Encode response
		$response_json = json_encode( $response );	

		// AJAX phone home
		header( "Content-Type: application/json" );
		echo $response_json;

		// Asta la vista
		exit;

	}
			
} add_action( 'wp_ajax_meridian-one-ajax-install-home-page', 'meridian_one_importer_ajax_install_home_page' );

/**
 * Set up customizer values
 *
 * @since 1.0
 */
function meridian_one_importer_ajax_install_customizer() {

	// Allowed to do this?
	if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {

		$prefix = 'meridian_one_';

		$response = array();
		$response['status'] = 'success';

		$theme_mods = array(
			$prefix . 'home_slider_section_image' => get_parent_theme_file_uri() . '/images/placeholders/slider-1.jpg',
			$prefix . 'home_features_section_image' => get_parent_theme_file_uri() . '/images/placeholders/features-image.jpg',
			$prefix . 'home_slider_section_button_url' => home_url('/'),
			$prefix . 'home_process_section_button_url' => home_url('/'),
			$prefix . 'home_team_section_button_url' => home_url('/'),
			$prefix . 'home_blog_section_button_url' => home_url('/'),
			$prefix . 'home_slider_section_enabled' => 1,
			$prefix . 'home_features_section_enabled' => 1,
			$prefix . 'home_about_section_enabled' => 1,
			$prefix . 'home_tour_section_enabled' => 1,
			$prefix . 'home_services_section_enabled' => 1,
			$prefix . 'home_process_section_enabled' => 1,
			$prefix . 'home_testimonials_section_enabled' => 1,
			$prefix . 'home_team_section_enabled' => 1,
			$prefix . 'home_blog_section_enabled' => 1,
			$prefix . 'home_contact_section_enabled' => 1,
			$prefix . 'footer_subscribe_enabled' => 1,
			// text
			$prefix . 'home_slider_section_heading' => esc_html__( 'We are creative geek', 'meridian-one' ),
			$prefix . 'home_slider_section_content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
			$prefix . 'home_slider_section_button_text' => esc_html__( 'Learn More', 'meridian-one' ),
			$prefix . 'home_features_section_title' => esc_html__( 'Features', 'meridian-one' ),
			$prefix . 'home_features_section_subtitle' => esc_html__( 'Build a website fast & easy', 'meridian-one' ),
			$prefix . 'home_features_section_description' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
			$prefix . 'home_features_section_image_title' => esc_html__( 'Image title', 'meridian-one' ),
			$prefix . 'home_features_section_image_description' => esc_html__( 'Veritatis quod ratione facilis iste, facere! Molestias in dolorem dolores, rem.', 'meridian-one' ),
			$prefix . 'home_about_section_title' => esc_html__( 'About us', 'meridian-one' ),
			$prefix . 'home_about_section_subtitle' => esc_html__( 'Find out who we are', 'meridian-one' ),
			$prefix . 'home_about_section_description' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
			$prefix . 'home_tour_section_title' => esc_html__( 'Company Tour', 'meridian-one' ),
			$prefix . 'home_tour_section_video_url' => esc_html__( 'https://www.youtube.com/watch?v=EYs_FckMqow', 'meridian-one' ),
			$prefix . 'home_services_section_title' => esc_html__( 'Services', 'meridian-one' ),
			$prefix . 'home_services_section_subtitle' => esc_html__( 'We love what we do', 'meridian-one' ),
			$prefix . 'home_services_section_description' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
			$prefix . 'home_process_section_button_text' => esc_html__( 'Let\'s work together', 'meridian-one' ),
			$prefix . 'home_testimonials_section_title' => esc_html__( 'Testimonials', 'meridian-one' ),
			$prefix . 'home_testimonials_section_subtitle' => esc_html__( 'What our customers said', 'meridian-one' ),
			$prefix . 'home_testimonials_customers_count_num' => esc_html__( '10457', 'meridian-one' ),
			$prefix . 'home_testimonials_customers_count_text' => esc_html__( 'happy/customers', 'meridian-one' ),
			$prefix . 'home_team_section_title' => esc_html__( 'Our Team', 'meridian-one' ),
			$prefix . 'home_team_section_subtitle' => esc_html__( 'The best and brightest', 'meridian-one' ),
			$prefix . 'home_team_section_description' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
			$prefix . 'home_team_section_button_text' => esc_html__( 'Join Our Team', 'meridian-one' ),
			$prefix . 'home_blog_section_title' => esc_html__( 'News', 'meridian-one' ),
			$prefix . 'home_blog_section_subtitle' => esc_html__( 'Keep Informed', 'meridian-one' ),
			$prefix . 'home_blog_section_button_text' => esc_html__( 'More News', 'meridian-one' ),
			$prefix . 'home_contact_section_title' => esc_html__( 'Contact Us', 'meridian-one' ),
			$prefix . 'home_contact_section_subtitle' => esc_html__( 'Get in touch', 'meridian-one' ),
			$prefix . 'home_contact_section_map_url' => 'https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2625.005322023728!2d2.293150922949521!3d48.85810890509051!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47e66e2964e34e2d%3A0x8ddca9ee380ef7e0!2sEiffel+Tower!5e0!3m2!1sen!2s!4v1504830426269',
			$prefix . 'footer_subscribe_title' => esc_html__( 'Newsletter', 'meridian-one' ),
			$prefix . 'footer_subscribe_subtitle' => esc_html__( 'Subscribe to our newsletter for latest news.', 'meridian-one' ),
			$prefix . 'footer_subscribe_content' => '[optinform]',
		);

		// Set new locations
		foreach ( $theme_mods as $theme_mod_key => $theme_mod_val ) {
			set_theme_mod( $theme_mod_key, $theme_mod_val );
		}

		// Encode response
		$response_json = json_encode( $response );	

		// AJAX phone home
		header( "Content-Type: application/json" );
		echo $response_json;

		// Asta la vista
		exit;

	}
			
} add_action( 'wp_ajax_meridian-one-ajax-install-customizer', 'meridian_one_importer_ajax_install_customizer' );

/**
 * Set up widgets
 *
 * @since 1.0
 */
function meridian_one_importer_ajax_install_widgets() {

	// Allowed to do this?
	if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {

		$prefix = 'meridian_one_';

		$response = array();
		$response['status'] = 'success';

		$data = array();

		// features
		$data['sidebar-home-features'] = array(
			array(
				'id_base'=> 'meridian_one_feature_widget',
				'instance' => array(
					'title' => esc_html__( 'Feature', 'meridian-one' ),
					'icon' => 'fa-laptop',
					'heading' => esc_html__( 'Fully Responsive', 'meridian-one' ),
					'content' => esc_html__( 'The theme is responsive, your website will look great on all devices.', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_feature_widget',
				'instance' => array(
					'title' => esc_html__( 'Feature', 'meridian-one' ),
					'icon' => 'fa-shopping-basket',
					'heading' => esc_html__( 'WooCommerce', 'meridian-one' ),
					'content' => esc_html__( 'Fully compatible with the most popular eCommerce plugin.', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_feature_widget',
				'instance' => array(
					'title' => esc_html__( 'Feature', 'meridian-one' ),
					'icon' => 'fa-server',
					'heading' => esc_html__( 'Customizer', 'meridian-one' ),
					'content' => esc_html__( 'Easily manage the homepage sections via the Customizer.', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_feature_widget',
				'instance' => array(
					'title' => esc_html__( 'Feature', 'meridian-one' ),
					'icon' => 'fa-list',
					'heading' => esc_html__( 'Homepage Sections', 'meridian-one' ),
					'content' => esc_html__( 'The homepage comes with 11 customizable sections.', 'meridian-one' ),
				)
			),
		);

		// about
		$data['sidebar-home-about'] = array(
			array(
				'id_base'=> 'meridian_one_about_widget',
				'instance' => array(
					'title' => esc_html__( 'About', 'meridian-one' ),
					'icon' => 'fa-asterisk',
					'heading' => esc_html__( 'Our History', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_about_widget',
				'instance' => array(
					'title' => esc_html__( 'About', 'meridian-one' ),
					'icon' => 'fa-rocket',
					'heading' => esc_html__( 'Our Achievements', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_about_widget',
				'instance' => array(
					'title' => esc_html__( 'About', 'meridian-one' ),
					'icon' => 'fa-thumbs-up',
					'heading' => esc_html__( 'Our Mission', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
				)
			),
		);

		// services
		$team_image = meridian_one_add_media_to_library( get_parent_theme_file_uri() . '/inc/importer/images/services-placeholder.jpg' );
		$data['sidebar-home-services'] = array(
			array(
				'id_base'=> 'meridian_one_service_widget',
				'instance' => array(
					'title' => esc_html__( 'Service', 'meridian-one' ),
					'icon' => 'fa-html5',
					'heading' => esc_html__( 'Web Design', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
					'image' => $team_image,
				)
			),
			array(
				'id_base'=> 'meridian_one_service_widget',
				'instance' => array(
					'title' => esc_html__( 'Service', 'meridian-one' ),
					'icon' => 'fa-bar-chart',
					'heading' => esc_html__( 'UI/UX Design', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
					'image' => $team_image,
				)
			),
			array(
				'id_base'=> 'meridian_one_service_widget',
				'instance' => array(
					'title' => esc_html__( 'Service', 'meridian-one' ),
					'icon' => 'fa-leaf',
					'heading' => esc_html__( 'Graphic Design', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
					'image' => $team_image,
				)
			),
			array(
				'id_base'=> 'meridian_one_service_widget',
				'instance' => array(
					'title' => esc_html__( 'Service', 'meridian-one' ),
					'icon' => 'fa-lightbulb-o',
					'heading' => esc_html__( 'Branding', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
					'image' => $team_image,
				)
			),
		);

		// process
		$data['sidebar-home-process'] = array(
			array(
				'id_base'=> 'meridian_one_process_widget',
				'instance' => array(
					'title' => esc_html__( 'Process', 'meridian-one' ),
					'heading' => esc_html__( 'Planning', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_process_widget',
				'instance' => array(
					'title' => esc_html__( 'Process', 'meridian-one' ),
					'heading' => esc_html__( 'Development', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_process_widget',
				'instance' => array(
					'title' => esc_html__( 'Process', 'meridian-one' ),
					'heading' => esc_html__( 'Launch', 'meridian-one' ),
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
				)
			),
		);

		// testimonials
		$data['sidebar-home-testimonials'] = array(
			array(
				'id_base'=> 'meridian_one_testimonial_widget',
				'instance' => array(
					'title' => esc_html__( 'Testimonial', 'meridian-one' ),
					'image' => '',
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
					'name' => esc_html__( 'Jake Paralta', 'meridian-one' ),
					'position' => esc_html__( 'USA', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_testimonial_widget',
				'instance' => array(
					'title' => esc_html__( 'Testimonial', 'meridian-one' ),
					'image' => '',
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
					'name' => esc_html__( 'Mikael Eidenberg', 'meridian-one' ),
					'position' => esc_html__( 'Sweden', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_testimonial_widget',
				'instance' => array(
					'title' => esc_html__( 'Testimonial', 'meridian-one' ),
					'image' => '',
					'content' => esc_html__( 'Nullam ut tempor eros. Donec faucibus, velit et imperdiet aliquam, lacus velit luctus urna, vitae porttitor orci libero id felis.', 'meridian-one' ),
					'name' => esc_html__( 'Tim Boelaars', 'meridian-one' ),
					'position' => esc_html__( 'Australia', 'meridian-one' ),
				)
			),
		);

		// clients
		

		// team
		$team_image = meridian_one_add_media_to_library( get_parent_theme_file_uri() . '/inc/importer/images/team-placeholder.jpg' );
		$data['sidebar-home-team'] = array(
			array(
				'id_base'=> 'meridian_one_team_member_widget',
				'instance' => array(
					'title' => esc_html__( 'Team Member', 'meridian-one' ),
					'image' => $team_image,
					'name' => esc_html__( 'James Gordon', 'meridian-one' ),
					'position' => esc_html__( 'Manager', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_team_member_widget',
				'instance' => array(
					'title' => esc_html__( 'Team Member', 'meridian-one' ),
					'image' => $team_image,
					'name' => esc_html__( 'Al Bundy', 'meridian-one' ),
					'position' => esc_html__( 'Web Developer', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_team_member_widget',
				'instance' => array(
					'title' => esc_html__( 'Team Member', 'meridian-one' ),
					'image' => $team_image,
					'name' => esc_html__( 'Carley Keener', 'meridian-one' ),
					'position' => esc_html__( 'Web Designer', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_team_member_widget',
				'instance' => array(
					'title' => esc_html__( 'Team Member', 'meridian-one' ),
					'image' => $team_image,
					'name' => esc_html__( 'Michael Kane', 'meridian-one' ),
					'position' => esc_html__( 'SEO Wizard', 'meridian-one' ),
				)
			),
		);

		// contact
		$data['sidebar-home-contact'] = array(
			array(
				'id_base'=> 'meridian_one_contact_info_widget',
				'instance' => array(
					'title' => esc_html__( 'Contact Info', 'meridian-one' ),
					'icon' => 'fa-building',
					'heading' => esc_html__( 'Address', 'meridian-one' ),
					'content' => esc_html__( 'E104 - Dharti 2 Near Silverstar Mall', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_contact_info_widget',
				'instance' => array(
					'title' => esc_html__( 'Contact Info', 'meridian-one' ),
					'icon' => 'fa-mobile-phone',
					'heading' => esc_html__( 'Phone No.', 'meridian-one' ),
					'content' => esc_html__( '123-456-7890', 'meridian-one' ),
				)
			),
			array(
				'id_base'=> 'meridian_one_contact_info_widget',
				'instance' => array(
					'title' => esc_html__( 'Contact Info', 'meridian-one' ),
					'icon' => 'fa-at',
					'heading' => esc_html__( 'Email', 'meridian-one' ),
					'content' => esc_html__( 'meridianone@example.com', 'meridian-one' ),
				)
			),
		);

		// add the widgets
		meridian_one_add_widgets_to_sidebar( $data );

		// Encode response
		$response_json = json_encode( $response );	

		// AJAX phone home
		header( "Content-Type: application/json" );
		echo $response_json;

		// Asta la vista
		exit;

	}
			
} add_action( 'wp_ajax_meridian-one-ajax-install-widgets', 'meridian_one_importer_ajax_install_widgets' );