actions.php ➔ give_update_payment_details()   F
last analyzed

Complexity

Conditions 56
Paths > 20000

Size

Total Lines 382

Duplication

Lines 3
Ratio 0.79 %

Importance

Changes 0
Metric Value
cc 56
nc 4294967295
nop 1
dl 3
loc 382
rs 0
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
 * Admin Payment Actions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Payments
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license 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
 * @since  1.0
22
 * @access private
23
 *
24
 * @param array $data Donation data.
25
 *
26
 * @return      void
27
 */
28
function give_update_payment_details( $data ) {
29
30 View Code Duplication
	if ( ! current_user_can( 'edit_give_payments', $data['give_payment_id'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
		wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
32
	}
33
34
	check_admin_referer( 'give_update_payment_details_nonce' );
35
36
	// Retrieve the payment ID.
37
	$payment_id = absint( $data['give_payment_id'] );
38
39
	/* @var Give_Payment $payment */
40
	$payment = new Give_Payment( $payment_id );
41
42
	$status = $data['give-payment-status'];
43
	$date   = sanitize_text_field( $data['give-payment-date'] );
44
	$hour   = sanitize_text_field( $data['give-payment-time-hour'] );
45
46
	// Restrict to our high and low.
47
	if ( $hour > 23 ) {
48
		$hour = 23;
49
	} elseif ( $hour < 0 ) {
50
		$hour = 00;
51
	}
52
53
	$minute = sanitize_text_field( $data['give-payment-time-min'] );
54
55
	// Restrict to our high and low.
56
	if ( $minute > 59 ) {
57
		$minute = 59;
58
	} elseif ( $minute < 0 ) {
59
		$minute = 00;
60
	}
61
62
	$address = give_clean( $data['give-payment-address'][0] );
63
64
	$curr_total = $payment->total;
65
	$new_total  = give_maybe_sanitize_amount( ( ! empty( $data['give-payment-total'] ) ? $data['give-payment-total'] : 0 ) );
66
	$date       = date( 'Y-m-d', strtotime( $date ) ) . ' ' . $hour . ':' . $minute . ':00';
67
68
	$curr_donor_id = sanitize_text_field( $data['give-current-donor'] );
69
	$new_donor_id  = sanitize_text_field( $data['donor-id'] );
70
71
	/**
72
	 * Fires before updating edited donation.
73
	 *
74
	 * @since 1.0
75
	 * @since 1.8.9 Changes hook name give_update_edited_purchase -> give_update_edited_donation
76
	 *
77
	 * @param int $payment_id The ID of the payment.
78
	 */
79
	do_action( 'give_update_edited_donation', $payment_id );
80
81
	$payment->date = $date;
82
	$payment->anonymous = isset( $data['give_anonymous_donation'] ) ? absint( $data['give_anonymous_donation'] ) : 0;
83
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
84
85
	$updated       = $payment->save();
86
87
	if ( 0 === $updated ) {
88
		wp_die( __( 'Error Updating Donation.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) );
89
	}
90
91
	$donor_changed = false;
92
93
	if ( isset( $data['give-new-donor'] ) && $data['give-new-donor'] == '1' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
94
95
		$email      = ! empty( $data['give-new-donor-email'] ) ? sanitize_text_field( $data['give-new-donor-email'] ) : '';
96
		$first_name = ! empty( $data['give-new-donor-first-name'] ) ? sanitize_text_field( $data['give-new-donor-first-name'] ) : '';
97
		$last_name  = ! empty( $data['give-new-donor-last-name'] ) ? sanitize_text_field( $data['give-new-donor-last-name'] ) : '';
98
		$names      = strip_tags( wp_unslash( trim( "{$first_name} {$last_name}" ) ) );
99
100
		if ( empty( $email ) || empty( $first_name ) ) {
101
			wp_die( __( 'New Donor requires first name and email address.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) );
102
		}
103
104
		$donor = new Give_Donor( $email );
105
		if ( empty( $donor->id ) ) {
106
			$donor_data = array( 'name' => $names, 'email' => $email );
107
			$user_id       = email_exists( $email );
108
			if ( false !== $user_id ) {
109
				$donor_data['user_id'] = $user_id;
110
			}
111
112
			if ( ! $donor->create( $donor_data ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $donor->create($donor_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...
113
				// Failed to create the new donor, assume the previous donor.
114
				$donor_changed = false;
0 ignored issues
show
Unused Code introduced by
$donor_changed 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...
115
				$donor         = new Give_Donor( $curr_donor_id );
116
				give_set_error( 'give-payment-new-donor-fail', __( 'Error creating new donor.', 'give' ) );
117
			}
118
		}
119
120
		// Create and Update Donor First Name and Last Name in Meta Fields.
121
		$donor->update_meta( '_give_donor_first_name', $first_name );
122
		$donor->update_meta( '_give_donor_last_name', $last_name );
123
124
		$new_donor_id = $donor->id;
0 ignored issues
show
Unused Code introduced by
$new_donor_id 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...
125
126
		$previous_donor = new Give_Donor( $curr_donor_id );
127
128
		$donor_changed = true;
129
130
	} elseif ( $curr_donor_id !== $new_donor_id ) {
131
132
		$donor = new Give_Donor( $new_donor_id );
133
		$email    = $donor->email;
0 ignored issues
show
Unused Code introduced by
$email 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...
134
		$names    = $donor->name;
0 ignored issues
show
Unused Code introduced by
$names 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...
135
136
		$previous_donor = new Give_Donor( $curr_donor_id );
137
138
		$donor_changed = true;
139
140
	} else {
141
		$donor = new Give_Donor( $curr_donor_id );
142
		$email    = $donor->email;
0 ignored issues
show
Unused Code introduced by
$email 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...
143
		$names    = $donor->name;
0 ignored issues
show
Unused Code introduced by
$names 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...
144
	}
145
146
	if ( $donor_changed ) {
147
148
		// Setup first and last name from input values.
149
		$first_name = $donor->get_first_name();
150
		$last_name  = $donor->get_last_name();
151
152
		$payment->first_name = $first_name;
153
		$payment->last_name  = $last_name;
154
155
		// Remove the stats and payment from the previous donor and attach it to the new donor.
156
		$previous_donor->remove_payment( $payment_id, false );
157
		$donor->attach_payment( $payment_id, false );
158
159
		if ( 'publish' == $status ) {
160
161
			// Reduce previous user donation count and amount.
162
			$previous_donor->decrease_donation_count();
163
			$previous_donor->decrease_value( $curr_total );
164
165
			// If donation was completed adjust stats of new donors.
166
			$donor->increase_purchase_count();
167
			$donor->increase_value( $new_total );
168
		}
169
170
		$payment->customer_id = $donor->id;
171
	} else {
172
173
		if ( 'publish' === $status ) {
174
			// Update user donation stat.
175
			$donor->update_donation_value( $curr_total, $new_total );
176
		}
177
	}
178
179
	// Set new meta values.
180
	$payment->user_id    = $donor->user_id;
181
	$payment->email      = $donor->email;
182
	$payment->address    = $address;
0 ignored issues
show
Documentation Bug introduced by
It seems like $address can also be of type string. However, the property $address is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
183
	$payment->total      = $new_total;
0 ignored issues
show
Documentation Bug introduced by
It seems like $new_total can also be of type integer or string. However, the property $total is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
184
185
	// Check for payment notes.
186
	if ( ! empty( $data['give-payment-note'] ) ) {
187
188
		$note = wp_kses( $data['give-payment-note'], array() );
189
		give_insert_payment_note( $payment_id, $note );
190
191
	}
192
193
	// Set new status.
194
	$payment->status = $status;
195
196
	// Adjust total store earnings if the payment total has been changed.
197
	if ( $new_total !== $curr_total && 'publish' == $status ) {
198
199
		if ( $new_total > $curr_total ) {
200
			// Increase if our new total is higher.
201
			$difference = $new_total - $curr_total;
202
			give_increase_total_earnings( $difference );
203
204
			// Increase form earnings.
205
			give_increase_earnings( $payment->form_id, $difference, $payment->ID );
206
		} elseif ( $curr_total > $new_total ) {
207
			// Decrease if our new total is lower.
208
			$difference = $curr_total - $new_total;
209
			give_decrease_total_earnings( $difference );
210
211
			// Decrease form earnings.
212
			give_decrease_form_earnings( $payment->form_id, $difference, $payment->ID );
213
		}
214
	}
215
216
	$payment->save();
217
218
	// Get new give form ID.
219
	$new_form_id     = absint( $data['give-payment-form-select'] );
220
	$current_form_id = absint( $payment->get_meta( '_give_payment_form_id' ) );
221
222
	// We are adding payment transfer code in last to remove any conflict with above functionality.
223
	// For example: above code will automatically handle form stat (increase/decrease) when payment status changes.
224
	// Check if user want to transfer current payment to new give form id.
225
	if ( $new_form_id && $new_form_id != $current_form_id ) {
226
227
		// Get new give form title.
228
		$new_form_title = get_the_title( $new_form_id );
229
230
		// Update payment give form meta data.
231
		$payment->update_meta( '_give_payment_form_id', $new_form_id );
232
		$payment->update_meta( '_give_payment_form_title', $new_form_title );
233
234
		// Update price id payment metadata.
235
		if ( ! give_has_variable_prices( $new_form_id ) ) {
236
			$payment->update_meta( '_give_payment_price_id', '' );
237
		}
238
239
		// If donation was completed, adjust stats of forms.
240
		if ( 'publish' == $status ) {
241
242
			// Decrease sale of old give form. For other payment status.
243
			$current_form = new Give_Donate_Form( $current_form_id );
244
			$current_form->decrease_sales();
245
			$current_form->decrease_earnings( $curr_total, $payment->ID );
246
247
			// Increase sale of new give form.
248
			$new_form = new Give_Donate_Form( $new_form_id );
249
			$new_form->increase_sales();
250
			$new_form->increase_earnings( $new_total, $payment->ID );
251
		}
252
253
		// Re setup payment to update new meta value in object.
254
		$payment->update_payment_setup( $payment->ID );
255
256
		// Update form id in payment logs.
257
		Give()->async_process->data( array(
258
			'data' => array( $new_form_id, $payment_id ),
259
			'hook' => 'give_update_log_form_id',
260
		) )->dispatch();
261
	}
262
263
	// Update price id if current form is variable form.
264
	/* @var Give_Donate_Form $form */
265
	$form = new Give_Donate_Form( $payment->form_id );
266
267
	if ( isset( $data['give-variable-price'] ) && $form->has_variable_prices() ) {
268
269
		// Get payment meta data.
270
		$payment_meta = $payment->get_meta();
271
272
		$price_info = array();
273
		$price_id = '';
274
275
		// Get price info
276
		if( 0 <= $data['give-variable-price'] ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
277
			foreach ( $form->prices as $variable_price ) {
278
				if( $new_total === give_maybe_sanitize_amount( $variable_price['_give_amount'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
279
					$price_info = $variable_price;
280
					break;
281
				}
282
			}
283
		}
284
285
		// Set price id.
286
		if( ! empty( $price_info ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
287
			$price_id = $data['give-variable-price'];
288
289
			if( $data['give-variable-price'] !== $price_info['_give_id']['level_id'] ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
290
				// Set price id to amount match.
291
				$price_id = $price_info['_give_id']['level_id'];
292
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
293
294
		} elseif( $form->is_custom_price_mode() ){
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
295
			$price_id = 'custom';
296
		}
297
298
		// Update payment meta data.
299
		$payment_meta['price_id'] = $price_id;
300
301
		// Update payment give form meta data.
302
		$payment->update_meta( '_give_payment_price_id', $price_id );
303
		$payment->update_meta( '_give_payment_meta', $payment_meta );
304
305
		// Re setup payment to update new meta value in object.
306
		$payment->update_payment_setup( $payment->ID );
307
	}
308
309
	$comment_id                   = isset( $data['give_comment_id'] ) ? absint( $data['give_comment_id'] ) : 0;
310
	$has_anonymous_setting_field = give_is_anonymous_donation_field_enabled( $payment->form_id );
311
312
	if ( $has_anonymous_setting_field ) {
313
		give_update_meta( $payment->ID, '_give_anonymous_donation', $payment->anonymous );
314
315
		// Set donor as anonymous only if donor does not have any non anonymous donation.
316
		$donations = Give()->donors->get_column_by( 'payment_ids', 'id', $payment->donor_id );
317
		$donations = ! empty( $donations ) ? explode( ',', $donations ) : array();
318
		$update_anonymous_donor_meta = false;
319
320
		if( ! empty( $donations ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
321
			$non_anonymous_donations = new WP_Query( array(
322
				'post_type'   => 'give_payment',
323
				'post_status' => 'publish',
324
				'post__in'    => $donations,
325
				'fields'      => 'ids',
326
				'meta_query'  => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
327
					'relation' => 'AND',
328
					array(
329
						'key'   => '_give_anonymous_donation',
330
						'value' => 0,
331
					),
332
					array(
333
						'key'   => '_give_payment_form_id',
334
						'value' => $payment->form_id,
335
					),
336
				),
337
			) );
338
339
			$update_anonymous_donor_meta = ! ( 0 < $non_anonymous_donations->found_posts );
340
341
			if(
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
342
				0 === absint( $non_anonymous_donations->found_posts )
343
				&&  $payment->anonymous
344
			) {
345
				$update_anonymous_donor_meta = true;
346
			} elseif (
347
				1 === absint( $non_anonymous_donations->found_posts )
348
				&& ! $payment->anonymous
349
			) {
350
				$update_anonymous_donor_meta =  true;
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
351
			}
352
		}
353
354
		if ( $update_anonymous_donor_meta ) {
355
			Give()->donor_meta->update_meta( $payment->donor_id, "_give_anonymous_donor_form_{$payment->form_id}", $payment->anonymous );
356
		}
357
358
		// Update comment meta if admin is not updating comment.
359
		if( $comment_id ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
360
			update_comment_meta( $comment_id, '_give_anonymous_donation', $payment->anonymous );
361
		}
362
	}
363
364
	// Update comment.
365
	if ( give_is_donor_comment_field_enabled( $payment->form_id ) ) {
366
		// We are access comment directly from $_POST because comment formatting remove because of give_clean in give_post_actions.
367
		$data['give_comment'] = trim( $_POST['give_comment'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
368
369
		if ( empty( $data['give_comment'] ) ) {
370
			// Delete comment if empty
371
			Give_Comment::delete( $comment_id, $payment_id, 'payment' );
372
373
		} else {
374
			$comment_args = array(
375
				'comment_author_email' => $payment->email
376
			);
377
378
			if ( $comment_id ) {
379
				$comment_args['comment_ID'] = $comment_id;
380
			}
381
382
			give_insert_donor_donation_comment(
383
				$payment->ID,
384
				$payment->donor_id,
385
				$data['give_comment'],
386
				$comment_args
387
			);
388
		}
389
390
		$donor_has_comment = empty( $data['give_comment'] )
391
			? ( $latest_comment = give_get_donor_latest_comment( $payment->donor_id ) && empty( $latest_comment ) ? '0' : '1' )
392
			: '1';
393
394
		Give()->donor_meta->update_meta( $payment->donor_id, '_give_has_comment', $donor_has_comment );
395
	}
396
397
	/**
398
	 * Fires after updating edited donation.
399
	 *
400
	 * @since 1.0
401
	 * @since 1.8.9 Changes hook name give_updated_edited_purchase -> give_updated_edited_donation
402
	 *
403
	 * @param int $payment_id The ID of the payment.
404
	 */
405
	do_action( 'give_updated_edited_donation', $payment_id );
406
407
	wp_safe_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&give-messages[]=payment-updated&id=' . $payment_id ) );
408
	exit;
409
}
410
411
add_action( 'give_update_payment_details', 'give_update_payment_details' );
412
413
/**
414
 * Trigger a Donation Deletion.
415
 *
416
 * @since 1.0
417
 *
418
 * @param array $data Arguments passed.
419
 *
420
 * @return void
421
 */
422
function give_trigger_donation_delete( $data ) {
423
	if ( wp_verify_nonce( $data['_wpnonce'], 'give_donation_nonce' ) ) {
424
425
		$payment_id = absint( $data['purchase_id'] );
426
427
		if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
428
			wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
429
		}
430
431
		give_delete_donation( $payment_id );
432
		wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&give-messages[]=donation-deleted' ) );
433
		give_die();
434
	}
435
}
436
437
add_action( 'give_delete_payment', 'give_trigger_donation_delete' );
438
439
/**
440
 * AJAX Store Donation Note
441
 */
442
function give_ajax_store_payment_note() {
443
444
	$payment_id = absint( $_POST['payment_id'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
445
	$note       = wp_kses( $_POST['note'], array() );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
446
447
	if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
448
		wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
449
	}
450
451
	if ( empty( $payment_id ) ) {
452
		die( '-1' );
453
	}
454
455
	if ( empty( $note ) ) {
456
		die( '-1' );
457
	}
458
459
	$note_id = give_insert_payment_note( $payment_id, $note );
460
	die( give_get_payment_note_html( $note_id ) );
461
}
462
463
add_action( 'wp_ajax_give_insert_payment_note', 'give_ajax_store_payment_note' );
464
465
/**
466
 * Triggers a donation note deletion without ajax
467
 *
468
 * @since 1.0
469
 *
470
 * @param array $data Arguments passed
471
 *
472
 * @return void
473
 */
474
function give_trigger_payment_note_deletion( $data ) {
475
476
	if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_delete_payment_note_' . $data['note_id'] ) ) {
477
		return;
478
	}
479
480 View Code Duplication
	if ( ! current_user_can( 'edit_give_payments', $data['payment_id'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
481
		wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
482
	}
483
484
	$edit_order_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&give-messages[]=donation-note-deleted&id=' . absint( $data['payment_id'] ) );
485
486
	give_delete_payment_note( $data['note_id'], $data['payment_id'] );
487
488
	wp_redirect( $edit_order_url );
489
}
490
491
add_action( 'give_delete_payment_note', 'give_trigger_payment_note_deletion' );
492
493
/**
494
 * Delete a payment note deletion with ajax
495
 *
496
 * @since 1.0
497
 *
498
 * @return void
499
 */
500
function give_ajax_delete_payment_note() {
501
502 View Code Duplication
	if ( ! current_user_can( 'edit_give_payments', $_POST['payment_id'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
503
		wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
504
	}
505
506
	if ( give_delete_payment_note( $_POST['note_id'], $_POST['payment_id'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
507
		die( '1' );
508
	} else {
509
		die( '-1' );
510
	}
511
512
}
513
514
add_action( 'wp_ajax_give_delete_payment_note', 'give_ajax_delete_payment_note' );
515