Skip to content

Commit

Permalink
Compatibility with new API version.
Browse files Browse the repository at this point in the history
  • Loading branch information
MB-Jan committed Feb 13, 2022
1 parent 91186d0 commit 482ea54
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 84 deletions.
Binary file added .DS_Store
Binary file not shown.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Project information ###

* Summary: Integrates WordPress with your iko.travel seller account at [https://sell.iko.travel][https://sell.iko.travel]
* Version: 1.0.3
* Version: 1.1.0

### Setup ###
Once you've created an account with iko.travel, you can install this WordPress plugin on your site. Once installed, the plugin will ask you for your clientId and secretKey to connect with your account on iko.travel.
Expand All @@ -29,3 +29,11 @@ There are 2 ways to use our components:
* Support: bjorn@iko.travel

[https://sell.iko.travel]: https://sell.iko.travel

### Changelog ###

###### 1.1.0 ######
Accomodated API changes.
###### 1.0.0 ######

Initial Launch
7 changes: 5 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Contributors: MB-Jan
Tags: travel, iko, booking
Requires at least: 4.7
Tested up to: 5.7.2
Tested up to: 5.9.0
Stable tag: 4.3
Requires PHP: 7.3
Requires PHP: 7.4
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -38,5 +38,8 @@ There are multiple ways to use our components:

== Changelog ==

= 1.1.0 =
* Accomodated API changes

= 1.0.19 =
* Initial Launch
64 changes: 32 additions & 32 deletions ikoTravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: iko.travel Affiliate
* Description: This plugin integrates your iko.travel affiliate account with WordPress. It integrates with Gutenberg, Elementor, Avada, WPBakery and as shortcodes.
* Version: 1.0.25
* Version: 1.1.0
* Author: iko.travel
* Author URI: https://iko.travel/
* License: GPL-3.0
Expand Down Expand Up @@ -45,8 +45,10 @@ function __construct() {
add_action( 'admin_enqueue_scripts', array($this,'customizeScripts'));

add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array($this,'settingsLink' ));

add_action( 'customize_save_after' , array($this, 'clearIkoCache' ));
}

function settingsLink( $links ) {
// Build and escape the URL.
$url = esc_url( add_query_arg(
Expand Down Expand Up @@ -166,13 +168,21 @@ function addSettings( $wp_customize ) {
'description' => __('Switch between environments. Use with caution and only if instructed by the iko.travel team.', $this->namespace),
'section' => $this->section,
'choices' => array(
'live' => __( 'Live' ),
'production' => __( 'Live' ),
'staging' => __( 'Staging' ),
'development' => __( 'Development' )
),
) );

}

function clearIkoCache() {
delete_option( 'ikoData' );
delete_option( 'ikodataTime' );
delete_option( 'ikocontentTime' );
delete_option( 'ikocontentBearer' );
}

function gutenbergBlockCategory($categories, $post) {
return array_merge(
$categories,
Expand All @@ -193,36 +203,26 @@ function __construct() {

}
static function environmentURL($target, $environment) {
// error_log('iko.travel - environment: '.$environment);
$javascriptEnvironments = array(
'staging' => 'https://staging-elements.iko.travel',
'development' => 'https://dev.traveliko.com:8011',
'production' => 'https://elements.iko.travel'
);
$jsonEnvironments = array(
'staging' => 'https://staging-api.iko.travel',
'development' => ' https://staging-iam.iko.travel',
'production' => 'https://iam.iko.travel'
// error_log('iko.travel - target: '.$target);
// error_log('iko.travel - environment: '.$environment);
$environments = array(
'js' => array(
'staging' => 'https://staging-elements.iko.travel',
'development' => 'https://dev.traveliko.com:8011',
'production' => 'https://elements.iko.travel'
),
'json' => array(
'staging' => 'https://staging-iam.iko.travel',
'development' => 'https://staging-iam.iko.travel',
'production' => 'https://iam.iko.travel'
),
'api' => array(
'staging' => 'https://staging-api.iko.travel',
'development' => 'https://dev.traveliko.com:8443',
'production' => 'https://api.iko.travel'
)
);

switch ($target) {
case 'json':
if (!empty($jsonEnvironments[$environment])) {
return $jsonEnvironments[$environment];
}
break;
case 'js':
if (!empty($javascriptEnvironments[$environment])) {
return $javascriptEnvironments[$environment];
}
break;
default:
error_log('iko.travel - Invalid target and / or environment');
error_log('iko.travel - target: '.$target);
error_log('iko.travel - environment: '.$environment);
}

return $jsonEnvironments['production'];
return $environments[$target][$environment];
}
}

Expand Down
Binary file added includes/.DS_Store
Binary file not shown.
Binary file added includes/elements/.DS_Store
Binary file not shown.
5 changes: 3 additions & 2 deletions includes/elements/elementor/ikocontent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function _register_controls() {
'label' => __( 'iko Options', $this->namespace )
]
);
$shortcodes = array();
$shortcodes = apply_filters( 'ikoShortcodes', $shortcodes);
if (!empty($shortcodes['ikocontent'])) {
$options = array();
Expand All @@ -33,11 +34,11 @@ protected function _register_controls() {
$this->add_control(
'layoutid',
[
'label' => 'Layout',
'label' => 'Inventory',
'type' => \Elementor\Controls_Manager::SELECT,
'placeholder' => '',
'options' => $options,
'description' => __('Select any of your saved layouts. We strongly recommend to use this block only in full-width content areas and not in columns.', $this->namespace ),
'description' => __('Select any of your saved inventories. We strongly recommend to use this block only in full-width content areas and not in columns.', $this->namespace ),
]
);
}
Expand Down
62 changes: 15 additions & 47 deletions includes/elements/ikocontent.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function ikoTravelElement($atts) {
function getIkoBearerToken()
{
$env = ikoCore::environmentURL('json', $this->environmentVal);

error_log($env);
$clientId = get_option($this->clientIdKey, false);
$clientSecret = get_option($this->clientSecretKey, false);

Expand All @@ -133,7 +133,7 @@ function getIkoBearerToken()
$curl = curl_init();

$url = $env . '/oauth2/token';
error_log('iko.travel - $token url: ' . $url);
error_log($url);
$curlConfigArray = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
Expand All @@ -143,12 +143,8 @@ function getIkoBearerToken()
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
CURLOPT_USERPWD => $clientId . ":" . $clientSecret
);
CURLOPT_POSTFIELDS => array('client_id' => $clientId,'client_secret' => $clientSecret,'grant_type' => 'client_credentials','scope' => 'inventory.read inventory.write'),
);

if ($this->environmentVal == 'development') {
// remove the need for valid SSL
Expand Down Expand Up @@ -197,9 +193,8 @@ function getIkoBearerToken()
return array();
}

function getIkoLayouts($bearerToken)
{
$env = ikoCore::environmentURL('json', $this->environmentVal);
function getIkoLayouts($bearerToken) {
$env = ikoCore::environmentURL('api', $this->environmentVal);
$currentTime = current_time('timestamp');
$dataTime = get_option('ikodataTime', 0);

Expand All @@ -209,43 +204,16 @@ function getIkoLayouts($bearerToken)
// error_log($env);
$ikoLayouts = get_option('ikoData', array());
if ($dataTime < $currentTime || empty($ikoLayouts)) {
$curl = curl_init();

// error_log('iko.travel - $bearerToken: ' . $bearerToken);
$url = $env . '/api/oauth2/seller/inventory/campaign/list';
// error_log('iko.travel - $data url: ' . $url);

$curlConfigArray = array(
CURLOPT_URL => $url, // to be changed to live URL
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $bearerToken
),
);

if ($this->environmentVal == 'development') {
// remove the need for valid SSL
error_log('Development environment. Ignoring self-signed certificates');
$curlConfigArray[CURLOPT_SSL_VERIFYHOST] = 0;
$curlConfigArray[CURLOPT_SSL_VERIFYPEER] = 0;
}

curl_setopt_array($curl, $curlConfigArray);

$response = curl_exec($curl);

$url = $env . '/api/inventory/campaign/list';
$options = array('http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$bearerToken
));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if (empty($response)) {
// print out any error
error_log('iko.travel - Empty response when trying to retrieve inventory. Details below:');
error_log(curl_error($curl));
} else {
$data = json_decode($response, true);
if (!empty($data)) {
Expand All @@ -262,10 +230,10 @@ function getIkoLayouts($bearerToken)
//
} else {
error_log('iko.travel - Empty response body when trying to retrieve inventory list.');
//error_log(print_r($response,true));
//error_log(print_r($curlConfigArray,true));
}

}
curl_close($curl);
} else {
//error_log('iko.travel - $dateTime > $currentTime');
return $ikoLayouts;
Expand Down

0 comments on commit 482ea54

Please sign in to comment.