Passed
Pull Request — master (#118)
by
unknown
04:20
created

subscriptions.php ➔ wpinv_recurring_subscription_details()   D

Complexity

Conditions 13
Paths 20

Size

Total Lines 244
Code Lines 130

Duplication

Lines 5
Ratio 2.05 %

Importance

Changes 0
Metric Value
cc 13
eloc 130
nc 20
nop 0
dl 5
loc 244
rs 4.9922
c 0
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
 * Render the Subscriptions table
4
 *
5
 * @access      public
6
 * @since       1.0.0
7
 * @return      void
8
 */
9
function wpinv_subscriptions_page() {
10
11
	if ( ! empty( $_GET['id'] ) ) {
12
13
        wpinv_recurring_subscription_details();
14
15
		return;
16
17
	}
18
	?>
19
	<div class="wrap">
20
21
		<h1>
22
			<?php _e( 'Subscriptions', 'invoicing' ); ?>
23
		</h1>
24
		<?php
25
		$subscribers_table = new WPInv_Subscription_Reports_Table();
26
		$subscribers_table->prepare_items();
27
		?>
28
29
		<form id="subscribers-filter" method="get">
30
31
			<input type="hidden" name="post_type" value="download" />
32
			<input type="hidden" name="page" value="wpinv-subscriptions" />
33
			<?php $subscribers_table->views(); ?>
34
			<?php $subscribers_table->search_box( __( 'Search', 'wpinvoicing' ), 'subscriptions' ); ?>
35
			<?php $subscribers_table->display(); ?>
36
37
		</form>
38
	</div>
39
	<?php
40
}
41
42
/**
43
 * Recurring Subscription Details
44
 * @description Outputs the subscriber details
45
 * @since       1.0.0
46
 *
47
 */
48
function wpinv_recurring_subscription_details() {
49
50
	$render = true;
0 ignored issues
show
Unused Code introduced by
$render is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
52
	if ( ! current_user_can( 'manage_invoicing' ) ) {
53
		die( __( 'You are not permitted to view this data.', 'invoicing' ) );
54
	}
55
56
	if ( ! isset( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) {
57
        die( __( 'Invalid subscription ID Provided.', 'invoicing' ) );
58
	}
59
60
	$sub_id  = (int) $_GET['id'];
61
	$sub     = new WPInv_Subscription( $sub_id );
62
63
	if ( empty( $sub ) ) {
64
		die( __( 'Invalid subscription ID Provided.', 'invoicing' ) );
65
	}
66
67
	?>
68
	<div class="wrap">
69
		<h2><?php _e( 'Subscription Details', 'invoicing' ); ?></h2>
70
71
		<?php if ( $sub ) : ?>
72
73
			<div id="wpinv-item-card-wrapper">
74
75
				<?php do_action( 'wpinv_subscription_card_top', $sub ); ?>
76
77
				<div class="info-wrapper item-section">
78
79
					<form id="edit-item-info" method="post" action="<?php echo admin_url( 'admin.php?page=wpinv-subscriptions&id=' . $sub->id ); ?>">
80
81
						<div class="item-info">
82
83
							<table class="widefat striped">
84
								<tbody>
85
									<tr>
86
										<td class="row-title">
87
											<label for="tablecell"><?php _e( 'Billing Cycle:', 'invoicing' ); ?></label>
88
										</td>
89
										<td>
90
											<?php
91
											$frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $sub->period, $sub->frequency );
0 ignored issues
show
Documentation introduced by
The property frequency does not exist on object<WPInv_Subscription>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
											$billing   = wpinv_price( wpinv_format_amount( $sub->recurring_amount ), wpinv_get_invoice_currency_code( $sub->parent_payment_id ) ) . ' / ' . $frequency;
93
											$initial   = wpinv_price( wpinv_format_amount( $sub->initial_amount ), wpinv_get_invoice_currency_code( $sub->parent_payment_id ) );
94
											printf( _x( '%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing' ), $initial, $billing );
95
											?>
96
										</td>
97
									</tr>
98
									<tr>
99
										<td class="row-title">
100
											<label for="tablecell"><?php _e( 'Times Billed:', 'invoicing' ); ?></label>
101
										</td>
102
										<td><?php echo $sub->get_times_billed() . ' / ' . ( ( $sub->bill_times == 0 ) ? 'Until Cancelled' : $sub->bill_times ); ?></td>
103
									</tr>
104
									<tr>
105
										<td class="row-title">
106
											<label for="tablecell"><?php _e( 'Customer:', 'invoicing' ); ?></label>
107
										</td>
108
										<td>
109
											<?php $subscriber = get_userdata( $sub->customer_id ); ?>
110
											<a href="<?php echo esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&view=overview&id=' . $subscriber->ID ) ); ?>" target="_blank"><?php echo ! empty( $subscriber->display_name ) ? $subscriber->display_name : $subscriber->user_email; ?></a>
111
										</td>
112
									</tr>
113
									<tr>
114
										<td class="row-title">
115
											<label for="tablecell"><?php _e( 'Initial Purchase ID:', 'invoicing' ); ?></label>
116
										</td>
117
										<td><?php echo '<a href="' . get_edit_post_link( $sub->parent_payment_id ) . '" target="_blank">' . $sub->parent_payment_id . '</a>'; ?></td>
118
									</tr>
119
									<tr>
120
										<td class="row-title">
121
											<label for="tablecell"><?php _e( 'Item:', 'invoicing' ); ?></label>
122
										</td>
123
										<td>
124
											<?php
125
                                            echo wpinv_item_dropdown( array(
126
                                                'name'             => 'product_id',
127
                                                'id'               => 'wpinv_invoice_item',
128
                                                'with_packages'    => false,
129
                                                'show_recurring'   => true,
130
                                                'selected' => $sub->product_id,
131
                                                'class' => 'wpinv-sub-product-id',
132
                                            ) );
133
134
                                            ?>
135
											<a href="<?php echo esc_url( add_query_arg( array(
136
													'post'   => $sub->product_id,
137
													'action' => 'edit'
138
												), admin_url( 'post.php' ) ) ); ?>" target="_blank"><?php _e( 'View Item', 'invoicing' ) ; ?></a>
139
										</td>
140
									</tr>
141
									<tr>
142
										<td class="row-title">
143
											<label for="tablecell"><?php _e( 'Payment Method:', 'invoicing' ); ?></label>
144
										</td>
145
										<td><?php echo wpinv_get_gateway_admin_label( wpinv_get_payment_gateway( $sub->parent_payment_id ) ); ?></td>
146
									</tr>
147
									<tr>
148
										<td class="row-title">
149
											<label for="tablecell"><?php _e( 'Profile ID:', 'invoicing' ); ?></label>
150
										</td>
151
										<td>
152
											<span class="wpinv-sub-profile-id">
153
												<?php echo apply_filters( 'wpinv_subscription_profile_link_' . $sub->gateway, $sub->profile_id, $sub ); ?>
154
											</span>
155
											<input type="text" name="profile_id" class="hidden wpinv-sub-profile-id" value="<?php echo esc_attr( $sub->profile_id ); ?>" />
156
											<span>&nbsp;&ndash;&nbsp;</span>
157
											<a href="#" class="wpinv-edit-sub-profile-id"><?php _e( 'Edit', 'invoicing' ); ?></a>
158
										</td>
159
									</tr>
160
									<tr>
161
										<td class="row-title">
162
											<label for="tablecell"><?php _e( 'Transaction ID:', 'invoicing' ); ?></label>
163
										</td>
164
										<td>
165
											<span class="wpinv-sub-transaction-id"><?php echo $sub->get_transaction_id(); ?></span>
166
											<input type="text" name="transaction_id" class="hidden wpinv-sub-transaction-id" value="<?php echo esc_attr( $sub->get_transaction_id() ); ?>" />
167
											<span>&nbsp;&ndash;&nbsp;</span>
168
											<a href="#" class="wpinv-edit-sub-transaction-id"><?php _e( 'Edit', 'invoicing' ); ?></a>
169
										</td>
170
									</tr>
171
									<tr>
172
										<td class="row-title">
173
											<label for="tablecell"><?php _e( 'Date Created:', 'invoicing' ); ?></label>
174
										</td>
175
										<td><?php echo date_i18n( get_option( 'date_format' ), strtotime( $sub->created, current_time( 'timestamp' ) ) ); ?></td>
176
									</tr>
177
									<tr>
178
										<td class="row-title">
179
											<label for="tablecell">
180 View Code Duplication
												<?php if( 'trialling' == $sub->status ) : ?>
181
													<?php _e( 'Trialling Until:', 'invoicing' ); ?>
182
												<?php else: ?>
183
													<?php _e( 'Expiration Date:', 'invoicing' ); ?>
184
												<?php endif; ?>
185
											</label>
186
										</td>
187
										<td>
188
											<span class="wpinv-sub-expiration"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $sub->expiration, current_time( 'timestamp' ) ) ); ?></span>
189
											<input type="text" name="expiration" class="wpiDatepicker hidden wpinv-sub-expiration" data-dateFormat="mm/dd/yy" value="<?php echo esc_attr( $sub->expiration ); ?>" />
190
											<span>&nbsp;&ndash;&nbsp;</span>
191
											<a href="#" class="wpinv-edit-sub-expiration"><?php _e( 'Edit', 'invoicing' ); ?></a>
192
										</td>
193
									</tr>
194
									<tr>
195
										<td class="row-title">
196
											<label for="tablecell"><?php _e( 'Subscription Status:', 'invoicing' ); ?></label>
197
										</td>
198
										<td>
199
											<select name="status">
200
												<option value="pending"<?php selected( 'pending', $sub->status ); ?>><?php _e( 'Pending', 'invoicing' ); ?></option>
201
												<option value="active"<?php selected( 'active', $sub->status ); ?>><?php _e( 'Active', 'invoicing' ); ?></option>
202
												<option value="cancelled"<?php selected( 'cancelled', $sub->status ); ?>><?php _e( 'Cancelled', 'invoicing' ); ?></option>
203
												<option value="expired"<?php selected( 'expired', $sub->status ); ?>><?php _e( 'Expired', 'invoicing' ); ?></option>
204
												<option value="trialling"<?php selected( 'trialling', $sub->status ); ?>><?php _e( 'Trialling', 'invoicing' ); ?></option>
205
												<option value="failing"<?php selected( 'failing', $sub->status ); ?>><?php _e( 'Failing', 'invoicing' ); ?></option>
206
												<option value="completed"<?php selected( 'completed', $sub->status ); ?>><?php _e( 'Completed', 'invoicing' ); ?></option>
207
											</select>
208
										</td>
209
									</tr>
210
								</tbody>
211
							</table>
212
						</div>
213
						<div id="wpinv-sub-notices">
214
							<div class="notice notice-info inline hidden" id="wpinv-sub-expiration-update-notice"><p><?php _e( 'Changing the expiration date will not affect when renewal payments are processed.', 'invoicing' ); ?></p></div>
215
							<div class="notice notice-info inline hidden" id="wpinv-sub-product-update-notice"><p><?php _e( 'Changing the product assigned will not automatically adjust any pricing.', 'invoicing' ); ?></p></div>
216
							<div class="notice notice-warning inline hidden" id="wpinv-sub-profile-id-update-notice"><p><?php _e( 'Changing the profile ID can result in renewals not being processed. Do this with caution.', 'invoicing' ); ?></p></div>
217
						</div>
218
						<div id="item-edit-actions" class="edit-item" style="float:right; margin: 10px 0 0; display: block;">
219
							<?php wp_nonce_field( 'wpinv-recurring-update', 'wpinv-recurring-update-nonce', false, true ); ?>
220
							<input type="submit" name="wpinv_update_subscription" id="wpinv_update_subscription" class="button button-primary" value="<?php _e( 'Update Subscription', 'invoicing' ); ?>"/>
221
							<input type="hidden" name="sub_id" value="<?php echo absint( $sub->id ); ?>" />
222
							<?php if( $sub->can_cancel() ) : ?>
223
								<a class="button button-primary" href="<?php echo $sub->get_cancel_url(); ?>" ><?php _e( 'Cancel Subscription', 'invoicing' ); ?></a>
224
							<?php endif; ?>
225
							&nbsp;<input type="submit" name="wpinv_delete_subscription" class="wpinv-delete-subscription button" value="<?php _e( 'Delete Subscription', 'invoicing' ); ?>"/>
226
						</div>
227
228
					</form>
229
				</div>
230
231
				<?php do_action( 'wpinv_subscription_before_stats', $sub ); ?>
232
233
				<?php do_action( 'wpinv_subscription_before_tables_wrapper', $sub ); ?>
234
235
				<div id="item-tables-wrapper" class="item-section">
236
237
					<?php do_action( 'wpinv_subscription_before_tables', $sub ); ?>
238
239
					<h3><?php _e( 'Renewal Invoices:', 'invoicing' ); ?></h3>
240
					<?php $payments = $sub->get_child_payments(); ?>
241
					<?php if( 'manual' == $sub->gateway ) : ?>
242
						<p><strong><?php _e( 'Note:', 'invoicing' ); ?></strong> <?php _e( 'Subscriptions purchased with the Test Payment gateway will not renew automatically.', 'invoicing' ); ?></p>
243
					<?php endif; ?>
244
					<table class="wp-list-table widefat striped payments">
245
						<thead>
246
						<tr>
247
							<th><?php _e( 'ID', 'invoicing' ); ?></th>
248
							<th><?php _e( 'Amount', 'invoicing' ); ?></th>
249
							<th><?php _e( 'Date', 'invoicing' ); ?></th>
250
							<th><?php _e( 'Status', 'invoicing' ); ?></th>
251
							<th><?php _e( 'Actions', 'invoicing' ); ?></th>
252
						</tr>
253
						</thead>
254
						<tbody>
255
						<?php if ( ! empty( $payments ) ) : ?>
256
							<?php foreach ( $payments as $payment ) : ?>
257
								<tr>
258
									<td><?php echo $payment->ID; ?></td>
259
									<td><?php echo wpinv_payment_total( $payment->ID ); ?></td>
260
									<td><?php echo date_i18n( get_option( 'date_format' ), strtotime( $payment->post_date ) ); ?></td>
261
									<td><?php echo wpinv_get_invoice_status( $payment->ID, true ); ?></td>
262
									<td>
263
										<a title="<?php _e( 'View Details for Invoice.', 'invoicing' );
264
										echo ' ' . $payment->ID; ?>" href="<?php echo get_edit_post_link( $payment->ID ); ?>">
265
											<?php _e( 'View Details', 'invoicing' ); ?>
266
										</a>
267
										<?php do_action( 'wpinv_subscription_payments_actions', $sub, $payment ); ?>
268
									</td>
269
								</tr>
270
							<?php endforeach; ?>
271
						<?php else: ?>
272
							<tr>
273
								<td colspan="5"><?php _e( 'No Invoices Found.', 'invoicing' ); ?></td>
274
							</tr>
275
						<?php endif; ?>
276
						</tbody>
277
						<tfoot></tfoot>
278
					</table>
279
280
					<?php do_action( 'wpinv_subscription_after_tables', $sub ); ?>
281
282
				</div>
283
284
				<?php do_action( 'wpinv_subscription_card_bottom', $sub ); ?>
285
			</div>
286
287
		<?php endif; ?>
288
289
	</div>
290
	<?php
291
}
292
293
/**
294
 * Handles subscription update
295
 *
296
 * @access      public
297
 * @since       1.0.0
298
 * @return      void
299
 */
300
function wpinv_recurring_process_subscription_update() {
301
302
	if( empty( $_POST['sub_id'] ) ) {
303
		return;
304
	}
305
306
	if( empty( $_POST['wpinv_update_subscription'] ) ) {
307
		return;
308
	}
309
310
	if( ! current_user_can( 'manage_invoicing') ) {
311
		return;
312
	}
313
314
	if( ! wp_verify_nonce( $_POST['wpinv-recurring-update-nonce'], 'wpinv-recurring-update' ) ) {
315
		wp_die( __( 'Nonce verification failed', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
316
	}
317
318
	$expiration      = date( 'Y-m-d 23:59:59', strtotime( $_POST['expiration'] ) );
319
	$profile_id      = sanitize_text_field( $_POST['profile_id'] );
320
	$transaction_id  = sanitize_text_field( $_POST['transaction_id'] );
321
	$product_id      = absint( $_POST['product_id'] );
322
	$subscription    = new WPInv_Subscription( absint( $_POST['sub_id'] ) );
323
	$subscription->update( array(
324
		'status'         => sanitize_text_field( $_POST['status'] ),
325
		'expiration'     => $expiration,
326
		'profile_id'     => $profile_id,
327
		'product_id'     => $product_id,
328
		'transaction_id' => $transaction_id,
329
	) );
330
331
	$status = sanitize_text_field( $_POST['status'] );
332
333
	switch( $status ) {
334
335
		case 'cancelled' :
336
337
			$subscription->cancel();
338
			break;
339
340
		case 'expired' :
341
342
			$subscription->expire();
343
			break;
344
345
		case 'completed' :
346
347
			$subscription->complete();
348
			break;
349
350
	}
351
352
	wp_redirect( admin_url( 'admin.php?page=wpinv-subscriptions&wpinv-message=updated&id=' . $subscription->id ) );
353
	exit;
354
355
}
356
add_action( 'admin_init', 'wpinv_recurring_process_subscription_update', 1 );
357
358
/**
359
 * Handles subscription deletion
360
 *
361
 * @access      public
362
 * @since       1.0.0
363
 * @return      void
364
 */
365
function wpinv_recurring_process_subscription_deletion() {
366
367
	if( empty( $_POST['sub_id'] ) ) {
368
		return;
369
	}
370
371
	if( empty( $_POST['wpinv_delete_subscription'] ) ) {
372
		return;
373
	}
374
375
	if( ! current_user_can( 'manage_invoicing') ) {
376
		return;
377
	}
378
379
	if( ! wp_verify_nonce( $_POST['wpinv-recurring-update-nonce'], 'wpinv-recurring-update' ) ) {
380
		wp_die( __( 'Nonce verification failed', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
381
	}
382
383
	$subscription = new WPInv_Subscription( absint( $_POST['sub_id'] ) );
384
385
	delete_post_meta( $subscription->parent_payment_id, '_wpinv_subscription_payment' );
386
387
	$subscription->delete();
388
389
	wp_redirect( admin_url( 'admin.php?page=wpinv-subscriptions&wpinv-message=deleted' ) );
390
	exit;
391
392
}
393
add_action( 'admin_init', 'wpinv_recurring_process_subscription_deletion', 2 );
394