1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Subscriptions Widget Class. |
4
|
|
|
* |
5
|
|
|
* @version 1.0.0 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
defined( 'ABSPATH' ) || exit; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Contains the subscriptions widget. |
12
|
|
|
* |
13
|
|
|
* @package INVOICING |
14
|
|
|
*/ |
15
|
|
|
class WPInv_Subscriptions_Widget extends WP_Super_Duper { |
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() { |
137
|
|
|
|
138
|
|
|
$columns = array( |
139
|
|
|
'subscription' => __( 'Subscription', 'invoicing' ), |
140
|
|
|
'amount' => __( 'Amount', 'invoicing' ), |
141
|
|
|
'renewal-date' => __( 'Next payment', 'invoicing' ), |
142
|
|
|
'status' => __( 'Status', 'invoicing' ), |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
return apply_filters( 'getpaid_frontend_subscriptions_table_columns', $columns ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Displays the table header. |
150
|
|
|
* |
151
|
|
|
*/ |
152
|
|
|
public function print_table_header() { |
153
|
|
|
|
154
|
|
|
?> |
155
|
|
|
|
156
|
|
|
<table class="table table-bordered table-striped"> |
157
|
|
|
|
158
|
|
|
<thead> |
159
|
|
|
<tr> |
160
|
|
|
<?php foreach ( $this->get_subscriptions_table_columns() as $key => $label ) : ?> |
161
|
|
|
<th scope="col" class="font-weight-bold getpaid-subscriptions-table-<?php echo sanitize_html_class( $key ); ?>"> |
162
|
|
|
<?php echo sanitize_text_field( $label ); ?> |
163
|
|
|
</th> |
164
|
|
|
<?php endforeach; ?> |
165
|
|
|
</tr> |
166
|
|
|
</thead> |
167
|
|
|
|
168
|
|
|
<?php |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Displays the table body. |
174
|
|
|
* |
175
|
|
|
* @param WPInv_Subscription[] $subscriptions |
176
|
|
|
*/ |
177
|
|
|
public function print_table_body( $subscriptions ) { |
178
|
|
|
|
179
|
|
|
if ( empty( $subscriptions ) ) { |
180
|
|
|
$this->print_table_body_no_subscriptions(); |
181
|
|
|
} else { |
182
|
|
|
$this->print_table_body_subscriptions( $subscriptions ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Displays the table body if no subscriptions were found. |
189
|
|
|
* |
190
|
|
|
*/ |
191
|
|
|
public function print_table_body_no_subscriptions() { |
192
|
|
|
|
193
|
|
|
?> |
194
|
|
|
<tbody> |
195
|
|
|
|
196
|
|
|
<tr> |
197
|
|
|
<td colspan="<?php echo count( $this->get_subscriptions_table_columns() ); ?>"> |
198
|
|
|
|
199
|
|
|
<?php |
200
|
|
|
echo aui()->alert( |
201
|
|
|
array( |
202
|
|
|
'content' => wp_kses_post( __( 'No subscriptions found.', 'invoicing' ) ), |
203
|
|
|
'type' => 'warning', |
204
|
|
|
) |
205
|
|
|
); |
206
|
|
|
?> |
207
|
|
|
|
208
|
|
|
</td> |
209
|
|
|
</tr> |
210
|
|
|
|
211
|
|
|
</tbody> |
212
|
|
|
<?php |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Displays the table body if subscriptions were found. |
217
|
|
|
* |
218
|
|
|
* @param WPInv_Subscription[] $subscriptions |
219
|
|
|
*/ |
220
|
|
|
public function print_table_body_subscriptions( $subscriptions ) { |
221
|
|
|
|
222
|
|
|
?> |
223
|
|
|
<tbody> |
224
|
|
|
|
225
|
|
|
<?php foreach ( $subscriptions as $subscription ) : ?> |
226
|
|
|
<tr class="getpaid-subscriptions-table-row subscription-<?php echo (int) $subscription->get_id(); ?>"> |
227
|
|
|
<?php |
228
|
|
|
wpinv_get_template( |
229
|
|
|
'subscriptions/subscriptions-table-row.php', |
230
|
|
|
array( |
231
|
|
|
'subscription' => $subscription, |
232
|
|
|
'widget' => $this |
233
|
|
|
) |
234
|
|
|
); |
235
|
|
|
?> |
236
|
|
|
</tr> |
237
|
|
|
<?php endforeach; ?> |
238
|
|
|
|
239
|
|
|
</tbody> |
240
|
|
|
<?php |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Adds row actions to a column |
245
|
|
|
* |
246
|
|
|
* @param string $content column content |
247
|
|
|
* @param WPInv_Subscription $subscription |
248
|
|
|
* @since 1.0.0 |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
public function add_row_actions( $content, $subscription ) { |
252
|
|
|
|
253
|
|
|
// Prepare row actions. |
254
|
|
|
$actions = array(); |
255
|
|
|
|
256
|
|
|
// View subscription action. |
257
|
|
|
$view_url = getpaid_get_tab_url( 'gp-subscriptions', get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ); |
|
|
|
|
258
|
|
|
$view_url = esc_url( add_query_arg( 'subscription', (int) $subscription->get_id(), $view_url ) ); |
259
|
|
|
$actions['view'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>'; |
260
|
|
|
|
261
|
|
|
// Filter the actions. |
262
|
|
|
$actions = apply_filters( 'getpaid_subscriptions_table_subscription_actions', $actions, $subscription ); |
263
|
|
|
|
264
|
|
|
$sanitized = array(); |
265
|
|
|
foreach ( $actions as $key => $action ) { |
266
|
|
|
$key = sanitize_html_class( $key ); |
267
|
|
|
$action = wp_kses_post( $action ); |
268
|
|
|
$sanitized[] = "<span class='$key'>$action</span>"; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$row_actions = "<small class='form-text getpaid-subscription-item-actions'>"; |
272
|
|
|
$row_actions .= implode( ' | ', $sanitized ); |
273
|
|
|
$row_actions .= '</small>'; |
274
|
|
|
|
275
|
|
|
return $content . $row_actions; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Displays the table footer. |
280
|
|
|
* |
281
|
|
|
*/ |
282
|
|
|
public function print_table_footer() { |
283
|
|
|
|
284
|
|
|
?> |
285
|
|
|
|
286
|
|
|
<tfoot> |
287
|
|
|
<tr> |
288
|
|
|
<?php foreach ( $this->get_subscriptions_table_columns() as $key => $label ) : ?> |
289
|
|
|
<th class="font-weight-bold getpaid-subscriptions-<?php echo sanitize_html_class( $key ); ?>"> |
290
|
|
|
<?php echo sanitize_text_field( $label ); ?> |
291
|
|
|
</th> |
292
|
|
|
<?php endforeach; ?> |
293
|
|
|
</tr> |
294
|
|
|
</tfoot> |
295
|
|
|
|
296
|
|
|
</table> |
297
|
|
|
<?php |
298
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Displays the navigation. |
303
|
|
|
* |
304
|
|
|
* @param int $total |
305
|
|
|
*/ |
306
|
|
|
public function print_navigation( $total ) { |
307
|
|
|
|
308
|
|
|
if ( $total < 1 ) { |
309
|
|
|
|
310
|
|
|
// Out-of-bounds, run the query again without LIMIT for total count. |
311
|
|
|
$args = array( |
312
|
|
|
'customer_in' => get_current_user_id(), |
313
|
|
|
'fields' => 'id', |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
$count_query = new GetPaid_Subscriptions_Query( $args ); |
317
|
|
|
$total = $count_query->get_total(); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
// Abort if we do not have pages. |
321
|
|
|
if ( 2 > $total ) { |
322
|
|
|
return; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
?> |
326
|
|
|
|
327
|
|
|
<div class="getpaid-subscriptions-pagination"> |
328
|
|
|
<?php |
329
|
|
|
$big = 999999; |
330
|
|
|
|
331
|
|
|
echo getpaid_paginate_links( |
332
|
|
|
array( |
333
|
|
|
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
334
|
|
|
'format' => '?paged=%#%', |
335
|
|
|
'total' => (int) ceil( $total / 10 ), |
336
|
|
|
) |
337
|
|
|
); |
338
|
|
|
?> |
339
|
|
|
</div> |
340
|
|
|
|
341
|
|
|
<?php |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Returns a single subscription's columns. |
346
|
|
|
* |
347
|
|
|
* @param WPInv_Subscription $subscription |
348
|
|
|
* |
349
|
|
|
* @return array |
350
|
|
|
*/ |
351
|
|
|
public function get_single_subscription_columns( $subscription ) { |
352
|
|
|
|
353
|
|
|
// Prepare subscription detail columns. |
354
|
|
|
$subscription_group = getpaid_get_invoice_subscription_group( $subscription->get_parent_invoice_id(), $subscription->get_id() ); |
355
|
|
|
$items_count = empty( $subscription_group ) ? 1 : count( $subscription_group['items'] ); |
356
|
|
|
$fields = apply_filters( |
357
|
|
|
'getpaid_single_subscription_details_fields', |
358
|
|
|
array( |
359
|
|
|
'status' => __( 'Status', 'invoicing' ), |
360
|
|
|
'initial_amount' => __( 'Initial amount', 'invoicing' ), |
361
|
|
|
'recurring_amount' => __( 'Recurring amount', 'invoicing' ), |
362
|
|
|
'start_date' => __( 'Start date', 'invoicing' ), |
363
|
|
|
'expiry_date' => __( 'Next payment', 'invoicing' ), |
364
|
|
|
'payments' => __( 'Payments', 'invoicing' ), |
365
|
|
|
'item' => _n( 'Item', 'Items', $items_count, 'invoicing' ), |
366
|
|
|
), |
367
|
|
|
$subscription |
368
|
|
|
); |
369
|
|
|
|
370
|
|
|
if ( isset( $fields['expiry_date'] ) ) { |
371
|
|
|
|
372
|
|
|
if ( ! $subscription->is_active() || $subscription->is_last_renewal() ) { |
373
|
|
|
$fields['expiry_date'] = __( 'End date', 'invoicing' ); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
if ( 'pending' == $subscription->get_status() ) { |
377
|
|
|
unset( $fields['expiry_date'] ); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
if ( isset( $fields['start_date'] ) && 'pending' == $subscription->get_status() ) { |
383
|
|
|
unset( $fields['start_date'] ); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
if ( $subscription->get_initial_amount() == $subscription->get_recurring_amount() ) { |
387
|
|
|
unset( $fields['initial_amount'] ); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
return $fields; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Displays a single subscription. |
395
|
|
|
* |
396
|
|
|
* @param string $subscription |
397
|
|
|
* |
398
|
|
|
* @return string |
399
|
|
|
*/ |
400
|
|
|
public function display_single_subscription( $subscription ) { |
401
|
|
|
|
402
|
|
|
// Fetch the subscription. |
403
|
|
|
$subscription = new WPInv_Subscription( (int) $subscription ); |
404
|
|
|
|
405
|
|
|
if ( ! $subscription->exists() ) { |
406
|
|
|
|
407
|
|
|
return aui()->alert( |
408
|
|
|
array( |
409
|
|
|
'content' => wp_kses_post( __( 'Subscription not found.', 'invoicing' ) ), |
410
|
|
|
'type' => 'error', |
411
|
|
|
) |
412
|
|
|
); |
413
|
|
|
|
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
// Ensure that the user owns this subscription key. |
417
|
|
|
if ( get_current_user_id() != $subscription->get_customer_id() && ! wpinv_current_user_can_manage_invoicing() ) { |
418
|
|
|
|
419
|
|
|
return aui()->alert( |
420
|
|
|
array( |
421
|
|
|
'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' ) ), |
422
|
|
|
'type' => 'error', |
423
|
|
|
) |
424
|
|
|
); |
425
|
|
|
|
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
return wpinv_get_template_html( |
429
|
|
|
'subscriptions/subscription-details.php', |
430
|
|
|
array( |
431
|
|
|
'subscription' => $subscription, |
432
|
|
|
'widget' => $this |
433
|
|
|
) |
434
|
|
|
); |
435
|
|
|
|
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
} |
439
|
|
|
|