Skip to content

Commit

Permalink
2.0.2
Browse files Browse the repository at this point in the history
- Added support for Localhost
- Changed some coding style to remove empty space between parentheses.
  • Loading branch information
hrsetyono committed Jan 17, 2022
1 parent bdbffab commit 43fc808
Show file tree
Hide file tree
Showing 14 changed files with 473 additions and 434 deletions.
80 changes: 0 additions & 80 deletions CHANGELOG.md

This file was deleted.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

Ultimate *Ongkos Kirim* Plugin for major Indonesian Shipping courier.

Add JNE, TIKI, POS, PCP, RPX, and J&T Express to your WooCommerce. Requires PRO License purchase from RajaOngkir.com. We are not affiliated with RajaOngkir in any way.
Add JNE, TIKI, POS, PCP, RPX, and J&T Express to your WooCommerce.

**Important:** Requires PRO License purchase from RajaOngkir.com. We are not affiliated with RajaOngkir in any way.

This plugin is free and provided as is. If you found a bug, please submit it [here](https://github.com/hrsetyono/wc-indo-shipping/issues).

Expand Down
194 changes: 194 additions & 0 deletions helper/rajaongkir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php

/**
* Handle API Call to RajaOngkir Server
*/
class RajaOngkir {
private $api_key;
private $base_url = 'https://pro.rajaongkir.com/api';

function __construct($key = null) {
// set API key
if($key) {
$this->api_key = $key;
}
else {
$cached_license = get_transient('wcis_license');
$this->api_key = $cached_license['key'];
}
}


/**
* Test if API key is working
*
* @return bool
*/
function is_valid() {
$result = $this->get('/province', [], false);
$status = wp_remote_retrieve_response_code($result);
return $status === 200;
}


/**
* Get shipping costs
*
* @param array $args - Formatted argument ready for API call
* @return array - JSON response
*/
function get_costs($args) {
$result = $this->post('/cost', $args);

if($result) {
$costs = $result['rajaongkir']['results'] ?? null;
return $costs;
} else {
return $result;
}
}

/////


/**
* GET request
*
* @param string $endpoint
* @param array $args - The URL params
* @param boolean $return_only_body
*
* @since 2.1.0
*/
function get($endpoint, $args = [], $return_only_body = true) {
$url = $this->get_endpoint_url($endpoint);

if(!empty($args)) {
$url = sprintf('%s?%s', $url, http_build_query($args));
}

$result = wp_remote_get($url, [
'headers' => [ 'key' => $this->api_key ],
'sslverify' => WP_DEBUG === true ? false : true,
]);

if(is_wp_error($result)) {
return $result->get_error_message();
}

if($return_only_body) {
return json_decode($result['body'], true);
}

return $result;
}

/**
* POST request
*
* @param string $endpoint
* @param array $args - The URL params
* @param boolean $return_only_body
*
* @since 2.1.0
*/
function post($endpoint, $args = [], $return_only_body = true) {
$url = $this->get_endpoint_url($endpoint);

$result = wp_remote_post($url, [
'headers' => [ 'key' => $this->api_key ],
'body' => $args,
'sslverify' => WP_DEBUG === true ? false : true,
]);

if(is_wp_error($result)) {
return $result->get_error_message();
}

if($return_only_body) {
return json_decode($result['body'], true);
}

return $result;
}

/**
* if URL doesn't start with "http", prepend base URL
*
* @since 2.1.0
*/
private function get_endpoint_url($endpoint) {
$is_full_url = preg_match('/^http/', $endpoint, $matches);
$url = $is_full_url ? $endpoint : sprintf('%s%s', $this->base_url, $endpoint);

return $url;
}



/**
* GET request using Curl
*
* @deprecated 2.1.0 - Use wp_remote_get() instead
*
* @param string $endpoint
* @param array $args - The URL params
*/
function get_curl($endpoint, $args = []) {
$url = $this->get_endpoint_url($endpoint);

if(!empty($args)) {
$url = sprintf('%s?%s', $url, http_build_query($args));
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'key:' . $this->api_key,
]);

if (WP_DEBUG) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}

$result = curl_exec($ch);
curl_close($ch);

return json_decode($result, true);
}

/**
* POST request using Curl
*
* @deprecated 2.1.0 - Use wp_remote_post() instead
*
* @param string $endpoint
* @param array $body
*/
function post_curl($endpoint, $body = []) {
$url = $this->get_endpoint_url($endpoint);
$payload = json_encode($body);

// Prepare new cURL resource
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'key:' . $this->api_key,
'Content-Type: application/json',
'Content-Length: ' . strlen($payload)
]);

// Submit the POST request
$response = curl_exec($ch);
curl_close($ch);

return $response;
}
}
54 changes: 27 additions & 27 deletions module-admin/init-main.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
/*
Global setting for Indo Shipping
*/
require_once WCIS_DIR . '/helper/rajaongkir.php';

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

Expand All @@ -15,13 +17,13 @@ public function __construct($instance_id = 0) {
$this->init_form_fields();

// allow save setting
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options') );
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_transients') );
add_action('woocommerce_update_options_shipping_' . $this->id, [$this, 'process_admin_options']);
add_action('woocommerce_update_options_shipping_' . $this->id, [$this, 'process_admin_transients']);
}

/*
Initiate global setting page for WCIS
*/
/**
* Initiate global setting page for WCIS
*/
function init_form_fields() {
$enabled_field = array(
'title' => __('Enable/Disable'),
Expand Down Expand Up @@ -76,24 +78,24 @@ function init_form_fields() {
* Add API Key to Transient so it's cached
*/
function process_admin_transients() {
$t_license = get_transient( 'wcis_license' );
$t_license = get_transient('wcis_license');

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

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

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

set_transient( 'wcis_license', $t_license, 60*60*24*30 );
set_transient('wcis_license', $t_license, 60*60*24*30);
}

return $t_license;
Expand All @@ -103,19 +105,18 @@ function process_admin_transients() {
/////


/*
Validate API Key by doing a sample AJAX call
@return bool
*/
/**
* Validate API Key by doing a sample AJAX call
* @return bool
*/
private function check_key_valid() {
$license = get_transient( 'wcis_license' );
$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'] === true ) {
if(isset($license['valid']) && $license['valid']) {
$msg = __('API Connected!');
$this->form_fields['key']['description'] = '<span style="color: #4caf50;">' . $msg . '</span>';
}
Expand All @@ -127,11 +128,10 @@ private function check_key_valid() {
return $license['valid'];
}

/*
Get cities list from cache. Cached when the setting is saved.
@return array - List of cities in base province
*/
/**
* Get cities list from cache. Cached when the setting is saved.
* @return array - List of cities in base province
*/
private function get_cities_origin() {
$country = wc_get_base_location();
$prov_id = wcis_get_province_id( $country['state'] );
Expand Down
Loading

0 comments on commit 43fc808

Please sign in to comment.