add rudimentary settings file import

This commit is contained in:
Ron Rennick 2020-11-05 14:57:21 -04:00
parent 1625d52187
commit 7ff1ed04fa
3 changed files with 84 additions and 11 deletions

View File

@ -289,7 +289,7 @@ Copy and paste the system status report from **WooCommerce > System Status** in
'parent' => 'wc-beta-tester',
'id' => 'import-export-settings',
'title' => __( 'Import/Export Settings', 'woocommerce-beta-tester' ),
'href' => admin_url( 'admin.php?page=wc-beta-tester-import-export' ),
'href' => admin_url( 'admin.php?page=wc-beta-tester-settings' ),
),
array(
'parent' => 'wc-beta-tester',

View File

@ -16,6 +16,26 @@ class WC_Beta_Tester_Import_Export {
*/
protected const AJAX_HOOK = 'wc_beta_tester_export_settings';
/**
* @var string WordPress nonce action
*/
protected const NONCE_ACTION = 'wc-beta-tester-import-settings';
/**
* @var string WordPress import action
*/
protected const IMPORT_ACTION = 'wc-beta-tester-import';
/**
* @var string WordPress import capability
*/
protected const IMPORT_CAP = 'install_plugins';
/**
* @var string WordPress import file name
*/
protected const IMPORT_FILENAME = 'woocommerce-settings-json';
/**
* Class constructor.
*/
@ -30,33 +50,44 @@ class WC_Beta_Tester_Import_Export {
add_action( 'admin_menu', array( $this, 'add_to_menu' ), 55 );
add_action( 'wp_ajax_' . static::AJAX_HOOK, array( $this, 'export_settings' ) );
}
/**
* Add options page to menu
*/
public function add_to_menu() {
add_submenu_page( 'woocommerce', __( 'WooCommerce Settings Import/Export', 'woocommerce-beta-tester' ), __( 'Settings Import/Export', 'woocommerce-beta-tester' ), 'manage_woocommerce', 'wc-beta-tester-import-export', array( $this, 'settings_page_html' ) );
add_submenu_page( 'plugins.php', __( 'WooCommerce Tester Import/Export Settings', 'woocommerce-beta-tester' ), __( 'WC Import/Export Settings', 'woocommerce-beta-tester' ), static::IMPORT_CAP, 'wc-beta-tester-settings', array( $this, 'settings_page_html' ) );
}
/**
* Output settings HTML
*/
public function settings_page_html() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
if ( ! current_user_can( static::IMPORT_CAP ) ) {
return;
}
$settings = $this->maybe_import_settings();
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<a href="<?php echo esc_url( admin_url( 'admin-ajax.php?action=wc_beta_tester_export_settings' ) ) ?>" class="button-primary"><?php
/* translators Download WooCommerce settings button text. */
esc_html_e( 'Download WooCommerce Settings', 'woocommerce-beta-tester' );
/* translators Export WooCommerce settings button text. */
esc_html_e( 'Export WooCommerce Settings', 'woocommerce-beta-tester' );
?></a>
<hr />
<form method="POST" enctype="multipart/form-data">
<?php wp_nonce_field( static::NONCE_ACTION ); ?>
<input type="hidden" name="action" value="<?php echo static::IMPORT_ACTION; ?>" />
<button type="submit" class="button-primary"><?php
/* translators Import WooCommerce settings button text. */
esc_html_e( 'Import WooCommerce Settings', 'woocommerce-beta-tester' );
?></button>
<input type="file" name="<?php echo static::IMPORT_FILENAME; ?>" />
</form>
</div>
<?php
echo $settings;
}
/**
* Export settings in json format.
*/
@ -72,17 +103,59 @@ class WC_Beta_Tester_Import_Export {
exit;
}
/**
* Import settings in json format if submitted.
*/
public function maybe_import_settings() {
$return_string = '';
if ( empty( $_POST ) || empty( $_POST['action'] ) || $_POST['action'] !== static::IMPORT_ACTION ) {
return $return_string;
}
if ( ! wp_verify_nonce( $_POST['_wpnonce'], static::NONCE_ACTION ) ) {
return __( 'Invalid submission', 'woocommerce-beta-tester' );
}
if ( ! empty( $_FILES[ static::IMPORT_FILENAME ] ) ) {
$tmp_file = $_FILES[ static::IMPORT_FILENAME ]['tmp_name'];
if ( is_readable( $tmp_file ) ) {
$maybe_json = file_get_contents( $tmp_file );
$settings = json_decode( $maybe_json, true );
if ( $settings !== null ) {
foreach ( $this->get_setting_list() as $option_name ) {
if ( ! isset( $settings[ $option_name ] ) ) {
continue;
}
$setting = maybe_unserialize( $settings[ $option_name ] );
update_option( $option_name, $setting );
}
// @todo: implement as notice
$return_string = __( 'Settings Updated', 'woocommerce-beta-tester' );
}
}
}
return $return_string;
}
/**
* Get an array of the WooCommerce related settings.
*/
protected function get_settings() {
$settings = array();
if ( current_user_can( 'manage_woocommerce' ) ) {
require_once(dirname(__FILE__) . '/wc-beta-tester-settings-list.php');
foreach (wp_beta_tester_setting_list() as $option_name) {
$settings[$option_name] = get_option($option_name);
foreach ( $this->get_setting_list() as $option_name ) {
$setting = get_option( $option_name );
$settings[ $option_name ] = is_string( $setting ) ? $setting : serialize( $setting );
}
}
return $settings;
}
/**
* Get the WooCommerce settings list keys.
*/
private function get_setting_list() {
require_once( dirname(__FILE__ ) . '/wc-beta-tester-settings-list.php');
return wc_beta_tester_setting_list();
}
}

View File

@ -3,7 +3,7 @@
* List of settings to be exported.
*/
function wp_beta_tester_setting_list() {
function wc_beta_tester_setting_list() {
$settings_list = array(
'date_format',
'gmt_offset',
@ -124,5 +124,5 @@ function wp_beta_tester_setting_list() {
'woocommerce_version',
'woocommerce_weight_unit'
);
return apply_filters( 'wp_beta_tester_setting_list', $settings_list );
return apply_filters( 'wc_beta_tester_setting_list', $settings_list );
}