@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | // Exit if accessed directly |
| 4 | 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | - exit; |
|
| 5 | + exit; |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | |
@@ -13,183 +13,183 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | class WPInv_Subscription { |
| 15 | 15 | |
| 16 | - private $subs_db; |
|
| 16 | + private $subs_db; |
|
| 17 | + |
|
| 18 | + public $id = 0; |
|
| 19 | + public $customer_id = 0; |
|
| 20 | + public $period = ''; |
|
| 21 | + public $initial_amount = ''; |
|
| 22 | + public $recurring_amount = ''; |
|
| 23 | + public $bill_times = 0; |
|
| 24 | + public $transaction_id = ''; |
|
| 25 | + public $parent_payment_id = 0; |
|
| 26 | + public $product_id = 0; |
|
| 27 | + public $created = '0000-00-00 00:00:00'; |
|
| 28 | + public $expiration = '0000-00-00 00:00:00'; |
|
| 29 | + public $trial_period = ''; |
|
| 30 | + public $status = 'pending'; |
|
| 31 | + public $profile_id = ''; |
|
| 32 | + public $gateway = ''; |
|
| 33 | + public $customer; |
|
| 17 | 34 | |
| 18 | - public $id = 0; |
|
| 19 | - public $customer_id = 0; |
|
| 20 | - public $period = ''; |
|
| 21 | - public $initial_amount = ''; |
|
| 22 | - public $recurring_amount = ''; |
|
| 23 | - public $bill_times = 0; |
|
| 24 | - public $transaction_id = ''; |
|
| 25 | - public $parent_payment_id = 0; |
|
| 26 | - public $product_id = 0; |
|
| 27 | - public $created = '0000-00-00 00:00:00'; |
|
| 28 | - public $expiration = '0000-00-00 00:00:00'; |
|
| 29 | - public $trial_period = ''; |
|
| 30 | - public $status = 'pending'; |
|
| 31 | - public $profile_id = ''; |
|
| 32 | - public $gateway = ''; |
|
| 33 | - public $customer; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Get us started |
|
| 37 | - * |
|
| 38 | - * @since 1.0.0 |
|
| 39 | - * @return void |
|
| 40 | - */ |
|
| 41 | - function __construct( $_id_or_object = 0, $_by_profile_id = false ) { |
|
| 35 | + /** |
|
| 36 | + * Get us started |
|
| 37 | + * |
|
| 38 | + * @since 1.0.0 |
|
| 39 | + * @return void |
|
| 40 | + */ |
|
| 41 | + function __construct( $_id_or_object = 0, $_by_profile_id = false ) { |
|
| 42 | 42 | |
| 43 | - $this->subs_db = new WPInv_Subscriptions_DB; |
|
| 43 | + $this->subs_db = new WPInv_Subscriptions_DB; |
|
| 44 | 44 | |
| 45 | - if( $_by_profile_id ) { |
|
| 45 | + if( $_by_profile_id ) { |
|
| 46 | 46 | |
| 47 | - $_sub = $this->subs_db->get_by( 'profile_id', $_id_or_object ); |
|
| 47 | + $_sub = $this->subs_db->get_by( 'profile_id', $_id_or_object ); |
|
| 48 | 48 | |
| 49 | - if( empty( $_sub ) ) { |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 49 | + if( empty( $_sub ) ) { |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - $_id_or_object = $_sub; |
|
| 53 | + $_id_or_object = $_sub; |
|
| 54 | 54 | |
| 55 | - } |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - return $this->setup_subscription( $_id_or_object ); |
|
| 58 | - } |
|
| 57 | + return $this->setup_subscription( $_id_or_object ); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Setup the subscription object |
|
| 62 | - * |
|
| 63 | - * @since 1.0.0 |
|
| 64 | - * @return void |
|
| 65 | - */ |
|
| 66 | - private function setup_subscription( $id_or_object = 0 ) { |
|
| 60 | + /** |
|
| 61 | + * Setup the subscription object |
|
| 62 | + * |
|
| 63 | + * @since 1.0.0 |
|
| 64 | + * @return void |
|
| 65 | + */ |
|
| 66 | + private function setup_subscription( $id_or_object = 0 ) { |
|
| 67 | 67 | |
| 68 | - if( empty( $id_or_object ) ) { |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 68 | + if( empty( $id_or_object ) ) { |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - if( is_numeric( $id_or_object ) ) { |
|
| 72 | + if( is_numeric( $id_or_object ) ) { |
|
| 73 | 73 | |
| 74 | - $sub = $this->subs_db->get( $id_or_object ); |
|
| 74 | + $sub = $this->subs_db->get( $id_or_object ); |
|
| 75 | 75 | |
| 76 | - } elseif( is_object( $id_or_object ) ) { |
|
| 76 | + } elseif( is_object( $id_or_object ) ) { |
|
| 77 | 77 | |
| 78 | - $sub = $id_or_object; |
|
| 78 | + $sub = $id_or_object; |
|
| 79 | 79 | |
| 80 | - } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - if( empty( $sub ) ) { |
|
| 83 | - return false; |
|
| 84 | - } |
|
| 82 | + if( empty( $sub ) ) { |
|
| 83 | + return false; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - foreach( $sub as $key => $value ) { |
|
| 87 | - $this->$key = $value; |
|
| 88 | - } |
|
| 86 | + foreach( $sub as $key => $value ) { |
|
| 87 | + $this->$key = $value; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - $this->customer = get_userdata( $this->customer_id ); |
|
| 91 | - $this->gateway = wpinv_get_payment_gateway( $this->parent_payment_id ); |
|
| 90 | + $this->customer = get_userdata( $this->customer_id ); |
|
| 91 | + $this->gateway = wpinv_get_payment_gateway( $this->parent_payment_id ); |
|
| 92 | 92 | |
| 93 | - do_action( 'wpinv_recurring_setup_subscription', $this ); |
|
| 93 | + do_action( 'wpinv_recurring_setup_subscription', $this ); |
|
| 94 | 94 | |
| 95 | - return $this; |
|
| 96 | - } |
|
| 95 | + return $this; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Magic __get function to dispatch a call to retrieve a private property |
|
| 100 | - * |
|
| 101 | - * @since 1.0.0 |
|
| 102 | - */ |
|
| 103 | - public function __get( $key ) { |
|
| 98 | + /** |
|
| 99 | + * Magic __get function to dispatch a call to retrieve a private property |
|
| 100 | + * |
|
| 101 | + * @since 1.0.0 |
|
| 102 | + */ |
|
| 103 | + public function __get( $key ) { |
|
| 104 | 104 | |
| 105 | - if( method_exists( $this, 'get_' . $key ) ) { |
|
| 105 | + if( method_exists( $this, 'get_' . $key ) ) { |
|
| 106 | 106 | |
| 107 | - return call_user_func( array( $this, 'get_' . $key ) ); |
|
| 107 | + return call_user_func( array( $this, 'get_' . $key ) ); |
|
| 108 | 108 | |
| 109 | - } else { |
|
| 109 | + } else { |
|
| 110 | 110 | |
| 111 | - return new WP_Error( 'wpinv-subscription-invalid-property', sprintf( __( 'Can\'t get property %s', 'invoicing' ), $key ) ); |
|
| 111 | + return new WP_Error( 'wpinv-subscription-invalid-property', sprintf( __( 'Can\'t get property %s', 'invoicing' ), $key ) ); |
|
| 112 | 112 | |
| 113 | - } |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Creates a subscription |
|
| 119 | - * |
|
| 120 | - * @since 1.0.0 |
|
| 121 | - * @param array $data Array of attributes for a subscription |
|
| 122 | - * @return mixed false if data isn't passed and class not instantiated for creation |
|
| 123 | - */ |
|
| 124 | - public function create( $data = array() ) { |
|
| 117 | + /** |
|
| 118 | + * Creates a subscription |
|
| 119 | + * |
|
| 120 | + * @since 1.0.0 |
|
| 121 | + * @param array $data Array of attributes for a subscription |
|
| 122 | + * @return mixed false if data isn't passed and class not instantiated for creation |
|
| 123 | + */ |
|
| 124 | + public function create( $data = array() ) { |
|
| 125 | 125 | |
| 126 | - if ( $this->id != 0 ) { |
|
| 127 | - return false; |
|
| 128 | - } |
|
| 126 | + if ( $this->id != 0 ) { |
|
| 127 | + return false; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - $defaults = array( |
|
| 131 | - 'customer_id' => 0, |
|
| 132 | - 'frequency' => '', |
|
| 133 | - 'period' => '', |
|
| 134 | - 'initial_amount' => '', |
|
| 135 | - 'recurring_amount' => '', |
|
| 136 | - 'bill_times' => 0, |
|
| 137 | - 'parent_payment_id' => 0, |
|
| 138 | - 'product_id' => 0, |
|
| 139 | - 'created' => '', |
|
| 140 | - 'expiration' => '', |
|
| 141 | - 'status' => '', |
|
| 142 | - 'profile_id' => '', |
|
| 143 | - ); |
|
| 130 | + $defaults = array( |
|
| 131 | + 'customer_id' => 0, |
|
| 132 | + 'frequency' => '', |
|
| 133 | + 'period' => '', |
|
| 134 | + 'initial_amount' => '', |
|
| 135 | + 'recurring_amount' => '', |
|
| 136 | + 'bill_times' => 0, |
|
| 137 | + 'parent_payment_id' => 0, |
|
| 138 | + 'product_id' => 0, |
|
| 139 | + 'created' => '', |
|
| 140 | + 'expiration' => '', |
|
| 141 | + 'status' => '', |
|
| 142 | + 'profile_id' => '', |
|
| 143 | + ); |
|
| 144 | 144 | |
| 145 | - $args = wp_parse_args( $data, $defaults ); |
|
| 145 | + $args = wp_parse_args( $data, $defaults ); |
|
| 146 | 146 | |
| 147 | - if( $args['expiration'] && strtotime( 'NOW', current_time( 'timestamp' ) ) > strtotime( $args['expiration'], current_time( 'timestamp' ) ) ) { |
|
| 147 | + if( $args['expiration'] && strtotime( 'NOW', current_time( 'timestamp' ) ) > strtotime( $args['expiration'], current_time( 'timestamp' ) ) ) { |
|
| 148 | 148 | |
| 149 | - if( 'active' == $args['status'] || 'trialling' == $args['status'] ) { |
|
| 149 | + if( 'active' == $args['status'] || 'trialling' == $args['status'] ) { |
|
| 150 | 150 | |
| 151 | - // Force an active subscription to expired if expiration date is in the past |
|
| 152 | - $args['status'] = 'expired'; |
|
| 151 | + // Force an active subscription to expired if expiration date is in the past |
|
| 152 | + $args['status'] = 'expired'; |
|
| 153 | 153 | |
| 154 | - } |
|
| 155 | - } |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - do_action( 'wpinv_subscription_pre_create', $args ); |
|
| 157 | + do_action( 'wpinv_subscription_pre_create', $args ); |
|
| 158 | 158 | |
| 159 | - $id = $this->subs_db->insert( $args, 'subscription' ); |
|
| 159 | + $id = $this->subs_db->insert( $args, 'subscription' ); |
|
| 160 | 160 | |
| 161 | - do_action( 'wpinv_subscription_post_create', $id, $args ); |
|
| 161 | + do_action( 'wpinv_subscription_post_create', $id, $args ); |
|
| 162 | 162 | |
| 163 | - return $this->setup_subscription( $id ); |
|
| 163 | + return $this->setup_subscription( $id ); |
|
| 164 | 164 | |
| 165 | - } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Updates a subscription |
|
| 169 | - * |
|
| 170 | - * @since 1.0.0 |
|
| 171 | - * @param array $args Array of fields to update |
|
| 172 | - * @return bool |
|
| 173 | - */ |
|
| 174 | - public function update( $args = array() ) { |
|
| 167 | + /** |
|
| 168 | + * Updates a subscription |
|
| 169 | + * |
|
| 170 | + * @since 1.0.0 |
|
| 171 | + * @param array $args Array of fields to update |
|
| 172 | + * @return bool |
|
| 173 | + */ |
|
| 174 | + public function update( $args = array() ) { |
|
| 175 | 175 | |
| 176 | - $ret = $this->subs_db->update( $this->id, $args ); |
|
| 176 | + $ret = $this->subs_db->update( $this->id, $args ); |
|
| 177 | 177 | |
| 178 | - do_action( 'wpinv_recurring_update_subscription', $this->id, $args, $this ); |
|
| 178 | + do_action( 'wpinv_recurring_update_subscription', $this->id, $args, $this ); |
|
| 179 | 179 | |
| 180 | - return $ret; |
|
| 180 | + return $ret; |
|
| 181 | 181 | |
| 182 | - } |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Delete the subscription |
|
| 186 | - * |
|
| 187 | - * @since 1.0.0 |
|
| 188 | - * @return bool |
|
| 189 | - */ |
|
| 190 | - public function delete() { |
|
| 191 | - return $this->subs_db->delete( $this->id ); |
|
| 192 | - } |
|
| 184 | + /** |
|
| 185 | + * Delete the subscription |
|
| 186 | + * |
|
| 187 | + * @since 1.0.0 |
|
| 188 | + * @return bool |
|
| 189 | + */ |
|
| 190 | + public function delete() { |
|
| 191 | + return $this->subs_db->delete( $this->id ); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | 194 | /** |
| 195 | 195 | * Retrieves the parent payment ID |
@@ -357,8 +357,8 @@ discard block |
||
| 357 | 357 | |
| 358 | 358 | $invoice = wpinv_get_invoice( $invoice->ID ); |
| 359 | 359 | |
| 360 | - // Send email notifications. |
|
| 361 | - wpinv_completed_invoice_notification( $invoice->ID ); |
|
| 360 | + // Send email notifications. |
|
| 361 | + wpinv_completed_invoice_notification( $invoice->ID ); |
|
| 362 | 362 | |
| 363 | 363 | do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this ); |
| 364 | 364 | do_action( 'wpinv_recurring_record_payment', $invoice->ID, $this->parent_payment_id, $args['amount'], $args['transaction_id'] ); |
@@ -369,185 +369,185 @@ discard block |
||
| 369 | 369 | return false; |
| 370 | 370 | } |
| 371 | 371 | |
| 372 | - /** |
|
| 373 | - * Retrieves the transaction ID from the subscription |
|
| 374 | - * |
|
| 375 | - * @since 1.0.0 |
|
| 376 | - * @return bool |
|
| 377 | - */ |
|
| 378 | - public function get_transaction_id() { |
|
| 372 | + /** |
|
| 373 | + * Retrieves the transaction ID from the subscription |
|
| 374 | + * |
|
| 375 | + * @since 1.0.0 |
|
| 376 | + * @return bool |
|
| 377 | + */ |
|
| 378 | + public function get_transaction_id() { |
|
| 379 | 379 | |
| 380 | - if( empty( $this->transaction_id ) ) { |
|
| 380 | + if( empty( $this->transaction_id ) ) { |
|
| 381 | 381 | |
| 382 | - $txn_id = wpinv_get_payment_transaction_id( $this->parent_payment_id ); |
|
| 382 | + $txn_id = wpinv_get_payment_transaction_id( $this->parent_payment_id ); |
|
| 383 | 383 | |
| 384 | - if( ! empty( $txn_id ) && (int) $this->parent_payment_id !== (int) $txn_id ) { |
|
| 385 | - $this->set_transaction_id( $txn_id ); |
|
| 386 | - } |
|
| 384 | + if( ! empty( $txn_id ) && (int) $this->parent_payment_id !== (int) $txn_id ) { |
|
| 385 | + $this->set_transaction_id( $txn_id ); |
|
| 386 | + } |
|
| 387 | 387 | |
| 388 | - } |
|
| 388 | + } |
|
| 389 | 389 | |
| 390 | - return $this->transaction_id; |
|
| 390 | + return $this->transaction_id; |
|
| 391 | 391 | |
| 392 | - } |
|
| 392 | + } |
|
| 393 | 393 | |
| 394 | - /** |
|
| 395 | - * Stores the transaction ID for the subscription purchase |
|
| 396 | - * |
|
| 397 | - * @since 1.0.0.4 |
|
| 398 | - * @return bool |
|
| 399 | - */ |
|
| 400 | - public function set_transaction_id( $txn_id = '' ) { |
|
| 401 | - $this->update( array( 'transaction_id' => $txn_id ) ); |
|
| 402 | - $this->transaction_id = $txn_id; |
|
| 403 | - } |
|
| 394 | + /** |
|
| 395 | + * Stores the transaction ID for the subscription purchase |
|
| 396 | + * |
|
| 397 | + * @since 1.0.0.4 |
|
| 398 | + * @return bool |
|
| 399 | + */ |
|
| 400 | + public function set_transaction_id( $txn_id = '' ) { |
|
| 401 | + $this->update( array( 'transaction_id' => $txn_id ) ); |
|
| 402 | + $this->transaction_id = $txn_id; |
|
| 403 | + } |
|
| 404 | 404 | |
| 405 | - /** |
|
| 406 | - * Renews a subscription |
|
| 407 | - * |
|
| 408 | - * @since 1.0.0 |
|
| 409 | - * @return bool |
|
| 410 | - */ |
|
| 411 | - public function renew() { |
|
| 405 | + /** |
|
| 406 | + * Renews a subscription |
|
| 407 | + * |
|
| 408 | + * @since 1.0.0 |
|
| 409 | + * @return bool |
|
| 410 | + */ |
|
| 411 | + public function renew() { |
|
| 412 | 412 | |
| 413 | - $expires = $this->get_expiration_time(); |
|
| 413 | + $expires = $this->get_expiration_time(); |
|
| 414 | 414 | |
| 415 | 415 | |
| 416 | - // Determine what date to use as the start for the new expiration calculation |
|
| 417 | - if( $expires > current_time( 'timestamp' ) && $this->is_active() ) { |
|
| 416 | + // Determine what date to use as the start for the new expiration calculation |
|
| 417 | + if( $expires > current_time( 'timestamp' ) && $this->is_active() ) { |
|
| 418 | 418 | |
| 419 | - $base_date = $expires; |
|
| 419 | + $base_date = $expires; |
|
| 420 | 420 | |
| 421 | - } else { |
|
| 421 | + } else { |
|
| 422 | 422 | |
| 423 | - $base_date = current_time( 'timestamp' ); |
|
| 423 | + $base_date = current_time( 'timestamp' ); |
|
| 424 | 424 | |
| 425 | - } |
|
| 425 | + } |
|
| 426 | 426 | |
| 427 | - $last_day = wpinv_cal_days_in_month( CAL_GREGORIAN, date( 'n', $base_date ), date( 'Y', $base_date ) ); |
|
| 427 | + $last_day = wpinv_cal_days_in_month( CAL_GREGORIAN, date( 'n', $base_date ), date( 'Y', $base_date ) ); |
|
| 428 | 428 | |
| 429 | 429 | |
| 430 | - $frequency = isset($this->frequency) ? $this->frequency : 1; |
|
| 431 | - $expiration = date( 'Y-m-d H:i:s', strtotime( '+' . $frequency . ' ' . $this->period . ' 23:59:59', $base_date ) ); |
|
| 430 | + $frequency = isset($this->frequency) ? $this->frequency : 1; |
|
| 431 | + $expiration = date( 'Y-m-d H:i:s', strtotime( '+' . $frequency . ' ' . $this->period . ' 23:59:59', $base_date ) ); |
|
| 432 | 432 | |
| 433 | - if( date( 'j', $base_date ) == $last_day && 'day' != $this->period ) { |
|
| 434 | - $expiration = date( 'Y-m-d H:i:s', strtotime( $expiration . ' +2 days' ) ); |
|
| 435 | - } |
|
| 433 | + if( date( 'j', $base_date ) == $last_day && 'day' != $this->period ) { |
|
| 434 | + $expiration = date( 'Y-m-d H:i:s', strtotime( $expiration . ' +2 days' ) ); |
|
| 435 | + } |
|
| 436 | 436 | |
| 437 | - $expiration = apply_filters( 'wpinv_subscription_renewal_expiration', $expiration, $this->id, $this ); |
|
| 437 | + $expiration = apply_filters( 'wpinv_subscription_renewal_expiration', $expiration, $this->id, $this ); |
|
| 438 | 438 | |
| 439 | - do_action( 'wpinv_subscription_pre_renew', $this->id, $expiration, $this ); |
|
| 439 | + do_action( 'wpinv_subscription_pre_renew', $this->id, $expiration, $this ); |
|
| 440 | 440 | |
| 441 | - $this->status = 'active'; |
|
| 442 | - $times_billed = $this->get_times_billed(); |
|
| 441 | + $this->status = 'active'; |
|
| 442 | + $times_billed = $this->get_times_billed(); |
|
| 443 | 443 | |
| 444 | - // Complete subscription if applicable |
|
| 445 | - if ( $this->bill_times > 0 && $times_billed >= $this->bill_times ) { |
|
| 446 | - $this->complete(); |
|
| 447 | - $this->status = 'completed'; |
|
| 448 | - } |
|
| 444 | + // Complete subscription if applicable |
|
| 445 | + if ( $this->bill_times > 0 && $times_billed >= $this->bill_times ) { |
|
| 446 | + $this->complete(); |
|
| 447 | + $this->status = 'completed'; |
|
| 448 | + } |
|
| 449 | 449 | |
| 450 | - $args = array( |
|
| 451 | - 'expiration' => $expiration, |
|
| 452 | - 'status' => $this->status, |
|
| 453 | - ); |
|
| 450 | + $args = array( |
|
| 451 | + 'expiration' => $expiration, |
|
| 452 | + 'status' => $this->status, |
|
| 453 | + ); |
|
| 454 | 454 | |
| 455 | 455 | $this->subs_db->update( $this->id, $args ); |
| 456 | 456 | |
| 457 | - do_action( 'wpinv_subscription_post_renew', $this->id, $expiration, $this ); |
|
| 458 | - do_action( 'wpinv_recurring_set_subscription_status', $this->id, $this->status, $this ); |
|
| 457 | + do_action( 'wpinv_subscription_post_renew', $this->id, $expiration, $this ); |
|
| 458 | + do_action( 'wpinv_recurring_set_subscription_status', $this->id, $this->status, $this ); |
|
| 459 | 459 | |
| 460 | - } |
|
| 460 | + } |
|
| 461 | 461 | |
| 462 | - /** |
|
| 463 | - * Marks a subscription as completed |
|
| 464 | - * |
|
| 465 | - * Subscription is completed when the number of payments matches the billing_times field |
|
| 466 | - * |
|
| 467 | - * @since 1.0.0 |
|
| 468 | - * @return void |
|
| 469 | - */ |
|
| 470 | - public function complete() { |
|
| 462 | + /** |
|
| 463 | + * Marks a subscription as completed |
|
| 464 | + * |
|
| 465 | + * Subscription is completed when the number of payments matches the billing_times field |
|
| 466 | + * |
|
| 467 | + * @since 1.0.0 |
|
| 468 | + * @return void |
|
| 469 | + */ |
|
| 470 | + public function complete() { |
|
| 471 | 471 | |
| 472 | - // Only mark a subscription as complete if it's not already cancelled. |
|
| 473 | - if ( 'cancelled' === $this->status ) { |
|
| 474 | - return; |
|
| 475 | - } |
|
| 472 | + // Only mark a subscription as complete if it's not already cancelled. |
|
| 473 | + if ( 'cancelled' === $this->status ) { |
|
| 474 | + return; |
|
| 475 | + } |
|
| 476 | 476 | |
| 477 | - $args = array( |
|
| 478 | - 'status' => 'completed' |
|
| 479 | - ); |
|
| 477 | + $args = array( |
|
| 478 | + 'status' => 'completed' |
|
| 479 | + ); |
|
| 480 | 480 | |
| 481 | - if( $this->subs_db->update( $this->id, $args ) ) { |
|
| 481 | + if( $this->subs_db->update( $this->id, $args ) ) { |
|
| 482 | 482 | |
| 483 | - $this->status = 'completed'; |
|
| 483 | + $this->status = 'completed'; |
|
| 484 | 484 | |
| 485 | - do_action( 'wpinv_subscription_completed', $this->id, $this ); |
|
| 485 | + do_action( 'wpinv_subscription_completed', $this->id, $this ); |
|
| 486 | 486 | |
| 487 | - } |
|
| 487 | + } |
|
| 488 | 488 | |
| 489 | - } |
|
| 489 | + } |
|
| 490 | 490 | |
| 491 | - /** |
|
| 492 | - * Marks a subscription as expired |
|
| 493 | - * |
|
| 494 | - * Subscription is completed when the billing times is reached |
|
| 495 | - * |
|
| 496 | - * @since 1.0.0 |
|
| 497 | - * @param $check_expiration bool True if expiration date should be checked with merchant processor before expiring |
|
| 498 | - * @return void |
|
| 499 | - */ |
|
| 500 | - public function expire( $check_expiration = false ) { |
|
| 491 | + /** |
|
| 492 | + * Marks a subscription as expired |
|
| 493 | + * |
|
| 494 | + * Subscription is completed when the billing times is reached |
|
| 495 | + * |
|
| 496 | + * @since 1.0.0 |
|
| 497 | + * @param $check_expiration bool True if expiration date should be checked with merchant processor before expiring |
|
| 498 | + * @return void |
|
| 499 | + */ |
|
| 500 | + public function expire( $check_expiration = false ) { |
|
| 501 | 501 | |
| 502 | - $expiration = $this->expiration; |
|
| 502 | + $expiration = $this->expiration; |
|
| 503 | 503 | |
| 504 | - if( $check_expiration ) { |
|
| 504 | + if( $check_expiration ) { |
|
| 505 | 505 | |
| 506 | - // check_expiration() updates $this->expiration so compare to $expiration above |
|
| 506 | + // check_expiration() updates $this->expiration so compare to $expiration above |
|
| 507 | 507 | |
| 508 | - if( $expiration < $this->get_expiration() && current_time( 'timestamp' ) < $this->get_expiration_time() ) { |
|
| 508 | + if( $expiration < $this->get_expiration() && current_time( 'timestamp' ) < $this->get_expiration_time() ) { |
|
| 509 | 509 | |
| 510 | - return false; // Do not mark as expired since real expiration date is in the future |
|
| 511 | - } |
|
| 510 | + return false; // Do not mark as expired since real expiration date is in the future |
|
| 511 | + } |
|
| 512 | 512 | |
| 513 | - } |
|
| 513 | + } |
|
| 514 | 514 | |
| 515 | - $args = array( |
|
| 516 | - 'status' => 'expired' |
|
| 517 | - ); |
|
| 515 | + $args = array( |
|
| 516 | + 'status' => 'expired' |
|
| 517 | + ); |
|
| 518 | 518 | |
| 519 | - if( $this->subs_db->update( $this->id, $args ) ) { |
|
| 519 | + if( $this->subs_db->update( $this->id, $args ) ) { |
|
| 520 | 520 | |
| 521 | - $this->status = 'expired'; |
|
| 521 | + $this->status = 'expired'; |
|
| 522 | 522 | |
| 523 | - do_action( 'wpinv_subscription_expired', $this->id, $this ); |
|
| 523 | + do_action( 'wpinv_subscription_expired', $this->id, $this ); |
|
| 524 | 524 | |
| 525 | - } |
|
| 525 | + } |
|
| 526 | 526 | |
| 527 | - } |
|
| 527 | + } |
|
| 528 | 528 | |
| 529 | - /** |
|
| 530 | - * Marks a subscription as failing |
|
| 531 | - * |
|
| 532 | - * @since 2.4.2 |
|
| 533 | - * @return void |
|
| 534 | - */ |
|
| 535 | - public function failing() { |
|
| 529 | + /** |
|
| 530 | + * Marks a subscription as failing |
|
| 531 | + * |
|
| 532 | + * @since 2.4.2 |
|
| 533 | + * @return void |
|
| 534 | + */ |
|
| 535 | + public function failing() { |
|
| 536 | 536 | |
| 537 | - $args = array( |
|
| 538 | - 'status' => 'failing' |
|
| 539 | - ); |
|
| 537 | + $args = array( |
|
| 538 | + 'status' => 'failing' |
|
| 539 | + ); |
|
| 540 | 540 | |
| 541 | - if( $this->subs_db->update( $this->id, $args ) ) { |
|
| 541 | + if( $this->subs_db->update( $this->id, $args ) ) { |
|
| 542 | 542 | |
| 543 | - $this->status = 'failing'; |
|
| 543 | + $this->status = 'failing'; |
|
| 544 | 544 | |
| 545 | - do_action( 'wpinv_subscription_failing', $this->id, $this ); |
|
| 545 | + do_action( 'wpinv_subscription_failing', $this->id, $this ); |
|
| 546 | 546 | |
| 547 | 547 | |
| 548 | - } |
|
| 548 | + } |
|
| 549 | 549 | |
| 550 | - } |
|
| 550 | + } |
|
| 551 | 551 | |
| 552 | 552 | /** |
| 553 | 553 | * Marks a subscription as cancelled |
@@ -581,22 +581,22 @@ discard block |
||
| 581 | 581 | } |
| 582 | 582 | } |
| 583 | 583 | |
| 584 | - /** |
|
| 585 | - * Determines if subscription can be cancelled |
|
| 586 | - * |
|
| 587 | - * This method is filtered by payment gateways in order to return true on subscriptions |
|
| 588 | - * that can be cancelled with a profile ID through the merchant processor |
|
| 589 | - * |
|
| 590 | - * @since 1.0.0 |
|
| 591 | - * @return bool |
|
| 592 | - */ |
|
| 593 | - public function can_cancel() { |
|
| 584 | + /** |
|
| 585 | + * Determines if subscription can be cancelled |
|
| 586 | + * |
|
| 587 | + * This method is filtered by payment gateways in order to return true on subscriptions |
|
| 588 | + * that can be cancelled with a profile ID through the merchant processor |
|
| 589 | + * |
|
| 590 | + * @since 1.0.0 |
|
| 591 | + * @return bool |
|
| 592 | + */ |
|
| 593 | + public function can_cancel() { |
|
| 594 | 594 | $ret = false; |
| 595 | - if( $this->gateway === 'manual' || in_array( $this->status, $this->get_cancellable_statuses() ) ) { |
|
| 595 | + if( $this->gateway === 'manual' || in_array( $this->status, $this->get_cancellable_statuses() ) ) { |
|
| 596 | 596 | $ret = true; |
| 597 | 597 | } |
| 598 | - return apply_filters( 'wpinv_subscription_can_cancel', $ret, $this ); |
|
| 599 | - } |
|
| 598 | + return apply_filters( 'wpinv_subscription_can_cancel', $ret, $this ); |
|
| 599 | + } |
|
| 600 | 600 | |
| 601 | 601 | /** |
| 602 | 602 | * Returns an array of subscription statuses that can be cancelled |
@@ -609,197 +609,197 @@ discard block |
||
| 609 | 609 | return apply_filters( 'wpinv_recurring_cancellable_statuses', array( 'active', 'trialling', 'failing' ) ); |
| 610 | 610 | } |
| 611 | 611 | |
| 612 | - /** |
|
| 613 | - * Retrieves the URL to cancel subscription |
|
| 614 | - * |
|
| 615 | - * @since 1.0.0 |
|
| 616 | - * @return string |
|
| 617 | - */ |
|
| 618 | - public function get_cancel_url() { |
|
| 612 | + /** |
|
| 613 | + * Retrieves the URL to cancel subscription |
|
| 614 | + * |
|
| 615 | + * @since 1.0.0 |
|
| 616 | + * @return string |
|
| 617 | + */ |
|
| 618 | + public function get_cancel_url() { |
|
| 619 | 619 | |
| 620 | - $url = wp_nonce_url( add_query_arg( array( 'wpinv_action' => 'cancel_subscription', 'sub_id' => $this->id ) ), 'wpinv-recurring-cancel' ); |
|
| 620 | + $url = wp_nonce_url( add_query_arg( array( 'wpinv_action' => 'cancel_subscription', 'sub_id' => $this->id ) ), 'wpinv-recurring-cancel' ); |
|
| 621 | 621 | |
| 622 | - return apply_filters( 'wpinv_subscription_cancel_url', $url, $this ); |
|
| 623 | - } |
|
| 622 | + return apply_filters( 'wpinv_subscription_cancel_url', $url, $this ); |
|
| 623 | + } |
|
| 624 | 624 | |
| 625 | - /** |
|
| 626 | - * Determines if subscription can be manually renewed |
|
| 627 | - * |
|
| 628 | - * This method is filtered by payment gateways in order to return true on subscriptions |
|
| 629 | - * that can be renewed manually |
|
| 630 | - * |
|
| 631 | - * @since 2.5 |
|
| 632 | - * @return bool |
|
| 633 | - */ |
|
| 634 | - public function can_renew() { |
|
| 625 | + /** |
|
| 626 | + * Determines if subscription can be manually renewed |
|
| 627 | + * |
|
| 628 | + * This method is filtered by payment gateways in order to return true on subscriptions |
|
| 629 | + * that can be renewed manually |
|
| 630 | + * |
|
| 631 | + * @since 2.5 |
|
| 632 | + * @return bool |
|
| 633 | + */ |
|
| 634 | + public function can_renew() { |
|
| 635 | 635 | |
| 636 | - return apply_filters( 'wpinv_subscription_can_renew', true, $this ); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Retrieves the URL to renew a subscription |
|
| 641 | - * |
|
| 642 | - * @since 2.5 |
|
| 643 | - * @return string |
|
| 644 | - */ |
|
| 645 | - public function get_renew_url() { |
|
| 646 | - |
|
| 647 | - $url = wp_nonce_url( add_query_arg( array( 'wpinv_action' => 'renew_subscription', 'sub_id' => $this->id ) ), 'wpinv-recurring-renew' ); |
|
| 648 | - |
|
| 649 | - return apply_filters( 'wpinv_subscription_renew_url', $url, $this ); |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * Determines if subscription can have their payment method updated |
|
| 654 | - * |
|
| 655 | - * @since 1.0.0 |
|
| 656 | - * @return bool |
|
| 657 | - */ |
|
| 658 | - public function can_update() { |
|
| 659 | - return apply_filters( 'wpinv_subscription_can_update', false, $this ); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * Retrieves the URL to update subscription |
|
| 664 | - * |
|
| 665 | - * @since 1.0.0 |
|
| 666 | - * @return void |
|
| 667 | - */ |
|
| 668 | - public function get_update_url() { |
|
| 669 | - |
|
| 670 | - $url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->id ) ); |
|
| 671 | - |
|
| 672 | - return apply_filters( 'wpinv_subscription_update_url', $url, $this ); |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - /** |
|
| 676 | - * Determines if subscription is active |
|
| 677 | - * |
|
| 678 | - * @since 1.0.0 |
|
| 679 | - * @return void |
|
| 680 | - */ |
|
| 681 | - public function is_active() { |
|
| 682 | - |
|
| 683 | - $ret = false; |
|
| 684 | - |
|
| 685 | - if( ! $this->is_expired() && ( $this->status == 'active' || $this->status == 'cancelled' || $this->status == 'trialling' ) ) { |
|
| 686 | - $ret = true; |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - return apply_filters( 'wpinv_subscription_is_active', $ret, $this->id, $this ); |
|
| 690 | - |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - /** |
|
| 694 | - * Determines if subscription is expired |
|
| 695 | - * |
|
| 696 | - * @since 1.0.0 |
|
| 697 | - * @return void |
|
| 698 | - */ |
|
| 699 | - public function is_expired() { |
|
| 700 | - |
|
| 701 | - $ret = false; |
|
| 702 | - |
|
| 703 | - if ( $this->status == 'expired' ) { |
|
| 704 | - |
|
| 705 | - $ret = true; |
|
| 706 | - |
|
| 707 | - } elseif( 'active' === $this->status || 'cancelled' === $this->status || $this->status == 'trialling' ) { |
|
| 708 | - |
|
| 709 | - $ret = false; |
|
| 710 | - $expiration = $this->get_expiration_time(); |
|
| 711 | - |
|
| 712 | - if( $expiration && strtotime( 'NOW', current_time( 'timestamp' ) ) > $expiration ) { |
|
| 713 | - $ret = true; |
|
| 714 | - |
|
| 715 | - if ( 'active' === $this->status || $this->status == 'trialling' ) { |
|
| 716 | - $this->expire(); |
|
| 717 | - } |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - return apply_filters( 'wpinv_subscription_is_expired', $ret, $this->id, $this ); |
|
| 723 | - |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - /** |
|
| 727 | - * Retrieves the expiration date |
|
| 728 | - * |
|
| 729 | - * @since 1.0.0 |
|
| 730 | - * @return string |
|
| 731 | - */ |
|
| 732 | - public function get_expiration() { |
|
| 733 | - return $this->expiration; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Retrieves the expiration date in a timestamp |
|
| 738 | - * |
|
| 739 | - * @since 1.0.0 |
|
| 740 | - * @return int |
|
| 741 | - */ |
|
| 742 | - public function get_expiration_time() { |
|
| 743 | - return strtotime( $this->expiration, current_time( 'timestamp' ) ); |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - /** |
|
| 747 | - * Retrieves the subscription status |
|
| 748 | - * |
|
| 749 | - * @since 1.0.0 |
|
| 750 | - * @return int |
|
| 751 | - */ |
|
| 752 | - public function get_status() { |
|
| 753 | - |
|
| 754 | - // Monitor for page load delays on pages with large subscription lists (IE: Subscriptions table in admin) |
|
| 755 | - $this->is_expired(); |
|
| 756 | - return $this->status; |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - /** |
|
| 760 | - * Retrieves the subscription status label |
|
| 761 | - * |
|
| 762 | - * @since 1.0.0 |
|
| 763 | - * @return int |
|
| 764 | - */ |
|
| 765 | - public function get_status_label() { |
|
| 766 | - |
|
| 767 | - switch( $this->get_status() ) { |
|
| 768 | - case 'active' : |
|
| 769 | - $status = __( 'Active', 'invoicing' ); |
|
| 770 | - break; |
|
| 771 | - |
|
| 772 | - case 'cancelled' : |
|
| 773 | - $status = __( 'Cancelled', 'invoicing' ); |
|
| 774 | - break; |
|
| 775 | - |
|
| 776 | - case 'expired' : |
|
| 777 | - $status = __( 'Expired', 'invoicing' ); |
|
| 778 | - break; |
|
| 779 | - |
|
| 780 | - case 'pending' : |
|
| 781 | - $status = __( 'Pending', 'invoicing' ); |
|
| 782 | - break; |
|
| 783 | - |
|
| 784 | - case 'failing' : |
|
| 785 | - $status = __( 'Failing', 'invoicing' ); |
|
| 786 | - break; |
|
| 787 | - |
|
| 788 | - case 'trialling' : |
|
| 789 | - $status = __( 'Trialling', 'invoicing' ); |
|
| 790 | - break; |
|
| 791 | - |
|
| 792 | - case 'completed' : |
|
| 793 | - $status = __( 'Completed', 'invoicing' ); |
|
| 794 | - break; |
|
| 795 | - |
|
| 796 | - default: |
|
| 797 | - $status = ucfirst( $this->get_status() ); |
|
| 798 | - break; |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - return $status; |
|
| 802 | - } |
|
| 636 | + return apply_filters( 'wpinv_subscription_can_renew', true, $this ); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Retrieves the URL to renew a subscription |
|
| 641 | + * |
|
| 642 | + * @since 2.5 |
|
| 643 | + * @return string |
|
| 644 | + */ |
|
| 645 | + public function get_renew_url() { |
|
| 646 | + |
|
| 647 | + $url = wp_nonce_url( add_query_arg( array( 'wpinv_action' => 'renew_subscription', 'sub_id' => $this->id ) ), 'wpinv-recurring-renew' ); |
|
| 648 | + |
|
| 649 | + return apply_filters( 'wpinv_subscription_renew_url', $url, $this ); |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * Determines if subscription can have their payment method updated |
|
| 654 | + * |
|
| 655 | + * @since 1.0.0 |
|
| 656 | + * @return bool |
|
| 657 | + */ |
|
| 658 | + public function can_update() { |
|
| 659 | + return apply_filters( 'wpinv_subscription_can_update', false, $this ); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * Retrieves the URL to update subscription |
|
| 664 | + * |
|
| 665 | + * @since 1.0.0 |
|
| 666 | + * @return void |
|
| 667 | + */ |
|
| 668 | + public function get_update_url() { |
|
| 669 | + |
|
| 670 | + $url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->id ) ); |
|
| 671 | + |
|
| 672 | + return apply_filters( 'wpinv_subscription_update_url', $url, $this ); |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + /** |
|
| 676 | + * Determines if subscription is active |
|
| 677 | + * |
|
| 678 | + * @since 1.0.0 |
|
| 679 | + * @return void |
|
| 680 | + */ |
|
| 681 | + public function is_active() { |
|
| 682 | + |
|
| 683 | + $ret = false; |
|
| 684 | + |
|
| 685 | + if( ! $this->is_expired() && ( $this->status == 'active' || $this->status == 'cancelled' || $this->status == 'trialling' ) ) { |
|
| 686 | + $ret = true; |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + return apply_filters( 'wpinv_subscription_is_active', $ret, $this->id, $this ); |
|
| 690 | + |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + /** |
|
| 694 | + * Determines if subscription is expired |
|
| 695 | + * |
|
| 696 | + * @since 1.0.0 |
|
| 697 | + * @return void |
|
| 698 | + */ |
|
| 699 | + public function is_expired() { |
|
| 700 | + |
|
| 701 | + $ret = false; |
|
| 702 | + |
|
| 703 | + if ( $this->status == 'expired' ) { |
|
| 704 | + |
|
| 705 | + $ret = true; |
|
| 706 | + |
|
| 707 | + } elseif( 'active' === $this->status || 'cancelled' === $this->status || $this->status == 'trialling' ) { |
|
| 708 | + |
|
| 709 | + $ret = false; |
|
| 710 | + $expiration = $this->get_expiration_time(); |
|
| 711 | + |
|
| 712 | + if( $expiration && strtotime( 'NOW', current_time( 'timestamp' ) ) > $expiration ) { |
|
| 713 | + $ret = true; |
|
| 714 | + |
|
| 715 | + if ( 'active' === $this->status || $this->status == 'trialling' ) { |
|
| 716 | + $this->expire(); |
|
| 717 | + } |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + return apply_filters( 'wpinv_subscription_is_expired', $ret, $this->id, $this ); |
|
| 723 | + |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + /** |
|
| 727 | + * Retrieves the expiration date |
|
| 728 | + * |
|
| 729 | + * @since 1.0.0 |
|
| 730 | + * @return string |
|
| 731 | + */ |
|
| 732 | + public function get_expiration() { |
|
| 733 | + return $this->expiration; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Retrieves the expiration date in a timestamp |
|
| 738 | + * |
|
| 739 | + * @since 1.0.0 |
|
| 740 | + * @return int |
|
| 741 | + */ |
|
| 742 | + public function get_expiration_time() { |
|
| 743 | + return strtotime( $this->expiration, current_time( 'timestamp' ) ); |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + /** |
|
| 747 | + * Retrieves the subscription status |
|
| 748 | + * |
|
| 749 | + * @since 1.0.0 |
|
| 750 | + * @return int |
|
| 751 | + */ |
|
| 752 | + public function get_status() { |
|
| 753 | + |
|
| 754 | + // Monitor for page load delays on pages with large subscription lists (IE: Subscriptions table in admin) |
|
| 755 | + $this->is_expired(); |
|
| 756 | + return $this->status; |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + /** |
|
| 760 | + * Retrieves the subscription status label |
|
| 761 | + * |
|
| 762 | + * @since 1.0.0 |
|
| 763 | + * @return int |
|
| 764 | + */ |
|
| 765 | + public function get_status_label() { |
|
| 766 | + |
|
| 767 | + switch( $this->get_status() ) { |
|
| 768 | + case 'active' : |
|
| 769 | + $status = __( 'Active', 'invoicing' ); |
|
| 770 | + break; |
|
| 771 | + |
|
| 772 | + case 'cancelled' : |
|
| 773 | + $status = __( 'Cancelled', 'invoicing' ); |
|
| 774 | + break; |
|
| 775 | + |
|
| 776 | + case 'expired' : |
|
| 777 | + $status = __( 'Expired', 'invoicing' ); |
|
| 778 | + break; |
|
| 779 | + |
|
| 780 | + case 'pending' : |
|
| 781 | + $status = __( 'Pending', 'invoicing' ); |
|
| 782 | + break; |
|
| 783 | + |
|
| 784 | + case 'failing' : |
|
| 785 | + $status = __( 'Failing', 'invoicing' ); |
|
| 786 | + break; |
|
| 787 | + |
|
| 788 | + case 'trialling' : |
|
| 789 | + $status = __( 'Trialling', 'invoicing' ); |
|
| 790 | + break; |
|
| 791 | + |
|
| 792 | + case 'completed' : |
|
| 793 | + $status = __( 'Completed', 'invoicing' ); |
|
| 794 | + break; |
|
| 795 | + |
|
| 796 | + default: |
|
| 797 | + $status = ucfirst( $this->get_status() ); |
|
| 798 | + break; |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + return $status; |
|
| 802 | + } |
|
| 803 | 803 | |
| 804 | 804 | /** |
| 805 | 805 | * Retrieves the subscription status label |