<?php

/*
 * Custom Importer
 *
 */

defined( 'ABSPATH' ) or die( 'You cannot access this script directly' );


add_action('admin_menu', 'xs_demo_importer_menu');
if ( !function_exists( 'xs_demo_importer_menu' ) ) {
	
	function xs_demo_importer_menu() {
	
		add_submenu_page( 'themes.php' , 'Demo Importer' , 'Demo Importer' , 'manage_options' , 'xs_demo_importer' , 'xs_view_updater' );
	
	}
	
}


	function xs_importer_admin_add_scripts() {
			
	    wp_enqueue_script( 'jquery' );
		wp_enqueue_script( 'ot-importer', get_template_directory_uri() . '/framework/option-tree/assets/js/ot-importer.js', array('jquery'), null , true );    
					
	}

add_action( 'admin_enqueue_scripts', 'xs_importer_admin_add_scripts' );

include_once(get_template_directory() ."/framework/components/importer/xs-importer-page.php");

add_action( 'wp_ajax_xs_action', 'xs_action_callback' );
function xs_action_callback() 
{
    global $wpdb; 

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

    // Load Importer API
    require_once ABSPATH . 'wp-admin/includes/import.php';

    if ( ! class_exists( 'WP_Importer' ) ) {
        $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
        if ( file_exists( $class_wp_importer ) )
        {
            require $class_wp_importer;
        }
    }

    if ( ! class_exists( 'WP_Import' ) ) {
        $class_wp_importer = get_template_directory() ."/framework/components/importer/wordpress-importer.php";
        if ( file_exists( $class_wp_importer ) )
            require $class_wp_importer;
    }


    if ( class_exists( 'WP_Import' ) ) 
    { 
        $import_filepath = get_template_directory() ."/framework/demo/xml/demo.xml" ; // Get the xml file from directory 

        include_once(get_template_directory() ."/framework/components/importer/xs-importer-callback.php");

        $wp_import = new xs_import();
        $wp_import->fetch_attachments = true;
        $wp_import->import($import_filepath);

        $wp_import->check();

    }
        die(); // this is required to return a proper result
}
