Skip to content

Commit

Permalink
2.4.0
Browse files Browse the repository at this point in the history
- Added ID Express, SAP, and LION Parcel to list of couriers
- Added warning that you need to refresh the setting page after entering API Key
- Renamed "wcis" to "ongkir"
  • Loading branch information
hrsetyono committed Sep 21, 2022
1 parent 88a72c3 commit 4b088bd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 36 deletions.
4 changes: 2 additions & 2 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function shipping_init() {
* @filter woocommerce_shipping_methods
*/
function shipping_method($methods) {
$methods['wcis'] = 'Ongkir_Method';
$methods['wcis_zone'] = 'Ongkir_Zone';
$methods['ongkir'] = 'Ongkir_Method';
$methods['ongkir_zone'] = 'Ongkir_Zone';
return $methods;
}
}
Expand Down
46 changes: 24 additions & 22 deletions admin/ongkir-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class Ongkir_Method extends WC_Shipping_Method {
private $api;

public function __construct($instance_id = 0) {
$this->id = 'wcis'; // wcis is the old plugin name
$this->title = __('Indo Ongkir');
$this->method_title = __('Indo Ongkir');
$this->method_description = __('Indonesian domestic shipping with JNE, TIKI, or POS');
$this->id = 'ongkir';
$this->title = __('Ongkir Indonesia');
$this->method_title = __('Ongkir Indonesia');
$this->method_description = __('Indonesian domestic shipping with JNE, J&T, Ninja Xpress, Sicepat, TIKI, or POS');

$this->enabled = $this->get_option('enabled');
$this->init_form_fields();
Expand All @@ -28,7 +28,7 @@ public function __construct($instance_id = 0) {
}

/**
* Initiate global setting page for WCIS
* Initiate global setting page
*/
function init_form_fields() {
$enabled_field = [
Expand All @@ -52,7 +52,7 @@ function init_form_fields() {
$key_field = [
'title' => __('API Key'),
'type' => 'password',
'description' => __('Signup at <a href="http://rajaongkir.com/akun/daftar" target="_blank">rajaongkir.com</a> and choose Pro license (Paid). Paste the API Key here'),
'description' => __('Signup at <a href="http://rajaongkir.com/akun/daftar" target="_blank">rajaongkir.com</a> and choose Pro license (Paid). Paste the API Key here. <br> ⚠️ <span style="color:#f44336">If nothing happen after saving, try refreshing the page as it takes a while to validate the key.</span>'),
];

$city_field = [
Expand All @@ -65,7 +65,6 @@ function init_form_fields() {

$this->form_fields = [
'key' => $key_field,
// 'type' => $type_field,
];

// if key is valid, show the other setting fields
Expand All @@ -77,6 +76,7 @@ function init_form_fields() {

// set service fields by each courier
$couriers = Ongkir_Data::get_couriers();

foreach ($couriers as $id => $name) {
$this->form_fields[$id . '_services'] = [
'title' => $name,
Expand All @@ -86,36 +86,34 @@ function init_form_fields() {
'options' => Ongkir_Data::get_services($id, true)
];
}

} // if valid
}
}


/**
* Add API Key to Transient so it's cached
*/
function process_admin_transients() {
$t_license = get_transient('wcis_license');
$cached_license = get_transient('ongkir_license');

$post_data = $this->get_post_data();
$key = $post_data['woocommerce_wcis_key'];
$key = $post_data['woocommerce_ongkir_key'];

// check license
$license_valid = isset($t_license['valid']) && $t_license['valid'];
$license_different = isset($t_license['key']) && $t_license['key'] === $key;
$license_different = isset($cached_license['key']) && $cached_license['key'] === $key;
$license_valid = isset($cached_license['valid']) && $cached_license['valid'];

// if not valid OR different from before, update transient
if (!$license_valid || $license_different) {
if ($license_different || !$license_valid) {
$rj = new RajaOngkir($key);
$t_license = [
$license = [
'key' => $key,
'valid' => $rj->is_valid()
];

set_transient('wcis_license', $t_license, 60*60*24*30);
set_transient('ongkir_license', $license, 60*60*24*30);
// return $license;
}

return $t_license;
}


Expand All @@ -125,12 +123,16 @@ function process_admin_transients() {
/**
* Validate API Key by doing a sample AJAX call
* @return bool
*
* @warn - due to woocommerce update, the transient isn't saved when the page first refreshed. So need to refresh the page again
*/
private function check_key_valid() {
$license = get_transient('wcis_license');

// if key doesn't exist, abort
if (!isset($license['key'])) { return false; }
$license = get_transient('ongkir_license');

// if key doesn't exist OR empty, abort
if (!isset($license['key']) || $license['key'] === '') {
return false;
}

// if valid, return success
if (isset($license['valid']) && $license['valid']) {
Expand Down
12 changes: 6 additions & 6 deletions admin/ongkir-zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class Ongkir_Zone extends WC_Shipping_Method {
private $main_settings;

public function __construct($instance_id = 0) {
$this->id = 'wcis_zone'; // wcis is the old plugin name
$this->id = 'ongkir_zone';
$this->instance_id = absint($instance_id);

$this->title = __('Indo Ongkir');
$this->method_title = __('Indo Ongkir');
$this->method_description = __('Indonesian domestic shipping with JNE, TIKI, or POS');
$this->title = __('Ongkir Indonesia');
$this->method_title = __('Ongkir Indonesia');
$this->method_description = __('Indonesian domestic shipping with JNE, TIKI, Ninja Xpress, Sicepat, or POS');
$this->supports = ['shipping-zones', 'instance-settings'];

// global
$this->main_settings = get_option('woocommerce_wcis_settings');
$this->main_settings = get_option('woocommerce_ongkir_settings');
$this->api = new RajaOngkir($this->main_settings['key']);

// allow save setting
Expand Down Expand Up @@ -157,7 +157,7 @@ private function _calculate_weight($package) {
}
// if no weight data, return default weight or 1kg
else {
$weight = (int) ceil(apply_filters('wcis_default_weight', 1000));
$weight = (int) ceil(apply_filters('ongkir_default_weight', 1000));
return $weight;
}
}
Expand Down
36 changes: 36 additions & 0 deletions includes/data/couriers.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,41 @@
"title": "Regular"
}
}
},

"ide": {
"code": "ide",
"name": "ID Express",
"services": {
"STD": {
"title": "Standard Service"
}
}
},

"lion": {
"code": "lion",
"name": "LION Parcel",
"services": {
"REGPACK": {
"title": "Regular Service"
},
"JAGOPACK": {
"title": "Economy Service"
}
}
},

"sap": {
"code": "sap",
"name": "SAP Express",
"services": {
"REG": {
"title": "Reguler"
},
"CARGO DARAT": {
"title": "Reguler Darat (Min 5kg)"
}
}
}
}
2 changes: 1 addition & 1 deletion includes/rajaongkir.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function __construct($key = null) {
$this->api_key = $key;
}
else {
$cached_license = get_transient('wcis_license');
$cached_license = get_transient('ongkir_license');
$this->api_key = $cached_license['key'];
}
}
Expand Down
10 changes: 5 additions & 5 deletions wc-ongkir-indonesia.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* Plugin Name: Ongkos Kirim Indonesia for WooCommerce
* Plugin Name: Ongkir Indonesia for WooCommerce
* Plugin URI: http://github.com/hrsetyono/wc-ongkir-indonesia
* Description: Calculate the shipping costs for Indonesian couriers like JNE, J&T, TIKI, POS, etc. Requires RajaOngkir PRO License.
* Version: 2.3.2
* Description: Calculate the shipping costs for Indonesian couriers like JNE, J&T, Ninja Xpress, Sicepat, TIKI, POS, etc. Requires RajaOngkir PRO License.
* Version: 2.4.0
* Author: Pixel Studio
* Author URI: https://pixelstudio.id/
* License: GPL-3.0+
Expand All @@ -18,7 +18,7 @@
return;
}

define('ONGKIR_VERSION', '2.3.2');
define('ONGKIR_VERSION', '2.4.0');
define('ONGKIR_FILE', plugins_url('', __FILE__));
define('ONGKIR_DIR', __DIR__);
define('ONGKIR_NAMESPACE', 'ongkir/v1');
Expand All @@ -34,7 +34,7 @@
* Run the plugin
*/
function ongkir_run_plugin() {
$settings = get_option('woocommerce_wcis_settings');
$settings = get_option('woocommerce_ongkir_settings');
$enabled = isset($settings['enabled']) ? $settings['enabled'] : 'no';

new Ongkir_Admin($enabled);
Expand Down

0 comments on commit 4b088bd

Please sign in to comment.