<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
add_action('admin_menu', 'pmthemes_add_pages');
function pmthemes_add_pages() {
add_submenu_page('pmthemes', 'Import/Export/Reset', 'Import/Export/Reset', 'manage_options', 'pmthemes-iex', 'pmthemes_iexp_page');
	wp_enqueue_style( 'pmthemes-options', PMTHEMES_URI . '/css/admin.css');
	wp_enqueue_style( 'pmthemes-style-grid', PMTHEMES_URI . '/css/grid.css', false, PMTHEMES_VERSION, 'all' );
}
function pmthemes_iexp_page() {
	?>
	<div id="pmthemes">
	<div class="wrap">
		<div class="metabox-holder">
			<div class="postbox-container">
				<div class="grid-container grid-parent">
					<div class="form-metabox grid-70">
						<div class="postbox pmthemes-metabox" id="pmthemes">
							<h3 class="hndle"><?php _e('Export Customizer Settings','pmthemes');?></h3>
								<div class="inside">	
							<p><strong>Export</strong> your settings set in the <a href="<?php echo admin_url('customize.php'); ?>">Customizer</a>.</p>
							<form method="post">
								<p><input type="hidden" name="pmthemes_action" value="export_settings" /></p>
								<p>
									<?php wp_nonce_field( 'pmthemes_export_nonce', 'pmthemes_export_nonce' ); ?>
									<?php submit_button( 'Export Customizer Settings', 'button-primary', 'submit', false ); ?>
								</p>
							</form>	
						</div>
					</div>
					<div class="postbox pmthemes-metabox" id="pmthemes-import">
						<h3 class="hndle"><?php _e('Import Customizer Settings','pmthemes');?></h3>
						<div class="inside">
					<p><?php _e( '<strong>Import</strong> your settings by uploading your exported .json file.','pmthemes' ); ?></p>
					<form method="post" enctype="multipart/form-data">
						<p>
							<input type="file" name="import_file"/>
						</p>
						<p>
							<input type="hidden" name="pmthemes_action" value="import_settings" />
							<?php wp_nonce_field( 'pmthemes_import_nonce', 'pmthemes_import_nonce' ); ?>
							<?php submit_button( 'Import Customizer Settings', 'button-primary', 'submit', false ); ?>
							
						</p>
					</form>
					<?php do_action('pmthemes_add_import_items'); ?>
						</div>
					</div>
						<div class="postbox pmthemes-metabox" id="gen-delete">
							<h3 class="hndle"><?php _e('Reset Customizer Settings','pmthemes');?></h3>
							<div class="inside">
												
								<p><?php _e( '<strong>Warning:</strong> Clicking this button will delete your settings set in the Customizer area.','pmthemes' ); ?></p>
								<p><?php _e( 'You may want to export your settings before deleting them forever.','pmthemes');?></p>
								<form method="post">
									<p><input type="hidden" name="pmthemes_reset_customizer" value="pmthemes_reset_customizer_settings" /></p>
									<p>
										<?php wp_nonce_field( 'pmthemes_reset_customizer_nonce', 'pmthemes_reset_customizer_nonce' ); ?>
										<?php submit_button( 'Delete Default Customizer Settings', 'button', 'submit', false ); ?>
									</p>	
								</form>
								<?php do_action('pmthemes_delete_settings_form');?>
							</div>
						</div>
					</div>
					<div class="pmthemes-right-sidebar grid-30">
					<div class="postbox pmthemes-metabox addon-metabox" id="gen-2">
					<h3 class="hndle"><?php _e('Import/Export/Reset','pmthemes');?></h3>
					<div class="inside">
						<p><?php _e( 'Use this page to import/export customizer settings. You can even reset it if needed.','pmthemes');?></p>
					</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	</div>
<?php }

/**
 * Reset customizer settings
 */
add_action( 'admin_init', 'pmthemes_reset_customizer_settings' );
function pmthemes_reset_customizer_settings() {

	if( empty( $_POST['pmthemes_reset_customizer'] ) || 'pmthemes_reset_customizer_settings' !== $_POST['pmthemes_reset_customizer'] )
		return;

	if( ! wp_verify_nonce( $_POST['pmthemes_reset_customizer_nonce'], 'pmthemes_reset_customizer_nonce' ) )
		return;

	if( ! current_user_can( 'manage_options' ) )
		return;

	delete_option('pmthemes_theme_options');
	
	wp_safe_redirect( admin_url( 'admin.php?page=pmthemes-iex&status=reset' ) ); exit;

}

/**
 * Process a settings export that generates a .json file of the shop settings
 */
add_action( 'admin_init', 'pmthemes_process_settings_export' );
function pmthemes_process_settings_export() {

	if( empty( $_POST['pmthemes_action'] ) || 'export_settings' != $_POST['pmthemes_action'] )
		return;

	if( ! wp_verify_nonce( $_POST['pmthemes_export_nonce'], 'pmthemes_export_nonce' ) )
		return;

	if( ! current_user_can( 'manage_options' ) )
		return;
	
	$settings = get_option( 'pmthemes_theme_options' );

	ignore_user_abort( true );

    nocache_headers();
    $site_title =  sanitize_title_with_dashes( get_bloginfo('name') );
    header( 'Content-Type: application/json; charset=utf-8' );
    header( 'Content-Disposition: attachment; filename='. $site_title .'-customizer-settings-' . date( 'm-d-Y' ) . '.json' );
    header( "Expires: 0" );

	echo json_encode( $settings );
	exit;
}

/**
 * Process a settings import from a json file
 */
add_action( 'admin_init', 'pmthemes_process_settings_import' );
function pmthemes_process_settings_import() {

	if( empty( $_POST['pmthemes_action'] ) || 'import_settings' != $_POST['pmthemes_action'] )
		return;

	if( ! wp_verify_nonce( $_POST['pmthemes_import_nonce'], 'pmthemes_import_nonce' ) )
		return;

	if( ! current_user_can( 'manage_options' ) )
		return;
	
	$filename = $_FILES['import_file']['name'];
	$extension = end( explode( '.', $_FILES['import_file']['name'] ) );
	$name = substr( $filename, 0, strrpos($filename,'-export') );
	$name = str_replace( '-', '_',$name);

	if( $extension != 'json' ) {
		wp_die( __( 'Please upload a valid .json file', 'pmthemes' ) );
	}
	$import_file = $_FILES['import_file']['tmp_name'];
	if( empty( $import_file ) ) {
		wp_die( __( 'Please upload a file to import', 'pmthemes' ) );
	}
	// Retrieve the settings from the file and convert the json object to an array.
	// This json_decode part will fail on WP Theme Check but read reason here: http://docs.reduxframework.com/core/theme-check/
	$settings = json_decode( file_get_contents( $import_file ), true );
	update_option( 'pmthemes_theme_options', $settings );
	wp_safe_redirect( admin_url( 'admin.php?page=pmthemes-iex&status=imported' ) ); exit;
}
/**
 * Add our admin notices
 */
add_action( 'admin_notices', 'pmthemes_admin_errors' );
function pmthemes_admin_errors()
{
	
	if ( isset( $_GET['status'] ) && 'imported' == $_GET['status'] ) {
		 add_settings_error( 'pmthemes-notices', 'imported', __( 'Import successful.', 'pmthemes' ), 'updated' );
	}
	
	if ( isset( $_GET['status'] ) && 'reset' == $_GET['status'] ) {
		 add_settings_error( 'pmthemes-notices', 'reset', __( 'Settings removed.', 'pmthemes' ), 'updated' );
	}

	settings_errors( 'pmthemes-notices' );
}