Completed
Push — master ( 10354b...44551c )
by Devin
29:37 queued 13:08
created

actions.php ➔ give_update_payment_details()   F

Complexity

Conditions 29
Paths > 20000

Size

Total Lines 189
Code Lines 106

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 29
eloc 106
nc 290304
nop 1
dl 0
loc 189
rs 2

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Admin Payment Actions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Payments
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 *
19
 * Process the payment details edit
20
 *
21
 * @access      private
22
 *
23
 * @param $data
24
 *
25
 * @since       1.0
26
 * @return      void
27
 *
28
 */
29
function give_update_payment_details( $data ) {
30
31
	if ( ! current_user_can( 'edit_give_payments', $data['give_payment_id'] ) ) {
32
		wp_die( __( 'You do not have permission to edit this payment record', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
33
	}
34
35
	check_admin_referer( 'give_update_payment_details_nonce' );
36
37
	// Retrieve the payment ID
38
	$payment_id = absint( $data['give_payment_id'] );
39
40
	// Retrieve existing payment meta
41
	$meta      = give_get_payment_meta( $payment_id );
42
	$user_info = give_get_payment_meta_user_info( $payment_id );
43
44
	$status  = $data['give-payment-status'];
45
	$user_id = isset( $data['give-payment-user-id'] ) ? intval( $data['give-payment-user-id'] ) : '';
46
	$date    = sanitize_text_field( $data['give-payment-date'] );
47
	$hour    = sanitize_text_field( $data['give-payment-time-hour'] );
48
	$form_id = give_get_payment_form_id($payment_id);
49
50
	// Restrict to our high and low
51
	if ( $hour > 23 ) {
52
		$hour = 23;
53
	} elseif ( $hour < 0 ) {
54
		$hour = 00;
55
	}
56
57
	$minute = sanitize_text_field( $data['give-payment-time-min'] );
58
59
	// Restrict to our high and low
60
	if ( $minute > 59 ) {
61
		$minute = 59;
62
	} elseif ( $minute < 0 ) {
63
		$minute = 00;
64
	}
65
66
	$address          = array_map( 'trim', $data['give-payment-address'][0] );
67
	$date             = date( 'Y-m-d', strtotime( $date ) ) . ' ' . $hour . ':' . $minute . ':00';
68
	$curr_total       = give_sanitize_amount( give_get_payment_amount( $payment_id ) );
69
	$new_total        = give_sanitize_amount( $_POST['give-payment-total'] );
70
	$curr_customer_id = sanitize_text_field( $data['give-current-customer'] );
71
	$new_customer_id  = sanitize_text_field( $data['customer-id'] );
72
73
	do_action( 'give_update_edited_purchase', $payment_id );
74
75
	// Update main payment record
76
	$updated = wp_update_post( array(
77
		'ID'        => $payment_id,
78
		'edit_date' => true,
79
		'post_date' => $date
80
	) );
81
82
	if ( 0 === $updated ) {
83
		wp_die( esc_attr__( 'Error Updating Payment', 'give' ), esc_attr__( 'Error', 'give' ), array( 'response' => 400 ) );
84
	}
85
86
	$customer_changed = false;
87
88
	if ( isset( $data['give-new-customer'] ) && $data['give-new-customer'] == '1' ) {
89
90
		$email = isset( $data['give-new-customer-email'] ) ? sanitize_text_field( $data['give-new-customer-email'] ) : '';
91
		$names = isset( $data['give-new-customer-name'] ) ? sanitize_text_field( $data['give-new-customer-name'] ) : '';
92
93
		if ( empty( $email ) || empty( $names ) ) {
94
			wp_die( esc_attr__( 'New Customers require a name and email address', 'give' ) );
95
		}
96
97
		$customer = new Give_Customer( $email );
98
		if ( empty( $customer->id ) ) {
99
			$customer_data = array( 'name' => $names, 'email' => $email );
100
			$user_id       = email_exists( $email );
101
			if ( false !== $user_id ) {
102
				$customer_data['user_id'] = $user_id;
103
			}
104
105
			if ( ! $customer->create( $customer_data ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $customer->create($customer_data) of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
106
				// Failed to crete the new donor, assume the previous donor
107
				$customer_changed = false;
108
				$customer         = new Give_Customer( $curr_customer_id );
109
				give_set_error( 'give-payment-new-customer-fail', __( 'Error creating new donor', 'give' ) );
110
			}
111
		}
112
113
		$new_customer_id = $customer->id;
114
115
		$previous_customer = new Give_Customer( $curr_customer_id );
116
117
		$customer_changed = true;
118
119
	} elseif ( $curr_customer_id !== $new_customer_id ) {
120
121
		$customer = new Give_Customer( $new_customer_id );
122
		$email    = $customer->email;
123
		$names    = $customer->name;
124
125
		$previous_customer = new Give_Customer( $curr_customer_id );
126
127
		$customer_changed = true;
128
129
	} else {
130
131
		$customer = new Give_Customer( $curr_customer_id );
132
		$email    = $customer->email;
133
		$names    = $customer->name;
134
135
	}
136
137
138
	// Setup first and last name from input values
139
	$names      = explode( ' ', $names );
140
	$first_name = ! empty( $names[0] ) ? $names[0] : '';
141
	$last_name  = '';
142
	if ( ! empty( $names[1] ) ) {
143
		unset( $names[0] );
144
		$last_name = implode( ' ', $names );
145
	}
146
147
148
	if ( $customer_changed ) {
149
150
		// Remove the stats and payment from the previous customer and attach it to the new customer
151
		$previous_customer->remove_payment( $payment_id, false );
0 ignored issues
show
Bug introduced by
The variable $previous_customer does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
152
		$customer->attach_payment( $payment_id, false );
153
154
		// If purchase was completed and not ever refunded, adjust stats of customers
155
		if ( 'revoked' == $status || 'publish' == $status ) {
156
157
			$previous_customer->decrease_purchase_count();
158
			$previous_customer->decrease_value( $new_total );
159
160
			$customer->increase_purchase_count();
161
			$customer->increase_value( $new_total );
162
		}
163
164
		update_post_meta( $payment_id, '_give_payment_customer_id', $customer->id );
165
166
	}
167
168
169
	// Set new meta values
170
	$user_info['id']         = $customer->user_id;
171
	$user_info['email']      = $customer->email;
172
	$user_info['first_name'] = $first_name;
173
	$user_info['last_name']  = $last_name;
174
	$user_info['address']    = $address;
175
	$meta['user_info']       = $user_info;
176
177
178
	// Check for payment notes
179
	if ( ! empty( $data['give-payment-note'] ) ) {
180
181
		$note = wp_kses( $data['give-payment-note'], array() );
182
		give_insert_payment_note( $payment_id, $note );
183
184
	}
185
186
	// Set new status
187
	give_update_payment_status( $payment_id, $status );
188
189
	give_update_payment_meta( $payment_id, '_give_payment_user_id', $customer->user_id );
190
	give_update_payment_meta( $payment_id, '_give_payment_user_email', $customer->email );
191
	give_update_payment_meta( $payment_id, '_give_payment_meta', $meta );
192
	give_update_payment_meta( $payment_id, '_give_payment_total', $new_total );
193
194
	// Adjust total store earnings if the payment total has been changed
195
	if ( $new_total !== $curr_total && ( 'publish' == $status || 'revoked' == $status ) ) {
196
197
		if ( $new_total > $curr_total ) {
198
			// Increase if our new total is higher
199
			$difference = $new_total - $curr_total;
200
			give_increase_total_earnings( $difference );
201
			$form = new Give_Donate_Form( $form_id );
202
			$form->increase_earnings( $difference );
203
		} elseif ( $curr_total > $new_total ) {
204
			// Decrease if our new total is lower
205
			$difference = $curr_total - $new_total;
206
			give_decrease_total_earnings( $difference );
207
			$form = new Give_Donate_Form( $form_id );
208
			$form->decrease_earnings( $difference );
209
		}
210
211
	}
212
213
	do_action( 'give_updated_edited_purchase', $payment_id );
214
215
	wp_safe_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&give-message=payment-updated&id=' . $payment_id ) );
216
	exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_update_payment_details() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
217
}
218
219
add_action( 'give_update_payment_details', 'give_update_payment_details' );
220
221
/**
222
 * Trigger a Purchase Deletion
223
 *
224
 * @since 1.0
225
 *
226
 * @param $data Arguments passed
227
 *
228
 * @return void
229
 */
230
function give_trigger_purchase_delete( $data ) {
231
	if ( wp_verify_nonce( $data['_wpnonce'], 'give_payment_nonce' ) ) {
232
233
		$payment_id = absint( $data['purchase_id'] );
234
235
		if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
236
			wp_die( __( 'You do not have permission to edit this payment record', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
237
		}
238
239
		give_delete_purchase( $payment_id );
240
		wp_redirect( admin_url( '/edit.php?post_type=give_forms&page=give-payment-history&give-message=payment_deleted' ) );
241
		give_die();
242
	}
243
}
244
245
add_action( 'give_delete_payment', 'give_trigger_purchase_delete' );
246
247
function give_ajax_store_payment_note() {
248
249
	$payment_id = absint( $_POST['payment_id'] );
250
	$note       = wp_kses( $_POST['note'], array() );
251
252
	if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
253
		wp_die( __( 'You do not have permission to edit this payment record', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
254
	}
255
256
	if ( empty( $payment_id ) ) {
257
		die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_ajax_store_payment_note() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
258
	}
259
260
	if ( empty( $note ) ) {
261
		die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_ajax_store_payment_note() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
262
	}
263
264
	$note_id = give_insert_payment_note( $payment_id, $note );
265
	die( give_get_payment_note_html( $note_id ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_ajax_store_payment_note() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
266
}
267
268
add_action( 'wp_ajax_give_insert_payment_note', 'give_ajax_store_payment_note' );
269
270
/**
271
 * Triggers a payment note deletion without ajax
272
 *
273
 * @since 1.0
274
 *
275
 * @param array $data Arguments passed
276
 *
277
 * @return void
278
 */
279
function give_trigger_payment_note_deletion( $data ) {
280
281
	if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_delete_payment_note_' . $data['note_id'] ) ) {
282
		return;
283
	}
284
285
	if ( ! current_user_can( 'edit_give_payments', $data['payment_id'] ) ) {
286
		wp_die( __( 'You do not have permission to edit this payment record', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
287
	}
288
289
	$edit_order_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&give-message=payment-note-deleted&id=' . absint( $data['payment_id'] ) );
290
291
	give_delete_payment_note( $data['note_id'], $data['payment_id'] );
292
293
	wp_redirect( $edit_order_url );
294
}
295
296
add_action( 'give_delete_payment_note', 'give_trigger_payment_note_deletion' );
297
298
/**
299
 * Delete a payment note deletion with ajax
300
 *
301
 * @since 1.0
302
 *
303
 * @param array $data Arguments passed
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
304
 *
305
 * @return void
306
 */
307
function give_ajax_delete_payment_note() {
308
309
	if ( ! current_user_can( 'edit_give_payments', $_POST['payment_id'] ) ) {
310
		wp_die( __( 'You do not have permission to edit this payment record', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
311
	}
312
313
	if ( give_delete_payment_note( $_POST['note_id'], $_POST['payment_id'] ) ) {
314
		die( '1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_ajax_delete_payment_note() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
315
	} else {
316
		die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_ajax_delete_payment_note() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
317
	}
318
319
}
320
321
add_action( 'wp_ajax_give_delete_payment_note', 'give_ajax_delete_payment_note' );