2020-11-03 18:27:32 +00:00
< ? php
/**
* Beta Tester settings export class
*
* @ package WC_Beta_Tester
*/
defined ( 'ABSPATH' ) || exit ;
/**
2023-08-09 00:20:34 +00:00
* WC_Beta_Tester_Import_Export Main Class .
2020-11-03 18:27:32 +00:00
*/
class WC_Beta_Tester_Import_Export {
/**
2023-08-09 00:20:34 +00:00
* The AJAX hook for the class .
2020-11-03 18:27:32 +00:00
*/
protected const AJAX_HOOK = 'wc_beta_tester_export_settings' ;
2020-11-05 18:57:21 +00:00
/**
2023-08-09 00:20:34 +00:00
* The security nonce for the class .
2020-11-05 18:57:21 +00:00
*/
protected const NONCE_ACTION = 'wc-beta-tester-import-settings' ;
/**
2023-08-09 00:20:34 +00:00
* The import AJAX action .
2020-11-05 18:57:21 +00:00
*/
protected const IMPORT_ACTION = 'wc-beta-tester-import' ;
/**
2023-08-09 00:20:34 +00:00
* The capability required to import settings .
2020-11-05 18:57:21 +00:00
*/
protected const IMPORT_CAP = 'install_plugins' ;
/**
2023-08-09 00:20:34 +00:00
* The filename of the file containing exported settings .
2020-11-05 18:57:21 +00:00
*/
protected const IMPORT_FILENAME = 'woocommerce-settings-json' ;
2020-11-06 18:36:02 +00:00
/**
2023-08-09 00:20:34 +00:00
* The status message of the import .
*
* @ var string
2020-11-06 18:36:02 +00:00
*/
protected $message ;
2020-11-03 18:27:32 +00:00
/**
* Class constructor .
*/
public function __construct () {
$this -> add_hooks ();
}
/**
* Hook into WordPress .
*/
public function add_hooks () {
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 () {
2021-01-05 20:59:15 +00:00
add_submenu_page ( 'plugins.php' , __ ( 'WC Beta Tester Import/Export' , 'woocommerce-beta-tester' ), __ ( 'WC Import/Export' , 'woocommerce-beta-tester' ), static :: IMPORT_CAP , 'wc-beta-tester-settings' , array ( $this , 'settings_page_html' ) );
2020-11-03 18:27:32 +00:00
}
/**
* Output settings HTML
*/
public function settings_page_html () {
2020-11-05 18:57:21 +00:00
if ( ! current_user_can ( static :: IMPORT_CAP ) ) {
2020-11-03 18:27:32 +00:00
return ;
}
2020-11-06 18:36:02 +00:00
$export_url = wp_nonce_url ( admin_url ( 'admin-ajax.php?action=wc_beta_tester_export_settings' ), static :: NONCE_ACTION );
$this -> maybe_import_settings ();
// show error/update messages.
if ( ! empty ( $this -> message ) ) {
?>
2023-08-09 00:20:34 +00:00
< div class = " notice
< ? php
2020-11-06 18:36:02 +00:00
echo ! empty ( $this -> message [ 'type' ] ) ? esc_attr ( $this -> message [ 'type' ] ) : '' ;
2023-08-09 00:20:34 +00:00
?>
" ><?php echo esc_html( $this->message ['message'] ); ?></div>
2020-11-06 18:36:02 +00:00
< ? php
}
2020-11-03 18:27:32 +00:00
?>
< div class = " wrap " >
< h1 >< ? php echo esc_html ( get_admin_page_title () ); ?> </h1>
2020-11-06 18:36:02 +00:00
< p >< ? php esc_html_e ( 'Export your WooCommerce Settings. The export file should not contain any fields that identify your site or reveal secrets (eg. API keys).' , 'woocommerce-beta-tester' ); ?> </p>
2023-08-09 00:20:34 +00:00
< a href = " <?php echo esc_url( $export_url ); ?> " class = " button-primary " >
< ? php
/* translators Export WooCommerce settings button text. */
esc_html_e ( 'Export WooCommerce Settings' , 'woocommerce-beta-tester' );
?>
</ a >
2020-11-05 18:57:21 +00:00
< hr />
< form method = " POST " enctype = " multipart/form-data " >
< ? php wp_nonce_field ( static :: NONCE_ACTION ); ?>
2023-08-09 00:20:34 +00:00
< input type = " hidden " name = " action " value = " <?php echo esc_attr( static::IMPORT_ACTION ); ?> " />
2020-11-06 18:36:02 +00:00
< p >< ? php esc_html_e ( 'Import WooCommerce Settings exported with this tool. Some settings like store address, payment gateways, etc. will need to be configured manually.' , 'woocommerce-beta-tester' ); ?> </p>
2023-08-09 00:20:34 +00:00
< button type = " submit " class = " button-primary " >
< ? php
2020-11-05 18:57:21 +00:00
/* translators Import WooCommerce settings button text. */
esc_html_e ( 'Import WooCommerce Settings' , 'woocommerce-beta-tester' );
2023-08-09 00:20:34 +00:00
?>
</ button >
< input type = " file " name = " <?php echo esc_attr( static::IMPORT_FILENAME ); ?> " />
2020-11-03 18:27:32 +00:00
</ form >
</ div >
< ? php
}
2020-11-05 18:57:21 +00:00
2020-11-03 18:27:32 +00:00
/**
* Export settings in json format .
*/
public function export_settings () {
2023-08-09 00:20:34 +00:00
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! isset ( $_GET [ '_wpnonce' ] ) || ! wp_verify_nonce ( wp_unslash ( $_GET [ '_wpnonce' ] ), static :: NONCE_ACTION ) ) {
2020-11-06 18:36:02 +00:00
header ( 'HTTP/1.1 403 Forbidden' );
exit ;
}
2020-11-03 18:27:32 +00:00
$filename = sprintf ( 'woocommerce-settings-%s.json' , gmdate ( 'Ymdgi' ) );
wc_set_time_limit ( 0 );
wc_nocache_headers ();
header ( 'Content-Type: text/csv; charset=utf-8' );
header ( 'Content-Disposition: attachment; filename=' . $filename );
header ( 'Pragma: no-cache' );
header ( 'Expires: 0' );
echo wp_json_encode ( $this -> get_settings () );
exit ;
}
2020-11-05 18:57:21 +00:00
/**
* Import settings in json format if submitted .
*/
public function maybe_import_settings () {
2023-08-09 00:20:34 +00:00
if ( empty ( $_POST ) || empty ( $_POST [ 'action' ] ) || static :: IMPORT_ACTION !== $_POST [ 'action' ] ) {
2020-11-06 18:36:02 +00:00
return ;
2020-11-05 18:57:21 +00:00
}
2023-08-09 00:20:34 +00:00
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! isset ( $_POST [ '_wpnonce' ] ) || ! wp_verify_nonce ( wp_unslash ( $_POST [ '_wpnonce' ] ), static :: NONCE_ACTION ) ) {
2020-11-06 18:36:02 +00:00
$this -> add_message ( __ ( 'Invalid submission' , 'woocommerce-beta-tester' ) );
return ;
2020-11-05 18:57:21 +00:00
}
2021-01-05 20:59:15 +00:00
if ( empty ( $_FILES [ static :: IMPORT_FILENAME ] ) ) {
$this -> add_message ( __ ( 'No file uploaded.' , 'woocommerce-beta-tester' ) );
return ;
}
2023-08-09 00:20:34 +00:00
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$tmp_file = wp_unslash ( $_FILES [ static :: IMPORT_FILENAME ][ 'tmp_name' ] );
2021-01-05 20:59:15 +00:00
if ( empty ( $tmp_file ) ) {
$this -> add_message ( __ ( 'No file uploaded.' , 'woocommerce-beta-tester' ) );
return ;
}
if ( ! is_readable ( $tmp_file ) ) {
2021-01-07 19:17:48 +00:00
$this -> add_message ( __ ( 'File could not be read.' , 'woocommerce-beta-tester' ) );
2021-01-05 20:59:15 +00:00
return ;
}
2020-11-06 18:36:02 +00:00
2023-08-09 00:20:34 +00:00
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
2021-01-05 20:59:15 +00:00
$maybe_json = file_get_contents ( $tmp_file );
2023-08-09 00:20:34 +00:00
$settings = json_decode ( $maybe_json , true );
if ( null !== $settings ) {
2021-01-05 20:59:15 +00:00
foreach ( $this -> get_setting_list () as $option_name ) {
if ( ! isset ( $settings [ $option_name ] ) ) {
continue ;
}
$setting = maybe_unserialize ( $settings [ $option_name ] );
if ( is_null ( $setting ) ) {
delete_option ( $option_name );
2020-11-06 18:36:02 +00:00
} else {
2021-01-05 20:59:15 +00:00
update_option ( $option_name , $setting );
2020-11-05 18:57:21 +00:00
}
}
2021-01-05 20:59:15 +00:00
$this -> add_message ( __ ( 'Settings Imported' , 'woocommerce-beta-tester' ), 'updated' );
2020-11-06 18:36:02 +00:00
} else {
2021-01-05 20:59:15 +00:00
$this -> add_message ( __ ( 'File did not contain well formed JSON.' , 'woocommerce-beta-tester' ) );
2020-11-05 18:57:21 +00:00
}
}
2020-11-03 18:27:32 +00:00
/**
* Get an array of the WooCommerce related settings .
*/
protected function get_settings () {
$settings = array ();
if ( current_user_can ( 'manage_woocommerce' ) ) {
2020-11-05 18:57:21 +00:00
foreach ( $this -> get_setting_list () as $option_name ) {
$setting = get_option ( $option_name );
2020-11-06 18:51:45 +00:00
if ( false === $setting ) {
$setting = null ;
}
2023-08-09 00:20:34 +00:00
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
2020-11-05 18:57:21 +00:00
$settings [ $option_name ] = is_string ( $setting ) ? $setting : serialize ( $setting );
2020-11-03 18:27:32 +00:00
}
}
return $settings ;
}
2020-11-05 18:57:21 +00:00
2020-11-06 18:36:02 +00:00
/**
2021-01-05 20:59:15 +00:00
* Add a settings import status message .
2020-11-06 18:36:02 +00:00
*
* @ param string $message Message string .
* @ param string $type Message type . Optional . Default 'error' .
*/
protected function add_message ( $message , $type = 'error' ) {
$this -> message = array (
'message' => $message ,
2023-08-09 00:20:34 +00:00
'type' => $type ,
2020-11-06 18:36:02 +00:00
);
}
2020-11-05 18:57:21 +00:00
/**
* Get the WooCommerce settings list keys .
*/
private function get_setting_list () {
2023-08-09 00:20:34 +00:00
require_once dirname ( __FILE__ ) . '/wc-beta-tester-settings-list.php' ;
2020-11-05 18:57:21 +00:00
return wc_beta_tester_setting_list ();
}
2020-11-03 18:27:32 +00:00
}