Passed
Push — master ( 4d7f46...f00b0d )
by Brian
04:46
created

getpaid_admin_subscription_item_details_metabox()   F

Complexity

Conditions 27
Paths 865

Size

Total Lines 162
Code Lines 105

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 27
eloc 105
nc 865
nop 1
dl 0
loc 162
rs 0.15
c 2
b 0
f 0

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->exists() ) {
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->exists() ) {
491
								continue;
492
							}
493
494
							// ... or belongs to a different subscription.
495
							if ( $payment->is_renewal() && $payment->get_subscription_id() && $payment->get_subscription_id() != $subscription->get_id() ) {
496
								continue;
497
							}
498
499
							echo '<tr>';
500
501
								foreach ( array_keys( $columns ) as $key ) {
502
503
									$class = 'text-left';
504
505
									echo "<td class='p-2 $class'>";
506
507
										switch( $key ) {
508
509
											case 'total':
510
												echo '<strong>' . wpinv_price( $payment->get_total(), $payment->get_currency() ) . '</strong>';
511
												break;
512
513
											case 'relationship':
514
												echo $payment->is_renewal() ? __( 'Renewal Invoice', 'invoicing' ) : __( 'Initial Invoice', 'invoicing' );
515
												break;
516
517
											case 'date':
518
												echo getpaid_format_date_value( $payment->get_date_created() );
519
												break;
520
521
											case 'status':
522
523
												$status = $payment->get_status_nicename();
524
												if ( is_admin() ) {
525
													$status = $payment->get_status_label_html();
526
												}
527
528
												echo $status;
529
												break;
530
531
											case 'invoice':
532
												$link    = esc_url( get_edit_post_link( $payment->get_id() ) );
533
534
												if ( ! is_admin() ) {
535
													$link = esc_url( $payment->get_view_url() );
536
												}
537
538
												$invoice = sanitize_text_field( $payment->get_number() );
539
												echo "<a href='$link'>$invoice</a>";
540
												break;
541
										}
542
543
									echo '</td>';
544
545
								}
546
547
							echo '</tr>';
548
549
						endforeach;
550
					?>
551
552
				</tbody>
553
554
			</table>
555
556
		</div>
557
558
	<?php
559
}
560
561
/**
562
 * Displays the subscriptions items metabox.
563
 *
564
 * @param WPInv_Subscription $subscription
565
 */
566
function getpaid_admin_subscription_item_details_metabox( $subscription ) {
567
568
	// Fetch the subscription group.
569
	$subscription_group = getpaid_get_invoice_subscription_group( $subscription->get_parent_payment_id(), $subscription->get_id() );
570
571
	if ( empty( $subscription_group ) || empty( $subscription_group['items'] ) ) {
572
		return;
573
	}
574
575
	// Prepare table columns.
576
	$columns = apply_filters(
577
		'getpaid_subscription_item_details_columns',
578
		array(
579
			'item_name'    => __( 'Item', 'invoicing' ),
580
			'price'        => __( 'Price', 'invoicing' ),
581
			'tax'          => __( 'Tax', 'invoicing' ),
582
			'discount'     => __( 'Discount', 'invoicing' ),
583
			'initial'      => __( 'Initial Amount', 'invoicing' ),
584
			'recurring'    => __( 'Recurring Amount', 'invoicing' ),
585
		),
586
		$subscription
587
	);
588
589
	// Prepare the invoices.
590
591
	$invoice = $subscription->get_parent_invoice();
592
593
	if ( ( ! wpinv_use_taxes() || ! $invoice->is_taxable() ) && isset( $columns['tax'] ) ) {
594
		unset( $columns['tax'] );
595
	}
596
597
	$table_class = 'w-100 bg-white';
598
599
	if ( ! is_admin() ) {
600
		$table_class = 'table table-bordered table-striped';
601
	}
602
603
	?>
604
		<div class="m-0" style="overflow: auto;">
605
606
			<table class="<?php echo $table_class; ?>">
607
608
				<thead>
609
					<tr>
610
						<?php
611
612
							foreach ( $columns as $key => $label ) {
613
								$key   = esc_attr( $key );
614
								$label = sanitize_text_field( $label );
615
								$class = 'text-left';
616
617
								echo "<th class='subscription-item-field-$key bg-light p-2 $class color-dark font-weight-bold'>$label</th>";
618
							}
619
						?>
620
					</tr>
621
				</thead>
622
623
				<tbody>
624
625
					<?php
626
627
						foreach( $subscription_group['items'] as $subscription_group_item ) :
628
629
							echo '<tr>';
630
631
								foreach ( array_keys( $columns ) as $key ) {
632
633
									$class = 'text-left';
634
635
									echo "<td class='p-2 $class'>";
636
637
										switch( $key ) {
638
639
											case 'item_name':
640
												$item_name = get_the_title( $subscription_group_item['item_id'] );
641
												$item_name = empty( $item_name ) ? $subscription_group_item['item_name'] : $item_name;
642
643
												if ( $invoice->get_template() == 'amount' || 1 === (int) $subscription_group_item['quantity'] ) {
644
													echo sanitize_text_field( $item_name );
645
												} else {
646
													printf( '%1$s x %2$d', sanitize_text_field( $item_name ), (int) $subscription_group_item['quantity'] );
647
												}
648
649
												break;
650
651
											case 'price':
652
												echo wpinv_price( $subscription_group_item['price'], $invoice->get_currency() );
653
												break;
654
655
											case 'tax':
656
												echo wpinv_price( $subscription_group_item['tax'], $invoice->get_currency() );
657
												break;
658
659
											case 'discount':
660
												echo wpinv_price( $subscription_group_item['discount'], $invoice->get_currency() );
661
												break;
662
663
											case 'initial':
664
												echo wpinv_price( $subscription_group_item['subtotal'], $invoice->get_currency() );
665
												break;
666
667
											case 'recurring':
668
												echo '<strong>' . wpinv_price( $subscription_group_item['price'] * $subscription_group_item['quantity'], $invoice->get_currency() ) . '</strong>';
669
												break;
670
671
										}
672
673
									echo '</td>';
674
675
								}
676
677
							echo '</tr>';
678
679
						endforeach;
680
681
						foreach( $subscription_group['fees'] as $subscription_group_fee ) :
682
683
							echo '<tr>';
684
685
								foreach ( array_keys( $columns ) as $key ) {
686
687
									$class = 'text-left';
688
689
									echo "<td class='p-2 $class'>";
690
691
										switch( $key ) {
692
693
											case 'item_name':
694
												echo sanitize_text_field( $subscription_group_fee['name'] );
695
												break;
696
697
											case 'price':
698
												echo wpinv_price( $subscription_group_fee['initial_fee'], $invoice->get_currency() );
699
												break;
700
701
											case 'tax':
702
												echo "&mdash;";
703
												break;
704
705
											case 'discount':
706
												echo "&mdash;";
707
												break;
708
709
											case 'initial':
710
												echo wpinv_price( $subscription_group_fee['initial_fee'], $invoice->get_currency() );
711
												break;
712
713
											case 'recurring':
714
												echo '<strong>' . wpinv_price( $subscription_group_fee['recurring_fee'], $invoice->get_currency() ) . '</strong>';
715
												break;
716
717
										}
718
719
									echo '</td>';
720
721
								}
722
723
							echo '</tr>';
724
725
						endforeach;
726
					?>
727
728
				</tbody>
729
730
			</table>
731
732
		</div>
733
734
	<?php
735
}
736
737
/**
738
 * Displays the related subscriptions metabox.
739
 *
740
 * @param WPInv_Subscription $subscription
741
 */
742
function getpaid_admin_subscription_related_subscriptions_metabox( $subscription ) {
743
744
	// Fetch the subscription groups.
745
	$subscription_groups = getpaid_get_invoice_subscription_groups( $subscription->get_parent_payment_id() );
746
747
	if ( empty( $subscription_groups ) ) {
748
		return;
749
	}
750
751
	// Prepare table columns.
752
	$columns = apply_filters(
753
		'getpaid_subscription_related_subscriptions_columns',
754
		array(
755
			'subscription'      => __( 'Subscription', 'invoicing' ),
756
			'start_date'        => __( 'Start Date', 'invoicing' ),
757
			'renewal_date'      => __( 'Next Payment', 'invoicing' ),
758
			'renewals'          => __( 'Payments', 'invoicing' ),
759
			'item'              => __( 'Items', 'invoicing' ),
760
			'status'            => __( 'Status', 'invoicing' ),
761
		),
762
		$subscription
763
	);
764
765
	$table_class = 'w-100 bg-white';
766
767
	if ( ! is_admin() ) {
768
		$table_class = 'table table-bordered table-striped';
769
	}
770
771
	?>
772
		<div class="m-0" style="overflow: auto;">
773
774
			<table class="<?php echo $table_class; ?>">
775
776
				<thead>
777
					<tr>
778
						<?php
779
780
							foreach ( $columns as $key => $label ) {
781
								$key   = esc_attr( $key );
782
								$label = sanitize_text_field( $label );
783
								$class = 'text-left';
784
785
								echo "<th class='related-subscription-field-$key bg-light p-2 $class color-dark font-weight-bold'>$label</th>";
786
							}
787
						?>
788
					</tr>
789
				</thead>
790
791
				<tbody>
792
793
					<?php
794
795
						foreach( $subscription_groups as $subscription_group ) :
796
797
							// Do not list current subscription.
798
							if ( (int) $subscription_group['subscription_id'] === $subscription->get_id() ) {
799
								continue;
800
							}
801
802
							// Ensure the subscription exists.
803
							$_suscription = new WPInv_Subscription( $subscription_group['subscription_id'] );
804
805
							if ( ! $_suscription->exists() ) {
806
								continue;
807
							}
808
809
							echo '<tr>';
810
811
								foreach ( array_keys( $columns ) as $key ) {
812
813
									$class = 'text-left';
814
815
									echo "<td class='p-2 $class'>";
816
817
										switch( $key ) {
818
819
											case 'status':
820
												echo $_suscription->get_status_label_html();
821
												break;
822
823
											case 'item':
824
												$markup = array_map( array( 'WPInv_Subscriptions_List_Table', 'generate_item_markup' ), array_keys( $subscription_group['items'] ) );
825
												echo implode( ' | ', $markup );
826
												break;
827
828
											case 'renewals':
829
												$max_bills = $_suscription->get_bill_times();
830
												echo $_suscription->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "&infin;" : $max_bills );
831
												break;
832
833
											case 'renewal_date':
834
												echo $_suscription->is_active() ? getpaid_format_date_value( $_suscription->get_expiration() ) : "&mdash;";
835
												break;
836
837
											case 'start_date':
838
												echo getpaid_format_date_value( $_suscription->get_date_created() );
839
												break;
840
841
											case 'subscription':
842
												$url = is_admin() ? admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $_suscription->get_id() ) ) : $_suscription->get_view_url();
843
												printf(
844
													'%1$s#%2$s%3$s',
845
													'<a href="' . esc_url( $url ) . '">',
846
													'<strong>' . intval( $_suscription->get_id() ) . '</strong>',
847
													'</a>'
848
												);
849
850
												echo WPInv_Subscriptions_List_Table::column_amount( $_suscription );
851
												break;
852
853
										}
854
855
									echo '</td>';
856
857
								}
858
859
							echo '</tr>';
860
861
						endforeach;
862
					?>
863
864
				</tbody>
865
866
			</table>
867
868
		</div>
869
870
	<?php
871
}
872