@@ -15,125 +15,125 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class WPInv_Subscription extends GetPaid_Data { |
17 | 17 | |
18 | - /** |
|
19 | - * Which data store to load. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - protected $data_store_name = 'subscription'; |
|
24 | - |
|
25 | - /** |
|
26 | - * This is the name of this object type. |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $object_type = 'subscription'; |
|
31 | - |
|
32 | - /** |
|
33 | - * Item Data array. This is the core item data exposed in APIs. |
|
34 | - * |
|
35 | - * @since 1.0.19 |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $data = array( |
|
39 | - 'customer_id' => 0, |
|
40 | - 'frequency' => 1, |
|
41 | - 'period' => 'D', |
|
42 | - 'initial_amount' => null, |
|
43 | - 'recurring_amount' => null, |
|
44 | - 'bill_times' => 0, |
|
45 | - 'transaction_id' => '', |
|
46 | - 'parent_payment_id' => null, |
|
47 | - 'product_id' => 0, |
|
48 | - 'created' => '0000-00-00 00:00:00', |
|
49 | - 'expiration' => '0000-00-00 00:00:00', |
|
50 | - 'trial_period' => '', |
|
51 | - 'status' => 'pending', |
|
52 | - 'profile_id' => '', |
|
53 | - 'gateway' => '', |
|
54 | - 'customer' => '', |
|
55 | - ); |
|
56 | - |
|
57 | - /** |
|
58 | - * Stores the status transition information. |
|
59 | - * |
|
60 | - * @since 1.0.19 |
|
61 | - * @var bool |
|
62 | - */ |
|
63 | - protected $status_transition = false; |
|
64 | - |
|
65 | - /** |
|
66 | - * Get the subscription if ID is passed, otherwise the subscription is new and empty. |
|
67 | - * |
|
68 | - * @param int|string|object|WPInv_Subscription $subscription Subscription id, profile_id, or object to read. |
|
69 | - * @param bool $deprecated |
|
70 | - */ |
|
71 | - function __construct( $subscription = 0, $deprecated = false ) { |
|
72 | - |
|
73 | - parent::__construct( $subscription ); |
|
74 | - |
|
75 | - if ( ! $deprecated && ! empty( $subscription ) && is_numeric( $subscription ) ) { |
|
76 | - $this->set_id( $subscription ); |
|
77 | - } elseif ( $subscription instanceof self ) { |
|
78 | - $this->set_id( $subscription->get_id() ); |
|
79 | - } elseif ( ! empty( $subscription->id ) ) { |
|
80 | - $this->set_id( $subscription->id ); |
|
81 | - } elseif ( $deprecated && $subscription_id = self::get_subscription_id_by_field( $subscription, 'profile_id' ) ) { |
|
82 | - $this->set_id( $subscription_id ); |
|
83 | - } else { |
|
84 | - $this->set_object_read( true ); |
|
85 | - } |
|
86 | - |
|
87 | - // Load the datastore. |
|
88 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
89 | - |
|
90 | - if ( $this->get_id() > 0 ) { |
|
91 | - $this->data_store->read( $this ); |
|
92 | - } |
|
93 | - |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Given an invoice id, profile id, transaction id, it returns the subscription's id. |
|
98 | - * |
|
99 | - * |
|
100 | - * @static |
|
101 | - * @param string $value |
|
102 | - * @param string $field Either invoice_id, transaction_id or profile_id. |
|
103 | - * @since 1.0.19 |
|
104 | - * @return int |
|
105 | - */ |
|
106 | - public static function get_subscription_id_by_field( $value, $field = 'profile_id' ) { |
|
18 | + /** |
|
19 | + * Which data store to load. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + protected $data_store_name = 'subscription'; |
|
24 | + |
|
25 | + /** |
|
26 | + * This is the name of this object type. |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $object_type = 'subscription'; |
|
31 | + |
|
32 | + /** |
|
33 | + * Item Data array. This is the core item data exposed in APIs. |
|
34 | + * |
|
35 | + * @since 1.0.19 |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $data = array( |
|
39 | + 'customer_id' => 0, |
|
40 | + 'frequency' => 1, |
|
41 | + 'period' => 'D', |
|
42 | + 'initial_amount' => null, |
|
43 | + 'recurring_amount' => null, |
|
44 | + 'bill_times' => 0, |
|
45 | + 'transaction_id' => '', |
|
46 | + 'parent_payment_id' => null, |
|
47 | + 'product_id' => 0, |
|
48 | + 'created' => '0000-00-00 00:00:00', |
|
49 | + 'expiration' => '0000-00-00 00:00:00', |
|
50 | + 'trial_period' => '', |
|
51 | + 'status' => 'pending', |
|
52 | + 'profile_id' => '', |
|
53 | + 'gateway' => '', |
|
54 | + 'customer' => '', |
|
55 | + ); |
|
56 | + |
|
57 | + /** |
|
58 | + * Stores the status transition information. |
|
59 | + * |
|
60 | + * @since 1.0.19 |
|
61 | + * @var bool |
|
62 | + */ |
|
63 | + protected $status_transition = false; |
|
64 | + |
|
65 | + /** |
|
66 | + * Get the subscription if ID is passed, otherwise the subscription is new and empty. |
|
67 | + * |
|
68 | + * @param int|string|object|WPInv_Subscription $subscription Subscription id, profile_id, or object to read. |
|
69 | + * @param bool $deprecated |
|
70 | + */ |
|
71 | + function __construct( $subscription = 0, $deprecated = false ) { |
|
72 | + |
|
73 | + parent::__construct( $subscription ); |
|
74 | + |
|
75 | + if ( ! $deprecated && ! empty( $subscription ) && is_numeric( $subscription ) ) { |
|
76 | + $this->set_id( $subscription ); |
|
77 | + } elseif ( $subscription instanceof self ) { |
|
78 | + $this->set_id( $subscription->get_id() ); |
|
79 | + } elseif ( ! empty( $subscription->id ) ) { |
|
80 | + $this->set_id( $subscription->id ); |
|
81 | + } elseif ( $deprecated && $subscription_id = self::get_subscription_id_by_field( $subscription, 'profile_id' ) ) { |
|
82 | + $this->set_id( $subscription_id ); |
|
83 | + } else { |
|
84 | + $this->set_object_read( true ); |
|
85 | + } |
|
86 | + |
|
87 | + // Load the datastore. |
|
88 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
89 | + |
|
90 | + if ( $this->get_id() > 0 ) { |
|
91 | + $this->data_store->read( $this ); |
|
92 | + } |
|
93 | + |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Given an invoice id, profile id, transaction id, it returns the subscription's id. |
|
98 | + * |
|
99 | + * |
|
100 | + * @static |
|
101 | + * @param string $value |
|
102 | + * @param string $field Either invoice_id, transaction_id or profile_id. |
|
103 | + * @since 1.0.19 |
|
104 | + * @return int |
|
105 | + */ |
|
106 | + public static function get_subscription_id_by_field( $value, $field = 'profile_id' ) { |
|
107 | 107 | global $wpdb; |
108 | 108 | |
109 | - // Trim the value. |
|
110 | - $value = trim( $value ); |
|
109 | + // Trim the value. |
|
110 | + $value = trim( $value ); |
|
111 | 111 | |
112 | - if ( empty( $value ) ) { |
|
113 | - return 0; |
|
114 | - } |
|
112 | + if ( empty( $value ) ) { |
|
113 | + return 0; |
|
114 | + } |
|
115 | 115 | |
116 | - if ( 'invoice_id' == $field ) { |
|
117 | - $field = 'parent_payment_id'; |
|
118 | - } |
|
116 | + if ( 'invoice_id' == $field ) { |
|
117 | + $field = 'parent_payment_id'; |
|
118 | + } |
|
119 | 119 | |
120 | 120 | // Valid fields. |
121 | 121 | $fields = array( |
122 | - 'parent_payment_id', |
|
123 | - 'transaction_id', |
|
124 | - 'profile_id' |
|
125 | - ); |
|
126 | - |
|
127 | - // Ensure a field has been passed. |
|
128 | - if ( empty( $field ) || ! in_array( $field, $fields ) ) { |
|
129 | - return 0; |
|
130 | - } |
|
131 | - |
|
132 | - // Maybe retrieve from the cache. |
|
133 | - $subscription_id = wp_cache_get( $value, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
134 | - if ( ! empty( $subscription_id ) ) { |
|
135 | - return $subscription_id; |
|
136 | - } |
|
122 | + 'parent_payment_id', |
|
123 | + 'transaction_id', |
|
124 | + 'profile_id' |
|
125 | + ); |
|
126 | + |
|
127 | + // Ensure a field has been passed. |
|
128 | + if ( empty( $field ) || ! in_array( $field, $fields ) ) { |
|
129 | + return 0; |
|
130 | + } |
|
131 | + |
|
132 | + // Maybe retrieve from the cache. |
|
133 | + $subscription_id = wp_cache_get( $value, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
134 | + if ( ! empty( $subscription_id ) ) { |
|
135 | + return $subscription_id; |
|
136 | + } |
|
137 | 137 | |
138 | 138 | // Fetch from the db. |
139 | 139 | $table = $wpdb->prefix . 'wpinv_subscriptions'; |
@@ -141,34 +141,34 @@ discard block |
||
141 | 141 | $wpdb->prepare( "SELECT `id` FROM $table WHERE `$field`=%s LIMIT 1", $value ) |
142 | 142 | ); |
143 | 143 | |
144 | - if ( empty( $subscription_id ) ) { |
|
145 | - return 0; |
|
146 | - } |
|
144 | + if ( empty( $subscription_id ) ) { |
|
145 | + return 0; |
|
146 | + } |
|
147 | 147 | |
148 | - // Update the cache with our data. |
|
149 | - wp_cache_set( $value, $subscription_id, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
148 | + // Update the cache with our data. |
|
149 | + wp_cache_set( $value, $subscription_id, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
150 | 150 | |
151 | - return $subscription_id; |
|
152 | - } |
|
151 | + return $subscription_id; |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
154 | + /** |
|
155 | 155 | * Clears the subscription's cache. |
156 | 156 | */ |
157 | 157 | public function clear_cache() { |
158 | - wp_cache_delete( $this->get_parent_payment_id(), 'getpaid_subscription_parent_payment_ids_to_subscription_ids' ); |
|
159 | - wp_cache_delete( $this->get_transaction_id(), 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
160 | - wp_cache_delete( $this->get_profile_id(), 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
161 | - wp_cache_delete( $this->get_id(), 'getpaid_subscriptions' ); |
|
162 | - } |
|
158 | + wp_cache_delete( $this->get_parent_payment_id(), 'getpaid_subscription_parent_payment_ids_to_subscription_ids' ); |
|
159 | + wp_cache_delete( $this->get_transaction_id(), 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
160 | + wp_cache_delete( $this->get_profile_id(), 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
161 | + wp_cache_delete( $this->get_id(), 'getpaid_subscriptions' ); |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
164 | + /** |
|
165 | 165 | * Checks if a subscription key is set. |
166 | 166 | */ |
167 | 167 | public function _isset( $key ) { |
168 | 168 | return isset( $this->data[$key] ) || method_exists( $this, "get_$key" ); |
169 | - } |
|
169 | + } |
|
170 | 170 | |
171 | - /* |
|
171 | + /* |
|
172 | 172 | |-------------------------------------------------------------------------- |
173 | 173 | | CRUD methods |
174 | 174 | |-------------------------------------------------------------------------- |
@@ -177,57 +177,57 @@ discard block |
||
177 | 177 | | |
178 | 178 | */ |
179 | 179 | |
180 | - /* |
|
180 | + /* |
|
181 | 181 | |-------------------------------------------------------------------------- |
182 | 182 | | Getters |
183 | 183 | |-------------------------------------------------------------------------- |
184 | 184 | */ |
185 | 185 | |
186 | - /** |
|
187 | - * Get customer id. |
|
188 | - * |
|
189 | - * @since 1.0.19 |
|
190 | - * @param string $context View or edit context. |
|
191 | - * @return int |
|
192 | - */ |
|
193 | - public function get_customer_id( $context = 'view' ) { |
|
194 | - return (int) $this->get_prop( 'customer_id', $context ); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Get customer information. |
|
199 | - * |
|
200 | - * @since 1.0.19 |
|
201 | - * @param string $context View or edit context. |
|
202 | - * @return WP_User|false WP_User object on success, false on failure. |
|
203 | - */ |
|
204 | - public function get_customer( $context = 'view' ) { |
|
205 | - return get_userdata( $this->get_customer_id( $context ) ); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Get parent invoice id. |
|
210 | - * |
|
211 | - * @since 1.0.19 |
|
212 | - * @param string $context View or edit context. |
|
213 | - * @return int |
|
214 | - */ |
|
215 | - public function get_parent_invoice_id( $context = 'view' ) { |
|
216 | - return (int) $this->get_prop( 'parent_payment_id', $context ); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Alias for self::get_parent_invoice_id(). |
|
221 | - * |
|
222 | - * @since 1.0.19 |
|
223 | - * @param string $context View or edit context. |
|
224 | - * @return int |
|
225 | - */ |
|
186 | + /** |
|
187 | + * Get customer id. |
|
188 | + * |
|
189 | + * @since 1.0.19 |
|
190 | + * @param string $context View or edit context. |
|
191 | + * @return int |
|
192 | + */ |
|
193 | + public function get_customer_id( $context = 'view' ) { |
|
194 | + return (int) $this->get_prop( 'customer_id', $context ); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Get customer information. |
|
199 | + * |
|
200 | + * @since 1.0.19 |
|
201 | + * @param string $context View or edit context. |
|
202 | + * @return WP_User|false WP_User object on success, false on failure. |
|
203 | + */ |
|
204 | + public function get_customer( $context = 'view' ) { |
|
205 | + return get_userdata( $this->get_customer_id( $context ) ); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Get parent invoice id. |
|
210 | + * |
|
211 | + * @since 1.0.19 |
|
212 | + * @param string $context View or edit context. |
|
213 | + * @return int |
|
214 | + */ |
|
215 | + public function get_parent_invoice_id( $context = 'view' ) { |
|
216 | + return (int) $this->get_prop( 'parent_payment_id', $context ); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Alias for self::get_parent_invoice_id(). |
|
221 | + * |
|
222 | + * @since 1.0.19 |
|
223 | + * @param string $context View or edit context. |
|
224 | + * @return int |
|
225 | + */ |
|
226 | 226 | public function get_parent_payment_id( $context = 'view' ) { |
227 | 227 | return $this->get_parent_invoice_id( $context ); |
228 | - } |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
230 | + /** |
|
231 | 231 | * Alias for self::get_parent_invoice_id(). |
232 | 232 | * |
233 | 233 | * @since 1.0.0 |
@@ -237,390 +237,390 @@ discard block |
||
237 | 237 | return $this->get_parent_invoice_id( $context ); |
238 | 238 | } |
239 | 239 | |
240 | - /** |
|
241 | - * Get parent invoice. |
|
242 | - * |
|
243 | - * @since 1.0.19 |
|
244 | - * @param string $context View or edit context. |
|
245 | - * @return WPInv_Invoice |
|
246 | - */ |
|
247 | - public function get_parent_invoice( $context = 'view' ) { |
|
248 | - return new WPInv_Invoice( $this->get_parent_invoice_id( $context ) ); |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Alias for self::get_parent_invoice(). |
|
253 | - * |
|
254 | - * @since 1.0.19 |
|
255 | - * @param string $context View or edit context. |
|
256 | - * @return WPInv_Invoice |
|
257 | - */ |
|
240 | + /** |
|
241 | + * Get parent invoice. |
|
242 | + * |
|
243 | + * @since 1.0.19 |
|
244 | + * @param string $context View or edit context. |
|
245 | + * @return WPInv_Invoice |
|
246 | + */ |
|
247 | + public function get_parent_invoice( $context = 'view' ) { |
|
248 | + return new WPInv_Invoice( $this->get_parent_invoice_id( $context ) ); |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Alias for self::get_parent_invoice(). |
|
253 | + * |
|
254 | + * @since 1.0.19 |
|
255 | + * @param string $context View or edit context. |
|
256 | + * @return WPInv_Invoice |
|
257 | + */ |
|
258 | 258 | public function get_parent_payment( $context = 'view' ) { |
259 | 259 | return $this->get_parent_invoice( $context ); |
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Get subscription's product id. |
|
264 | - * |
|
265 | - * @since 1.0.19 |
|
266 | - * @param string $context View or edit context. |
|
267 | - * @return int |
|
268 | - */ |
|
269 | - public function get_product_id( $context = 'view' ) { |
|
270 | - return (int) $this->get_prop( 'product_id', $context ); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Get the subscription product. |
|
275 | - * |
|
276 | - * @since 1.0.19 |
|
277 | - * @param string $context View or edit context. |
|
278 | - * @return WPInv_Item |
|
279 | - */ |
|
280 | - public function get_product( $context = 'view' ) { |
|
281 | - return new WPInv_Item( $this->get_product_id( $context ) ); |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Get parent invoice's gateway. |
|
286 | - * |
|
287 | - * Here for backwards compatibility. |
|
288 | - * |
|
289 | - * @since 1.0.19 |
|
290 | - * @param string $context View or edit context. |
|
291 | - * @return string |
|
292 | - */ |
|
293 | - public function get_gateway( $context = 'view' ) { |
|
294 | - return $this->get_parent_invoice( $context )->get_gateway(); |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Get the period of a renewal. |
|
299 | - * |
|
300 | - * @since 1.0.19 |
|
301 | - * @param string $context View or edit context. |
|
302 | - * @return string |
|
303 | - */ |
|
304 | - public function get_period( $context = 'view' ) { |
|
305 | - return $this->get_prop( 'period', $context ); |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Get number of periods each renewal is valid for. |
|
310 | - * |
|
311 | - * @since 1.0.19 |
|
312 | - * @param string $context View or edit context. |
|
313 | - * @return int |
|
314 | - */ |
|
315 | - public function get_frequency( $context = 'view' ) { |
|
316 | - return (int) $this->get_prop( 'frequency', $context ); |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Get the initial amount for the subscription. |
|
321 | - * |
|
322 | - * @since 1.0.19 |
|
323 | - * @param string $context View or edit context. |
|
324 | - * @return float |
|
325 | - */ |
|
326 | - public function get_initial_amount( $context = 'view' ) { |
|
327 | - return (float) wpinv_sanitize_amount( $this->get_prop( 'initial_amount', $context ) ); |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Get the recurring amount for the subscription. |
|
332 | - * |
|
333 | - * @since 1.0.19 |
|
334 | - * @param string $context View or edit context. |
|
335 | - * @return float |
|
336 | - */ |
|
337 | - public function get_recurring_amount( $context = 'view' ) { |
|
338 | - return (float) wpinv_sanitize_amount( $this->get_prop( 'recurring_amount', $context ) ); |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * Get number of times that this subscription can be renewed. |
|
343 | - * |
|
344 | - * @since 1.0.19 |
|
345 | - * @param string $context View or edit context. |
|
346 | - * @return int |
|
347 | - */ |
|
348 | - public function get_bill_times( $context = 'view' ) { |
|
349 | - return (int) $this->get_prop( 'bill_times', $context ); |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * Get transaction id of this subscription's parent invoice. |
|
354 | - * |
|
355 | - * @since 1.0.19 |
|
356 | - * @param string $context View or edit context. |
|
357 | - * @return string |
|
358 | - */ |
|
359 | - public function get_transaction_id( $context = 'view' ) { |
|
360 | - return $this->get_prop( 'transaction_id', $context ); |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Get the date that the subscription was created. |
|
365 | - * |
|
366 | - * @since 1.0.19 |
|
367 | - * @param string $context View or edit context. |
|
368 | - * @return string |
|
369 | - */ |
|
370 | - public function get_created( $context = 'view' ) { |
|
371 | - return $this->get_prop( 'created', $context ); |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Alias for self::get_created(). |
|
376 | - * |
|
377 | - * @since 1.0.19 |
|
378 | - * @param string $context View or edit context. |
|
379 | - * @return string |
|
380 | - */ |
|
381 | - public function get_date_created( $context = 'view' ) { |
|
382 | - return $this->get_created( $context ); |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Retrieves the creation date in a timestamp |
|
387 | - * |
|
388 | - * @since 1.0.0 |
|
389 | - * @return int |
|
390 | - */ |
|
391 | - public function get_time_created() { |
|
392 | - $created = $this->get_date_created(); |
|
393 | - return empty( $created ) ? current_time( 'timestamp' ) : strtotime( $created, current_time( 'timestamp' ) ); |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Get GMT date when the subscription was created. |
|
398 | - * |
|
399 | - * @since 1.0.19 |
|
400 | - * @param string $context View or edit context. |
|
401 | - * @return string |
|
402 | - */ |
|
403 | - public function get_date_created_gmt( $context = 'view' ) { |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Get subscription's product id. |
|
264 | + * |
|
265 | + * @since 1.0.19 |
|
266 | + * @param string $context View or edit context. |
|
267 | + * @return int |
|
268 | + */ |
|
269 | + public function get_product_id( $context = 'view' ) { |
|
270 | + return (int) $this->get_prop( 'product_id', $context ); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Get the subscription product. |
|
275 | + * |
|
276 | + * @since 1.0.19 |
|
277 | + * @param string $context View or edit context. |
|
278 | + * @return WPInv_Item |
|
279 | + */ |
|
280 | + public function get_product( $context = 'view' ) { |
|
281 | + return new WPInv_Item( $this->get_product_id( $context ) ); |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Get parent invoice's gateway. |
|
286 | + * |
|
287 | + * Here for backwards compatibility. |
|
288 | + * |
|
289 | + * @since 1.0.19 |
|
290 | + * @param string $context View or edit context. |
|
291 | + * @return string |
|
292 | + */ |
|
293 | + public function get_gateway( $context = 'view' ) { |
|
294 | + return $this->get_parent_invoice( $context )->get_gateway(); |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Get the period of a renewal. |
|
299 | + * |
|
300 | + * @since 1.0.19 |
|
301 | + * @param string $context View or edit context. |
|
302 | + * @return string |
|
303 | + */ |
|
304 | + public function get_period( $context = 'view' ) { |
|
305 | + return $this->get_prop( 'period', $context ); |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Get number of periods each renewal is valid for. |
|
310 | + * |
|
311 | + * @since 1.0.19 |
|
312 | + * @param string $context View or edit context. |
|
313 | + * @return int |
|
314 | + */ |
|
315 | + public function get_frequency( $context = 'view' ) { |
|
316 | + return (int) $this->get_prop( 'frequency', $context ); |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Get the initial amount for the subscription. |
|
321 | + * |
|
322 | + * @since 1.0.19 |
|
323 | + * @param string $context View or edit context. |
|
324 | + * @return float |
|
325 | + */ |
|
326 | + public function get_initial_amount( $context = 'view' ) { |
|
327 | + return (float) wpinv_sanitize_amount( $this->get_prop( 'initial_amount', $context ) ); |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Get the recurring amount for the subscription. |
|
332 | + * |
|
333 | + * @since 1.0.19 |
|
334 | + * @param string $context View or edit context. |
|
335 | + * @return float |
|
336 | + */ |
|
337 | + public function get_recurring_amount( $context = 'view' ) { |
|
338 | + return (float) wpinv_sanitize_amount( $this->get_prop( 'recurring_amount', $context ) ); |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Get number of times that this subscription can be renewed. |
|
343 | + * |
|
344 | + * @since 1.0.19 |
|
345 | + * @param string $context View or edit context. |
|
346 | + * @return int |
|
347 | + */ |
|
348 | + public function get_bill_times( $context = 'view' ) { |
|
349 | + return (int) $this->get_prop( 'bill_times', $context ); |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * Get transaction id of this subscription's parent invoice. |
|
354 | + * |
|
355 | + * @since 1.0.19 |
|
356 | + * @param string $context View or edit context. |
|
357 | + * @return string |
|
358 | + */ |
|
359 | + public function get_transaction_id( $context = 'view' ) { |
|
360 | + return $this->get_prop( 'transaction_id', $context ); |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Get the date that the subscription was created. |
|
365 | + * |
|
366 | + * @since 1.0.19 |
|
367 | + * @param string $context View or edit context. |
|
368 | + * @return string |
|
369 | + */ |
|
370 | + public function get_created( $context = 'view' ) { |
|
371 | + return $this->get_prop( 'created', $context ); |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Alias for self::get_created(). |
|
376 | + * |
|
377 | + * @since 1.0.19 |
|
378 | + * @param string $context View or edit context. |
|
379 | + * @return string |
|
380 | + */ |
|
381 | + public function get_date_created( $context = 'view' ) { |
|
382 | + return $this->get_created( $context ); |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Retrieves the creation date in a timestamp |
|
387 | + * |
|
388 | + * @since 1.0.0 |
|
389 | + * @return int |
|
390 | + */ |
|
391 | + public function get_time_created() { |
|
392 | + $created = $this->get_date_created(); |
|
393 | + return empty( $created ) ? current_time( 'timestamp' ) : strtotime( $created, current_time( 'timestamp' ) ); |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Get GMT date when the subscription was created. |
|
398 | + * |
|
399 | + * @since 1.0.19 |
|
400 | + * @param string $context View or edit context. |
|
401 | + * @return string |
|
402 | + */ |
|
403 | + public function get_date_created_gmt( $context = 'view' ) { |
|
404 | 404 | $date = $this->get_date_created( $context ); |
405 | 405 | |
406 | 406 | if ( $date ) { |
407 | 407 | $date = get_gmt_from_date( $date ); |
408 | 408 | } |
409 | - return $date; |
|
410 | - } |
|
411 | - |
|
412 | - /** |
|
413 | - * Get the date that the subscription will renew. |
|
414 | - * |
|
415 | - * @since 1.0.19 |
|
416 | - * @param string $context View or edit context. |
|
417 | - * @return string |
|
418 | - */ |
|
419 | - public function get_next_renewal_date( $context = 'view' ) { |
|
420 | - return $this->get_prop( 'expiration', $context ); |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Alias for self::get_next_renewal_date(). |
|
425 | - * |
|
426 | - * @since 1.0.19 |
|
427 | - * @param string $context View or edit context. |
|
428 | - * @return string |
|
429 | - */ |
|
430 | - public function get_expiration( $context = 'view' ) { |
|
431 | - return $this->get_next_renewal_date( $context ); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Retrieves the expiration date in a timestamp |
|
436 | - * |
|
437 | - * @since 1.0.0 |
|
438 | - * @return int |
|
439 | - */ |
|
440 | - public function get_expiration_time() { |
|
441 | - $expiration = $this->get_expiration(); |
|
442 | - |
|
443 | - if ( empty( $expiration ) || '0000-00-00 00:00:00' == $expiration ) { |
|
444 | - return current_time( 'timestamp' ); |
|
445 | - } |
|
446 | - |
|
447 | - $expiration = strtotime( $expiration, current_time( 'timestamp' ) ); |
|
448 | - return $expiration < current_time( 'timestamp' ) ? current_time( 'timestamp' ) : $expiration; |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * Get GMT date when the subscription will renew. |
|
453 | - * |
|
454 | - * @since 1.0.19 |
|
455 | - * @param string $context View or edit context. |
|
456 | - * @return string |
|
457 | - */ |
|
458 | - public function get_next_renewal_date_gmt( $context = 'view' ) { |
|
409 | + return $date; |
|
410 | + } |
|
411 | + |
|
412 | + /** |
|
413 | + * Get the date that the subscription will renew. |
|
414 | + * |
|
415 | + * @since 1.0.19 |
|
416 | + * @param string $context View or edit context. |
|
417 | + * @return string |
|
418 | + */ |
|
419 | + public function get_next_renewal_date( $context = 'view' ) { |
|
420 | + return $this->get_prop( 'expiration', $context ); |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Alias for self::get_next_renewal_date(). |
|
425 | + * |
|
426 | + * @since 1.0.19 |
|
427 | + * @param string $context View or edit context. |
|
428 | + * @return string |
|
429 | + */ |
|
430 | + public function get_expiration( $context = 'view' ) { |
|
431 | + return $this->get_next_renewal_date( $context ); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Retrieves the expiration date in a timestamp |
|
436 | + * |
|
437 | + * @since 1.0.0 |
|
438 | + * @return int |
|
439 | + */ |
|
440 | + public function get_expiration_time() { |
|
441 | + $expiration = $this->get_expiration(); |
|
442 | + |
|
443 | + if ( empty( $expiration ) || '0000-00-00 00:00:00' == $expiration ) { |
|
444 | + return current_time( 'timestamp' ); |
|
445 | + } |
|
446 | + |
|
447 | + $expiration = strtotime( $expiration, current_time( 'timestamp' ) ); |
|
448 | + return $expiration < current_time( 'timestamp' ) ? current_time( 'timestamp' ) : $expiration; |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * Get GMT date when the subscription will renew. |
|
453 | + * |
|
454 | + * @since 1.0.19 |
|
455 | + * @param string $context View or edit context. |
|
456 | + * @return string |
|
457 | + */ |
|
458 | + public function get_next_renewal_date_gmt( $context = 'view' ) { |
|
459 | 459 | $date = $this->get_next_renewal_date( $context ); |
460 | 460 | |
461 | 461 | if ( $date ) { |
462 | 462 | $date = get_gmt_from_date( $date ); |
463 | 463 | } |
464 | - return $date; |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * Get the subscription's trial period. |
|
469 | - * |
|
470 | - * @since 1.0.19 |
|
471 | - * @param string $context View or edit context. |
|
472 | - * @return string |
|
473 | - */ |
|
474 | - public function get_trial_period( $context = 'view' ) { |
|
475 | - return $this->get_prop( 'trial_period', $context ); |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Get the subscription's status. |
|
480 | - * |
|
481 | - * @since 1.0.19 |
|
482 | - * @param string $context View or edit context. |
|
483 | - * @return string |
|
484 | - */ |
|
485 | - public function get_status( $context = 'view' ) { |
|
486 | - return $this->get_prop( 'status', $context ); |
|
487 | - } |
|
488 | - |
|
489 | - /** |
|
490 | - * Get the subscription's profile id. |
|
491 | - * |
|
492 | - * @since 1.0.19 |
|
493 | - * @param string $context View or edit context. |
|
494 | - * @return string |
|
495 | - */ |
|
496 | - public function get_profile_id( $context = 'view' ) { |
|
497 | - return $this->get_prop( 'profile_id', $context ); |
|
498 | - } |
|
499 | - |
|
500 | - /* |
|
464 | + return $date; |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * Get the subscription's trial period. |
|
469 | + * |
|
470 | + * @since 1.0.19 |
|
471 | + * @param string $context View or edit context. |
|
472 | + * @return string |
|
473 | + */ |
|
474 | + public function get_trial_period( $context = 'view' ) { |
|
475 | + return $this->get_prop( 'trial_period', $context ); |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Get the subscription's status. |
|
480 | + * |
|
481 | + * @since 1.0.19 |
|
482 | + * @param string $context View or edit context. |
|
483 | + * @return string |
|
484 | + */ |
|
485 | + public function get_status( $context = 'view' ) { |
|
486 | + return $this->get_prop( 'status', $context ); |
|
487 | + } |
|
488 | + |
|
489 | + /** |
|
490 | + * Get the subscription's profile id. |
|
491 | + * |
|
492 | + * @since 1.0.19 |
|
493 | + * @param string $context View or edit context. |
|
494 | + * @return string |
|
495 | + */ |
|
496 | + public function get_profile_id( $context = 'view' ) { |
|
497 | + return $this->get_prop( 'profile_id', $context ); |
|
498 | + } |
|
499 | + |
|
500 | + /* |
|
501 | 501 | |-------------------------------------------------------------------------- |
502 | 502 | | Setters |
503 | 503 | |-------------------------------------------------------------------------- |
504 | 504 | */ |
505 | 505 | |
506 | - /** |
|
507 | - * Set customer id. |
|
508 | - * |
|
509 | - * @since 1.0.19 |
|
510 | - * @param int $value The customer's id. |
|
511 | - */ |
|
512 | - public function set_customer_id( $value ) { |
|
513 | - $this->set_prop( 'customer_id', (int) $value ); |
|
514 | - } |
|
515 | - |
|
516 | - /** |
|
517 | - * Set parent invoice id. |
|
518 | - * |
|
519 | - * @since 1.0.19 |
|
520 | - * @param int $value The parent invoice id. |
|
521 | - */ |
|
522 | - public function set_parent_invoice_id( $value ) { |
|
523 | - $this->set_prop( 'parent_payment_id', (int) $value ); |
|
524 | - } |
|
525 | - |
|
526 | - /** |
|
527 | - * Alias for self::set_parent_invoice_id(). |
|
528 | - * |
|
529 | - * @since 1.0.19 |
|
530 | - * @param int $value The parent invoice id. |
|
531 | - */ |
|
506 | + /** |
|
507 | + * Set customer id. |
|
508 | + * |
|
509 | + * @since 1.0.19 |
|
510 | + * @param int $value The customer's id. |
|
511 | + */ |
|
512 | + public function set_customer_id( $value ) { |
|
513 | + $this->set_prop( 'customer_id', (int) $value ); |
|
514 | + } |
|
515 | + |
|
516 | + /** |
|
517 | + * Set parent invoice id. |
|
518 | + * |
|
519 | + * @since 1.0.19 |
|
520 | + * @param int $value The parent invoice id. |
|
521 | + */ |
|
522 | + public function set_parent_invoice_id( $value ) { |
|
523 | + $this->set_prop( 'parent_payment_id', (int) $value ); |
|
524 | + } |
|
525 | + |
|
526 | + /** |
|
527 | + * Alias for self::set_parent_invoice_id(). |
|
528 | + * |
|
529 | + * @since 1.0.19 |
|
530 | + * @param int $value The parent invoice id. |
|
531 | + */ |
|
532 | 532 | public function set_parent_payment_id( $value ) { |
533 | 533 | $this->set_parent_invoice_id( $value ); |
534 | - } |
|
534 | + } |
|
535 | 535 | |
536 | - /** |
|
536 | + /** |
|
537 | 537 | * Alias for self::set_parent_invoice_id(). |
538 | 538 | * |
539 | 539 | * @since 1.0.19 |
540 | - * @param int $value The parent invoice id. |
|
540 | + * @param int $value The parent invoice id. |
|
541 | 541 | */ |
542 | 542 | public function set_original_payment_id( $value ) { |
543 | 543 | $this->set_parent_invoice_id( $value ); |
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * Set subscription's product id. |
|
548 | - * |
|
549 | - * @since 1.0.19 |
|
550 | - * @param int $value The subscription product id. |
|
551 | - */ |
|
552 | - public function set_product_id( $value ) { |
|
553 | - $this->set_prop( 'product_id', (int) $value ); |
|
554 | - } |
|
555 | - |
|
556 | - /** |
|
557 | - * Set the period of a renewal. |
|
558 | - * |
|
559 | - * @since 1.0.19 |
|
560 | - * @param string $value The renewal period. |
|
561 | - */ |
|
562 | - public function set_period( $value ) { |
|
563 | - $this->set_prop( 'period', $value ); |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Set number of periods each renewal is valid for. |
|
568 | - * |
|
569 | - * @since 1.0.19 |
|
570 | - * @param int $value The subscription frequency. |
|
571 | - */ |
|
572 | - public function set_frequency( $value ) { |
|
573 | - $value = empty( $value ) ? 1 : (int) $value; |
|
574 | - $this->set_prop( 'frequency', absint( $value ) ); |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * Set the initial amount for the subscription. |
|
579 | - * |
|
580 | - * @since 1.0.19 |
|
581 | - * @param float $value The initial subcription amount. |
|
582 | - */ |
|
583 | - public function set_initial_amount( $value ) { |
|
584 | - $this->set_prop( 'initial_amount', wpinv_sanitize_amount( $value ) ); |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * Set the recurring amount for the subscription. |
|
589 | - * |
|
590 | - * @since 1.0.19 |
|
591 | - * @param float $value The recurring subcription amount. |
|
592 | - */ |
|
593 | - public function set_recurring_amount( $value ) { |
|
594 | - $this->set_prop( 'recurring_amount', wpinv_sanitize_amount( $value ) ); |
|
595 | - } |
|
596 | - |
|
597 | - /** |
|
598 | - * Set number of times that this subscription can be renewed. |
|
599 | - * |
|
600 | - * @since 1.0.19 |
|
601 | - * @param int $value Bill times. |
|
602 | - */ |
|
603 | - public function set_bill_times( $value ) { |
|
604 | - $this->set_prop( 'bill_times', (int) $value ); |
|
605 | - } |
|
606 | - |
|
607 | - /** |
|
608 | - * Get transaction id of this subscription's parent invoice. |
|
609 | - * |
|
610 | - * @since 1.0.19 |
|
611 | - * @param string $value Bill times. |
|
612 | - */ |
|
613 | - public function set_transaction_id( $value ) { |
|
614 | - $this->set_prop( 'transaction_id', sanitize_text_field( $value ) ); |
|
615 | - } |
|
616 | - |
|
617 | - /** |
|
618 | - * Set date when this subscription started. |
|
619 | - * |
|
620 | - * @since 1.0.19 |
|
621 | - * @param string $value strtotime compliant date. |
|
622 | - */ |
|
623 | - public function set_created( $value ) { |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * Set subscription's product id. |
|
548 | + * |
|
549 | + * @since 1.0.19 |
|
550 | + * @param int $value The subscription product id. |
|
551 | + */ |
|
552 | + public function set_product_id( $value ) { |
|
553 | + $this->set_prop( 'product_id', (int) $value ); |
|
554 | + } |
|
555 | + |
|
556 | + /** |
|
557 | + * Set the period of a renewal. |
|
558 | + * |
|
559 | + * @since 1.0.19 |
|
560 | + * @param string $value The renewal period. |
|
561 | + */ |
|
562 | + public function set_period( $value ) { |
|
563 | + $this->set_prop( 'period', $value ); |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Set number of periods each renewal is valid for. |
|
568 | + * |
|
569 | + * @since 1.0.19 |
|
570 | + * @param int $value The subscription frequency. |
|
571 | + */ |
|
572 | + public function set_frequency( $value ) { |
|
573 | + $value = empty( $value ) ? 1 : (int) $value; |
|
574 | + $this->set_prop( 'frequency', absint( $value ) ); |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * Set the initial amount for the subscription. |
|
579 | + * |
|
580 | + * @since 1.0.19 |
|
581 | + * @param float $value The initial subcription amount. |
|
582 | + */ |
|
583 | + public function set_initial_amount( $value ) { |
|
584 | + $this->set_prop( 'initial_amount', wpinv_sanitize_amount( $value ) ); |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * Set the recurring amount for the subscription. |
|
589 | + * |
|
590 | + * @since 1.0.19 |
|
591 | + * @param float $value The recurring subcription amount. |
|
592 | + */ |
|
593 | + public function set_recurring_amount( $value ) { |
|
594 | + $this->set_prop( 'recurring_amount', wpinv_sanitize_amount( $value ) ); |
|
595 | + } |
|
596 | + |
|
597 | + /** |
|
598 | + * Set number of times that this subscription can be renewed. |
|
599 | + * |
|
600 | + * @since 1.0.19 |
|
601 | + * @param int $value Bill times. |
|
602 | + */ |
|
603 | + public function set_bill_times( $value ) { |
|
604 | + $this->set_prop( 'bill_times', (int) $value ); |
|
605 | + } |
|
606 | + |
|
607 | + /** |
|
608 | + * Get transaction id of this subscription's parent invoice. |
|
609 | + * |
|
610 | + * @since 1.0.19 |
|
611 | + * @param string $value Bill times. |
|
612 | + */ |
|
613 | + public function set_transaction_id( $value ) { |
|
614 | + $this->set_prop( 'transaction_id', sanitize_text_field( $value ) ); |
|
615 | + } |
|
616 | + |
|
617 | + /** |
|
618 | + * Set date when this subscription started. |
|
619 | + * |
|
620 | + * @since 1.0.19 |
|
621 | + * @param string $value strtotime compliant date. |
|
622 | + */ |
|
623 | + public function set_created( $value ) { |
|
624 | 624 | $date = strtotime( $value ); |
625 | 625 | |
626 | 626 | if ( $date && $value !== '0000-00-00 00:00:00' ) { |
@@ -628,94 +628,94 @@ discard block |
||
628 | 628 | return; |
629 | 629 | } |
630 | 630 | |
631 | - $this->set_prop( 'created', '' ); |
|
631 | + $this->set_prop( 'created', '' ); |
|
632 | 632 | |
633 | - } |
|
633 | + } |
|
634 | 634 | |
635 | - /** |
|
636 | - * Alias for self::set_created(). |
|
637 | - * |
|
638 | - * @since 1.0.19 |
|
639 | - * @param string $value strtotime compliant date. |
|
640 | - */ |
|
641 | - public function set_date_created( $value ) { |
|
642 | - $this->set_created( $value ); |
|
635 | + /** |
|
636 | + * Alias for self::set_created(). |
|
637 | + * |
|
638 | + * @since 1.0.19 |
|
639 | + * @param string $value strtotime compliant date. |
|
640 | + */ |
|
641 | + public function set_date_created( $value ) { |
|
642 | + $this->set_created( $value ); |
|
643 | 643 | } |
644 | 644 | |
645 | - /** |
|
646 | - * Set the date that the subscription will renew. |
|
647 | - * |
|
648 | - * @since 1.0.19 |
|
649 | - * @param string $value strtotime compliant date. |
|
650 | - */ |
|
651 | - public function set_next_renewal_date( $value ) { |
|
652 | - $date = strtotime( $value ); |
|
645 | + /** |
|
646 | + * Set the date that the subscription will renew. |
|
647 | + * |
|
648 | + * @since 1.0.19 |
|
649 | + * @param string $value strtotime compliant date. |
|
650 | + */ |
|
651 | + public function set_next_renewal_date( $value ) { |
|
652 | + $date = strtotime( $value ); |
|
653 | 653 | |
654 | 654 | if ( $date && $value !== '0000-00-00 00:00:00' ) { |
655 | 655 | $this->set_prop( 'expiration', date( 'Y-m-d H:i:s', $date ) ); |
656 | 656 | return; |
657 | - } |
|
658 | - |
|
659 | - $this->set_prop( 'expiration', '' ); |
|
660 | - |
|
661 | - } |
|
662 | - |
|
663 | - /** |
|
664 | - * Alias for self::set_next_renewal_date(). |
|
665 | - * |
|
666 | - * @since 1.0.19 |
|
667 | - * @param string $value strtotime compliant date. |
|
668 | - */ |
|
669 | - public function set_expiration( $value ) { |
|
670 | - $this->set_next_renewal_date( $value ); |
|
671 | - } |
|
672 | - |
|
673 | - /** |
|
674 | - * Set the subscription's trial period. |
|
675 | - * |
|
676 | - * @since 1.0.19 |
|
677 | - * @param string $value trial period e.g 1 year. |
|
678 | - */ |
|
679 | - public function set_trial_period( $value ) { |
|
680 | - $this->set_prop( 'trial_period', $value ); |
|
681 | - } |
|
682 | - |
|
683 | - /** |
|
684 | - * Set the subscription's status. |
|
685 | - * |
|
686 | - * @since 1.0.19 |
|
687 | - * @param string $new_status New subscription status. |
|
688 | - */ |
|
689 | - public function set_status( $new_status ) { |
|
690 | - |
|
691 | - // Abort if this is not a valid status; |
|
692 | - if ( ! array_key_exists( $new_status, getpaid_get_subscription_statuses() ) ) { |
|
693 | - return; |
|
694 | - } |
|
695 | - |
|
696 | - $old_status = $this->get_status(); |
|
697 | - $this->set_prop( 'status', $new_status ); |
|
698 | - |
|
699 | - if ( true === $this->object_read && $old_status !== $new_status ) { |
|
700 | - $this->status_transition = array( |
|
701 | - 'from' => ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $old_status, |
|
702 | - 'to' => $new_status, |
|
703 | - ); |
|
704 | - } |
|
705 | - |
|
706 | - } |
|
707 | - |
|
708 | - /** |
|
709 | - * Set the subscription's (remote) profile id. |
|
710 | - * |
|
711 | - * @since 1.0.19 |
|
712 | - * @param string $value the remote profile id. |
|
713 | - */ |
|
714 | - public function set_profile_id( $value ) { |
|
715 | - $this->set_prop( 'profile_id', sanitize_text_field( $value ) ); |
|
716 | - } |
|
717 | - |
|
718 | - /* |
|
657 | + } |
|
658 | + |
|
659 | + $this->set_prop( 'expiration', '' ); |
|
660 | + |
|
661 | + } |
|
662 | + |
|
663 | + /** |
|
664 | + * Alias for self::set_next_renewal_date(). |
|
665 | + * |
|
666 | + * @since 1.0.19 |
|
667 | + * @param string $value strtotime compliant date. |
|
668 | + */ |
|
669 | + public function set_expiration( $value ) { |
|
670 | + $this->set_next_renewal_date( $value ); |
|
671 | + } |
|
672 | + |
|
673 | + /** |
|
674 | + * Set the subscription's trial period. |
|
675 | + * |
|
676 | + * @since 1.0.19 |
|
677 | + * @param string $value trial period e.g 1 year. |
|
678 | + */ |
|
679 | + public function set_trial_period( $value ) { |
|
680 | + $this->set_prop( 'trial_period', $value ); |
|
681 | + } |
|
682 | + |
|
683 | + /** |
|
684 | + * Set the subscription's status. |
|
685 | + * |
|
686 | + * @since 1.0.19 |
|
687 | + * @param string $new_status New subscription status. |
|
688 | + */ |
|
689 | + public function set_status( $new_status ) { |
|
690 | + |
|
691 | + // Abort if this is not a valid status; |
|
692 | + if ( ! array_key_exists( $new_status, getpaid_get_subscription_statuses() ) ) { |
|
693 | + return; |
|
694 | + } |
|
695 | + |
|
696 | + $old_status = $this->get_status(); |
|
697 | + $this->set_prop( 'status', $new_status ); |
|
698 | + |
|
699 | + if ( true === $this->object_read && $old_status !== $new_status ) { |
|
700 | + $this->status_transition = array( |
|
701 | + 'from' => ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $old_status, |
|
702 | + 'to' => $new_status, |
|
703 | + ); |
|
704 | + } |
|
705 | + |
|
706 | + } |
|
707 | + |
|
708 | + /** |
|
709 | + * Set the subscription's (remote) profile id. |
|
710 | + * |
|
711 | + * @since 1.0.19 |
|
712 | + * @param string $value the remote profile id. |
|
713 | + */ |
|
714 | + public function set_profile_id( $value ) { |
|
715 | + $this->set_prop( 'profile_id', sanitize_text_field( $value ) ); |
|
716 | + } |
|
717 | + |
|
718 | + /* |
|
719 | 719 | |-------------------------------------------------------------------------- |
720 | 720 | | Boolean methods |
721 | 721 | |-------------------------------------------------------------------------- |
@@ -724,55 +724,55 @@ discard block |
||
724 | 724 | | |
725 | 725 | */ |
726 | 726 | |
727 | - /** |
|
727 | + /** |
|
728 | 728 | * Checks if the subscription has a given status. |
729 | - * |
|
730 | - * @param string|array String or array of strings to check for. |
|
731 | - * @return bool |
|
729 | + * |
|
730 | + * @param string|array String or array of strings to check for. |
|
731 | + * @return bool |
|
732 | 732 | */ |
733 | 733 | public function has_status( $status ) { |
734 | 734 | return in_array( $this->get_status(), wpinv_clean( wpinv_parse_list( $status ) ) ); |
735 | - } |
|
735 | + } |
|
736 | 736 | |
737 | - /** |
|
737 | + /** |
|
738 | 738 | * Checks if the subscription has a trial period. |
739 | - * |
|
740 | - * @return bool |
|
739 | + * |
|
740 | + * @return bool |
|
741 | 741 | */ |
742 | 742 | public function has_trial_period() { |
743 | - $period = $this->get_trial_period(); |
|
743 | + $period = $this->get_trial_period(); |
|
744 | 744 | return ! empty( $period ); |
745 | - } |
|
746 | - |
|
747 | - /** |
|
748 | - * Is the subscription active? |
|
749 | - * |
|
750 | - * @return bool |
|
751 | - */ |
|
752 | - public function is_active() { |
|
753 | - return $this->has_status( 'active trialling' ) && ! $this->is_expired(); |
|
754 | - } |
|
755 | - |
|
756 | - /** |
|
757 | - * Is the subscription expired? |
|
758 | - * |
|
759 | - * @return bool |
|
760 | - */ |
|
761 | - public function is_expired() { |
|
762 | - return $this->has_status( 'expired' ) || ( $this->has_status( 'active cancelled trialling' ) && $this->get_expiration_time() < current_time( 'mysql' ) ); |
|
763 | - } |
|
764 | - |
|
765 | - /** |
|
766 | - * Is this the last renewals? |
|
767 | - * |
|
768 | - * @return bool |
|
769 | - */ |
|
770 | - public function is_last_renewal() { |
|
771 | - $max_bills = $this->get_bill_times(); |
|
772 | - return ! empty( $max_bills ) && $max_bills == $this->get_times_billed(); |
|
773 | - } |
|
774 | - |
|
775 | - /* |
|
745 | + } |
|
746 | + |
|
747 | + /** |
|
748 | + * Is the subscription active? |
|
749 | + * |
|
750 | + * @return bool |
|
751 | + */ |
|
752 | + public function is_active() { |
|
753 | + return $this->has_status( 'active trialling' ) && ! $this->is_expired(); |
|
754 | + } |
|
755 | + |
|
756 | + /** |
|
757 | + * Is the subscription expired? |
|
758 | + * |
|
759 | + * @return bool |
|
760 | + */ |
|
761 | + public function is_expired() { |
|
762 | + return $this->has_status( 'expired' ) || ( $this->has_status( 'active cancelled trialling' ) && $this->get_expiration_time() < current_time( 'mysql' ) ); |
|
763 | + } |
|
764 | + |
|
765 | + /** |
|
766 | + * Is this the last renewals? |
|
767 | + * |
|
768 | + * @return bool |
|
769 | + */ |
|
770 | + public function is_last_renewal() { |
|
771 | + $max_bills = $this->get_bill_times(); |
|
772 | + return ! empty( $max_bills ) && $max_bills == $this->get_times_billed(); |
|
773 | + } |
|
774 | + |
|
775 | + /* |
|
776 | 776 | |-------------------------------------------------------------------------- |
777 | 777 | | Additional methods |
778 | 778 | |-------------------------------------------------------------------------- |
@@ -781,27 +781,27 @@ discard block |
||
781 | 781 | | |
782 | 782 | */ |
783 | 783 | |
784 | - /** |
|
785 | - * Backwards compatibilty. |
|
786 | - */ |
|
787 | - public function create( $data = array() ) { |
|
784 | + /** |
|
785 | + * Backwards compatibilty. |
|
786 | + */ |
|
787 | + public function create( $data = array() ) { |
|
788 | 788 | |
789 | - // Set the properties. |
|
790 | - if ( is_array( $data ) ) { |
|
791 | - $this->set_props( $data ); |
|
792 | - } |
|
789 | + // Set the properties. |
|
790 | + if ( is_array( $data ) ) { |
|
791 | + $this->set_props( $data ); |
|
792 | + } |
|
793 | 793 | |
794 | - // Save the item. |
|
795 | - return $this->save(); |
|
794 | + // Save the item. |
|
795 | + return $this->save(); |
|
796 | 796 | |
797 | - } |
|
797 | + } |
|
798 | 798 | |
799 | - /** |
|
800 | - * Backwards compatibilty. |
|
801 | - */ |
|
802 | - public function update( $args = array() ) { |
|
803 | - return $this->create( $args ); |
|
804 | - } |
|
799 | + /** |
|
800 | + * Backwards compatibilty. |
|
801 | + */ |
|
802 | + public function update( $args = array() ) { |
|
803 | + return $this->create( $args ); |
|
804 | + } |
|
805 | 805 | |
806 | 806 | /** |
807 | 807 | * Retrieve renewal payments for a subscription |
@@ -811,15 +811,15 @@ discard block |
||
811 | 811 | */ |
812 | 812 | public function get_child_payments() { |
813 | 813 | return get_posts( |
814 | - array( |
|
815 | - 'post_parent' => $this->get_parent_payment_id(), |
|
816 | - 'numberposts' => -1, |
|
817 | - 'post_status' => array( 'publish', 'wpi-processing', 'wpi-renewal' ), |
|
818 | - 'orderby' => 'ID', |
|
819 | - 'order' => 'DESC', |
|
820 | - 'post_type' => 'wpi_invoice' |
|
821 | - ) |
|
822 | - ); |
|
814 | + array( |
|
815 | + 'post_parent' => $this->get_parent_payment_id(), |
|
816 | + 'numberposts' => -1, |
|
817 | + 'post_status' => array( 'publish', 'wpi-processing', 'wpi-renewal' ), |
|
818 | + 'orderby' => 'ID', |
|
819 | + 'order' => 'DESC', |
|
820 | + 'post_type' => 'wpi_invoice' |
|
821 | + ) |
|
822 | + ); |
|
823 | 823 | } |
824 | 824 | |
825 | 825 | /** |
@@ -829,16 +829,16 @@ discard block |
||
829 | 829 | * @return int |
830 | 830 | */ |
831 | 831 | public function get_total_payments() { |
832 | - global $wpdb; |
|
832 | + global $wpdb; |
|
833 | 833 | |
834 | - $count = (int) $wpdb->get_var( |
|
835 | - $wpdb->prepare( |
|
836 | - "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent=%d AND post_status IN ( 'publish', 'wpi-processing', 'wpi-renewal' )", |
|
837 | - $this->get_parent_invoice_id() |
|
838 | - ) |
|
839 | - ); |
|
834 | + $count = (int) $wpdb->get_var( |
|
835 | + $wpdb->prepare( |
|
836 | + "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent=%d AND post_status IN ( 'publish', 'wpi-processing', 'wpi-renewal' )", |
|
837 | + $this->get_parent_invoice_id() |
|
838 | + ) |
|
839 | + ); |
|
840 | 840 | |
841 | - // Maybe include parent invoice. |
|
841 | + // Maybe include parent invoice. |
|
842 | 842 | if ( ! $this->has_status( 'pending' ) ) { |
843 | 843 | $count++; |
844 | 844 | } |
@@ -867,57 +867,57 @@ discard block |
||
867 | 867 | * |
868 | 868 | * @since 2.4 |
869 | 869 | * @param array $args Array of values for the payment, including amount and transaction ID |
870 | - * @param WPInv_Invoice $invoice If adding an existing invoice. |
|
870 | + * @param WPInv_Invoice $invoice If adding an existing invoice. |
|
871 | 871 | * @return bool |
872 | 872 | */ |
873 | 873 | public function add_payment( $args = array(), $invoice = false ) { |
874 | 874 | |
875 | - // Process each payment once. |
|
875 | + // Process each payment once. |
|
876 | 876 | if ( ! empty( $args['transaction_id'] ) && $this->payment_exists( $args['transaction_id'] ) ) { |
877 | 877 | return false; |
878 | 878 | } |
879 | 879 | |
880 | - // Are we creating a new invoice? |
|
881 | - if ( empty( $invoice ) ) { |
|
882 | - $invoice = $this->create_payment(); |
|
880 | + // Are we creating a new invoice? |
|
881 | + if ( empty( $invoice ) ) { |
|
882 | + $invoice = $this->create_payment(); |
|
883 | 883 | |
884 | - if ( empty( $invoice ) ) { |
|
885 | - return false; |
|
886 | - } |
|
884 | + if ( empty( $invoice ) ) { |
|
885 | + return false; |
|
886 | + } |
|
887 | 887 | |
888 | - $invoice->set_status( 'wpi-renewal' ); |
|
888 | + $invoice->set_status( 'wpi-renewal' ); |
|
889 | 889 | |
890 | - } |
|
890 | + } |
|
891 | 891 | |
892 | - // Maybe set a transaction id. |
|
893 | - if ( ! empty( $args['transaction_id'] ) ) { |
|
894 | - $invoice->set_transaction_id( $args['transaction_id'] ); |
|
895 | - } |
|
892 | + // Maybe set a transaction id. |
|
893 | + if ( ! empty( $args['transaction_id'] ) ) { |
|
894 | + $invoice->set_transaction_id( $args['transaction_id'] ); |
|
895 | + } |
|
896 | 896 | |
897 | - // Set the completed date. |
|
898 | - $invoice->set_completed_date( current_time( 'mysql' ) ); |
|
897 | + // Set the completed date. |
|
898 | + $invoice->set_completed_date( current_time( 'mysql' ) ); |
|
899 | 899 | |
900 | - // And the gateway. |
|
901 | - if ( ! empty( $args['gateway'] ) ) { |
|
902 | - $invoice->set_gateway( $args['gateway'] ); |
|
903 | - } |
|
900 | + // And the gateway. |
|
901 | + if ( ! empty( $args['gateway'] ) ) { |
|
902 | + $invoice->set_gateway( $args['gateway'] ); |
|
903 | + } |
|
904 | 904 | |
905 | - $invoice->save(); |
|
905 | + $invoice->save(); |
|
906 | 906 | |
907 | - if ( ! $invoice->get_id() ) { |
|
908 | - return 0; |
|
909 | - } |
|
907 | + if ( ! $invoice->get_id() ) { |
|
908 | + return 0; |
|
909 | + } |
|
910 | 910 | |
911 | - do_action( 'getpaid_after_create_subscription_renewal_invoice', $invoice, $this ); |
|
912 | - do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this ); |
|
911 | + do_action( 'getpaid_after_create_subscription_renewal_invoice', $invoice, $this ); |
|
912 | + do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this ); |
|
913 | 913 | do_action( 'wpinv_recurring_record_payment', $invoice->get_id(), $this->get_parent_invoice_id(), $invoice->get_recurring_total(), $invoice->get_transaction_id() ); |
914 | 914 | |
915 | 915 | update_post_meta( $invoice->get_id(), '_wpinv_subscription_id', $this->id ); |
916 | 916 | |
917 | 917 | return $invoice->get_id(); |
918 | - } |
|
918 | + } |
|
919 | 919 | |
920 | - /** |
|
920 | + /** |
|
921 | 921 | * Creates a new invoice and returns it. |
922 | 922 | * |
923 | 923 | * @since 1.0.19 |
@@ -925,104 +925,104 @@ discard block |
||
925 | 925 | */ |
926 | 926 | public function create_payment() { |
927 | 927 | |
928 | - $parent_invoice = $this->get_parent_payment(); |
|
929 | - |
|
930 | - if ( ! $parent_invoice->get_id() ) { |
|
931 | - return false; |
|
932 | - } |
|
933 | - |
|
934 | - // Duplicate the parent invoice. |
|
935 | - $invoice = new WPInv_Invoice(); |
|
936 | - $invoice->set_props( $parent_invoice->get_data() ); |
|
937 | - $invoice->set_id( 0 ); |
|
938 | - $invoice->set_items( $parent_invoice->get_items() ); |
|
939 | - $invoice->set_parent_id( $parent_invoice->get_id() ); |
|
940 | - $invoice->set_transaction_id( '' ); |
|
941 | - $invoice->set_key( $invoice->generate_key( 'renewal_' ) ); |
|
942 | - $invoice->set_number( '' ); |
|
943 | - $invoice->set_completed_date( '' ); |
|
944 | - $invoice->set_status( 'wpi-pending' ); |
|
945 | - $invoice->recalculate_total(); |
|
946 | - $invoice->save(); |
|
947 | - |
|
948 | - return $invoice->get_id() ? $invoice : false; |
|
949 | - } |
|
950 | - |
|
951 | - /** |
|
952 | - * Renews or completes a subscription |
|
953 | - * |
|
954 | - * @since 1.0.0 |
|
955 | - * @return int The subscription's id |
|
956 | - */ |
|
957 | - public function renew() { |
|
958 | - |
|
959 | - // Complete subscription if applicable |
|
960 | - if ( $this->get_bill_times() > 0 && $this->get_times_billed() >= $this->get_bill_times() ) { |
|
961 | - return $this->complete(); |
|
962 | - } |
|
963 | - |
|
964 | - // Calculate new expiration |
|
965 | - $frequency = $this->get_frequency(); |
|
966 | - $period = $this->get_period(); |
|
967 | - $new_expiration = strtotime( "+ $frequency $period", $this->get_expiration_time() ); |
|
968 | - |
|
969 | - $this->set_expiration( date( 'Y-m-d H:i:s',$new_expiration ) ); |
|
970 | - $this->set_status( 'active' ); |
|
971 | - return $this->save(); |
|
972 | - |
|
973 | - do_action( 'getpaid_subscription_renewed', $this ); |
|
974 | - |
|
975 | - } |
|
976 | - |
|
977 | - /** |
|
978 | - * Marks a subscription as completed |
|
979 | - * |
|
980 | - * Subscription is completed when the number of payments matches the billing_times field |
|
981 | - * |
|
982 | - * @since 1.0.0 |
|
983 | - * @return int|bool Subscription id or false if the subscription is cancelled. |
|
984 | - */ |
|
985 | - public function complete() { |
|
986 | - |
|
987 | - // Only mark a subscription as complete if it's not already cancelled. |
|
988 | - if ( $this->has_status( 'cancelled' ) ) { |
|
989 | - return false; |
|
990 | - } |
|
991 | - |
|
992 | - $this->set_status( 'completed' ); |
|
993 | - return $this->save(); |
|
994 | - |
|
995 | - } |
|
996 | - |
|
997 | - /** |
|
998 | - * Marks a subscription as expired |
|
999 | - * |
|
1000 | - * @since 1.0.0 |
|
1001 | - * @param bool $check_expiration |
|
1002 | - * @return int|bool Subscription id or false if $check_expiration is true and expiration date is in the future. |
|
1003 | - */ |
|
1004 | - public function expire( $check_expiration = false ) { |
|
1005 | - |
|
1006 | - if ( $check_expiration && $this->get_expiration_time() > current_time( 'timestamp' ) ) { |
|
1007 | - // Do not mark as expired since real expiration date is in the future |
|
1008 | - return false; |
|
1009 | - } |
|
1010 | - |
|
1011 | - $this->set_status( 'expired' ); |
|
1012 | - return $this->save(); |
|
1013 | - |
|
1014 | - } |
|
1015 | - |
|
1016 | - /** |
|
1017 | - * Marks a subscription as failing |
|
1018 | - * |
|
1019 | - * @since 2.4.2 |
|
1020 | - * @return int Subscription id. |
|
1021 | - */ |
|
1022 | - public function failing() { |
|
1023 | - $this->set_status( 'failing' ); |
|
1024 | - return $this->save(); |
|
1025 | - } |
|
928 | + $parent_invoice = $this->get_parent_payment(); |
|
929 | + |
|
930 | + if ( ! $parent_invoice->get_id() ) { |
|
931 | + return false; |
|
932 | + } |
|
933 | + |
|
934 | + // Duplicate the parent invoice. |
|
935 | + $invoice = new WPInv_Invoice(); |
|
936 | + $invoice->set_props( $parent_invoice->get_data() ); |
|
937 | + $invoice->set_id( 0 ); |
|
938 | + $invoice->set_items( $parent_invoice->get_items() ); |
|
939 | + $invoice->set_parent_id( $parent_invoice->get_id() ); |
|
940 | + $invoice->set_transaction_id( '' ); |
|
941 | + $invoice->set_key( $invoice->generate_key( 'renewal_' ) ); |
|
942 | + $invoice->set_number( '' ); |
|
943 | + $invoice->set_completed_date( '' ); |
|
944 | + $invoice->set_status( 'wpi-pending' ); |
|
945 | + $invoice->recalculate_total(); |
|
946 | + $invoice->save(); |
|
947 | + |
|
948 | + return $invoice->get_id() ? $invoice : false; |
|
949 | + } |
|
950 | + |
|
951 | + /** |
|
952 | + * Renews or completes a subscription |
|
953 | + * |
|
954 | + * @since 1.0.0 |
|
955 | + * @return int The subscription's id |
|
956 | + */ |
|
957 | + public function renew() { |
|
958 | + |
|
959 | + // Complete subscription if applicable |
|
960 | + if ( $this->get_bill_times() > 0 && $this->get_times_billed() >= $this->get_bill_times() ) { |
|
961 | + return $this->complete(); |
|
962 | + } |
|
963 | + |
|
964 | + // Calculate new expiration |
|
965 | + $frequency = $this->get_frequency(); |
|
966 | + $period = $this->get_period(); |
|
967 | + $new_expiration = strtotime( "+ $frequency $period", $this->get_expiration_time() ); |
|
968 | + |
|
969 | + $this->set_expiration( date( 'Y-m-d H:i:s',$new_expiration ) ); |
|
970 | + $this->set_status( 'active' ); |
|
971 | + return $this->save(); |
|
972 | + |
|
973 | + do_action( 'getpaid_subscription_renewed', $this ); |
|
974 | + |
|
975 | + } |
|
976 | + |
|
977 | + /** |
|
978 | + * Marks a subscription as completed |
|
979 | + * |
|
980 | + * Subscription is completed when the number of payments matches the billing_times field |
|
981 | + * |
|
982 | + * @since 1.0.0 |
|
983 | + * @return int|bool Subscription id or false if the subscription is cancelled. |
|
984 | + */ |
|
985 | + public function complete() { |
|
986 | + |
|
987 | + // Only mark a subscription as complete if it's not already cancelled. |
|
988 | + if ( $this->has_status( 'cancelled' ) ) { |
|
989 | + return false; |
|
990 | + } |
|
991 | + |
|
992 | + $this->set_status( 'completed' ); |
|
993 | + return $this->save(); |
|
994 | + |
|
995 | + } |
|
996 | + |
|
997 | + /** |
|
998 | + * Marks a subscription as expired |
|
999 | + * |
|
1000 | + * @since 1.0.0 |
|
1001 | + * @param bool $check_expiration |
|
1002 | + * @return int|bool Subscription id or false if $check_expiration is true and expiration date is in the future. |
|
1003 | + */ |
|
1004 | + public function expire( $check_expiration = false ) { |
|
1005 | + |
|
1006 | + if ( $check_expiration && $this->get_expiration_time() > current_time( 'timestamp' ) ) { |
|
1007 | + // Do not mark as expired since real expiration date is in the future |
|
1008 | + return false; |
|
1009 | + } |
|
1010 | + |
|
1011 | + $this->set_status( 'expired' ); |
|
1012 | + return $this->save(); |
|
1013 | + |
|
1014 | + } |
|
1015 | + |
|
1016 | + /** |
|
1017 | + * Marks a subscription as failing |
|
1018 | + * |
|
1019 | + * @since 2.4.2 |
|
1020 | + * @return int Subscription id. |
|
1021 | + */ |
|
1022 | + public function failing() { |
|
1023 | + $this->set_status( 'failing' ); |
|
1024 | + return $this->save(); |
|
1025 | + } |
|
1026 | 1026 | |
1027 | 1027 | /** |
1028 | 1028 | * Marks a subscription as cancelled |
@@ -1031,19 +1031,19 @@ discard block |
||
1031 | 1031 | * @return int Subscription id. |
1032 | 1032 | */ |
1033 | 1033 | public function cancel() { |
1034 | - $this->set_status( 'cancelled' ); |
|
1035 | - return $this->save(); |
|
1034 | + $this->set_status( 'cancelled' ); |
|
1035 | + return $this->save(); |
|
1036 | 1036 | } |
1037 | 1037 | |
1038 | - /** |
|
1039 | - * Determines if a subscription can be cancelled both locally and with a payment processor. |
|
1040 | - * |
|
1041 | - * @since 1.0.0 |
|
1042 | - * @return bool |
|
1043 | - */ |
|
1044 | - public function can_cancel() { |
|
1045 | - return apply_filters( 'wpinv_subscription_can_cancel', $this->has_status( $this->get_cancellable_statuses() ), $this ); |
|
1046 | - } |
|
1038 | + /** |
|
1039 | + * Determines if a subscription can be cancelled both locally and with a payment processor. |
|
1040 | + * |
|
1041 | + * @since 1.0.0 |
|
1042 | + * @return bool |
|
1043 | + */ |
|
1044 | + public function can_cancel() { |
|
1045 | + return apply_filters( 'wpinv_subscription_can_cancel', $this->has_status( $this->get_cancellable_statuses() ), $this ); |
|
1046 | + } |
|
1047 | 1047 | |
1048 | 1048 | /** |
1049 | 1049 | * Returns an array of subscription statuses that can be cancelled |
@@ -1056,82 +1056,82 @@ discard block |
||
1056 | 1056 | return apply_filters( 'wpinv_recurring_cancellable_statuses', array( 'active', 'trialling', 'failing' ) ); |
1057 | 1057 | } |
1058 | 1058 | |
1059 | - /** |
|
1060 | - * Retrieves the URL to cancel subscription |
|
1061 | - * |
|
1062 | - * @since 1.0.0 |
|
1063 | - * @return string |
|
1064 | - */ |
|
1065 | - public function get_cancel_url() { |
|
1066 | - $url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'subscription_cancel', 'sub_id' => $this->get_id() ) ), 'getpaid-nonce' ); |
|
1067 | - return apply_filters( 'wpinv_subscription_cancel_url', $url, $this ); |
|
1068 | - } |
|
1069 | - |
|
1070 | - /** |
|
1071 | - * Determines if subscription can be manually renewed |
|
1072 | - * |
|
1073 | - * This method is filtered by payment gateways in order to return true on subscriptions |
|
1074 | - * that can be renewed manually |
|
1075 | - * |
|
1076 | - * @since 2.5 |
|
1077 | - * @return bool |
|
1078 | - */ |
|
1079 | - public function can_renew() { |
|
1080 | - return apply_filters( 'wpinv_subscription_can_renew', true, $this ); |
|
1081 | - } |
|
1082 | - |
|
1083 | - /** |
|
1084 | - * Retrieves the URL to renew a subscription |
|
1085 | - * |
|
1086 | - * @since 2.5 |
|
1087 | - * @return string |
|
1088 | - */ |
|
1089 | - public function get_renew_url() { |
|
1090 | - $url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'renew_subscription', 'sub_id' => $this->get_id ) ), 'getpaid-nonce' ); |
|
1091 | - return apply_filters( 'wpinv_subscription_renew_url', $url, $this ); |
|
1092 | - } |
|
1093 | - |
|
1094 | - /** |
|
1095 | - * Determines if subscription can have their payment method updated |
|
1096 | - * |
|
1097 | - * @since 1.0.0 |
|
1098 | - * @return bool |
|
1099 | - */ |
|
1100 | - public function can_update() { |
|
1101 | - return apply_filters( 'wpinv_subscription_can_update', false, $this ); |
|
1102 | - } |
|
1103 | - |
|
1104 | - /** |
|
1105 | - * Retrieves the URL to update subscription |
|
1106 | - * |
|
1107 | - * @since 1.0.0 |
|
1108 | - * @return string |
|
1109 | - */ |
|
1110 | - public function get_update_url() { |
|
1111 | - $url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->get_id() ) ); |
|
1112 | - return apply_filters( 'wpinv_subscription_update_url', $url, $this ); |
|
1113 | - } |
|
1114 | - |
|
1115 | - /** |
|
1116 | - * Retrieves the subscription status label |
|
1117 | - * |
|
1118 | - * @since 1.0.0 |
|
1119 | - * @return string |
|
1120 | - */ |
|
1121 | - public function get_status_label() { |
|
1122 | - return getpaid_get_subscription_status_label( $this->get_status() ); |
|
1123 | - } |
|
1124 | - |
|
1125 | - /** |
|
1126 | - * Retrieves the subscription status class |
|
1127 | - * |
|
1128 | - * @since 1.0.19 |
|
1129 | - * @return string |
|
1130 | - */ |
|
1131 | - public function get_status_class() { |
|
1132 | - $statuses = getpaid_get_subscription_status_classes(); |
|
1133 | - return isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : 'text-white bg-secondary'; |
|
1134 | - } |
|
1059 | + /** |
|
1060 | + * Retrieves the URL to cancel subscription |
|
1061 | + * |
|
1062 | + * @since 1.0.0 |
|
1063 | + * @return string |
|
1064 | + */ |
|
1065 | + public function get_cancel_url() { |
|
1066 | + $url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'subscription_cancel', 'sub_id' => $this->get_id() ) ), 'getpaid-nonce' ); |
|
1067 | + return apply_filters( 'wpinv_subscription_cancel_url', $url, $this ); |
|
1068 | + } |
|
1069 | + |
|
1070 | + /** |
|
1071 | + * Determines if subscription can be manually renewed |
|
1072 | + * |
|
1073 | + * This method is filtered by payment gateways in order to return true on subscriptions |
|
1074 | + * that can be renewed manually |
|
1075 | + * |
|
1076 | + * @since 2.5 |
|
1077 | + * @return bool |
|
1078 | + */ |
|
1079 | + public function can_renew() { |
|
1080 | + return apply_filters( 'wpinv_subscription_can_renew', true, $this ); |
|
1081 | + } |
|
1082 | + |
|
1083 | + /** |
|
1084 | + * Retrieves the URL to renew a subscription |
|
1085 | + * |
|
1086 | + * @since 2.5 |
|
1087 | + * @return string |
|
1088 | + */ |
|
1089 | + public function get_renew_url() { |
|
1090 | + $url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'renew_subscription', 'sub_id' => $this->get_id ) ), 'getpaid-nonce' ); |
|
1091 | + return apply_filters( 'wpinv_subscription_renew_url', $url, $this ); |
|
1092 | + } |
|
1093 | + |
|
1094 | + /** |
|
1095 | + * Determines if subscription can have their payment method updated |
|
1096 | + * |
|
1097 | + * @since 1.0.0 |
|
1098 | + * @return bool |
|
1099 | + */ |
|
1100 | + public function can_update() { |
|
1101 | + return apply_filters( 'wpinv_subscription_can_update', false, $this ); |
|
1102 | + } |
|
1103 | + |
|
1104 | + /** |
|
1105 | + * Retrieves the URL to update subscription |
|
1106 | + * |
|
1107 | + * @since 1.0.0 |
|
1108 | + * @return string |
|
1109 | + */ |
|
1110 | + public function get_update_url() { |
|
1111 | + $url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->get_id() ) ); |
|
1112 | + return apply_filters( 'wpinv_subscription_update_url', $url, $this ); |
|
1113 | + } |
|
1114 | + |
|
1115 | + /** |
|
1116 | + * Retrieves the subscription status label |
|
1117 | + * |
|
1118 | + * @since 1.0.0 |
|
1119 | + * @return string |
|
1120 | + */ |
|
1121 | + public function get_status_label() { |
|
1122 | + return getpaid_get_subscription_status_label( $this->get_status() ); |
|
1123 | + } |
|
1124 | + |
|
1125 | + /** |
|
1126 | + * Retrieves the subscription status class |
|
1127 | + * |
|
1128 | + * @since 1.0.19 |
|
1129 | + * @return string |
|
1130 | + */ |
|
1131 | + public function get_status_class() { |
|
1132 | + $statuses = getpaid_get_subscription_status_classes(); |
|
1133 | + return isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : 'text-white bg-secondary'; |
|
1134 | + } |
|
1135 | 1135 | |
1136 | 1136 | /** |
1137 | 1137 | * Retrieves the subscription status label |
@@ -1141,11 +1141,11 @@ discard block |
||
1141 | 1141 | */ |
1142 | 1142 | public function get_status_label_html() { |
1143 | 1143 | |
1144 | - $status_label = sanitize_text_field( $this->get_status_label() ); |
|
1145 | - $class = esc_attr( $this->get_status_class() ); |
|
1146 | - $status = sanitize_html_class( $this->get_status_label() ); |
|
1144 | + $status_label = sanitize_text_field( $this->get_status_label() ); |
|
1145 | + $class = esc_attr( $this->get_status_class() ); |
|
1146 | + $status = sanitize_html_class( $this->get_status_label() ); |
|
1147 | 1147 | |
1148 | - return "<span class='bsui'><span class='d-inline-block py-2 px-3 rounded $class $status'>$status_label</span></span>"; |
|
1148 | + return "<span class='bsui'><span class='d-inline-block py-2 px-3 rounded $class $status'>$status_label</span></span>"; |
|
1149 | 1149 | } |
1150 | 1150 | |
1151 | 1151 | /** |
@@ -1156,63 +1156,63 @@ discard block |
||
1156 | 1156 | * @return bool |
1157 | 1157 | */ |
1158 | 1158 | public function payment_exists( $txn_id = '' ) { |
1159 | - $invoice_id = WPInv_Invoice::get_invoice_id_by_field( $txn_id, 'transaction_id' ); |
|
1159 | + $invoice_id = WPInv_Invoice::get_invoice_id_by_field( $txn_id, 'transaction_id' ); |
|
1160 | 1160 | return ! empty( $invoice_id ); |
1161 | - } |
|
1162 | - |
|
1163 | - /** |
|
1164 | - * Handle the status transition. |
|
1165 | - */ |
|
1166 | - protected function status_transition() { |
|
1167 | - $status_transition = $this->status_transition; |
|
1168 | - |
|
1169 | - // Reset status transition variable. |
|
1170 | - $this->status_transition = false; |
|
1171 | - |
|
1172 | - if ( $status_transition ) { |
|
1173 | - try { |
|
1174 | - |
|
1175 | - // Fire a hook for the status change. |
|
1176 | - do_action( 'wpinv_subscription_' . $status_transition['to'], $this->get_id(), $this, $status_transition ); |
|
1177 | - do_action( 'getpaid_subscription_' . $status_transition['to'], $this, $status_transition ); |
|
1178 | - |
|
1179 | - if ( ! empty( $status_transition['from'] ) ) { |
|
1180 | - |
|
1181 | - /* translators: 1: old subscription status 2: new subscription status */ |
|
1182 | - $transition_note = sprintf( __( 'Subscription status changed from %1$s to %2$s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['from'] ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
1183 | - |
|
1184 | - // Note the transition occurred. |
|
1185 | - $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
1186 | - |
|
1187 | - // Fire another hook. |
|
1188 | - do_action( 'getpaid_subscription_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this ); |
|
1189 | - do_action( 'getpaid_subscription_status_changed', $this, $status_transition['from'], $status_transition['to'] ); |
|
1190 | - |
|
1191 | - } else { |
|
1192 | - /* translators: %s: new invoice status */ |
|
1193 | - $transition_note = sprintf( __( 'Subscription status set to %s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
1194 | - |
|
1195 | - // Note the transition occurred. |
|
1196 | - $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
1197 | - |
|
1198 | - } |
|
1199 | - } catch ( Exception $e ) { |
|
1200 | - $this->get_parent_payment()->add_note( __( 'Error during subscription status transition.', 'invoicing' ) . ' ' . $e->getMessage() ); |
|
1201 | - } |
|
1202 | - } |
|
1203 | - |
|
1204 | - } |
|
1205 | - |
|
1206 | - /** |
|
1207 | - * Save data to the database. |
|
1208 | - * |
|
1209 | - * @since 1.0.19 |
|
1210 | - * @return int subscription ID |
|
1211 | - */ |
|
1212 | - public function save() { |
|
1213 | - parent::save(); |
|
1214 | - $this->status_transition(); |
|
1215 | - return $this->get_id(); |
|
1216 | - } |
|
1161 | + } |
|
1162 | + |
|
1163 | + /** |
|
1164 | + * Handle the status transition. |
|
1165 | + */ |
|
1166 | + protected function status_transition() { |
|
1167 | + $status_transition = $this->status_transition; |
|
1168 | + |
|
1169 | + // Reset status transition variable. |
|
1170 | + $this->status_transition = false; |
|
1171 | + |
|
1172 | + if ( $status_transition ) { |
|
1173 | + try { |
|
1174 | + |
|
1175 | + // Fire a hook for the status change. |
|
1176 | + do_action( 'wpinv_subscription_' . $status_transition['to'], $this->get_id(), $this, $status_transition ); |
|
1177 | + do_action( 'getpaid_subscription_' . $status_transition['to'], $this, $status_transition ); |
|
1178 | + |
|
1179 | + if ( ! empty( $status_transition['from'] ) ) { |
|
1180 | + |
|
1181 | + /* translators: 1: old subscription status 2: new subscription status */ |
|
1182 | + $transition_note = sprintf( __( 'Subscription status changed from %1$s to %2$s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['from'] ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
1183 | + |
|
1184 | + // Note the transition occurred. |
|
1185 | + $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
1186 | + |
|
1187 | + // Fire another hook. |
|
1188 | + do_action( 'getpaid_subscription_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this ); |
|
1189 | + do_action( 'getpaid_subscription_status_changed', $this, $status_transition['from'], $status_transition['to'] ); |
|
1190 | + |
|
1191 | + } else { |
|
1192 | + /* translators: %s: new invoice status */ |
|
1193 | + $transition_note = sprintf( __( 'Subscription status set to %s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
1194 | + |
|
1195 | + // Note the transition occurred. |
|
1196 | + $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
1197 | + |
|
1198 | + } |
|
1199 | + } catch ( Exception $e ) { |
|
1200 | + $this->get_parent_payment()->add_note( __( 'Error during subscription status transition.', 'invoicing' ) . ' ' . $e->getMessage() ); |
|
1201 | + } |
|
1202 | + } |
|
1203 | + |
|
1204 | + } |
|
1205 | + |
|
1206 | + /** |
|
1207 | + * Save data to the database. |
|
1208 | + * |
|
1209 | + * @since 1.0.19 |
|
1210 | + * @return int subscription ID |
|
1211 | + */ |
|
1212 | + public function save() { |
|
1213 | + parent::save(); |
|
1214 | + $this->status_transition(); |
|
1215 | + return $this->get_id(); |
|
1216 | + } |
|
1217 | 1217 | |
1218 | 1218 | } |
@@ -16,495 +16,495 @@ |
||
16 | 16 | */ |
17 | 17 | class GetPaid_Subscriptions_Query { |
18 | 18 | |
19 | - /** |
|
20 | - * Query vars, after parsing |
|
21 | - * |
|
22 | - * @since 1.0.19 |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - public $query_vars = array(); |
|
26 | - |
|
27 | - /** |
|
28 | - * List of found subscriptions. |
|
29 | - * |
|
30 | - * @since 1.0.19 |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - private $results; |
|
34 | - |
|
35 | - /** |
|
36 | - * Total number of found subscriptions for the current query |
|
37 | - * |
|
38 | - * @since 1.0.19 |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - private $total_subscriptions = 0; |
|
42 | - |
|
43 | - /** |
|
44 | - * The SQL query used to fetch matching subscriptions. |
|
45 | - * |
|
46 | - * @since 1.0.19 |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - public $request; |
|
50 | - |
|
51 | - // SQL clauses |
|
52 | - |
|
53 | - /** |
|
54 | - * Contains the 'FIELDS' sql clause |
|
55 | - * |
|
56 | - * @since 1.0.19 |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - public $query_fields; |
|
60 | - |
|
61 | - /** |
|
62 | - * Contains the 'FROM' sql clause |
|
63 | - * |
|
64 | - * @since 1.0.19 |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - public $query_from; |
|
68 | - |
|
69 | - /** |
|
70 | - * Contains the 'WHERE' sql clause |
|
71 | - * |
|
72 | - * @since 1.0.19 |
|
73 | - * @var string |
|
74 | - */ |
|
75 | - public $query_where; |
|
76 | - |
|
77 | - /** |
|
78 | - * Contains the 'ORDER BY' sql clause |
|
79 | - * |
|
80 | - * @since 1.0.19 |
|
81 | - * @var string |
|
82 | - */ |
|
83 | - public $query_orderby; |
|
84 | - |
|
85 | - /** |
|
86 | - * Contains the 'LIMIT' sql clause |
|
87 | - * |
|
88 | - * @since 1.0.19 |
|
89 | - * @var string |
|
90 | - */ |
|
91 | - public $query_limit; |
|
92 | - |
|
93 | - /** |
|
94 | - * Class constructor. |
|
95 | - * |
|
96 | - * @since 1.0.19 |
|
97 | - * |
|
98 | - * @param null|string|array $query Optional. The query variables. |
|
99 | - */ |
|
100 | - public function __construct( $query = null ) { |
|
101 | - if ( ! is_null( $query ) ) { |
|
102 | - $this->prepare_query( $query ); |
|
103 | - $this->query(); |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Fills in missing query variables with default values. |
|
109 | - * |
|
110 | - * @since 1.0.19 |
|
111 | - * |
|
112 | - * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
113 | - * @return array Complete query variables with undefined ones filled in with defaults. |
|
114 | - */ |
|
115 | - public static function fill_query_vars( $args ) { |
|
116 | - $defaults = array( |
|
117 | - 'status' => 'all', |
|
118 | - 'customer_in' => array(), |
|
119 | - 'customer_not_in' => array(), |
|
120 | - 'product_in' => array(), |
|
121 | - 'product_not_in' => array(), |
|
122 | - 'include' => array(), |
|
123 | - 'exclude' => array(), |
|
124 | - 'orderby' => 'id', |
|
125 | - 'order' => 'DESC', |
|
126 | - 'offset' => '', |
|
127 | - 'number' => 10, |
|
128 | - 'paged' => 1, |
|
129 | - 'count_total' => true, |
|
130 | - 'fields' => 'all', |
|
131 | - ); |
|
132 | - |
|
133 | - return wp_parse_args( $args, $defaults ); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Prepare the query variables. |
|
138 | - * |
|
139 | - * @since 1.0.19 |
|
140 | - * |
|
141 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
142 | - * |
|
143 | - * @param string|array $query { |
|
144 | - * Optional. Array or string of Query parameters. |
|
145 | - * |
|
146 | - * @type string|array $status The subscription status to filter by. Can either be a single status or an array of statuses. |
|
147 | - * Default is all. |
|
148 | - * @type int[] $customer_in An array of customer ids to filter by. |
|
149 | - * @type int[] $customer_not_in An array of customer ids whose subscriptions should be excluded. |
|
150 | - * @type int[] $invoice_in An array of invoice ids to filter by. |
|
151 | - * @type int[] $invoice_not_in An array of invoice ids whose subscriptions should be excluded. |
|
152 | - * @type int[] $product_in An array of product ids to filter by. |
|
153 | - * @type int[] $product_not_in An array of product ids whose subscriptions should be excluded. |
|
154 | - * @type array $date_created_query A WP_Date_Query compatible array use to filter subscriptions by their date of creation. |
|
155 | - * @type array $date_expires_query A WP_Date_Query compatible array use to filter subscriptions by their expiration date. |
|
156 | - * @type array $include An array of subscription IDs to include. Default empty array. |
|
157 | - * @type array $exclude An array of subscription IDs to exclude. Default empty array. |
|
158 | - * @type string|array $orderby Field(s) to sort the retrieved subscription by. May be a single value, |
|
159 | - * an array of values, or a multi-dimensional array with fields as |
|
160 | - * keys and orders ('ASC' or 'DESC') as values. Accepted values are |
|
161 | - * 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
162 | - * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
163 | - * 'transaction_id', 'product_id', 'trial_period', 'include', 'status', 'profile_id'. Default array( 'id' ). |
|
164 | - * @type string $order Designates ascending or descending order of subscriptions. Order values |
|
165 | - * passed as part of an `$orderby` array take precedence over this |
|
166 | - * parameter. Accepts 'ASC', 'DESC'. Default 'DESC'. |
|
167 | - * @type int $offset Number of subscriptions to offset in retrieved results. Can be used in |
|
168 | - * conjunction with pagination. Default 0. |
|
169 | - * @type int $number Number of subscriptions to limit the query for. Can be used in |
|
170 | - * conjunction with pagination. Value -1 (all) is supported, but |
|
171 | - * should be used with caution on larger sites. |
|
172 | - * Default 10. |
|
173 | - * @type int $paged When used with number, defines the page of results to return. |
|
174 | - * Default 1. |
|
175 | - * @type bool $count_total Whether to count the total number of subscriptions found. If pagination |
|
176 | - * is not needed, setting this to false can improve performance. |
|
177 | - * Default true. |
|
178 | - * @type string|array $fields Which fields to return. Single or all fields (string), or array |
|
179 | - * of fields. Accepts 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
180 | - * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
181 | - * 'transaction_id', 'product_id', 'trial_period', 'status', 'profile_id'. |
|
182 | - * Use 'all' for all fields. Default 'all'. |
|
183 | - * } |
|
184 | - */ |
|
185 | - public function prepare_query( $query = array() ) { |
|
186 | - global $wpdb; |
|
187 | - |
|
188 | - if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
189 | - $this->query_limit = null; |
|
190 | - $this->query_vars = $this->fill_query_vars( $query ); |
|
191 | - } |
|
192 | - |
|
193 | - if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
194 | - $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
195 | - } |
|
196 | - |
|
197 | - do_action( 'getpaid_pre_get_subscriptions', array( &$this ) ); |
|
198 | - |
|
199 | - // Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'. |
|
200 | - $qv =& $this->query_vars; |
|
201 | - $qv = $this->fill_query_vars( $qv ); |
|
202 | - $table = $wpdb->prefix . 'wpinv_subscriptions'; |
|
203 | - $this->query_from = "FROM $table"; |
|
204 | - |
|
205 | - // Prepare query fields. |
|
206 | - $this->prepare_query_fields( $qv, $table ); |
|
207 | - |
|
208 | - // Prepare query where. |
|
209 | - $this->prepare_query_where( $qv, $table ); |
|
210 | - |
|
211 | - // Prepare query order. |
|
212 | - $this->prepare_query_order( $qv, $table ); |
|
213 | - |
|
214 | - // limit |
|
215 | - if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
216 | - if ( $qv['offset'] ) { |
|
217 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
218 | - } else { |
|
219 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) ); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Prepares the query fields. |
|
228 | - * |
|
229 | - * @since 1.0.19 |
|
230 | - * |
|
231 | - * @param array $qv Query vars. |
|
232 | - * @param string $table Table name. |
|
233 | - */ |
|
234 | - protected function prepare_query_fields( &$qv, $table ) { |
|
235 | - |
|
236 | - if ( is_array( $qv['fields'] ) ) { |
|
237 | - $qv['fields'] = array_unique( $qv['fields'] ); |
|
238 | - |
|
239 | - $query_fields = array(); |
|
240 | - foreach ( $qv['fields'] as $field ) { |
|
241 | - $field = sanitize_key( $field ); |
|
242 | - $query_fields[] = "$table.`$field`"; |
|
243 | - } |
|
244 | - $this->query_fields = implode( ',', $query_fields ); |
|
245 | - } else { |
|
246 | - $this->query_fields = "$table.*"; |
|
247 | - } |
|
248 | - |
|
249 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
250 | - $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
251 | - } |
|
252 | - |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Prepares the query where. |
|
257 | - * |
|
258 | - * @since 1.0.19 |
|
259 | - * |
|
260 | - * @param array $qv Query vars. |
|
261 | - * @param string $table Table name. |
|
262 | - */ |
|
263 | - protected function prepare_query_where( &$qv, $table ) { |
|
264 | - global $wpdb; |
|
265 | - $this->query_where = 'WHERE 1=1'; |
|
266 | - |
|
267 | - // Status. |
|
268 | - if ( 'all' !== $qv['status'] ) { |
|
269 | - $statuses = wpinv_clean( wpinv_parse_list( $qv['status'] ) ); |
|
270 | - $prepared_statuses = join( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
|
271 | - $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses ); |
|
272 | - } |
|
273 | - |
|
274 | - if ( ! empty( $qv['customer_in'] ) ) { |
|
275 | - $customer_in = implode( ',', wp_parse_id_list( $qv['customer_in'] ) ); |
|
276 | - $this->query_where .= " AND $table.`customer_id` IN ($customer_in)"; |
|
277 | - } elseif ( ! empty( $qv['customer_not_in'] ) ) { |
|
278 | - $customer_not_in = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) ); |
|
279 | - $this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)"; |
|
280 | - } |
|
281 | - |
|
282 | - if ( ! empty( $qv['product_in'] ) ) { |
|
283 | - $product_in = implode( ',', wp_parse_id_list( $qv['product_in'] ) ); |
|
284 | - $this->query_where .= " AND $table.`product_id` IN ($product_in)"; |
|
285 | - } elseif ( ! empty( $qv['product_not_in'] ) ) { |
|
286 | - $product_not_in = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) ); |
|
287 | - $this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)"; |
|
288 | - } |
|
289 | - |
|
290 | - if ( ! empty( $qv['invoice_in'] ) ) { |
|
291 | - $invoice_in = implode( ',', wp_parse_id_list( $qv['invoice_in'] ) ); |
|
292 | - $this->query_where .= " AND $table.`parent_payment_id` IN ($invoice_in)"; |
|
293 | - } elseif ( ! empty( $qv['invoice_not_in'] ) ) { |
|
294 | - $invoice_not_in = implode( ',', wp_parse_id_list( $qv['invoice_not_in'] ) ); |
|
295 | - $this->query_where .= " AND $table.`parent_payment_id` NOT IN ($invoice_not_in)"; |
|
296 | - } |
|
297 | - |
|
298 | - if ( ! empty( $qv['include'] ) ) { |
|
299 | - $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
300 | - $this->query_where .= " AND $table.`id` IN ($include)"; |
|
301 | - } elseif ( ! empty( $qv['exclude'] ) ) { |
|
302 | - $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
303 | - $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
304 | - } |
|
305 | - |
|
306 | - // Date queries are allowed for the subscription creation date. |
|
307 | - if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
308 | - $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" ); |
|
309 | - $this->query_where .= $date_created_query->get_sql(); |
|
310 | - } |
|
311 | - |
|
312 | - // Date queries are also allowed for the subscription expiration date. |
|
313 | - if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) { |
|
314 | - $date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" ); |
|
315 | - $this->query_where .= $date_expires_query->get_sql(); |
|
316 | - } |
|
317 | - |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Prepares the query order. |
|
322 | - * |
|
323 | - * @since 1.0.19 |
|
324 | - * |
|
325 | - * @param array $qv Query vars. |
|
326 | - * @param string $table Table name. |
|
327 | - */ |
|
328 | - protected function prepare_query_order( &$qv, $table ) { |
|
329 | - |
|
330 | - // sorting. |
|
331 | - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
332 | - $order = $this->parse_order( $qv['order'] ); |
|
333 | - |
|
334 | - // Default order is by 'id' (latest subscriptions). |
|
335 | - if ( empty( $qv['orderby'] ) ) { |
|
336 | - $qv['orderby'] = array( 'id' ); |
|
337 | - } |
|
338 | - |
|
339 | - // 'orderby' values may be an array, comma- or space-separated list. |
|
340 | - $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
341 | - |
|
342 | - $orderby_array = array(); |
|
343 | - foreach ( $ordersby as $_key => $_value ) { |
|
344 | - |
|
345 | - if ( is_int( $_key ) ) { |
|
346 | - // Integer key means this is a flat array of 'orderby' fields. |
|
347 | - $_orderby = $_value; |
|
348 | - $_order = $order; |
|
349 | - } else { |
|
350 | - // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
351 | - $_orderby = $_key; |
|
352 | - $_order = $_value; |
|
353 | - } |
|
354 | - |
|
355 | - $parsed = $this->parse_orderby( $_orderby, $table ); |
|
356 | - |
|
357 | - if ( $parsed ) { |
|
358 | - $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
359 | - } |
|
360 | - |
|
361 | - } |
|
362 | - |
|
363 | - // If no valid clauses were found, order by id. |
|
364 | - if ( empty( $orderby_array ) ) { |
|
365 | - $orderby_array[] = "id $order"; |
|
366 | - } |
|
367 | - |
|
368 | - $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
369 | - |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Execute the query, with the current variables. |
|
374 | - * |
|
375 | - * @since 1.0.19 |
|
376 | - * |
|
377 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
378 | - */ |
|
379 | - public function query() { |
|
380 | - global $wpdb; |
|
381 | - |
|
382 | - $qv =& $this->query_vars; |
|
383 | - |
|
384 | - // Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the |
|
385 | - // total_subscriptions property. |
|
386 | - $this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) ); |
|
387 | - |
|
388 | - if ( null === $this->results ) { |
|
389 | - $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
390 | - |
|
391 | - if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) { |
|
392 | - $this->results = $wpdb->get_results( $this->request ); |
|
393 | - } else { |
|
394 | - $this->results = $wpdb->get_col( $this->request ); |
|
395 | - } |
|
396 | - |
|
397 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
398 | - $found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this ); |
|
399 | - $this->total_subscriptions = (int) $wpdb->get_var( $found_subscriptions_query ); |
|
400 | - } |
|
401 | - } |
|
402 | - |
|
403 | - if ( 'all' == $qv['fields'] ) { |
|
404 | - foreach ( $this->results as $key => $subscription ) { |
|
405 | - wp_cache_set( $subscription->id, $subscription, 'getpaid_subscriptions' ); |
|
406 | - wp_cache_set( $subscription->profile_id, $subscription->id, 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
407 | - wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
408 | - wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
409 | - $this->results[ $key ] = new WPInv_Subscription( $subscription ); |
|
410 | - } |
|
411 | - } |
|
412 | - |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Retrieve query variable. |
|
417 | - * |
|
418 | - * @since 1.0.19 |
|
419 | - * |
|
420 | - * @param string $query_var Query variable key. |
|
421 | - * @return mixed |
|
422 | - */ |
|
423 | - public function get( $query_var ) { |
|
424 | - if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
425 | - return $this->query_vars[ $query_var ]; |
|
426 | - } |
|
427 | - |
|
428 | - return null; |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Set query variable. |
|
433 | - * |
|
434 | - * @since 1.0.19 |
|
435 | - * |
|
436 | - * @param string $query_var Query variable key. |
|
437 | - * @param mixed $value Query variable value. |
|
438 | - */ |
|
439 | - public function set( $query_var, $value ) { |
|
440 | - $this->query_vars[ $query_var ] = $value; |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Return the list of subscriptions. |
|
445 | - * |
|
446 | - * @since 1.0.19 |
|
447 | - * |
|
448 | - * @return WPInv_Subscription[]|array Found subscriptions. |
|
449 | - */ |
|
450 | - public function get_results() { |
|
451 | - return $this->results; |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Return the total number of subscriptions for the current query. |
|
456 | - * |
|
457 | - * @since 1.0.19 |
|
458 | - * |
|
459 | - * @return int Number of total subscriptions. |
|
460 | - */ |
|
461 | - public function get_total() { |
|
462 | - return $this->total_subscriptions; |
|
463 | - } |
|
464 | - |
|
465 | - /** |
|
466 | - * Parse and sanitize 'orderby' keys passed to the subscriptions query. |
|
467 | - * |
|
468 | - * @since 1.0.19 |
|
469 | - * |
|
470 | - * @param string $orderby Alias for the field to order by. |
|
471 | - * @param string $table The current table. |
|
472 | - * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
473 | - */ |
|
474 | - protected function parse_orderby( $orderby, $table ) { |
|
475 | - |
|
476 | - $_orderby = ''; |
|
477 | - if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) { |
|
478 | - $_orderby = "$table.`$orderby`"; |
|
479 | - } elseif ( 'id' === strtolower( $orderby ) ) { |
|
480 | - $_orderby = "$table.id"; |
|
481 | - } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
482 | - $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
483 | - $include_sql = implode( ',', $include ); |
|
484 | - $_orderby = "FIELD( $table.id, $include_sql )"; |
|
485 | - } |
|
486 | - |
|
487 | - return $_orderby; |
|
488 | - } |
|
489 | - |
|
490 | - /** |
|
491 | - * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
492 | - * |
|
493 | - * @since 1.0.19 |
|
494 | - * |
|
495 | - * @param string $order The 'order' query variable. |
|
496 | - * @return string The sanitized 'order' query variable. |
|
497 | - */ |
|
498 | - protected function parse_order( $order ) { |
|
499 | - if ( ! is_string( $order ) || empty( $order ) ) { |
|
500 | - return 'DESC'; |
|
501 | - } |
|
502 | - |
|
503 | - if ( 'ASC' === strtoupper( $order ) ) { |
|
504 | - return 'ASC'; |
|
505 | - } else { |
|
506 | - return 'DESC'; |
|
507 | - } |
|
508 | - } |
|
19 | + /** |
|
20 | + * Query vars, after parsing |
|
21 | + * |
|
22 | + * @since 1.0.19 |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + public $query_vars = array(); |
|
26 | + |
|
27 | + /** |
|
28 | + * List of found subscriptions. |
|
29 | + * |
|
30 | + * @since 1.0.19 |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + private $results; |
|
34 | + |
|
35 | + /** |
|
36 | + * Total number of found subscriptions for the current query |
|
37 | + * |
|
38 | + * @since 1.0.19 |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + private $total_subscriptions = 0; |
|
42 | + |
|
43 | + /** |
|
44 | + * The SQL query used to fetch matching subscriptions. |
|
45 | + * |
|
46 | + * @since 1.0.19 |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + public $request; |
|
50 | + |
|
51 | + // SQL clauses |
|
52 | + |
|
53 | + /** |
|
54 | + * Contains the 'FIELDS' sql clause |
|
55 | + * |
|
56 | + * @since 1.0.19 |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + public $query_fields; |
|
60 | + |
|
61 | + /** |
|
62 | + * Contains the 'FROM' sql clause |
|
63 | + * |
|
64 | + * @since 1.0.19 |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + public $query_from; |
|
68 | + |
|
69 | + /** |
|
70 | + * Contains the 'WHERE' sql clause |
|
71 | + * |
|
72 | + * @since 1.0.19 |
|
73 | + * @var string |
|
74 | + */ |
|
75 | + public $query_where; |
|
76 | + |
|
77 | + /** |
|
78 | + * Contains the 'ORDER BY' sql clause |
|
79 | + * |
|
80 | + * @since 1.0.19 |
|
81 | + * @var string |
|
82 | + */ |
|
83 | + public $query_orderby; |
|
84 | + |
|
85 | + /** |
|
86 | + * Contains the 'LIMIT' sql clause |
|
87 | + * |
|
88 | + * @since 1.0.19 |
|
89 | + * @var string |
|
90 | + */ |
|
91 | + public $query_limit; |
|
92 | + |
|
93 | + /** |
|
94 | + * Class constructor. |
|
95 | + * |
|
96 | + * @since 1.0.19 |
|
97 | + * |
|
98 | + * @param null|string|array $query Optional. The query variables. |
|
99 | + */ |
|
100 | + public function __construct( $query = null ) { |
|
101 | + if ( ! is_null( $query ) ) { |
|
102 | + $this->prepare_query( $query ); |
|
103 | + $this->query(); |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Fills in missing query variables with default values. |
|
109 | + * |
|
110 | + * @since 1.0.19 |
|
111 | + * |
|
112 | + * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
113 | + * @return array Complete query variables with undefined ones filled in with defaults. |
|
114 | + */ |
|
115 | + public static function fill_query_vars( $args ) { |
|
116 | + $defaults = array( |
|
117 | + 'status' => 'all', |
|
118 | + 'customer_in' => array(), |
|
119 | + 'customer_not_in' => array(), |
|
120 | + 'product_in' => array(), |
|
121 | + 'product_not_in' => array(), |
|
122 | + 'include' => array(), |
|
123 | + 'exclude' => array(), |
|
124 | + 'orderby' => 'id', |
|
125 | + 'order' => 'DESC', |
|
126 | + 'offset' => '', |
|
127 | + 'number' => 10, |
|
128 | + 'paged' => 1, |
|
129 | + 'count_total' => true, |
|
130 | + 'fields' => 'all', |
|
131 | + ); |
|
132 | + |
|
133 | + return wp_parse_args( $args, $defaults ); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Prepare the query variables. |
|
138 | + * |
|
139 | + * @since 1.0.19 |
|
140 | + * |
|
141 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
142 | + * |
|
143 | + * @param string|array $query { |
|
144 | + * Optional. Array or string of Query parameters. |
|
145 | + * |
|
146 | + * @type string|array $status The subscription status to filter by. Can either be a single status or an array of statuses. |
|
147 | + * Default is all. |
|
148 | + * @type int[] $customer_in An array of customer ids to filter by. |
|
149 | + * @type int[] $customer_not_in An array of customer ids whose subscriptions should be excluded. |
|
150 | + * @type int[] $invoice_in An array of invoice ids to filter by. |
|
151 | + * @type int[] $invoice_not_in An array of invoice ids whose subscriptions should be excluded. |
|
152 | + * @type int[] $product_in An array of product ids to filter by. |
|
153 | + * @type int[] $product_not_in An array of product ids whose subscriptions should be excluded. |
|
154 | + * @type array $date_created_query A WP_Date_Query compatible array use to filter subscriptions by their date of creation. |
|
155 | + * @type array $date_expires_query A WP_Date_Query compatible array use to filter subscriptions by their expiration date. |
|
156 | + * @type array $include An array of subscription IDs to include. Default empty array. |
|
157 | + * @type array $exclude An array of subscription IDs to exclude. Default empty array. |
|
158 | + * @type string|array $orderby Field(s) to sort the retrieved subscription by. May be a single value, |
|
159 | + * an array of values, or a multi-dimensional array with fields as |
|
160 | + * keys and orders ('ASC' or 'DESC') as values. Accepted values are |
|
161 | + * 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
162 | + * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
163 | + * 'transaction_id', 'product_id', 'trial_period', 'include', 'status', 'profile_id'. Default array( 'id' ). |
|
164 | + * @type string $order Designates ascending or descending order of subscriptions. Order values |
|
165 | + * passed as part of an `$orderby` array take precedence over this |
|
166 | + * parameter. Accepts 'ASC', 'DESC'. Default 'DESC'. |
|
167 | + * @type int $offset Number of subscriptions to offset in retrieved results. Can be used in |
|
168 | + * conjunction with pagination. Default 0. |
|
169 | + * @type int $number Number of subscriptions to limit the query for. Can be used in |
|
170 | + * conjunction with pagination. Value -1 (all) is supported, but |
|
171 | + * should be used with caution on larger sites. |
|
172 | + * Default 10. |
|
173 | + * @type int $paged When used with number, defines the page of results to return. |
|
174 | + * Default 1. |
|
175 | + * @type bool $count_total Whether to count the total number of subscriptions found. If pagination |
|
176 | + * is not needed, setting this to false can improve performance. |
|
177 | + * Default true. |
|
178 | + * @type string|array $fields Which fields to return. Single or all fields (string), or array |
|
179 | + * of fields. Accepts 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
180 | + * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
181 | + * 'transaction_id', 'product_id', 'trial_period', 'status', 'profile_id'. |
|
182 | + * Use 'all' for all fields. Default 'all'. |
|
183 | + * } |
|
184 | + */ |
|
185 | + public function prepare_query( $query = array() ) { |
|
186 | + global $wpdb; |
|
187 | + |
|
188 | + if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
189 | + $this->query_limit = null; |
|
190 | + $this->query_vars = $this->fill_query_vars( $query ); |
|
191 | + } |
|
192 | + |
|
193 | + if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
194 | + $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
195 | + } |
|
196 | + |
|
197 | + do_action( 'getpaid_pre_get_subscriptions', array( &$this ) ); |
|
198 | + |
|
199 | + // Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'. |
|
200 | + $qv =& $this->query_vars; |
|
201 | + $qv = $this->fill_query_vars( $qv ); |
|
202 | + $table = $wpdb->prefix . 'wpinv_subscriptions'; |
|
203 | + $this->query_from = "FROM $table"; |
|
204 | + |
|
205 | + // Prepare query fields. |
|
206 | + $this->prepare_query_fields( $qv, $table ); |
|
207 | + |
|
208 | + // Prepare query where. |
|
209 | + $this->prepare_query_where( $qv, $table ); |
|
210 | + |
|
211 | + // Prepare query order. |
|
212 | + $this->prepare_query_order( $qv, $table ); |
|
213 | + |
|
214 | + // limit |
|
215 | + if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
216 | + if ( $qv['offset'] ) { |
|
217 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
218 | + } else { |
|
219 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) ); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Prepares the query fields. |
|
228 | + * |
|
229 | + * @since 1.0.19 |
|
230 | + * |
|
231 | + * @param array $qv Query vars. |
|
232 | + * @param string $table Table name. |
|
233 | + */ |
|
234 | + protected function prepare_query_fields( &$qv, $table ) { |
|
235 | + |
|
236 | + if ( is_array( $qv['fields'] ) ) { |
|
237 | + $qv['fields'] = array_unique( $qv['fields'] ); |
|
238 | + |
|
239 | + $query_fields = array(); |
|
240 | + foreach ( $qv['fields'] as $field ) { |
|
241 | + $field = sanitize_key( $field ); |
|
242 | + $query_fields[] = "$table.`$field`"; |
|
243 | + } |
|
244 | + $this->query_fields = implode( ',', $query_fields ); |
|
245 | + } else { |
|
246 | + $this->query_fields = "$table.*"; |
|
247 | + } |
|
248 | + |
|
249 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
250 | + $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
251 | + } |
|
252 | + |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Prepares the query where. |
|
257 | + * |
|
258 | + * @since 1.0.19 |
|
259 | + * |
|
260 | + * @param array $qv Query vars. |
|
261 | + * @param string $table Table name. |
|
262 | + */ |
|
263 | + protected function prepare_query_where( &$qv, $table ) { |
|
264 | + global $wpdb; |
|
265 | + $this->query_where = 'WHERE 1=1'; |
|
266 | + |
|
267 | + // Status. |
|
268 | + if ( 'all' !== $qv['status'] ) { |
|
269 | + $statuses = wpinv_clean( wpinv_parse_list( $qv['status'] ) ); |
|
270 | + $prepared_statuses = join( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
|
271 | + $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses ); |
|
272 | + } |
|
273 | + |
|
274 | + if ( ! empty( $qv['customer_in'] ) ) { |
|
275 | + $customer_in = implode( ',', wp_parse_id_list( $qv['customer_in'] ) ); |
|
276 | + $this->query_where .= " AND $table.`customer_id` IN ($customer_in)"; |
|
277 | + } elseif ( ! empty( $qv['customer_not_in'] ) ) { |
|
278 | + $customer_not_in = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) ); |
|
279 | + $this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)"; |
|
280 | + } |
|
281 | + |
|
282 | + if ( ! empty( $qv['product_in'] ) ) { |
|
283 | + $product_in = implode( ',', wp_parse_id_list( $qv['product_in'] ) ); |
|
284 | + $this->query_where .= " AND $table.`product_id` IN ($product_in)"; |
|
285 | + } elseif ( ! empty( $qv['product_not_in'] ) ) { |
|
286 | + $product_not_in = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) ); |
|
287 | + $this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)"; |
|
288 | + } |
|
289 | + |
|
290 | + if ( ! empty( $qv['invoice_in'] ) ) { |
|
291 | + $invoice_in = implode( ',', wp_parse_id_list( $qv['invoice_in'] ) ); |
|
292 | + $this->query_where .= " AND $table.`parent_payment_id` IN ($invoice_in)"; |
|
293 | + } elseif ( ! empty( $qv['invoice_not_in'] ) ) { |
|
294 | + $invoice_not_in = implode( ',', wp_parse_id_list( $qv['invoice_not_in'] ) ); |
|
295 | + $this->query_where .= " AND $table.`parent_payment_id` NOT IN ($invoice_not_in)"; |
|
296 | + } |
|
297 | + |
|
298 | + if ( ! empty( $qv['include'] ) ) { |
|
299 | + $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
300 | + $this->query_where .= " AND $table.`id` IN ($include)"; |
|
301 | + } elseif ( ! empty( $qv['exclude'] ) ) { |
|
302 | + $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
303 | + $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
304 | + } |
|
305 | + |
|
306 | + // Date queries are allowed for the subscription creation date. |
|
307 | + if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
308 | + $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" ); |
|
309 | + $this->query_where .= $date_created_query->get_sql(); |
|
310 | + } |
|
311 | + |
|
312 | + // Date queries are also allowed for the subscription expiration date. |
|
313 | + if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) { |
|
314 | + $date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" ); |
|
315 | + $this->query_where .= $date_expires_query->get_sql(); |
|
316 | + } |
|
317 | + |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Prepares the query order. |
|
322 | + * |
|
323 | + * @since 1.0.19 |
|
324 | + * |
|
325 | + * @param array $qv Query vars. |
|
326 | + * @param string $table Table name. |
|
327 | + */ |
|
328 | + protected function prepare_query_order( &$qv, $table ) { |
|
329 | + |
|
330 | + // sorting. |
|
331 | + $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
332 | + $order = $this->parse_order( $qv['order'] ); |
|
333 | + |
|
334 | + // Default order is by 'id' (latest subscriptions). |
|
335 | + if ( empty( $qv['orderby'] ) ) { |
|
336 | + $qv['orderby'] = array( 'id' ); |
|
337 | + } |
|
338 | + |
|
339 | + // 'orderby' values may be an array, comma- or space-separated list. |
|
340 | + $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
341 | + |
|
342 | + $orderby_array = array(); |
|
343 | + foreach ( $ordersby as $_key => $_value ) { |
|
344 | + |
|
345 | + if ( is_int( $_key ) ) { |
|
346 | + // Integer key means this is a flat array of 'orderby' fields. |
|
347 | + $_orderby = $_value; |
|
348 | + $_order = $order; |
|
349 | + } else { |
|
350 | + // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
351 | + $_orderby = $_key; |
|
352 | + $_order = $_value; |
|
353 | + } |
|
354 | + |
|
355 | + $parsed = $this->parse_orderby( $_orderby, $table ); |
|
356 | + |
|
357 | + if ( $parsed ) { |
|
358 | + $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
359 | + } |
|
360 | + |
|
361 | + } |
|
362 | + |
|
363 | + // If no valid clauses were found, order by id. |
|
364 | + if ( empty( $orderby_array ) ) { |
|
365 | + $orderby_array[] = "id $order"; |
|
366 | + } |
|
367 | + |
|
368 | + $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
369 | + |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Execute the query, with the current variables. |
|
374 | + * |
|
375 | + * @since 1.0.19 |
|
376 | + * |
|
377 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
378 | + */ |
|
379 | + public function query() { |
|
380 | + global $wpdb; |
|
381 | + |
|
382 | + $qv =& $this->query_vars; |
|
383 | + |
|
384 | + // Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the |
|
385 | + // total_subscriptions property. |
|
386 | + $this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) ); |
|
387 | + |
|
388 | + if ( null === $this->results ) { |
|
389 | + $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
390 | + |
|
391 | + if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) { |
|
392 | + $this->results = $wpdb->get_results( $this->request ); |
|
393 | + } else { |
|
394 | + $this->results = $wpdb->get_col( $this->request ); |
|
395 | + } |
|
396 | + |
|
397 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
398 | + $found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this ); |
|
399 | + $this->total_subscriptions = (int) $wpdb->get_var( $found_subscriptions_query ); |
|
400 | + } |
|
401 | + } |
|
402 | + |
|
403 | + if ( 'all' == $qv['fields'] ) { |
|
404 | + foreach ( $this->results as $key => $subscription ) { |
|
405 | + wp_cache_set( $subscription->id, $subscription, 'getpaid_subscriptions' ); |
|
406 | + wp_cache_set( $subscription->profile_id, $subscription->id, 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
407 | + wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
408 | + wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
409 | + $this->results[ $key ] = new WPInv_Subscription( $subscription ); |
|
410 | + } |
|
411 | + } |
|
412 | + |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Retrieve query variable. |
|
417 | + * |
|
418 | + * @since 1.0.19 |
|
419 | + * |
|
420 | + * @param string $query_var Query variable key. |
|
421 | + * @return mixed |
|
422 | + */ |
|
423 | + public function get( $query_var ) { |
|
424 | + if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
425 | + return $this->query_vars[ $query_var ]; |
|
426 | + } |
|
427 | + |
|
428 | + return null; |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Set query variable. |
|
433 | + * |
|
434 | + * @since 1.0.19 |
|
435 | + * |
|
436 | + * @param string $query_var Query variable key. |
|
437 | + * @param mixed $value Query variable value. |
|
438 | + */ |
|
439 | + public function set( $query_var, $value ) { |
|
440 | + $this->query_vars[ $query_var ] = $value; |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Return the list of subscriptions. |
|
445 | + * |
|
446 | + * @since 1.0.19 |
|
447 | + * |
|
448 | + * @return WPInv_Subscription[]|array Found subscriptions. |
|
449 | + */ |
|
450 | + public function get_results() { |
|
451 | + return $this->results; |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Return the total number of subscriptions for the current query. |
|
456 | + * |
|
457 | + * @since 1.0.19 |
|
458 | + * |
|
459 | + * @return int Number of total subscriptions. |
|
460 | + */ |
|
461 | + public function get_total() { |
|
462 | + return $this->total_subscriptions; |
|
463 | + } |
|
464 | + |
|
465 | + /** |
|
466 | + * Parse and sanitize 'orderby' keys passed to the subscriptions query. |
|
467 | + * |
|
468 | + * @since 1.0.19 |
|
469 | + * |
|
470 | + * @param string $orderby Alias for the field to order by. |
|
471 | + * @param string $table The current table. |
|
472 | + * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
473 | + */ |
|
474 | + protected function parse_orderby( $orderby, $table ) { |
|
475 | + |
|
476 | + $_orderby = ''; |
|
477 | + if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) { |
|
478 | + $_orderby = "$table.`$orderby`"; |
|
479 | + } elseif ( 'id' === strtolower( $orderby ) ) { |
|
480 | + $_orderby = "$table.id"; |
|
481 | + } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
482 | + $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
483 | + $include_sql = implode( ',', $include ); |
|
484 | + $_orderby = "FIELD( $table.id, $include_sql )"; |
|
485 | + } |
|
486 | + |
|
487 | + return $_orderby; |
|
488 | + } |
|
489 | + |
|
490 | + /** |
|
491 | + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
492 | + * |
|
493 | + * @since 1.0.19 |
|
494 | + * |
|
495 | + * @param string $order The 'order' query variable. |
|
496 | + * @return string The sanitized 'order' query variable. |
|
497 | + */ |
|
498 | + protected function parse_order( $order ) { |
|
499 | + if ( ! is_string( $order ) || empty( $order ) ) { |
|
500 | + return 'DESC'; |
|
501 | + } |
|
502 | + |
|
503 | + if ( 'ASC' === strtoupper( $order ) ) { |
|
504 | + return 'ASC'; |
|
505 | + } else { |
|
506 | + return 'DESC'; |
|
507 | + } |
|
508 | + } |
|
509 | 509 | |
510 | 510 | } |
@@ -43,77 +43,77 @@ discard block |
||
43 | 43 | <td class="w-75"> |
44 | 44 | <?php |
45 | 45 | |
46 | - switch ( $key ) { |
|
46 | + switch ( $key ) { |
|
47 | 47 | |
48 | - case 'status': |
|
49 | - echo sanitize_text_field( $subscription->get_status_label() ); |
|
50 | - break; |
|
48 | + case 'status': |
|
49 | + echo sanitize_text_field( $subscription->get_status_label() ); |
|
50 | + break; |
|
51 | 51 | |
52 | - case 'start_date': |
|
53 | - echo sanitize_text_field( getpaid_format_date_value( $subscription->get_date_created() ) ); |
|
54 | - break; |
|
52 | + case 'start_date': |
|
53 | + echo sanitize_text_field( getpaid_format_date_value( $subscription->get_date_created() ) ); |
|
54 | + break; |
|
55 | 55 | |
56 | - case 'expiry_date': |
|
57 | - echo sanitize_text_field( getpaid_format_date_value( $subscription->get_next_renewal_date() ) ); |
|
58 | - break; |
|
56 | + case 'expiry_date': |
|
57 | + echo sanitize_text_field( getpaid_format_date_value( $subscription->get_next_renewal_date() ) ); |
|
58 | + break; |
|
59 | 59 | |
60 | - case 'initial_amount': |
|
61 | - echo wpinv_price( wpinv_format_amount( $subscription->get_initial_amount() ), $subscription->get_parent_payment()->get_currency() ); |
|
60 | + case 'initial_amount': |
|
61 | + echo wpinv_price( wpinv_format_amount( $subscription->get_initial_amount() ), $subscription->get_parent_payment()->get_currency() ); |
|
62 | 62 | |
63 | - if ( $subscription->has_trial_period() ) { |
|
63 | + if ( $subscription->has_trial_period() ) { |
|
64 | 64 | |
65 | - echo "<small class='text-muted'> "; |
|
66 | - printf( |
|
67 | - _x( '( %1$s trial )', 'Subscription trial period. (e.g.: 1 month trial)', 'invoicing' ), |
|
68 | - sanitize_text_field( $subscription->get_trial_period() ) |
|
69 | - ); |
|
70 | - echo '</small>'; |
|
65 | + echo "<small class='text-muted'> "; |
|
66 | + printf( |
|
67 | + _x( '( %1$s trial )', 'Subscription trial period. (e.g.: 1 month trial)', 'invoicing' ), |
|
68 | + sanitize_text_field( $subscription->get_trial_period() ) |
|
69 | + ); |
|
70 | + echo '</small>'; |
|
71 | 71 | |
72 | - } |
|
72 | + } |
|
73 | 73 | |
74 | - break; |
|
74 | + break; |
|
75 | 75 | |
76 | - case 'recurring_amount': |
|
77 | - $frequency = sanitize_text_field( WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->get_period(), $subscription->get_frequency(), true ) ); |
|
78 | - $amount = wpinv_price( wpinv_format_amount( $subscription->get_recurring_amount() ), $subscription->get_parent_payment()->get_currency() ); |
|
79 | - echo strtolower( "<strong style='font-weight: 500;'>$amount</strong> / $frequency" ); |
|
80 | - break; |
|
76 | + case 'recurring_amount': |
|
77 | + $frequency = sanitize_text_field( WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->get_period(), $subscription->get_frequency(), true ) ); |
|
78 | + $amount = wpinv_price( wpinv_format_amount( $subscription->get_recurring_amount() ), $subscription->get_parent_payment()->get_currency() ); |
|
79 | + echo strtolower( "<strong style='font-weight: 500;'>$amount</strong> / $frequency" ); |
|
80 | + break; |
|
81 | 81 | |
82 | - case 'invoice': |
|
83 | - $invoice = $subscription->get_parent_invoice(); |
|
82 | + case 'invoice': |
|
83 | + $invoice = $subscription->get_parent_invoice(); |
|
84 | 84 | |
85 | - if ( $invoice->get_id() ) { |
|
86 | - $view_url = esc_url( $invoice->get_view_url() ); |
|
87 | - $number = sanitize_text_field( $invoice->get_number() ); |
|
88 | - echo "<a href='$view_url' class='text-decoration-none'>$number</a>"; |
|
89 | - } else { |
|
90 | - echo "—"; |
|
91 | - } |
|
85 | + if ( $invoice->get_id() ) { |
|
86 | + $view_url = esc_url( $invoice->get_view_url() ); |
|
87 | + $number = sanitize_text_field( $invoice->get_number() ); |
|
88 | + echo "<a href='$view_url' class='text-decoration-none'>$number</a>"; |
|
89 | + } else { |
|
90 | + echo "—"; |
|
91 | + } |
|
92 | 92 | |
93 | - break; |
|
93 | + break; |
|
94 | 94 | |
95 | - case 'item': |
|
96 | - $item = get_post( $subscription->get_product_id() ); |
|
95 | + case 'item': |
|
96 | + $item = get_post( $subscription->get_product_id() ); |
|
97 | 97 | |
98 | - if ( ! empty( $item ) ) { |
|
99 | - echo esc_html( get_the_title( $item ) ); |
|
100 | - } else { |
|
101 | - echo sprintf( __( 'Item #%s', 'invoicing' ), $subscription->get_product_id() ); |
|
102 | - } |
|
98 | + if ( ! empty( $item ) ) { |
|
99 | + echo esc_html( get_the_title( $item ) ); |
|
100 | + } else { |
|
101 | + echo sprintf( __( 'Item #%s', 'invoicing' ), $subscription->get_product_id() ); |
|
102 | + } |
|
103 | 103 | |
104 | - break; |
|
104 | + break; |
|
105 | 105 | |
106 | - case 'payments': |
|
106 | + case 'payments': |
|
107 | 107 | |
108 | - $max_activations = (int) $subscription->get_bill_times(); |
|
109 | - echo (int) $subscription->get_times_billed() . ' / ' . ( empty( $max_activations ) ? "∞" : $max_activations ); |
|
108 | + $max_activations = (int) $subscription->get_bill_times(); |
|
109 | + echo (int) $subscription->get_times_billed() . ' / ' . ( empty( $max_activations ) ? "∞" : $max_activations ); |
|
110 | 110 | |
111 | - break; |
|
111 | + break; |
|
112 | 112 | |
113 | - } |
|
114 | - do_action( "getpaid_render_single_subscription_column_$key", $subscription ); |
|
113 | + } |
|
114 | + do_action( "getpaid_render_single_subscription_column_$key", $subscription ); |
|
115 | 115 | |
116 | - ?> |
|
116 | + ?> |
|
117 | 117 | </td> |
118 | 118 | |
119 | 119 | </tr> |
@@ -125,28 +125,28 @@ discard block |
||
125 | 125 | |
126 | 126 | <?php |
127 | 127 | |
128 | - $payments = $subscription->get_child_payments(); |
|
129 | - $parent = $subscription->get_parent_invoice(); |
|
128 | + $payments = $subscription->get_child_payments(); |
|
129 | + $parent = $subscription->get_parent_invoice(); |
|
130 | 130 | |
131 | - if ( $parent->get_id() ) { |
|
132 | - $payments = array_merge( array( $parent ), $payments ); |
|
133 | - } |
|
131 | + if ( $parent->get_id() ) { |
|
132 | + $payments = array_merge( array( $parent ), $payments ); |
|
133 | + } |
|
134 | 134 | |
135 | - if ( empty( $payments ) ) { |
|
136 | - return; |
|
137 | - } |
|
135 | + if ( empty( $payments ) ) { |
|
136 | + return; |
|
137 | + } |
|
138 | 138 | |
139 | - $title = __( 'Related Invoices', 'invoicing' ); |
|
139 | + $title = __( 'Related Invoices', 'invoicing' ); |
|
140 | 140 | |
141 | - echo "<h2 class='mt-4 mb-1 h4'>$title</h2>"; |
|
141 | + echo "<h2 class='mt-4 mb-1 h4'>$title</h2>"; |
|
142 | 142 | |
143 | - foreach ( $payments as $payment ) { |
|
144 | - $payment = new WPInv_Invoice( $payment ); |
|
145 | - $number = sanitize_text_field( $payment->get_number() ); |
|
146 | - $url = esc_url( $payment->get_view_url() ); |
|
147 | - $date = sanitize_text_field( getpaid_format_date_value( $payment->get_date_created() ) ); |
|
148 | - $status = $payment->get_status_nicename(); |
|
149 | - $amount = wpinv_price( wpinv_format_amount( $payment->get_total() ), $payment->get_currency() ); |
|
150 | - } |
|
143 | + foreach ( $payments as $payment ) { |
|
144 | + $payment = new WPInv_Invoice( $payment ); |
|
145 | + $number = sanitize_text_field( $payment->get_number() ); |
|
146 | + $url = esc_url( $payment->get_view_url() ); |
|
147 | + $date = sanitize_text_field( getpaid_format_date_value( $payment->get_date_created() ) ); |
|
148 | + $status = $payment->get_status_nicename(); |
|
149 | + $amount = wpinv_price( wpinv_format_amount( $payment->get_total() ), $payment->get_currency() ); |
|
150 | + } |
|
151 | 151 | |
152 | 152 | ?> |
@@ -13,38 +13,38 @@ |
||
13 | 13 | |
14 | 14 | foreach ( array_keys( $widget->get_subscriptions_table_columns() ) as $column ) : |
15 | 15 | |
16 | - $class = sanitize_html_class( $column ); |
|
17 | - echo "<td class='getpaid-subscriptions-table-column-$class'>"; |
|
16 | + $class = sanitize_html_class( $column ); |
|
17 | + echo "<td class='getpaid-subscriptions-table-column-$class'>"; |
|
18 | 18 | |
19 | - do_action( "getpaid_subscriptions_before_frontend_subscription_table_$column", $subscription ); |
|
19 | + do_action( "getpaid_subscriptions_before_frontend_subscription_table_$column", $subscription ); |
|
20 | 20 | |
21 | - switch( $column ) : |
|
21 | + switch( $column ) : |
|
22 | 22 | |
23 | - case 'subscription': |
|
24 | - $subscription_id = (int) $subscription->get_id(); |
|
25 | - $url = esc_url( add_query_arg( 'subscription', $subscription_id, get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
|
26 | - echo $widget->add_row_actions( "<a href='$url' class='text-decoration-none'>#$subscription_id</a>", $subscription ); |
|
27 | - break; |
|
23 | + case 'subscription': |
|
24 | + $subscription_id = (int) $subscription->get_id(); |
|
25 | + $url = esc_url( add_query_arg( 'subscription', $subscription_id, get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
|
26 | + echo $widget->add_row_actions( "<a href='$url' class='text-decoration-none'>#$subscription_id</a>", $subscription ); |
|
27 | + break; |
|
28 | 28 | |
29 | - case 'status': |
|
30 | - echo $subscription->get_status_label(); |
|
31 | - break; |
|
29 | + case 'status': |
|
30 | + echo $subscription->get_status_label(); |
|
31 | + break; |
|
32 | 32 | |
33 | - case 'renewal-date': |
|
34 | - $renewal = getpaid_format_date_value( $subscription->get_next_renewal_date() ); |
|
35 | - echo $subscription->is_active() ? $renewal : "—"; |
|
36 | - break; |
|
33 | + case 'renewal-date': |
|
34 | + $renewal = getpaid_format_date_value( $subscription->get_next_renewal_date() ); |
|
35 | + echo $subscription->is_active() ? $renewal : "—"; |
|
36 | + break; |
|
37 | 37 | |
38 | - case 'amount': |
|
39 | - $frequency = sanitize_text_field( WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->get_period(), $subscription->get_frequency(), true ) ); |
|
40 | - $amount = wpinv_price( wpinv_format_amount( wpinv_sanitize_amount( $subscription->get_recurring_amount() ) ), $subscription->get_parent_payment()->get_currency() ); |
|
41 | - echo "<strong style='font-weight: 500;'>$amount</strong> / $frequency"; |
|
42 | - break; |
|
38 | + case 'amount': |
|
39 | + $frequency = sanitize_text_field( WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->get_period(), $subscription->get_frequency(), true ) ); |
|
40 | + $amount = wpinv_price( wpinv_format_amount( wpinv_sanitize_amount( $subscription->get_recurring_amount() ) ), $subscription->get_parent_payment()->get_currency() ); |
|
41 | + echo "<strong style='font-weight: 500;'>$amount</strong> / $frequency"; |
|
42 | + break; |
|
43 | 43 | |
44 | - endswitch; |
|
44 | + endswitch; |
|
45 | 45 | |
46 | - do_action( "getpaid_subscriptions_frontend_subscription_table_$column", $subscription ); |
|
46 | + do_action( "getpaid_subscriptions_frontend_subscription_table_$column", $subscription ); |
|
47 | 47 | |
48 | - echo '</td>'; |
|
48 | + echo '</td>'; |
|
49 | 49 | |
50 | 50 | endforeach; |
@@ -14,144 +14,144 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class WPInv_Subscriptions_Widget extends WP_Super_Duper { |
16 | 16 | |
17 | - /** |
|
18 | - * Register the widget with WordPress. |
|
19 | - * |
|
20 | - */ |
|
21 | - public function __construct() { |
|
22 | - |
|
23 | - $options = array( |
|
24 | - 'textdomain' => 'invoicing', |
|
25 | - 'block-icon' => 'controls-repeat', |
|
26 | - 'block-category'=> 'widgets', |
|
27 | - 'block-keywords'=> "['invoicing','subscriptions', 'getpaid']", |
|
28 | - 'class_name' => __CLASS__, |
|
29 | - 'base_id' => 'wpinv_subscriptions', |
|
30 | - 'name' => __( 'GetPaid > Subscriptions', 'invoicing' ), |
|
31 | - 'widget_ops' => array( |
|
32 | - 'classname' => 'getpaid-subscriptions bsui', |
|
33 | - 'description' => esc_html__( "Displays the current user's subscriptions.", 'invoicing' ), |
|
34 | - ), |
|
35 | - 'arguments' => array( |
|
36 | - 'title' => array( |
|
37 | - 'title' => __( 'Widget title', 'invoicing' ), |
|
38 | - 'desc' => __( 'Enter widget title.', 'invoicing' ), |
|
39 | - 'type' => 'text', |
|
40 | - 'desc_tip' => true, |
|
41 | - 'default' => '', |
|
42 | - 'advanced' => false |
|
43 | - ), |
|
44 | - ) |
|
45 | - |
|
46 | - ); |
|
47 | - |
|
48 | - |
|
49 | - parent::__construct( $options ); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Retrieves current user's subscriptions. |
|
54 | - * |
|
55 | - * @return GetPaid_Subscriptions_Query |
|
56 | - */ |
|
57 | - public function get_subscriptions() { |
|
58 | - |
|
59 | - // Prepare license args. |
|
60 | - $args = array( |
|
61 | - 'customer_in' => get_current_user_id(), |
|
62 | - 'paged' => ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1, |
|
63 | - ); |
|
64 | - |
|
65 | - return new GetPaid_Subscriptions_Query( $args ); |
|
66 | - |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * The Super block output function. |
|
71 | - * |
|
72 | - * @param array $args |
|
73 | - * @param array $widget_args |
|
74 | - * @param string $content |
|
75 | - * |
|
76 | - * @return mixed|string|bool |
|
77 | - */ |
|
78 | - public function output( $args = array(), $widget_args = array(), $content = '' ) { |
|
79 | - |
|
80 | - // Ensure that the user is logged in. |
|
81 | - if ( ! is_user_logged_in() ) { |
|
82 | - |
|
83 | - return aui()->alert( |
|
84 | - array( |
|
85 | - 'content' => wp_kses_post( __( 'You need to log-in or create an account to view this section.', 'invoicing' ) ), |
|
86 | - 'type' => 'error', |
|
87 | - ) |
|
88 | - ); |
|
89 | - |
|
90 | - } |
|
91 | - |
|
92 | - // Are we displaying a single subscription? |
|
93 | - if ( isset( $_GET['subscription'] ) ) { |
|
94 | - return $this->display_single_subscription( trim( $_GET['subscription'] ) ); |
|
95 | - } |
|
96 | - |
|
97 | - // Retrieve the user's subscriptions. |
|
98 | - $subscriptions = $this->get_subscriptions(); |
|
99 | - |
|
100 | - // Start the output buffer. |
|
101 | - ob_start(); |
|
102 | - |
|
103 | - // Backwards compatibility. |
|
104 | - do_action( 'wpinv_before_user_subscriptions' ); |
|
105 | - |
|
106 | - // Display errors and notices. |
|
107 | - wpinv_print_errors(); |
|
108 | - |
|
109 | - do_action( 'getpaid_license_manager_before_subscriptions', $subscriptions ); |
|
110 | - |
|
111 | - // Print the table header. |
|
112 | - $this->print_table_header(); |
|
113 | - |
|
114 | - // Print table body. |
|
115 | - $this->print_table_body( $subscriptions->get_results() ); |
|
116 | - |
|
117 | - // Print table footer. |
|
118 | - $this->print_table_footer(); |
|
119 | - |
|
120 | - // Print the navigation. |
|
121 | - $this->print_navigation( $subscriptions->get_total() ); |
|
122 | - |
|
123 | - // Backwards compatibility. |
|
124 | - do_action( 'wpinv_after_user_subscriptions' ); |
|
125 | - |
|
126 | - // Return the output. |
|
127 | - return ob_get_clean(); |
|
128 | - |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Retrieves the subscription columns. |
|
133 | - * |
|
134 | - * @return array |
|
135 | - */ |
|
136 | - public function get_subscriptions_table_columns() { |
|
17 | + /** |
|
18 | + * Register the widget with WordPress. |
|
19 | + * |
|
20 | + */ |
|
21 | + public function __construct() { |
|
22 | + |
|
23 | + $options = array( |
|
24 | + 'textdomain' => 'invoicing', |
|
25 | + 'block-icon' => 'controls-repeat', |
|
26 | + 'block-category'=> 'widgets', |
|
27 | + 'block-keywords'=> "['invoicing','subscriptions', 'getpaid']", |
|
28 | + 'class_name' => __CLASS__, |
|
29 | + 'base_id' => 'wpinv_subscriptions', |
|
30 | + 'name' => __( 'GetPaid > Subscriptions', 'invoicing' ), |
|
31 | + 'widget_ops' => array( |
|
32 | + 'classname' => 'getpaid-subscriptions bsui', |
|
33 | + 'description' => esc_html__( "Displays the current user's subscriptions.", 'invoicing' ), |
|
34 | + ), |
|
35 | + 'arguments' => array( |
|
36 | + 'title' => array( |
|
37 | + 'title' => __( 'Widget title', 'invoicing' ), |
|
38 | + 'desc' => __( 'Enter widget title.', 'invoicing' ), |
|
39 | + 'type' => 'text', |
|
40 | + 'desc_tip' => true, |
|
41 | + 'default' => '', |
|
42 | + 'advanced' => false |
|
43 | + ), |
|
44 | + ) |
|
45 | + |
|
46 | + ); |
|
47 | + |
|
48 | + |
|
49 | + parent::__construct( $options ); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Retrieves current user's subscriptions. |
|
54 | + * |
|
55 | + * @return GetPaid_Subscriptions_Query |
|
56 | + */ |
|
57 | + public function get_subscriptions() { |
|
58 | + |
|
59 | + // Prepare license args. |
|
60 | + $args = array( |
|
61 | + 'customer_in' => get_current_user_id(), |
|
62 | + 'paged' => ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1, |
|
63 | + ); |
|
64 | + |
|
65 | + return new GetPaid_Subscriptions_Query( $args ); |
|
66 | + |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * The Super block output function. |
|
71 | + * |
|
72 | + * @param array $args |
|
73 | + * @param array $widget_args |
|
74 | + * @param string $content |
|
75 | + * |
|
76 | + * @return mixed|string|bool |
|
77 | + */ |
|
78 | + public function output( $args = array(), $widget_args = array(), $content = '' ) { |
|
79 | + |
|
80 | + // Ensure that the user is logged in. |
|
81 | + if ( ! is_user_logged_in() ) { |
|
82 | + |
|
83 | + return aui()->alert( |
|
84 | + array( |
|
85 | + 'content' => wp_kses_post( __( 'You need to log-in or create an account to view this section.', 'invoicing' ) ), |
|
86 | + 'type' => 'error', |
|
87 | + ) |
|
88 | + ); |
|
89 | + |
|
90 | + } |
|
91 | + |
|
92 | + // Are we displaying a single subscription? |
|
93 | + if ( isset( $_GET['subscription'] ) ) { |
|
94 | + return $this->display_single_subscription( trim( $_GET['subscription'] ) ); |
|
95 | + } |
|
96 | + |
|
97 | + // Retrieve the user's subscriptions. |
|
98 | + $subscriptions = $this->get_subscriptions(); |
|
99 | + |
|
100 | + // Start the output buffer. |
|
101 | + ob_start(); |
|
102 | + |
|
103 | + // Backwards compatibility. |
|
104 | + do_action( 'wpinv_before_user_subscriptions' ); |
|
105 | + |
|
106 | + // Display errors and notices. |
|
107 | + wpinv_print_errors(); |
|
108 | + |
|
109 | + do_action( 'getpaid_license_manager_before_subscriptions', $subscriptions ); |
|
110 | + |
|
111 | + // Print the table header. |
|
112 | + $this->print_table_header(); |
|
113 | + |
|
114 | + // Print table body. |
|
115 | + $this->print_table_body( $subscriptions->get_results() ); |
|
116 | + |
|
117 | + // Print table footer. |
|
118 | + $this->print_table_footer(); |
|
119 | + |
|
120 | + // Print the navigation. |
|
121 | + $this->print_navigation( $subscriptions->get_total() ); |
|
122 | + |
|
123 | + // Backwards compatibility. |
|
124 | + do_action( 'wpinv_after_user_subscriptions' ); |
|
125 | + |
|
126 | + // Return the output. |
|
127 | + return ob_get_clean(); |
|
128 | + |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Retrieves the subscription columns. |
|
133 | + * |
|
134 | + * @return array |
|
135 | + */ |
|
136 | + public function get_subscriptions_table_columns() { |
|
137 | 137 | |
138 | - $columns = array( |
|
139 | - 'subscription' => __( 'Subscription', 'invoicing' ), |
|
140 | - 'amount' => __( 'Amount', 'invoicing' ), |
|
141 | - 'renewal-date' => __( 'Next payment', 'invoicing' ), |
|
142 | - 'status' => __( 'Status', 'invoicing' ), |
|
143 | - ); |
|
138 | + $columns = array( |
|
139 | + 'subscription' => __( 'Subscription', 'invoicing' ), |
|
140 | + 'amount' => __( 'Amount', 'invoicing' ), |
|
141 | + 'renewal-date' => __( 'Next payment', 'invoicing' ), |
|
142 | + 'status' => __( 'Status', 'invoicing' ), |
|
143 | + ); |
|
144 | 144 | |
145 | - return apply_filters( 'getpaid_frontend_subscriptions_table_columns', $columns ); |
|
146 | - } |
|
145 | + return apply_filters( 'getpaid_frontend_subscriptions_table_columns', $columns ); |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Displays the table header. |
|
150 | - * |
|
151 | - */ |
|
152 | - public function print_table_header() { |
|
148 | + /** |
|
149 | + * Displays the table header. |
|
150 | + * |
|
151 | + */ |
|
152 | + public function print_table_header() { |
|
153 | 153 | |
154 | - ?> |
|
154 | + ?> |
|
155 | 155 | |
156 | 156 | <style> |
157 | 157 | |
@@ -191,128 +191,128 @@ discard block |
||
191 | 191 | |
192 | 192 | <?php |
193 | 193 | |
194 | - } |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * Displays the table body. |
|
198 | - * |
|
199 | - * @param WPInv_Subscription[] $subscriptions |
|
200 | - */ |
|
201 | - public function print_table_body( $subscriptions ) { |
|
196 | + /** |
|
197 | + * Displays the table body. |
|
198 | + * |
|
199 | + * @param WPInv_Subscription[] $subscriptions |
|
200 | + */ |
|
201 | + public function print_table_body( $subscriptions ) { |
|
202 | 202 | |
203 | - if ( empty( $subscriptions ) ) { |
|
204 | - $this->print_table_body_no_subscriptions(); |
|
205 | - } else { |
|
206 | - $this->print_table_body_subscriptions( $subscriptions ); |
|
207 | - } |
|
203 | + if ( empty( $subscriptions ) ) { |
|
204 | + $this->print_table_body_no_subscriptions(); |
|
205 | + } else { |
|
206 | + $this->print_table_body_subscriptions( $subscriptions ); |
|
207 | + } |
|
208 | 208 | |
209 | - } |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * Displays the table body if no subscriptions were found. |
|
213 | - * |
|
214 | - */ |
|
215 | - public function print_table_body_no_subscriptions() { |
|
211 | + /** |
|
212 | + * Displays the table body if no subscriptions were found. |
|
213 | + * |
|
214 | + */ |
|
215 | + public function print_table_body_no_subscriptions() { |
|
216 | 216 | |
217 | - ?> |
|
217 | + ?> |
|
218 | 218 | <tbody> |
219 | 219 | |
220 | 220 | <tr> |
221 | 221 | <td colspan="<?php echo count( $this->get_subscriptions_table_columns() ); ?>"> |
222 | 222 | |
223 | 223 | <?php |
224 | - echo aui()->alert( |
|
225 | - array( |
|
226 | - 'content' => wp_kses_post( __( 'No subscriptions found.', 'invoicing' ) ), |
|
227 | - 'type' => 'warning', |
|
228 | - ) |
|
229 | - ); |
|
230 | - ?> |
|
224 | + echo aui()->alert( |
|
225 | + array( |
|
226 | + 'content' => wp_kses_post( __( 'No subscriptions found.', 'invoicing' ) ), |
|
227 | + 'type' => 'warning', |
|
228 | + ) |
|
229 | + ); |
|
230 | + ?> |
|
231 | 231 | |
232 | 232 | </td> |
233 | 233 | </tr> |
234 | 234 | |
235 | 235 | </tbody> |
236 | 236 | <?php |
237 | - } |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * Displays the table body if subscriptions were found. |
|
241 | - * |
|
242 | - * @param WPInv_Subscription[] $subscriptions |
|
243 | - */ |
|
244 | - public function print_table_body_subscriptions( $subscriptions ) { |
|
239 | + /** |
|
240 | + * Displays the table body if subscriptions were found. |
|
241 | + * |
|
242 | + * @param WPInv_Subscription[] $subscriptions |
|
243 | + */ |
|
244 | + public function print_table_body_subscriptions( $subscriptions ) { |
|
245 | 245 | |
246 | - ?> |
|
246 | + ?> |
|
247 | 247 | <tbody> |
248 | 248 | |
249 | 249 | <?php foreach ( $subscriptions as $subscription ) : ?> |
250 | 250 | <tr class="getpaid-subscriptions-table-row subscription-<?php echo (int) $subscription->get_id(); ?>"> |
251 | 251 | <?php |
252 | - wpinv_get_template( |
|
253 | - 'subscriptions/subscriptions-table-row.php', |
|
254 | - array( |
|
255 | - 'subscription' => $subscription, |
|
256 | - 'widget' => $this |
|
257 | - ) |
|
258 | - ); |
|
259 | - ?> |
|
252 | + wpinv_get_template( |
|
253 | + 'subscriptions/subscriptions-table-row.php', |
|
254 | + array( |
|
255 | + 'subscription' => $subscription, |
|
256 | + 'widget' => $this |
|
257 | + ) |
|
258 | + ); |
|
259 | + ?> |
|
260 | 260 | </tr> |
261 | 261 | <?php endforeach; ?> |
262 | 262 | |
263 | 263 | </tbody> |
264 | 264 | <?php |
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Adds row actions to a column |
|
269 | - * |
|
270 | - * @param string $content column content |
|
271 | - * @param WPInv_Subscription $subscription |
|
272 | - * @since 1.0.0 |
|
273 | - * @return string |
|
274 | - */ |
|
275 | - public function add_row_actions( $content, $subscription ) { |
|
276 | - |
|
277 | - // Prepare row actions. |
|
278 | - $actions = array(); |
|
279 | - |
|
280 | - // View subscription action. |
|
281 | - $view_url = esc_url( add_query_arg( 'subscription', (int) $subscription->get_id(), get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
|
282 | - $actions['view'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>'; |
|
283 | - |
|
284 | - // View invoice action. |
|
285 | - $invoice = $subscription->get_parent_payment(); |
|
286 | - |
|
287 | - if ( $invoice->get_id() ) { |
|
288 | - $view_url = esc_url( $invoice->get_view_url() ); |
|
289 | - $actions['invoice'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'View Invoice', 'invoicing' ) . '</a>'; |
|
290 | - } |
|
291 | - |
|
292 | - // Filter the actions. |
|
293 | - $actions = apply_filters( 'getpaid_subscriptions_table_subscription_actions', $actions, $subscription ); |
|
294 | - |
|
295 | - $sanitized = array(); |
|
296 | - foreach ( $actions as $key => $action ) { |
|
297 | - $key = sanitize_html_class( $key ); |
|
298 | - $action = wp_kses_post( $action ); |
|
299 | - $sanitized[] = "<span class='$key'>$action</span>"; |
|
300 | - } |
|
301 | - |
|
302 | - $row_actions = "<small class='form-text getpaid-subscription-item-actions'>"; |
|
303 | - $row_actions .= implode( ' | ', $sanitized ); |
|
304 | - $row_actions .= '</small>'; |
|
305 | - |
|
306 | - return $content . $row_actions; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Displays the table footer. |
|
311 | - * |
|
312 | - */ |
|
313 | - public function print_table_footer() { |
|
314 | - |
|
315 | - ?> |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Adds row actions to a column |
|
269 | + * |
|
270 | + * @param string $content column content |
|
271 | + * @param WPInv_Subscription $subscription |
|
272 | + * @since 1.0.0 |
|
273 | + * @return string |
|
274 | + */ |
|
275 | + public function add_row_actions( $content, $subscription ) { |
|
276 | + |
|
277 | + // Prepare row actions. |
|
278 | + $actions = array(); |
|
279 | + |
|
280 | + // View subscription action. |
|
281 | + $view_url = esc_url( add_query_arg( 'subscription', (int) $subscription->get_id(), get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
|
282 | + $actions['view'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>'; |
|
283 | + |
|
284 | + // View invoice action. |
|
285 | + $invoice = $subscription->get_parent_payment(); |
|
286 | + |
|
287 | + if ( $invoice->get_id() ) { |
|
288 | + $view_url = esc_url( $invoice->get_view_url() ); |
|
289 | + $actions['invoice'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'View Invoice', 'invoicing' ) . '</a>'; |
|
290 | + } |
|
291 | + |
|
292 | + // Filter the actions. |
|
293 | + $actions = apply_filters( 'getpaid_subscriptions_table_subscription_actions', $actions, $subscription ); |
|
294 | + |
|
295 | + $sanitized = array(); |
|
296 | + foreach ( $actions as $key => $action ) { |
|
297 | + $key = sanitize_html_class( $key ); |
|
298 | + $action = wp_kses_post( $action ); |
|
299 | + $sanitized[] = "<span class='$key'>$action</span>"; |
|
300 | + } |
|
301 | + |
|
302 | + $row_actions = "<small class='form-text getpaid-subscription-item-actions'>"; |
|
303 | + $row_actions .= implode( ' | ', $sanitized ); |
|
304 | + $row_actions .= '</small>'; |
|
305 | + |
|
306 | + return $content . $row_actions; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Displays the table footer. |
|
311 | + * |
|
312 | + */ |
|
313 | + public function print_table_footer() { |
|
314 | + |
|
315 | + ?> |
|
316 | 316 | |
317 | 317 | <tfoot> |
318 | 318 | <tr> |
@@ -327,130 +327,130 @@ discard block |
||
327 | 327 | </table> |
328 | 328 | <?php |
329 | 329 | |
330 | - } |
|
330 | + } |
|
331 | 331 | |
332 | - /** |
|
333 | - * Displays the navigation. |
|
334 | - * |
|
335 | - * @param int $total |
|
336 | - */ |
|
337 | - public function print_navigation( $total ) { |
|
332 | + /** |
|
333 | + * Displays the navigation. |
|
334 | + * |
|
335 | + * @param int $total |
|
336 | + */ |
|
337 | + public function print_navigation( $total ) { |
|
338 | 338 | |
339 | - if ( $total < 1 ) { |
|
339 | + if ( $total < 1 ) { |
|
340 | 340 | |
341 | - // Out-of-bounds, run the query again without LIMIT for total count. |
|
342 | - $args = array( |
|
343 | - 'customer_in' => get_current_user_id(), |
|
344 | - 'fields' => 'id', |
|
345 | - ); |
|
341 | + // Out-of-bounds, run the query again without LIMIT for total count. |
|
342 | + $args = array( |
|
343 | + 'customer_in' => get_current_user_id(), |
|
344 | + 'fields' => 'id', |
|
345 | + ); |
|
346 | 346 | |
347 | - $count_query = new GetPaid_Subscriptions_Query( $args ); |
|
348 | - $total = $count_query->get_total(); |
|
349 | - } |
|
347 | + $count_query = new GetPaid_Subscriptions_Query( $args ); |
|
348 | + $total = $count_query->get_total(); |
|
349 | + } |
|
350 | 350 | |
351 | - // Abort if we do not have pages. |
|
352 | - if ( 2 > $total ) { |
|
353 | - return; |
|
354 | - } |
|
351 | + // Abort if we do not have pages. |
|
352 | + if ( 2 > $total ) { |
|
353 | + return; |
|
354 | + } |
|
355 | 355 | |
356 | - ?> |
|
356 | + ?> |
|
357 | 357 | |
358 | 358 | <div class="getpaid-subscriptions-pagination"> |
359 | 359 | <?php |
360 | - $big = 999999; |
|
361 | - |
|
362 | - echo getpaid_paginate_links( |
|
363 | - array( |
|
364 | - 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
|
365 | - 'format' => '?paged=%#%', |
|
366 | - 'total' => (int) ceil( $total / 10 ), |
|
367 | - ) |
|
368 | - ); |
|
369 | - ?> |
|
360 | + $big = 999999; |
|
361 | + |
|
362 | + echo getpaid_paginate_links( |
|
363 | + array( |
|
364 | + 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
|
365 | + 'format' => '?paged=%#%', |
|
366 | + 'total' => (int) ceil( $total / 10 ), |
|
367 | + ) |
|
368 | + ); |
|
369 | + ?> |
|
370 | 370 | </div> |
371 | 371 | |
372 | 372 | <?php |
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Returns a single subscription's columns. |
|
377 | - * |
|
378 | - * @param WPInv_Subscription $subscription |
|
379 | - * |
|
380 | - * @return array |
|
381 | - */ |
|
382 | - public function get_single_subscription_columns( $subscription ) { |
|
383 | - |
|
384 | - // Prepare subscription detail columns. |
|
385 | - $fields = apply_filters( |
|
386 | - 'getpaid_single_subscription_details_fields', |
|
387 | - array( |
|
388 | - 'status' => __( 'Status', 'invoicing' ), |
|
389 | - 'initial_amount' => __( 'Initial amount', 'invoicing' ), |
|
390 | - 'recurring_amount' => __( 'Recurring amount', 'invoicing' ), |
|
391 | - 'start_date' => __( 'Start date', 'invoicing' ), |
|
392 | - 'expiry_date' => __( 'Next payment', 'invoicing' ), |
|
393 | - 'payments' => __( 'Payments', 'invoicing' ), |
|
394 | - 'item' => __( 'Item', 'invoicing' ), |
|
395 | - 'invoice' => __( 'Invoice', 'invoicing' ), |
|
396 | - ), |
|
397 | - $subscription |
|
398 | - ); |
|
399 | - |
|
400 | - if ( ! $subscription->is_active() || $subscription->is_last_renewal() ) { |
|
401 | - $fields['expiry_date'] = __( 'End date', 'invoicing' ); |
|
402 | - } |
|
403 | - |
|
404 | - if ( $subscription->get_initial_amount() == $subscription->get_recurring_amount() ) { |
|
405 | - unset( $fields['initial_amount'] ); |
|
406 | - } |
|
407 | - |
|
408 | - return $fields; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Displays a single subscription. |
|
413 | - * |
|
414 | - * @param string $subscription |
|
415 | - * |
|
416 | - * @return string |
|
417 | - */ |
|
418 | - public function display_single_subscription( $subscription ) { |
|
419 | - |
|
420 | - // Fetch the subscription. |
|
421 | - $subscription = new WPInv_Subscription( (int) $subscription ); |
|
422 | - |
|
423 | - if ( ! $subscription->get_id() ) { |
|
424 | - |
|
425 | - return aui()->alert( |
|
426 | - array( |
|
427 | - 'content' => wp_kses_post( __( 'Subscription not found.', 'invoicing' ) ), |
|
428 | - 'type' => 'error', |
|
429 | - ) |
|
430 | - ); |
|
431 | - |
|
432 | - } |
|
433 | - |
|
434 | - // Ensure that the user owns this subscription key. |
|
435 | - if ( get_current_user_id() != $subscription->get_customer_id() ) { |
|
436 | - |
|
437 | - return aui()->alert( |
|
438 | - array( |
|
439 | - 'content' => wp_kses_post( __( 'You do not have permission to view this subscription. Ensure that you are logged in to the account that owns the subscription.', 'invoicing' ) ), |
|
440 | - 'type' => 'error', |
|
441 | - ) |
|
442 | - ); |
|
443 | - |
|
444 | - } |
|
445 | - |
|
446 | - return wpinv_get_template_html( |
|
447 | - 'subscriptions/subscription-details.php', |
|
448 | - array( |
|
449 | - 'subscription' => $subscription, |
|
450 | - 'widget' => $this |
|
451 | - ) |
|
452 | - ); |
|
453 | - |
|
454 | - } |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Returns a single subscription's columns. |
|
377 | + * |
|
378 | + * @param WPInv_Subscription $subscription |
|
379 | + * |
|
380 | + * @return array |
|
381 | + */ |
|
382 | + public function get_single_subscription_columns( $subscription ) { |
|
383 | + |
|
384 | + // Prepare subscription detail columns. |
|
385 | + $fields = apply_filters( |
|
386 | + 'getpaid_single_subscription_details_fields', |
|
387 | + array( |
|
388 | + 'status' => __( 'Status', 'invoicing' ), |
|
389 | + 'initial_amount' => __( 'Initial amount', 'invoicing' ), |
|
390 | + 'recurring_amount' => __( 'Recurring amount', 'invoicing' ), |
|
391 | + 'start_date' => __( 'Start date', 'invoicing' ), |
|
392 | + 'expiry_date' => __( 'Next payment', 'invoicing' ), |
|
393 | + 'payments' => __( 'Payments', 'invoicing' ), |
|
394 | + 'item' => __( 'Item', 'invoicing' ), |
|
395 | + 'invoice' => __( 'Invoice', 'invoicing' ), |
|
396 | + ), |
|
397 | + $subscription |
|
398 | + ); |
|
399 | + |
|
400 | + if ( ! $subscription->is_active() || $subscription->is_last_renewal() ) { |
|
401 | + $fields['expiry_date'] = __( 'End date', 'invoicing' ); |
|
402 | + } |
|
403 | + |
|
404 | + if ( $subscription->get_initial_amount() == $subscription->get_recurring_amount() ) { |
|
405 | + unset( $fields['initial_amount'] ); |
|
406 | + } |
|
407 | + |
|
408 | + return $fields; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Displays a single subscription. |
|
413 | + * |
|
414 | + * @param string $subscription |
|
415 | + * |
|
416 | + * @return string |
|
417 | + */ |
|
418 | + public function display_single_subscription( $subscription ) { |
|
419 | + |
|
420 | + // Fetch the subscription. |
|
421 | + $subscription = new WPInv_Subscription( (int) $subscription ); |
|
422 | + |
|
423 | + if ( ! $subscription->get_id() ) { |
|
424 | + |
|
425 | + return aui()->alert( |
|
426 | + array( |
|
427 | + 'content' => wp_kses_post( __( 'Subscription not found.', 'invoicing' ) ), |
|
428 | + 'type' => 'error', |
|
429 | + ) |
|
430 | + ); |
|
431 | + |
|
432 | + } |
|
433 | + |
|
434 | + // Ensure that the user owns this subscription key. |
|
435 | + if ( get_current_user_id() != $subscription->get_customer_id() ) { |
|
436 | + |
|
437 | + return aui()->alert( |
|
438 | + array( |
|
439 | + 'content' => wp_kses_post( __( 'You do not have permission to view this subscription. Ensure that you are logged in to the account that owns the subscription.', 'invoicing' ) ), |
|
440 | + 'type' => 'error', |
|
441 | + ) |
|
442 | + ); |
|
443 | + |
|
444 | + } |
|
445 | + |
|
446 | + return wpinv_get_template_html( |
|
447 | + 'subscriptions/subscription-details.php', |
|
448 | + array( |
|
449 | + 'subscription' => $subscription, |
|
450 | + 'widget' => $this |
|
451 | + ) |
|
452 | + ); |
|
453 | + |
|
454 | + } |
|
455 | 455 | |
456 | 456 | } |