Passed
Push — master ( f00b0d...869d1f )
by Brian
05:34
created

getpaid_admin_subscription_related_subscriptions_metabox()   D

Complexity

Conditions 18
Paths 49

Size

Total Lines 122
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

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