Passed
Pull Request — master (#413)
by Brian
05:26
created

print_table_body_subscriptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 19
rs 9.7
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
			<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="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        = esc_url( add_query_arg( 'subscription', (int) $subscription->get_id(), get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) );
258
		$actions['view'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>';
259
260
		// Filter the actions.
261
		$actions = apply_filters( 'getpaid_subscriptions_table_subscription_actions', $actions, $subscription );
262
263
		$sanitized  = array();
264
		foreach ( $actions as $key => $action ) {
265
			$key         = sanitize_html_class( $key );
266
			$action      = wp_kses_post( $action );
267
			$sanitized[] = "<span class='$key'>$action</span>";
268
		}
269
270
		$row_actions  = "<small class='form-text getpaid-subscription-item-actions'>";
271
		$row_actions .= implode( ' | ', $sanitized );
272
		$row_actions .= '</small>';
273
274
		return $content . $row_actions;
275
	}
276
277
	/**
278
	 * Displays the table footer.
279
	 *
280
	 */
281
	public function print_table_footer() {
282
283
		?>
284
285
				<tfoot>
286
					<tr>
287
						<?php foreach ( $this->get_subscriptions_table_columns() as $key => $label ) : ?>
288
							<th class="getpaid-subscriptions-<?php echo sanitize_html_class( $key ); ?>">
289
								<?php echo sanitize_text_field( $label ); ?>
290
							</th>
291
						<?php endforeach; ?>
292
					</tr>
293
				</tfoot>
294
295
			</table>
296
		<?php
297
298
	}
299
300
	/**
301
	 * Displays the navigation.
302
	 *
303
	 * @param int $total
304
	 */
305
	public function print_navigation( $total ) {
306
307
		if ( $total < 1 ) {
308
309
			// Out-of-bounds, run the query again without LIMIT for total count.
310
			$args  = array(
311
				'customer_in' => get_current_user_id(),
312
				'fields'      => 'id',
313
			);
314
315
			$count_query = new GetPaid_Subscriptions_Query( $args );
316
			$total       = $count_query->get_total();
317
		}
318
319
		// Abort if we do not have pages.
320
		if ( 2 > $total ) {
321
			return;
322
		}
323
324
		?>
325
326
		<div class="getpaid-subscriptions-pagination">
327
			<?php
328
				$big = 999999;
329
330
				echo getpaid_paginate_links(
331
					array(
332
						'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
333
						'format'  => '?paged=%#%',
334
						'total'   => (int) ceil( $total / 10 ),
335
					)
336
				);
337
			?>
338
		</div>
339
340
		<?php
341
	}
342
343
	/**
344
	 * Returns a single subscription's columns.
345
	 *
346
	 * @param WPInv_Subscription $subscription
347
	 *
348
	 * @return array
349
	 */
350
	public function get_single_subscription_columns( $subscription ) {
351
352
		// Prepare subscription detail columns.
353
		$fields = apply_filters(
354
			'getpaid_single_subscription_details_fields',
355
			array(
356
				'status'           => __( 'Status', 'invoicing' ),
357
				'initial_amount'   => __( 'Initial amount', 'invoicing' ),
358
				'recurring_amount' => __( 'Recurring amount', 'invoicing' ),
359
				'start_date'       => __( 'Start date', 'invoicing' ),
360
				'expiry_date'      => __( 'Next payment', 'invoicing' ),
361
				'payments'         => __( 'Payments', 'invoicing' ),
362
				'item'             => __( 'Item', 'invoicing' ),
363
			),
364
			$subscription
365
		);
366
367
		if ( ! $subscription->is_active() || $subscription->is_last_renewal() ) {
368
			$fields['expiry_date'] = __( 'End date', 'invoicing' );
369
		}
370
371
		if ( $subscription->get_initial_amount() == $subscription->get_recurring_amount() ) {
372
			unset( $fields['initial_amount'] );
373
		}
374
375
		return $fields;
376
	}
377
378
	/**
379
	 * Displays a single subscription.
380
	 *
381
	 * @param string $subscription
382
	 *
383
	 * @return string
384
	 */
385
	public function display_single_subscription( $subscription ) {
386
387
		// Fetch the subscription.
388
		$subscription = new WPInv_Subscription( (int) $subscription );
389
390
		if ( ! $subscription->get_id() ) {
391
392
			return aui()->alert(
393
				array(
394
					'content' => wp_kses_post( __( 'Subscription not found.', 'invoicing' ) ),
395
					'type'    => 'error',
396
				)
397
			);
398
399
		}
400
401
		// Ensure that the user owns this subscription key.
402
		if ( get_current_user_id() != $subscription->get_customer_id() ) {
403
404
			return aui()->alert(
405
				array(
406
					'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' ) ),
407
					'type'    => 'error',
408
				)
409
			);
410
411
		}
412
413
		return wpinv_get_template_html(
414
			'subscriptions/subscription-details.php',
415
			array(
416
				'subscription' => $subscription,
417
				'widget'       => $this
418
			)
419
		);
420
421
	}
422
423
}
424