@@ -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 |
@@ -33,173 +33,173 @@ discard block |
||
| 33 | 33 | */ |
| 34 | 34 | class WPInv_Discount { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Discount ID. |
|
| 38 | - * |
|
| 39 | - * @since 1.0.15 |
|
| 40 | - * @var integer|null |
|
| 41 | - */ |
|
| 42 | - public $ID = null; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Old discount status. |
|
| 46 | - * |
|
| 47 | - * @since 1.0.15 |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - public $old_status = 'draft'; |
|
| 36 | + /** |
|
| 37 | + * Discount ID. |
|
| 38 | + * |
|
| 39 | + * @since 1.0.15 |
|
| 40 | + * @var integer|null |
|
| 41 | + */ |
|
| 42 | + public $ID = null; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Old discount status. |
|
| 46 | + * |
|
| 47 | + * @since 1.0.15 |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + public $old_status = 'draft'; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Data array. |
|
| 54 | - * |
|
| 55 | - * @since 1.0.15 |
|
| 56 | - * @var array |
|
| 57 | - */ |
|
| 58 | - protected $data = array(); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Discount constructor. |
|
| 62 | - * |
|
| 63 | - * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
|
| 64 | - * @since 1.0.15 |
|
| 65 | - */ |
|
| 66 | - public function __construct( $discount = array() ) { |
|
| 52 | + /** |
|
| 53 | + * Data array. |
|
| 54 | + * |
|
| 55 | + * @since 1.0.15 |
|
| 56 | + * @var array |
|
| 57 | + */ |
|
| 58 | + protected $data = array(); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Discount constructor. |
|
| 62 | + * |
|
| 63 | + * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
|
| 64 | + * @since 1.0.15 |
|
| 65 | + */ |
|
| 66 | + public function __construct( $discount = array() ) { |
|
| 67 | 67 | |
| 68 | 68 | // If the discount is an instance of this class... |
| 69 | - if ( $discount instanceof WPInv_Discount ) { |
|
| 70 | - $this->init( $discount->data ); |
|
| 71 | - return; |
|
| 69 | + if ( $discount instanceof WPInv_Discount ) { |
|
| 70 | + $this->init( $discount->data ); |
|
| 71 | + return; |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | // If the discount is an array of discount details... |
| 75 | 75 | if ( is_array( $discount ) ) { |
| 76 | - $this->init( $discount ); |
|
| 77 | - return; |
|
| 78 | - } |
|
| 76 | + $this->init( $discount ); |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - // Try fetching the discount by its post id. |
|
| 81 | - $data = false; |
|
| 80 | + // Try fetching the discount by its post id. |
|
| 81 | + $data = false; |
|
| 82 | 82 | |
| 83 | - if ( ! empty( $discount ) && is_numeric( $discount ) ) { |
|
| 84 | - $discount = absint( $discount ); |
|
| 85 | - $data = self::get_data_by( 'id', $discount ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - if ( is_array( $data ) ) { |
|
| 89 | - $this->init( $data ); |
|
| 90 | - return; |
|
| 91 | - } |
|
| 83 | + if ( ! empty( $discount ) && is_numeric( $discount ) ) { |
|
| 84 | + $discount = absint( $discount ); |
|
| 85 | + $data = self::get_data_by( 'id', $discount ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + if ( is_array( $data ) ) { |
|
| 89 | + $this->init( $data ); |
|
| 90 | + return; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - // Try fetching the discount by its discount code. |
|
| 94 | - if ( ! empty( $discount ) && is_string( $discount ) ) { |
|
| 95 | - $data = self::get_data_by( 'discount_code', $discount ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if ( is_array( $data ) ) { |
|
| 99 | - $this->init( $data ); |
|
| 100 | - return; |
|
| 101 | - } |
|
| 93 | + // Try fetching the discount by its discount code. |
|
| 94 | + if ( ! empty( $discount ) && is_string( $discount ) ) { |
|
| 95 | + $data = self::get_data_by( 'discount_code', $discount ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if ( is_array( $data ) ) { |
|
| 99 | + $this->init( $data ); |
|
| 100 | + return; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - // If we are here then the discount does not exist. |
|
| 104 | - $this->init( array() ); |
|
| 105 | - } |
|
| 103 | + // If we are here then the discount does not exist. |
|
| 104 | + $this->init( array() ); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Sets up object properties |
|
| 109 | - * |
|
| 110 | - * @since 1.0.15 |
|
| 111 | - * @param array $data An array containing the discount's data |
|
| 112 | - */ |
|
| 113 | - public function init( $data ) { |
|
| 114 | - $data = self::sanitize_discount_data( $data ); |
|
| 115 | - $this->data = $data; |
|
| 116 | - $this->old_status = $data['status']; |
|
| 117 | - $this->ID = $data['ID']; |
|
| 118 | - } |
|
| 107 | + /** |
|
| 108 | + * Sets up object properties |
|
| 109 | + * |
|
| 110 | + * @since 1.0.15 |
|
| 111 | + * @param array $data An array containing the discount's data |
|
| 112 | + */ |
|
| 113 | + public function init( $data ) { |
|
| 114 | + $data = self::sanitize_discount_data( $data ); |
|
| 115 | + $this->data = $data; |
|
| 116 | + $this->old_status = $data['status']; |
|
| 117 | + $this->ID = $data['ID']; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Fetch a discount from the db/cache |
|
| 122 | - * |
|
| 123 | - * |
|
| 124 | - * @static |
|
| 125 | - * @param string $field The field to query against: 'ID', 'discount_code' |
|
| 126 | - * @param string|int $value The field value |
|
| 127 | - * @since 1.0.15 |
|
| 128 | - * @return array|bool array of discount details on success. False otherwise. |
|
| 129 | - */ |
|
| 130 | - public static function get_data_by( $field, $value ) { |
|
| 131 | - |
|
| 132 | - if ( 'id' == strtolower( $field ) ) { |
|
| 133 | - // Make sure the value is numeric to avoid casting objects, for example, |
|
| 134 | - // to int 1. |
|
| 135 | - if ( ! is_numeric( $value ) ) |
|
| 136 | - return false; |
|
| 137 | - $value = intval( $value ); |
|
| 138 | - if ( $value < 1 ) |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - if ( ! $value || ! is_string( $field ) ) { |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 120 | + /** |
|
| 121 | + * Fetch a discount from the db/cache |
|
| 122 | + * |
|
| 123 | + * |
|
| 124 | + * @static |
|
| 125 | + * @param string $field The field to query against: 'ID', 'discount_code' |
|
| 126 | + * @param string|int $value The field value |
|
| 127 | + * @since 1.0.15 |
|
| 128 | + * @return array|bool array of discount details on success. False otherwise. |
|
| 129 | + */ |
|
| 130 | + public static function get_data_by( $field, $value ) { |
|
| 131 | + |
|
| 132 | + if ( 'id' == strtolower( $field ) ) { |
|
| 133 | + // Make sure the value is numeric to avoid casting objects, for example, |
|
| 134 | + // to int 1. |
|
| 135 | + if ( ! is_numeric( $value ) ) |
|
| 136 | + return false; |
|
| 137 | + $value = intval( $value ); |
|
| 138 | + if ( $value < 1 ) |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + if ( ! $value || ! is_string( $field ) ) { |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - $field = trim( $field ); |
|
| 147 | - |
|
| 148 | - // prepare query args |
|
| 149 | - switch ( strtolower( $field ) ) { |
|
| 150 | - case 'id': |
|
| 151 | - $discount_id = $value; |
|
| 152 | - $args = array( 'include' => array( $value ) ); |
|
| 153 | - break; |
|
| 154 | - case 'discount_code': |
|
| 155 | - case 'code': |
|
| 156 | - $value = trim( $value ); |
|
| 157 | - $discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' ); |
|
| 158 | - $args = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value ); |
|
| 159 | - break; |
|
| 160 | - case 'name': |
|
| 161 | - $discount_id = 0; |
|
| 162 | - $args = array( 'name' => trim( $value ) ); |
|
| 163 | - break; |
|
| 164 | - default: |
|
| 165 | - $args = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value ); |
|
| 166 | - if ( ! is_array( $args ) ) { |
|
| 167 | - return apply_filters( "wpinv_discount_get_data_by_$field", false, $value ); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - // Check if there is a cached value. |
|
| 173 | - if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) { |
|
| 174 | - return $discount; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $args = array_merge( |
|
| 178 | - $args, |
|
| 179 | - array( |
|
| 180 | - 'post_type' => 'wpi_discount', |
|
| 181 | - 'posts_per_page' => 1, |
|
| 182 | - 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ) |
|
| 183 | - ) |
|
| 184 | - ); |
|
| 185 | - |
|
| 186 | - $discount = get_posts( $args ); |
|
| 146 | + $field = trim( $field ); |
|
| 147 | + |
|
| 148 | + // prepare query args |
|
| 149 | + switch ( strtolower( $field ) ) { |
|
| 150 | + case 'id': |
|
| 151 | + $discount_id = $value; |
|
| 152 | + $args = array( 'include' => array( $value ) ); |
|
| 153 | + break; |
|
| 154 | + case 'discount_code': |
|
| 155 | + case 'code': |
|
| 156 | + $value = trim( $value ); |
|
| 157 | + $discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' ); |
|
| 158 | + $args = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value ); |
|
| 159 | + break; |
|
| 160 | + case 'name': |
|
| 161 | + $discount_id = 0; |
|
| 162 | + $args = array( 'name' => trim( $value ) ); |
|
| 163 | + break; |
|
| 164 | + default: |
|
| 165 | + $args = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value ); |
|
| 166 | + if ( ! is_array( $args ) ) { |
|
| 167 | + return apply_filters( "wpinv_discount_get_data_by_$field", false, $value ); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + // Check if there is a cached value. |
|
| 173 | + if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) { |
|
| 174 | + return $discount; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $args = array_merge( |
|
| 178 | + $args, |
|
| 179 | + array( |
|
| 180 | + 'post_type' => 'wpi_discount', |
|
| 181 | + 'posts_per_page' => 1, |
|
| 182 | + 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ) |
|
| 183 | + ) |
|
| 184 | + ); |
|
| 185 | + |
|
| 186 | + $discount = get_posts( $args ); |
|
| 187 | 187 | |
| 188 | - if( empty( $discount ) ) { |
|
| 189 | - return false; |
|
| 190 | - } |
|
| 188 | + if( empty( $discount ) ) { |
|
| 189 | + return false; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - $discount = $discount[0]; |
|
| 192 | + $discount = $discount[0]; |
|
| 193 | 193 | |
| 194 | - // Prepare the return data. |
|
| 195 | - $return = array( |
|
| 194 | + // Prepare the return data. |
|
| 195 | + $return = array( |
|
| 196 | 196 | 'ID' => $discount->ID, |
| 197 | 197 | 'code' => get_post_meta( $discount->ID, '_wpi_discount_code', true ), |
| 198 | 198 | 'amount' => get_post_meta( $discount->ID, '_wpi_discount_amount', true ), |
| 199 | 199 | 'date_created' => $discount->post_date, |
| 200 | - 'date_modified' => $discount->post_modified, |
|
| 201 | - 'status' => $discount->post_status, |
|
| 202 | - 'start' => get_post_meta( $discount->ID, '_wpi_discount_start', true ), |
|
| 200 | + 'date_modified' => $discount->post_modified, |
|
| 201 | + 'status' => $discount->post_status, |
|
| 202 | + 'start' => get_post_meta( $discount->ID, '_wpi_discount_start', true ), |
|
| 203 | 203 | 'expiration' => get_post_meta( $discount->ID, '_wpi_discount_expiration', true ), |
| 204 | 204 | 'type' => get_post_meta( $discount->ID, '_wpi_discount_type', true ), |
| 205 | 205 | 'description' => $discount->post_excerpt, |
@@ -213,38 +213,38 @@ discard block |
||
| 213 | 213 | 'max_total' => get_post_meta( $discount->ID, '_wpi_discount_max_total', true ), |
| 214 | 214 | ); |
| 215 | 215 | |
| 216 | - $return = self::sanitize_discount_data( $return ); |
|
| 217 | - $return = apply_filters( 'wpinv_discount_properties', $return ); |
|
| 216 | + $return = self::sanitize_discount_data( $return ); |
|
| 217 | + $return = apply_filters( 'wpinv_discount_properties', $return ); |
|
| 218 | 218 | |
| 219 | - // Update the cache with our data |
|
| 220 | - wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' ); |
|
| 221 | - wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' ); |
|
| 219 | + // Update the cache with our data |
|
| 220 | + wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' ); |
|
| 221 | + wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' ); |
|
| 222 | 222 | |
| 223 | - return $return; |
|
| 224 | - } |
|
| 223 | + return $return; |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * Sanitizes discount data |
|
| 228 | - * |
|
| 229 | - * @static |
|
| 230 | - * @since 1.0.15 |
|
| 231 | - * @access public |
|
| 232 | - * |
|
| 233 | - * @return array the sanitized data |
|
| 234 | - */ |
|
| 235 | - public static function sanitize_discount_data( $data ) { |
|
| 226 | + /** |
|
| 227 | + * Sanitizes discount data |
|
| 228 | + * |
|
| 229 | + * @static |
|
| 230 | + * @since 1.0.15 |
|
| 231 | + * @access public |
|
| 232 | + * |
|
| 233 | + * @return array the sanitized data |
|
| 234 | + */ |
|
| 235 | + public static function sanitize_discount_data( $data ) { |
|
| 236 | 236 | |
| 237 | - $allowed_discount_types = array_keys( wpinv_get_discount_types() ); |
|
| 237 | + $allowed_discount_types = array_keys( wpinv_get_discount_types() ); |
|
| 238 | 238 | |
| 239 | - $return = array( |
|
| 239 | + $return = array( |
|
| 240 | 240 | 'ID' => null, |
| 241 | 241 | 'code' => '', |
| 242 | 242 | 'amount' => 0, |
| 243 | 243 | 'date_created' => current_time('mysql'), |
| 244 | 244 | 'date_modified' => current_time('mysql'), |
| 245 | - 'expiration' => null, |
|
| 246 | - 'start' => current_time('mysql'), |
|
| 247 | - 'status' => 'draft', |
|
| 245 | + 'expiration' => null, |
|
| 246 | + 'start' => current_time('mysql'), |
|
| 247 | + 'status' => 'draft', |
|
| 248 | 248 | 'type' => 'percent', |
| 249 | 249 | 'description' => '', |
| 250 | 250 | 'uses' => 0, |
@@ -254,426 +254,426 @@ discard block |
||
| 254 | 254 | 'max_uses' => 0, |
| 255 | 255 | 'is_recurring' => false, |
| 256 | 256 | 'min_total' => '', |
| 257 | - 'max_total' => '', |
|
| 258 | - ); |
|
| 257 | + 'max_total' => '', |
|
| 258 | + ); |
|
| 259 | 259 | |
| 260 | 260 | |
| 261 | - // Arrays only please. |
|
| 262 | - if ( ! is_array( $data ) ) { |
|
| 261 | + // Arrays only please. |
|
| 262 | + if ( ! is_array( $data ) ) { |
|
| 263 | 263 | return $return; |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | - // If an id is provided, ensure it is a valid discount. |
|
| 266 | + // If an id is provided, ensure it is a valid discount. |
|
| 267 | 267 | if ( ! empty( $data['ID'] ) && ( ! is_numeric( $data['ID'] ) || 'wpi_discount' !== get_post_type( $data['ID'] ) ) ) { |
| 268 | 268 | return $return; |
| 269 | - } |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | 271 | $return = array_merge( $return, $data ); |
| 272 | 272 | |
| 273 | 273 | // Sanitize some keys. |
| 274 | 274 | $return['amount'] = wpinv_sanitize_amount( $return['amount'] ); |
| 275 | - $return['is_single_use'] = (bool) $return['is_single_use']; |
|
| 276 | - $return['is_recurring'] = (bool) $return['is_recurring']; |
|
| 277 | - $return['uses'] = (int) $return['uses']; |
|
| 278 | - $return['max_uses'] = (int) $return['max_uses']; |
|
| 279 | - $return['min_total'] = wpinv_sanitize_amount( $return['min_total'] ); |
|
| 275 | + $return['is_single_use'] = (bool) $return['is_single_use']; |
|
| 276 | + $return['is_recurring'] = (bool) $return['is_recurring']; |
|
| 277 | + $return['uses'] = (int) $return['uses']; |
|
| 278 | + $return['max_uses'] = (int) $return['max_uses']; |
|
| 279 | + $return['min_total'] = wpinv_sanitize_amount( $return['min_total'] ); |
|
| 280 | 280 | $return['max_total'] = wpinv_sanitize_amount( $return['max_total'] ); |
| 281 | 281 | |
| 282 | - // Trim all values. |
|
| 283 | - $return = wpinv_clean( $return ); |
|
| 282 | + // Trim all values. |
|
| 283 | + $return = wpinv_clean( $return ); |
|
| 284 | 284 | |
| 285 | - // Ensure the discount type is supported. |
|
| 285 | + // Ensure the discount type is supported. |
|
| 286 | 286 | if ( ! in_array( $return['type'], $allowed_discount_types, true ) ) { |
| 287 | 287 | $return['type'] = 'percent'; |
| 288 | - } |
|
| 289 | - $return['type_name'] = wpinv_get_discount_type_name( $return['type'] ); |
|
| 288 | + } |
|
| 289 | + $return['type_name'] = wpinv_get_discount_type_name( $return['type'] ); |
|
| 290 | 290 | |
| 291 | - // Do not offer more than a 100% discount. |
|
| 292 | - if ( $return['type'] == 'percent' && (float) $return['amount'] > 100 ) { |
|
| 293 | - $return['amount'] = 100; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - // Format dates. |
|
| 297 | - foreach( wpinv_parse_list( 'date_created date_modified expiration start') as $prop ) { |
|
| 298 | - if( ! empty( $return[$prop] ) ) { |
|
| 299 | - $return[$prop] = date_i18n( 'Y-m-d H:i:s', strtotime( $return[$prop] ) ); |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - // Formart items. |
|
| 304 | - foreach( array( 'excluded_items', 'items' ) as $prop ) { |
|
| 305 | - |
|
| 306 | - if( ! empty( $return[$prop] ) ) { |
|
| 307 | - // Ensure that the property is an array of non-empty integers. |
|
| 308 | - $return[$prop] = array_filter( array_map( 'intval', wpinv_parse_list( $return[$prop] ) ) ); |
|
| 309 | - } else { |
|
| 310 | - $return[$prop] = array(); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - } |
|
| 291 | + // Do not offer more than a 100% discount. |
|
| 292 | + if ( $return['type'] == 'percent' && (float) $return['amount'] > 100 ) { |
|
| 293 | + $return['amount'] = 100; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + // Format dates. |
|
| 297 | + foreach( wpinv_parse_list( 'date_created date_modified expiration start') as $prop ) { |
|
| 298 | + if( ! empty( $return[$prop] ) ) { |
|
| 299 | + $return[$prop] = date_i18n( 'Y-m-d H:i:s', strtotime( $return[$prop] ) ); |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + // Formart items. |
|
| 304 | + foreach( array( 'excluded_items', 'items' ) as $prop ) { |
|
| 305 | + |
|
| 306 | + if( ! empty( $return[$prop] ) ) { |
|
| 307 | + // Ensure that the property is an array of non-empty integers. |
|
| 308 | + $return[$prop] = array_filter( array_map( 'intval', wpinv_parse_list( $return[$prop] ) ) ); |
|
| 309 | + } else { |
|
| 310 | + $return[$prop] = array(); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | - return apply_filters( 'wpinv_sanitize_discount_data', $return, $data ); |
|
| 316 | - } |
|
| 315 | + return apply_filters( 'wpinv_sanitize_discount_data', $return, $data ); |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | - /** |
|
| 319 | - * Magic method for checking the existence of a certain custom field. |
|
| 320 | - * |
|
| 321 | - * @since 1.0.15 |
|
| 322 | - * @access public |
|
| 323 | - * |
|
| 324 | - * @return bool Whether the given discount field is set. |
|
| 325 | - */ |
|
| 326 | - public function __isset( $key ){ |
|
| 327 | - return isset( $this->data[$key] ) || method_exists( $this, "get_$key"); |
|
| 328 | - } |
|
| 318 | + /** |
|
| 319 | + * Magic method for checking the existence of a certain custom field. |
|
| 320 | + * |
|
| 321 | + * @since 1.0.15 |
|
| 322 | + * @access public |
|
| 323 | + * |
|
| 324 | + * @return bool Whether the given discount field is set. |
|
| 325 | + */ |
|
| 326 | + public function __isset( $key ){ |
|
| 327 | + return isset( $this->data[$key] ) || method_exists( $this, "get_$key"); |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - /** |
|
| 331 | - * Magic method for accessing discount properties. |
|
| 332 | - * |
|
| 333 | - * @since 1.0.15 |
|
| 334 | - * @access public |
|
| 335 | - * |
|
| 336 | - * @param string $key Discount data to retrieve |
|
| 337 | - * @return mixed Value of the given discount property (if set). |
|
| 338 | - */ |
|
| 339 | - public function __get( $key ) { |
|
| 340 | - return $this->get( $key ); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Magic method for accessing discount properties. |
|
| 345 | - * |
|
| 346 | - * @since 1.0.15 |
|
| 347 | - * @access public |
|
| 348 | - * |
|
| 349 | - * @param string $key Discount data to retrieve |
|
| 350 | - * @return mixed Value of the given discount property (if set). |
|
| 351 | - */ |
|
| 352 | - public function get( $key ) { |
|
| 330 | + /** |
|
| 331 | + * Magic method for accessing discount properties. |
|
| 332 | + * |
|
| 333 | + * @since 1.0.15 |
|
| 334 | + * @access public |
|
| 335 | + * |
|
| 336 | + * @param string $key Discount data to retrieve |
|
| 337 | + * @return mixed Value of the given discount property (if set). |
|
| 338 | + */ |
|
| 339 | + public function __get( $key ) { |
|
| 340 | + return $this->get( $key ); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Magic method for accessing discount properties. |
|
| 345 | + * |
|
| 346 | + * @since 1.0.15 |
|
| 347 | + * @access public |
|
| 348 | + * |
|
| 349 | + * @param string $key Discount data to retrieve |
|
| 350 | + * @return mixed Value of the given discount property (if set). |
|
| 351 | + */ |
|
| 352 | + public function get( $key ) { |
|
| 353 | 353 | |
| 354 | - if ( $key == 'id' ) { |
|
| 355 | - $key = 'ID'; |
|
| 356 | - } |
|
| 354 | + if ( $key == 'id' ) { |
|
| 355 | + $key = 'ID'; |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - if( method_exists( $this, "get_$key") ) { |
|
| 359 | - $value = call_user_func( array( $this, "get_$key" ) ); |
|
| 360 | - } else if( isset( $this->data[$key] ) ) { |
|
| 361 | - $value = $this->data[$key]; |
|
| 362 | - } else { |
|
| 363 | - $value = null; |
|
| 364 | - } |
|
| 358 | + if( method_exists( $this, "get_$key") ) { |
|
| 359 | + $value = call_user_func( array( $this, "get_$key" ) ); |
|
| 360 | + } else if( isset( $this->data[$key] ) ) { |
|
| 361 | + $value = $this->data[$key]; |
|
| 362 | + } else { |
|
| 363 | + $value = null; |
|
| 364 | + } |
|
| 365 | 365 | |
| 366 | - /** |
|
| 367 | - * Filters a discount's property value. |
|
| 368 | - * |
|
| 369 | - * The dynamic part ($key) can be any property name e.g items, code, type etc. |
|
| 370 | - * |
|
| 371 | - * @param mixed $value The property's value. |
|
| 372 | - * @param int $ID The discount's ID. |
|
| 373 | - * @param WPInv_Discount $discount The discount object. |
|
| 374 | - * @param string $code The discount's discount code. |
|
| 375 | - * @param array $data The discount's data array. |
|
| 376 | - */ |
|
| 377 | - return apply_filters( "wpinv_get_discount_{$key}", $value, $this->ID, $this, $this->data['code'], $this->data ); |
|
| 378 | - |
|
| 379 | - } |
|
| 366 | + /** |
|
| 367 | + * Filters a discount's property value. |
|
| 368 | + * |
|
| 369 | + * The dynamic part ($key) can be any property name e.g items, code, type etc. |
|
| 370 | + * |
|
| 371 | + * @param mixed $value The property's value. |
|
| 372 | + * @param int $ID The discount's ID. |
|
| 373 | + * @param WPInv_Discount $discount The discount object. |
|
| 374 | + * @param string $code The discount's discount code. |
|
| 375 | + * @param array $data The discount's data array. |
|
| 376 | + */ |
|
| 377 | + return apply_filters( "wpinv_get_discount_{$key}", $value, $this->ID, $this, $this->data['code'], $this->data ); |
|
| 378 | + |
|
| 379 | + } |
|
| 380 | 380 | |
| 381 | - /** |
|
| 382 | - * Magic method for setting discount fields. |
|
| 383 | - * |
|
| 384 | - * This method does not update custom fields in the database. |
|
| 385 | - * |
|
| 386 | - * @since 1.0.15 |
|
| 387 | - * @access public |
|
| 388 | - * |
|
| 389 | - */ |
|
| 390 | - public function __set( $key, $value ) { |
|
| 381 | + /** |
|
| 382 | + * Magic method for setting discount fields. |
|
| 383 | + * |
|
| 384 | + * This method does not update custom fields in the database. |
|
| 385 | + * |
|
| 386 | + * @since 1.0.15 |
|
| 387 | + * @access public |
|
| 388 | + * |
|
| 389 | + */ |
|
| 390 | + public function __set( $key, $value ) { |
|
| 391 | 391 | |
| 392 | - if ( 'id' == strtolower( $key ) ) { |
|
| 392 | + if ( 'id' == strtolower( $key ) ) { |
|
| 393 | 393 | |
| 394 | - $this->ID = $value; |
|
| 395 | - $this->data['ID'] = $value; |
|
| 396 | - return; |
|
| 394 | + $this->ID = $value; |
|
| 395 | + $this->data['ID'] = $value; |
|
| 396 | + return; |
|
| 397 | 397 | |
| 398 | - } |
|
| 398 | + } |
|
| 399 | 399 | |
| 400 | - /** |
|
| 401 | - * Filters a discount's property value before it is saved. |
|
| 402 | - * |
|
| 403 | - * |
|
| 404 | - * |
|
| 405 | - * The dynamic part ($key) can be any property name e.g items, code, type etc. |
|
| 406 | - * |
|
| 407 | - * @param mixed $value The property's value. |
|
| 408 | - * @param int $ID The discount's ID. |
|
| 409 | - * @param WPInv_Discount $discount The discount object. |
|
| 410 | - * @param string $code The discount's discount code. |
|
| 411 | - * @param array $data The discount's data array. |
|
| 412 | - */ |
|
| 413 | - $value = apply_filters( "wpinv_set_discount_{$key}", $value, $this->ID, $this, $this->code, $this->data ); |
|
| 414 | - |
|
| 415 | - if( method_exists( $this, "set_$key") ) { |
|
| 416 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
| 417 | - } else { |
|
| 418 | - $this->data[$key] = $value; |
|
| 419 | - } |
|
| 400 | + /** |
|
| 401 | + * Filters a discount's property value before it is saved. |
|
| 402 | + * |
|
| 403 | + * |
|
| 404 | + * |
|
| 405 | + * The dynamic part ($key) can be any property name e.g items, code, type etc. |
|
| 406 | + * |
|
| 407 | + * @param mixed $value The property's value. |
|
| 408 | + * @param int $ID The discount's ID. |
|
| 409 | + * @param WPInv_Discount $discount The discount object. |
|
| 410 | + * @param string $code The discount's discount code. |
|
| 411 | + * @param array $data The discount's data array. |
|
| 412 | + */ |
|
| 413 | + $value = apply_filters( "wpinv_set_discount_{$key}", $value, $this->ID, $this, $this->code, $this->data ); |
|
| 414 | + |
|
| 415 | + if( method_exists( $this, "set_$key") ) { |
|
| 416 | + call_user_func( array( $this, "set_$key" ), $value ); |
|
| 417 | + } else { |
|
| 418 | + $this->data[$key] = $value; |
|
| 419 | + } |
|
| 420 | 420 | |
| 421 | - } |
|
| 421 | + } |
|
| 422 | 422 | |
| 423 | - /** |
|
| 424 | - * Saves (or updates) a discount to the database |
|
| 425 | - * |
|
| 426 | - * @since 1.0.15 |
|
| 427 | - * @access public |
|
| 428 | - * @return bool |
|
| 429 | - * |
|
| 430 | - */ |
|
| 431 | - public function save(){ |
|
| 423 | + /** |
|
| 424 | + * Saves (or updates) a discount to the database |
|
| 425 | + * |
|
| 426 | + * @since 1.0.15 |
|
| 427 | + * @access public |
|
| 428 | + * @return bool |
|
| 429 | + * |
|
| 430 | + */ |
|
| 431 | + public function save(){ |
|
| 432 | 432 | |
| 433 | - $data = self::sanitize_discount_data( $this->data ); |
|
| 433 | + $data = self::sanitize_discount_data( $this->data ); |
|
| 434 | 434 | |
| 435 | - // Should we create a new post? |
|
| 436 | - if( ! $data[ 'ID' ] ) { |
|
| 435 | + // Should we create a new post? |
|
| 436 | + if( ! $data[ 'ID' ] ) { |
|
| 437 | 437 | |
| 438 | - $id = wp_insert_post( array( |
|
| 439 | - 'post_status' => $data['status'], |
|
| 440 | - 'post_type' => 'wpi_discount', |
|
| 441 | - 'post_excerpt' => $data['description'], |
|
| 442 | - ) ); |
|
| 438 | + $id = wp_insert_post( array( |
|
| 439 | + 'post_status' => $data['status'], |
|
| 440 | + 'post_type' => 'wpi_discount', |
|
| 441 | + 'post_excerpt' => $data['description'], |
|
| 442 | + ) ); |
|
| 443 | 443 | |
| 444 | - if( empty( $id ) ) { |
|
| 445 | - return false; |
|
| 446 | - } |
|
| 444 | + if( empty( $id ) ) { |
|
| 445 | + return false; |
|
| 446 | + } |
|
| 447 | 447 | |
| 448 | - $data[ 'ID' ] = (int) $id; |
|
| 449 | - $this->ID = $data[ 'ID' ]; |
|
| 450 | - $this->data['ID'] = $data[ 'ID' ]; |
|
| 448 | + $data[ 'ID' ] = (int) $id; |
|
| 449 | + $this->ID = $data[ 'ID' ]; |
|
| 450 | + $this->data['ID'] = $data[ 'ID' ]; |
|
| 451 | 451 | |
| 452 | - } else { |
|
| 453 | - $this->update_status( $data['status'] ); |
|
| 454 | - } |
|
| 452 | + } else { |
|
| 453 | + $this->update_status( $data['status'] ); |
|
| 454 | + } |
|
| 455 | 455 | |
| 456 | - $meta = apply_filters( 'wpinv_update_discount', $data, $this->ID, $this ); |
|
| 456 | + $meta = apply_filters( 'wpinv_update_discount', $data, $this->ID, $this ); |
|
| 457 | 457 | |
| 458 | - do_action( 'wpinv_pre_update_discount', $meta, $this->ID, $this ); |
|
| 458 | + do_action( 'wpinv_pre_update_discount', $meta, $this->ID, $this ); |
|
| 459 | 459 | |
| 460 | - foreach( wpinv_parse_list( 'ID date_created date_modified status description type_name' ) as $prop ) { |
|
| 461 | - if ( isset( $meta[$prop] ) ) { |
|
| 462 | - unset( $meta[$prop] ); |
|
| 463 | - } |
|
| 464 | - } |
|
| 460 | + foreach( wpinv_parse_list( 'ID date_created date_modified status description type_name' ) as $prop ) { |
|
| 461 | + if ( isset( $meta[$prop] ) ) { |
|
| 462 | + unset( $meta[$prop] ); |
|
| 463 | + } |
|
| 464 | + } |
|
| 465 | 465 | |
| 466 | - if( isset( $meta['uses'] ) && empty( $meta['uses'] ) ) { |
|
| 467 | - unset( $meta['uses'] ); |
|
| 468 | - } |
|
| 466 | + if( isset( $meta['uses'] ) && empty( $meta['uses'] ) ) { |
|
| 467 | + unset( $meta['uses'] ); |
|
| 468 | + } |
|
| 469 | 469 | |
| 470 | - // Save the metadata. |
|
| 471 | - foreach( $meta as $key => $value ) { |
|
| 472 | - update_post_meta( $this->ID, "_wpi_discount_$key", $value ); |
|
| 473 | - } |
|
| 470 | + // Save the metadata. |
|
| 471 | + foreach( $meta as $key => $value ) { |
|
| 472 | + update_post_meta( $this->ID, "_wpi_discount_$key", $value ); |
|
| 473 | + } |
|
| 474 | 474 | |
| 475 | - $this->refresh(); |
|
| 475 | + $this->refresh(); |
|
| 476 | 476 | |
| 477 | - do_action( 'wpinv_post_update_discount', $meta, $this->ID ); |
|
| 477 | + do_action( 'wpinv_post_update_discount', $meta, $this->ID ); |
|
| 478 | 478 | |
| 479 | - return true; |
|
| 480 | - } |
|
| 479 | + return true; |
|
| 480 | + } |
|
| 481 | 481 | |
| 482 | - /** |
|
| 483 | - * Refreshes the discount data. |
|
| 484 | - * |
|
| 485 | - * @since 1.0.15 |
|
| 486 | - * @access public |
|
| 487 | - * @return bool |
|
| 488 | - * |
|
| 489 | - */ |
|
| 490 | - public function refresh(){ |
|
| 482 | + /** |
|
| 483 | + * Refreshes the discount data. |
|
| 484 | + * |
|
| 485 | + * @since 1.0.15 |
|
| 486 | + * @access public |
|
| 487 | + * @return bool |
|
| 488 | + * |
|
| 489 | + */ |
|
| 490 | + public function refresh(){ |
|
| 491 | 491 | |
| 492 | - // Empty the cache for this discount. |
|
| 493 | - wp_cache_delete( $this->ID, 'WPInv_Discounts' ); |
|
| 494 | - wp_cache_delete( $this->get( 'code' ), 'WPInv_Discount_Codes' ); |
|
| 492 | + // Empty the cache for this discount. |
|
| 493 | + wp_cache_delete( $this->ID, 'WPInv_Discounts' ); |
|
| 494 | + wp_cache_delete( $this->get( 'code' ), 'WPInv_Discount_Codes' ); |
|
| 495 | 495 | |
| 496 | - $data = self::get_data_by( 'id', $this->ID ); |
|
| 497 | - if( is_array( $data ) ) { |
|
| 498 | - $this->init( $data ); |
|
| 499 | - } else { |
|
| 500 | - $this->init( array() ); |
|
| 501 | - } |
|
| 496 | + $data = self::get_data_by( 'id', $this->ID ); |
|
| 497 | + if( is_array( $data ) ) { |
|
| 498 | + $this->init( $data ); |
|
| 499 | + } else { |
|
| 500 | + $this->init( array() ); |
|
| 501 | + } |
|
| 502 | 502 | |
| 503 | - } |
|
| 503 | + } |
|
| 504 | 504 | |
| 505 | - /** |
|
| 506 | - * Saves (or updates) a discount to the database |
|
| 507 | - * |
|
| 508 | - * @since 1.0.15 |
|
| 509 | - * @access public |
|
| 510 | - * @return bool |
|
| 511 | - * |
|
| 512 | - */ |
|
| 513 | - public function update_status( $status = 'publish' ){ |
|
| 505 | + /** |
|
| 506 | + * Saves (or updates) a discount to the database |
|
| 507 | + * |
|
| 508 | + * @since 1.0.15 |
|
| 509 | + * @access public |
|
| 510 | + * @return bool |
|
| 511 | + * |
|
| 512 | + */ |
|
| 513 | + public function update_status( $status = 'publish' ){ |
|
| 514 | 514 | |
| 515 | 515 | |
| 516 | - if ( $this->exists() && $this->old_status != $status ) { |
|
| 516 | + if ( $this->exists() && $this->old_status != $status ) { |
|
| 517 | 517 | |
| 518 | - do_action( 'wpinv_pre_update_discount_status', $this->ID, $this->old_status, $status ); |
|
| 519 | - $updated = wp_update_post( array( 'ID' => $this->ID, 'post_status' => $status ) ); |
|
| 520 | - do_action( 'wpinv_post_update_discount_status', $this->ID, $this->old_status, $status ); |
|
| 518 | + do_action( 'wpinv_pre_update_discount_status', $this->ID, $this->old_status, $status ); |
|
| 519 | + $updated = wp_update_post( array( 'ID' => $this->ID, 'post_status' => $status ) ); |
|
| 520 | + do_action( 'wpinv_post_update_discount_status', $this->ID, $this->old_status, $status ); |
|
| 521 | 521 | |
| 522 | - $this->refresh(); |
|
| 522 | + $this->refresh(); |
|
| 523 | 523 | |
| 524 | - return $updated !== 0; |
|
| 524 | + return $updated !== 0; |
|
| 525 | 525 | |
| 526 | - } |
|
| 526 | + } |
|
| 527 | 527 | |
| 528 | - return false; |
|
| 529 | - } |
|
| 528 | + return false; |
|
| 529 | + } |
|
| 530 | 530 | |
| 531 | 531 | |
| 532 | - /** |
|
| 533 | - * Checks whether a discount exists in the database or not |
|
| 534 | - * |
|
| 535 | - * @since 1.0.15 |
|
| 536 | - */ |
|
| 537 | - public function exists(){ |
|
| 538 | - return ! empty( $this->ID ); |
|
| 539 | - } |
|
| 532 | + /** |
|
| 533 | + * Checks whether a discount exists in the database or not |
|
| 534 | + * |
|
| 535 | + * @since 1.0.15 |
|
| 536 | + */ |
|
| 537 | + public function exists(){ |
|
| 538 | + return ! empty( $this->ID ); |
|
| 539 | + } |
|
| 540 | 540 | |
| 541 | - // Boolean methods |
|
| 541 | + // Boolean methods |
|
| 542 | 542 | |
| 543 | - /** |
|
| 544 | - * Checks the discount type. |
|
| 545 | - * |
|
| 546 | - * |
|
| 547 | - * @param string $type the discount type to check against |
|
| 548 | - * @since 1.0.15 |
|
| 549 | - * @return bool |
|
| 550 | - */ |
|
| 551 | - public function is_type( $type ) { |
|
| 552 | - return $this->type == $type; |
|
| 553 | - } |
|
| 543 | + /** |
|
| 544 | + * Checks the discount type. |
|
| 545 | + * |
|
| 546 | + * |
|
| 547 | + * @param string $type the discount type to check against |
|
| 548 | + * @since 1.0.15 |
|
| 549 | + * @return bool |
|
| 550 | + */ |
|
| 551 | + public function is_type( $type ) { |
|
| 552 | + return $this->type == $type; |
|
| 553 | + } |
|
| 554 | 554 | |
| 555 | - /** |
|
| 556 | - * Checks whether the discount is published or not |
|
| 557 | - * |
|
| 558 | - * @since 1.0.15 |
|
| 559 | - * @return bool |
|
| 560 | - */ |
|
| 561 | - public function is_active() { |
|
| 562 | - return $this->status == 'publish'; |
|
| 563 | - } |
|
| 555 | + /** |
|
| 556 | + * Checks whether the discount is published or not |
|
| 557 | + * |
|
| 558 | + * @since 1.0.15 |
|
| 559 | + * @return bool |
|
| 560 | + */ |
|
| 561 | + public function is_active() { |
|
| 562 | + return $this->status == 'publish'; |
|
| 563 | + } |
|
| 564 | 564 | |
| 565 | - /** |
|
| 566 | - * Checks whether the discount is has exided the usage limit or not |
|
| 567 | - * |
|
| 568 | - * @since 1.0.15 |
|
| 569 | - * @return bool |
|
| 570 | - */ |
|
| 571 | - public function has_exceeded_limit() { |
|
| 572 | - if( empty( $this->max_uses ) || empty( $this->uses ) ) { |
|
| 573 | - return false ; |
|
| 574 | - } |
|
| 565 | + /** |
|
| 566 | + * Checks whether the discount is has exided the usage limit or not |
|
| 567 | + * |
|
| 568 | + * @since 1.0.15 |
|
| 569 | + * @return bool |
|
| 570 | + */ |
|
| 571 | + public function has_exceeded_limit() { |
|
| 572 | + if( empty( $this->max_uses ) || empty( $this->uses ) ) { |
|
| 573 | + return false ; |
|
| 574 | + } |
|
| 575 | 575 | |
| 576 | - $exceeded = $this->uses >= $this->max_uses; |
|
| 577 | - return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->ID, $this, $this->code ); |
|
| 578 | - } |
|
| 576 | + $exceeded = $this->uses >= $this->max_uses; |
|
| 577 | + return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->ID, $this, $this->code ); |
|
| 578 | + } |
|
| 579 | 579 | |
| 580 | - /** |
|
| 581 | - * Checks if the discount is expired |
|
| 582 | - * |
|
| 583 | - * @since 1.0.15 |
|
| 584 | - * @return bool |
|
| 585 | - */ |
|
| 586 | - public function is_expired() { |
|
| 587 | - $expired = empty ( $this->expiration ) ? false : current_time( 'timestamp' ) > strtotime( $this->expiration ); |
|
| 588 | - return apply_filters( 'wpinv_is_discount_expired', $expired, $this->ID, $this, $this->code ); |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - /** |
|
| 592 | - * Checks the discount start date. |
|
| 593 | - * |
|
| 594 | - * @since 1.0.15 |
|
| 595 | - * @return bool |
|
| 596 | - */ |
|
| 597 | - public function has_started() { |
|
| 598 | - $started = empty ( $this->start ) ? true : current_time( 'timestamp' ) > strtotime( $this->start ); |
|
| 599 | - return apply_filters( 'wpinv_is_discount_started', $started, $this->ID, $this, $this->code ); |
|
| 600 | - } |
|
| 580 | + /** |
|
| 581 | + * Checks if the discount is expired |
|
| 582 | + * |
|
| 583 | + * @since 1.0.15 |
|
| 584 | + * @return bool |
|
| 585 | + */ |
|
| 586 | + public function is_expired() { |
|
| 587 | + $expired = empty ( $this->expiration ) ? false : current_time( 'timestamp' ) > strtotime( $this->expiration ); |
|
| 588 | + return apply_filters( 'wpinv_is_discount_expired', $expired, $this->ID, $this, $this->code ); |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + /** |
|
| 592 | + * Checks the discount start date. |
|
| 593 | + * |
|
| 594 | + * @since 1.0.15 |
|
| 595 | + * @return bool |
|
| 596 | + */ |
|
| 597 | + public function has_started() { |
|
| 598 | + $started = empty ( $this->start ) ? true : current_time( 'timestamp' ) > strtotime( $this->start ); |
|
| 599 | + return apply_filters( 'wpinv_is_discount_started', $started, $this->ID, $this, $this->code ); |
|
| 600 | + } |
|
| 601 | 601 | |
| 602 | - /** |
|
| 603 | - * Check if a discount is valid for a given item id. |
|
| 604 | - * |
|
| 605 | - * @param int|int[] $item_ids |
|
| 606 | - * @since 1.0.15 |
|
| 607 | - * @return boolean |
|
| 608 | - */ |
|
| 609 | - public function is_valid_for_items( $item_ids ) { |
|
| 602 | + /** |
|
| 603 | + * Check if a discount is valid for a given item id. |
|
| 604 | + * |
|
| 605 | + * @param int|int[] $item_ids |
|
| 606 | + * @since 1.0.15 |
|
| 607 | + * @return boolean |
|
| 608 | + */ |
|
| 609 | + public function is_valid_for_items( $item_ids ) { |
|
| 610 | 610 | |
| 611 | - $item_ids = array_map( 'intval', wpinv_parse_list( $item_ids ) ); |
|
| 612 | - $included = array_intersect( $item_ids, $this->items ); |
|
| 613 | - $excluded = array_intersect( $item_ids, $this->excluded_items ); |
|
| 614 | - |
|
| 615 | - if( ! empty( $this->excluded_items ) && ! empty( $excluded ) ) { |
|
| 616 | - return false; |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - if( ! empty( $this->items ) && empty( $included ) ) { |
|
| 620 | - return false; |
|
| 621 | - } |
|
| 622 | - return true; |
|
| 623 | - } |
|
| 611 | + $item_ids = array_map( 'intval', wpinv_parse_list( $item_ids ) ); |
|
| 612 | + $included = array_intersect( $item_ids, $this->items ); |
|
| 613 | + $excluded = array_intersect( $item_ids, $this->excluded_items ); |
|
| 614 | + |
|
| 615 | + if( ! empty( $this->excluded_items ) && ! empty( $excluded ) ) { |
|
| 616 | + return false; |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + if( ! empty( $this->items ) && empty( $included ) ) { |
|
| 620 | + return false; |
|
| 621 | + } |
|
| 622 | + return true; |
|
| 623 | + } |
|
| 624 | 624 | |
| 625 | - /** |
|
| 626 | - * Check if a discount is valid for the given amount |
|
| 627 | - * |
|
| 628 | - * @param float $amount The amount to check against |
|
| 629 | - * @since 1.0.15 |
|
| 630 | - * @return boolean |
|
| 631 | - */ |
|
| 632 | - public function is_valid_for_amount( $amount ) { |
|
| 633 | - return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * Checks if the minimum amount is met |
|
| 638 | - * |
|
| 639 | - * @param float $amount The amount to check against |
|
| 640 | - * @since 1.0.15 |
|
| 641 | - * @return boolean |
|
| 642 | - */ |
|
| 643 | - public function is_minimum_amount_met( $amount ) { |
|
| 644 | - $amount = floatval( $amount ); |
|
| 645 | - $min_met= ! ( $this->min_total > 0 && $amount < $this->min_total ); |
|
| 646 | - return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->ID, $this, $this->code, $amount ); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * Checks if the maximum amount is met |
|
| 651 | - * |
|
| 652 | - * @param float $amount The amount to check against |
|
| 653 | - * @since 1.0.15 |
|
| 654 | - * @return boolean |
|
| 655 | - */ |
|
| 656 | - public function is_maximum_amount_met( $amount ) { |
|
| 657 | - $amount = floatval( $amount ); |
|
| 658 | - $max_met= ! ( $this->max_total > 0 && $amount > $this->max_total ); |
|
| 659 | - return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->ID, $this, $this->code, $amount ); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * Check if a discount is valid for the given user |
|
| 664 | - * |
|
| 665 | - * @param int|string $user |
|
| 666 | - * @since 1.0.15 |
|
| 667 | - * @return boolean |
|
| 668 | - */ |
|
| 669 | - public function is_valid_for_user( $user ) { |
|
| 670 | - global $wpi_checkout_id; |
|
| 671 | - |
|
| 672 | - if( empty( $user ) || empty( $this->is_single_use ) ) { |
|
| 673 | - return true; |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - $user_id = 0; |
|
| 625 | + /** |
|
| 626 | + * Check if a discount is valid for the given amount |
|
| 627 | + * |
|
| 628 | + * @param float $amount The amount to check against |
|
| 629 | + * @since 1.0.15 |
|
| 630 | + * @return boolean |
|
| 631 | + */ |
|
| 632 | + public function is_valid_for_amount( $amount ) { |
|
| 633 | + return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * Checks if the minimum amount is met |
|
| 638 | + * |
|
| 639 | + * @param float $amount The amount to check against |
|
| 640 | + * @since 1.0.15 |
|
| 641 | + * @return boolean |
|
| 642 | + */ |
|
| 643 | + public function is_minimum_amount_met( $amount ) { |
|
| 644 | + $amount = floatval( $amount ); |
|
| 645 | + $min_met= ! ( $this->min_total > 0 && $amount < $this->min_total ); |
|
| 646 | + return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->ID, $this, $this->code, $amount ); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * Checks if the maximum amount is met |
|
| 651 | + * |
|
| 652 | + * @param float $amount The amount to check against |
|
| 653 | + * @since 1.0.15 |
|
| 654 | + * @return boolean |
|
| 655 | + */ |
|
| 656 | + public function is_maximum_amount_met( $amount ) { |
|
| 657 | + $amount = floatval( $amount ); |
|
| 658 | + $max_met= ! ( $this->max_total > 0 && $amount > $this->max_total ); |
|
| 659 | + return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->ID, $this, $this->code, $amount ); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * Check if a discount is valid for the given user |
|
| 664 | + * |
|
| 665 | + * @param int|string $user |
|
| 666 | + * @since 1.0.15 |
|
| 667 | + * @return boolean |
|
| 668 | + */ |
|
| 669 | + public function is_valid_for_user( $user ) { |
|
| 670 | + global $wpi_checkout_id; |
|
| 671 | + |
|
| 672 | + if( empty( $user ) || empty( $this->is_single_use ) ) { |
|
| 673 | + return true; |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + $user_id = 0; |
|
| 677 | 677 | if ( is_int( $user ) ) { |
| 678 | 678 | $user_id = absint( $user ); |
| 679 | 679 | } else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) { |
@@ -682,164 +682,164 @@ discard block |
||
| 682 | 682 | $user_id = $user_data->ID; |
| 683 | 683 | } else if ( absint( $user ) > 0 ) { |
| 684 | 684 | $user_id = absint( $user ); |
| 685 | - } |
|
| 685 | + } |
|
| 686 | 686 | |
| 687 | - if ( empty( $user_id ) ) { |
|
| 688 | - return true; |
|
| 689 | - } |
|
| 687 | + if ( empty( $user_id ) ) { |
|
| 688 | + return true; |
|
| 689 | + } |
|
| 690 | 690 | |
| 691 | - // Get all payments with matching user id |
|
| 691 | + // Get all payments with matching user id |
|
| 692 | 692 | $payments = wpinv_get_invoices( array( 'user' => $user_id, 'limit' => false ) ); |
| 693 | - $code = strtolower( $this->code ); |
|
| 693 | + $code = strtolower( $this->code ); |
|
| 694 | 694 | |
| 695 | - foreach ( $payments as $payment ) { |
|
| 695 | + foreach ( $payments as $payment ) { |
|
| 696 | 696 | |
| 697 | - // Don't count discount used for current invoice checkout. |
|
| 698 | - if ( ! empty( $wpi_checkout_id ) && $wpi_checkout_id == $payment->ID ) { |
|
| 699 | - continue; |
|
| 700 | - } |
|
| 697 | + // Don't count discount used for current invoice checkout. |
|
| 698 | + if ( ! empty( $wpi_checkout_id ) && $wpi_checkout_id == $payment->ID ) { |
|
| 699 | + continue; |
|
| 700 | + } |
|
| 701 | 701 | |
| 702 | - if ( $payment->has_status( array( 'wpi-cancelled', 'wpi-failed' ) ) ) { |
|
| 703 | - continue; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - $discounts = $payment->get_discounts( true ); |
|
| 707 | - if ( empty( $discounts ) ) { |
|
| 708 | - continue; |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - $discounts = array_map( 'strtolower', wpinv_parse_list( $discounts ) ); |
|
| 712 | - if ( ! empty( $discounts ) && in_array( $code, $discounts ) ) { |
|
| 713 | - return false; |
|
| 714 | - } |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - return true; |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - /** |
|
| 721 | - * Deletes the discount from the database |
|
| 722 | - * |
|
| 723 | - * @since 1.0.15 |
|
| 724 | - * @return boolean |
|
| 725 | - */ |
|
| 726 | - public function remove() { |
|
| 727 | - |
|
| 728 | - if ( empty( $this->ID ) ) { |
|
| 729 | - return true; |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - do_action( 'wpinv_pre_delete_discount', $this->ID, $this->data ); |
|
| 733 | - wp_cache_delete( $this->ID, 'WPInv_Discounts' ); |
|
| 734 | - wp_delete_post( $this->ID, true ); |
|
| 735 | - wp_cache_delete( $this->code, 'WPInv_Discount_Codes' ); |
|
| 736 | - do_action( 'wpinv_post_delete_discount', $this->ID, $this->data ); |
|
| 737 | - |
|
| 738 | - $this->ID = null; |
|
| 739 | - $this->data['id'] = null; |
|
| 740 | - return true; |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - /** |
|
| 744 | - * Increases a discount's usage. |
|
| 745 | - * |
|
| 746 | - * @since 1.0.15 |
|
| 747 | - * @param int $by The number of usages to increas by. |
|
| 748 | - * @return int |
|
| 749 | - */ |
|
| 750 | - public function increase_usage( $by = 1 ) { |
|
| 751 | - |
|
| 752 | - $this->uses = $this->uses + $by; |
|
| 753 | - |
|
| 754 | - if( $this->uses < 0 ) { |
|
| 755 | - $this->uses = 0; |
|
| 756 | - update_post_meta( $this->ID, "_wpi_discount_uses", 0 ); |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - $this->save(); |
|
| 760 | - |
|
| 761 | - if( $by > 0 ) { |
|
| 762 | - do_action( 'wpinv_discount_increase_use_count', $this->uses, $this->ID, $this->code, $by ); |
|
| 763 | - } else { |
|
| 764 | - do_action( 'wpinv_discount_decrease_use_count', $this->uses, $this->ID, $this->code, absint( $by ) ); |
|
| 765 | - } |
|
| 702 | + if ( $payment->has_status( array( 'wpi-cancelled', 'wpi-failed' ) ) ) { |
|
| 703 | + continue; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + $discounts = $payment->get_discounts( true ); |
|
| 707 | + if ( empty( $discounts ) ) { |
|
| 708 | + continue; |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + $discounts = array_map( 'strtolower', wpinv_parse_list( $discounts ) ); |
|
| 712 | + if ( ! empty( $discounts ) && in_array( $code, $discounts ) ) { |
|
| 713 | + return false; |
|
| 714 | + } |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + return true; |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + /** |
|
| 721 | + * Deletes the discount from the database |
|
| 722 | + * |
|
| 723 | + * @since 1.0.15 |
|
| 724 | + * @return boolean |
|
| 725 | + */ |
|
| 726 | + public function remove() { |
|
| 727 | + |
|
| 728 | + if ( empty( $this->ID ) ) { |
|
| 729 | + return true; |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + do_action( 'wpinv_pre_delete_discount', $this->ID, $this->data ); |
|
| 733 | + wp_cache_delete( $this->ID, 'WPInv_Discounts' ); |
|
| 734 | + wp_delete_post( $this->ID, true ); |
|
| 735 | + wp_cache_delete( $this->code, 'WPInv_Discount_Codes' ); |
|
| 736 | + do_action( 'wpinv_post_delete_discount', $this->ID, $this->data ); |
|
| 737 | + |
|
| 738 | + $this->ID = null; |
|
| 739 | + $this->data['id'] = null; |
|
| 740 | + return true; |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + /** |
|
| 744 | + * Increases a discount's usage. |
|
| 745 | + * |
|
| 746 | + * @since 1.0.15 |
|
| 747 | + * @param int $by The number of usages to increas by. |
|
| 748 | + * @return int |
|
| 749 | + */ |
|
| 750 | + public function increase_usage( $by = 1 ) { |
|
| 751 | + |
|
| 752 | + $this->uses = $this->uses + $by; |
|
| 753 | + |
|
| 754 | + if( $this->uses < 0 ) { |
|
| 755 | + $this->uses = 0; |
|
| 756 | + update_post_meta( $this->ID, "_wpi_discount_uses", 0 ); |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + $this->save(); |
|
| 760 | + |
|
| 761 | + if( $by > 0 ) { |
|
| 762 | + do_action( 'wpinv_discount_increase_use_count', $this->uses, $this->ID, $this->code, $by ); |
|
| 763 | + } else { |
|
| 764 | + do_action( 'wpinv_discount_decrease_use_count', $this->uses, $this->ID, $this->code, absint( $by ) ); |
|
| 765 | + } |
|
| 766 | 766 | |
| 767 | - return $this->uses; |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - /** |
|
| 771 | - * Retrieves discount data |
|
| 772 | - * |
|
| 773 | - * @since 1.0.15 |
|
| 774 | - * @return array |
|
| 775 | - */ |
|
| 776 | - public function get_data() { |
|
| 777 | - $return = array(); |
|
| 778 | - foreach( array_keys( $this->data ) as $key ) { |
|
| 779 | - $return[ $key ] = $this->get( $key ); |
|
| 780 | - } |
|
| 781 | - return $return; |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - /** |
|
| 785 | - * Retrieves discount data as json |
|
| 786 | - * |
|
| 787 | - * @since 1.0.15 |
|
| 788 | - * @return string|false |
|
| 789 | - */ |
|
| 790 | - public function get_data_as_json() { |
|
| 791 | - return wp_json_encode( $this->get_data() ); |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - /** |
|
| 795 | - * Checks if a discount can only be used once per user. |
|
| 796 | - * |
|
| 797 | - * @since 1.0.15 |
|
| 798 | - * @return bool |
|
| 799 | - */ |
|
| 800 | - public function get_is_single_use() { |
|
| 801 | - return (bool) apply_filters( 'wpinv_is_discount_single_use', $this->data['is_single_use'], $this->ID, $this, $this->code ); |
|
| 802 | - } |
|
| 803 | - |
|
| 804 | - /** |
|
| 805 | - * Checks if a discount is recurring. |
|
| 806 | - * |
|
| 807 | - * @since 1.0.15 |
|
| 808 | - * @return bool |
|
| 809 | - */ |
|
| 810 | - public function get_is_recurring() { |
|
| 811 | - return (bool) apply_filters( 'wpinv_is_discount_recurring', $this->data['is_recurring'], $this->ID, $this->code, $this ); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - /** |
|
| 815 | - * Returns a discount's included items. |
|
| 816 | - * |
|
| 817 | - * @since 1.0.15 |
|
| 818 | - * @return array |
|
| 819 | - */ |
|
| 820 | - public function get_items() { |
|
| 821 | - return wpinv_parse_list( apply_filters( 'wpinv_get_discount_item_reqs', $this->data['items'], $this->ID, $this, $this->code ) ); |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - /** |
|
| 825 | - * Returns a discount's discounted amount. |
|
| 826 | - * |
|
| 827 | - * @since 1.0.15 |
|
| 828 | - * @return float |
|
| 829 | - */ |
|
| 830 | - public function get_discounted_amount( $amount ) { |
|
| 831 | - |
|
| 832 | - if ( $this->type == 'flat' ) { |
|
| 767 | + return $this->uses; |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + /** |
|
| 771 | + * Retrieves discount data |
|
| 772 | + * |
|
| 773 | + * @since 1.0.15 |
|
| 774 | + * @return array |
|
| 775 | + */ |
|
| 776 | + public function get_data() { |
|
| 777 | + $return = array(); |
|
| 778 | + foreach( array_keys( $this->data ) as $key ) { |
|
| 779 | + $return[ $key ] = $this->get( $key ); |
|
| 780 | + } |
|
| 781 | + return $return; |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + /** |
|
| 785 | + * Retrieves discount data as json |
|
| 786 | + * |
|
| 787 | + * @since 1.0.15 |
|
| 788 | + * @return string|false |
|
| 789 | + */ |
|
| 790 | + public function get_data_as_json() { |
|
| 791 | + return wp_json_encode( $this->get_data() ); |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + /** |
|
| 795 | + * Checks if a discount can only be used once per user. |
|
| 796 | + * |
|
| 797 | + * @since 1.0.15 |
|
| 798 | + * @return bool |
|
| 799 | + */ |
|
| 800 | + public function get_is_single_use() { |
|
| 801 | + return (bool) apply_filters( 'wpinv_is_discount_single_use', $this->data['is_single_use'], $this->ID, $this, $this->code ); |
|
| 802 | + } |
|
| 803 | + |
|
| 804 | + /** |
|
| 805 | + * Checks if a discount is recurring. |
|
| 806 | + * |
|
| 807 | + * @since 1.0.15 |
|
| 808 | + * @return bool |
|
| 809 | + */ |
|
| 810 | + public function get_is_recurring() { |
|
| 811 | + return (bool) apply_filters( 'wpinv_is_discount_recurring', $this->data['is_recurring'], $this->ID, $this->code, $this ); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + /** |
|
| 815 | + * Returns a discount's included items. |
|
| 816 | + * |
|
| 817 | + * @since 1.0.15 |
|
| 818 | + * @return array |
|
| 819 | + */ |
|
| 820 | + public function get_items() { |
|
| 821 | + return wpinv_parse_list( apply_filters( 'wpinv_get_discount_item_reqs', $this->data['items'], $this->ID, $this, $this->code ) ); |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + /** |
|
| 825 | + * Returns a discount's discounted amount. |
|
| 826 | + * |
|
| 827 | + * @since 1.0.15 |
|
| 828 | + * @return float |
|
| 829 | + */ |
|
| 830 | + public function get_discounted_amount( $amount ) { |
|
| 831 | + |
|
| 832 | + if ( $this->type == 'flat' ) { |
|
| 833 | 833 | $amount = $amount - $this->amount; |
| 834 | - } else { |
|
| 834 | + } else { |
|
| 835 | 835 | $amount = $amount - ( $amount * ( $this->amount / 100 ) ); |
| 836 | - } |
|
| 836 | + } |
|
| 837 | 837 | |
| 838 | - if ( $amount < 0 ) { |
|
| 839 | - $amount = 0; |
|
| 840 | - } |
|
| 838 | + if ( $amount < 0 ) { |
|
| 839 | + $amount = 0; |
|
| 840 | + } |
|
| 841 | 841 | |
| 842 | - return apply_filters( 'wpinv_discounted_amount', $amount, $this->ID, $this, $this->code, $this->amount ); |
|
| 843 | - } |
|
| 842 | + return apply_filters( 'wpinv_discounted_amount', $amount, $this->ID, $this, $this->code, $this->amount ); |
|
| 843 | + } |
|
| 844 | 844 | |
| 845 | 845 | } |
@@ -700,7 +700,7 @@ discard block |
||
| 700 | 700 | } |
| 701 | 701 | |
| 702 | 702 | function wpinv_get_php_arg_separator_output() { |
| 703 | - return ini_get( 'arg_separator.output' ); |
|
| 703 | + return ini_get( 'arg_separator.output' ); |
|
| 704 | 704 | } |
| 705 | 705 | |
| 706 | 706 | function wpinv_rgb_from_hex( $color ) { |
@@ -1038,11 +1038,11 @@ discard block |
||
| 1038 | 1038 | * @return array Sanitized array of values. |
| 1039 | 1039 | */ |
| 1040 | 1040 | function wpinv_parse_list( $list ) { |
| 1041 | - if ( ! is_array( $list ) ) { |
|
| 1042 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 1043 | - } |
|
| 1041 | + if ( ! is_array( $list ) ) { |
|
| 1042 | + return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 1043 | + } |
|
| 1044 | 1044 | |
| 1045 | - return $list; |
|
| 1045 | + return $list; |
|
| 1046 | 1046 | } |
| 1047 | 1047 | |
| 1048 | 1048 | /** |
@@ -1062,9 +1062,9 @@ discard block |
||
| 1062 | 1062 | } |
| 1063 | 1063 | |
| 1064 | 1064 | $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" ); |
| 1065 | - wp_cache_set( "wpinv-$key", $data, 'wpinv' ); |
|
| 1065 | + wp_cache_set( "wpinv-$key", $data, 'wpinv' ); |
|
| 1066 | 1066 | |
| 1067 | - return $data; |
|
| 1067 | + return $data; |
|
| 1068 | 1068 | } |
| 1069 | 1069 | |
| 1070 | 1070 | /** |
@@ -1093,17 +1093,17 @@ discard block |
||
| 1093 | 1093 | */ |
| 1094 | 1094 | function wpinv_clean( $var ) { |
| 1095 | 1095 | |
| 1096 | - if ( is_array( $var ) ) { |
|
| 1097 | - return array_map( 'wpinv_clean', $var ); |
|
| 1096 | + if ( is_array( $var ) ) { |
|
| 1097 | + return array_map( 'wpinv_clean', $var ); |
|
| 1098 | 1098 | } |
| 1099 | 1099 | |
| 1100 | 1100 | if ( is_object( $var ) ) { |
| 1101 | - $object_vars = get_object_vars( $var ); |
|
| 1102 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
| 1103 | - $var->$property_name = wpinv_clean( $property_value ); |
|
| 1101 | + $object_vars = get_object_vars( $var ); |
|
| 1102 | + foreach ( $object_vars as $property_name => $property_value ) { |
|
| 1103 | + $var->$property_name = wpinv_clean( $property_value ); |
|
| 1104 | 1104 | } |
| 1105 | 1105 | return $var; |
| 1106 | - } |
|
| 1106 | + } |
|
| 1107 | 1107 | |
| 1108 | 1108 | return is_string( $var ) ? sanitize_text_field( $var ) : $var; |
| 1109 | 1109 | } |
| 1110 | 1110 | \ No newline at end of file |
@@ -1,271 +1,271 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | - exit; |
|
| 4 | + exit; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | if ( ! class_exists( "AyeCode_Connect_Helper" ) ) { |
| 8 | - /** |
|
| 9 | - * Allow the quick setup and connection of our AyeCode Connect plugin. |
|
| 10 | - * |
|
| 11 | - * Class AyeCode_Connect_Helper |
|
| 12 | - */ |
|
| 13 | - class AyeCode_Connect_Helper { |
|
| 14 | - |
|
| 15 | - // Hold the version number |
|
| 16 | - var $version = "1.0.3"; |
|
| 17 | - |
|
| 18 | - // Hold the default strings. |
|
| 19 | - var $strings = array(); |
|
| 20 | - |
|
| 21 | - // Hold the default pages. |
|
| 22 | - var $pages = array(); |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * The constructor. |
|
| 26 | - * |
|
| 27 | - * AyeCode_Connect_Helper constructor. |
|
| 28 | - * |
|
| 29 | - * @param array $strings |
|
| 30 | - * @param array $pages |
|
| 31 | - */ |
|
| 32 | - public function __construct( $strings = array(), $pages = array() ) { |
|
| 33 | - |
|
| 34 | - // Only fire if not localhost and the current user has the right permissions. |
|
| 35 | - if ( ! $this->is_localhost() && current_user_can( 'manage_options' ) ) { |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - // set default strings |
|
| 39 | - $default_strings = array( |
|
| 40 | - 'connect_title' => __( "Thanks for choosing an AyeCode Product!" ), |
|
| 41 | - 'connect_external' => __( "Please confirm you wish to connect your site?" ), |
|
| 42 | - 'connect' => sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s" ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>" ), |
|
| 43 | - 'connect_button' => __( "Connect Site" ), |
|
| 44 | - 'connecting_button' => __( "Connecting..." ), |
|
| 45 | - 'error_localhost' => __( "This service will only work with a live domain, not a localhost." ), |
|
| 46 | - 'error' => __( "Something went wrong, please refresh and try again." ), |
|
| 47 | - ); |
|
| 48 | - $this->strings = array_merge( $default_strings, $strings ); |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - // set default pages |
|
| 52 | - $default_pages = array(); |
|
| 53 | - $this->pages = array_merge( $default_pages, $pages ); |
|
| 54 | - |
|
| 55 | - // maybe show connect site notice |
|
| 56 | - add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) ); |
|
| 57 | - |
|
| 58 | - // add ajax action if not already added |
|
| 59 | - if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) { |
|
| 60 | - add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) ); |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // add ajax action if not already added |
|
| 65 | - if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) { |
|
| 66 | - add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Give a way to check we can connect via a external redirect. |
|
| 73 | - */ |
|
| 74 | - public function ayecode_connect_helper_installed(){ |
|
| 75 | - $active = array( |
|
| 76 | - 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0, |
|
| 77 | - 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0, |
|
| 78 | - 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0, |
|
| 79 | - ); |
|
| 80 | - wp_send_json_success( $active ); |
|
| 81 | - wp_die(); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Get slug from path |
|
| 86 | - * |
|
| 87 | - * @param string $key |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - private function format_plugin_slug( $key ) { |
|
| 92 | - $slug = explode( '/', $key ); |
|
| 93 | - $slug = explode( '.', end( $slug ) ); |
|
| 94 | - |
|
| 95 | - return $slug[0]; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Install and activate the AyeCode Connect Plugin |
|
| 100 | - */ |
|
| 101 | - public function ayecode_connect_install() { |
|
| 102 | - |
|
| 103 | - // bail if localhost |
|
| 104 | - if ( $this->is_localhost() ) { |
|
| 105 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Explicitly clear the event. |
|
| 109 | - wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() ); |
|
| 110 | - |
|
| 111 | - $success = true; |
|
| 112 | - $plugin_slug = "ayecode-connect"; |
|
| 113 | - if ( ! empty( $plugin_slug ) ) { |
|
| 114 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 115 | - require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
| 116 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
| 117 | - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
|
| 118 | - |
|
| 119 | - WP_Filesystem(); |
|
| 120 | - |
|
| 121 | - $skin = new Automatic_Upgrader_Skin; |
|
| 122 | - $upgrader = new WP_Upgrader( $skin ); |
|
| 123 | - $installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) ); |
|
| 124 | - $plugin_slug = $plugin_slug; |
|
| 125 | - $plugin = $plugin_slug . '/' . $plugin_slug . '.php'; |
|
| 126 | - $installed = false; |
|
| 127 | - $activate = false; |
|
| 128 | - |
|
| 129 | - // See if the plugin is installed already |
|
| 130 | - if ( in_array( $plugin_slug, $installed_plugins ) ) { |
|
| 131 | - $installed = true; |
|
| 132 | - $activate = ! is_plugin_active( $plugin ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // Install this thing! |
|
| 136 | - if ( ! $installed ) { |
|
| 137 | - |
|
| 138 | - // Suppress feedback |
|
| 139 | - ob_start(); |
|
| 140 | - |
|
| 141 | - try { |
|
| 142 | - $plugin_information = plugins_api( 'plugin_information', array( |
|
| 143 | - 'slug' => $plugin_slug, |
|
| 144 | - 'fields' => array( |
|
| 145 | - 'short_description' => false, |
|
| 146 | - 'sections' => false, |
|
| 147 | - 'requires' => false, |
|
| 148 | - 'rating' => false, |
|
| 149 | - 'ratings' => false, |
|
| 150 | - 'downloaded' => false, |
|
| 151 | - 'last_updated' => false, |
|
| 152 | - 'added' => false, |
|
| 153 | - 'tags' => false, |
|
| 154 | - 'homepage' => false, |
|
| 155 | - 'donate_link' => false, |
|
| 156 | - 'author_profile' => false, |
|
| 157 | - 'author' => false, |
|
| 158 | - ), |
|
| 159 | - ) ); |
|
| 160 | - |
|
| 161 | - if ( is_wp_error( $plugin_information ) ) { |
|
| 162 | - throw new Exception( $plugin_information->get_error_message() ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $package = $plugin_information->download_link; |
|
| 166 | - $download = $upgrader->download_package( $package ); |
|
| 167 | - |
|
| 168 | - if ( is_wp_error( $download ) ) { |
|
| 169 | - throw new Exception( $download->get_error_message() ); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $working_dir = $upgrader->unpack_package( $download, true ); |
|
| 173 | - |
|
| 174 | - if ( is_wp_error( $working_dir ) ) { |
|
| 175 | - throw new Exception( $working_dir->get_error_message() ); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - $result = $upgrader->install_package( array( |
|
| 179 | - 'source' => $working_dir, |
|
| 180 | - 'destination' => WP_PLUGIN_DIR, |
|
| 181 | - 'clear_destination' => false, |
|
| 182 | - 'abort_if_destination_exists' => false, |
|
| 183 | - 'clear_working' => true, |
|
| 184 | - 'hook_extra' => array( |
|
| 185 | - 'type' => 'plugin', |
|
| 186 | - 'action' => 'install', |
|
| 187 | - ), |
|
| 188 | - ) ); |
|
| 189 | - |
|
| 190 | - if ( is_wp_error( $result ) ) { |
|
| 191 | - throw new Exception( $result->get_error_message() ); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - $activate = true; |
|
| 195 | - |
|
| 196 | - } catch ( Exception $e ) { |
|
| 197 | - $success = false; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // Discard feedback |
|
| 201 | - ob_end_clean(); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - wp_clean_plugins_cache(); |
|
| 205 | - |
|
| 206 | - // Activate this thing |
|
| 207 | - if ( $activate ) { |
|
| 208 | - try { |
|
| 209 | - $result = activate_plugin( $plugin ); |
|
| 210 | - |
|
| 211 | - if ( is_wp_error( $result ) ) { |
|
| 212 | - $success = false; |
|
| 213 | - } else { |
|
| 214 | - $success = true; |
|
| 215 | - } |
|
| 216 | - } catch ( Exception $e ) { |
|
| 217 | - $success = false; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - if ( $success && function_exists( 'ayecode_connect_args' ) ) { |
|
| 223 | - ayecode_connect();// init |
|
| 224 | - $args = ayecode_connect_args(); |
|
| 225 | - $client = new AyeCode_Connect( $args ); |
|
| 226 | - $redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : ''; |
|
| 227 | - $redirect = $client->build_connect_url( $redirect_to ); |
|
| 228 | - wp_send_json_success( array( 'connect_url' => $redirect ) ); |
|
| 229 | - } else { |
|
| 230 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
| 231 | - } |
|
| 232 | - wp_die(); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Check if maybe localhost. |
|
| 237 | - * |
|
| 238 | - * @return bool |
|
| 239 | - */ |
|
| 240 | - public function is_localhost() { |
|
| 241 | - $localhost = false; |
|
| 242 | - |
|
| 243 | - $host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : ''; |
|
| 244 | - $localhost_domains = array( |
|
| 245 | - 'localhost', |
|
| 246 | - 'localhost.localdomain', |
|
| 247 | - '127.0.0.1', |
|
| 248 | - '::1' |
|
| 249 | - ); |
|
| 250 | - |
|
| 251 | - if ( in_array( $host, $localhost_domains ) ) { |
|
| 252 | - $localhost = true; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return $localhost; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Show notice to connect site. |
|
| 260 | - */ |
|
| 261 | - public function ayecode_connect_install_notice() { |
|
| 262 | - if ( $this->maybe_show() ) { |
|
| 263 | - $connect_title_string = $this->strings['connect_title']; |
|
| 264 | - $connect_external_string = $this->strings['connect_external']; |
|
| 265 | - $connect_string = $this->strings['connect']; |
|
| 266 | - $connect_button_string = $this->strings['connect_button']; |
|
| 267 | - $connecting_button_string = $this->strings['connecting_button']; |
|
| 268 | - ?> |
|
| 8 | + /** |
|
| 9 | + * Allow the quick setup and connection of our AyeCode Connect plugin. |
|
| 10 | + * |
|
| 11 | + * Class AyeCode_Connect_Helper |
|
| 12 | + */ |
|
| 13 | + class AyeCode_Connect_Helper { |
|
| 14 | + |
|
| 15 | + // Hold the version number |
|
| 16 | + var $version = "1.0.3"; |
|
| 17 | + |
|
| 18 | + // Hold the default strings. |
|
| 19 | + var $strings = array(); |
|
| 20 | + |
|
| 21 | + // Hold the default pages. |
|
| 22 | + var $pages = array(); |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * The constructor. |
|
| 26 | + * |
|
| 27 | + * AyeCode_Connect_Helper constructor. |
|
| 28 | + * |
|
| 29 | + * @param array $strings |
|
| 30 | + * @param array $pages |
|
| 31 | + */ |
|
| 32 | + public function __construct( $strings = array(), $pages = array() ) { |
|
| 33 | + |
|
| 34 | + // Only fire if not localhost and the current user has the right permissions. |
|
| 35 | + if ( ! $this->is_localhost() && current_user_can( 'manage_options' ) ) { |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + // set default strings |
|
| 39 | + $default_strings = array( |
|
| 40 | + 'connect_title' => __( "Thanks for choosing an AyeCode Product!" ), |
|
| 41 | + 'connect_external' => __( "Please confirm you wish to connect your site?" ), |
|
| 42 | + 'connect' => sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s" ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>" ), |
|
| 43 | + 'connect_button' => __( "Connect Site" ), |
|
| 44 | + 'connecting_button' => __( "Connecting..." ), |
|
| 45 | + 'error_localhost' => __( "This service will only work with a live domain, not a localhost." ), |
|
| 46 | + 'error' => __( "Something went wrong, please refresh and try again." ), |
|
| 47 | + ); |
|
| 48 | + $this->strings = array_merge( $default_strings, $strings ); |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + // set default pages |
|
| 52 | + $default_pages = array(); |
|
| 53 | + $this->pages = array_merge( $default_pages, $pages ); |
|
| 54 | + |
|
| 55 | + // maybe show connect site notice |
|
| 56 | + add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) ); |
|
| 57 | + |
|
| 58 | + // add ajax action if not already added |
|
| 59 | + if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) { |
|
| 60 | + add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) ); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // add ajax action if not already added |
|
| 65 | + if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) { |
|
| 66 | + add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Give a way to check we can connect via a external redirect. |
|
| 73 | + */ |
|
| 74 | + public function ayecode_connect_helper_installed(){ |
|
| 75 | + $active = array( |
|
| 76 | + 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0, |
|
| 77 | + 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0, |
|
| 78 | + 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0, |
|
| 79 | + ); |
|
| 80 | + wp_send_json_success( $active ); |
|
| 81 | + wp_die(); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Get slug from path |
|
| 86 | + * |
|
| 87 | + * @param string $key |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + private function format_plugin_slug( $key ) { |
|
| 92 | + $slug = explode( '/', $key ); |
|
| 93 | + $slug = explode( '.', end( $slug ) ); |
|
| 94 | + |
|
| 95 | + return $slug[0]; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Install and activate the AyeCode Connect Plugin |
|
| 100 | + */ |
|
| 101 | + public function ayecode_connect_install() { |
|
| 102 | + |
|
| 103 | + // bail if localhost |
|
| 104 | + if ( $this->is_localhost() ) { |
|
| 105 | + wp_send_json_error( $this->strings['error_localhost'] ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Explicitly clear the event. |
|
| 109 | + wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() ); |
|
| 110 | + |
|
| 111 | + $success = true; |
|
| 112 | + $plugin_slug = "ayecode-connect"; |
|
| 113 | + if ( ! empty( $plugin_slug ) ) { |
|
| 114 | + require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 115 | + require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
| 116 | + require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
| 117 | + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
|
| 118 | + |
|
| 119 | + WP_Filesystem(); |
|
| 120 | + |
|
| 121 | + $skin = new Automatic_Upgrader_Skin; |
|
| 122 | + $upgrader = new WP_Upgrader( $skin ); |
|
| 123 | + $installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) ); |
|
| 124 | + $plugin_slug = $plugin_slug; |
|
| 125 | + $plugin = $plugin_slug . '/' . $plugin_slug . '.php'; |
|
| 126 | + $installed = false; |
|
| 127 | + $activate = false; |
|
| 128 | + |
|
| 129 | + // See if the plugin is installed already |
|
| 130 | + if ( in_array( $plugin_slug, $installed_plugins ) ) { |
|
| 131 | + $installed = true; |
|
| 132 | + $activate = ! is_plugin_active( $plugin ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // Install this thing! |
|
| 136 | + if ( ! $installed ) { |
|
| 137 | + |
|
| 138 | + // Suppress feedback |
|
| 139 | + ob_start(); |
|
| 140 | + |
|
| 141 | + try { |
|
| 142 | + $plugin_information = plugins_api( 'plugin_information', array( |
|
| 143 | + 'slug' => $plugin_slug, |
|
| 144 | + 'fields' => array( |
|
| 145 | + 'short_description' => false, |
|
| 146 | + 'sections' => false, |
|
| 147 | + 'requires' => false, |
|
| 148 | + 'rating' => false, |
|
| 149 | + 'ratings' => false, |
|
| 150 | + 'downloaded' => false, |
|
| 151 | + 'last_updated' => false, |
|
| 152 | + 'added' => false, |
|
| 153 | + 'tags' => false, |
|
| 154 | + 'homepage' => false, |
|
| 155 | + 'donate_link' => false, |
|
| 156 | + 'author_profile' => false, |
|
| 157 | + 'author' => false, |
|
| 158 | + ), |
|
| 159 | + ) ); |
|
| 160 | + |
|
| 161 | + if ( is_wp_error( $plugin_information ) ) { |
|
| 162 | + throw new Exception( $plugin_information->get_error_message() ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $package = $plugin_information->download_link; |
|
| 166 | + $download = $upgrader->download_package( $package ); |
|
| 167 | + |
|
| 168 | + if ( is_wp_error( $download ) ) { |
|
| 169 | + throw new Exception( $download->get_error_message() ); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $working_dir = $upgrader->unpack_package( $download, true ); |
|
| 173 | + |
|
| 174 | + if ( is_wp_error( $working_dir ) ) { |
|
| 175 | + throw new Exception( $working_dir->get_error_message() ); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + $result = $upgrader->install_package( array( |
|
| 179 | + 'source' => $working_dir, |
|
| 180 | + 'destination' => WP_PLUGIN_DIR, |
|
| 181 | + 'clear_destination' => false, |
|
| 182 | + 'abort_if_destination_exists' => false, |
|
| 183 | + 'clear_working' => true, |
|
| 184 | + 'hook_extra' => array( |
|
| 185 | + 'type' => 'plugin', |
|
| 186 | + 'action' => 'install', |
|
| 187 | + ), |
|
| 188 | + ) ); |
|
| 189 | + |
|
| 190 | + if ( is_wp_error( $result ) ) { |
|
| 191 | + throw new Exception( $result->get_error_message() ); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + $activate = true; |
|
| 195 | + |
|
| 196 | + } catch ( Exception $e ) { |
|
| 197 | + $success = false; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // Discard feedback |
|
| 201 | + ob_end_clean(); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + wp_clean_plugins_cache(); |
|
| 205 | + |
|
| 206 | + // Activate this thing |
|
| 207 | + if ( $activate ) { |
|
| 208 | + try { |
|
| 209 | + $result = activate_plugin( $plugin ); |
|
| 210 | + |
|
| 211 | + if ( is_wp_error( $result ) ) { |
|
| 212 | + $success = false; |
|
| 213 | + } else { |
|
| 214 | + $success = true; |
|
| 215 | + } |
|
| 216 | + } catch ( Exception $e ) { |
|
| 217 | + $success = false; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + if ( $success && function_exists( 'ayecode_connect_args' ) ) { |
|
| 223 | + ayecode_connect();// init |
|
| 224 | + $args = ayecode_connect_args(); |
|
| 225 | + $client = new AyeCode_Connect( $args ); |
|
| 226 | + $redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : ''; |
|
| 227 | + $redirect = $client->build_connect_url( $redirect_to ); |
|
| 228 | + wp_send_json_success( array( 'connect_url' => $redirect ) ); |
|
| 229 | + } else { |
|
| 230 | + wp_send_json_error( $this->strings['error_localhost'] ); |
|
| 231 | + } |
|
| 232 | + wp_die(); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Check if maybe localhost. |
|
| 237 | + * |
|
| 238 | + * @return bool |
|
| 239 | + */ |
|
| 240 | + public function is_localhost() { |
|
| 241 | + $localhost = false; |
|
| 242 | + |
|
| 243 | + $host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : ''; |
|
| 244 | + $localhost_domains = array( |
|
| 245 | + 'localhost', |
|
| 246 | + 'localhost.localdomain', |
|
| 247 | + '127.0.0.1', |
|
| 248 | + '::1' |
|
| 249 | + ); |
|
| 250 | + |
|
| 251 | + if ( in_array( $host, $localhost_domains ) ) { |
|
| 252 | + $localhost = true; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return $localhost; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Show notice to connect site. |
|
| 260 | + */ |
|
| 261 | + public function ayecode_connect_install_notice() { |
|
| 262 | + if ( $this->maybe_show() ) { |
|
| 263 | + $connect_title_string = $this->strings['connect_title']; |
|
| 264 | + $connect_external_string = $this->strings['connect_external']; |
|
| 265 | + $connect_string = $this->strings['connect']; |
|
| 266 | + $connect_button_string = $this->strings['connect_button']; |
|
| 267 | + $connecting_button_string = $this->strings['connecting_button']; |
|
| 268 | + ?> |
|
| 269 | 269 | <div class="notice notice-info acch-notice"> |
| 270 | 270 | <span class="acch-float-left"> |
| 271 | 271 | <svg width="61px" height="61px" viewBox="0 0 61 61" version="1.1" |
@@ -304,8 +304,8 @@ discard block |
||
| 304 | 304 | <h3 class="acch-title"><?php echo esc_attr( $connect_title_string ); ?></h3> |
| 305 | 305 | <p> |
| 306 | 306 | <?php |
| 307 | - echo $connect_string; |
|
| 308 | - ?> |
|
| 307 | + echo $connect_string; |
|
| 308 | + ?> |
|
| 309 | 309 | </p> |
| 310 | 310 | </span> |
| 311 | 311 | |
@@ -318,9 +318,9 @@ discard block |
||
| 318 | 318 | </div> |
| 319 | 319 | |
| 320 | 320 | <?php |
| 321 | - // only include the popup HTML if needed. |
|
| 322 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
| 323 | - ?> |
|
| 321 | + // only include the popup HTML if needed. |
|
| 322 | + if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
| 323 | + ?> |
|
| 324 | 324 | <div id="ayecode-connect-helper-external-confirm" style="display:none;"> |
| 325 | 325 | <div class="noticex notice-info acch-notice" style="border: none;"> |
| 326 | 326 | <span class="acch-float-left"> |
@@ -369,23 +369,23 @@ discard block |
||
| 369 | 369 | </div> |
| 370 | 370 | </div> |
| 371 | 371 | <?php |
| 372 | - } |
|
| 373 | - |
|
| 374 | - // add required scripts |
|
| 375 | - $this->script(); |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Get the JS Script. |
|
| 381 | - */ |
|
| 382 | - public function script() { |
|
| 383 | - |
|
| 384 | - // add thickbox if external request is requested |
|
| 385 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
| 386 | - add_thickbox(); |
|
| 387 | - } |
|
| 388 | - ?> |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + // add required scripts |
|
| 375 | + $this->script(); |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Get the JS Script. |
|
| 381 | + */ |
|
| 382 | + public function script() { |
|
| 383 | + |
|
| 384 | + // add thickbox if external request is requested |
|
| 385 | + if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
| 386 | + add_thickbox(); |
|
| 387 | + } |
|
| 388 | + ?> |
|
| 389 | 389 | <style> |
| 390 | 390 | .acch-title { |
| 391 | 391 | margin: 0; |
@@ -454,43 +454,43 @@ discard block |
||
| 454 | 454 | |
| 455 | 455 | |
| 456 | 456 | <?php |
| 457 | - // add thickbox if external request is requested |
|
| 458 | - if(! empty( $_REQUEST['external-connect-request'] )) { |
|
| 459 | - ?> |
|
| 457 | + // add thickbox if external request is requested |
|
| 458 | + if(! empty( $_REQUEST['external-connect-request'] )) { |
|
| 459 | + ?> |
|
| 460 | 460 | jQuery(function () { |
| 461 | 461 | setTimeout(function () { |
| 462 | 462 | tb_show("AyeCode Connect", "?TB_inline?width=300&height=80&inlineId=ayecode-connect-helper-external-confirm"); |
| 463 | 463 | }, 200); |
| 464 | 464 | }); |
| 465 | 465 | <?php |
| 466 | - } |
|
| 467 | - ?> |
|
| 466 | + } |
|
| 467 | + ?> |
|
| 468 | 468 | |
| 469 | 469 | </script> |
| 470 | 470 | <?php |
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * Decide what pages to show on. |
|
| 475 | - * |
|
| 476 | - * @return bool |
|
| 477 | - */ |
|
| 478 | - public function maybe_show() { |
|
| 479 | - $show = false; |
|
| 480 | - |
|
| 481 | - // check if on a page set to show |
|
| 482 | - if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->pages ) ) { |
|
| 483 | - |
|
| 484 | - // check if not active and connected |
|
| 485 | - if ( ! defined( 'AYECODE_CONNECT_VERSION' ) || ! get_option( 'ayecode_connect_blog_token' ) ) { |
|
| 486 | - $show = true; |
|
| 487 | - } |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * Decide what pages to show on. |
|
| 475 | + * |
|
| 476 | + * @return bool |
|
| 477 | + */ |
|
| 478 | + public function maybe_show() { |
|
| 479 | + $show = false; |
|
| 480 | + |
|
| 481 | + // check if on a page set to show |
|
| 482 | + if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->pages ) ) { |
|
| 483 | + |
|
| 484 | + // check if not active and connected |
|
| 485 | + if ( ! defined( 'AYECODE_CONNECT_VERSION' ) || ! get_option( 'ayecode_connect_blog_token' ) ) { |
|
| 486 | + $show = true; |
|
| 487 | + } |
|
| 488 | 488 | |
| 489 | - } |
|
| 489 | + } |
|
| 490 | 490 | |
| 491 | - return $show; |
|
| 492 | - } |
|
| 491 | + return $show; |
|
| 492 | + } |
|
| 493 | 493 | |
| 494 | - } |
|
| 494 | + } |
|
| 495 | 495 | |
| 496 | 496 | } |
@@ -175,13 +175,13 @@ discard block |
||
| 175 | 175 | $is_writeable = $is_dir && is_writeable( $this->export_dir ); |
| 176 | 176 | |
| 177 | 177 | if ( $is_dir && $is_writeable ) { |
| 178 | - return true; |
|
| 178 | + return true; |
|
| 179 | 179 | } else if ( $is_dir && !$is_writeable ) { |
| 180 | - if ( !$this->wp_filesystem->chmod( $this->export_dir, FS_CHMOD_DIR ) ) { |
|
| 181 | - return wp_sprintf( __( 'Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'invoicing' ), $this->export_dir ); |
|
| 182 | - } |
|
| 180 | + if ( !$this->wp_filesystem->chmod( $this->export_dir, FS_CHMOD_DIR ) ) { |
|
| 181 | + return wp_sprintf( __( 'Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'invoicing' ), $this->export_dir ); |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - return true; |
|
| 184 | + return true; |
|
| 185 | 185 | } else { |
| 186 | 186 | if ( !$this->wp_filesystem->mkdir( $this->export_dir, FS_CHMOD_DIR ) ) { |
| 187 | 187 | return wp_sprintf( __( 'Filesystem ERROR: Could not create directory %s. This is usually due to inconsistent file permissions.', 'invoicing' ), $this->export_dir ); |
@@ -276,12 +276,12 @@ discard block |
||
| 276 | 276 | $output = fopen( 'php://output', 'w' ) or die( 'Unsupported server' ); |
| 277 | 277 | |
| 278 | 278 | // Let the browser know what content we're streaming and how it should save the content. |
| 279 | - $name = time(); |
|
| 280 | - header( "Content-Type:application/csv" ); |
|
| 279 | + $name = time(); |
|
| 280 | + header( "Content-Type:application/csv" ); |
|
| 281 | 281 | header( "Content-Disposition:attachment;filename=noptin-subscribers-$name.csv" ); |
| 282 | 282 | |
| 283 | 283 | // Output the csv column headers. |
| 284 | - fputcsv( |
|
| 284 | + fputcsv( |
|
| 285 | 285 | $output, |
| 286 | 286 | array( |
| 287 | 287 | __( 'Discount Id', 'invoicing' ), |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * @return string The action ID. |
| 14 | 14 | */ |
| 15 | 15 | function as_enqueue_async_action( $hook, $args = array(), $group = '' ) { |
| 16 | - return ActionScheduler::factory()->async( $hook, $args, $group ); |
|
| 16 | + return ActionScheduler::factory()->async( $hook, $args, $group ); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | /** |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | * @return string The job ID |
| 28 | 28 | */ |
| 29 | 29 | function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '' ) { |
| 30 | - return ActionScheduler::factory()->single( $hook, $args, $timestamp, $group ); |
|
| 30 | + return ActionScheduler::factory()->single( $hook, $args, $timestamp, $group ); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | * @return string The job ID |
| 43 | 43 | */ |
| 44 | 44 | function as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) { |
| 45 | - return ActionScheduler::factory()->recurring( $hook, $args, $timestamp, $interval_in_seconds, $group ); |
|
| 45 | + return ActionScheduler::factory()->recurring( $hook, $args, $timestamp, $interval_in_seconds, $group ); |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * @return string The job ID |
| 70 | 70 | */ |
| 71 | 71 | function as_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '' ) { |
| 72 | - return ActionScheduler::factory()->cron( $hook, $args, $timestamp, $schedule, $group ); |
|
| 72 | + return ActionScheduler::factory()->cron( $hook, $args, $timestamp, $schedule, $group ); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -89,20 +89,20 @@ discard block |
||
| 89 | 89 | * @return string The scheduled action ID if a scheduled action was found, or empty string if no matching action found. |
| 90 | 90 | */ |
| 91 | 91 | function as_unschedule_action( $hook, $args = array(), $group = '' ) { |
| 92 | - $params = array(); |
|
| 93 | - if ( is_array($args) ) { |
|
| 94 | - $params['args'] = $args; |
|
| 95 | - } |
|
| 96 | - if ( !empty($group) ) { |
|
| 97 | - $params['group'] = $group; |
|
| 98 | - } |
|
| 99 | - $job_id = ActionScheduler::store()->find_action( $hook, $params ); |
|
| 100 | - |
|
| 101 | - if ( ! empty( $job_id ) ) { |
|
| 102 | - ActionScheduler::store()->cancel_action( $job_id ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - return $job_id; |
|
| 92 | + $params = array(); |
|
| 93 | + if ( is_array($args) ) { |
|
| 94 | + $params['args'] = $args; |
|
| 95 | + } |
|
| 96 | + if ( !empty($group) ) { |
|
| 97 | + $params['group'] = $group; |
|
| 98 | + } |
|
| 99 | + $job_id = ActionScheduler::store()->find_action( $hook, $params ); |
|
| 100 | + |
|
| 101 | + if ( ! empty( $job_id ) ) { |
|
| 102 | + ActionScheduler::store()->cancel_action( $job_id ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + return $job_id; |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -113,19 +113,19 @@ discard block |
||
| 113 | 113 | * @param string $group |
| 114 | 114 | */ |
| 115 | 115 | function as_unschedule_all_actions( $hook, $args = array(), $group = '' ) { |
| 116 | - if ( empty( $args ) ) { |
|
| 117 | - if ( ! empty( $hook ) && empty( $group ) ) { |
|
| 118 | - ActionScheduler_Store::instance()->cancel_actions_by_hook( $hook ); |
|
| 119 | - return; |
|
| 120 | - } |
|
| 121 | - if ( ! empty( $group ) && empty( $hook ) ) { |
|
| 122 | - ActionScheduler_Store::instance()->cancel_actions_by_group( $group ); |
|
| 123 | - return; |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - do { |
|
| 127 | - $unscheduled_action = as_unschedule_action( $hook, $args, $group ); |
|
| 128 | - } while ( ! empty( $unscheduled_action ) ); |
|
| 116 | + if ( empty( $args ) ) { |
|
| 117 | + if ( ! empty( $hook ) && empty( $group ) ) { |
|
| 118 | + ActionScheduler_Store::instance()->cancel_actions_by_hook( $hook ); |
|
| 119 | + return; |
|
| 120 | + } |
|
| 121 | + if ( ! empty( $group ) && empty( $hook ) ) { |
|
| 122 | + ActionScheduler_Store::instance()->cancel_actions_by_group( $group ); |
|
| 123 | + return; |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + do { |
|
| 127 | + $unscheduled_action = as_unschedule_action( $hook, $args, $group ); |
|
| 128 | + } while ( ! empty( $unscheduled_action ) ); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
@@ -144,33 +144,33 @@ discard block |
||
| 144 | 144 | * @return int|bool The timestamp for the next occurrence of a pending scheduled action, true for an async or in-progress action or false if there is no matching action. |
| 145 | 145 | */ |
| 146 | 146 | function as_next_scheduled_action( $hook, $args = NULL, $group = '' ) { |
| 147 | - $params = array(); |
|
| 148 | - if ( is_array($args) ) { |
|
| 149 | - $params['args'] = $args; |
|
| 150 | - } |
|
| 151 | - if ( !empty($group) ) { |
|
| 152 | - $params['group'] = $group; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $params['status'] = ActionScheduler_Store::STATUS_RUNNING; |
|
| 156 | - $job_id = ActionScheduler::store()->find_action( $hook, $params ); |
|
| 157 | - if ( ! empty( $job_id ) ) { |
|
| 158 | - return true; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $params['status'] = ActionScheduler_Store::STATUS_PENDING; |
|
| 162 | - $job_id = ActionScheduler::store()->find_action( $hook, $params ); |
|
| 163 | - if ( empty($job_id) ) { |
|
| 164 | - return false; |
|
| 165 | - } |
|
| 166 | - $job = ActionScheduler::store()->fetch_action( $job_id ); |
|
| 167 | - $scheduled_date = $job->get_schedule()->get_date(); |
|
| 168 | - if ( $scheduled_date ) { |
|
| 169 | - return (int) $scheduled_date->format( 'U' ); |
|
| 170 | - } elseif ( NULL === $scheduled_date ) { // pending async action with NullSchedule |
|
| 171 | - return true; |
|
| 172 | - } |
|
| 173 | - return false; |
|
| 147 | + $params = array(); |
|
| 148 | + if ( is_array($args) ) { |
|
| 149 | + $params['args'] = $args; |
|
| 150 | + } |
|
| 151 | + if ( !empty($group) ) { |
|
| 152 | + $params['group'] = $group; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $params['status'] = ActionScheduler_Store::STATUS_RUNNING; |
|
| 156 | + $job_id = ActionScheduler::store()->find_action( $hook, $params ); |
|
| 157 | + if ( ! empty( $job_id ) ) { |
|
| 158 | + return true; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $params['status'] = ActionScheduler_Store::STATUS_PENDING; |
|
| 162 | + $job_id = ActionScheduler::store()->find_action( $hook, $params ); |
|
| 163 | + if ( empty($job_id) ) { |
|
| 164 | + return false; |
|
| 165 | + } |
|
| 166 | + $job = ActionScheduler::store()->fetch_action( $job_id ); |
|
| 167 | + $scheduled_date = $job->get_schedule()->get_date(); |
|
| 168 | + if ( $scheduled_date ) { |
|
| 169 | + return (int) $scheduled_date->format( 'U' ); |
|
| 170 | + } elseif ( NULL === $scheduled_date ) { // pending async action with NullSchedule |
|
| 171 | + return true; |
|
| 172 | + } |
|
| 173 | + return false; |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
@@ -196,30 +196,30 @@ discard block |
||
| 196 | 196 | * @return array |
| 197 | 197 | */ |
| 198 | 198 | function as_get_scheduled_actions( $args = array(), $return_format = OBJECT ) { |
| 199 | - $store = ActionScheduler::store(); |
|
| 200 | - foreach ( array('date', 'modified') as $key ) { |
|
| 201 | - if ( isset($args[$key]) ) { |
|
| 202 | - $args[$key] = as_get_datetime_object($args[$key]); |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - $ids = $store->query_actions( $args ); |
|
| 206 | - |
|
| 207 | - if ( $return_format == 'ids' || $return_format == 'int' ) { |
|
| 208 | - return $ids; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $actions = array(); |
|
| 212 | - foreach ( $ids as $action_id ) { |
|
| 213 | - $actions[$action_id] = $store->fetch_action( $action_id ); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - if ( $return_format == ARRAY_A ) { |
|
| 217 | - foreach ( $actions as $action_id => $action_object ) { |
|
| 218 | - $actions[$action_id] = get_object_vars($action_object); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - return $actions; |
|
| 199 | + $store = ActionScheduler::store(); |
|
| 200 | + foreach ( array('date', 'modified') as $key ) { |
|
| 201 | + if ( isset($args[$key]) ) { |
|
| 202 | + $args[$key] = as_get_datetime_object($args[$key]); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + $ids = $store->query_actions( $args ); |
|
| 206 | + |
|
| 207 | + if ( $return_format == 'ids' || $return_format == 'int' ) { |
|
| 208 | + return $ids; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $actions = array(); |
|
| 212 | + foreach ( $ids as $action_id ) { |
|
| 213 | + $actions[$action_id] = $store->fetch_action( $action_id ); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + if ( $return_format == ARRAY_A ) { |
|
| 217 | + foreach ( $actions as $action_id => $action_object ) { |
|
| 218 | + $actions[$action_id] = get_object_vars($action_object); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + return $actions; |
|
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | /** |
@@ -240,12 +240,12 @@ discard block |
||
| 240 | 240 | * @return ActionScheduler_DateTime |
| 241 | 241 | */ |
| 242 | 242 | function as_get_datetime_object( $date_string = null, $timezone = 'UTC' ) { |
| 243 | - if ( is_object( $date_string ) && $date_string instanceof DateTime ) { |
|
| 244 | - $date = new ActionScheduler_DateTime( $date_string->format( 'Y-m-d H:i:s' ), new DateTimeZone( $timezone ) ); |
|
| 245 | - } elseif ( is_numeric( $date_string ) ) { |
|
| 246 | - $date = new ActionScheduler_DateTime( '@' . $date_string, new DateTimeZone( $timezone ) ); |
|
| 247 | - } else { |
|
| 248 | - $date = new ActionScheduler_DateTime( $date_string, new DateTimeZone( $timezone ) ); |
|
| 249 | - } |
|
| 250 | - return $date; |
|
| 243 | + if ( is_object( $date_string ) && $date_string instanceof DateTime ) { |
|
| 244 | + $date = new ActionScheduler_DateTime( $date_string->format( 'Y-m-d H:i:s' ), new DateTimeZone( $timezone ) ); |
|
| 245 | + } elseif ( is_numeric( $date_string ) ) { |
|
| 246 | + $date = new ActionScheduler_DateTime( '@' . $date_string, new DateTimeZone( $timezone ) ); |
|
| 247 | + } else { |
|
| 248 | + $date = new ActionScheduler_DateTime( $date_string, new DateTimeZone( $timezone ) ); |
|
| 249 | + } |
|
| 250 | + return $date; |
|
| 251 | 251 | } |
@@ -10,138 +10,138 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class ActionScheduler_AdminView_Deprecated { |
| 12 | 12 | |
| 13 | - public function action_scheduler_post_type_args( $args ) { |
|
| 14 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 15 | - return $args; |
|
| 16 | - } |
|
| 13 | + public function action_scheduler_post_type_args( $args ) { |
|
| 14 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 15 | + return $args; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Customise the post status related views displayed on the Scheduled Actions administration screen. |
|
| 20 | - * |
|
| 21 | - * @param array $views An associative array of views and view labels which can be used to filter the 'scheduled-action' posts displayed on the Scheduled Actions administration screen. |
|
| 22 | - * @return array $views An associative array of views and view labels which can be used to filter the 'scheduled-action' posts displayed on the Scheduled Actions administration screen. |
|
| 23 | - */ |
|
| 24 | - public function list_table_views( $views ) { |
|
| 25 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 26 | - return $views; |
|
| 27 | - } |
|
| 18 | + /** |
|
| 19 | + * Customise the post status related views displayed on the Scheduled Actions administration screen. |
|
| 20 | + * |
|
| 21 | + * @param array $views An associative array of views and view labels which can be used to filter the 'scheduled-action' posts displayed on the Scheduled Actions administration screen. |
|
| 22 | + * @return array $views An associative array of views and view labels which can be used to filter the 'scheduled-action' posts displayed on the Scheduled Actions administration screen. |
|
| 23 | + */ |
|
| 24 | + public function list_table_views( $views ) { |
|
| 25 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 26 | + return $views; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Do not include the "Edit" action for the Scheduled Actions administration screen. |
|
| 31 | - * |
|
| 32 | - * Hooked to the 'bulk_actions-edit-action-scheduler' filter. |
|
| 33 | - * |
|
| 34 | - * @param array $actions An associative array of actions which can be performed on the 'scheduled-action' post type. |
|
| 35 | - * @return array $actions An associative array of actions which can be performed on the 'scheduled-action' post type. |
|
| 36 | - */ |
|
| 37 | - public function bulk_actions( $actions ) { |
|
| 38 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 39 | - return $actions; |
|
| 40 | - } |
|
| 29 | + /** |
|
| 30 | + * Do not include the "Edit" action for the Scheduled Actions administration screen. |
|
| 31 | + * |
|
| 32 | + * Hooked to the 'bulk_actions-edit-action-scheduler' filter. |
|
| 33 | + * |
|
| 34 | + * @param array $actions An associative array of actions which can be performed on the 'scheduled-action' post type. |
|
| 35 | + * @return array $actions An associative array of actions which can be performed on the 'scheduled-action' post type. |
|
| 36 | + */ |
|
| 37 | + public function bulk_actions( $actions ) { |
|
| 38 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 39 | + return $actions; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Completely customer the columns displayed on the Scheduled Actions administration screen. |
|
| 44 | - * |
|
| 45 | - * Because we can't filter the content of the default title and date columns, we need to recreate our own |
|
| 46 | - * custom columns for displaying those post fields. For the column content, @see self::list_table_column_content(). |
|
| 47 | - * |
|
| 48 | - * @param array $columns An associative array of columns that are use for the table on the Scheduled Actions administration screen. |
|
| 49 | - * @return array $columns An associative array of columns that are use for the table on the Scheduled Actions administration screen. |
|
| 50 | - */ |
|
| 51 | - public function list_table_columns( $columns ) { |
|
| 52 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 53 | - return $columns; |
|
| 54 | - } |
|
| 42 | + /** |
|
| 43 | + * Completely customer the columns displayed on the Scheduled Actions administration screen. |
|
| 44 | + * |
|
| 45 | + * Because we can't filter the content of the default title and date columns, we need to recreate our own |
|
| 46 | + * custom columns for displaying those post fields. For the column content, @see self::list_table_column_content(). |
|
| 47 | + * |
|
| 48 | + * @param array $columns An associative array of columns that are use for the table on the Scheduled Actions administration screen. |
|
| 49 | + * @return array $columns An associative array of columns that are use for the table on the Scheduled Actions administration screen. |
|
| 50 | + */ |
|
| 51 | + public function list_table_columns( $columns ) { |
|
| 52 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 53 | + return $columns; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Make our custom title & date columns use defaulting title & date sorting. |
|
| 58 | - * |
|
| 59 | - * @param array $columns An associative array of columns that can be used to sort the table on the Scheduled Actions administration screen. |
|
| 60 | - * @return array $columns An associative array of columns that can be used to sort the table on the Scheduled Actions administration screen. |
|
| 61 | - */ |
|
| 62 | - public static function list_table_sortable_columns( $columns ) { |
|
| 63 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 64 | - return $columns; |
|
| 65 | - } |
|
| 56 | + /** |
|
| 57 | + * Make our custom title & date columns use defaulting title & date sorting. |
|
| 58 | + * |
|
| 59 | + * @param array $columns An associative array of columns that can be used to sort the table on the Scheduled Actions administration screen. |
|
| 60 | + * @return array $columns An associative array of columns that can be used to sort the table on the Scheduled Actions administration screen. |
|
| 61 | + */ |
|
| 62 | + public static function list_table_sortable_columns( $columns ) { |
|
| 63 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 64 | + return $columns; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Print the content for our custom columns. |
|
| 69 | - * |
|
| 70 | - * @param string $column_name The key for the column for which we should output our content. |
|
| 71 | - * @param int $post_id The ID of the 'scheduled-action' post for which this row relates. |
|
| 72 | - */ |
|
| 73 | - public static function list_table_column_content( $column_name, $post_id ) { |
|
| 74 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 75 | - } |
|
| 67 | + /** |
|
| 68 | + * Print the content for our custom columns. |
|
| 69 | + * |
|
| 70 | + * @param string $column_name The key for the column for which we should output our content. |
|
| 71 | + * @param int $post_id The ID of the 'scheduled-action' post for which this row relates. |
|
| 72 | + */ |
|
| 73 | + public static function list_table_column_content( $column_name, $post_id ) { |
|
| 74 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Hide the inline "Edit" action for all 'scheduled-action' posts. |
|
| 79 | - * |
|
| 80 | - * Hooked to the 'post_row_actions' filter. |
|
| 81 | - * |
|
| 82 | - * @param array $actions An associative array of actions which can be performed on the 'scheduled-action' post type. |
|
| 83 | - * @return array $actions An associative array of actions which can be performed on the 'scheduled-action' post type. |
|
| 84 | - */ |
|
| 85 | - public static function row_actions( $actions, $post ) { |
|
| 86 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 87 | - return $actions; |
|
| 88 | - } |
|
| 77 | + /** |
|
| 78 | + * Hide the inline "Edit" action for all 'scheduled-action' posts. |
|
| 79 | + * |
|
| 80 | + * Hooked to the 'post_row_actions' filter. |
|
| 81 | + * |
|
| 82 | + * @param array $actions An associative array of actions which can be performed on the 'scheduled-action' post type. |
|
| 83 | + * @return array $actions An associative array of actions which can be performed on the 'scheduled-action' post type. |
|
| 84 | + */ |
|
| 85 | + public static function row_actions( $actions, $post ) { |
|
| 86 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 87 | + return $actions; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Run an action when triggered from the Action Scheduler administration screen. |
|
| 92 | - * |
|
| 93 | - * @codeCoverageIgnore |
|
| 94 | - */ |
|
| 95 | - public static function maybe_execute_action() { |
|
| 96 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 97 | - } |
|
| 90 | + /** |
|
| 91 | + * Run an action when triggered from the Action Scheduler administration screen. |
|
| 92 | + * |
|
| 93 | + * @codeCoverageIgnore |
|
| 94 | + */ |
|
| 95 | + public static function maybe_execute_action() { |
|
| 96 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Convert an interval of seconds into a two part human friendly string. |
|
| 101 | - * |
|
| 102 | - * The WordPress human_time_diff() function only calculates the time difference to one degree, meaning |
|
| 103 | - * even if an action is 1 day and 11 hours away, it will display "1 day". This funciton goes one step |
|
| 104 | - * further to display two degrees of accuracy. |
|
| 105 | - * |
|
| 106 | - * Based on Crontrol::interval() function by Edward Dale: https://wordpress.org/plugins/wp-crontrol/ |
|
| 107 | - * |
|
| 108 | - * @param int $interval A interval in seconds. |
|
| 109 | - * @return string A human friendly string representation of the interval. |
|
| 110 | - */ |
|
| 111 | - public static function admin_notices() { |
|
| 112 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 113 | - } |
|
| 99 | + /** |
|
| 100 | + * Convert an interval of seconds into a two part human friendly string. |
|
| 101 | + * |
|
| 102 | + * The WordPress human_time_diff() function only calculates the time difference to one degree, meaning |
|
| 103 | + * even if an action is 1 day and 11 hours away, it will display "1 day". This funciton goes one step |
|
| 104 | + * further to display two degrees of accuracy. |
|
| 105 | + * |
|
| 106 | + * Based on Crontrol::interval() function by Edward Dale: https://wordpress.org/plugins/wp-crontrol/ |
|
| 107 | + * |
|
| 108 | + * @param int $interval A interval in seconds. |
|
| 109 | + * @return string A human friendly string representation of the interval. |
|
| 110 | + */ |
|
| 111 | + public static function admin_notices() { |
|
| 112 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Filter search queries to allow searching by Claim ID (i.e. post_password). |
|
| 117 | - * |
|
| 118 | - * @param string $orderby MySQL orderby string. |
|
| 119 | - * @param WP_Query $query Instance of a WP_Query object |
|
| 120 | - * @return string MySQL orderby string. |
|
| 121 | - */ |
|
| 122 | - public function custom_orderby( $orderby, $query ){ |
|
| 123 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 124 | - } |
|
| 115 | + /** |
|
| 116 | + * Filter search queries to allow searching by Claim ID (i.e. post_password). |
|
| 117 | + * |
|
| 118 | + * @param string $orderby MySQL orderby string. |
|
| 119 | + * @param WP_Query $query Instance of a WP_Query object |
|
| 120 | + * @return string MySQL orderby string. |
|
| 121 | + */ |
|
| 122 | + public function custom_orderby( $orderby, $query ){ |
|
| 123 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Filter search queries to allow searching by Claim ID (i.e. post_password). |
|
| 128 | - * |
|
| 129 | - * @param string $search MySQL search string. |
|
| 130 | - * @param WP_Query $query Instance of a WP_Query object |
|
| 131 | - * @return string MySQL search string. |
|
| 132 | - */ |
|
| 133 | - public function search_post_password( $search, $query ) { |
|
| 134 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 135 | - } |
|
| 126 | + /** |
|
| 127 | + * Filter search queries to allow searching by Claim ID (i.e. post_password). |
|
| 128 | + * |
|
| 129 | + * @param string $search MySQL search string. |
|
| 130 | + * @param WP_Query $query Instance of a WP_Query object |
|
| 131 | + * @return string MySQL search string. |
|
| 132 | + */ |
|
| 133 | + public function search_post_password( $search, $query ) { |
|
| 134 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * Change messages when a scheduled action is updated. |
|
| 139 | - * |
|
| 140 | - * @param array $messages |
|
| 141 | - * @return array |
|
| 142 | - */ |
|
| 143 | - public function post_updated_messages( $messages ) { |
|
| 144 | - _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 145 | - return $messages; |
|
| 146 | - } |
|
| 137 | + /** |
|
| 138 | + * Change messages when a scheduled action is updated. |
|
| 139 | + * |
|
| 140 | + * @param array $messages |
|
| 141 | + * @return array |
|
| 142 | + */ |
|
| 143 | + public function post_updated_messages( $messages ) { |
|
| 144 | + _deprecated_function( __METHOD__, '2.0.0' ); |
|
| 145 | + return $messages; |
|
| 146 | + } |
|
| 147 | 147 | } |
| 148 | 148 | \ No newline at end of file |
@@ -19,8 +19,8 @@ discard block |
||
| 19 | 19 | * @return string The job ID |
| 20 | 20 | */ |
| 21 | 21 | function wc_schedule_single_action( $timestamp, $hook, $args = array(), $group = '' ) { |
| 22 | - _deprecated_function( __FUNCTION__, '2.1.0', 'as_schedule_single_action()' ); |
|
| 23 | - return as_schedule_single_action( $timestamp, $hook, $args, $group ); |
|
| 22 | + _deprecated_function( __FUNCTION__, '2.1.0', 'as_schedule_single_action()' ); |
|
| 23 | + return as_schedule_single_action( $timestamp, $hook, $args, $group ); |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | /** |
@@ -37,8 +37,8 @@ discard block |
||
| 37 | 37 | * @return string The job ID |
| 38 | 38 | */ |
| 39 | 39 | function wc_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) { |
| 40 | - _deprecated_function( __FUNCTION__, '2.1.0', 'as_schedule_recurring_action()' ); |
|
| 41 | - return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group ); |
|
| 40 | + _deprecated_function( __FUNCTION__, '2.1.0', 'as_schedule_recurring_action()' ); |
|
| 41 | + return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group ); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
@@ -65,8 +65,8 @@ discard block |
||
| 65 | 65 | * @return string The job ID |
| 66 | 66 | */ |
| 67 | 67 | function wc_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '' ) { |
| 68 | - _deprecated_function( __FUNCTION__, '2.1.0', 'as_schedule_cron_action()' ); |
|
| 69 | - return as_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group ); |
|
| 68 | + _deprecated_function( __FUNCTION__, '2.1.0', 'as_schedule_cron_action()' ); |
|
| 69 | + return as_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group ); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -79,8 +79,8 @@ discard block |
||
| 79 | 79 | * @deprecated 2.1.0 |
| 80 | 80 | */ |
| 81 | 81 | function wc_unschedule_action( $hook, $args = array(), $group = '' ) { |
| 82 | - _deprecated_function( __FUNCTION__, '2.1.0', 'as_unschedule_action()' ); |
|
| 83 | - as_unschedule_action( $hook, $args, $group ); |
|
| 82 | + _deprecated_function( __FUNCTION__, '2.1.0', 'as_unschedule_action()' ); |
|
| 83 | + as_unschedule_action( $hook, $args, $group ); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -93,8 +93,8 @@ discard block |
||
| 93 | 93 | * @return int|bool The timestamp for the next occurrence, or false if nothing was found |
| 94 | 94 | */ |
| 95 | 95 | function wc_next_scheduled_action( $hook, $args = NULL, $group = '' ) { |
| 96 | - _deprecated_function( __FUNCTION__, '2.1.0', 'as_next_scheduled_action()' ); |
|
| 97 | - return as_next_scheduled_action( $hook, $args, $group ); |
|
| 96 | + _deprecated_function( __FUNCTION__, '2.1.0', 'as_next_scheduled_action()' ); |
|
| 97 | + return as_next_scheduled_action( $hook, $args, $group ); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | /** |
@@ -121,6 +121,6 @@ discard block |
||
| 121 | 121 | * @return array |
| 122 | 122 | */ |
| 123 | 123 | function wc_get_scheduled_actions( $args = array(), $return_format = OBJECT ) { |
| 124 | - _deprecated_function( __FUNCTION__, '2.1.0', 'as_get_scheduled_actions()' ); |
|
| 125 | - return as_get_scheduled_actions( $args, $return_format ); |
|
| 124 | + _deprecated_function( __FUNCTION__, '2.1.0', 'as_get_scheduled_actions()' ); |
|
| 125 | + return as_get_scheduled_actions( $args, $return_format ); |
|
| 126 | 126 | } |
@@ -5,25 +5,25 @@ |
||
| 5 | 5 | */ |
| 6 | 6 | abstract class ActionScheduler_Schedule_Deprecated implements ActionScheduler_Schedule { |
| 7 | 7 | |
| 8 | - /** |
|
| 9 | - * Get the date & time this schedule was created to run, or calculate when it should be run |
|
| 10 | - * after a given date & time. |
|
| 11 | - * |
|
| 12 | - * @param DateTime $after |
|
| 13 | - * |
|
| 14 | - * @return DateTime|null |
|
| 15 | - */ |
|
| 16 | - public function next( DateTime $after = NULL ) { |
|
| 17 | - if ( empty( $after ) ) { |
|
| 18 | - $return_value = $this->get_date(); |
|
| 19 | - $replacement_method = 'get_date()'; |
|
| 20 | - } else { |
|
| 21 | - $return_value = $this->get_next( $after ); |
|
| 22 | - $replacement_method = 'get_next( $after )'; |
|
| 23 | - } |
|
| 8 | + /** |
|
| 9 | + * Get the date & time this schedule was created to run, or calculate when it should be run |
|
| 10 | + * after a given date & time. |
|
| 11 | + * |
|
| 12 | + * @param DateTime $after |
|
| 13 | + * |
|
| 14 | + * @return DateTime|null |
|
| 15 | + */ |
|
| 16 | + public function next( DateTime $after = NULL ) { |
|
| 17 | + if ( empty( $after ) ) { |
|
| 18 | + $return_value = $this->get_date(); |
|
| 19 | + $replacement_method = 'get_date()'; |
|
| 20 | + } else { |
|
| 21 | + $return_value = $this->get_next( $after ); |
|
| 22 | + $replacement_method = 'get_next( $after )'; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - _deprecated_function( __METHOD__, '3.0.0', __CLASS__ . '::' . $replacement_method ); |
|
| 25 | + _deprecated_function( __METHOD__, '3.0.0', __CLASS__ . '::' . $replacement_method ); |
|
| 26 | 26 | |
| 27 | - return $return_value; |
|
| 28 | - } |
|
| 27 | + return $return_value; |
|
| 28 | + } |
|
| 29 | 29 | } |