Files
woo-viet/woo-viet.php
T

221 lines
5.4 KiB
PHP
Raw Normal View History

2017-01-01 00:37:34 +07:00
<?php
/**
2017-01-03 12:37:56 +07:00
* Plugin Name: Woo Viet - WooCommerce for Vietnam
2017-01-02 08:18:01 +07:00
* Plugin URI: https://github.com/htdat/woo-viet
2017-01-01 00:37:34 +07:00
* Description: This plugin provides features and integrations specifically for Vietnam.
* Author: htdat
* Author URI: https://profiles.wordpress.org/htdat
2017-01-02 08:18:01 +07:00
* Text Domain: woo-viet
2017-01-01 00:37:34 +07:00
* Domain Path: /languages
2017-04-20 17:40:14 +07:00
* Version: 1.3-dev
2017-01-01 00:37:34 +07:00
* License: GPLv2+
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'WOO_VIET_DIR', plugin_dir_path( __FILE__ ) );
define( 'WOO_VIET_URL', plugins_url( '/', __FILE__ ) );
2017-01-01 00:37:34 +07:00
/**
* Start the instance
*/
new WooViet();
/**
* The main class of the plugin
*
* @author htdat
* @since 1.0
*/
class WooViet {
2017-01-01 14:50:53 +07:00
/**
* @var array The default settings for the whole plugin
*/
2017-01-01 00:37:34 +07:00
static $default_settings = array(
'add_province' =>
array(
'enabled' => 'yes',
),
2017-04-22 09:48:29 +07:00
'add_city' =>
array(
'enabled' => 'yes',
),
2017-01-01 00:37:34 +07:00
'change_currency_symbol' =>
array(
'enabled' => 'yes',
'text' => 'VND',
),
'convert_price' =>
array(
'enabled' => 'yes',
'text' => 'K',
),
'vnd_paypal_standard' =>
array(
'enabled' => 'yes',
'currency' => 'USD',
'rate' => '22770',
),
2017-04-22 09:48:29 +07:00
'add_onepay_domestic' =>
2017-04-20 17:39:11 +07:00
array(
'enabled' => 'yes',
),
2017-04-22 09:48:29 +07:00
);
2017-01-01 14:50:53 +07:00
/**
* 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;
2017-01-01 18:38:28 +07:00
protected $VND_PayPal_Standard;
2017-01-01 14:50:53 +07:00
protected $Admin_Page;
2017-01-01 00:37:34 +07:00
/**
* Setup class.
*
* @since 1.0
*/
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
/**
* Throw a notice if WooCommerce is NOT active
*/
2017-01-01 15:19:30 +07:00
public function notice_if_not_woocommerce() {
$class = 'notice notice-error';
2017-01-02 08:18:01 +07:00
$message = __( 'Woo Viet is not running because WooCommerce is not active. Please activate both plugins',
'woo-viet' );
printf( '<div class="%1$s"><p><strong>%2$s</strong></p></div>', $class, $message );
}
2017-01-01 00:37:34 +07:00
/**
* Run this method under the "init" action
*/
2017-01-01 15:19:30 +07:00
public function init() {
// Load the localization feature
$this->i18n();
if ( class_exists( 'WooCommerce' ) ) {
// Run this plugin normally if WooCommerce is active
$this->main();
} else {
// Throw a notice if WooCommerce is NOT active
2017-01-01 15:19:30 +07:00
add_action( 'admin_notices', array( $this, 'notice_if_not_woocommerce' ) );
}
2017-01-01 00:37:34 +07:00
}
2017-01-01 15:19:30 +07:00
/**
* Localize the plugin
* @since 1.0
*/
public function i18n() {
2017-02-26 16:24:05 +07:00
load_plugin_textdomain( 'woo-viet', false, basename( dirname( __FILE__ ) ) . '/languages/' );
2017-01-01 15:19:30 +07:00
}
/**
* The main method to load the components
*/
public function main() {
2017-01-01 00:37:34 +07:00
if ( is_admin() ) {
2017-01-01 14:50:53 +07:00
// Add the admin setting page
2017-01-01 00:37:34 +07:00
include( WOO_VIET_DIR . 'inc/class-wooviet-admin-page.php' );
2017-01-01 14:50:53 +07:00
$this->Admin_Page = new WooViet_Admin_Page();
2017-01-01 00:37:34 +07:00
}
$settings = self::get_settings();
2017-04-22 09:48:29 +07:00
2017-04-20 17:39:11 +07:00
// Check if "Add the OnePay Domestic Gateway" is enabled
if ( 'yes' == $settings['add_onepay_domestic']['enabled'] ) {
2017-04-22 09:48:29 +07:00
include( 'inc/class-wooviet-onepay-domestic.php' );
2017-04-20 17:39:11 +07:00
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway_class' ) );
2017-04-22 09:48:29 +07:00
// Add the action to check the cron job for handling queryDR
// It's not possible to add in the class "WooViet_OnePay_Domestic_Hook" because it's NOT always loadded
if ( defined( 'DOING_CRON' ) and DOING_CRON ) {
$this->WooViet_OnePay_Domestic_Hook = new WooViet_OnePay_Domestic();
add_action( 'wooviet_handle_onepay_querydr', array(
$this->WooViet_OnePay_Domestic_Hook,
'handle_onepay_querydr'
), 10, 1 );
}
2017-04-20 17:39:11 +07:00
}
2017-01-01 00:37:34 +07:00
// Check if "Add provinces for Vietnam " is enabled.
if ( 'yes' == $settings['add_province']['enabled'] ) {
include( WOO_VIET_DIR . 'inc/class-wooviet-provinces.php' );
$this->Provinces = new WooViet_Provinces();
// Enable "Add cities for Vietnam" if the province option is selected.
if ( 'yes' == $settings['add_city']['enabled'] ) {
include( WOO_VIET_DIR . 'inc/class-wooviet-cities.php' );
new WooViet_Cities();
}
2017-01-01 00:37:34 +07:00
}
include( WOO_VIET_DIR . 'inc/class-wooviet-currency.php' );
$this->Currency = new WooViet_Currency();
// Check if "Change VND currency symbol" is enabled
if ( 'yes' == $settings['change_currency_symbol']['enabled'] ) {
$this->Currency->change_currency_symbol( $settings['change_currency_symbol']['text'] );
}
// Check if "Convert 000 of prices to K (or anything)" is enabled
if ( 'yes' == $settings['convert_price']['enabled'] ) {
$this->Currency->convert_price_thousand_to_k( $settings['convert_price']['text'] );
}
2017-01-01 18:38:28 +07:00
// Check if "Support VND for the PayPal Standard gateway" is enabled
2017-01-01 00:37:34 +07:00
if ( 'yes' == $settings['vnd_paypal_standard']['enabled'] ) {
include( WOO_VIET_DIR . 'inc/class-wooviet-vnd-paypal-standard.php' );
2017-01-01 18:38:28 +07:00
$this->VND_PayPal_Standard = new WooViet_VND_PayPal_Standard(
2017-01-01 00:37:34 +07:00
$settings['vnd_paypal_standard']['rate'],
$settings['vnd_paypal_standard']['currency']
);
}
}
2017-01-01 14:50:53 +07:00
/**
* The wrapper method to get the settings of the plugin
* @return array
*/
static function get_settings() {
$settings = get_option( 'woo-viet', self::$default_settings );
$settings = wp_parse_args( $settings, self::$default_settings );
2017-04-22 09:48:29 +07:00
return $settings;
2017-01-01 14:50:53 +07:00
}
2017-04-22 09:48:29 +07:00
/**
* Add the gateways to WooCommerce
*
* @param array $methods
*
* @since 1.3
* @return array
*/
public function add_gateway_class( $methods ) {
$methods[] = 'WooViet_OnePay_Domestic';
return $methods;
}
2017-01-01 00:37:34 +07:00
}