Passed
Push — master ( 55577f...4d7f46 )
by Brian
04:27
created

getpaid_admin_subscription_invoice_details_metabox()   D

Complexity

Conditions 16
Paths 176

Size

Total Lines 122
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 16
eloc 84
c 3
b 0
f 0
nc 176
nop 1
dl 0
loc 122
rs 4.2957

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

367
	echo /** @scrutinizer ignore-type */ str_ireplace( 'form-control', 'regular-text', $input );
Loading history...
368
369
	$url = apply_filters( 'getpaid_remote_subscription_profile_url', '', $subscription );
370
	if ( ! empty( $url ) ) {
371
		$url = esc_url_raw( $url );
372
		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>';
373
	}
374
375
}
376
add_action( 'getpaid_subscription_admin_display_profile_id', 'getpaid_admin_subscription_metabox_display_profile_id' );
377
378
/**
379
 * Displays the subscriptions update metabox.
380
 *
381
 * @param WPInv_Subscription $subscription
382
 */
383
function getpaid_admin_subscription_update_metabox( $subscription ) {
384
385
	?>
386
	<div class="mt-3">
387
388
		<?php
389
			echo aui()->select(
390
				array(
391
					'options'          => getpaid_get_subscription_statuses(),
392
					'name'             => 'subscription_status',
393
					'id'               => 'subscription_status_update_select',
394
					'required'         => true,
395
					'no_wrap'          => false,
396
					'label'            => __( 'Subscription Status', 'invoicing' ),
397
					'help_text'        => __( 'Updating the status will trigger related actions and hooks', 'invoicing' ),
398
					'select2'          => true,
399
					'value'            => $subscription->get_status( 'edit' ),
400
				)
401
			);
402
		?>
403
404
		<div class="mt-2 px-3 py-2 bg-light border-top" style="margin: -12px;">
405
406
		<?php
407
			submit_button( __( 'Update', 'invoicing' ), 'primary', 'submit', false );
408
409
			$url    = esc_url( wp_nonce_url( add_query_arg( 'getpaid-admin-action', 'subscription_manual_renew' ), 'getpaid-nonce', 'getpaid-nonce' ) );
410
			$anchor = __( 'Renew Subscription', 'invoicing' );
411
			$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' );
412
413
			if ( $subscription->is_active() ) {
414
				echo "<a href='$url' class='float-right text-muted' onclick='return confirm(\"$title\")'>$anchor</a>";
415
			}
416
417
	echo '</div></div>';
418
}
419
420
/**
421
 * Displays the subscriptions invoices metabox.
422
 *
423
 * @param WPInv_Subscription $subscription
424
 */
425
function getpaid_admin_subscription_invoice_details_metabox( $subscription ) {
426
427
	$columns = apply_filters(
428
		'getpaid_subscription_related_invoices_columns',
429
		array(
430
			'invoice'      => __( 'Invoice', 'invoicing' ),
431
			'relationship' => __( 'Relationship', 'invoicing' ),
432
			'date'         => __( 'Date', 'invoicing' ),
433
			'status'       => __( 'Status', 'invoicing' ),
434
			'total'        => __( 'Total', 'invoicing' ),
435
		),
436
		$subscription
437
	);
438
439
	// Prepare the invoices.
440
	$payments = $subscription->get_child_payments( ! is_admin() );
441
	$parent   = $subscription->get_parent_invoice();
442
443
	if ( $parent->get_id() ) {
444
		$payments = array_merge( array( $parent ), $payments );
445
	}
446
447
	$table_class = 'w-100 bg-white';
448
449
	if ( ! is_admin() ) {
450
		$table_class = 'table table-bordered table-striped';
451
	}
452
453
	?>
454
		<div class="m-0" style="overflow: auto;">
455
456
			<table class="<?php echo $table_class; ?>">
457
458
				<thead>
459
					<tr>
460
						<?php
461
							foreach ( $columns as $key => $label ) {
462
								$key   = esc_attr( $key );
463
								$label = sanitize_text_field( $label );
464
								$class = 'text-left';
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
								foreach ( array_keys( $columns ) as $key ) {
497
498
									$class = 'text-left';
499
500
									echo "<td class='p-2 $class'>";
501
502
										switch( $key ) {
503
504
											case 'total':
505
												echo '<strong>' . wpinv_price( $payment->get_total(), $payment->get_currency() ) . '</strong>';
506
												break;
507
508
											case 'relationship':
509
												echo $payment->is_renewal() ? __( 'Renewal Invoice', 'invoicing' ) : __( 'Initial Invoice', 'invoicing' );
510
												break;
511
512
											case 'date':
513
												echo getpaid_format_date_value( $payment->get_date_created() );
514
												break;
515
516
											case 'status':
517
518
												$status = $payment->get_status_nicename();
519
												if ( is_admin() ) {
520
													$status = $payment->get_status_label_html();
521
												}
522
523
												echo $status;
524
												break;
525
526
											case 'invoice':
527
												$link    = esc_url( get_edit_post_link( $payment->get_id() ) );
528
529
												if ( ! is_admin() ) {
530
													$link = esc_url( $payment->get_view_url() );
531
												}
532
533
												$invoice = sanitize_text_field( $payment->get_number() );
534
												echo "<a href='$link'>$invoice</a>";
535
												break;
536
										}
537
538
									echo '</td>';
539
540
								}
541
542
							echo '</tr>';
543
544
						endforeach;
545
					?>
546
547
				</tbody>
548
549
			</table>
550
551
		</div>
552
553
	<?php
554
}
555
556
/**
557
 * Displays the subscriptions items metabox.
558
 *
559
 * @param WPInv_Subscription $subscription
560
 */
561
function getpaid_admin_subscription_item_details_metabox( $subscription ) {
562
563
	// Fetch the subscription group.
564
	$subscription_group = getpaid_get_invoice_subscription_group( $subscription->get_parent_payment_id(), $subscription->get_id() );
565
566
	if ( empty( $subscription_group ) || empty( $subscription_group['items'] ) ) {
567
		return;
568
	}
569
570
	// Prepare table columns.
571
	$columns = apply_filters(
572
		'getpaid_subscription_item_details_columns',
573
		array(
574
			'item_name'    => __( 'Item', 'invoicing' ),
575
			'price'        => __( 'Price', 'invoicing' ),
576
			'tax'          => __( 'Tax', 'invoicing' ),
577
			'discount'     => __( 'Discount', 'invoicing' ),
578
			'initial'      => __( 'Initial Amount', 'invoicing' ),
579
			'recurring'    => __( 'Recurring Amount', 'invoicing' ),
580
		),
581
		$subscription
582
	);
583
584
	// Prepare the invoices.
585
586
	$invoice = $subscription->get_parent_invoice();
587
588
	if ( ( ! wpinv_use_taxes() || ! $invoice->is_taxable() ) && isset( $columns['tax'] ) ) {
589
		unset( $columns['tax'] );
590
	}
591
592
	$table_class = 'w-100 bg-white';
593
594
	if ( ! is_admin() ) {
595
		$table_class = 'table table-bordered table-striped';
596
	}
597
598
	?>
599
		<div class="m-0" style="overflow: auto;">
600
601
			<table class="<?php echo $table_class; ?>">
602
603
				<thead>
604
					<tr>
605
						<?php
606
607
							foreach ( $columns as $key => $label ) {
608
								$key   = esc_attr( $key );
609
								$label = sanitize_text_field( $label );
610
								$class = 'text-left';
611
612
								echo "<th class='subscription-item-field-$key bg-light p-2 $class color-dark font-weight-bold'>$label</th>";
613
							}
614
						?>
615
					</tr>
616
				</thead>
617
618
				<tbody>
619
620
					<?php
621
622
						foreach( $subscription_group['items'] as $subscription_group_item ) :
623
624
							echo '<tr>';
625
626
								foreach ( array_keys( $columns ) as $key ) {
627
628
									$class = 'text-left';
629
630
									echo "<td class='p-2 $class'>";
631
632
										switch( $key ) {
633
634
											case 'item_name':
635
												$item_name = get_the_title( $subscription_group_item['item_id'] );
636
												$item_name = empty( $item_name ) ? $subscription_group_item['item_name'] : $item_name;
637
638
												if ( $invoice->get_template() == 'amount' || 1 === (int) $subscription_group_item['quantity'] ) {
639
													echo sanitize_text_field( $item_name );
640
												} else {
641
													printf( '%1$s x %2$d', sanitize_text_field( $item_name ), (int) $subscription_group_item['quantity'] );
642
												}
643
644
												break;
645
646
											case 'price':
647
												echo wpinv_price( $subscription_group_item['price'], $invoice->get_currency() );
648
												break;
649
650
											case 'tax':
651
												echo wpinv_price( $subscription_group_item['tax'], $invoice->get_currency() );
652
												break;
653
654
											case 'discount':
655
												echo wpinv_price( $subscription_group_item['discount'], $invoice->get_currency() );
656
												break;
657
658
											case 'initial':
659
												echo wpinv_price( $subscription_group_item['subtotal'], $invoice->get_currency() );
660
												break;
661
662
											case 'recurring':
663
												echo '<strong>' . wpinv_price( $subscription_group_item['price'] * $subscription_group_item['quantity'], $invoice->get_currency() ) . '</strong>';
664
												break;
665
666
										}
667
668
									echo '</td>';
669
670
								}
671
672
							echo '</tr>';
673
674
						endforeach;
675
					?>
676
677
				</tbody>
678
679
			</table>
680
681
		</div>
682
683
	<?php
684
}
685
686
/**
687
 * Displays the related subscriptions metabox.
688
 *
689
 * @param WPInv_Subscription $subscription
690
 */
691
function getpaid_admin_subscription_related_subscriptions_metabox( $subscription ) {
692
693
	// Fetch the subscription groups.
694
	$subscription_groups = getpaid_get_invoice_subscription_groups( $subscription->get_parent_payment_id() );
695
696
	if ( empty( $subscription_groups ) ) {
697
		return;
698
	}
699
700
	// Prepare table columns.
701
	$columns = apply_filters(
702
		'getpaid_subscription_related_subscriptions_columns',
703
		array(
704
			'subscription'      => __( 'Subscription', 'invoicing' ),
705
			'start_date'        => __( 'Start Date', 'invoicing' ),
706
			'renewal_date'      => __( 'Next Payment', 'invoicing' ),
707
			'renewals'          => __( 'Payments', 'invoicing' ),
708
			'item'              => __( 'Items', 'invoicing' ),
709
			'status'            => __( 'Status', 'invoicing' ),
710
		),
711
		$subscription
712
	);
713
714
	$table_class = 'w-100 bg-white';
715
716
	if ( ! is_admin() ) {
717
		$table_class = 'table table-bordered table-striped';
718
	}
719
720
	?>
721
		<div class="m-0" style="overflow: auto;">
722
723
			<table class="<?php echo $table_class; ?>">
724
725
				<thead>
726
					<tr>
727
						<?php
728
729
							foreach ( $columns as $key => $label ) {
730
								$key   = esc_attr( $key );
731
								$label = sanitize_text_field( $label );
732
								$class = 'text-left';
733
734
								echo "<th class='related-subscription-field-$key bg-light p-2 $class color-dark font-weight-bold'>$label</th>";
735
							}
736
						?>
737
					</tr>
738
				</thead>
739
740
				<tbody>
741
742
					<?php
743
744
						foreach( $subscription_groups as $subscription_group ) :
745
746
							// Do not list current subscription.
747
							if ( (int) $subscription_group['subscription_id'] === $subscription->get_id() ) {
748
								continue;
749
							}
750
751
							// Ensure the subscription exists.
752
							$_suscription = new WPInv_Subscription( $subscription_group['subscription_id'] );
753
754
							if ( ! $_suscription->exists() ) {
755
								continue;
756
							}
757
758
							echo '<tr>';
759
760
								foreach ( array_keys( $columns ) as $key ) {
761
762
									$class = 'text-left';
763
764
									echo "<td class='p-2 $class'>";
765
766
										switch( $key ) {
767
768
											case 'status':
769
												echo $_suscription->get_status_label_html();
770
												break;
771
772
											case 'item':
773
												$markup = array_map( array( 'WPInv_Subscriptions_List_Table', 'generate_item_markup' ), array_keys( $subscription_group['items'] ) );
774
												echo implode( ' | ', $markup );
775
												break;
776
777
											case 'renewals':
778
												$max_bills = $_suscription->get_bill_times();
779
												echo $_suscription->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "&infin;" : $max_bills );
780
												break;
781
782
											case 'renewal_date':
783
												echo $_suscription->is_active() ? getpaid_format_date_value( $_suscription->get_expiration() ) : "&mdash;";
784
												break;
785
786
											case 'start_date':
787
												echo getpaid_format_date_value( $_suscription->get_date_created() );
788
												break;
789
790
											case 'subscription':
791
												$url = is_admin() ? admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $_suscription->get_id() ) ) : $_suscription->get_view_url();
792
												printf(
793
													'%1$s#%2$s%3$s',
794
													'<a href="' . esc_url( $url ) . '">',
795
													'<strong>' . intval( $_suscription->get_id() ) . '</strong>',
796
													'</a>'
797
												);
798
799
												echo WPInv_Subscriptions_List_Table::column_amount( $_suscription );
800
												break;
801
802
										}
803
804
									echo '</td>';
805
806
								}
807
808
							echo '</tr>';
809
810
						endforeach;
811
					?>
812
813
				</tbody>
814
815
			</table>
816
817
		</div>
818
819
	<?php
820
}
821