| 1 |
<?php |
| 2 |
/** |
| 3 |
* Give Recurring Subscriber |
| 4 |
* |
| 5 |
* @since 1.0 |
| 6 |
* @copyright Copyright (c) 2016, GiveWP |
| 7 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 8 |
* @package Give |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Give_Recurring_Subscriber |
| 18 |
* |
| 19 |
* Includes methods for setting users as donors, setting their status, expiration, etc. |
| 20 |
* @since 2.19.0 - migrated from give-recurring |
| 21 |
*/ |
| 22 |
class Give_Recurring_Subscriber extends Give_Donor { |
| 23 |
|
| 24 |
/** |
| 25 |
* Subscriber DB |
| 26 |
* |
| 27 |
* @var Give_Subscriptions_DB |
| 28 |
*/ |
| 29 |
private $subs_db; |
| 30 |
|
| 31 |
/** |
| 32 |
* Give_Recurring_Subscriber constructor. |
| 33 |
* |
| 34 |
* @param bool $_id_or_email |
| 35 |
* @param bool $by_user_id |
| 36 |
*/ |
| 37 |
function __construct( $_id_or_email = false, $by_user_id = false ) { |
| 38 |
parent::__construct( $_id_or_email, $by_user_id ); |
| 39 |
$this->subs_db = new Give_Subscriptions_DB(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Has donation form subscription. |
| 44 |
* |
| 45 |
* @param int $form_id |
| 46 |
* |
| 47 |
* @return bool |
| 48 |
*/ |
| 49 |
public function has_subscription( $form_id = 0 ) { |
| 50 |
|
| 51 |
// Check all subscriptions by default. |
| 52 |
if ( empty( $form_id ) ) { |
| 53 |
$subs = $this->get_subscriptions(); |
| 54 |
} else { |
| 55 |
// If filtering by form. |
| 56 |
$subs = $this->get_subscriptions( $form_id ); |
| 57 |
} |
| 58 |
|
| 59 |
$ret = ! empty( $subs ); |
| 60 |
|
| 61 |
return apply_filters( 'give_recurring_has_subscription', $ret, $form_id, $this ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Has active subscription. |
| 66 |
* |
| 67 |
* @param int $form_id Optional for filtering by form ID. |
| 68 |
* |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
public function has_active_subscription( $form_id = 0 ) { |
| 72 |
|
| 73 |
$ret = false; |
| 74 |
|
| 75 |
// Check all subscriptions by default. |
| 76 |
if ( empty( $form_id ) ) { |
| 77 |
$subs = $this->get_subscriptions(); |
| 78 |
} else { |
| 79 |
// If filtering by form. |
| 80 |
$subs = $this->get_subscriptions( $form_id ); |
| 81 |
} |
| 82 |
|
| 83 |
if ( $subs ) { |
| 84 |
foreach ( $subs as $sub ) { |
| 85 |
if ( $sub->is_active() ) { |
| 86 |
$ret = true; |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return apply_filters( 'give_recurring_has_active_subscription', $ret, $this ); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Add Subscription. |
| 98 |
* |
| 99 |
* @param array $args |
| 100 |
* |
| 101 |
* @return false|Give_Subscription |
| 102 |
*/ |
| 103 |
public function add_subscription( $args = [] ) { |
| 104 |
|
| 105 |
$args = wp_parse_args( $args, $this->subs_db->get_column_defaults() ); |
| 106 |
|
| 107 |
if ( empty( $args['form_id'] ) ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! empty( $this->user_id ) ) { |
| 112 |
$this->set_as_subscriber(); |
| 113 |
} |
| 114 |
|
| 115 |
$args['donor_id'] = $this->id; |
| 116 |
|
| 117 |
$subscription = new Give_Subscription(); |
| 118 |
|
| 119 |
return $subscription->create( $args ); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Add Payment |
| 125 |
* |
| 126 |
* @since 1.0 |
| 127 |
* |
| 128 |
* @param array $args |
| 129 |
* |
| 130 |
* @return mixed |
| 131 |
*/ |
| 132 |
public function add_payment( $args = [] ) { |
| 133 |
|
| 134 |
$args = wp_parse_args( $args, [ |
| 135 |
'subscription_id' => 0, |
| 136 |
'amount' => '0.00', |
| 137 |
'transaction_id' => '', |
| 138 |
] ); |
| 139 |
|
| 140 |
if ( empty( $args['subscription_id'] ) ) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
$subscription = new Give_Subscription( $args['subscription_id'] ); |
| 145 |
|
| 146 |
if ( empty( $subscription ) ) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
unset( $args['subscription_id'] ); |
| 151 |
|
| 152 |
return $subscription->add_payment( $args ); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get Subscription |
| 158 |
* |
| 159 |
* @param int $subscription_id |
| 160 |
* |
| 161 |
* @return object|bool |
| 162 |
*/ |
| 163 |
public function get_subscription( $subscription_id = 0 ) { |
| 164 |
|
| 165 |
$sub = new Give_Subscription( $subscription_id ); |
| 166 |
|
| 167 |
if ( (int) $sub->donor_id !== (int) $this->id ) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
return $sub; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get Subscription by Profile ID |
| 176 |
* |
| 177 |
* @param string $profile_id |
| 178 |
* |
| 179 |
* @return bool|Give_Subscription |
| 180 |
*/ |
| 181 |
public function get_subscription_by_profile_id( $profile_id = '' ) { |
| 182 |
|
| 183 |
if ( empty( $profile_id ) ) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
$sub = new Give_Subscription( $profile_id, true ); |
| 188 |
|
| 189 |
if ( (int) $sub->donor_id !== (int) $this->id ) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
return $sub; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get Subscriptions. |
| 199 |
* |
| 200 |
* Retrieves an array of subscriptions for a the donor. |
| 201 |
* Optional form ID and status(es) can be supplied. |
| 202 |
* |
| 203 |
* @param int $form_id |
| 204 |
* @param array $args Array of arguments |
| 205 |
* |
| 206 |
* @return Give_Subscription[] |
| 207 |
*/ |
| 208 |
public function get_subscriptions( $form_id = 0, $args = [] ) { |
| 209 |
|
| 210 |
if ( ! $this->id > 0 ) { |
| 211 |
return []; |
| 212 |
} |
| 213 |
|
| 214 |
// Backward compatibility |
| 215 |
if ( ! empty( $args ) && is_numeric( current( array_keys( $args ) ) ) ) { |
| 216 |
$args = [ |
| 217 |
'status' => $args, |
| 218 |
]; |
| 219 |
} |
| 220 |
|
| 221 |
$args = wp_parse_args( |
| 222 |
$args, |
| 223 |
[ |
| 224 |
'number' => - 1, |
| 225 |
'status' => give_recurring_get_subscription_statuses_key(), |
| 226 |
] |
| 227 |
); |
| 228 |
|
| 229 |
if ( ! empty( $form_id ) ) { |
| 230 |
$args['form_id'] = $form_id; |
| 231 |
} |
| 232 |
|
| 233 |
$args['donor_id'] = $this->id; |
| 234 |
|
| 235 |
return $this->subs_db->get_subscriptions( $args ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Set as Subscriber |
| 240 |
* |
| 241 |
* Set a user as a subscriber |
| 242 |
* |
| 243 |
* @return void |
| 244 |
*/ |
| 245 |
public function set_as_subscriber() { |
| 246 |
|
| 247 |
$user = new WP_User( $this->user_id ); |
| 248 |
|
| 249 |
if ( $user ) { |
| 250 |
$user->add_role( 'give_subscriber' ); |
| 251 |
do_action( 'give_recurring_set_as_subscriber', $this->user_id ); |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Get New Expiration |
| 258 |
* |
| 259 |
* Calculate a new expiration date |
| 260 |
* |
| 261 |
* @param int $form_id Donation Form ID. |
| 262 |
* @param null $price_id Price ID. |
| 263 |
* @param int $frequency Frequency/Interval Count. |
| 264 |
* @param string $period Period/Interval. Default empty. |
| 265 |
* |
| 266 |
* @return bool|string |
| 267 |
*/ |
| 268 |
public function get_new_expiration( $form_id = 0, $price_id = null, $frequency = 1, $period = '' ) { |
| 269 |
|
| 270 |
// If $period is empty, then try fetching it with possible scenarios. |
| 271 |
if ( empty( $period ) ) { |
| 272 |
if ( isset( $_POST['give-recurring-period-donors-choice'] ) ) { |
| 273 |
$period = give_clean( $_POST['give-recurring-period-donors-choice'] ); |
| 274 |
} else if ( give_has_variable_prices( $form_id ) ) { |
| 275 |
$period = Give_Recurring::get_period( $form_id, $price_id ); |
| 276 |
} else { |
| 277 |
$period = Give_Recurring::get_period( $form_id ); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
// Calculate the quarter as times 3 months |
| 282 |
if ( $period === 'quarter' ) { |
| 283 |
$frequency *= 3; |
| 284 |
$period = 'month'; |
| 285 |
} |
| 286 |
|
| 287 |
return date( 'Y-m-d H:i:s', strtotime( '+ ' . $frequency . $period . ' 23:59:59' ) ); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Get Recurring Customer ID |
| 292 |
* |
| 293 |
* Get a recurring customer ID |
| 294 |
* |
| 295 |
* @since 1.0 |
| 296 |
* |
| 297 |
* @param $gateway string The gateway to get the customer ID for |
| 298 |
* |
| 299 |
* @return string |
| 300 |
*/ |
| 301 |
public function get_recurring_donor_id( $gateway ) { |
| 302 |
|
| 303 |
$recurring_ids = $this->get_recurring_donor_ids(); |
| 304 |
|
| 305 |
if ( is_array( $recurring_ids ) ) { |
| 306 |
if ( false === $gateway || ! array_key_exists( $gateway, $recurring_ids ) ) { |
| 307 |
$gateway = reset( $recurring_ids ); |
| 308 |
} |
| 309 |
|
| 310 |
$donor_id = $recurring_ids[ $gateway ]; |
| 311 |
} else { |
| 312 |
$donor_id = empty( $recurring_ids ) ? false : $recurring_ids; |
| 313 |
} |
| 314 |
|
| 315 |
return apply_filters( 'give_recurring_get_donor_id', $donor_id, $this ); |
| 316 |
|
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Store a recurring customer ID in array |
| 321 |
* |
| 322 |
* Sets a customer ID per gateway as needed; for instance, Stripe you create a customer and then subscribe them to a plan. The customer ID is stored here. |
| 323 |
* |
| 324 |
* @since 1.0 |
| 325 |
* |
| 326 |
* @param $gateway string The gateway to set the customer ID for |
| 327 |
* @param $recurring_id string The recurring profile ID to set |
| 328 |
* |
| 329 |
* @return bool |
| 330 |
*/ |
| 331 |
public function set_recurring_donor_id( $gateway, $recurring_id = '' ) { |
| 332 |
|
| 333 |
// We require a gateway identifier to be included, if it's not, return false. |
| 334 |
if ( false === $gateway ) { |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
$recurring_id = apply_filters( 'give_recurring_set_donor_id', $recurring_id, $this->user_id ); |
| 339 |
$recurring_ids = $this->get_recurring_donor_ids(); |
| 340 |
|
| 341 |
if ( ! is_array( $recurring_ids ) ) { |
| 342 |
|
| 343 |
$existing = $recurring_ids; |
| 344 |
$recurring_ids = []; |
| 345 |
|
| 346 |
// If the first three characters match, we know the existing ID belongs to this gateway |
| 347 |
if ( substr( $recurring_id, 0, 3 ) === substr( $existing, 0, 3 ) ) { |
| 348 |
|
| 349 |
$recurring_ids[ $gateway ] = $existing; |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
$recurring_ids[ $gateway ] = $recurring_id; |
| 356 |
|
| 357 |
// Update donor meta. |
| 358 |
return $this->update_meta( '_give_recurring_id', $recurring_ids ); |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Retrieve the recurring gateway specific IDs for the donor. |
| 364 |
* |
| 365 |
* @since 1.2 |
| 366 |
* |
| 367 |
* @return mixed The profile IDs |
| 368 |
*/ |
| 369 |
public function get_recurring_donor_ids() { |
| 370 |
|
| 371 |
$ids = $this->get_meta( '_give_recurring_id' ); |
| 372 |
|
| 373 |
// Backwards compatibility for user meta. |
| 374 |
if ( empty( $ids ) ) { |
| 375 |
$ids = get_user_meta( $this->user_id, '_give_recurring_id', true ); |
| 376 |
} |
| 377 |
|
| 378 |
return apply_filters( 'give_recurring_donor_ids', $ids, $this ); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
/** |
| 383 |
* Return subscriber object. |
| 384 |
* |
| 385 |
* @since 1.10.1 |
| 386 |
* |
| 387 |
* @return Give_Recurring_Subscriber|null |
| 388 |
*/ |
| 389 |
public static function getSubscriber() { |
| 390 |
// Get subscription. |
| 391 |
$current_user_id = get_current_user_id(); |
| 392 |
|
| 393 |
// Get by user id. |
| 394 |
if ( ! empty( $current_user_id ) ) { |
| 395 |
return new static( $current_user_id, true ); |
| 396 |
} |
| 397 |
|
| 398 |
// Get by email access. |
| 399 |
if ( Give()->email_access->token_exists ) { |
| 400 |
return new static( Give()->email_access->token_email, false ); |
| 401 |
} |
| 402 |
|
| 403 |
// Get by email. |
| 404 |
if ( Give()->session->get_session_expiration() ) { |
| 405 |
$subscriber_email = maybe_unserialize( Give()->session->get( 'give_purchase' ) ); |
| 406 |
$subscriber_email = isset( $subscriber_email['user_email'] ) ? $subscriber_email['user_email'] : ''; |
| 407 |
|
| 408 |
return new static( $subscriber_email, false ); |
| 409 |
} |
| 410 |
|
| 411 |
return null; |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Return boolean to determine access |
| 416 |
* |
| 417 |
* @since 1.10.1 |
| 418 |
* |
| 419 |
* a. Check if a user is logged in |
| 420 |
* b. Does an email-access token exist? |
| 421 |
*/ |
| 422 |
public static function canAccessView() { |
| 423 |
return is_user_logged_in() || ( give_is_setting_enabled( give_get_option( 'email_access' ) ) && Give()->email_access->token_exists ); |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Return boolean to determine subscription belongs to donor or not |
| 428 |
* |
| 429 |
* @param Give_Subscription $subscription |
| 430 |
* @param Give_Recurring_Subscriber $subscriber |
| 431 |
* |
| 432 |
* @return boolean |
| 433 |
*/ |
| 434 |
public static function doesSubscriptionBelongsTo( $subscription, $subscriber = null ) { |
| 435 |
$subscriber = $subscriber ?: self::getSubscriber(); |
| 436 |
|
| 437 |
return $subscription->donor_id === $subscriber->id; |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* Get subscriber access type. |
| 442 |
* |
| 443 |
* @since 1.10.1 |
| 444 |
* @return string |
| 445 |
*/ |
| 446 |
public static function getAccessType() { |
| 447 |
if ( is_user_logged_in() ) { |
| 448 |
return 'wpUser'; |
| 449 |
} |
| 450 |
|
| 451 |
if ( Give()->email_access->token_exists && give_is_setting_enabled( give_get_option( 'email_access' ) ) ) { |
| 452 |
return 'EmailAccess'; |
| 453 |
} |
| 454 |
|
| 455 |
if ( false !== Give()->session->get_session_expiration() ) { |
| 456 |
return 'DonorSession'; |
| 457 |
} |
| 458 |
|
| 459 |
return ''; |
| 460 |
} |
| 461 |
|
| 462 |
} |
| 463 |
|