<?php
/*
Plugin Name: STM Importer
Plugin URI: http://stylemixthemes.com/
Description: STM Importer
Author: Stylemix Themes
Author URI: http://stylemixthemes.com/
Text Domain: stm_importer
Version: 3.1
*/

$plugin_path = dirname( __FILE__ );
define( 'STM_IMPORTER_DIR', $plugin_path );

if ( !function_exists('stm_demo_import'))
{

	if( ! function_exists( 'transcargo_importer_done_function' ) ){
		function transcargo_importer_done_function(){

			global $wp_filesystem;

			if ( empty( $wp_filesystem ) ) {
				require_once ABSPATH . '/wp-admin/includes/file.php';
				WP_Filesystem();
			}

			$locations = get_theme_mod( 'nav_menu_locations' );
			$menus     = wp_get_nav_menus();

			if ( ! empty( $menus ) ) {
				foreach ( $menus as $menu ) {
					if ( is_object( $menu ) ) {
						switch ( $menu->name ) {
							case 'Primary Menu':
								$locations['transcargo-primary'] = $menu->term_id;
								break;
							case 'Footer Menu':
								$locations['transcargo-footer'] = $menu->term_id;
								break;
						}
					}
				}
			}

			set_theme_mod( 'nav_menu_locations', $locations );

			update_option( 'show_on_front', 'page' );

			$front_page = get_page_by_title( 'Home' );
			if ( isset( $front_page->ID ) ) {
				update_option( 'page_on_front', $front_page->ID );
			}
			$blog_page = get_page_by_title( 'News' );
			if ( isset( $blog_page->ID ) ) {
				update_option( 'page_for_posts', $blog_page->ID );
			}

			$encode_options = $wp_filesystem->get_contents( STM_IMPORTER_DIR . '/demo/theme_mods.json' );
			$options        = json_decode( $encode_options, true );
			foreach ( $options as $key => $value ) {
				set_theme_mod( $key, $value );
			}

			if ( class_exists( 'RevSlider' ) ) {

				$main_slider = STM_IMPORTER_DIR . '/demo/main_slider.zip';

				if ( file_exists( $main_slider ) ) {
					$slider = new RevSlider();
					$slider->importSliderFromPost( true, true, $main_slider );
				}

				$about_us_slider = STM_IMPORTER_DIR . '/demo/about_us_slider.zip';

				if ( file_exists( $about_us_slider ) ) {
					$slider = new RevSlider();
					$slider->importSliderFromPost( true, true, $about_us_slider );
				}

				$boxed_slider = STM_IMPORTER_DIR . '/demo/boxed-slider.zip';

				if ( file_exists( $boxed_slider ) ) {
					$slider = new RevSlider();
					$slider->importSliderFromPost( true, true, $boxed_slider );
				}
			}
		}
	}

	add_action( 'stm_importer_done', 'transcargo_importer_done_function' );

	function stm_demo_import()
	{
		?>
		<div class="stm_message content" style="display:none;">
			<img src="<?php echo plugin_dir_url( __FILE__ ) ?>assets/images/spinner.gif" alt="spinner">
			<h1 class="stm_message_title"><?php esc_html_e('Importing Demo Content...', 'transcargo'); ?></h1>
			<p class="stm_message_text"><?php esc_html_e('Demo content import duration relies on your server speed.', 'transcargo'); ?></p>
		</div>

		<div class="stm_message success" style="display:none;">
			<p class="stm_message_text"><?php echo wp_kses( sprintf(__('Congratulations and enjoy <a href="%s" target="_blank">your website</a> now!', 'transcargo'), esc_url( home_url() )), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ); ?></p>
		</div>

		<form class="stm_importer" id="import_demo_data_form" action="?page=stm_demo_import" method="post">

			<div class="stm_importer_options">

				<div class="stm_importer_note">
					<strong><?php esc_html_e('Before installing the demo content, please NOTE:', 'transcargo'); ?></strong>
					<p><?php echo wp_kses( sprintf(__('Install the demo content only on a clean WordPress. Use <a href="%s" target="_blank">Wordpress Database Reset</a> plugin to clean the current Theme.', 'transcargo'), 'http://wordpress.org/plugins/wordpress-database-reset/', esc_url( home_url() )), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ); ?></p>
					<p><?php esc_html_e('Remember that you will NOT get the images from live demo due to copyright / license reason.', 'transcargo'); ?></p>
				</div>
				<input class="button-primary size_big" type="submit" value="Import" id="import_demo_data">

			</div>

		</form>
		<script type="text/javascript">
			jQuery(document).ready(function() {
				jQuery('#import_demo_data_form').on('submit', function() {
					jQuery("html, body").animate({
						scrollTop: 0
					}, {
						duration: 300
					});
					jQuery('.stm_importer').slideUp(null, function(){
						jQuery('.stm_message.content').slideDown();
					});

					// Importing Content
					jQuery.ajax({
						type: 'POST',
						url: '<?php echo admin_url('admin-ajax.php'); ?>',
						data: {
							action: 'stm_demo_import_content'
						},
						success: function(){

							jQuery('.stm_message.content').slideUp();
							jQuery('.stm_message.success').slideDown();
							
							/*Analytics*/
                            jQuery.ajax({
                                url: 'https://panel.stylemixthemes.com/api/active/',
                                type: 'post',
                                dataType: 'json',
                                data: {
                                    theme: 'transcargo',
                                    layout: 'default',
                                    website: "<?php echo esc_url(get_site_url()); ?>",

                                    <?php
                                    $envato = get_option('envato_market', array());
                                    $token = (!empty($envato['token'])) ? $envato['token'] : ''; ?>
                                    token: "<?php echo esc_js($token); ?>"
                                }
                            });

						}
					});
					return false;
				});
			});
		</script>
		<?php
	}

	// Content Import
	function stm_demo_import_content() {
		set_time_limit( 0 );

		if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) {
			define( 'WP_LOAD_IMPORTERS', true );
		}

		require_once( 'wordpress-importer/wordpress-importer.php' );

		$wp_import                    = new WP_Import();
		$wp_import->fetch_attachments = true;

		ob_start();
		$wp_import->import( STM_IMPORTER_DIR . '/demo/demo_content.xml' );
		ob_end_clean();

		do_action( 'stm_importer_done' );

		echo 'done';
		die();

	}

	add_action( 'wp_ajax_stm_demo_import_content', 'stm_demo_import_content' );

}
