Passed
Push — master ( 72aaeb...55577f )
by Brian
04:34
created

getpaid_admin_subscription_item_details_metabox()   F

Complexity

Conditions 23
Paths 841

Size

Total Lines 123
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
eloc 85
c 1
b 0
f 0
nc 841
nop 1
dl 0
loc 123
rs 0.2208

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Contains functions that display the subscriptions admin page.
4
 */
5
6
defined( 'ABSPATH' ) || exit;
7
8
/**
9
 * Render the Subscriptions page
10
 *
11
 * @access      public
12
 * @since       1.0.0
13
 * @return      void
14
 */
15
function wpinv_subscriptions_page() {
16
17
	?>
18
19
	<div class="wrap">
20
		<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
21
		<div class="bsui">
22
23
			<?php
24
25
				// Verify user permissions.
26
				if ( ! wpinv_current_user_can_manage_invoicing() ) {
27
28
					echo aui()->alert(
29
						array(
30
							'type'    => 'danger',
31
							'content' => __( 'You are not permitted to view this page.', 'invoicing' ),
32
						)
33
					);
34
35
				} else if ( ! empty( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) {
36
37
					// Display a single subscription.
38
					wpinv_recurring_subscription_details();
39
				} else {
40
41
					// Display a list of available subscriptions.
42
					getpaid_print_subscriptions_list();
43
				}
44
45
			?>
46
47
		</div>
48
	</div>
49
50
	<?php
51
}
52
53
/**
54
 * Render the Subscriptions table
55
 *
56
 * @access      public
57
 * @since       1.0.19
58
 * @return      void
59
 */
60
function getpaid_print_subscriptions_list() {
61
62
	$subscribers_table = new WPInv_Subscriptions_List_Table();
63
	$subscribers_table->prepare_items();
64
65
	?>
66
	<form id="subscribers-filter" class="bsui" method="get">
67
		<input type="hidden" name="page" value="wpinv-subscriptions" />
68
		<?php $subscribers_table->views(); ?>
69
		<?php $subscribers_table->display(); ?>
70
	</form>
71
	<?php
72
}
73
74
/**
75
 * Render a single subscription.
76
 *
77
 * @access      public
78
 * @since       1.0.0
79
 * @return      void
80
 */
81
function wpinv_recurring_subscription_details() {
82
83
	// Fetch the subscription.
84
	$sub = new WPInv_Subscription( (int) $_GET['id'] );
85
	if ( ! $sub->exists() ) {
86
87
		echo aui()->alert(
88
			array(
89
				'type'    => 'danger',
90
				'content' => __( 'Subscription not found.', 'invoicing' ),
91
			)
92
		);
93
94
		return;
95
	}
96
97
	// Use metaboxes to display the subscription details.
98
	add_meta_box( 'getpaid_admin_subscription_details_metabox', __( 'Subscription Details', 'invoicing' ), 'getpaid_admin_subscription_details_metabox', get_current_screen(), 'normal' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
99
	add_meta_box( 'getpaid_admin_subscription_update_metabox', __( 'Change Status', 'invoicing' ), 'getpaid_admin_subscription_update_metabox', get_current_screen(), 'side' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
100
101
	$subscription_id     = $sub->get_id();
102
	$subscription_groups = getpaid_get_invoice_subscription_groups( $sub->get_parent_invoice_id() );
103
	$subscription_group  = wp_list_filter( $subscription_groups, compact( 'subscription_id' ) );
104
105
	if ( ! empty( $subscription_group ) ) {
106
		add_meta_box( 'getpaid_admin_subscription_item_details_metabox', __( 'Subscription Items', 'invoicing' ), 'getpaid_admin_subscription_item_details_metabox', get_current_screen(), 'advanced' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
107
	}
108
109
	add_meta_box( 'getpaid_admin_subscription_invoice_details_metabox', __( 'Related Invoices', 'invoicing' ), 'getpaid_admin_subscription_invoice_details_metabox', get_current_screen(), 'advanced', 'low' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
110
111
	do_action( 'getpaid_admin_single_subscription_register_metabox', $sub );
112
113
	?>
114
115
		<form method="post" action="<?php echo admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $sub->get_id() ) ); ?>">
116
117
			<?php wp_nonce_field( 'getpaid-nonce', 'getpaid-nonce' ); ?>
118
			<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
119
			<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
120
			<input type="hidden" name="getpaid-admin-action" value="update_single_subscription" />
121
			<input type="hidden" name="subscription_id" value="<?php echo (int) $sub->get_id() ;?>" />
122
123
			<div id="poststuff">
124
				<div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
125
126
					<div id="postbox-container-1" class="postbox-container">
127
						<?php do_meta_boxes( get_current_screen(), 'side', $sub ); ?>
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
128
					</div>
129
130
					<div id="postbox-container-2" class="postbox-container">
131
						<?php do_meta_boxes( get_current_screen(), 'normal', $sub ); ?>
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
132
						<?php do_meta_boxes( get_current_screen(), 'advanced', $sub ); ?>
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
133
					</div>
134
135
				</div>
136
			</div>
137
138
		</form>
139
140
		<script>jQuery(document).ready(function(){ postboxes.add_postbox_toggles('getpaid_page_wpinv-subscriptions'); });</script>
141
142
	<?php
143
144
}
145
146
/**
147
 * Displays the subscription details metabox.
148
 *
149
 * @param WPInv_Subscription $sub
150
 */
151
function getpaid_admin_subscription_details_metabox( $sub ) {
152
153
	// Subscription items.
154
	$subscription_group = getpaid_get_invoice_subscription_group( $sub->get_parent_invoice_id(), $sub->get_id() );
155
	$items_count        = empty( $subscription_group ) ? 1 : count( $subscription_group['items'] );
156
157
	// Prepare subscription detail columns.
158
	$fields = apply_filters(
159
		'getpaid_subscription_admin_page_fields',
160
		array(
161
			'subscription'   => __( 'Subscription', 'invoicing' ),
162
			'customer'       => __( 'Customer', 'invoicing' ),
163
			'amount'         => __( 'Amount', 'invoicing' ),
164
			'start_date'     => __( 'Start Date', 'invoicing' ),
165
			'renews_on'      => __( 'Next Payment', 'invoicing' ),
166
			'renewals'       => __( 'Payments', 'invoicing' ),
167
			'item'           => _n( 'Item', 'Items', $items_count,  'invoicing' ),
168
			'gateway'        => __( 'Payment Method', 'invoicing' ),
169
			'profile_id'     => __( 'Profile ID', 'invoicing' ),
170
			'status'         => __( 'Status', 'invoicing' ),
171
		)
172
	);
173
174
	if ( ! $sub->is_active() ) {
175
176
		if ( isset( $fields['renews_on'] ) ) {
177
			unset( $fields['renews_on'] );
178
		}
179
180
		if ( isset( $fields['gateway'] ) ) {
181
			unset( $fields['gateway'] );
182
		}
183
184
	}
185
186
	$profile_id = $sub->get_profile_id();
187
	if ( empty( $profile_id ) && isset( $fields['profile_id'] ) ) {
188
		unset( $fields['profile_id'] );
189
	}
190
191
	?>
192
193
		<table class="table table-borderless" style="font-size: 14px;">
194
			<tbody>
195
196
				<?php foreach ( $fields as $key => $label ) : ?>
197
198
					<tr class="getpaid-subscription-meta-<?php echo sanitize_html_class( $key ); ?>">
199
200
						<th class="w-25" style="font-weight: 500;">
201
							<?php echo sanitize_text_field( $label ); ?>
202
						</th>
203
204
						<td class="w-75 text-muted">
205
							<?php do_action( 'getpaid_subscription_admin_display_' . sanitize_text_field( $key ), $sub, $subscription_group ); ?>
206
						</td>
207
208
					</tr>
209
210
				<?php endforeach; ?>
211
212
			</tbody>
213
		</table>
214
215
	<?php
216
}
217
218
/**
219
 * Displays the subscription customer.
220
 *
221
 * @param WPInv_Subscription $subscription
222
 */
223
function getpaid_admin_subscription_metabox_display_customer( $subscription ) {
224
225
	$username = __( '(Missing User)', 'invoicing' );
226
227
	$user = get_userdata( $subscription->get_customer_id() );
228
	if ( $user ) {
229
230
		$username = sprintf(
231
			'<a href="user-edit.php?user_id=%s">%s</a>',
232
			absint( $user->ID ),
233
			! empty( $user->display_name ) ? sanitize_text_field( $user->display_name ) : sanitize_email( $user->user_email )
234
		);
235
236
	}
237
238
	echo  $username;
239
}
240
add_action( 'getpaid_subscription_admin_display_customer', 'getpaid_admin_subscription_metabox_display_customer' );
241
242
/**
243
 * Displays the subscription amount.
244
 *
245
 * @param WPInv_Subscription $subscription
246
 */
247
function getpaid_admin_subscription_metabox_display_amount( $subscription ) {
248
	$amount    = sanitize_text_field( getpaid_get_formatted_subscription_amount( $subscription ) );
249
	echo "<span>$amount</span>";
250
}
251
add_action( 'getpaid_subscription_admin_display_amount', 'getpaid_admin_subscription_metabox_display_amount' );
252
253
/**
254
 * Displays the subscription id.
255
 *
256
 * @param WPInv_Subscription $subscription
257
 */
258
function getpaid_admin_subscription_metabox_display_id( $subscription ) {
259
	echo  '#' . absint( $subscription->get_id() );
260
}
261
add_action( 'getpaid_subscription_admin_display_subscription', 'getpaid_admin_subscription_metabox_display_id' );
262
263
/**
264
 * Displays the subscription renewal date.
265
 *
266
 * @param WPInv_Subscription $subscription
267
 */
268
function getpaid_admin_subscription_metabox_display_start_date( $subscription ) {
269
	echo getpaid_format_date_value( $subscription->get_date_created() );
270
}
271
add_action( 'getpaid_subscription_admin_display_start_date', 'getpaid_admin_subscription_metabox_display_start_date' );
272
273
/**
274
 * Displays the subscription renewal date.
275
 *
276
 * @param WPInv_Subscription $subscription
277
 */
278
function getpaid_admin_subscription_metabox_display_renews_on( $subscription ) {
279
	echo getpaid_format_date_value( $subscription->get_expiration() );
280
}
281
add_action( 'getpaid_subscription_admin_display_renews_on', 'getpaid_admin_subscription_metabox_display_renews_on' );
282
283
/**
284
 * Displays the subscription renewal count.
285
 *
286
 * @param WPInv_Subscription $subscription
287
 */
288
function getpaid_admin_subscription_metabox_display_renewals( $subscription ) {
289
	$max_bills = $subscription->get_bill_times();
290
	echo $subscription->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "&infin;" : $max_bills );
291
}
292
add_action( 'getpaid_subscription_admin_display_renewals', 'getpaid_admin_subscription_metabox_display_renewals' );
293
/**
294
 * Displays the subscription item.
295
 *
296
 * @param WPInv_Subscription $subscription
297
 * @param false|array $subscription_group
298
 */
299
function getpaid_admin_subscription_metabox_display_item( $subscription, $subscription_group = false ) {
300
301
	if ( empty( $subscription_group ) ) {
302
		echo WPInv_Subscriptions_List_Table::generate_item_markup( $subscription->get_product_id() );
303
		return;
304
	}
305
306
	$markup = array_map( array( 'WPInv_Subscriptions_List_Table', 'generate_item_markup' ), array_keys( $subscription_group['items'] ) );
307
	echo implode( ' | ', $markup );
308
309
}
310
add_action( 'getpaid_subscription_admin_display_item', 'getpaid_admin_subscription_metabox_display_item', 10, 2 );
311
312
/**
313
 * Displays the subscription gateway.
314
 *
315
 * @param WPInv_Subscription $subscription
316
 */
317
function getpaid_admin_subscription_metabox_display_gateway( $subscription ) {
318
319
	$gateway = $subscription->get_gateway();
320
321
	if ( ! empty( $gateway ) ) {
322
		echo sanitize_text_field( wpinv_get_gateway_admin_label( $gateway ) );
323
	} else {
324
		echo "&mdash;";
325
	}
326
327
}
328
add_action( 'getpaid_subscription_admin_display_gateway', 'getpaid_admin_subscription_metabox_display_gateway' );
329
330
/**
331
 * Displays the subscription status.
332
 *
333
 * @param WPInv_Subscription $subscription
334
 */
335
function getpaid_admin_subscription_metabox_display_status( $subscription ) {
336
	echo $subscription->get_status_label_html();
337
}
338
add_action( 'getpaid_subscription_admin_display_status', 'getpaid_admin_subscription_metabox_display_status' );
339
340
/**
341
 * Displays the subscription profile id.
342
 *
343
 * @param WPInv_Subscription $subscription
344
 */
345
function getpaid_admin_subscription_metabox_display_profile_id( $subscription ) {
346
347
	$profile_id = $subscription->get_profile_id();
348
349
	$input = aui()->input(
350
		array(
351
			'type'        => 'text',
352
			'id'          => 'wpinv_subscription_profile_id',
353
			'name'        => 'wpinv_subscription_profile_id',
354
			'label'       => __( 'Profile Id', 'invoicing' ),
355
			'label_type'  => 'hidden',
356
			'placeholder' => __( 'Profile Id', 'invoicing' ),
357
			'value'       => sanitize_text_field( $profile_id ),
358
			'input_group_right' => '',
359
			'no_wrap'     => true,
360
		)
361
	);
362
363
	echo str_ireplace( 'form-control', 'regular-text', $input );
0 ignored issues
show
Bug introduced by
Are you sure str_ireplace('form-contr...'regular-text', $input) of type array|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

363
	echo /** @scrutinizer ignore-type */ str_ireplace( 'form-control', 'regular-text', $input );
Loading history...
364
365
	$url = apply_filters( 'getpaid_remote_subscription_profile_url', '', $subscription );
366
	if ( ! empty( $url ) ) {
367
		$url = esc_url_raw( $url );
368
		echo '&nbsp;<a href="' . $url . '" title="' . __( 'View in Gateway', 'invoicing' ) . '" target="_blank"><i class="fas fa-external-link-alt fa-xs fa-fw align-top"></i></a>';
369
	}
370
371
}
372
add_action( 'getpaid_subscription_admin_display_profile_id', 'getpaid_admin_subscription_metabox_display_profile_id' );
373
374
/**
375
 * Displays the subscriptions update metabox.
376
 *
377
 * @param WPInv_Subscription $subscription
378
 */
379
function getpaid_admin_subscription_update_metabox( $subscription ) {
380
381
	?>
382
	<div class="mt-3">
383
384
		<?php
385
			echo aui()->select(
386
				array(
387
					'options'          => getpaid_get_subscription_statuses(),
388
					'name'             => 'subscription_status',
389
					'id'               => 'subscription_status_update_select',
390
					'required'         => true,
391
					'no_wrap'          => false,
392
					'label'            => __( 'Subscription Status', 'invoicing' ),
393
					'help_text'        => __( 'Updating the status will trigger related actions and hooks', 'invoicing' ),
394
					'select2'          => true,
395
					'value'            => $subscription->get_status( 'edit' ),
396
				)
397
			);
398
		?>
399
400
		<div class="mt-2 px-3 py-2 bg-light border-top" style="margin: -12px;">
401
402
		<?php
403
			submit_button( __( 'Update', 'invoicing' ), 'primary', 'submit', false );
404
405
			$url    = esc_url( wp_nonce_url( add_query_arg( 'getpaid-admin-action', 'subscription_manual_renew' ), 'getpaid-nonce', 'getpaid-nonce' ) );
406
			$anchor = __( 'Renew Subscription', 'invoicing' );
407
			$title  = esc_attr__( 'Are you sure you want to extend the subscription and generate a new invoice that will be automatically marked as paid?', 'invoicing' );
408
409
			if ( $subscription->is_active() ) {
410
				echo "<a href='$url' class='float-right text-muted' onclick='return confirm(\"$title\")'>$anchor</a>";
411
			}
412
413
	echo '</div></div>';
414
}
415
416
/**
417
 * Displays the subscriptions invoices metabox.
418
 *
419
 * @param WPInv_Subscription $subscription
420
 */
421
function getpaid_admin_subscription_invoice_details_metabox( $subscription ) {
422
423
	$columns = apply_filters(
424
		'getpaid_subscription_related_invoices_columns',
425
		array(
426
			'invoice'      => __( 'Invoice', 'invoicing' ),
427
			'relationship' => __( 'Relationship', 'invoicing' ),
428
			'date'         => __( 'Date', 'invoicing' ),
429
			'status'       => __( 'Status', 'invoicing' ),
430
			'total'        => __( 'Total', 'invoicing' ),
431
		),
432
		$subscription
433
	);
434
435
	// Prepare the invoices.
436
	$payments = $subscription->get_child_payments( ! is_admin() );
437
	$parent   = $subscription->get_parent_invoice();
438
439
	if ( $parent->get_id() ) {
440
		$payments = array_merge( array( $parent ), $payments );
441
	}
442
443
	$table_class = 'w-100 bg-white';
444
445
	if ( ! is_admin() ) {
446
		$table_class = 'table table-bordered table-striped';
447
	}
448
449
	?>
450
		<div class="m-0" style="overflow: auto;">
451
452
			<table class="<?php echo $table_class; ?>">
453
454
				<thead>
455
					<tr>
456
						<?php
457
							$processed_columns = 0;
458
							foreach ( $columns as $key => $label ) {
459
								$key   = esc_attr( $key );
460
								$label = sanitize_text_field( $label );
461
								$class = 'text-center';
462
								$class = empty( $processed_columns ) ? 'text-left' : $class;
463
								$class = count( $columns ) - 1 == $processed_columns ? 'text-right' : $class;
464
								$processed_columns ++;
465
466
								echo "<th class='subscription-invoice-field-$key bg-light p-2 $class color-dark font-weight-bold'>$label</th>";
467
							}
468
						?>
469
					</tr>
470
				</thead>
471
472
				<tbody>
473
474
					<?php if ( empty( $payments ) ) : ?>
475
						<tr>
476
							<td colspan="<?php echo count($columns); ?>" class="p-2 text-left text-muted">
477
								<?php _e( 'This subscription has no invoices.', 'invoicing' ); ?>
478
							</td>
479
						</tr>
480
					<?php endif; ?>
481
482
					<?php
483
484
						foreach( $payments as $payment ) :
485
486
							// Ensure that we have an invoice.
487
							$payment = new WPInv_Invoice( $payment );
488
489
							// Abort if the invoice is invalid.
490
							if ( ! $payment->get_id() ) {
491
								continue;
492
							}
493
494
							echo '<tr>';
495
496
								$processed_columns = 0;
497
								foreach ( array_keys( $columns ) as $key ) {
498
499
									$class = 'text-center';
500
									$class = empty( $processed_columns ) ? 'text-left' : $class;
501
									$class = count( $columns ) - 1 == $processed_columns ? 'text-right' : $class;
502
									$processed_columns ++;
503
504
									echo "<td class='p-2 $class'>";
505
506
										switch( $key ) {
507
508
											case 'total':
509
												echo '<strong>' . wpinv_price( $payment->get_total(), $payment->get_currency() ) . '</strong>';
510
												break;
511
512
											case 'relationship':
513
												echo $payment->is_renewal() ? __( 'Renewal Invoice', 'invoicing' ) : __( 'Initial Invoice', 'invoicing' );
514
												break;
515
516
											case 'date':
517
												echo getpaid_format_date_value( $payment->get_date_created() );
518
												break;
519
520
											case 'status':
521
522
												$status = $payment->get_status_nicename();
523
												if ( is_admin() ) {
524
													$status = $payment->get_status_label_html();
525
												}
526
527
												echo $status;
528
												break;
529
530
											case 'invoice':
531
												$link    = esc_url( get_edit_post_link( $payment->get_id() ) );
532
533
												if ( ! is_admin() ) {
534
													$link = esc_url( $payment->get_view_url() );
535
												}
536
537
												$invoice = sanitize_text_field( $payment->get_number() );
538
												echo "<a href='$link'>$invoice</a>";
539
												break;
540
										}
541
542
									echo '</td>';
543
544
								}
545
546
							echo '</tr>';
547
548
						endforeach;
549
					?>
550
551
				</tbody>
552
553
			</table>
554
555
		</div>
556
557
	<?php
558
}
559
560
/**
561
 * Displays the subscriptions items metabox.
562
 *
563
 * @param WPInv_Subscription $subscription
564
 */
565
function getpaid_admin_subscription_item_details_metabox( $subscription ) {
566
567
	// Fetch the subscription group.
568
	$subscription_group = getpaid_get_invoice_subscription_group( $subscription->get_parent_payment_id(), $subscription->get_id() );
569
570
	if ( empty( $subscription_group ) || empty( $subscription_group['items'] ) ) {
571
		return;
572
	}
573
574
	// Prepare table columns.
575
	$columns = apply_filters(
576
		'getpaid_subscription_item_details_columns',
577
		array(
578
			'item_name'    => __( 'Item', 'invoicing' ),
579
			'price'        => __( 'Price', 'invoicing' ),
580
			'tax'          => __( 'Tax', 'invoicing' ),
581
			'discount'     => __( 'Discount', 'invoicing' ),
582
			'initial'      => __( 'Initial Amount', 'invoicing' ),
583
			'recurring'    => __( 'Recurring Amount', 'invoicing' ),
584
		),
585
		$subscription
586
	);
587
588
	// Prepare the invoices.
589
590
	$invoice = $subscription->get_parent_invoice();
591
592
	if ( ( ! wpinv_use_taxes() || ! $invoice->is_taxable() ) && isset( $columns['tax'] ) ) {
593
		unset( $columns['tax'] );
594
	}
595
596
	$table_class = 'w-100 bg-white';
597
598
	if ( ! is_admin() ) {
599
		$table_class = 'table table-bordered table-striped';
600
	}
601
602
	?>
603
		<div class="m-0" style="overflow: auto;">
604
605
			<table class="<?php echo $table_class; ?>">
606
607
				<thead>
608
					<tr>
609
						<?php
610
							$processed_columns = 0;
611
							foreach ( $columns as $key => $label ) {
612
								$key   = esc_attr( $key );
613
								$label = sanitize_text_field( $label );
614
								$class = 'text-center';
615
								$class = empty( $processed_columns ) ? 'text-left' : $class;
616
								$class = count( $columns ) - 1 == $processed_columns ? 'text-right' : $class;
617
								$processed_columns ++;
618
619
								echo "<th class='subscription-item-field-$key bg-light p-2 $class color-dark font-weight-bold'>$label</th>";
620
							}
621
						?>
622
					</tr>
623
				</thead>
624
625
				<tbody>
626
627
					<?php
628
629
						foreach( $subscription_group['items'] as $subscription_group_item ) :
630
631
							echo '<tr>';
632
633
								$processed_columns = 0;
634
								foreach ( array_keys( $columns ) as $key ) {
635
636
									$class = 'text-center';
637
									$class = empty( $processed_columns ) ? 'text-left' : $class;
638
									$class = count( $columns ) - 1 == $processed_columns ? 'text-right' : $class;
639
									$processed_columns ++;
640
641
									echo "<td class='p-2 $class'>";
642
643
										switch( $key ) {
644
645
											case 'item_name':
646
												$item_name = get_the_title( $subscription_group_item['item_id'] );
647
												$item_name = empty( $item_name ) ? $subscription_group_item['item_name'] : $item_name;
648
649
												if ( $invoice->get_template() == 'amount' || 1 === (int) $subscription_group_item['quantity'] ) {
650
													echo sanitize_text_field( $item_name );
651
												} else {
652
													printf( '%1$s x %2$d', sanitize_text_field( $item_name ), (int) $subscription_group_item['quantity'] );
653
												}
654
655
												break;
656
657
											case 'price':
658
												echo wpinv_price( $subscription_group_item['price'], $invoice->get_currency() );
659
												break;
660
661
											case 'tax':
662
												echo wpinv_price( $subscription_group_item['tax'], $invoice->get_currency() );
663
												break;
664
665
											case 'discount':
666
												echo wpinv_price( $subscription_group_item['discount'], $invoice->get_currency() );
667
												break;
668
669
											case 'initial':
670
												echo wpinv_price( $subscription_group_item['subtotal'], $invoice->get_currency() );
671
												break;
672
673
											case 'recurring':
674
												echo '<strong>' . wpinv_price( $subscription_group_item['price'] * $subscription_group_item['quantity'], $invoice->get_currency() ) . '</strong>';
675
												break;
676
677
										}
678
679
									echo '</td>';
680
681
								}
682
683
							echo '</tr>';
684
685
						endforeach;
686
					?>
687
688
				</tbody>
689
690
			</table>
691
692
		</div>
693
694
	<?php
695
}
696