From 4b088bd34f3bc04c6444bf324dac52ceb704ca53 Mon Sep 17 00:00:00 2001 From: Henner S Date: Wed, 21 Sep 2022 10:19:10 +0700 Subject: [PATCH] 2.4.0 - 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" --- admin/index.php | 4 ++-- admin/ongkir-method.php | 46 +++++++++++++++++++------------------ admin/ongkir-zone.php | 12 +++++----- includes/data/couriers.json | 36 +++++++++++++++++++++++++++++ includes/rajaongkir.php | 2 +- wc-ongkir-indonesia.php | 10 ++++---- 6 files changed, 74 insertions(+), 36 deletions(-) diff --git a/admin/index.php b/admin/index.php index 0a76444..090b484 100644 --- a/admin/index.php +++ b/admin/index.php @@ -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; } } diff --git a/admin/ongkir-method.php b/admin/ongkir-method.php index da303b3..841a4c4 100644 --- a/admin/ongkir-method.php +++ b/admin/ongkir-method.php @@ -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(); @@ -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 = [ @@ -52,7 +52,7 @@ function init_form_fields() { $key_field = [ 'title' => __('API Key'), 'type' => 'password', - 'description' => __('Signup at rajaongkir.com and choose Pro license (Paid). Paste the API Key here'), + 'description' => __('Signup at rajaongkir.com and choose Pro license (Paid). Paste the API Key here.
⚠️ If nothing happen after saving, try refreshing the page as it takes a while to validate the key.'), ]; $city_field = [ @@ -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 @@ -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, @@ -86,8 +86,7 @@ function init_form_fields() { 'options' => Ongkir_Data::get_services($id, true) ]; } - - } // if valid + } } @@ -95,27 +94,26 @@ function init_form_fields() { * 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; } @@ -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']) { diff --git a/admin/ongkir-zone.php b/admin/ongkir-zone.php index 5b046b5..5ef7e56 100644 --- a/admin/ongkir-zone.php +++ b/admin/ongkir-zone.php @@ -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 @@ -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; } } diff --git a/includes/data/couriers.json b/includes/data/couriers.json index 79177e4..9ff452a 100644 --- a/includes/data/couriers.json +++ b/includes/data/couriers.json @@ -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)" + } + } } } diff --git a/includes/rajaongkir.php b/includes/rajaongkir.php index 208fc41..6db6233 100644 --- a/includes/rajaongkir.php +++ b/includes/rajaongkir.php @@ -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']; } } diff --git a/wc-ongkir-indonesia.php b/wc-ongkir-indonesia.php index d3082cc..4b54f90 100644 --- a/wc-ongkir-indonesia.php +++ b/wc-ongkir-indonesia.php @@ -1,9 +1,9 @@