Passed
Push — master ( 5452c3...c16653 )
by Brian
04:51 queued 19s
created

getpaid_admin_subscription_details_metabox()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 57
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 0
loc 57
rs 8.2826
cc 7
nc 20
nop 1

How to fix   Long Method   

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->get_id() ) {
86
87
		echo aui()->alert(
88
			array(
89
				'type'    => 'danger',
90
				'content' => __( 'Subscription not found.', 'invoicing' ),
91
			)
92
		);
93
94
		return;
95
	}
96
97
	// Use metaboxes to display the subscription details.
98
	add_meta_box( 'getpaid_admin_subscription_details_metabox', __( 'Subscription Details', 'invoicing' ), 'getpaid_admin_subscription_details_metabox', get_current_screen(), 'normal' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

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

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

}

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

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

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

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

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

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

}

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

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

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

Loading history...
100
	add_meta_box( 'getpaid_admin_subscription_invoice_details_metabox', __( '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...
101
	do_action( 'getpaid_admin_single_subscription_register_metabox', $sub );
102
103
	?>
104
105
		<form method="post" action="<?php echo admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $sub->get_id() ) ); ?>">
106
107
			<?php wp_nonce_field( 'getpaid-nonce', 'getpaid-nonce' ); ?>
108
			<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
109
			<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
110
			<input type="hidden" name="getpaid-admin-action" value="update_single_subscription" />
111
			<input type="hidden" name="subscription_id" value="<?php echo (int) $sub->get_id() ;?>" />
112
113
			<div id="poststuff">
114
				<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...
115
116
					<div id="postbox-container-1" class="postbox-container">
117
						<?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...
118
					</div>
119
120
					<div id="postbox-container-2" class="postbox-container">
121
						<?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...
122
						<?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...
123
					</div>
124
125
				</div>
126
			</div>
127
128
		</form>
129
130
		<script>jQuery(document).ready(function(){ postboxes.add_postbox_toggles('getpaid_page_wpinv-subscriptions'); });</script>
131
132
	<?php
133
134
}
135
136
/**
137
 * Displays the subscription details metabox.
138
 *
139
 * @param WPInv_Subscription $sub
140
 */
141
function getpaid_admin_subscription_details_metabox( $sub ) {
142
143
	// Prepare subscription detail columns.
144
	$fields = apply_filters(
145
		'getpaid_subscription_admin_page_fields',
146
		array(
147
			'subscription'   => __( 'Subscription', 'invoicing' ),
148
			'customer'       => __( 'Customer', 'invoicing' ),
149
			'amount'         => __( 'Amount', 'invoicing' ),
150
			'start_date'     => __( 'Start Date', 'invoicing' ),
151
			'renews_on'      => __( 'Next Payment', 'invoicing' ),
152
			'renewals'       => __( 'Renewals', 'invoicing' ),
153
			'item'           => __( 'Item', 'invoicing' ),
154
			'gateway'        => __( 'Payment Method', 'invoicing' ),
155
			'profile_id'     => __( 'Profile ID', 'invoicing' ),
156
			'status'         => __( 'Status', 'invoicing' ),
157
		)
158
	);
159
160
	if ( ! $sub->is_active() ) {
161
162
		if ( isset( $fields['renews_on'] ) ) {
163
			unset( $fields['renews_on'] );
164
		}
165
166
		if ( isset( $fields['gateway'] ) ) {
167
			unset( $fields['gateway'] );
168
		}
169
		
170
	}
171
172
	$profile_id = $sub->get_profile_id();
173
	if ( empty( $profile_id ) && isset( $fields['profile_id'] ) ) {
174
		unset( $fields['profile_id'] );
175
	}
176
177
	?>
178
179
		<table class="table table-borderless" style="font-size: 14px;">
180
			<tbody>
181
182
				<?php foreach ( $fields as $key => $label ) : ?>
183
184
					<tr class="getpaid-subscription-meta-<?php echo sanitize_html_class( $key ); ?>">
185
186
						<th class="w-25" style="font-weight: 500;">
187
							<?php echo sanitize_text_field( $label ); ?>
188
						</th>
189
190
						<td class="w-75 text-muted">
191
							<?php do_action( 'getpaid_subscription_admin_display_' . sanitize_text_field( $key ), $sub ); ?>
192
						</td>
193
194
					</tr>
195
196
				<?php endforeach; ?>
197
198
			</tbody>
199
		</table>
200
201
	<?php
202
}
203
204
/**
205
 * Displays the subscription customer.
206
 *
207
 * @param WPInv_Subscription $subscription
208
 */
209
function getpaid_admin_subscription_metabox_display_customer( $subscription ) {
210
211
	$username = __( '(Missing User)', 'invoicing' );
212
213
	$user = get_userdata( $subscription->get_customer_id() );
214
	if ( $user ) {
215
216
		$username = sprintf(
217
			'<a href="user-edit.php?user_id=%s">%s</a>',
218
			absint( $user->ID ),
219
			! empty( $user->display_name ) ? sanitize_text_field( $user->display_name ) : sanitize_email( $user->user_email )
220
		);
221
222
	}
223
224
	echo  $username;
225
}
226
add_action( 'getpaid_subscription_admin_display_customer', 'getpaid_admin_subscription_metabox_display_customer' );
227
228
/**
229
 * Displays the subscription amount.
230
 *
231
 * @param WPInv_Subscription $subscription
232
 */
233
function getpaid_admin_subscription_metabox_display_amount( $subscription ) {
234
235
	$initial   = wpinv_price( wpinv_format_amount( wpinv_sanitize_amount( $subscription->get_initial_amount() ) ), $subscription->get_parent_payment()->get_currency() );
236
	$recurring = wpinv_price( wpinv_format_amount( wpinv_sanitize_amount( $subscription->get_recurring_amount() ) ), $subscription->get_parent_payment()->get_currency() );
237
	$period    = 1 == $subscription->get_frequency() ? getpaid_get_subscription_period_label( $subscription->get_period() ) : WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->get_period(),$subscription->get_frequency() );
238
239
	if ( $subscription->has_trial_period() ) {
240
241
		// translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period
242
		$amount = sprintf(
243
			_x( '%1$s trial for %2$s(s) then %3$s / %4$s', 'Subscription amount on admin table. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ),
244
			$initial,
245
			sanitize_text_field( $subscription->get_trial_period() ),
246
			$recurring,
247
			sanitize_text_field( strtolower( $period ) )
248
		);
249
250
	} else if ( $initial != $recurring ) {
251
252
		// translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring perio
253
		$amount = sprintf(
254
			_x( 'Initial payment of %1$s then %2$s / %3$s', 'Subscription amount on admin table. (e.g.:Initial payment of $100 then $120 / year)', 'invoicing' ),
255
			$initial,
256
			$recurring,
257
			sanitize_text_field( strtolower( $period ) )
258
		);
259
260
	} else {
261
262
		// translators: $1: is the recurring amount, $2: is the recurring period
263
		$amount = sprintf(
264
			_x( '%1$s / %2$s', 'Subscription amount on admin table. (e.g.: $120 / year)', 'invoicing' ),
265
			$initial,
266
			sanitize_text_field( strtolower( $period ) )
267
		);
268
269
	}
270
271
	echo "<span>$amount</span>";
272
}
273
add_action( 'getpaid_subscription_admin_display_amount', 'getpaid_admin_subscription_metabox_display_amount' );
274
275
/**
276
 * Displays the subscription id.
277
 *
278
 * @param WPInv_Subscription $subscription
279
 */
280
function getpaid_admin_subscription_metabox_display_id( $subscription ) {
281
	echo  '#' . absint( $subscription->get_id() );
282
}
283
add_action( 'getpaid_subscription_admin_display_subscription', 'getpaid_admin_subscription_metabox_display_id' );
284
285
/**
286
 * Displays the subscription renewal date.
287
 *
288
 * @param WPInv_Subscription $subscription
289
 */
290
function getpaid_admin_subscription_metabox_display_start_date( $subscription ) {
291
292
	$created = $subscription->get_date_created();
293
	if ( empty( $created ) || '0000-00-00 00:00:00' == $created ) {
294
		echo "&mdash;";
295
	} else {
296
		echo date_i18n( /** @scrutinizer ignore-type */get_option( 'date_format' ), strtotime( $created ) );
297
	}
298
299
}
300
add_action( 'getpaid_subscription_admin_display_start_date', 'getpaid_admin_subscription_metabox_display_start_date' );
301
302
/**
303
 * Displays the subscription renewal date.
304
 *
305
 * @param WPInv_Subscription $subscription
306
 */
307
function getpaid_admin_subscription_metabox_display_renews_on( $subscription ) {
308
309
	$expiration = $subscription->get_expiration();
310
	if ( empty( $expiration ) || '0000-00-00 00:00:00' == $expiration ) {
311
		echo "&mdash;";
312
	} else {
313
		echo date_i18n( /** @scrutinizer ignore-type */get_option( 'date_format' ), strtotime( $expiration ) );
314
	}
315
316
}
317
add_action( 'getpaid_subscription_admin_display_renews_on', 'getpaid_admin_subscription_metabox_display_renews_on' );
318
319
/**
320
 * Displays the subscription renewal count.
321
 *
322
 * @param WPInv_Subscription $subscription
323
 */
324
function getpaid_admin_subscription_metabox_display_renewals( $subscription ) {
325
	$max_bills = $subscription->get_bill_times();
326
	echo $subscription->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "&infin;" : $max_bills );
327
}
328
add_action( 'getpaid_subscription_admin_display_renewals', 'getpaid_admin_subscription_metabox_display_renewals' );
329
330
/**
331
 * Displays the subscription item.
332
 *
333
 * @param WPInv_Subscription $subscription
334
 */
335
function getpaid_admin_subscription_metabox_display_item( $subscription ) {
336
337
	$item = get_post( $subscription->get_product_id() );
338
339
	if ( ! empty( $item ) ) {
340
		$link = get_edit_post_link( $item );
341
		$link = esc_url( $link );
342
		$name = esc_html( get_the_title( $item ) );
343
		echo "<a href='$link'>$name</a>";
344
	} else {
345
		echo sprintf( __( 'Item #%s', 'invoicing' ), $subscription->get_product_id() );
346
	}
347
348
}
349
add_action( 'getpaid_subscription_admin_display_item', 'getpaid_admin_subscription_metabox_display_item' );
350
351
/**
352
 * Displays the subscription gateway.
353
 *
354
 * @param WPInv_Subscription $subscription
355
 */
356
function getpaid_admin_subscription_metabox_display_gateway( $subscription ) {
357
358
	$gateway = $subscription->get_gateway();
359
360
	if ( ! empty( $gateway ) ) {
361
		echo sanitize_text_field( wpinv_get_gateway_admin_label( $gateway ) );
362
	} else {
363
		echo "&mdash;";
364
	}
365
366
}
367
add_action( 'getpaid_subscription_admin_display_gateway', 'getpaid_admin_subscription_metabox_display_gateway' );
368
369
/**
370
 * Displays the subscription status.
371
 *
372
 * @param WPInv_Subscription $subscription
373
 */
374
function getpaid_admin_subscription_metabox_display_status( $subscription ) {
375
	echo $subscription->get_status_label_html();
376
}
377
add_action( 'getpaid_subscription_admin_display_status', 'getpaid_admin_subscription_metabox_display_status' );
378
379
/**
380
 * Displays the subscription profile id.
381
 *
382
 * @param WPInv_Subscription $subscription
383
 */
384
function getpaid_admin_subscription_metabox_display_profile_id( $subscription ) {
385
386
	$profile_id = $subscription->get_profile_id();
387
388
	if ( ! empty( $profile_id ) ) {
389
		$profile_id = sanitize_text_field( $profile_id );
390
		echo apply_filters( 'getpaid_subscription_profile_id_display', $profile_id, $subscription );
391
	} else {
392
		echo "&mdash;";
393
	}
394
395
}
396
add_action( 'getpaid_subscription_admin_display_profile_id', 'getpaid_admin_subscription_metabox_display_profile_id' );
397
398
/**
399
 * Displays the subscriptions update metabox.
400
 * 
401
 * @param WPInv_Subscription $subscription
402
 */
403
function getpaid_admin_subscription_update_metabox( $subscription ) {
404
405
	?>
406
	<div class="mt-3">
407
408
		<?php
409
			echo aui()->select(
410
				array(
411
					'options'          => getpaid_get_subscription_statuses(),
412
					'name'             => 'subscription_status',
413
					'id'               => 'subscription_status_update_select',
414
					'required'         => true,
415
					'no_wrap'          => false,
416
					'label'            => __( 'Subscription Status', 'invoicing' ),
417
					'help_text'        => __( 'Updating the status will trigger related actions and hooks', 'invoicing' ),
418
					'select2'          => true,
419
					'value'            => $subscription->get_status( 'edit' ),
420
				)
421
			);
422
		?>
423
424
		<div class="mt-2 px-3 py-2 bg-light border-top" style="margin: -12px;">
425
	
426
		<?php
427
			submit_button( __( 'Update', 'invoicing' ), 'primary', 'submit', false );
428
429
			$url    = esc_url( wp_nonce_url( add_query_arg( 'getpaid-admin-action', 'subscription_manual_renew' ), 'getpaid-nonce', 'getpaid-nonce' ) );
430
			$anchor = __( 'Renew Subscription', 'invoicing' );
431
			$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' );
432
433
			if ( $subscription->is_active() ) {
434
				echo "<a href='$url' class='float-right text-muted' onclick='return confirm(\"$title\")'>$anchor</a>";
435
			}
436
437
	echo '</div></div>';
438
}
439
440
/**
441
 * Displays the subscriptions invoices metabox.
442
 * 
443
 * @param WPInv_Subscription $subscription
444
 */
445
function getpaid_admin_subscription_invoice_details_metabox( $subscription ) {
446
447
	$columns = apply_filters(
448
		'getpaid_subscription_related_invoices_columns',
449
		array(
450
			'invoice'      => __( 'Invoice', 'invoicing' ),
451
			'relationship' => __( 'Relationship', 'invoicing' ),
452
			'date'         => __( 'Date', 'invoicing' ),
453
			'status'       => __( 'Status', 'invoicing' ),
454
			'total'        => __( 'Total', 'invoicing' ),
455
		),
456
		$subscription
457
	);
458
459
	// Prepare the invoices.
460
	$payments = $subscription->get_child_payments();
461
	$parent   = $subscription->get_parent_invoice();
462
463
	if ( $parent->get_id() ) {
464
		$payments = array_merge( array( $parent ), $payments );
465
	}
466
	
467
	$table_class = 'w-100 bg-white';
468
469
	if ( ! is_admin() ) {
470
		$table_class = 'table table-bordered table-striped';
471
	}
472
473
	?>
474
		<div class="m-0" style="overflow: auto;">
475
476
			<table class="<?php echo $table_class; ?>">
477
478
				<thead>
479
					<tr>
480
						<?php
481
							foreach ( $columns as $key => $label ) {
482
								$key   = esc_attr( $key );
483
								$label = sanitize_text_field( $label );
484
485
								echo "<th class='subscription-invoice-field-$key bg-light p-2 text-left color-dark'>$label</th>";
486
							}
487
						?>
488
					</tr>
489
				</thead>
490
491
				<tbody>
492
493
					<?php if ( empty( $payments ) ) : ?>
494
						<tr>
495
							<td colspan="<?php echo count($columns); ?>" class="p-2 text-left text-muted">
496
								<?php _e( 'This subscription has no invoices.', 'invoicing' ); ?>
497
							</td>
498
						</tr>
499
					<?php endif; ?>
500
501
					<?php
502
503
						foreach( $payments as $payment ) :
504
505
							// Ensure that we have an invoice.
506
							$payment = new WPInv_Invoice( $payment );
507
508
							// Abort if the invoice is invalid.
509
							if ( ! $payment->get_id() ) {
510
								continue;
511
							}
512
513
							echo '<tr>';
514
515
								foreach ( array_keys( $columns ) as $key ) {
516
									
517
									echo '<td class="p-2 text-left">';
518
519
										switch( $key ) {
520
521
											case 'total':
522
												echo '<strong>' . wpinv_price( wpinv_format_amount( wpinv_sanitize_amount( $payment->get_total ) ), $payment->get_currency() ) . '</strong>';
0 ignored issues
show
Bug Best Practice introduced by
The property get_total does not exist on WPInv_Invoice. Since you implemented __get, consider adding a @property annotation.
Loading history...
523
												break;
524
525
											case 'relationship':
526
												echo $payment->is_renewal() ? __( 'Renewal Invoice', 'invoicing' ) : __( 'Initial Invoice', 'invoicing' );
527
												break;
528
529
											case 'date':
530
												echo date_i18n( /** @scrutinizer ignore-type */get_option( 'date_format' ), strtotime( $payment->get_date_created() ) );
531
												break;
532
533
											case 'status':
534
535
												$status = $payment->get_status_nicename();
536
												if ( is_admin() ) {
537
													$status = $payment->get_status_label_html();
538
												}
539
540
												echo $status;
541
												break;
542
543
											case 'invoice':
544
												$link    = esc_url( get_edit_post_link( $payment->get_id() ) );
545
546
												if ( ! is_admin() ) {
547
													$link = esc_url( $payment->get_view_url() );
548
												}
549
550
												$invoice = sanitize_text_field( $payment->get_number() );
551
												echo "<a href='$link'>$invoice</a>";
552
												break;
553
										}
554
555
									echo '</td>';
556
557
								}
558
559
							echo '</tr>';
560
561
						endforeach;
562
					?>
563
564
				</tbody>
565
566
			</table>
567
568
		</div>
569
570
	<?php
571
}
572