<?php

/*
 * Demo Importer
 * by www.katonpress.net
  */

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

/*
|--------------------------------------------------------------------------
| Demo Importer Menu Item
|--------------------------------------------------------------------------
*/
add_action('admin_menu', 'kp_demo_importer_menu');

if ( !function_exists( 'kp_demo_importer_menu' ) ) {
	
	function kp_demo_importer_menu() {
	
		add_submenu_page( 'themes.php' , 'Demo Importer' , 'Demo Importer' , 'manage_options' , 'kp_view_updater' , 'kp_view_updater' );
	
	}
	
}


/*
|--------------------------------------------------------------------------
| Demo Importer Styles
|--------------------------------------------------------------------------
*/
if ( !function_exists( 'kp_importer_admin_add_scripts' ) ) {

	function kp_importer_admin_add_scripts() {
			
		wp_enqueue_style('kp-importer', KP_THEME_DIRECTORY . '/framework/assets/css/importer.css');
		wp_enqueue_script( 'kp-importer', KP_THEME_DIRECTORY . '/framework/assets/js/importer.js' );
					
	}
	
}

if ( isset($_GET['page']) && $_GET['page'] == 'kp_view_updater' ) {	
	add_action( 'admin_enqueue_scripts', 'kp_importer_admin_add_scripts' );
}


/*
|--------------------------------------------------------------------------
| Check if wp-content is writeable for demo images
|--------------------------------------------------------------------------
*/
if ( !function_exists( 'kp_is_writable' ) ) {
	
	function kp_is_writable( $path ) {
	
		if ( $path{strlen($path)-1}=='/' ) {
			return kp_is_writable($path.uniqid(mt_rand()).'.tmp');
		}
		
		if (file_exists($path)) {
			if (!($f = @fopen($path, 'r+')))
				return false;
			fclose($f);
			return true;
		}
		
		if (!($f = @fopen($path, 'w')))
			return false;
		fclose($f);
		unlink($path);
		return true;
		
	}
	
}


/*
|--------------------------------------------------------------------------
| Demo Importer Interface
|--------------------------------------------------------------------------
*/

if ( !function_exists( 'kp_view_updater' ) ) {
	
	function kp_view_updater() { ?>
		
		<div id="kp-importer" class="wrap">
		
			<div class="icon32" id="icon-options-general"><br></div>
            <h2><?php _e( 'Theme Demo Importer' , 'KP_THEME_NAME' ); ?></h2>

            
			<?php 
			
			/*
			|--------------------------------------------------------------------------
			| Notifications
			|--------------------------------------------------------------------------
			*/
			if( file_exists( ABSPATH . 'wp-content/uploads/' ) ) {
				
				/* wp-content upload folder not writeable  */ 
				if( !kp_is_writable( ABSPATH . 'wp-content/uploads/' ) ) :
				
					echo '<div class="error"><p>';
						
						echo '<strong>' .__('Your upload folder is not writeable! The importer won\'t be able to import the placeholder images. Please check the folder permissions', 'KP_THEME_NAME').'</strong><br />';
						echo ABSPATH . 'wp-content/uploads/';
						
					echo '</p></div>';
					
				endif;
				
			
			} else {
			
				/* wp-content folder not writeable  */ 
				if( !kp_is_writable( ABSPATH . 'wp-content/uploads/' ) ) :
					
					echo '<div class="error"><p>';
					
						echo '<strong>' .__('Your wp-content folder is not writeable! The importer won\'t be able to import the placeholder images. Please check the folder permissions', 'KP_THEME_NAME').'</strong><br />';
						echo ABSPATH . 'wp-content/';
					
					echo '</p></div>';
					
				endif;
			
			}
			

			
			/* importer has been used before */
			if( get_option('kp_import_loaded') == 'active' ) :
				
				echo '<div class="error"><p>'.__('You already have imported the demo content before. Running this operation twice will result in double content!', 'KP_THEME_NAME').'</p></div>';
			
			endif;
			
			/* import was successful */
			if( isset($_GET['kpimport']) && $_GET['kpimport'] == 'success' ) : 
				
				echo '<div class="updated"><p>'.__('Import was successful, have fun!', 'KP_THEME_NAME').'</p></div>';
			
			endif; 
			
			?>
            
            <form id="kp-importer-form" method="POST" action="?page=kp_view_updater" class="form-horizontal">
            
            <div class="xml">
                <input type="radio" id="demo_skew" name="kp_demo_file" value="demo_skew" checked class="kp-choose-demo-radio">
                <label class="kp-choose-demo-img" for="demo_skew">
                    <img src="<?php echo KP_THEME_DIRECTORY; ?>/framework/assets/img/import/slant-skew.jpg" />                    
                </label>
                <h3 class="xml-name">Slant Skew</h3>
                <div class="xml-actions">
                	<a target="_blank" href="http://katonpress.net/themes/slant/skewslider/" class="button button-primary"><?php _e('Preview' , 'KP_THEME_NAME'); ?></a>
                </div>
            </div>
			
			<div class="xml">
                <input type="radio" id="demo_dining" name="kp_demo_file" value="demo_dining" checked class="kp-choose-demo-radio">
                <label class="kp-choose-demo-img" for="demo_dining">
                    <img src="<?php echo KP_THEME_DIRECTORY; ?>/framework/assets/img/import/slant-dining.jpg" />                    
                </label>
                <h3 class="xml-name">Slant Dining</h3>
                <div class="xml-actions">
                	<a target="_blank" href="http://katonpress.net/themes/slant/dining/" class="button button-primary"><?php _e('Preview' , 'KP_THEME_NAME'); ?></a>
                </div>
            </div>
			
			<div class="xml">
                <input type="radio" id="demo_ocean" name="kp_demo_file" value="demo_ocean" checked class="kp-choose-demo-radio">
                <label class="kp-choose-demo-img" for="demo_ocean">
                    <img src="<?php echo KP_THEME_DIRECTORY; ?>/framework/assets/img/import/slant-ocean.jpg" />                    
                </label>
                <h3 class="xml-name">Slant Ocean</h3>
                <div class="xml-actions">
                	<a target="_blank" href="http://katonpress.net/themes/slant/ocean/" class="button button-primary"><?php _e('Preview' , 'KP_THEME_NAME'); ?></a>
                </div>
            </div>
			
			<div class="xml">
                <input type="radio" id="demo_desert" name="kp_demo_file" value="demo_desert" checked class="kp-choose-demo-radio">
                <label class="kp-choose-demo-img" for="demo_desert">
                    <img src="<?php echo KP_THEME_DIRECTORY; ?>/framework/assets/img/import/slant-desert.jpg" />                    
                </label>
                <h3 class="xml-name">Slant Desert</h3>
                <div class="xml-actions">
                	<a target="_blank" href="http://katonpress.net/themes/slant/desert/" class="button button-primary"><?php _e('Preview' , 'KP_THEME_NAME'); ?></a>
                </div>
            </div>
			
			<div class="xml">
                <input type="radio" id="demo_autumn" name="kp_demo_file" value="demo_autumn" checked class="kp-choose-demo-radio">
                <label class="kp-choose-demo-img" for="demo_autumn">
                    <img src="<?php echo KP_THEME_DIRECTORY; ?>/framework/assets/img/import/slant-autumn.jpg" />                    
                </label>
                <h3 class="xml-name">Slant Autumn</h3>
                <div class="xml-actions">
                	<a target="_blank" href="http://katonpress.net/themes/slant/autumn/" class="button button-primary"><?php _e('Preview' , 'KP_THEME_NAME'); ?></a>
                </div>
            </div>
			
			<div class="xml">
                <input type="radio" id="demo_sky" name="kp_demo_file" value="demo_sky" checked class="kp-choose-demo-radio">
                <label class="kp-choose-demo-img" for="demo_sky">
                    <img src="<?php echo KP_THEME_DIRECTORY; ?>/framework/assets/img/import/slant-sky.jpg" />                    
                </label>
                <h3 class="xml-name">Slant Sky</h3>
                <div class="xml-actions">
                	<a target="_blank" href="http://katonpress.net/themes/slant/sky/" class="button button-primary"><?php _e('Preview' , 'KP_THEME_NAME'); ?></a>
                </div>
            </div>
			
			<div class="xml">
                <input type="radio" id="demo_soon" name="kp_demo_file" value="demo_soon" checked class="kp-choose-demo-radio">
                <label class="kp-choose-demo-img" for="demo_soon">
                    <img src="<?php echo KP_THEME_DIRECTORY; ?>/framework/assets/img/import/slant-soon.jpg" />                    
                </label>
                <h3 class="xml-name">Slant Comming soon</h3>
                <div class="xml-actions">
                	<a target="_blank" href="http://katonpress.net/themes/slant/commingsoon/" class="button button-primary"><?php _e('Preview' , 'KP_THEME_NAME'); ?></a>
                </div>
            </div>
            
            <div class="clear"></div>

			<div class="kp-import-options">
			
			<table class="form-table">
            	<tbody>
				
				    <tr valign="top">
                    	
                        <th scope="row">
							<?php _e( 'Import Theme Options?' , 'KP_THEME_NAME' ); ?>
                        </th>
                      	
                        <td>
                            <input type="checkbox" value="yes" id="kp-import-options" name="kp-import-options">
                        </td>                       
                    
                    </tr> 
                                        
            	</tbody>
            </table>
            
            </div>
            
            <div class="clear"></div>
			
			<div class="kp-import-notes">
			<table class="form-table">
            	<tbody>
			 
                    <tr valign="top">
                    	
                        <th scope="row">
							<?php _e('Important Notes:' , 'KP_THEME_NAME'); ?>
                        </th>
                      	
                        <td>
                            <ol>
                                <li><?php _e('We recommend to run this importer on a clean WordPress installation.' , 'KP_THEME_NAME'); ?></li>
                                <li><?php _e('The Demo Importer will not import premium images used in live demos, will include free images instead' , 'KP_THEME_NAME'); ?></li>
                                <li><?php _e('Do not run the importer multiple times , this will result in double content.' , 'KP_THEME_NAME'); ?></li>
								<li><?php _e('To reset your installation we can recommend this plugin here:' , 'KP_THEME_NAME'); ?> <a href="http://wordpress.org/plugins/wordpress-database-reset/">Wordpress Database Reset</a></li>
                            </ol>
                        </td>                       
                    
                    </tr>  
                                        
            	</tbody>
            </table>
			
			</div>
			
			<div class="clear"></div>
            
            <div class="kp-import-button">
                
                <input type="hidden" name="kp_import_demo_content" value="true" />
                <input type="submit" value="<?php _e( 'Import' , 'KP_THEME_NAME' ); ?>" class="button button-primary" id="submit" name="submit">
                
            </div>
            
            </form>
		
		</div>
		
	<?php }
	
}

/*
|--------------------------------------------------------------------------
| Demo Importer
|--------------------------------------------------------------------------
*/
add_action( 'admin_init', 'kp_demo_importer' );

if ( !function_exists( 'kp_demo_importer' ) ) {

	function kp_demo_importer() {
		
		global $wpdb;
		
		/* add option flag to wordpress */
		add_option('kp_import_loaded');
		
		/* security array for valid filenames */
		$kp_recognized_file_names = apply_filters( 'kp_recognized_file_names', array( 
		  'demo_skew', 'demo_dining', 'demo_ocean', 'demo_desert', 'demo_autumn', 'demo_sky', 'demo_soon'
		));
			
		if ( current_user_can( 'manage_options' ) && isset( $_POST['kp_import_demo_content'] ) && !empty( $_POST['kp_demo_file'] ) ) {

			if ( !defined('WP_LOAD_IMPORTERS') ) define('WP_LOAD_IMPORTERS', true);
			
			if ( ! class_exists( 'WP_Importer' ) ) {
				$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
				if ( file_exists( $class_wp_importer ) ) {
					include $class_wp_importer;
				}
			}

			if ( ! class_exists('KP_WP_Import') ) { 
				$class_wp_import = KP_THEME_DOCUMENT_ROOT . '/framework/assets/plugins/wordpress-importer.php';
				if ( file_exists( $class_wp_import ) ) {
					include $class_wp_import;
				}
			}	
			
			if ( class_exists( 'WP_Importer' ) && class_exists( 'KP_WP_Import' ) ) {
				
				/*
				|--------------------------------------------------------------------------
				| Import choosen XML
				|--------------------------------------------------------------------------
				*/
				
				$importer = new KP_WP_Import();
				
				$demo_file = sanitize_file_name($_POST['kp_demo_file']);
				$theme_xml = KP_THEME_DOCUMENT_ROOT . '/framework/assets/xml/' . $demo_file . '.xml.gz';
								
				if ( file_exists( $class_wp_importer ) && in_array( $demo_file , $kp_recognized_file_names) ) {
										
					$importer->fetch_attachments = true;
					ob_start();
					$importer->import($theme_xml);
					ob_end_clean();					
					
				} else {
					
					wp_redirect( admin_url( 'themes.php?page=kp_view_updater&utimport=failed' ) );
					
				}
				
				/*
				|--------------------------------------------------------------------------
				| Set Primary Menu Navigation
				|--------------------------------------------------------------------------
				*/
												
				$locations = get_theme_mod( 'nav_menu_locations' ); 
				$menus = wp_get_nav_menus(); 
				
				if( is_array($menus) ) {
					foreach($menus as $menu) { // assign menus to theme locations
						                                               
						$locations['main_nav'] = $menu->term_id;

					}
				}
				
				set_theme_mod( 'nav_menu_locations', $locations );
				
				/*
				|--------------------------------------------------------------------------
				| Set Reading Options
				|--------------------------------------------------------------------------
				*/
				
				$homepage 	= get_page_by_title( 'Home Page' );
				$posts_page = get_page_by_title( 'Blog' );
				
                
				if( isset($homepage->ID) && isset($posts_page->ID) ) {
					update_option('show_on_front', 'page');
					update_option('page_on_front',  $homepage->ID); // Home Page
					update_option('page_for_posts', $posts_page->ID); // Blog Page
				}
				
				
				/*
                |--------------------------------------------------------------------------
                | Update Theme Options
                |--------------------------------------------------------------------------
                */
				
				
				if( isset( $_POST['kp-import-options'] ) && $_POST['kp-import-options'] == 'yes' ) {
				if ( class_exists( 'ReduxFramework' ) && file_exists( KP_THEME_DOCUMENT_ROOT . '/framework/assets/xml/options/' . $demo_file. '_option.json' ) ) {
                    global $reduxConfig;
                    $option_file = KP_THEME_DOCUMENT_ROOT . '/framework/assets/xml/options/' . $demo_file. '_option.json';
                    $file_contents = file_get_contents( $option_file );
                    $options = json_decode($file_contents, true);
                    
                    foreach ($options as $key => $value) {
                            $reduxConfig->ReduxFramework->set($key, $value);
                    }
                    //$redux = ReduxFrameworkInstances::get_instance('slant_options');
                    //$redux->set_options($options);
                    
				   }
				  }

				

				
				/*
				|--------------------------------------------------------------------------
				| Update Import Flag
				|--------------------------------------------------------------------------
				*/
				update_option('kp_import_loaded', 'active');
				
				/*
				|--------------------------------------------------------------------------
				| Redirect User
				|--------------------------------------------------------------------------
				*/
				wp_redirect( admin_url( 'themes.php?page=kp_view_updater&kpimport=success' ) );
								
				
			}
		
		}
		
	}

} ?>