Changeset 2420344
- Timestamp:
- 11/17/2020 09:04:57 PM (5 years ago)
- Location:
- transact
- Files:
-
- 10 edited
- 11 copied
-
tags/5.3.1 (copied) (copied from transact/trunk)
-
tags/5.3.1/admin/controllers/transact-admin-settings-post.php (copied) (copied from transact/trunk/admin/controllers/transact-admin-settings-post.php)
-
tags/5.3.1/admin/controllers/transact-api.php (modified) (1 diff)
-
tags/5.3.1/config.ini (copied) (copied from transact/trunk/config.ini)
-
tags/5.3.1/frontend/assets/style.css (copied) (copied from transact/trunk/frontend/assets/style.css)
-
tags/5.3.1/frontend/assets/transact_post.js (copied) (copied from transact/trunk/frontend/assets/transact_post.js) (2 diffs)
-
tags/5.3.1/frontend/controllers/account_meta.php (copied) (copied from transact/trunk/frontend/controllers/account_meta.php) (10 diffs)
-
tags/5.3.1/frontend/controllers/transact-api.php (copied) (copied from transact/trunk/frontend/controllers/transact-api.php) (4 diffs)
-
tags/5.3.1/frontend/controllers/transact-single-post.php (copied) (copied from transact/trunk/frontend/controllers/transact-single-post.php) (3 diffs)
-
tags/5.3.1/models/transact-subscription-transactions-table.php (modified) (1 diff)
-
tags/5.3.1/readme.txt (copied) (copied from transact/trunk/readme.txt) (2 diffs)
-
tags/5.3.1/transact-plugin.php (copied) (copied from transact/trunk/transact-plugin.php) (1 diff)
-
tags/5.3.1/utils/transact-utils-config-parser.php (copied) (copied from transact/trunk/utils/transact-utils-config-parser.php)
-
trunk/admin/controllers/transact-api.php (modified) (1 diff)
-
trunk/frontend/assets/transact_post.js (modified) (2 diffs)
-
trunk/frontend/controllers/account_meta.php (modified) (10 diffs)
-
trunk/frontend/controllers/transact-api.php (modified) (4 diffs)
-
trunk/frontend/controllers/transact-single-post.php (modified) (3 diffs)
-
trunk/models/transact-subscription-transactions-table.php (modified) (1 diff)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/transact-plugin.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
transact/tags/5.3.1/admin/controllers/transact-api.php
r2230635 r2420344 1 1 <?php 2 2 namespace Transact\Admin\Api; 3 4 3 /** 5 4 * Class TransactApi -
transact/tags/5.3.1/frontend/assets/transact_post.js
r2402593 r2420344 284 284 return function (height) { 285 285 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; 287 307 } 288 308 289 309 function loginSuccess(event) { 290 310 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); 298 312 validation_data.check_premium = 1; 299 313 … … 321 335 322 336 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 } 352 361 } else { 353 362 gtmEvent('cancel-purchase'); -
transact/tags/5.3.1/frontend/controllers/account_meta.php
r2373362 r2420344 23 23 */ 24 24 protected $cur_user; 25 26 /** 27 * @var number|null 28 */ 29 protected $xsact_user_id; 30 protected $subscription_info_meta = NULL; 25 31 26 32 function __construct() { … … 45 51 $user_id = $this->cur_user->ID; 46 52 } 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); 48 56 } 49 57 … … 52 60 * @return bool 53 61 */ 54 function validate_subscription($post_id, $user_id )62 function validate_subscription($post_id, $user_id, $subscription_info = null) 55 63 { 56 64 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 58 73 if ($subscription_info) { 59 74 // if the subscription is expired, check to see if they have renewed on xsact … … 63 78 } 64 79 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 66 81 return true; 67 82 } else { … … 80 95 */ 81 96 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); 83 98 $options = get_option('transact-settings'); 84 99 $publisher_id = $options['account_id']; 100 85 101 $subscription_id = $xsact_id . '_' . $publisher_id; 86 102 // Check with api … … 88 104 $valid = (new TransactApi($post_id))->check_if_valid_user_subscription($validate_url, $publisher_id, $subscription_id); 89 105 90 if ($valid) {106 if ($valid) { 91 107 $update_subscription_info = array( 92 108 'expires' => (gettype($valid) === 'boolean' ? $subscription_info['expires'] : $valid), … … 100 116 $update_subscription_info 101 117 ); 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 ) { 103 127 delete_user_meta( 104 128 $user_id, 105 129 self::SUBSCRIPTION_INFO_META 106 130 ); 131 $this->subscription_info_meta = null; 107 132 } 108 133 … … 131 156 * @return bool 132 157 */ 133 function add_subscription($ expiration, $sales_id, $timestamp, $user_id = 0)158 function add_subscription($post_id, $expiration, $sales_id, $timestamp, $user_id = 0) 134 159 { 135 160 if(!$user_id) { 136 161 $user_id = $this->cur_user->ID; 137 162 } 138 163 139 164 $new_subscription_info = array( 140 165 'expires' => $expiration, 141 166 'sale_id' => $sales_id, 142 167 'timestamp' => $timestamp, 143 'validated' => $timestamp168 'validated' => 0 144 169 ); 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; 159 189 } 160 190 } … … 170 200 $user_id = $this->cur_user->ID; 171 201 } 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 } 192 226 } 193 227 } … … 199 233 */ 200 234 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( 202 238 $wp_userid, 203 239 self::XSACT_USER_ID_META, -
transact/tags/5.3.1/frontend/controllers/transact-api.php
r2397933 r2420344 22 22 * Managing user account metadata 23 23 */ 24 require_once plugin_dir_path(__FILE__) . '/account_meta.php'; 24 25 use Transact\FrontEnd\Controllers\AccountMeta\AccountMetaManager; 25 require_once plugin_dir_path(__FILE__) . '/account_meta.php';26 27 26 28 27 /** … … 269 268 } 270 269 $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; 272 272 } 273 273 … … 277 277 } 278 278 279 /** check_if_valid_user_subscription 280 * @ret 281 */ 279 282 public function check_if_valid_user_subscription($validate_url, $account_id, $subscription_id) { 280 283 $url = str_replace(self::ACCOUNT_ID_KEY, $account_id, $validate_url); … … 286 289 $secret = $this->transact->getSubscriptionValidationToken($account_id); 287 290 291 $referer_uri = get_permalink($this->post_id); 292 if (is_wp_error( $referer_uri )) { 293 return null; // Null is error state 294 } 295 288 296 // for now don't want to add more dependencies so ignore. 289 297 //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get 290 298 $ret = wp_remote_get($url, array( 291 'headers' => array( 'Secret' => $secret ) 299 'headers' => array( 300 'Secret' => $secret, 301 'Referer' => $referer_uri, 302 ) 292 303 )); 293 304 294 305 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 300 315 } 301 316 302 317 $body = json_decode($ret['body']); 303 318 if (empty($body)) { 304 return false;319 return null; 305 320 } 306 321 -
transact/tags/5.3.1/frontend/controllers/transact-single-post.php
r2405880 r2420344 317 317 return new WP_Error(400, 'Error: missing token', array( 318 318 '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', 319 325 'status' => 'ERROR', 320 326 )); … … 417 423 418 424 // 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); 420 426 /** 421 427 * If it is an ala carte purchase, create a transaction record … … 682 688 $post_object = get_post($this->post_id); 683 689 $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; 700 712 } 701 713 } -
transact/tags/5.3.1/models/transact-subscription-transactions-table.php
r2199590 r2420344 71 71 { 72 72 try { 73 $row = $this->connector-> insert(73 $row = $this->connector->replace( 74 74 $this->table_name, 75 75 array( -
transact/tags/5.3.1/readme.txt
r2405880 r2420344 5 5 Requires at least: 5.0 6 6 Requires PHP: 5.6 7 Tested up to: 5.5. 18 Stable tag: 5.3. 07 Tested up to: 5.5.3 8 Stable tag: 5.3.1 9 9 License: APACHE-2.0 10 10 License URI: https://www.apache.org/licenses/LICENSE-2.0 … … 82 82 83 83 84 = 5.3.1 = 85 * Fix for old posts not using block editor 86 * Validate subscriptions improvements 87 84 88 = 5.3.0 = 85 89 * Set wordpress_logged_in to 15 days to stay signed in. -
transact/tags/5.3.1/transact-plugin.php
r2405880 r2420344 3 3 * Plugin Name: transact.io 4 4 * Description: Integrates transact.io services into WP 5 * Version: 5.3. 05 * Version: 5.3.1 6 6 * Author: transact.io 7 7 * Author URI: https://transact.io -
transact/trunk/admin/controllers/transact-api.php
r2230635 r2420344 1 1 <?php 2 2 namespace Transact\Admin\Api; 3 4 3 /** 5 4 * Class TransactApi -
transact/trunk/frontend/assets/transact_post.js
r2402593 r2420344 284 284 return function (height) { 285 285 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; 287 307 } 288 308 289 309 function loginSuccess(event) { 290 310 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); 298 312 validation_data.check_premium = 1; 299 313 … … 321 335 322 336 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 } 352 361 } else { 353 362 gtmEvent('cancel-purchase'); -
transact/trunk/frontend/controllers/account_meta.php
r2373362 r2420344 23 23 */ 24 24 protected $cur_user; 25 26 /** 27 * @var number|null 28 */ 29 protected $xsact_user_id; 30 protected $subscription_info_meta = NULL; 25 31 26 32 function __construct() { … … 45 51 $user_id = $this->cur_user->ID; 46 52 } 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); 48 56 } 49 57 … … 52 60 * @return bool 53 61 */ 54 function validate_subscription($post_id, $user_id )62 function validate_subscription($post_id, $user_id, $subscription_info = null) 55 63 { 56 64 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 58 73 if ($subscription_info) { 59 74 // if the subscription is expired, check to see if they have renewed on xsact … … 63 78 } 64 79 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 66 81 return true; 67 82 } else { … … 80 95 */ 81 96 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); 83 98 $options = get_option('transact-settings'); 84 99 $publisher_id = $options['account_id']; 100 85 101 $subscription_id = $xsact_id . '_' . $publisher_id; 86 102 // Check with api … … 88 104 $valid = (new TransactApi($post_id))->check_if_valid_user_subscription($validate_url, $publisher_id, $subscription_id); 89 105 90 if ($valid) {106 if ($valid) { 91 107 $update_subscription_info = array( 92 108 'expires' => (gettype($valid) === 'boolean' ? $subscription_info['expires'] : $valid), … … 100 116 $update_subscription_info 101 117 ); 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 ) { 103 127 delete_user_meta( 104 128 $user_id, 105 129 self::SUBSCRIPTION_INFO_META 106 130 ); 131 $this->subscription_info_meta = null; 107 132 } 108 133 … … 131 156 * @return bool 132 157 */ 133 function add_subscription($ expiration, $sales_id, $timestamp, $user_id = 0)158 function add_subscription($post_id, $expiration, $sales_id, $timestamp, $user_id = 0) 134 159 { 135 160 if(!$user_id) { 136 161 $user_id = $this->cur_user->ID; 137 162 } 138 163 139 164 $new_subscription_info = array( 140 165 'expires' => $expiration, 141 166 'sale_id' => $sales_id, 142 167 'timestamp' => $timestamp, 143 'validated' => $timestamp168 'validated' => 0 144 169 ); 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; 159 189 } 160 190 } … … 170 200 $user_id = $this->cur_user->ID; 171 201 } 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 } 192 226 } 193 227 } … … 199 233 */ 200 234 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( 202 238 $wp_userid, 203 239 self::XSACT_USER_ID_META, -
transact/trunk/frontend/controllers/transact-api.php
r2397933 r2420344 22 22 * Managing user account metadata 23 23 */ 24 require_once plugin_dir_path(__FILE__) . '/account_meta.php'; 24 25 use Transact\FrontEnd\Controllers\AccountMeta\AccountMetaManager; 25 require_once plugin_dir_path(__FILE__) . '/account_meta.php';26 27 26 28 27 /** … … 269 268 } 270 269 $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; 272 272 } 273 273 … … 277 277 } 278 278 279 /** check_if_valid_user_subscription 280 * @ret 281 */ 279 282 public function check_if_valid_user_subscription($validate_url, $account_id, $subscription_id) { 280 283 $url = str_replace(self::ACCOUNT_ID_KEY, $account_id, $validate_url); … … 286 289 $secret = $this->transact->getSubscriptionValidationToken($account_id); 287 290 291 $referer_uri = get_permalink($this->post_id); 292 if (is_wp_error( $referer_uri )) { 293 return null; // Null is error state 294 } 295 288 296 // for now don't want to add more dependencies so ignore. 289 297 //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get 290 298 $ret = wp_remote_get($url, array( 291 'headers' => array( 'Secret' => $secret ) 299 'headers' => array( 300 'Secret' => $secret, 301 'Referer' => $referer_uri, 302 ) 292 303 )); 293 304 294 305 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 300 315 } 301 316 302 317 $body = json_decode($ret['body']); 303 318 if (empty($body)) { 304 return false;319 return null; 305 320 } 306 321 -
transact/trunk/frontend/controllers/transact-single-post.php
r2405880 r2420344 317 317 return new WP_Error(400, 'Error: missing token', array( 318 318 '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', 319 325 'status' => 'ERROR', 320 326 )); … … 417 423 418 424 // 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); 420 426 /** 421 427 * If it is an ala carte purchase, create a transaction record … … 682 688 $post_object = get_post($this->post_id); 683 689 $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; 700 712 } 701 713 } -
transact/trunk/models/transact-subscription-transactions-table.php
r2199590 r2420344 71 71 { 72 72 try { 73 $row = $this->connector-> insert(73 $row = $this->connector->replace( 74 74 $this->table_name, 75 75 array( -
transact/trunk/readme.txt
r2405880 r2420344 5 5 Requires at least: 5.0 6 6 Requires PHP: 5.6 7 Tested up to: 5.5. 18 Stable tag: 5.3. 07 Tested up to: 5.5.3 8 Stable tag: 5.3.1 9 9 License: APACHE-2.0 10 10 License URI: https://www.apache.org/licenses/LICENSE-2.0 … … 82 82 83 83 84 = 5.3.1 = 85 * Fix for old posts not using block editor 86 * Validate subscriptions improvements 87 84 88 = 5.3.0 = 85 89 * Set wordpress_logged_in to 15 days to stay signed in. -
transact/trunk/transact-plugin.php
r2405880 r2420344 3 3 * Plugin Name: transact.io 4 4 * Description: Integrates transact.io services into WP 5 * Version: 5.3. 05 * Version: 5.3.1 6 6 * Author: transact.io 7 7 * Author URI: https://transact.io
Note: See TracChangeset
for help on using the changeset viewer.