????
| Current Path : /home2/morganrand/www/wp-content/themes/wanderfuls-2/framework/addons/ |
| Current File : /home2/morganrand/www/wp-content/themes/wanderfuls-2/framework/addons/import-export.php |
<?php
/**
* Creates the admin panel for the customizer
*
* @package Wanderfuls WordPress theme
* @subpackage Framework
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Start Class
if ( ! class_exists( 'TB_Import_Export' ) ) {
class TB_Import_Export {
/**
* Start things up
*
* @since 1.6.0
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_page' ), 50 );
add_action( 'admin_init', array( $this,'register_settings' ) );
add_action( 'admin_notices', array( $this, 'notices' ) );
}
/**
* Add sub menu page
*
* @since 1.6.0
*/
public function add_page() {
add_submenu_page(
TB_THEME_PANEL_SLUG,
__( 'Import/Export', 'tb' ),
__( 'Import/Export', 'tb' ),
'manage_options',
TB_THEME_PANEL_SLUG .'-import-export',
array( $this, 'create_admin_page' )
);
}
/**
* Register a setting and its sanitization callback.
*
* @since 1.6.0
*/
public function register_settings() {
register_setting(
'tb_customizer_options',
'tb_customizer_options',
array( $this, 'sanitize' )
);
}
/**
* Displays all messages registered to 'tb-customizer-notices'
*
* @since 1.6.0
*/
public static function notices() {
settings_errors( 'tb-customizer-notices' );
}
/**
* Sanitization callback
*
* @since 1.6.0
*/
public static function sanitize( $options ) {
// Import the imported options
if ( $options ) {
// Delete options if import set to -1
if ( '-1' == $options['reset'] ) {
// Get menu locations
$locations = get_theme_mod( 'nav_menu_locations' );
$save_menus = array();
if ( $locations ) {
foreach( $locations as $key => $val ) {
$save_menus[$key] = $val;
}
}
// Get sidebars
$widget_areas = get_theme_mod( 'widget_areas' );
// Remove all mods
remove_theme_mods();
// Remove CSS cache
delete_option( 'tb_customizer_inline_css_cache' );
// Re-add the menus
set_theme_mod( 'nav_menu_locations', array_map( 'absint', $save_menus ) );
set_theme_mod( 'widget_areas', $widget_areas );
// Error messages
$error_msg = __( 'All settings have been reset.', 'tb' );
$error_type = 'updated';
}
// Set theme mods based on json data
elseif( ! empty( $options['import'] ) ) {
// Decode input data
$theme_mods = json_decode( $options['import'], true );
// Validate json file then set new theme options
if ( function_exists( 'json_last_error' ) ) {
if ( '0' == json_last_error() ) {
// Delete CSS cache
delete_option( 'tb_customizer_inline_css_cache' );
// Loop through mods and add them
foreach ( $theme_mods as $theme_mod => $value ) {
set_theme_mod( $theme_mod, $value );
}
// Success message
$error_msg = __( 'Settings imported successfully.', 'tb' );
$error_type = 'updated';
}
// Display invalid json data error
else {
$error_msg = __( 'Invalid Import Data.', 'tb' );
$error_type = 'error';
}
}
}
// No json data entered
else {
$error_msg = __( 'No import data found.', 'tb' );
$error_type = 'error';
}
// Make sure the settings data is reset!
$options = array(
'import' => '',
'reset' => '',
);
}
// Display message
add_settings_error(
'tb-customizer-notices',
esc_attr( 'settings_updated' ),
$error_msg,
$error_type
);
// Return options
return $options;
}
/**
* Settings page output
*
* @since 1.6.0
*/
public static function create_admin_page() { ?>
<div class="wrap">
<h2><?php _e( 'Import, Export or Reset Theme Settings', 'tb' ); ?></h2>
<p><?php _e( 'This will export/import/delete ALL theme_mods that means if other plugins are adding settings in the Customizer it will export/import/delete those as well.', 'tb' ); ?></p>
<?php
// Default options
$options = array(
'import' => '',
'reset' => '',
); ?>
<form method="post" action="options.php">
<?php
// Output nonce, action, and option_page fields for a settings page
$options = get_option( 'tb_customizer_options', $options );
settings_fields( 'tb_customizer_options' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e( 'Export Settings', 'tb' ); ?></th>
<td>
<?php
// Get an array of all the theme mods
if ( $theme_mods = get_theme_mods() ) {
$mods = array();
foreach ( $theme_mods as $theme_mod => $value ) {
$mods[$theme_mod] = maybe_unserialize( $value );
}
$json = json_encode( $mods );
$disabled = '';
} else {
$json = __( 'No Settings Found', 'tb' );
$disabled = 'disabled';
}
echo '<textarea rows="10" cols="50" readonly id="tb-customizer-export" style="width:100%;">' . $json . '</textarea>'; ?>
<p class="submit">
<a href="#" class="button-primary tb-highlight-options <?php echo $disabled; ?>"><?php _e( 'Highlight Options', 'tb' ); ?></a>
</p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e( 'Import Settings', 'tb' ); ?></th>
<td>
<textarea name="tb_customizer_options[import]" rows="10" cols="50" style="width:100%;"><?php echo stripslashes( $options['import'] ); ?></textarea>
<input id="tb-reset-hidden" name="tb_customizer_options[reset]" type="hidden" value=""></input>
<p class="submit">
<input type="submit" class="button-primary tb-submit-form" value="<?php _e( 'Import Options', 'tb' ) ?>" />
<a href="#" class="button-secondary tb-delete-options"><?php _e( 'Reset Options', 'tb' ); ?></a>
<a href="#" class="button-secondary tb-cancel-delete-options" style="display:none;"><?php _e( 'Cancel Reset', 'tb' ); ?></a>
</p>
<div class="tb-delete-options-warning error inline" style="display:none;">
<p style="margin:.5em 0;"><?php _e( 'Always make sure you have a backup of your settings before resetting, just incase! Your menu locations and widget areas will not reset and will remain intact. All customizer and addon settings will reset.', 'tb' ); ?></p>
</div>
</td>
</tr>
</table>
</form>
<script>
(function($) {
"use strict";
$( '.tb-highlight-options' ).click( function() {
$( '#tb-customizer-export' ).focus().select();
return false;
} );
$( '.tb-delete-options' ).click( function() {
$(this).hide();
$( '.tb-delete-options-warning, .tb-cancel-delete-options' ).show();
$( '.tb-submit-form' ).val( "<?php _e( 'Confirm Reset', 'tb' ); ?>" );
$( '#tb-reset-hidden' ).val( '-1' );
return false;
} );
$( '.tb-cancel-delete-options' ).click( function() {
$(this).hide();
$( '.tb-delete-options-warning' ).hide();
$( '.tb-delete-options' ).show();
$( '.tb-submit-form' ).val( "<?php _e( 'Import Options', 'tb' ); ?>" );
$( '#tb-reset-hidden' ).val( '' );
return false;
} );
} ) ( jQuery );
</script>
</div>
<?php }
}
}
new TB_Import_Export();