Plugin Directory

Changeset 2420344


Ignore:
Timestamp:
11/17/2020 09:04:57 PM (5 years ago)
Author:
transact
Message:

5.3.1

Location:
transact
Files:
10 edited
11 copied

Legend:

Unmodified
Added
Removed
  • transact/tags/5.3.1/admin/controllers/transact-api.php

    r2230635 r2420344  
    11<?php
    22namespace Transact\Admin\Api;
    3 
    43/**
    54 * Class TransactApi
  • transact/tags/5.3.1/frontend/assets/transact_post.js

    r2402593 r2420344  
    284284        return function (height) {
    285285            frame.style.height = height + 'px';
    286         }
     286        };
     287    }
     288
     289    function parseUserInfo(data) {
     290        if (!data.userInfo) {
     291            return;
     292        }
     293
     294        var validation_data = {};
     295        validation_data.post_id = transact_params.post_id;
     296        validation_data.token = data.t;
     297        var userInfo = transactApi.decodeUserInfo(data.userInfo);
     298        validation_data.user_email = userInfo.email;
     299        validation_data.user_display_name =
     300            userInfo.firstName && userInfo.lastName ? userInfo.firstName + ' ' + userInfo.lastName
     301            : userInfo.firstName ? userInfo.firstName
     302            : userInfo.lastName ? userInfo.lastName
     303            : userInfo.email;
     304        validation_data.user_xsact_id = userInfo.xsactId;
     305       
     306        return validation_data;
    287307    }
    288308
    289309    function loginSuccess(event) {
    290310        if (event && event.data) {
    291             var validation_data = {};
    292             validation_data.post_id = transact_params.post_id;
    293             validation_data.token = event.data.t;
    294             var userInfo = transactApi.decodeUserInfo(event.data.userInfo);
    295             validation_data.user_email = userInfo.email;
    296             validation_data.user_display_name = userInfo.firstName + ' ' + userInfo.lastName;
    297             validation_data.user_xsact_id = userInfo.xsactId;
     311            var validation_data = parseUserInfo(event.data);
    298312            validation_data.check_premium = 1;
    299313
     
    321335
    322336        if (event && event.data && event.data.status !== 'cancelPurchase') {
    323             var validation_data = {};
    324             validation_data.post_id = transact_params.post_id;
    325             validation_data.token = event.data.t;
    326             var userInfo = transactApi.decodeUserInfo(event.data.userInfo);
    327             validation_data.user_email = userInfo.email;
    328             validation_data.user_display_name = userInfo.firstName + ' ' + userInfo.lastName;
    329             validation_data.user_xsact_id = userInfo.xsactId;
    330 
    331             ajaxPost(transact_params.ajaxurl + 'verify', validation_data)
    332                 .done(function(resp_data) {
    333                     gtmEvent('complete-purchase');
    334 
    335                     purchasedInSession = true;
    336                     // Handles cookie
    337                     handleCookies(validation_data, resp_data);
    338                     // if custom redirect, send user to it, otherwise reload
    339                     if (custom_redirect.length > 0) {
    340                         // we set redirect here
    341                         // phpcs:ignore WordPressVIPMinimum.JS.Window.location
    342                         window.location = custom_redirect;
    343                     } else {
    344                         // getPremiumContent(false);
    345                         location.reload();
    346                     }
    347                 })
    348                 .fail(function(resp_data) {
    349                     console.warn('Error Response data:', resp_data);
    350                     jQuery('#button_purchase').html('purchase failed');
    351                 });
     337            var validation_data = parseUserInfo(event.data);
     338            if(validation_data) {
     339                ajaxPost(transact_params.ajaxurl + 'verify', validation_data)
     340                    .done(function(resp_data) {
     341                        gtmEvent('complete-purchase');
     342   
     343                        purchasedInSession = true;
     344                        // Handles cookie
     345                        handleCookies(validation_data, resp_data);
     346                        // if custom redirect, send user to it, otherwise reload
     347                        if (custom_redirect.length > 0) {
     348                            // we set redirect here
     349                            // phpcs:ignore WordPressVIPMinimum.JS.Window.location
     350                            window.location = custom_redirect;
     351                        } else {
     352                            // getPremiumContent(false);
     353                            location.reload();
     354                        }
     355                    })
     356                    .fail(function(resp_data) {
     357                        console.warn('Error Response data:', resp_data);
     358                        jQuery('#button_purchase').html('purchase failed');
     359                    });
     360            }
    352361        } else {
    353362            gtmEvent('cancel-purchase');
  • transact/tags/5.3.1/frontend/controllers/account_meta.php

    r2373362 r2420344  
    2323     */
    2424    protected $cur_user;
     25   
     26    /**
     27     * @var number|null
     28     */
     29    protected $xsact_user_id;
     30    protected $subscription_info_meta = NULL;
    2531
    2632    function __construct() {
     
    4551            $user_id = $this->cur_user->ID;
    4652        }
    47         return ($this->validate_subscription($post_id, $user_id) || $this->validate_purchase($post_id, $user_id));
     53        $sub_valid = $this->validate_subscription($post_id, $user_id);
     54        $purch_valid = $this->validate_purchase($post_id, $user_id);
     55        return ($sub_valid || $purch_valid);
    4856    }
    4957
     
    5260     * @return bool
    5361     */
    54     function validate_subscription($post_id, $user_id)
     62    function validate_subscription($post_id, $user_id, $subscription_info = null)
    5563    {
    5664        if ($this->cur_user) {
    57             $subscription_info = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
     65            if(is_null($this->subscription_info_meta)) {
     66                $this->subscription_info_meta = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
     67            }
     68
     69            if(!$subscription_info) {
     70                $subscription_info = $this->subscription_info_meta;
     71            }
     72
    5873            if ($subscription_info) {
    5974                // if the subscription is expired, check to see if they have renewed on xsact
     
    6378                }
    6479
    65                 if (time() * 1000 - $subscription_info['validated'] < 60 * 60 * 24 * 1000) {
     80                if (time() * 1000 - $subscription_info['validated'] < 60 * 60 * 1 * 1000) { // Check once an hour
    6681                    return true;
    6782                } else {
     
    8095     */
    8196    function refresh_subscription($user_id, $subscription_info, $post_id) {
    82         $xsact_id = get_user_meta($user_id, self::XSACT_USER_ID_META, true);
     97        $xsact_id = $this->xsact_user_id ? $this->xsact_user_id : get_user_meta($user_id, self::XSACT_USER_ID_META, true);
    8398        $options = get_option('transact-settings');
    8499        $publisher_id = $options['account_id'];
     100
    85101        $subscription_id = $xsact_id . '_' . $publisher_id;
    86102        // Check with api
     
    88104        $valid = (new TransactApi($post_id))->check_if_valid_user_subscription($validate_url, $publisher_id, $subscription_id);
    89105
    90         if($valid) {
     106        if ($valid) {
    91107            $update_subscription_info = array(
    92108                'expires' => (gettype($valid) === 'boolean' ? $subscription_info['expires'] : $valid),
     
    100116                $update_subscription_info
    101117            );
    102         } else {
     118            $this->subscription_info_meta = $update_subscription_info;
     119        } elseif (
     120            // If valid is false, delete the subscription.
     121            // Also delete the subscription if the call failed
     122            // and the subscription is expired, or validated a long time ago
     123            $valid === false ||
     124            ($valid === null && (time() * 1000 - $subscription_info['expires'] > 0)) ||
     125            ($valid === null && (time() * 1000 - $subscription_info['validated'] < 60 * 60 * 24 * 1000))
     126        ) {
    103127            delete_user_meta(
    104128                $user_id,
    105129                self::SUBSCRIPTION_INFO_META
    106130            );
     131            $this->subscription_info_meta = null;
    107132        }
    108133
     
    131156     * @return bool
    132157     */
    133     function add_subscription($expiration, $sales_id, $timestamp, $user_id = 0)
     158    function add_subscription($post_id, $expiration, $sales_id, $timestamp, $user_id = 0)
    134159    {
    135160        if(!$user_id) {
    136161            $user_id = $this->cur_user->ID;
    137162        }
    138 
     163       
    139164        $new_subscription_info = array(
    140165            'expires' => $expiration,
    141166            'sale_id' => $sales_id,
    142167            'timestamp' => $timestamp,
    143             'validated' => $timestamp
     168            'validated' => 0
    144169        );
    145        
    146         $subscription_info = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
    147         if ($subscription_info) {
    148             update_user_meta(
    149                 $user_id,
    150                 self::SUBSCRIPTION_INFO_META,
    151                 $new_subscription_info
    152             );
    153         } else {
    154             add_user_meta(
    155                 $user_id,
    156                 self::SUBSCRIPTION_INFO_META,
    157                 $new_subscription_info
    158             );
     170
     171        if($this->validate_subscription($post_id, $user_id, $new_subscription_info)) {
     172            $new_subscription_info['validated'] = $timestamp;
     173           
     174            $subscription_info = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
     175            if ($subscription_info) {
     176                update_user_meta(
     177                    $user_id,
     178                    self::SUBSCRIPTION_INFO_META,
     179                    $new_subscription_info
     180                );
     181            } else {
     182                add_user_meta(
     183                    $user_id,
     184                    self::SUBSCRIPTION_INFO_META,
     185                    $new_subscription_info
     186                );
     187            }
     188            $this->subscription_info_meta = $new_subscription_info;
    159189        }
    160190    }
     
    170200            $user_id = $this->cur_user->ID;
    171201        }
    172 
    173         $new_purchase_info = array(
    174             'sale_id' => $sales_id,
    175             'timestamp' => $timestamp
    176         );
    177         $meta_key = self::PURCHASE_INFO_META . $post_id;
    178 
    179         $purchase_info = get_user_meta($user_id, $meta_key, true);
    180         if ($purchase_info) {
    181             update_user_meta(
    182                 $user_id,
    183                 $meta_key,
    184                 $new_purchase_info
    185             );
    186         } else {
    187             add_user_meta(
    188                 $user_id,
    189                 $meta_key,
    190                 $new_purchase_info
    191             );
     202        $subscription_info = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
     203
     204        // Do not register a purchase if the user has a subscription
     205        if(!$subscription_info) {
     206            $new_purchase_info = array(
     207                'sale_id' => $sales_id,
     208                'timestamp' => $timestamp
     209            );
     210            $meta_key = self::PURCHASE_INFO_META . $post_id;
     211   
     212            $purchase_info = get_user_meta($user_id, $meta_key, true);
     213            if ($purchase_info) {
     214                update_user_meta(
     215                    $user_id,
     216                    $meta_key,
     217                    $new_purchase_info
     218                );
     219            } else {
     220                add_user_meta(
     221                    $user_id,
     222                    $meta_key,
     223                    $new_purchase_info
     224                );
     225            }
    192226        }
    193227    }
     
    199233     */
    200234    function connect_xsact_id_meta($wp_userid, $xsact_id) {
    201         add_user_meta(
     235        $this->xsact_user_id = intval($xsact_id);
     236
     237        update_user_meta(
    202238            $wp_userid,
    203239            self::XSACT_USER_ID_META,
  • transact/tags/5.3.1/frontend/controllers/transact-api.php

    r2397933 r2420344  
    2222 * Managing user account metadata
    2323 */
     24require_once  plugin_dir_path(__FILE__) . '/account_meta.php';
    2425use Transact\FrontEnd\Controllers\AccountMeta\AccountMetaManager;
    25 require_once  plugin_dir_path(__FILE__) . '/account_meta.php';
    26 
    2726
    2827/**
     
    269268        }
    270269        $account_meta = new AccountMetaManager();
    271         return $account_meta->validate_access($this->post_id, $user_id);
     270        $result = $account_meta->validate_access($this->post_id, $user_id);
     271        return $result;
    272272    }
    273273
     
    277277    }
    278278
     279    /** check_if_valid_user_subscription
     280     * @ret
     281     */
    279282    public function check_if_valid_user_subscription($validate_url, $account_id, $subscription_id) {
    280283        $url = str_replace(self::ACCOUNT_ID_KEY, $account_id, $validate_url);
     
    286289        $secret = $this->transact->getSubscriptionValidationToken($account_id);
    287290
     291        $referer_uri = get_permalink($this->post_id);
     292        if (is_wp_error( $referer_uri )) {
     293            return null; // Null is error state
     294        }
     295
    288296        // for now don't want to add more dependencies so ignore.
    289297        //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
    290298        $ret = wp_remote_get($url, array(
    291             'headers' => array( 'Secret' => $secret )
     299            'headers' => array(
     300                'Secret' => $secret,
     301                'Referer' => $referer_uri,
     302            )
    292303        ));
    293304
    294305        if (is_wp_error( $ret )) {
    295             return false;
    296         }
    297 
    298         if (empty($ret) || $ret['response']['code'] !== 200) {
    299             return false;
     306            return null; // Null is error state
     307        }
     308
     309        //error_log(print_r($ret, true));
     310
     311        $code = $ret['response']['code'];
     312
     313        if ($code == 404 || $code == 402) {
     314            return false; // False removes the subscription
    300315        }
    301316
    302317        $body = json_decode($ret['body']);
    303318        if (empty($body)) {
    304             return false;
     319            return null;
    305320        }
    306321
  • transact/tags/5.3.1/frontend/controllers/transact-single-post.php

    r2405880 r2420344  
    317317            return new WP_Error(400, 'Error: missing token', array(
    318318                'content' => 'Invalid request. Missing purchase token',
     319                'status' => 'ERROR',
     320            ));
     321        }
     322        if (empty($xsact_user_id) || $xsact_user_id === 0) {
     323            return new WP_Error(400, 'Error: missing xsact user id', array(
     324                'content' => 'Invalid request. Missing xsact user id',
    319325                'status' => 'ERROR',
    320326            ));
     
    417423
    418424                // Record the subscription in the user account meta
    419                 $accountMeta->add_subscription($decoded->sub_expires, $decoded->uid, $decoded->iat * 1000, $wp_userid);
     425                $accountMeta->add_subscription($post_id, $decoded->sub_expires, $decoded->uid, $decoded->iat * 1000, $wp_userid);
    420426            /**
    421427             * If it is an ala carte purchase, create a transaction record
     
    682688        $post_object = get_post($this->post_id);
    683689        $has_premium = $this->post_has_premium($transact, $post_object);
    684 
    685         if(!$premium && $has_premium) {
    686             $nonce_field = wp_nonce_field( 'transact_purchase_buttons', 'transact_purchase_buttons_nonce', true, false );
    687 
    688             if (!has_shortcode($content, 'transact_button')) {
    689                 $button_controller = new transactHandleButtons($this->post_id, $transact);
    690                 $options = get_option('transact-settings');
    691                 $count_words = false;
    692                 if (isset($options['show_count_words']) && $options['show_count_words']) {
    693                     $count_words = str_word_count(wp_strip_all_tags($content));
    694                 }
    695 
    696                 if($button_controller->get_if_article_donation()) {
    697                     $content = $content . $button_controller->print_donation_button($count_words);
    698                 } else {
    699                     $content = $content . $button_controller->print_purchase_buttons($count_words);
     690   
     691        if($has_premium) {
     692            if(!$premium) {
     693                if (!has_shortcode($content, 'transact_button')) {
     694                    $button_controller = new transactHandleButtons($this->post_id, $transact);
     695                    $options = get_option('transact-settings');
     696                    $count_words = false;
     697                    if (isset($options['show_count_words']) && $options['show_count_words']) {
     698                        $count_words = str_word_count(wp_strip_all_tags($content));
     699                    }
     700   
     701                    if($button_controller->get_if_article_donation()) {
     702                        $content = $content . $button_controller->print_donation_button($count_words);
     703                    } else {
     704                        $content = $content . $button_controller->print_purchase_buttons($count_words);
     705                    }
     706                }
     707            } else {
     708                // If the post has old premium content, append it
     709                $premium_from_meta = $transact->get_premium_content();
     710                if (isset($premium_from_meta) && $premium_from_meta !== '') {
     711                    $content = $content . $premium_from_meta;
    700712                }
    701713            }
  • transact/tags/5.3.1/models/transact-subscription-transactions-table.php

    r2199590 r2420344  
    7171    {
    7272        try {
    73             $row = $this->connector->insert(
     73            $row = $this->connector->replace(
    7474                $this->table_name,
    7575                array(
  • transact/tags/5.3.1/readme.txt

    r2405880 r2420344  
    55Requires at least: 5.0
    66Requires PHP: 5.6
    7 Tested up to: 5.5.1
    8 Stable tag: 5.3.0
     7Tested up to: 5.5.3
     8Stable tag: 5.3.1
    99License: APACHE-2.0
    1010License URI: https://www.apache.org/licenses/LICENSE-2.0
     
    8282
    8383
     84= 5.3.1 =
     85* Fix for old posts not using block editor
     86* Validate subscriptions improvements
     87
    8488= 5.3.0 =
    8589* Set wordpress_logged_in to 15 days to stay signed in.
  • transact/tags/5.3.1/transact-plugin.php

    r2405880 r2420344  
    33 * Plugin Name: transact.io
    44 * Description: Integrates transact.io services into WP
    5  * Version: 5.3.0
     5 * Version: 5.3.1
    66 * Author: transact.io
    77 * Author URI: https://transact.io
  • transact/trunk/admin/controllers/transact-api.php

    r2230635 r2420344  
    11<?php
    22namespace Transact\Admin\Api;
    3 
    43/**
    54 * Class TransactApi
  • transact/trunk/frontend/assets/transact_post.js

    r2402593 r2420344  
    284284        return function (height) {
    285285            frame.style.height = height + 'px';
    286         }
     286        };
     287    }
     288
     289    function parseUserInfo(data) {
     290        if (!data.userInfo) {
     291            return;
     292        }
     293
     294        var validation_data = {};
     295        validation_data.post_id = transact_params.post_id;
     296        validation_data.token = data.t;
     297        var userInfo = transactApi.decodeUserInfo(data.userInfo);
     298        validation_data.user_email = userInfo.email;
     299        validation_data.user_display_name =
     300            userInfo.firstName && userInfo.lastName ? userInfo.firstName + ' ' + userInfo.lastName
     301            : userInfo.firstName ? userInfo.firstName
     302            : userInfo.lastName ? userInfo.lastName
     303            : userInfo.email;
     304        validation_data.user_xsact_id = userInfo.xsactId;
     305       
     306        return validation_data;
    287307    }
    288308
    289309    function loginSuccess(event) {
    290310        if (event && event.data) {
    291             var validation_data = {};
    292             validation_data.post_id = transact_params.post_id;
    293             validation_data.token = event.data.t;
    294             var userInfo = transactApi.decodeUserInfo(event.data.userInfo);
    295             validation_data.user_email = userInfo.email;
    296             validation_data.user_display_name = userInfo.firstName + ' ' + userInfo.lastName;
    297             validation_data.user_xsact_id = userInfo.xsactId;
     311            var validation_data = parseUserInfo(event.data);
    298312            validation_data.check_premium = 1;
    299313
     
    321335
    322336        if (event && event.data && event.data.status !== 'cancelPurchase') {
    323             var validation_data = {};
    324             validation_data.post_id = transact_params.post_id;
    325             validation_data.token = event.data.t;
    326             var userInfo = transactApi.decodeUserInfo(event.data.userInfo);
    327             validation_data.user_email = userInfo.email;
    328             validation_data.user_display_name = userInfo.firstName + ' ' + userInfo.lastName;
    329             validation_data.user_xsact_id = userInfo.xsactId;
    330 
    331             ajaxPost(transact_params.ajaxurl + 'verify', validation_data)
    332                 .done(function(resp_data) {
    333                     gtmEvent('complete-purchase');
    334 
    335                     purchasedInSession = true;
    336                     // Handles cookie
    337                     handleCookies(validation_data, resp_data);
    338                     // if custom redirect, send user to it, otherwise reload
    339                     if (custom_redirect.length > 0) {
    340                         // we set redirect here
    341                         // phpcs:ignore WordPressVIPMinimum.JS.Window.location
    342                         window.location = custom_redirect;
    343                     } else {
    344                         // getPremiumContent(false);
    345                         location.reload();
    346                     }
    347                 })
    348                 .fail(function(resp_data) {
    349                     console.warn('Error Response data:', resp_data);
    350                     jQuery('#button_purchase').html('purchase failed');
    351                 });
     337            var validation_data = parseUserInfo(event.data);
     338            if(validation_data) {
     339                ajaxPost(transact_params.ajaxurl + 'verify', validation_data)
     340                    .done(function(resp_data) {
     341                        gtmEvent('complete-purchase');
     342   
     343                        purchasedInSession = true;
     344                        // Handles cookie
     345                        handleCookies(validation_data, resp_data);
     346                        // if custom redirect, send user to it, otherwise reload
     347                        if (custom_redirect.length > 0) {
     348                            // we set redirect here
     349                            // phpcs:ignore WordPressVIPMinimum.JS.Window.location
     350                            window.location = custom_redirect;
     351                        } else {
     352                            // getPremiumContent(false);
     353                            location.reload();
     354                        }
     355                    })
     356                    .fail(function(resp_data) {
     357                        console.warn('Error Response data:', resp_data);
     358                        jQuery('#button_purchase').html('purchase failed');
     359                    });
     360            }
    352361        } else {
    353362            gtmEvent('cancel-purchase');
  • transact/trunk/frontend/controllers/account_meta.php

    r2373362 r2420344  
    2323     */
    2424    protected $cur_user;
     25   
     26    /**
     27     * @var number|null
     28     */
     29    protected $xsact_user_id;
     30    protected $subscription_info_meta = NULL;
    2531
    2632    function __construct() {
     
    4551            $user_id = $this->cur_user->ID;
    4652        }
    47         return ($this->validate_subscription($post_id, $user_id) || $this->validate_purchase($post_id, $user_id));
     53        $sub_valid = $this->validate_subscription($post_id, $user_id);
     54        $purch_valid = $this->validate_purchase($post_id, $user_id);
     55        return ($sub_valid || $purch_valid);
    4856    }
    4957
     
    5260     * @return bool
    5361     */
    54     function validate_subscription($post_id, $user_id)
     62    function validate_subscription($post_id, $user_id, $subscription_info = null)
    5563    {
    5664        if ($this->cur_user) {
    57             $subscription_info = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
     65            if(is_null($this->subscription_info_meta)) {
     66                $this->subscription_info_meta = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
     67            }
     68
     69            if(!$subscription_info) {
     70                $subscription_info = $this->subscription_info_meta;
     71            }
     72
    5873            if ($subscription_info) {
    5974                // if the subscription is expired, check to see if they have renewed on xsact
     
    6378                }
    6479
    65                 if (time() * 1000 - $subscription_info['validated'] < 60 * 60 * 24 * 1000) {
     80                if (time() * 1000 - $subscription_info['validated'] < 60 * 60 * 1 * 1000) { // Check once an hour
    6681                    return true;
    6782                } else {
     
    8095     */
    8196    function refresh_subscription($user_id, $subscription_info, $post_id) {
    82         $xsact_id = get_user_meta($user_id, self::XSACT_USER_ID_META, true);
     97        $xsact_id = $this->xsact_user_id ? $this->xsact_user_id : get_user_meta($user_id, self::XSACT_USER_ID_META, true);
    8398        $options = get_option('transact-settings');
    8499        $publisher_id = $options['account_id'];
     100
    85101        $subscription_id = $xsact_id . '_' . $publisher_id;
    86102        // Check with api
     
    88104        $valid = (new TransactApi($post_id))->check_if_valid_user_subscription($validate_url, $publisher_id, $subscription_id);
    89105
    90         if($valid) {
     106        if ($valid) {
    91107            $update_subscription_info = array(
    92108                'expires' => (gettype($valid) === 'boolean' ? $subscription_info['expires'] : $valid),
     
    100116                $update_subscription_info
    101117            );
    102         } else {
     118            $this->subscription_info_meta = $update_subscription_info;
     119        } elseif (
     120            // If valid is false, delete the subscription.
     121            // Also delete the subscription if the call failed
     122            // and the subscription is expired, or validated a long time ago
     123            $valid === false ||
     124            ($valid === null && (time() * 1000 - $subscription_info['expires'] > 0)) ||
     125            ($valid === null && (time() * 1000 - $subscription_info['validated'] < 60 * 60 * 24 * 1000))
     126        ) {
    103127            delete_user_meta(
    104128                $user_id,
    105129                self::SUBSCRIPTION_INFO_META
    106130            );
     131            $this->subscription_info_meta = null;
    107132        }
    108133
     
    131156     * @return bool
    132157     */
    133     function add_subscription($expiration, $sales_id, $timestamp, $user_id = 0)
     158    function add_subscription($post_id, $expiration, $sales_id, $timestamp, $user_id = 0)
    134159    {
    135160        if(!$user_id) {
    136161            $user_id = $this->cur_user->ID;
    137162        }
    138 
     163       
    139164        $new_subscription_info = array(
    140165            'expires' => $expiration,
    141166            'sale_id' => $sales_id,
    142167            'timestamp' => $timestamp,
    143             'validated' => $timestamp
     168            'validated' => 0
    144169        );
    145        
    146         $subscription_info = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
    147         if ($subscription_info) {
    148             update_user_meta(
    149                 $user_id,
    150                 self::SUBSCRIPTION_INFO_META,
    151                 $new_subscription_info
    152             );
    153         } else {
    154             add_user_meta(
    155                 $user_id,
    156                 self::SUBSCRIPTION_INFO_META,
    157                 $new_subscription_info
    158             );
     170
     171        if($this->validate_subscription($post_id, $user_id, $new_subscription_info)) {
     172            $new_subscription_info['validated'] = $timestamp;
     173           
     174            $subscription_info = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
     175            if ($subscription_info) {
     176                update_user_meta(
     177                    $user_id,
     178                    self::SUBSCRIPTION_INFO_META,
     179                    $new_subscription_info
     180                );
     181            } else {
     182                add_user_meta(
     183                    $user_id,
     184                    self::SUBSCRIPTION_INFO_META,
     185                    $new_subscription_info
     186                );
     187            }
     188            $this->subscription_info_meta = $new_subscription_info;
    159189        }
    160190    }
     
    170200            $user_id = $this->cur_user->ID;
    171201        }
    172 
    173         $new_purchase_info = array(
    174             'sale_id' => $sales_id,
    175             'timestamp' => $timestamp
    176         );
    177         $meta_key = self::PURCHASE_INFO_META . $post_id;
    178 
    179         $purchase_info = get_user_meta($user_id, $meta_key, true);
    180         if ($purchase_info) {
    181             update_user_meta(
    182                 $user_id,
    183                 $meta_key,
    184                 $new_purchase_info
    185             );
    186         } else {
    187             add_user_meta(
    188                 $user_id,
    189                 $meta_key,
    190                 $new_purchase_info
    191             );
     202        $subscription_info = get_user_meta($user_id, self::SUBSCRIPTION_INFO_META, true);
     203
     204        // Do not register a purchase if the user has a subscription
     205        if(!$subscription_info) {
     206            $new_purchase_info = array(
     207                'sale_id' => $sales_id,
     208                'timestamp' => $timestamp
     209            );
     210            $meta_key = self::PURCHASE_INFO_META . $post_id;
     211   
     212            $purchase_info = get_user_meta($user_id, $meta_key, true);
     213            if ($purchase_info) {
     214                update_user_meta(
     215                    $user_id,
     216                    $meta_key,
     217                    $new_purchase_info
     218                );
     219            } else {
     220                add_user_meta(
     221                    $user_id,
     222                    $meta_key,
     223                    $new_purchase_info
     224                );
     225            }
    192226        }
    193227    }
     
    199233     */
    200234    function connect_xsact_id_meta($wp_userid, $xsact_id) {
    201         add_user_meta(
     235        $this->xsact_user_id = intval($xsact_id);
     236
     237        update_user_meta(
    202238            $wp_userid,
    203239            self::XSACT_USER_ID_META,
  • transact/trunk/frontend/controllers/transact-api.php

    r2397933 r2420344  
    2222 * Managing user account metadata
    2323 */
     24require_once  plugin_dir_path(__FILE__) . '/account_meta.php';
    2425use Transact\FrontEnd\Controllers\AccountMeta\AccountMetaManager;
    25 require_once  plugin_dir_path(__FILE__) . '/account_meta.php';
    26 
    2726
    2827/**
     
    269268        }
    270269        $account_meta = new AccountMetaManager();
    271         return $account_meta->validate_access($this->post_id, $user_id);
     270        $result = $account_meta->validate_access($this->post_id, $user_id);
     271        return $result;
    272272    }
    273273
     
    277277    }
    278278
     279    /** check_if_valid_user_subscription
     280     * @ret
     281     */
    279282    public function check_if_valid_user_subscription($validate_url, $account_id, $subscription_id) {
    280283        $url = str_replace(self::ACCOUNT_ID_KEY, $account_id, $validate_url);
     
    286289        $secret = $this->transact->getSubscriptionValidationToken($account_id);
    287290
     291        $referer_uri = get_permalink($this->post_id);
     292        if (is_wp_error( $referer_uri )) {
     293            return null; // Null is error state
     294        }
     295
    288296        // for now don't want to add more dependencies so ignore.
    289297        //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
    290298        $ret = wp_remote_get($url, array(
    291             'headers' => array( 'Secret' => $secret )
     299            'headers' => array(
     300                'Secret' => $secret,
     301                'Referer' => $referer_uri,
     302            )
    292303        ));
    293304
    294305        if (is_wp_error( $ret )) {
    295             return false;
    296         }
    297 
    298         if (empty($ret) || $ret['response']['code'] !== 200) {
    299             return false;
     306            return null; // Null is error state
     307        }
     308
     309        //error_log(print_r($ret, true));
     310
     311        $code = $ret['response']['code'];
     312
     313        if ($code == 404 || $code == 402) {
     314            return false; // False removes the subscription
    300315        }
    301316
    302317        $body = json_decode($ret['body']);
    303318        if (empty($body)) {
    304             return false;
     319            return null;
    305320        }
    306321
  • transact/trunk/frontend/controllers/transact-single-post.php

    r2405880 r2420344  
    317317            return new WP_Error(400, 'Error: missing token', array(
    318318                'content' => 'Invalid request. Missing purchase token',
     319                'status' => 'ERROR',
     320            ));
     321        }
     322        if (empty($xsact_user_id) || $xsact_user_id === 0) {
     323            return new WP_Error(400, 'Error: missing xsact user id', array(
     324                'content' => 'Invalid request. Missing xsact user id',
    319325                'status' => 'ERROR',
    320326            ));
     
    417423
    418424                // Record the subscription in the user account meta
    419                 $accountMeta->add_subscription($decoded->sub_expires, $decoded->uid, $decoded->iat * 1000, $wp_userid);
     425                $accountMeta->add_subscription($post_id, $decoded->sub_expires, $decoded->uid, $decoded->iat * 1000, $wp_userid);
    420426            /**
    421427             * If it is an ala carte purchase, create a transaction record
     
    682688        $post_object = get_post($this->post_id);
    683689        $has_premium = $this->post_has_premium($transact, $post_object);
    684 
    685         if(!$premium && $has_premium) {
    686             $nonce_field = wp_nonce_field( 'transact_purchase_buttons', 'transact_purchase_buttons_nonce', true, false );
    687 
    688             if (!has_shortcode($content, 'transact_button')) {
    689                 $button_controller = new transactHandleButtons($this->post_id, $transact);
    690                 $options = get_option('transact-settings');
    691                 $count_words = false;
    692                 if (isset($options['show_count_words']) && $options['show_count_words']) {
    693                     $count_words = str_word_count(wp_strip_all_tags($content));
    694                 }
    695 
    696                 if($button_controller->get_if_article_donation()) {
    697                     $content = $content . $button_controller->print_donation_button($count_words);
    698                 } else {
    699                     $content = $content . $button_controller->print_purchase_buttons($count_words);
     690   
     691        if($has_premium) {
     692            if(!$premium) {
     693                if (!has_shortcode($content, 'transact_button')) {
     694                    $button_controller = new transactHandleButtons($this->post_id, $transact);
     695                    $options = get_option('transact-settings');
     696                    $count_words = false;
     697                    if (isset($options['show_count_words']) && $options['show_count_words']) {
     698                        $count_words = str_word_count(wp_strip_all_tags($content));
     699                    }
     700   
     701                    if($button_controller->get_if_article_donation()) {
     702                        $content = $content . $button_controller->print_donation_button($count_words);
     703                    } else {
     704                        $content = $content . $button_controller->print_purchase_buttons($count_words);
     705                    }
     706                }
     707            } else {
     708                // If the post has old premium content, append it
     709                $premium_from_meta = $transact->get_premium_content();
     710                if (isset($premium_from_meta) && $premium_from_meta !== '') {
     711                    $content = $content . $premium_from_meta;
    700712                }
    701713            }
  • transact/trunk/models/transact-subscription-transactions-table.php

    r2199590 r2420344  
    7171    {
    7272        try {
    73             $row = $this->connector->insert(
     73            $row = $this->connector->replace(
    7474                $this->table_name,
    7575                array(
  • transact/trunk/readme.txt

    r2405880 r2420344  
    55Requires at least: 5.0
    66Requires PHP: 5.6
    7 Tested up to: 5.5.1
    8 Stable tag: 5.3.0
     7Tested up to: 5.5.3
     8Stable tag: 5.3.1
    99License: APACHE-2.0
    1010License URI: https://www.apache.org/licenses/LICENSE-2.0
     
    8282
    8383
     84= 5.3.1 =
     85* Fix for old posts not using block editor
     86* Validate subscriptions improvements
     87
    8488= 5.3.0 =
    8589* Set wordpress_logged_in to 15 days to stay signed in.
  • transact/trunk/transact-plugin.php

    r2405880 r2420344  
    33 * Plugin Name: transact.io
    44 * Description: Integrates transact.io services into WP
    5  * Version: 5.3.0
     5 * Version: 5.3.1
    66 * Author: transact.io
    77 * Author URI: https://transact.io
Note: See TracChangeset for help on using the changeset viewer.