Test Failed
Pull Request — master (#3647)
by
unknown
08:16
created

actions.php ➔ give_ajax_store_payment_note()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 0
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
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
	$is_anonymous_donation = give_is_anonymous_donation_field_enabled( $payment->form_id );
311
312
	if ( $is_anonymous_donation ) {
313
		give_update_meta( $payment->ID, '_give_anonymous_donation', $payment->anonymous );
314
		Give()->donor_meta->update_meta( $payment->donor_id, '_give_anonymous_donor', $payment->anonymous );
315
316
		// Update comment meta if admin is not updating comment.
317
		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...
318
			update_comment_meta( $comment_id, '_give_anonymous_donation', $payment->anonymous );
319
		}
320
	}
321
322
	// Update comment.
323
	if ( give_is_donor_comment_field_enabled( $payment->form_id ) ) {
324
		// We are access comment directly from $_POST because comment formatting remove because of give_clean in give_post_actions.
325
		$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...
326
327
		if ( empty( $data['give_comment'] ) ) {
328
			// Delete comment if empty
329
			Give_Comment::delete( $comment_id, $payment_id, 'payment' );
330
331
		} else {
332
333
			// Update/Insert comment.
334
			$is_update_comment_meta = ! $comment_id;
335
336
			$comment_args = array(
337
				'comment_author_email' => $payment->email
338
			);
339
340
			if ( $comment_id ) {
341
				$comment_args['comment_ID'] = $comment_id;
342
			}
343
344
			$comment_id = give_insert_donor_donation_comment(
345
				$payment->ID,
346
				$payment->donor_id,
347
				$data['give_comment'],
348
				$comment_args
349
			);
350
351
			if ( $is_update_comment_meta ) {
352
				update_comment_meta( $comment_id, '_give_anonymous_donation', $is_anonymous_donation );
353
			}
354
		}
355
356
		$donor_has_comment = empty( $data['give_comment'] )
357
			? ( $latest_comment = give_get_donor_latest_comment( $payment->donor_id ) && empty( $latest_comment ) ? '0' : '1' )
358
			: '1';
359
360
		Give()->donor_meta->update_meta( $payment->donor_id, '_give_has_comment', $donor_has_comment );
361
	}
362
363
	/**
364
	 * Fires after updating edited donation.
365
	 *
366
	 * @since 1.0
367
	 * @since 1.8.9 Changes hook name give_updated_edited_purchase -> give_updated_edited_donation
368
	 *
369
	 * @param int $payment_id The ID of the payment.
370
	 */
371
	do_action( 'give_updated_edited_donation', $payment_id );
372
373
	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 ) );
374
	exit;
375
}
376
377
add_action( 'give_update_payment_details', 'give_update_payment_details' );
378
379
/**
380
 * Trigger a Donation Deletion.
381
 *
382
 * @since 1.0
383
 *
384
 * @param array $data Arguments passed.
385
 *
386
 * @return void
387
 */
388
function give_trigger_donation_delete( $data ) {
389
	if ( wp_verify_nonce( $data['_wpnonce'], 'give_donation_nonce' ) ) {
390
391
		$payment_id = absint( $data['purchase_id'] );
392
393
		if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
394
			wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
395
		}
396
397
		give_delete_donation( $payment_id );
398
		wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&give-messages[]=donation-deleted' ) );
399
		give_die();
400
	}
401
}
402
403
add_action( 'give_delete_payment', 'give_trigger_donation_delete' );
404
405
/**
406
 * AJAX Store Donation Note
407
 */
408
function give_ajax_store_payment_note() {
409
	$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...
410
	$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...
411
	$note_type  = give_clean( $_POST['type'] );
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...
412
413
	if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
414
		wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
415
	}
416
417
	if ( empty( $payment_id ) ) {
418
		die( '-1' );
419
	}
420
421
	if ( empty( $note ) ) {
422
		die( '-1' );
423
	}
424
425
	$note_id = give_insert_payment_note( $payment_id, $note );
426
427
	if( $note_id && $note_type ) {
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...
428
		add_comment_meta( $note_id, 'note_type', $note_type, true );
429
430
		/**
431
		 * Fire the action
432
		 *
433
		 * @since 2.3.0
434
		 */
435
		do_action( 'give_donor-note_email_notification', $note_id, $payment_id );
436
	}
437
438
	die( give_get_payment_note_html( $note_id ) );
439
}
440
441
add_action( 'wp_ajax_give_insert_payment_note', 'give_ajax_store_payment_note' );
442
443
/**
444
 * Triggers a donation note deletion without ajax
445
 *
446
 * @since 1.0
447
 *
448
 * @param array $data Arguments passed
449
 *
450
 * @return void
451
 */
452
function give_trigger_payment_note_deletion( $data ) {
453
454
	if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_delete_payment_note_' . $data['note_id'] ) ) {
455
		return;
456
	}
457
458 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...
459
		wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
460
	}
461
462
	$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'] ) );
463
464
	give_delete_payment_note( $data['note_id'], $data['payment_id'] );
465
466
	wp_redirect( $edit_order_url );
467
}
468
469
add_action( 'give_delete_payment_note', 'give_trigger_payment_note_deletion' );
470
471
/**
472
 * Delete a payment note deletion with ajax
473
 *
474
 * @since 1.0
475
 *
476
 * @return void
477
 */
478
function give_ajax_delete_payment_note() {
479
480 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...
481
		wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
482
	}
483
484
	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...
485
		die( '1' );
486
	} else {
487
		die( '-1' );
488
	}
489
490
}
491
492
add_action( 'wp_ajax_give_delete_payment_note', 'give_ajax_delete_payment_note' );
493