Update: Reformat code and add comments

This commit is contained in:
Dat Hoang
2017-01-01 14:50:53 +07:00
parent dc16ee94a1
commit ca048b961b
2 changed files with 177 additions and 114 deletions
+87 -35
View File
@@ -13,17 +13,49 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class WooViet_Admin_Page {
/**
* @var string The message to display after saving settings
*/
var $message = '';
/**
* WooViet_Admin_Page constructor.
*/
public function __construct() {
if (isset($_REQUEST['wooviet_nonce']) && isset ($_REQUEST['action']) && 'wooviet_save_settings' == $_REQUEST['action'])
// Catch and run the save_settings() action
if ( isset( $_REQUEST['wooviet_nonce'] ) && isset ( $_REQUEST['action'] ) && 'wooviet_save_settings' == $_REQUEST['action'] ) {
$this->save_settings();
}
add_action( 'admin_menu', array( $this, 'register_submenu_page' ) );
}
/**
* Save settings for the plugin
*/
public function save_settings() {
if ( wp_verify_nonce( $_REQUEST['wooviet_nonce'], 'wooviet_save_settings' ) ) {
update_option( 'woocommerce-for-vietnam', $_REQUEST['settings'] );
$this->message =
'<div class="updated notice"><p><strong>' .
__( 'Settings saved', 'woocommerce-for-vietnam' ) .
'</p></strong></div>';
} else {
$this->message =
'<div class="error notice"><p><strong>' .
__( 'Can not save settings! Please refresh this page.', 'woocommerce-for-vietnam' ) .
'</p></strong></div>';
}
}
/**
* Register the sub-menu under "WooCommerce"
* Link: http://my-site.com/wp-admin/admin.php?page=woocommerce-for-vietnam
*/
public function register_submenu_page() {
add_submenu_page(
'woocommerce',
@@ -35,25 +67,9 @@ class WooViet_Admin_Page {
);
}
public function save_settings(){
if ( wp_verify_nonce( $_REQUEST['wooviet_nonce'], 'wooviet_save_settings') ){
update_option('woocommerce-for-vietnam', $_REQUEST['settings']);
$this->message =
'<div class="updated notice"><p><strong>' .
__('Settings saved', 'woocommerce-for-vietnam') .
'</p></strong></div>'
;
} else {
$this->message =
'<div class="error notice"><p><strong>' .
__('Please refresh this page. Can not save settings!', 'woocommerce-for-vietnam') .
'</p></strong></div>'
;
}
}
/**
* Generate the HTML code of the settings page
*/
public function admin_page_html() {
// check user capabilities
if ( ! current_user_can( 'manage_options' ) ) {
@@ -61,8 +77,6 @@ class WooViet_Admin_Page {
}
$settings = WooViet::get_settings();
//var_dump($settings); exit;
?>
<div class="wrap">
@@ -70,7 +84,8 @@ class WooViet_Admin_Page {
<form name="woocommerce_for_vietnam" method="post">
<?php echo $this->message ?>
<input type="hidden" id="action" name="action" value="wooviet_save_settings">
<input type="hidden" id="wooviet_nonce" name="wooviet_nonce" value="<?php echo wp_create_nonce( 'wooviet_save_settings' ) ?>">
<input type="hidden" id="wooviet_nonce" name="wooviet_nonce"
value="<?php echo wp_create_nonce( 'wooviet_save_settings' ) ?>">
<table class="form-table">
<tbody>
<tr>
@@ -78,7 +93,8 @@ class WooViet_Admin_Page {
<td>
<input name="settings[add_province][enabled]" type="hidden" value="no">
<input name="settings[add_province][enabled]" type="checkbox" id="add_province" value="yes"
<?php if('yes' == $settings['add_province']['enabled'] ) echo 'checked="checked"'?>>
<?php if ( 'yes' == $settings['add_province']['enabled'] )
echo 'checked="checked"' ?>>
<label for="add_province"><?php _e( 'Enabled', 'woocommerce-for-vietnam' ) ?></label>
</td>
</tr>
@@ -87,8 +103,10 @@ class WooViet_Admin_Page {
<th scope="row"><?php _e( 'Change VND currency symbol', 'woocommerce-for-vietnam' ) ?></th>
<td>
<input name="settings[change_currency_symbol][enabled]" type="hidden" value="no">
<input name="settings[change_currency_symbol][enabled]" type="checkbox" id="change_currency_symbol" value="yes"
<?php if('yes' == $settings['change_currency_symbol']['enabled'] )echo 'checked="checked"'?>>
<input name="settings[change_currency_symbol][enabled]" type="checkbox"
id="change_currency_symbol" value="yes"
<?php if ( 'yes' == $settings['change_currency_symbol']['enabled'] )
echo 'checked="checked"' ?>>
<label for="change_currency_symbol"><?php _e( 'Enabled', 'woocommerce-for-vietnam' ) ?></label>
<br/>
<br/>
@@ -102,15 +120,18 @@ class WooViet_Admin_Page {
<th scope="row"><?php _e( 'Convert <code>000</code> of prices to K (or anything)', 'woocommerce-for-vietnam' ) ?></th>
<td>
<input name="settings[convert_price][enabled]" type="hidden" value="no">
<input name="settings[convert_price][enabled]" type="checkbox" id="convert_price" value="yes"
<?php if('yes' == $settings['convert_price']['enabled'] ) echo 'checked="checked"'?>>
<input name="settings[convert_price][enabled]" type="checkbox" id="convert_price"
value="yes"
<?php if ( 'yes' == $settings['convert_price']['enabled'] )
echo 'checked="checked"' ?>>
<label for="convert_price"><?php _e( 'Enabled', 'woocommerce-for-vietnam' ) ?></label>
<fieldset><br/>
<input type="text" name="settings[convert_price][text]"
value="<?php echo $settings['convert_price']['text'] ?>"
id="convert_price_text" class="small-text">
<label for="convert_price_text"><?php _e('Choose what you want to change. E.g:', 'woocommerce-for-vietnam')?> <code>K</code>, <code>nghìn</code>, <code>ngàn</code></label>
<label for="convert_price_text"><?php _e( 'Choose what you want to change. E.g:', 'woocommerce-for-vietnam' ) ?>
<code>K</code>, <code>nghìn</code>, <code>ngàn</code></label>
</fieldset>
</td>
</tr>
@@ -118,14 +139,44 @@ class WooViet_Admin_Page {
<th scope="row"><?php printf( __( 'Support VND for <a href="%s">the Paypal Standard gateway</a>', 'woocommerce-for-vietnam' ), 'https://docs.woocommerce.com/document/paypal-standard/' ) ?></th>
<td>
<input name="settings[vnd_paypal_standard][enabled]" type="hidden" value="no">
<input name="settings[vnd_paypal_standard][enabled]" type="checkbox" id="vnd_paypal_standard" value="yes"
<?php if('yes' == $settings['vnd_paypal_standard']['enabled'] ) echo 'checked="checked"'?>>
<input name="settings[vnd_paypal_standard][enabled]" type="checkbox"
id="vnd_paypal_standard" value="yes"
<?php if ( 'yes' == $settings['vnd_paypal_standard']['enabled'] )
echo 'checked="checked"' ?>>
<label for="vnd_paypal_standard"><?php _e( 'Enabled', 'woocommerce-for-vietnam' ) ?></label>
<fieldset><br/>
<select name="settings[vnd_paypal_standard][currency]" id="vnd_paypal_standard_currency">
<select name="settings[vnd_paypal_standard][currency]"
id="vnd_paypal_standard_currency">
<?php
$paypal_supported_currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB' );
$paypal_supported_currencies = array(
'AUD',
'BRL',
'CAD',
'MXN',
'NZD',
'HKD',
'SGD',
'USD',
'EUR',
'JPY',
'TRY',
'NOK',
'CZK',
'DKK',
'HUF',
'ILS',
'MYR',
'PHP',
'PLN',
'SEK',
'CHF',
'TWD',
'THB',
'GBP',
'RMB',
'RUB'
);
foreach ( $paypal_supported_currencies as $currency ) {
if ( strtoupper( $currency ) == $settings['vnd_paypal_standard']['currency'] ) {
@@ -141,7 +192,8 @@ class WooViet_Admin_Page {
<br/>
<br/>
<input name="settings[vnd_paypal_standard][rate]" type="number" step="1" min="100" id="vnd_paypal_standard_rate" style="width: 70px;"
<input name="settings[vnd_paypal_standard][rate]" type="number" step="1" min="100"
id="vnd_paypal_standard_rate" style="width: 70px;"
value="<?php echo $settings['vnd_paypal_standard']['rate'] ?>"
<label for="vnd_paypal_standard_rate"><?php _e( 'Insert the exchange rate of this currency to VND', 'woocommerce-for-vietnam' ) ?></label>
</fieldset>
+24 -13
View File
@@ -32,12 +32,9 @@ new WooViet();
*/
class WooViet {
protected $Provinces;
protected $Currency;
protected $VND_Paypal_Standard;
// protected $settings;
/**
* @var array The default settings for the whole plugin
*/
static $default_settings = array(
'add_province' =>
array(
@@ -60,6 +57,17 @@ class WooViet {
'rate' => '22770',
),
);
/**
* The properties to manage all classes under the "inc/" folder
* Example:
* - File name: class-wooviet-provinces.php
* - Class Name: \WooViet_Provinces
* - Method Name: WooViet->Provinces
*/
protected $Provinces;
protected $Currency;
protected $VND_Paypal_Standard;
protected $Admin_Page;
/**
* Setup class.
@@ -75,9 +83,9 @@ class WooViet {
public function init() {
if ( is_admin() ) {
// @todo: Add the admin setting page
// Add the admin setting page
include( WOO_VIET_DIR . 'inc/class-wooviet-admin-page.php' );
new WooViet_Admin_Page();
$this->Admin_Page = new WooViet_Admin_Page();
}
@@ -114,6 +122,14 @@ class WooViet {
}
/**
* The wrapper method to get the settings of the plugin
* @return array
*/
static function get_settings() {
return get_option( 'woocommerce-for-vietnam', self::$default_settings );
}
/**
* Localize the plugin
* @since 1.0
@@ -122,9 +138,4 @@ class WooViet {
load_plugin_textdomain( 'freshfunbits', false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
static function get_settings() {
return get_option( 'woocommerce-for-vietnam', self::$default_settings );
}
}