<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * RT-Theme Tools
 *
 * Create theme tools
 *
 * @class 		RT_Tools
 * @version		1.0
 * @author 		RT-Themes
 */

class RT_Tools
{

	/**
	 * Capability
	 */
	public $capability = "edit_theme_options";

	/**
	 * Construct
	 */
	public function __construct()
	{

		//Additional customizer settings
		add_action('init', array(&$this, 'customizer_hooks'));  			
		
		
		//Rest of the class is only for admin area 
		if( ! is_admin() ){
			return;
		}

		//load tools
		add_action('registered_taxonomy', array(&$this, 'lood_tools'));  	
	}


	/**
	 * Load Tools
	 * @return array $skin_list
	 */
	public function lood_tools() {

		//if it is not an RT-Theme or this one is not belong to the current RT-Theme
		if ( RT_THEMENAME != RT_EXTENSIONS_PLUGIN_FOR || ! defined("RT_FRAMEWOK") ) { 	
			return ;	
		}

		//init
		add_action('admin_menu', array(&$this, 'add_menu_item'),1);   
		add_action('admin_init', array(&$this, 'demo_import'));  
		add_action('admin_init', array(&$this, 'export_settings'));  
		add_action('admin_init', array(&$this, 'import_settings'));  
		add_action('admin_init', array(&$this, 'custom_css'));  
		add_action('admin_init', array(&$this, 'reset_settings')); 		
		add_action('admin_init', array(&$this, 'save_custom_fonts'));  
		add_action('init', array(&$this, 'save_utilities'),1);  	
		add_filter( "wp_importframework_post_meta",  array(&$this, 'cf7_mail_fix'));

		//load skin changer
		add_action('customize_controls_print_footer_scripts', array( &$this, 'skin_selector'));   

		//skin loader actions
		add_action('wp_ajax_rt_ajax_skin_loader', array( &$this,'load_skins'));
		add_action('wp_ajax_nopriv_rt_ajax_skin_loader', array( &$this,'load_skins'));		
		add_action('wp_ajax_rt_ajax_get_skin_data', array( &$this,'get_skin_data_json'));
		add_action('wp_ajax_nopriv_rt_ajax_get_skin_data', array( &$this,'get_skin_data_json'));		

	}


	/**
	 * Get Skin List
	 * @return array $skin_list
	 */
	public function get_skin_list() {
		
		/**
		 * Skin list
		 */
		$skin_list = array();

		
		// to do : get user drafts / skins

			
		//get theme skins
		$theme_skins = array();

		$theme_skins[] = array(
							"type" => "theme",
							"id" => "1",
							"name" => "Default Skin"
						);

		$theme_skins[] = array(
							"type" => "theme",
							"id" => "2",
							"name" => "Skin 2"
						);

		$theme_skins[] = array(
							"type" => "theme",
							"id" => "3",
							"name" => "Skin 3"
						);

		return $theme_skins;
	}


	/**
	 * Get Skin Data Json
	 * @return json data $json
	 */
	public function get_skin_data_json() {
		check_ajax_referer( 'rt_admin_nonce' );

		//check the current user access 
		if ( ! current_user_can( $this->capability ) ){
			return;
		}

		$new_data = array(); 

		if(isset( $_POST["skin-id"] )){
			$skin_id = intval( sanitize_text_field( $_POST["skin-id"] ) );
		}
  		
		require_once( RT_EXTENSIONS_PATH . '/inc/skins/'.$skin_id.'.php' );
		$skin_data = unserialize( base64_decode( $skin_data ) );
 
 		foreach ( RTFramework_Customize_Panel()->skin_options as $option_id ) {

 			if( ! isset( $skin_data[ $option_id ] )){
 				$new_data[ $option_id ] = "";
 				continue;
 			}

 			$new_data[ $option_id ] = str_replace("{{theme-image-directory}}", get_template_directory_uri()."/images/", $skin_data[ $option_id ] );
 		}

 		echo( json_encode($new_data) );

		die;
	}


	/**
	 * Skin & Draft Selector
	 */
	public function load_skins() {
		check_ajax_referer( 'rt_admin_nonce' );

		//check the current user access 
		if ( ! current_user_can( $this->capability ) ){
			return;
		}

		$skins = $this->get_skin_list();

		foreach ($skins as $k => $skin) {
				?>
				
					<div class="skin" data-skin-id="<?php echo $skin["id"]; ?>" data-skin-type="<?php echo $skin["type"]; ?>">
						<img class="skin-image" src="<?php echo RT_THEMEADMINURI; ?>/images/skins/<?php echo $skin["id"]; ?>.png">
						<p><?php echo $skin["name"]; ?></p>
					</div>

				<?php
				}

			die;
	}


	/**
	 * Skin & Draft Selector
	 */
	public function skin_selector() {

		echo '
		
			<div id="available-rt-skins">

				<img src="images/spinner.gif" class="skins-loading-spinner">
			
			</div><!-- #available-rt-skins -->

		';
	}

	/**
	 * Customizer Related Hooks
	 */
	public function customizer_hooks(){
		add_filter( "rt_customizer_options",  array(&$this, 'additional_theme_settings') );
	}


 	/**
	 * Additional theme settings
	 */
	public function additional_theme_settings( $options )
	{
		//disabled
		return;

		//Sking Selector
		$skin_selector = array(
									'id'       => 'skins_selector_section',
									'title'    => esc_html_x("Pre-Made Skins", 'Admin Panel','businesslounge'), 
									'controls' => array( 

														array(
															'id'        => 'rt_skin_selector_html',
															'label'     => esc_html_x("Skin Selector",'Admin Panel','businesslounge'),
															'type'      => 'rt_content', 
															"transport" => "postMessage",
															'description' => '<p>'.esc_html_x("Select a pre-made skin to use. Once you select a skin from the list, it's settings will be applied to the live preview only and won't affect your website unliness you save & publish the changes.",'Admin Panel','businesslounge').'</p><span id="rt-skin-selector" class="button-secondary rt-skin-selector" tabindex="0">'.esc_html_x('Select a Skin','Admin Panel','businesslounge').'</span>',
														), 

												),
								);

		array_unshift( $options["rt_color_schemas"]["sections"], $skin_selector );

		return $options;
	}


	/**
	 * Add admin menu item
	 */
	public function add_menu_item()
	{


		global $RTFrameworkManager;
 
		add_menu_page( esc_html_x('BusinessLounge','Admin Panel','businesslounge'), esc_html_x('BusinessLounge','Admin Panel','businesslounge'), $this->capability, 'rt_framework_welcome', array(&$RTFrameworkManager,"welcome_page"), NULL, NULL, 61 );
		add_submenu_page( 'rt_framework_welcome', esc_html_x('Customize','Admin Panel','businesslounge'), esc_html_x('Customize','Admin Panel','businesslounge'), $this->capability , 'customize.php' );	
		add_submenu_page( 'rt_framework_welcome', esc_html_x('Utilities','Admin Panel','businesslounge'), esc_html_x('Utilities','Admin Panel','businesslounge'), $this->capability, 'rt_utilities', array(&$this,"utilities_page") );
		add_submenu_page( 'rt_framework_welcome', esc_html_x('Plugins','Admin Panel','businesslounge'), esc_html_x('Plugins','Admin Panel','businesslounge'), $this->capability , 'rt_framework_plugins', array(&$RTFrameworkManager,"plugins_page") );			
		add_submenu_page( 'rt_framework_welcome', esc_html_x('Customizations','Admin Panel','businesslounge'), esc_html_x('Customizations','Admin Panel','businesslounge'), $this->capability, 'rt_framework_customizations', array(&$this,"customizations_page") );	
		add_submenu_page( 'rt_framework_welcome', esc_html_x('Custom Fonts','Admin Panel','businesslounge'), esc_html_x('Custom Fonts','Admin Panel','businesslounge'), $this->capability, 'rt_custom_fonts', array(&$this,"custom_fonts_page") ); 	
		add_submenu_page( 'rt_framework_welcome', esc_html_x('Custom CSS','Admin Panel','businesslounge'), esc_html_x('Custom CSS','Admin Panel','businesslounge'), $this->capability, 'rt_custom_css', array(&$this,"custom_css_page") );		
		add_submenu_page( 'rt_framework_welcome', esc_html_x('Demo Import','Admin Panel','businesslounge'), esc_html_x('Demo Import','Admin Panel','businesslounge'), $this->capability, 'rt_demo_import', array(&$this,"demo_import_page") );		
	   
		//chanage the title of the first page in the sub menu
		global $submenu;

		if ( isset( $submenu['rt_framework_welcome'] ) ) {
			$submenu['rt_framework_welcome'][0][0] =  esc_html_x('Welcome','Admin Panel','businesslounge');
		}
	}

	/**
	 * Add Admin Notices
	 */
	public function add_notices( $message = "", $type = "error" )
	{
		global $RTFrameworkManager;
		
		if( empty( $type ) ){
			$type = "updated";
		}

		$RTFrameworkManager->nags['tools'] = ['output' => sprintf('<p>%s</p>',$message), 'type' => $type] ;

	}

	/**
	 * Utilities
	 */
	public function utilities_page()
	{	
		global $RTFrameworkManager;

		$title = esc_html_x('Utilities','Admin Panel','businesslounge');

		// about the page
		$about = esc_html_x('Several utility tools related to performance or functionality.','Admin Panel','businesslounge'); 

		// admin header
		$RTFrameworkManager->admin_header( $title, $about );
	?>

		<div id="rt_utilities_page">
			<form action="<?php echo wp_nonce_url(admin_url('admin.php?page=rt_utilities'),"rt_utilities_page","rt_framework"); ?>" method="post" enctype="multipart/form-data">
					<input type="hidden" value="save_utilities" name="action">

					<h3><?php printf( _x('Performance','Admin Panel','businesslounge'), RT_THEMENAME ) ;?></h3>

					<p><?php _ex('Speed up your website by using minified and combined versions of the css/js files that used for the theme. ','Admin Panel', 'businesslounge');?></p>

					<table class="form-table">
						<tbody>

							<tr>
								<th scope="row">
									<label><?php _ex('CSS Files','Admin Panel','businesslounge') ?></label>
								</th>
								<td>
									<?php
										$businesslounge_optimize_css = get_theme_mod("businesslounge_optimize_css");
									?>
									<label for="rt_utility_vals[businesslounge_optimize_css]">
										<input type="checkbox" id="rt_utility_vals[businesslounge_optimize_css]" name="rt_utility_vals[businesslounge_optimize_css]" <?php echo $businesslounge_optimize_css == "on" ? "checked" : "" ?>/>
										<?php _ex('Use optimized CSS files of the theme','Admin Panel','businesslounge') ?>
									</label>
					 	
								</td>
							</tr>


							<tr>
								<th scope="row">
									<label><?php _ex('JS Files','Admin Panel','businesslounge') ?></label>
								</th>
								<td>
									<?php
										$businesslounge_optimize_js = get_theme_mod("businesslounge_optimize_js");
									?>
									<label for="rt_utility_vals[businesslounge_optimize_js]">
										<input type="checkbox" id="rt_utility_vals[businesslounge_optimize_js]" name="rt_utility_vals[businesslounge_optimize_js]" <?php echo $businesslounge_optimize_js == "on" ? "checked" : "" ?>/>
										<?php _ex('Use optimized JS files of the theme','Admin Panel','businesslounge') ?>
									</label>
					 	
								</td>
							</tr>

						</tbody>
					</table>

					<hr />	

					<h3><?php printf( _x('Special Pages','Admin Panel','businesslounge'), RT_THEMENAME ) ;?></h3>
					<table class="form-table">
						<tbody>

							<tr>
								<th scope="row">
									<label><?php _ex('404 Page','Admin Panel','businesslounge') ?></label>
								</th>
								<td>
									<select name="rt_utility_vals[businesslounge_404_page]" autocomplete="off">
										<?php
											$pages = RTFramework::rt_get_pages();
											$stored_404 = get_theme_mod("businesslounge_404_page");
 
											printf('<option value="">%s</option>', _x('Select a page','Admin Panel','businesslounge') );

											foreach ($pages as $id => $page) {
												printf('<option value="%s"%s>%s</option>', $id, $stored_404 == $id ? " selected" : "", $page);
											}
										?>
									</select>
									<p class="description"><?php _ex('Select a page to be used as a 404 page.','Admin Panel','businesslounge') ?></p>										
								</td>
							</tr> 	

							<tr>
								<th scope="row">
									<?php _ex('Maintenance Mode','Admin Panel','businesslounge') ?>
								</th>
								<td>

									<fieldset>

										<?php
											$businesslounge_maintenance_mode = get_theme_mod("businesslounge_maintenance_mode");
										?>									
										<label for="rt_utility_vals[businesslounge_maintenance_mode]">
											<input type="checkbox" id="rt_utility_vals[businesslounge_maintenance_mode]" name="rt_utility_vals[businesslounge_maintenance_mode]" <?php echo $businesslounge_maintenance_mode == "on" ? "checked" : "" ?>/>
											<?php _ex('Enable/disable the maintenance mode.','Admin Panel','businesslounge') ?>
										</label>
										
										<p class="description"><?php _ex('When this option enabled, all your front-end URLs will be redirected to the selected page below. 
										The redirection will be ignored if the user role is "Administrator". 
										So, you need to sign out to see the changes and be sure all caches have been cleared if you have any caching plugin installed.','Admin Panel','businesslounge') ?></p>
										
										<br />

										<label for="rt_utility_vals[businesslounge_maintenance_page]">
											<select name="rt_utility_vals[businesslounge_maintenance_page]" autocomplete="off">
												<?php
													$pages = RTFramework::rt_get_pages();
													$stored_404 = get_theme_mod("businesslounge_maintenance_page");

													printf('<option value="">%s</option>', _x('Select a page','Admin Panel','businesslounge') );

													foreach ($pages as $id => $page) {
														printf('<option value="%s"%s>%s</option>', $id, $stored_404 == $id ? " selected" : "", $page);
													}
												?>
											</select>
										</label>
										<p class="description"><?php _ex('Select a page to be used as a Maintenance or Coming Soon page.','Admin Panel','businesslounge') ?></p>										

									</fieldset>
								</td>
							</tr> 	 										
						</tbody>
					</table>

					<hr />	

					<h3><?php printf( _x('Custom Posts','Admin Panel','businesslounge'), RT_THEMENAME ) ;?></h3>
					<table class="form-table">
						<tbody>

							<tr>
								<th scope="row">
									<label><?php _ex('Portfolio','Admin Panel','businesslounge') ?></label>
								</th>
								<td>
									<?php
										$is_portfolio_active = get_theme_mod("is_portfolio_active", true);
									?>
									<label for="rt_utility_vals[is_portfolio_active]">
										<input type="checkbox" id="rt_utility_vals[is_portfolio_active]" name="rt_utility_vals[is_portfolio_active]" <?php echo $is_portfolio_active == "on" ? "checked" : "" ?>/>
										<?php _ex('Enable/disable the portfolio custom post type.','Admin Panel','businesslounge') ?>
									</label>					 
								</td>
							</tr>

							<tr>
								<th scope="row">
									<label><?php _ex('Team','Admin Panel','businesslounge') ?></label>
								</th>
								<td>
									<?php
										$is_team_active = get_theme_mod("is_team_active", true);
									?>
									<label for="rt_utility_vals[is_team_active]">
										<input type="checkbox" id="rt_utility_vals[is_team_active]" name="rt_utility_vals[is_team_active]" <?php echo $is_team_active == "on" ? "checked" : "" ?>/>
										<?php _ex('Enable/disable the team custom post type.','Admin Panel','businesslounge') ?>
									</label>					 
								</td>
							</tr>

							<tr>
								<th scope="row">
									<label><?php _ex('Testimonials','Admin Panel','businesslounge') ?></label>
								</th>
								<td>
									<?php
										$is_testimonials_active = get_theme_mod("is_testimonials_active", true);
									?>
									<label for="rt_utility_vals[is_testimonials_active]">
										<input type="checkbox" id="rt_utility_vals[is_testimonials_active]" name="rt_utility_vals[is_testimonials_active]" <?php echo $is_testimonials_active == "on" ? "checked" : "" ?>/>
										<?php _ex('Enable/disable the team testimonials post type.','Admin Panel','businesslounge') ?>
									</label>					 
								</td>
							</tr>
						</tbody>
					</table>

					<hr />	
	 		<?php echo submit_button();?>				
	 		</form>
		</div>

		<?php
		$RTFrameworkManager->admin_footer();
	}

	/**
	 * Save Utilities
	 */
	public function save_utilities()
	{

		//check the current user access 
		if ( ! current_user_can( $this->capability ) ){
			return ;
		}

		//check the nonce url
		if ( !isset($_GET['rt_framework'] ) || ! wp_verify_nonce($_GET['rt_framework'], 'rt_utilities_page') ){
			return ;
		}

		//check
		if( ! isset( $_GET['page'] ) || ! isset( $_POST['action'] ) || ! isset( $_POST['rt_utility_vals'] ) ){
			return ;
		}

		if( $_GET['page'] != "rt_utilities" || $_POST['action'] != "save_utilities" ){
			return ;
		}

		$post_values = isset($_POST['rt_utility_vals']) ? $_POST['rt_utility_vals'] : "";


		if( empty( $post_values ) ){
			return;
		}

		foreach ( RTFramework_Customize_Panel()->utility_options as $option_name => $value ) {			
			set_theme_mod( $option_name, isset($post_values[$option_name]) ? $post_values[$option_name]: "" );			
		}

		$this->add_notices( esc_html_x("Settings updated successfully.",'Admin Panel','businesslounge') ,"updated" );
	}


	/**
	 * Demo Import settings page
	 */
	public function demo_import_page()
	{	
		//check the current user access 
		if ( ! current_user_can( $this->capability ) ){
			return;
		}

		global $RTFrameworkManager;

		// about the page
		$title = sprintf( esc_html_x('Demo Import','Admin Panel','businesslounge'), RT_THEMENAME );		
		$about = sprintf( esc_html_x('This demo importer tool will help you to import a demo website of  %1$s theme to let you have a quick start or learn how it is done. Please carefully read the following remarks before start.','Admin Panel','businesslounge'), '<strong>'.RT_THEMENAME.'</strong>');

		// admin header
		$RTFrameworkManager->admin_header( $title, $about );

		if( ! $RTFrameworkManager->license ){
			$RTFrameworkManager->admin_footer();
			return;
		}		

			// /**
			//  * 
			//  * Elementor 2.9.6 has a conflict with WordPress Importer 0.7 
			//  * https://github.com/elementor/elementor/issues/10774
			//  * 
			//  */

			// $get_plugins = get_plugins();
			// $wp_importer_plugin_version = isset( $get_plugins['wordpress-importer/wordpress-importer.php'] ) ? $get_plugins['wordpress-importer/wordpress-importer.php']['Version'] : false;
			
			// if( is_plugin_active('wordpress-importer/wordpress-importer.php') && $wp_importer_plugin_version && version_compare( $wp_importer_plugin_version, "0.7" , ">=" ) ){
	
			// 	echo '
			// 		<br /><br />
			// 		<h1>Notice!</h1>
			// 		<p style="font-size:1.4em;">Please disable the <strong>WordPress Importer</strong> plugin via the <strong>Plugins</strong> page to continue using this importer.</p>
			// 	';

			// 	return;
			// }



		echo '<div id="rt_demo_import_settings">';

		$plugin_messages = "";
		if ( ! class_exists( 'Woocommerce' ) ) {
			$plugin_messages .='
					<li class="rt-panel-icon-attention" style="color:red;">
						'._x('If you are planning to use WooCommerce, install and/or activate it first to import the plugin related contents.','Admin Panel','businesslounge').'
						<a href="'.admin_url( 'admin.php?page=rt_framework_plugins' ).'">'._x('Install','Admin Panel','businesslounge').' & '._x('activate','Admin Panel','businesslounge').'</a>
					</li>				
			';
		}

		if ( ! class_exists( "WPCF7" ) ){			
			$plugin_messages .='
					<li class="rt-panel-icon-attention" style="color:red;">
						'._x('If you are planning to use Contact Form 7, install and/or activate it first to import the plugin related contents.','Admin Panel','businesslounge').'
						<a href="'.admin_url( 'themes.php?page=rt_framework_plugins').'">'._x('Install','Admin Panel','businesslounge').' & '._x('activate','Admin Panel','businesslounge').'</a>
					</li>						
			';
		}

 		if ( ! class_exists( "RevSliderFront" ) ){
 			$revslider_link = '';
			$plugin_messages .='
					<li class="rt-panel-icon-attention" style="color:red;">
						'._x('If you are planning to use Revolution Slider, install and/or activate it first to import the demo sliders.','Admin Panel','businesslounge').'
						<a href="'.admin_url( 'themes.php?page=rt_framework_plugins').'">'._x('Install','Admin Panel','businesslounge').' & '._x('activate','Admin Panel','businesslounge').'</a>
					</li>						
			';
		}

		$is_elementor_installed = class_exists("Elementor\Plugin") ? "true" : "false";
		$is_vc_installed = class_exists("Vc_Manager") ? "true" : "false"; 
		$is_revslider_installed = class_exists("RevSliderFront") ? "true" : "false"; 

 		echo '
 			<div class="wrap" id="rt_demo_import_settings">					
				
				<div class="import_desc">
					<h3 class="important">'. _x('STEP 1: Notes','Admin Panel','businesslounge') .'</h3>
					<ul>
						<li class="rt-panel-icon-check">
							'._x('It is always recommended to use this tool with a clean install to get a perfect demo clone. The importer will clean the navigations and replace them with the demo navigations.','Admin Panel','businesslounge').'
						</li>						
						<li class="rt-panel-icon-check">
							<strong>'._x('Do not close this window before the import process completed.','Admin Panel','businesslounge').'</strong>
						</li>
						<li class="rt-panel-icon-check">
							'._x('Select your home page from Settings -> Reading -> Front page displays -> Front page, after the demo contents installed.','Admin Panel','businesslounge').'
						</li>
						'.$plugin_messages.'					
					</ul>
				</div>
 
				<form class="wp-upload-form" method="get">

					<h3 class="important">'. _x('STEP 2: Import','Admin Panel','businesslounge') .'</h3>

					<!--	<img class="demo-image"  src="'.RT_THEMEADMINURI .'/images/skins/1.png" data-base_url="'.RT_THEMEADMINURI .'" /> -->

					<table>
						<tr>
							<td class="parts">
								<p>

									<input type="hidden" value="import" name="action">

									<label for="upload">'._x('Select parts','Admin Panel','businesslounge').':</label>

									<select name="parts" id="rt-demo-parts" autocomplete="off">
										<option value="">'._x('Select','Admin Panel','businesslounge').'</option>
										<option value="all">'._x('All','Admin Panel','businesslounge').'</option>
										<option value="contents">'._x('Contents & Media','Admin Panel','businesslounge').'</option>
										<option value="widgets">'._x('Widgets','Admin Panel','businesslounge').'</option>
										<option value="options">'._x('Theme Options','Admin Panel','businesslounge').'</option>
										<option value="revslider" data-active="'.$is_revslider_installed.'">'._x('Revolution Slider Samples','Admin Panel','businesslounge').'</option>
									</select>

									<input type="hidden" value="import" name="action">
								</p>
							</td>

							<td class="builder">
								<p>
									<input type="hidden" value="import" name="action">

									<label for="upload">'._x('Select a page builder','Admin Panel','businesslounge').':</label>

									<select name="parts" id="rt-builder-option" autocomplete="off">
										<option value="">'._x('Select','Admin Panel','businesslounge').'</option>
										<option value="elementor" data-active="'.$is_elementor_installed.'">'._x('Elementor','Admin Panel','businesslounge').'</option>
										<option value="vc" data-active="'.$is_vc_installed.'">'._x('Visual Composer','Admin Panel','businesslounge').'</option> 
									</select>

									<input type="hidden" value="import" name="action">
								</p>
							</td>

						</tr>
					</table>

					<div id="contents-result" class="">
						<p>
						<span class="animate-spin">'._x('Importing Contents:','Admin Panel','businesslounge').' <strong></strong></span>
						<span class="ok">'._x('Contents have been imported','Admin Panel','businesslounge').'</span>
						<span class="failed">'._x('Error','Admin Panel','businesslounge').'</span>
						<a href="#" class="see_logs">see the logs</a></p>
						<p class="logs"></p>
					</div>
					<div id="widgets-result" class="">
						<p>
						<span class="animate-spin">'._x('Importing widgets','Admin Panel','businesslounge').'</span>
						<span class="ok">'._x('Widgets have been imported','Admin Panel','businesslounge').'</span>
						<span class="failed">'._x('Error','Admin Panel','businesslounge').'</span>
						<a href="#" class="see_logs">see the logs</a></p>
						<p class="logs"></p>
					</div>
					<div id="options-result" class="">
						<p>
						<span class="animate-spin">'._x('Importing theme options','Admin Panel','businesslounge').'</span>
						<span class="ok">'._x('Theme options have been imported','Admin Panel','businesslounge').'</span>
						<span class="failed">'._x('Error','Admin Panel','businesslounge').'</span>
						<a href="#" class="see_logs">see the logs</a></p>
						<p class="logs"></p>						
					</div>

					<div id="revslider-result" class="">
						<p>
						<span class="animate-spin">'._x('Importing Revolution Slider samples','Admin Panel','businesslounge').'</span>
						<span class="ok">'._x('Revolution Slider samples have been imported','Admin Panel','businesslounge').'</span>
						<span class="failed">'._x('Error','Admin Panel','businesslounge').'</span>
						<a href="#" class="see_logs">see the logs</a></p>
						<p class="logs"></p>						
					</div>

					<p class="submit"><input id="rt-demo-import-button" type="button" value="'._x('Start Importing','Admin Panel','businesslounge').'" class="button button-primary" name="submit"></p>

				</form>
			</div>
		';
	
		echo '</div>';
		$RTFrameworkManager->admin_footer();
	}

	/**
	 * Demo Import Process
	 */
	public function demo_import()
	{	

		//check the current user access 
		if ( ! current_user_can( $this->capability ) ){
			return;
		}
		
		//check
		if( ! isset( $_GET['action'] ) || ! isset( $_GET['part'] ) ){
			return;
		}

		if( $_GET['action'] != "demo_import" ){
			return;
		}

		//file server		
		$file_server = "http://rtthemes.com/demo-imports/businesslounge/";

		//file locations
		$file_location = "internal";		


		define('WP_LOAD_IMPORTERS', true);

		//demo
		$demo = isset( $_GET['demo'] ) && ! empty( $_GET['demo'] ) ? intval( $_GET['demo'] ) : 1;
			
		//contens & media
		if( $_GET['part'] == "contents" ){	

			//step
			if( ! isset( $_GET['step'] ) || ! is_numeric( $_GET['step'] ) ){
				return;
			}
			$step = intval( sanitize_text_field( $_GET['step'] ) );

			//builder 
			$builder = isset( $_GET['builder'] ) ? sanitize_key( $_GET['builder'] )."-contents/" : "";

			// Load Importer API
			if ( ! function_exists( 'wordpress_importer_init' ) ) { 
				include( RT_EXTENSIONS_PATH ."/inc/imports/wordpress-importer/wordpress-importer.php" );
			}else{
				/** WordPress Import Administration 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;
				}

				/** Functions missing in older WordPress versions. */
				require_once ABSPATH .'/wp-content/plugins/wordpress-importer/compat.php';

				/** WXR_Parser class */
				require_once ABSPATH .'/wp-content/plugins/wordpress-importer/parsers/class-wxr-parser.php';

				/** WXR_Parser_SimpleXML class */
				require_once ABSPATH .'/wp-content/plugins/wordpress-importer/parsers/class-wxr-parser-simplexml.php';

				/** WXR_Parser_XML class */
				require_once ABSPATH .'/wp-content/plugins/wordpress-importer/parsers/class-wxr-parser-xml.php';

				/** WXR_Parser_Regex class */
				require_once ABSPATH .'/wp-content/plugins/wordpress-importer/parsers/class-wxr-parser-regex.php';

				/** WP_Import class */
				require_once ABSPATH .'/wp-content/plugins/wordpress-importer/class-wp-import.php';

				load_plugin_textdomain( 'wordpress-importer' );
			}

			if ( ! class_exists( 'WP_Importer' ) ) {
				echo "{{importer_error}}";
				_ex("ERROR: IMPORTER COULD NOT FOUND",'Admin Panel','businesslounge');
				die();
			}

			if( $step == 1 ){
				//delete menus
				wp_delete_nav_menu( esc_html_x( 'Main Navigation' , 'Admin Panel','businesslounge') );
				wp_delete_nav_menu( esc_html_x( 'Footer Navigation' , 'Admin Panel','businesslounge') );
				wp_delete_nav_menu( esc_html_x( 'Side Panel Navigation' , 'Admin Panel','businesslounge') );
				wp_delete_nav_menu( esc_html_x( 'Mobile Navigation' , 'Admin Panel','businesslounge') );			
			}
			
			//read the file			
			if( $file_location == "external" ){
				$file_url = $file_server ."/demo-{$demo}/{$builder}contents.xml";
				$file_name = "contents_{$step}.xml";
				
				$get_file = $this->get_external_content( "dir", $file_url , $file_name );

				if( $get_file["error"] ){
					echo $get_file["error_message"];
					die();
				}else{
					$content_xml = $get_file["content"];	
				}				

			}else{
				$content_xml = RT_EXTENSIONS_PATH ."/inc/imports/demos/demo-{$demo}/{$builder}contents.xml";	
			}

			echo "----- STEP " . esc_html( $step ) . " -----\n\n<br />";

			if( ! is_file( $content_xml ) ) {
				echo "{{importer_error}}";
				echo sprintf( _x("The dummy content file is not available or could not be read in <pre>%s/inc/imports/demos/.</pre>", 'Admin Panel','businesslounge' ), RT_EXTENSIONS_PATH );
				die();
			} else {
				$wp_import = new WP_Import();

				// fetch attachments
 				$wp_import->fetch_attachments = true; 

				// assign to current user
				$current_user = wp_get_current_user();
				set_time_limit(0);

				add_filter( 'import_post_meta_key', array( $wp_import, 'is_valid_meta_key' ) );
				add_filter( 'http_request_timeout', array( &$wp_import, 'bump_request_timeout' ) );
				
				//import start
				$import_data = $wp_import->parse( $content_xml );
		
				if ( is_wp_error( $import_data ) ) {
					echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
					echo esc_html( $import_data->get_error_message() ) . '</p>';
					$wp_import->footer();
					die();
				}
		
				$wp_import->version = $import_data['version'];
				$wp_import->posts = $import_data['posts'];
				$wp_import->terms = $import_data['terms'];
				$wp_import->categories = $import_data['categories'];
				$wp_import->tags = $import_data['tags'];
				$wp_import->base_url = esc_url( $import_data['base_url'] );
		
				wp_defer_term_counting( true );
				wp_defer_comment_counting( true );
		
				do_action( 'import_start' );
				
				$wp_import->get_author_mapping();


				// prepare posts
				$attachments = [];				
				foreach( $wp_import->posts as $_post => $post_to_import ){

					// $wp_import->posts[$_post]["post_author"] = $current_user->user_login; 
					if( $post_to_import["post_type"] == "attachment" ){
						$attachments[] = $post_to_import;
					}
 
					if( $step == 10 && $post_to_import["post_type"] == "attachment" ){
						$attachments[] = $post_to_import;
						unset( $wp_import->posts[$_post] );
					}						

					// remove comments
					if( isset( $post_to_import['comments'] ) ){
						unset($wp_import->posts[$_post]["comments"]);
					}
				}

				wp_suspend_cache_invalidation( true );					

				$processed_posts = get_transient('rt_framework_demo_imports');
				if( ! is_array( $processed_posts ) ){
					$processed_posts = array();
				}

				if($step < 10){
					$attachments_size = count( $attachments );
					$piece = round( $attachments_size / 9, 0 ) + 1;
					$start = $step == 1 ? 0 : ( $step * $piece ) - $piece; 
					$attachments_to_add = array_slice($attachments, $start, $piece );
					$wp_import->posts = $attachments_to_add;
					$wp_import->processed_posts = $processed_posts;

					$wp_import->process_posts();

					set_transient('rt_framework_demo_imports', $wp_import->processed_posts, HOUR_IN_SECONDS );
		
					wp_suspend_cache_invalidation( false );

				}else{ 
					$wp_import->processed_posts = $processed_posts;

					delete_transient('rt_framework_demo_imports');	

					$wp_import->process_categories();
					$wp_import->process_tags();
					$wp_import->process_terms();
					$wp_import->process_posts();	

					wp_suspend_cache_invalidation( false );
		
					// update incorrect/missing information in the DB
					$wp_import->backfill_parents();
					$wp_import->backfill_attachment_urls();
					$wp_import->remap_featured_images();					
				}

				// import end
				wp_import_cleanup( $wp_import->id );

				wp_cache_flush();
				foreach ( get_taxonomies() as $tax ) {
					delete_option( "{$tax}_children" );
					_get_term_hierarchy( $tax );
				}
		
				wp_defer_term_counting( false );
				wp_defer_comment_counting( false );
		
				if( $step == 10 ){ 
					echo _x('Done','Admin Panel','businesslounge' );
				}

				do_action( 'import_end' );
				
			}	


			if( $step == 10 ){
				do_action('rt_after_content_import' );
			}
		}

		//widgets
		if( $_GET['part'] == "widgets" ){

			//remove default widgets & blocks
			rt_framework_reset_widgets();
			
			if ( ! function_exists("wie_import_data") ) {
				include( RT_EXTENSIONS_PATH ."/inc/imports/widgets-importer.php" );
			}
	
			//read the file
			if( $file_location == "external" ){

				$file_url = $file_server ."/demo-{$demo}/widgets.txt";	
				$get_file = $this->get_external_content( "content", $file_url );

				if( $get_file["error"] ){
					echo $get_file["error_message"];
					die();
				}else{
					$data = $get_file["content"];	
				}				

				$widgets_data = json_decode( $data );

			}else{
				require RT_EXTENSIONS_PATH ."/inc/imports/demos/demo-{$demo}/widgets.php";
				$widgets_data = json_decode( $data );				
			}

			wie_import_data( $widgets_data );
			
			echo _x('Done','Admin Panel','businesslounge' );

		}

		//theme options
		if( $_GET['part'] == "options" ){


			//read the file
			if( $file_location == "external" ){

				$file_url = $file_server ."/demo-{$demo}/theme-options.txt";	
				$get_file = $this->get_external_content( "content", $file_url );

				if( $get_file["error"] ){
					echo $get_file["error_message"];
					die();
				}else{
					$skin_data = $get_file["content"];	
				}				
				
			}else{
				require RT_EXTENSIONS_PATH ."/inc/imports/demos/demo-{$demo}/theme-options.php";
			}

			//decode
			$settings = unserialize( base64_decode( $skin_data )); 

			//import now
			$get_options = RTFramework_Customize_Panel()->all_options;

			foreach ($settings as $name => $value) {
				if( isset( $get_options[$name] ) ){

					//replaces
					$value = str_replace("{{theme-image-directory}}", get_template_directory_uri()."/images/", $value );
					$value = $name == RT_EXTENSIONS_SLUG."_maintenance_mode" ? "" : $value;

					set_theme_mod( $name, $value );
				}
			}

			_ex("Settings imported successfully.",'Admin Panel','businesslounge');

			//actions
			usleep(250000);
			do_action( 'rtframework_after_reset' );

		}

		//revslider
		if( $_GET['part'] == "revslider" ){

			if ( class_exists( "RevSliderFront" ) ){

				require_once (ABSPATH . 'wp-load.php');
				require_once (ABSPATH . 'wp-includes/functions.php');

				$slider_array = array("business-lounge-1.zip","business-lounge-2.zip","business-lounge-3.zip","business-lounge-4.zip");

				$import_path = RT_EXTENSIONS_PATH ."/inc/imports/demos/demo-{$demo}/revslider/";	

				$slider = new RevSlider();
				
				$log = "";

				foreach($slider_array as $filepath){
					$response = $slider->importSliderFromPost(true,true,$import_path.$filepath);  

					if($response["success"] == false){
						$message = $response["error"];
						echo "{{importer_error}}";
						echo $response["error"];
						die();
					}

				}

				echo _x('Done','Admin Panel','businesslounge' );				
			}
		}

		die();
	}

	/**
	 * Export Settings
	 */
	public function export_settings()
	{
		if ( ! current_user_can( "edit_theme_options" ) ){
			return;
		}

		//check
		if( ! isset( $_GET['page'] ) || ! isset( $_POST['action'] ) ){
			return ;
		}

		if( $_GET['page'] != "rt_framework_customizations" || $_POST['action'] != "rt_export" ){
			return ;
		}

		// server time 
		$file_time = date('y-M-d-H-i-s');

		// sent file to user
		header('Content-type: text/plain');  
		header('Content-Disposition: attachment; filename=" '.get_bloginfo('name').' Theme Settings '.$file_time.'.txt"'); 


		print base64_encode( serialize( get_theme_mods() ) ); //write into the export file

		die();
	}


	/**
	 * Import Settings
	 */
	public function import_settings()
	{

		global $wp_filesystem;
		
		//check the current user access 
		if ( ! current_user_can( $this->capability ) ){
			return ;
		}

		//check
		if( ! isset( $_GET['page'] ) || ! isset( $_POST['action'] ) ){
			return ;
		}

		if( $_GET['page'] != "rt_framework_customizations" || $_POST['action'] != "rt_import" ){
			return ;
		}

		//include wp handle upload
		if ( ! function_exists( 'wp_handle_upload' ) ){
			require_once( ABSPATH . 'wp-admin/includes/file.php' );
		}

		$uploadedfile = isset( $_FILES['import'] ) ? $_FILES['import'] : "";

		if ( ! $uploadedfile ) {
			$error = _x("No file selected", 'Admin Panel','businesslounge') . $uploadedfile["error"];

			$this->add_notices($error);			
			return false; 
		}

		//check upload error
		if ( $uploadedfile && $uploadedfile["error"] ) {
			$error = _x("Error!", 'Admin Panel','businesslounge') . $uploadedfile["error"];

			$this->add_notices($error);
			return false;
		}

		//check file type
		if ( $uploadedfile && $uploadedfile["type"] != "text/plain" ) {
			$error = _x("Invalid file type!", 'Admin Panel','businesslounge'); 

			$this->add_notices($error);
			return false;
		}

		$upload_overrides = array( 'test_form' => false );
		$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

		if ( isset( $movefile ) ) {

			//check file error
			if ( $movefile && isset( $movefile["error"] ) ) {
				$error = _x("Error!", 'Admin Panel','businesslounge') . $movefile["error"];
 
				$this->add_notices($error);
				return false;
			}

			//Get Credentials
			$url = wp_nonce_url('admin.php?page=rt_import&action=import','rt-theme-import');
			if (false === ( $creds = request_filesystem_credentials($url, '', false, false, null ) ) ) {
				return; // stop processing here
			}

			//Initialize WP_Filesystem_Base   
			if ( ! WP_Filesystem( $creds ) ) {
				request_filesystem_credentials($url, '', true, false, null);
				return;
			}

			$file_content = $wp_filesystem->get_contents( $movefile["file"] );


			//check file content
			if( empty( $file_content ) ){
				$error = _x("This file is empty!", 'Admin Panel','businesslounge') . $movefile["error"];

				$this->add_notices($error);
				return false;
			}

			//decode
			$settings = unserialize( base64_decode( $file_content )); 

			if( ! is_array( $settings ) ){
				$error = _x("This settings file is broken!", 'Admin Panel','businesslounge') . $movefile["error"];

				$this->add_notices($error);			
				return false;
			}

			//import now
			foreach ($settings as $name => $value) {
				set_theme_mod( $name, str_replace("{{theme-image-directory}}", get_template_directory_uri()."/images/", $value ) );
			}

			$this->add_notices( _x("Settings imported successfully.",'Admin Panel','businesslounge') ,"updated" );

			//actions
			do_action( 'rtframework_after_reset' );

		} else {
			//Possible file upload attack!

			$error = _x("File cannot be uploaded!", 'Admin Panel','businesslounge');
			$this->add_notices($error);

			return false;
		}
	}	

	/**
	 * Custom CSS Page
	 */
	public function custom_css_page()
	{	
		?>
			<div class="wrap" id="rt_theme_custom_css">
					<h3><?php _ex( 'Custom CSS', 'Admin Panel','businesslounge' ); ?></h3>

					<p><?php _ex( 'Enter your custom CSS into the text area below', 'Admin Panel','businesslounge' ); ?></p>

					<form action="<?php echo wp_nonce_url(admin_url('admin.php?page=rt_custom_css')); ?>" method="post" enctype="multipart/form-data">
						<p class="textarea"><textarea name="custom_css" id="css" class="textarea rt_custom_css" cols="120" rows="40"><?php echo stripcslashes( get_option( RT_THEMESLUG."_user_custom_css") ); ?></textarea></p>

						<p class="submit"><input name="submit" id="submit" class="button button-primary" value="<?php _ex( 'Update', 'Admin Panel','businesslounge' ); ?>" type="submit"></p>
						<input type="hidden" name="action" value="save">
					</form>

			</div>
		<?php
	}


	/**
	 * Custom CSS Customize
	 */
	public function custom_css()
	{		

		//check the current user access 
		if ( ! current_user_can( $this->capability ) ){
			return ;
		}		
		 
		//check
		if( ! isset( $_GET['page'] ) || ! isset( $_POST['action'] ) ){
			return ;
		}

		if( $_GET['page'] != "rt_custom_css" || $_POST['action'] != "save" ){
			return ;
		}

		$this->add_notices(esc_html_x( 'Custom CSS File Updated!', 'Admin Panel','businesslounge' ),"updated");

		if( isset( $_POST['custom_css'] ) ){
			update_option( RT_THEMESLUG."_user_custom_css", $_POST['custom_css'] );	
		}
		
		update_option( RT_THEMESLUG."_custom_css_output", "");
		
		//actions
		do_action( 'rtframework_after_user_custom_css' );
	}

	/**
	 * Customizations Page
	 */
	public function customizations_page(){
		global $RTFrameworkManager;

		// about the page
		$title = sprintf( esc_html_x('Customizations','Admin Panel','businesslounge'), RT_THEMENAME );		
		$about = esc_html_x('Add your own fonts, Typekit or another external font family.','Admin Panel','businesslounge');

		// admin header
		$RTFrameworkManager->admin_header( $title, $about );	


		//import form
		$file_byte     = wp_max_upload_size() ;
		$file_size     = size_format( $file_byte );
		$wp_upload_dir = wp_upload_dir();

		//check if multisite
		$multisite_notice = "";
		if ( is_multisite() ) {
		 	$multisite_notice = '<strong>'._x('NOTE 3:','Admin Panel','businesslounge').'</strong>';
		 	$multisite_notice .= _x('You need to add "txt" in your allowed file types list before upload the file if it does not exist. For further reading: http://premium.wpmudev.org/blog/how-to-change-the-allowed-file-upload-types-in-wordpress-multisite/','Admin Panel','businesslounge');
		}

		if ( ! empty( $wp_upload_dir['error'] ) ){
			echo "<h3>"._ex("ERROR",'Admin Panel','businesslounge').":</h3><br />".$wp_upload_dir['error'];
		}else{

	 		echo '
	 			<div class="wrap" id="rt_import_settings">

					<h3>'._x('Import Customizations','Admin Panel','businesslounge').'</h3>
					
					<div class="import_desc">
						<p>
							'._x('Upload your exported settings (txt) file and to import.','Admin Panel','businesslounge').'<br />  
							'._x('<strong>NOTE 1: </strong> This importer will overwrite to the current settings.','Admin Panel','businesslounge').'<br />
							'._x('<strong>NOTE 2: </strong> This tool will only import the settings. You need to upload your images that used within settings and correct the image urls by manually after settings imported.','Admin Panel','businesslounge').'<br />
							'.$multisite_notice.'
						</p>
					</div>
	 
					<form class="wp-upload-form" action="" method="post" enctype="multipart/form-data">
						<p>
							<label for="upload">'._x('Choose a file from your computer','Admin Panel','businesslounge').':</label> ('.sprintf( _x('Maximum size: %s', 'Admin Panel','businesslounge' ), $file_size ).') <input type="file" size="25" name="import" id="upload">
							<input type="hidden" value="rt_import" name="action"> 
						</p>

						<p class="submit"><input type="submit" value="'._x('Upload file and import','Admin Panel','businesslounge').'" class="button" name="submit"></p>

					</form>

				</div>
			';
		}		
		?>

			<hr />

			<h3><?php _ex( 'Export Theme Customizations', 'Admin Panel','businesslounge' ); ?></h3>

			<p><?php _ex( 'Click to the export button to download the settings.', 'Admin Panel','businesslounge' ); ?></p>

			<form novalidate="novalidate" action="" method="post">
				<p class="submit"><input name="submit" id="submit" class="button button-primary" value="<?php _ex( 'Export', 'Admin Panel','businesslounge' ); ?>" type="submit"></p>
				<input type="hidden" name="action" value="rt_export">
			</form>

			<hr />

			<h3><?php _ex( 'Reset Theme Customizations', 'Admin Panel','businesslounge' ); ?></h3>

			<p><?php printf( esc_html_x( 'Click to the reset button to reset all the theme settings to default values that changed by using the %sCustomizer%s', 'Admin Panel','businesslounge' ), '<a href="'.wp_nonce_url(admin_url('admin.php')).'">', '</a>' ); ?></p>

			<p><strong><?php printf( esc_html_x( 'Dont\' forget to %sExport%s the settings before if you want to save them.', 'Admin Panel','businesslounge' ), '<a href="'.wp_nonce_url(admin_url('admin.php?page=rt_framework_customizations')).'">', '</a>' ); ?></strong></p>

			<form action="<?php echo wp_nonce_url(admin_url('admin.php?page=rt_framework_customizations')); ?>" method="post">
				<p class="submit"><input name="submit" id="submit" class="button button-primary" value="<?php _ex( 'Reset', 'Admin Panel','businesslounge' ); ?>" type="submit"></p>
				<input type="hidden" name="action" value="reset_settings">
			</form>

			
		<?php
		$RTFrameworkManager->admin_footer();
	}

	/**
	 * Reset Theme Customize
	 */
	public function reset_settings()
	{		

		//check the current user access 
		if ( ! current_user_can( $this->capability ) ){
			return ;
		}		
		 
		//check
		if( ! isset( $_GET['page'] ) || ! isset( $_POST['action'] ) ){
			return ;
		}

		if( $_GET['page'] != "rt_framework_customizations" || $_POST['action'] != "reset_settings" ){
			return ;
		}

		$this->add_notices( esc_html_x( 'Theme settings resetted to their default values.', 'Admin Panel','businesslounge' ),"updated");
		
		//remove_theme_mods();
		update_option( RT_THEMESLUG."_custom_css_output", "");
		RTFramework_Customize_Panel()->save_defaults("true"); 

	}

	/**
	 * Custom Fonts Page
	 */
	public function custom_fonts_page()
	{	
		global $RTFrameworkManager;
		
		//get saved fonts 
		$saved_fonts = get_option( RT_THEMESLUG ."_custom_fonts" );

		$upload_path = wp_upload_dir();
		$recommended_dir = $upload_path['basedir']."/custom_fonts";
		$recommended_url = $upload_path['baseurl']."/custom_fonts";

		// about the page
		$title = sprintf( esc_html_x('Custom Fonts','Admin Panel','businesslounge'), RT_THEMENAME );		
		$about = esc_html_x('Add your own fonts, Typekit or another external font family.','Admin Panel','businesslounge');
 
		// admin header
		$RTFrameworkManager->admin_header( $title, $about );
	?>		
		<div id="rt_theme_custom_fonts">
			<form action="<?php echo wp_nonce_url(admin_url('admin.php?page=rt_custom_fonts')); ?>" method="post" enctype="multipart/form-data">
					<input type="hidden" value="save_custom_fonts" name="action">

					<?php
						//Saved fonts
						if( ! empty( $saved_fonts ) ):
		
							//list previously save fonts
							foreach ( unserialize( $saved_fonts ) as $key => $font):

								$font["font-type"] = ! isset( $font["font-type"] ) ? "self-hosted" : $font["font-type"];
								$font["kitid"] = isset( $font["kitid"] ) ? $font["kitid"] : ""; 
							?>	
							<div class="font-holder">		
								<h3 class="title"><?php echo $font["family_name"] ?> <span class="delete-font"><?php _ex("remove this font",'Admin Panel','businesslounge')?></span></h3>

								<table class="form-table">
									<tbody>

										<tr>
											<th scope="row">
												<label for="eot"><?php _ex("Font Type",'Admin Panel','businesslounge')?></label>
											</th>
											<td>
												<div class="upload">
													<select name="rt_custom_fonts[<?php echo $key;?>][font-type]" class="rt-font-type" autocomplete="off">
														<option value="self-hosted" <?php echo $font["font-type"] == "self-hosted" ? "selected" : "" ?>><?php _ex("Self Hosted",'Admin Panel','businesslounge') ?></option>
														<option value="typekit" <?php echo $font["font-type"] == "typekit" ? "selected" : "" ?>><?php _ex("Typekit",'Admin Panel','businesslounge')?></option>
														<option value="external" <?php echo $font["font-type"] == "external" ? "selected" : "" ?>><?php _ex("Other",'Admin Panel','businesslounge')?></option>
													</select>
												</div>
											</td>
										</tr> 	

										<tr>
											<th scope="row">
												<label for="eot"><?php _ex("Font Family Name",'Admin Panel','businesslounge')?></label>
											</th>
											<td>
												<div class="upload">
													<input name="rt_custom_fonts[<?php echo $key;?>][family_name]" type="text" autocomplete="off" value="<?php echo $font["family_name"] ?>">
												</div>
											</td>
										</tr> 	

										<tr class="kitid">
											<th scope="row">
												<label for="eot"><?php _ex("Kit ID",'Admin Panel','businesslounge')?></label>
											</th>
											<td>
												<div class="upload">
													<input name="rt_custom_fonts[<?php echo $key;?>][kitid]" type="text" autocomplete="off" value="<?php echo $font["kitid"] ?>">
												</div>
											</td>
										</tr> 										

										<tr class="self_hosted_font">
											<th scope="row">
												<label for="eot"><?php _ex("EOT file URL",'Admin Panel','businesslounge')?></label>
											</th>
											<td>
												<div class="upload">
													<input name="rt_custom_fonts[<?php echo $key;?>][eot]" type="text" autocomplete="off" class="large-text" value="<?php echo $font["eot"] ?>">						
												</div>
											</td>
										</tr> 

										<tr class="self_hosted_font">
											<th scope="row">
												<label for="woff"><?php _ex("WOFF file URL",'Admin Panel','businesslounge')?></label>
											</th>
											<td>
												<div class="upload">
													<input name="rt_custom_fonts[<?php echo $key;?>][woff]" type="text" autocomplete="off" class="large-text" value="<?php echo $font["woff"] ?>">
												</div>
											</td>
										</tr> 

										<tr class="self_hosted_font">
											<th scope="row">
												<label for="woff"><?php _ex("WOFF2 file URL",'Admin Panel','businesslounge')?></label>
											</th>
											<td>
												<div class="upload">
													<input name="rt_custom_fonts[<?php echo $key;?>][woff2]" type="text" autocomplete="off" class="large-text" value="<?php echo $font["woff2"] ?>">
												</div>
											</td>
										</tr> 

										<tr class="self_hosted_font">
											<th scope="row">
												<label for="woff"><?php _ex("TTF file URL",'Admin Panel','businesslounge')?></label>
											</th>
											<td>
												<div class="upload">
													<input name="rt_custom_fonts[<?php echo $key;?>][ttf]" type="text" autocomplete="off" class="large-text" value="<?php echo $font["ttf"] ?>">
												</div>
											</td>
										</tr> 

										<tr class="self_hosted_font">
											<th scope="row">
												<label for="woff"><?php _ex("SVG file URL",'Admin Panel','businesslounge')?></label>
											</th>
											<td>
												<div class="upload">
													<input name="rt_custom_fonts[<?php echo $key;?>][svg]" type="text" autocomplete="off" class="large-text" value="<?php echo $font["svg"] ?>">
												</div>
											</td>
										</tr> 

									</tbody>
								</table>
								
								<hr />	
							</div>
					<?php 
							endforeach;
						endif;
					?>

					<h3 class="title"><?php _ex('New Font','Admin Panel','businesslounge')?></h3>

					<p>
						<?php printf(
						_x('You can add your own fonts, typekit or another external font family by using the form below. The new font will be dispayed in the font list along with Google Fonts and WebSafe Fonts inside the Typography Options of the Customize Panel. In order to use a self hosted font please upload the font files into the %s folder. Create the folder if it does not exists. Then type their full urls like shown in the form. You can use any file name for the custom font but make sure it is url frienly like %s. For %s fonts, the font family name must be the same as Typekit website.','Admin Panel','businesslounge'),
						'<code>'.$recommended_dir.'</code>',
						'<code>font-name</code>',
						'<a href="http://typekit.com" target="_new">Typekit</a>'); 
						?>
					</p>

					<table class="form-table">
						<tbody>
							<tr>
								<th scope="row">
									<label for="eot"><?php _ex("Font Type",'Admin Panel','businesslounge')?></label>
								</th>
								<td>
									<div class="upload">
										<select name="rt_custom_fonts[new][font-type]" class="rt-font-type" autocomplete="off">
											<option value="self-hosted"><?php _ex("Self Hosted",'Admin Panel','businesslounge')?></option>
											<option value="typekit"><?php _ex("Typekit",'Admin Panel','businesslounge')?></option>
											<option value="external"><?php _ex("Other",'Admin Panel','businesslounge')?></option>
										</select>
									</div>
								</td>
							</tr> 	

							<tr>
								<th scope="row">
									<label for="eot"><?php _ex("Font Family Name",'Admin Panel','businesslounge')?></label>
								</th>
								<td>
									<div class="upload">
										<input name="rt_custom_fonts[new][family_name]" type="text" autocomplete="off" placeholder="<?php _ex("My Custom Font",'Admin Panel','businesslounge')?>">
									</div>
								</td>
							</tr> 

							<tr class="kitid">
								<th scope="row">
									<label for="eot"><?php _ex("Kit ID",'Admin Panel','businesslounge')?></label>
								</th>
								<td>
									<div class="upload">
										<input name="rt_custom_fonts[new][kitid]" type="text" autocomplete="off" placeholder="<?php _ex("Typekit Kit ID",'Admin Panel','businesslounge')?>">
									</div>
								</td>
							</tr> 										

							<tr class="self_hosted_font">
								<th scope="row">
									<label for="eot"><?php _ex("EOT file URL",'Admin Panel','businesslounge')?></label>
								</th>
								<td>
									<div class="upload">
										<input name="rt_custom_fonts[new][eot]" type="text" autocomplete="off" class="large-text" size="130" placeholder="<?php echo esc_url($recommended_url); ?>/your-custom-font.eot">
									</div>
								</td>
							</tr> 

							<tr class="self_hosted_font">
								<th scope="row">
									<label for="woff"><?php _ex("WOFF file URL",'Admin Panel','businesslounge')?></label>
								</th>
								<td>
									<div class="upload">
										<input name="rt_custom_fonts[new][woff]" type="text" autocomplete="off" class="large-text" size="130" placeholder="<?php echo esc_url($recommended_url); ?>/your-custom-font.woff">
									</div>
								</td>
							</tr> 

							<tr class="self_hosted_font">
								<th scope="row">
									<label for="woff"><?php _ex("WOFF2 file URL",'Admin Panel','businesslounge')?></label>
								</th>
								<td>
									<div class="upload">
										<input name="rt_custom_fonts[new][woff2]" type="text" autocomplete="off" class="large-text" size="130" placeholder="<?php echo esc_url($recommended_url); ?>/your-custom-font.woff2">
									</div>
								</td>
							</tr> 

							<tr class="self_hosted_font">
								<th scope="row">
									<label for="woff"><?php _ex("TTF file URL",'Admin Panel','businesslounge')?></label>
								</th>
								<td>
									<div class="upload">
										<input name="rt_custom_fonts[new][ttf]" type="text" autocomplete="off" class="large-text" size="130" placeholder="<?php echo esc_url($recommended_url); ?>/your-custom-font.ttf">
									</div>
								</td>
							</tr> 

							<tr class="self_hosted_font">
								<th scope="row">
									<label for="woff"><?php _ex("SVG file URL",'Admin Panel','businesslounge')?></label>
								</th>
								<td>
									<div class="upload">
										<input name="rt_custom_fonts[new][svg]" type="text" autocomplete="off" class="large-text" size="130" placeholder="<?php echo esc_url($recommended_url); ?>/your-custom-font.svg">
									</div>
								</td>
							</tr> 
																					
						</tbody>
					</table> 

					<hr />	

					<?php echo submit_button();?>				
			</form>
		</div>
    
		<?php	
		$RTFrameworkManager->admin_footer();
	}

	/**
	 * Save Custom Fonts
	 */
	public function save_custom_fonts()
	{

		//check the current user access 
		if ( ! current_user_can( $this->capability ) ){
			return ;
		}

		//check
		if( ! isset( $_GET['page'] ) || ! isset( $_POST['action'] ) || ! isset( $_POST['rt_custom_fonts'] ) ){
			return ;
		}

		if( $_GET['page'] != "rt_custom_fonts" || $_POST['action'] != "save_custom_fonts" ){
			return ;
		}

		$rt_custom_fonts = $_POST['rt_custom_fonts'];

 		if( 
			empty( $rt_custom_fonts["new"]["eot"] ) &&
			empty( $rt_custom_fonts["new"]["ttf"] ) && 
			empty( $rt_custom_fonts["new"]["woff2"] ) && 
			empty( $rt_custom_fonts["new"]["woff"] ) && 
			empty( $rt_custom_fonts["new"]["svg"] )  &&
			empty( $rt_custom_fonts["new"]["kitid"] ) && 
			empty( $rt_custom_fonts["new"]["family_name"] )
 		){

 			unset($rt_custom_fonts["new"]);

 		}else{
 	
			$family_name = ! empty( $rt_custom_fonts["new"]["family_name"] ) ? $rt_custom_fonts["new"]["family_name"] : 'custom-font-'.rand(100000, 1000000);

			//add the new font to the list
			$rt_custom_fonts[$family_name] = $rt_custom_fonts["new"];
			unset($rt_custom_fonts["new"]);

 		}

 		if( ! empty( $rt_custom_fonts ) ){
			update_option( RT_THEMESLUG ."_custom_fonts",  serialize( $rt_custom_fonts ) );			
			$this->add_notices( esc_html_x("Custom fonts list updated successfully.",'Admin Panel','businesslounge') ,"updated" );
 		}else{
 			delete_option( RT_THEMESLUG ."_custom_fonts" );			
 		}

	}

	/**
	 * Contact Form 7 Email Fixes
	 *
	 * Replace sample emails with users email which used in the demo cotnents
	 * 
	 */
	public function cf7_mail_fix( $postmeta )
	{

		$post['postmeta'] = $postmeta;
		$current_user = wp_get_current_user();
		
		if ( ! empty( $post['postmeta'] ) ) {
			foreach ( $post['postmeta'] as $meta_key => $meta ) {
				$key = $meta['key'];
			 
				if ( '_mail' == $key ) {

					$value = maybe_unserialize( $meta['value'] );
					$new_value = array();

					if( is_array( $value) ){
						foreach ($value as $v_key => $v_value) {
							$new_value[$v_key] = str_replace("mail@your-website.com", $current_user->user_email, $v_value ); 
						}	

						$post['postmeta'][$meta_key]['value'] = serialize( $new_value );				
					}
				}
			}
		}	

		return $post['postmeta'];
	 
	}		


	/**
	 * Get external content for importer
	 *
	 * @return array["error"=>"","content"=>file dir or file content] 
	 *
	 * 
	 */
	public function get_external_content( $output_type = "dir", $file_url = "", $file_name = "" )
	{	
		$output = array("error"=>false,"error_message"=>"","content"=>"");

		if( empty( $file_url ) ){
			$output["error"] = true;
			return;
		}

		$get_file = wp_remote_get( $file_url, array( 'timeout' => 90, 'httpversion' => '1.1' ) ); 

		//wperror file could not be opened
		if ( is_wp_error( $get_file ) ) {
			
			$error_string = $get_file->get_error_message();

			$output["error"] = true;
			$output["error_message"] .= "{{importer_error}}";
			$output["error_message"] .= "Import file could not be opened!<br />";
			$output["error_message"] .= $error_string;
			 
			return $output;
		}

		//not found
		if ( isset( $get_file["response"] ) && $get_file["response"]["code"] !== 200  ) {

			$output["error"] = true;
			$output["error_message"] .= "{{importer_error}}";
			$output["error_message"] .= "Import file could not be read!<br />";
			$output["error_message"] .= $get_file["response"]["message"];			 

			return $output;			
		}

		//success 
		if ( isset( $get_file["response"] ) && $get_file["response"]["code"] === 200  ) {

			if( $output_type == "dir" ){

				//get xml content & store 
				global $wp_filesystem;

				if (empty($wp_filesystem)) {
					require_once (ABSPATH . '/wp-admin/includes/file.php');
					WP_Filesystem();
				}
	 	
	 			//dir
				$upload_path = wp_upload_dir();
				$dir = $upload_path['basedir'] ."/". strtolower(RT_THEMESLUG). "-demo-imports/";			

				//create dir 
				if( ! $wp_filesystem->is_dir( $dir ) ) {
					@$wp_filesystem->mkdir( $dir );
				}

				//check if dir writable
				if( ! is_writable( $dir ) && ! $wp_filesystem ){

					$output["error"] = true;
					$output["error_message"] .= "{{importer_error}}";
					$output["error_message"] .= "Import file could not be stored!";

					return $output;								
				}

				//write the file
				$wp_filesystem->put_contents(
					$dir."/".$file_name,
					$get_file["body"],
					FS_CHMOD_FILE // predefined mode settings for WP files
				); 				

				$output["content"] = $dir."/".$file_name;
				return $output;	

			}else{
				$output["content"] = $get_file["body"];
				return $output;					
			}		

		}else{

			$output["error"] = true;
			$output["error_message"] .= "{{importer_error}}";
			$output["error_message"] .= "Import failed!";
			
			return $output;	
		}
	}

}

new RT_Tools();