Passed
Pull Request — master (#413)
by Brian
04:29
created

WPInv_Subscriptions_Widget::print_table_body()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
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
			<style>
157
158
				.getpaid-subscriptions table {
159
					font-size: 0.9em;
160
					table-layout: fixed;
161
				}
162
163
				.getpaid-subscriptions-table-column-subscription {
164
					font-weight: 500;
165
				}
166
167
				.getpaid-subscriptions-table-row span.label {
168
					font-weight: 500;
169
				}
170
171
				.getpaid-subscriptions.bsui .table-bordered thead th {
172
					border-bottom-width: 1px;
173
				}
174
175
				.getpaid-subscriptions.bsui .table-striped tbody tr:nth-of-type(odd) {
176
					background-color: rgb(0 0 0 / 0.01);
177
				}
178
			</style>
179
180
			<table class="table table-bordered table-striped">
181
182
				<thead>
183
					<tr>
184
						<?php foreach ( $this->get_subscriptions_table_columns() as $key => $label ) : ?>
185
							<th scope="col" class="getpaid-subscriptions-table-<?php echo sanitize_html_class( $key ); ?>">
186
								<?php echo sanitize_text_field( $label ); ?>
187
							</th>
188
						<?php endforeach; ?>
189
					</tr>
190
				</thead>
191
192
		<?php
193
194
	}
195
196
	/**
197
	 * Displays the table body.
198
	 *
199
	 * @param WPInv_Subscription[] $subscriptions
200
	 */
201
	public function print_table_body( $subscriptions ) {
202
203
		if ( empty( $subscriptions ) ) {
204
			$this->print_table_body_no_subscriptions();
205
		} else {
206
			$this->print_table_body_subscriptions( $subscriptions );
207
		}
208
209
	}
210
211
	/**
212
	 * Displays the table body if no subscriptions were found.
213
	 *
214
	 */
215
	public function print_table_body_no_subscriptions() {
216
217
		?>
218
		<tbody>
219
220
			<tr>
221
				<td colspan="<?php echo count( $this->get_subscriptions_table_columns() ); ?>">
222
223
					<?php
224
						echo aui()->alert(
225
							array(
226
								'content' => wp_kses_post( __( 'No subscriptions found.', 'invoicing' ) ),
227
								'type'    => 'warning',
228
							)
229
						);
230
					?>
231
232
				</td>
233
			</tr>
234
235
		</tbody>
236
		<?php
237
	}
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 ) {
245
246
		?>
247
		<tbody>
248
249
			<?php foreach ( $subscriptions as $subscription ) : ?>
250
				<tr class="getpaid-subscriptions-table-row subscription-<?php echo (int) $subscription->get_id(); ?>">
251
					<?php
252
						wpinv_get_template(
253
							'subscriptions/subscriptions-table-row.php',
254
							array(
255
								'subscription' => $subscription,
256
								'widget'       => $this
257
							)
258
						);
259
					?>
260
				</tr>
261
			<?php endforeach; ?>
262
263
		</tbody>
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
		?>
316
317
				<tfoot>
318
					<tr>
319
						<?php foreach ( $this->get_subscriptions_table_columns() as $key => $label ) : ?>
320
							<th class="getpaid-subscriptions-<?php echo sanitize_html_class( $key ); ?>">
321
								<?php echo sanitize_text_field( $label ); ?>
322
							</th>
323
						<?php endforeach; ?>
324
					</tr>
325
				</tfoot>
326
327
			</table>
328
		<?php
329
330
	}
331
332
	/**
333
	 * Displays the navigation.
334
	 *
335
	 * @param int $total
336
	 */
337
	public function print_navigation( $total ) {
338
339
		if ( $total < 1 ) {
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
			);
346
347
			$count_query = new GetPaid_Subscriptions_Query( $args );
348
			$total       = $count_query->get_total();
349
		}
350
351
		// Abort if we do not have pages.
352
		if ( 2 > $total ) {
353
			return;
354
		}
355
356
		?>
357
358
		<div class="getpaid-subscriptions-pagination">
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
			?>
370
		</div>
371
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
	}
455
456
}
457