Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 4193296
Author: Henner R Setyono <[email protected]>
Date:   Thu May 19 11:39:04 2022 +0700

    2.2.0

commit 45808d5
Author: Henner R Setyono <[email protected]>
Date:   Thu May 19 11:08:31 2022 +0700

    2.2.0

    Finished refactoring

commit 2fae8a4
Author: Henner R Setyono <[email protected]>
Date:   Wed May 18 15:57:33 2022 +0700

    refactor 2

commit e1f4cc9
Author: Henner R Setyono <[email protected]>
Date:   Wed May 18 11:48:12 2022 +0700

    refactoring in progress
  • Loading branch information
hrsetyono committed May 19, 2022
1 parent 20ec173 commit ffae673
Show file tree
Hide file tree
Showing 73 changed files with 3,596 additions and 1,483 deletions.
37 changes: 37 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"root": true,
"env": {
"node": true,
"browser": true,
"es6": true
},
"extends": [
"airbnb-base"
],
"parserOptions": {
"parser": "babel-eslint"
},
"plugins": [],
"rules": {
"max-len": 0,
"linebreak-style": 0,
"no-alert": "off",
"no-param-reassign": [2, { "props": false }],
"prefer-destructuring": [
"error",
{
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": false,
"object": false
}
},
{
"enforceForRenamedProperties": false
}
]
}
}
675 changes: 670 additions & 5 deletions LICENSE.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ Done! You will now see additional fields for City and District (Kecamatan) when

## 3. Changelog

2.2.0 (19 May 2022)

- Refactored the plugin in preparation to be submitted to official repository.

2.1.0 (17 Jan 2022)

- Added support for WooCommerce 5.9.
Expand Down
1 change: 1 addition & 0 deletions admin/assets/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './admin.sass';
File renamed without changes.
34 changes: 34 additions & 0 deletions admin/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

if (!class_exists('Ongkir_Admin')):

class Ongkir_Admin {
function __construct($enabled = 'no') {
// run this code even if disabled
add_action('woocommerce_shipping_init', [$this, 'shipping_init']);
add_filter('woocommerce_shipping_methods', [$this, 'shipping_method']);
}

/**
* Initiate WC Shipping
*
* @filter woocommerce_shipping_init
*/
function shipping_init() {
require_once __DIR__ . '/ongkir-method.php';
require_once __DIR__ . '/ongkir-zone.php';
}

/**
* Add our custom Shipping method
*
* @filter woocommerce_shipping_methods
*/
function shipping_method($methods) {
$methods['wcis'] = 'Ongkir_Method';
$methods['wcis_zone'] = 'Ongkir_Zone';
return $methods;
}
}

endif;
81 changes: 49 additions & 32 deletions module-admin/init-main.php → admin/ongkir-method.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
<?php
require_once WCIS_DIR . '/helper/rajaongkir.php';
if (!defined('ABSPATH')) { exit; }

require_once ONGKIR_DIR . '/includes/rajaongkir.php';
require_once ONGKIR_DIR . '/includes/ongkir-data.php';


if (!class_exists('Ongkir_Method')):

/**
* Global setting for Indo Shipping
*/
class WCIS_Method extends WC_Shipping_Method {
class Ongkir_Method extends WC_Shipping_Method {
private $api;

public function __construct($instance_id = 0) {
$this->id = 'wcis';
$this->title = __('Indo Shipping');
$this->method_title = __('Indo Shipping');
$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->enabled = $this->get_option('enabled');
Expand All @@ -25,49 +31,60 @@ public function __construct($instance_id = 0) {
* Initiate global setting page for WCIS
*/
function init_form_fields() {
$enabled_field = array(
$enabled_field = [
'title' => __('Enable/Disable'),
'type' => 'checkbox',
'label' => __('Enable Indo Shipping'),
'description' => __( 'Tick this then go to Shipping Zone > Create Indonesia Zone > Add Shipping Method > Choose "Indo Shipping"' ),
'default' => 'yes'
);

$key_field = array(
];

// $type_field = [
// 'title' => __('License Type'),
// 'type' => '',
// 'description' => __('Starter is free, but limited to JNE, TIKI, and POS'),
// 'options' => [
// 'pro' => 'Pro',
// 'starter' => 'Starter',
// ],
// ];

$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'),
);
];

$city_field = array(
$city_field = [
'title' => __('City Origin'),
'type' => 'select',
// 'class' => 'wc-enhanced-select', // bugged!! doesn't save the value
'description' => __('Ship from where? <br> Change your province at General > Store Address'),
'options' => array()
);
'options' => [],
];

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

// if key is valid, show the other setting fields
if( $this->check_key_valid() ) {
if ($this->check_key_valid()) {
$city_field['options'] = $this->get_cities_origin();

$this->form_fields['enabled'] = $enabled_field;
$this->form_fields['city'] = $city_field;

// set service fields by each courier
$couriers = wcis_get_couriers();
foreach( $couriers as $id => $name ) {
$this->form_fields[$id . '_services'] = array(
$couriers = Ongkir_Data::get_couriers();
foreach ($couriers as $id => $name) {
$this->form_fields[$id . '_services'] = [
'title' => $name,
'type' => 'multiselect',
'class' => 'wc-enhanced-select',
'description' => __("Choose allowed services by $name."),
'options' => wcis_get_services($id, true)
);
'options' => Ongkir_Data::get_services($id, true)
];
}

} // if valid
Expand All @@ -88,7 +105,7 @@ function process_admin_transients() {
$license_different = isset($t_license['key']) && $t_license['key'] === $key;

// if not valid OR different from before, update transient
if(!$license_valid || $license_different) {
if (!$license_valid || $license_different) {
$rj = new RajaOngkir($key);
$t_license = [
'key' => $key,
Expand All @@ -113,15 +130,15 @@ private function check_key_valid() {
$license = get_transient('wcis_license');

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

// if valid, return success
if(isset($license['valid']) && $license['valid']) {
if (isset($license['valid']) && $license['valid']) {
$msg = __('API Connected!');
$this->form_fields['key']['description'] = '<span style="color: #4caf50;">' . $msg . '</span>';
}
else {
$msg = __('Invalid API Key. Are you using non-Pro license?');
$msg = __('Invalid API Key. Make sure it is a PRO license');
$this->form_fields['key']['description'] = '<span style="color:#f44336;">' . $msg . '</span>';
}

Expand All @@ -134,14 +151,14 @@ private function check_key_valid() {
*/
private function get_cities_origin() {
$country = wc_get_base_location();
$prov_id = wcis_get_province_id( $country['state'] );
$prov_id = Ongkir_Data::get_province_id($country['state']);

// get cities data
$cities_raw = wcis_get_cities( $prov_id );
$cities_raw = Ongkir_Data::get_cities($prov_id);

// parse raw data
$cities = array();
foreach( $cities_raw as $id => $value ) {
$cities = [];
foreach ($cities_raw as $id => $value) {
$cities[$id] = $value['city_name'];
}

Expand All @@ -157,6 +174,6 @@ private function get_cities_origin() {
private function _set_license_cache() {

}


}

endif;
61 changes: 36 additions & 25 deletions module-admin/init-zones.php → admin/ongkir-zone.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
<?php
if (!defined('ABSPATH')) { exit; }

require_once ONGKIR_DIR . '/includes/rajaongkir.php';
require_once ONGKIR_DIR . '/includes/ongkir-data.php';


if (!class_exists('Ongkir_Zone')):

/**
* Zone setting for Indo Shipping
*/
class WCIS_Zones_Method extends WC_Shipping_Method {
class Ongkir_Zone extends WC_Shipping_Method {
private $api;
private $main_settings;

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

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

// global
$this->main_settings = get_option('woocommerce_wcis_settings');
Expand All @@ -31,7 +39,7 @@ public function __construct($instance_id = 0) {
function calculate_shipping($package = []) {
// if district not exists or empty
$id_exists = array_key_exists('destination_id', $package['destination']);
if(!$id_exists || empty($package['destination']['destination_id'])) {
if (!$id_exists || empty($package['destination']['destination_id'])) {
return false;
}

Expand Down Expand Up @@ -72,37 +80,38 @@ private function _get_costs($package) {
* @param array $costs - Cost list from API
*/
private function _set_rate($costs) {
if(!$costs) { return; }
if (!$costs) { return; }

// format the costs from API to WooCommerce
foreach($costs as $courier):
if(empty($courier)) { break; }
foreach ($costs as $courier):
if (empty($courier)) { break; }

// get full list of services
$code = $courier['code'];
if($code === 'J&T') { $code = 'jnt'; } // for weird reason, the response code for 'jnt' is 'J&T'
$all_services = wcis_get_services($code);
if ($code === 'J&T') { $code = 'jnt'; } // for weird reason, the response code for 'jnt' is 'J&T'

$all_services = Ongkir_Data::get_services($code);

// get allowed service from this courier
$setting_id = $code . '_services';
$allowed_services = isset($this->main_settings[$setting_id]) ? $this->main_settings[$setting_id] : array();

foreach($courier['costs'] as $service):
foreach ($courier['costs'] as $service):
// check if this service is allowed
$is_allowed = false;
foreach($allowed_services as $as) {
foreach ($allowed_services as $as) {
// if has variation
if(isset($all_services[$as]['vars'])) {
if (isset($all_services[$as]['vars'])) {
$is_allowed = in_array($service['service'], $all_services[$as]['vars']);
}
else {
$is_allowed = $service['service'] === $as;
}

if($is_allowed) { break; }
if ($is_allowed) { break; }
}

if($is_allowed) {
if ($is_allowed) {
$rate = array(
'id' => $code . '_' . strtolower($service['service']) . $this->instance_id,
'label' => strtoupper($code) . ' ' . $service['service'],
Expand Down Expand Up @@ -143,7 +152,7 @@ private function _calculate_weight($package) {
// if volume is heavier than weight, use the volume
$weight = $weight_volume > $weight ? $weight_volume : $weight;

if($weight > 0) {
if ($weight > 0) {
return $weight;
}
// if no weight data, return default weight or 1kg
Expand All @@ -153,21 +162,23 @@ private function _calculate_weight($package) {
}
}

/*
Get selected services from the courier
@return string - The courier format accepted by RajaOngkir, separated by semicolon (jne:tiki:pos)
*/
/**
* Get selected services from the courier
*
* @return string - The courier format accepted by RajaOngkir, separated by semicolon (jne:tiki:pos)
*/
private function _get_selected_couriers() {
$couriers = wcis_get_couriers();
$couriers = Ongkir_Data::get_couriers();

$selected_couriers = [];
foreach($couriers as $id => $name) {
if(!empty($this->main_settings[$id . '_services'])) {
foreach ($couriers as $id => $name) {
if (!empty($this->main_settings[$id . '_services'])) {
$selected_couriers[] = $id;
}
}

return join(':', $selected_couriers);
}
}

endif;
1 change: 0 additions & 1 deletion assets/dist/wcis-public.css

This file was deleted.

1 change: 0 additions & 1 deletion assets/dist/wcis-public.js

This file was deleted.

1 change: 0 additions & 1 deletion assets/js/wcis-admin.js

This file was deleted.

Loading

0 comments on commit ffae673

Please sign in to comment.