Completed
Push — issue-2719 ( c98c0f )
by Ravinder
383:18 queued 375:02
created

admin-actions.php ➔ give_cache_flush()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Actions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Actions
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
 * Load wp editor by ajax.
19
 *
20
 * @since 1.8
21
 */
22
function give_load_wp_editor() {
23
	if ( ! isset( $_POST['wp_editor'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
24
		die();
25
	}
26
27
	$wp_editor                     = json_decode( base64_decode( $_POST['wp_editor'] ), true );
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-sanitized input variable: $_POST
Loading history...
28
	$wp_editor[2]['textarea_name'] = $_POST['textarea_name'];
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-sanitized input variable: $_POST
Loading history...
29
30
	wp_editor( $wp_editor[0], $_POST['wp_editor_id'], $wp_editor[2] );
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...
31
32
	die();
33
}
34
35
add_action( 'wp_ajax_give_load_wp_editor', 'give_load_wp_editor' );
36
37
38
/**
39
 * Redirect admin to clean url give admin pages.
40
 *
41
 * @since 1.8
42
 *
43
 * @return bool
44
 */
45
function give_redirect_to_clean_url_admin_pages() {
46
	// Give admin pages.
47
	$give_pages = array(
48
		'give-payment-history',
49
		'give-donors',
50
		'give-reports',
51
		'give-tools',
52
	);
53
54
	// Get current page.
55
	$current_page = isset( $_GET['page'] ) ? esc_attr( $_GET['page'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
56
57
	// Bailout.
58
	if (
59
		empty( $current_page )
60
		|| empty( $_GET['_wp_http_referer'] )
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
61
		|| ! in_array( $current_page, $give_pages )
62
	) {
63
		return false;
64
	}
65
66
	/**
67
	 * Verify current page request.
68
	 *
69
	 * @since 1.8
70
	 */
71
	$redirect = apply_filters( "give_validate_{$current_page}", true );
72
73
	if ( $redirect ) {
74
		// Redirect.
75
		wp_redirect(
76
			remove_query_arg(
77
				array( '_wp_http_referer', '_wpnonce' ),
78
				wp_unslash( $_SERVER['REQUEST_URI'] )
0 ignored issues
show
introduced by
Detected usage of a non-validated input variable: $_SERVER
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_SERVER
Loading history...
79
			)
80
		);
81
		exit;
82
	}
83
}
84
85
add_action( 'admin_init', 'give_redirect_to_clean_url_admin_pages' );
86
87
88
/**
89
 * Hide Outdated PHP Notice Shortly.
90
 *
91
 * This code is used with AJAX call to hide outdated PHP notice for a short period of time
92
 *
93
 * @since 1.8.9
94
 *
95
 * @return void
96
 */
97
function give_hide_outdated_php_notice() {
98
99
	if ( ! isset( $_POST['_give_hide_outdated_php_notices_shortly'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
100
		give_die();
101
	}
102
103
	// Transient key name.
104
	$transient_key = '_give_hide_outdated_php_notices_shortly';
105
106
	if ( Give_Cache::get( $transient_key, true ) ) {
107
		return;
108
	}
109
110
	// Hide notice for 24 hours.
111
	Give_Cache::set( $transient_key, true, DAY_IN_SECONDS, true );
112
113
	give_die();
114
115
}
116
117
add_action( 'wp_ajax_give_hide_outdated_php_notice', 'give_hide_outdated_php_notice' );
118
119
/**
120
 * Register admin notices.
121
 *
122
 * @since 1.8.9
123
 */
124
function _give_register_admin_notices() {
125
	// Bailout.
126
	if ( ! is_admin() ) {
127
		return;
128
	}
129
130
	// Bulk action notices.
131
	if (
132
		isset( $_GET['action'] ) &&
133
		! empty( $_GET['action'] )
134
	) {
135
136
		// Add payment bulk notice.
137
		if (
138
			current_user_can( 'edit_give_payments' ) &&
139
			isset( $_GET['payment'] ) &&
140
			! empty( $_GET['payment'] )
141
		) {
142
			$payment_count = isset( $_GET['payment'] ) ? count( $_GET['payment'] ) : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
143
144
			switch ( $_GET['action'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
145 View Code Duplication
				case 'delete':
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...
146
					Give()->notices->register_notice( array(
147
						'id'          => 'bulk_action_delete',
148
						'type'        => 'updated',
149
						'description' => sprintf(
150
							_n(
151
								'Successfully deleted one donation.',
152
								'Successfully deleted %d donations.',
153
								$payment_count,
154
								'give'
155
							),
156
							$payment_count ),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 24 spaces, but found 28.
Loading history...
157
						'show'        => true,
158
					) );
159
160
					break;
161
162 View Code Duplication
				case 'resend-receipt':
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...
163
					Give()->notices->register_notice( array(
164
						'id'          => 'bulk_action_resend_receipt',
165
						'type'        => 'updated',
166
						'description' => sprintf(
167
							_n(
168
								'Successfully sent email receipt to one recipient.',
169
								'Successfully sent email receipts to %d recipients.',
170
								$payment_count,
171
								'give'
172
							),
173
							$payment_count
174
						),
175
						'show'        => true,
176
					) );
177
					break;
178
179
				case 'set-status-publish' :
180
				case 'set-status-pending' :
181
				case 'set-status-processing' :
182
				case 'set-status-refunded' :
183
				case 'set-status-revoked' :
184
				case 'set-status-failed' :
185
				case 'set-status-cancelled' :
186
				case 'set-status-abandoned' :
187 View Code Duplication
				case 'set-status-preapproval' :
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...
188
					Give()->notices->register_notice( array(
189
						'id'          => 'bulk_action_status_change',
190
						'type'        => 'updated',
191
						'description' => _n(
192
							'Donation status updated successfully.',
193
							'Donation statuses updated successfully.',
194
							$payment_count,
195
							'give'
196
						),
197
						'show'        => true,
198
					) );
199
					break;
200
			}// End switch().
201
		}// End if().
202
	}// End if().
203
204
	// Add give message notices.
205
	if ( ! empty( $_GET['give-message'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
206
		// Donation reports errors.
207
		if ( current_user_can( 'view_give_reports' ) ) {
208
			switch ( $_GET['give-message'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
209 View Code Duplication
				case 'donation_deleted' :
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...
210
					Give()->notices->register_notice( array(
211
						'id'          => 'give-donation-deleted',
212
						'type'        => 'updated',
213
						'description' => __( 'The donation has been deleted.', 'give' ),
214
						'show'        => true,
215
					) );
216
					break;
217 View Code Duplication
				case 'email_sent' :
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...
218
					Give()->notices->register_notice( array(
219
						'id'          => 'give-payment-sent',
220
						'type'        => 'updated',
221
						'description' => __( 'The donation receipt has been resent.', 'give' ),
222
						'show'        => true,
223
					) );
224
					break;
225 View Code Duplication
				case 'refreshed-reports' :
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...
226
					Give()->notices->register_notice( array(
227
						'id'          => 'give-refreshed-reports',
228
						'type'        => 'updated',
229
						'description' => __( 'The reports cache has been cleared.', 'give' ),
230
						'show'        => true,
231
					) );
232
					break;
233 View Code Duplication
				case 'donation-note-deleted' :
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...
234
					Give()->notices->register_notice( array(
235
						'id'          => 'give-donation-note-deleted',
236
						'type'        => 'updated',
237
						'description' => __( 'The donation note has been deleted.', 'give' ),
238
						'show'        => true,
239
					) );
240
					break;
241
			}
242
		}// End if().
243
244
		// Give settings notices and errors.
245
		if ( current_user_can( 'manage_give_settings' ) ) {
246
			switch ( $_GET['give-message'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
247 View Code Duplication
				case 'settings-imported' :
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...
248
					Give()->notices->register_notice( array(
249
						'id'          => 'give-settings-imported',
250
						'type'        => 'updated',
251
						'description' => __( 'The settings have been imported.', 'give' ),
252
						'show'        => true,
253
					) );
254
					break;
255 View Code Duplication
				case 'api-key-generated' :
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...
256
					Give()->notices->register_notice( array(
257
						'id'          => 'give-api-key-generated',
258
						'type'        => 'updated',
259
						'description' => __( 'API keys have been generated.', 'give' ),
260
						'show'        => true,
261
					) );
262
					break;
263 View Code Duplication
				case 'api-key-exists' :
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...
264
					Give()->notices->register_notice( array(
265
						'id'          => 'give-api-key-exists',
266
						'type'        => 'updated',
267
						'description' => __( 'The specified user already has API keys.', 'give' ),
268
						'show'        => true,
269
					) );
270
					break;
271 View Code Duplication
				case 'api-key-regenerated' :
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...
272
					Give()->notices->register_notice( array(
273
						'id'          => 'give-api-key-regenerated',
274
						'type'        => 'updated',
275
						'description' => __( 'API keys have been regenerated.', 'give' ),
276
						'show'        => true,
277
					) );
278
					break;
279 View Code Duplication
				case 'api-key-revoked' :
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...
280
					Give()->notices->register_notice( array(
281
						'id'          => 'give-api-key-revoked',
282
						'type'        => 'updated',
283
						'description' => __( 'API keys have been revoked.', 'give' ),
284
						'show'        => true,
285
					) );
286
					break;
287 View Code Duplication
				case 'sent-test-email' :
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...
288
					Give()->notices->register_notice( array(
289
						'id'          => 'give-sent-test-email',
290
						'type'        => 'updated',
291
						'description' => __( 'The test email has been sent.', 'give' ),
292
						'show'        => true,
293
					) );
294
					break;
295 View Code Duplication
				case 'matched-success-failure-page':
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...
296
					Give()->notices->register_notice( array(
297
						'id'          => 'give-matched-success-failure-page',
298
						'type'        => 'updated',
299
						'description' => __( 'You cannot set the success and failed pages to the same page', 'give' ),
300
						'show'        => true,
301
					) );
302
					break;
303
			}// End switch().
304
		}// End if().
305
		// Payments errors.
306
		if ( current_user_can( 'edit_give_payments' ) ) {
307
			switch ( $_GET['give-message'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
308 View Code Duplication
				case 'note-added' :
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...
309
					Give()->notices->register_notice( array(
310
						'id'          => 'give-note-added',
311
						'type'        => 'updated',
312
						'description' => __( 'The donation note has been added.', 'give' ),
313
						'show'        => true,
314
					) );
315
					break;
316 View Code Duplication
				case 'payment-updated' :
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...
317
					Give()->notices->register_notice( array(
318
						'id'          => 'give-payment-updated',
319
						'type'        => 'updated',
320
						'description' => __( 'The donation has been updated.', 'give' ),
321
						'show'        => true,
322
					) );
323
					break;
324
			}
325
		}
326
327
		// Donor Notices.
328
		if ( current_user_can( 'edit_give_payments' ) ) {
329
			switch ( $_GET['give-message'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
330 View Code Duplication
				case 'donor-deleted' :
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...
331
					Give()->notices->register_notice( array(
332
						'id'          => 'give-donor-deleted',
333
						'type'        => 'updated',
334
						'description' => __( 'The selected donor(s) has been deleted.', 'give' ),
335
						'show'        => true,
336
					) );
337
					break;
338
339 View Code Duplication
				case 'donor-donations-deleted' :
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...
340
					Give()->notices->register_notice( array(
341
						'id'          => 'give-donor-donations-deleted',
342
						'type'        => 'updated',
343
						'description' => __( 'The selected donor(s) and its associated donations has been deleted.', 'give' ),
344
						'show'        => true,
345
					) );
346
					break;
347
348 View Code Duplication
				case 'confirm-delete-donor' :
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...
349
					Give()->notices->register_notice( array(
350
						'id'          => 'give-confirm-delete-donor',
351
						'type'        => 'updated',
352
						'description' => __( 'You must confirm to delete the selected donor(s).', 'give' ),
353
						'show'        => true,
354
					) );
355
					break;
356
357 View Code Duplication
				case 'invalid-donor-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...
358
					Give()->notices->register_notice( array(
359
						'id'          => 'give-invalid-donor-id',
360
						'type'        => 'updated',
361
						'description' => __( 'Invalid Donor ID.', 'give' ),
362
						'show'        => true,
363
					) );
364
					break;
365
366
				case 'donor-delete-failed' :
367
					Give()->notices->register_notice( array(
368
						'id'          => 'give-donor-delete-failed',
369
						'type'        => 'error',
370
						'description' => __( 'Unable to delete selected donor(s).', 'give' ),
371
						'show'        => true,
372
					) );
373
					break;
374
375 View Code Duplication
				case 'email-added' :
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...
376
					Give()->notices->register_notice( array(
377
						'id'          => 'give-donor-email-added',
378
						'type'        => 'updated',
379
						'description' => __( 'Donor email added.', 'give' ),
380
						'show'        => true,
381
					) );
382
					break;
383
384 View Code Duplication
				case 'email-removed' :
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...
385
					Give()->notices->register_notice( array(
386
						'id'          => 'give-donor-email-removed',
387
						'type'        => 'updated',
388
						'description' => __( 'Donor email removed.', 'give' ),
389
						'show'        => true,
390
					) );
391
					break;
392
393 View Code Duplication
				case 'email-remove-failed' :
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...
394
					Give()->notices->register_notice( array(
395
						'id'          => 'give-donor-email-remove-failed',
396
						'type'        => 'updated',
397
						'description' => __( 'Failed to remove donor email.', 'give' ),
398
						'show'        => true,
399
					) );
400
					break;
401
402 View Code Duplication
				case 'primary-email-updated' :
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...
403
					Give()->notices->register_notice( array(
404
						'id'          => 'give-donor-primary-email-updated',
405
						'type'        => 'updated',
406
						'description' => __( 'Primary email updated for donor.', 'give' ),
407
						'show'        => true,
408
					) );
409
					break;
410
411 View Code Duplication
				case 'primary-email-failed' :
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...
412
					Give()->notices->register_notice( array(
413
						'id'          => 'give-donor-primary-email-failed',
414
						'type'        => 'updated',
415
						'description' => __( 'Failed to set primary email.', 'give' ),
416
						'show'        => true,
417
					) );
418
					break;
419
420 View Code Duplication
				case 'reconnect-user' :
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...
421
					Give()->notices->register_notice( array(
422
						'id'          => 'give-donor-reconnect-user',
423
						'type'        => 'updated',
424
						'description' => __( 'User has been successfully connected with Donor.', 'give' ),
425
						'show'        => true,
426
					) );
427
					break;
428
429 View Code Duplication
				case 'profile-updated' :
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...
430
					Give()->notices->register_notice( array(
431
						'id'          => 'give-donor-profile-updated',
432
						'type'        => 'updated',
433
						'description' => __( 'Donor information updated successfully.', 'give' ),
434
						'show'        => true,
435
					) );
436
					break;
437
			}// End switch().
438
		}// End if().
439
	}// End if().
440
}
441
442
add_action( 'admin_notices', '_give_register_admin_notices', - 1 );
443
444
445
/**
446
 * Display admin bar when active.
447
 *
448
 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
449
 *
450
 * @return bool
451
 */
452
function _give_show_test_mode_notice_in_admin_bar( $wp_admin_bar ) {
453
	$is_test_mode = ! empty( $_POST['test_mode'] ) ?
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
454
		give_is_setting_enabled( $_POST['test_mode'] ) :
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-sanitized input variable: $_POST
Loading history...
455
		give_is_test_mode();
456
457
	if (
458
		! current_user_can( 'view_give_reports' ) ||
459
		! $is_test_mode
460
	) {
461
		return false;
462
	}
463
464
	// Add the main site admin menu item.
465
	$wp_admin_bar->add_menu( array(
466
		'id'     => 'give-test-notice',
467
		'href'   => admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways' ),
468
		'parent' => 'top-secondary',
469
		'title'  => __( 'Give Test Mode Active', 'give' ),
470
		'meta'   => array(
471
			'class' => 'give-test-mode-active',
472
		),
473
	) );
474
475
	return true;
476
}
477
478
add_action( 'admin_bar_menu', '_give_show_test_mode_notice_in_admin_bar', 1000, 1 );
479
480
/**
481
 * Outputs the Give admin bar CSS.
482
 */
483
function _give_test_mode_notice_admin_bar_css() {
484
	if ( ! give_is_test_mode() ) {
485
		return;
486
	}
487
	?>
488
	<style>
489
		#wpadminbar .give-test-mode-active > .ab-item {
490
			color: #fff;
491
			background-color: #ffba00;
492
		}
493
494
		#wpadminbar .give-test-mode-active:hover > .ab-item, #wpadminbar .give-test-mode-active:hover > .ab-item {
495
			background-color: rgba(203, 144, 0, 1) !important;
496
			color: #fff !important;
497
		}
498
	</style>
499
	<?php
500
}
501
502
add_action( 'admin_head', '_give_test_mode_notice_admin_bar_css' );
503
504
505
/**
506
 * Add Link to Import page in from donation archive and donation single page
507
 *
508
 * @since 1.8.13
509
 */
510
function give_import_page_link_callback() {
511
	?>
512
	<a href="<?php echo esc_url( give_import_page_url() ); ?>"
513
	   class="page-import-action page-title-action"><?php _e( 'Import Donations', 'give' ); ?></a>
514
515
	<?php
516
	// Check if view donation single page only.
517
	if ( ! empty( $_REQUEST['view'] ) && 'view-payment-details' === (string) give_clean( $_REQUEST['view'] ) && 'give-payment-history' === give_clean( $_REQUEST['page'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
518
		?>
519
		<style type="text/css">
520
			.wrap #transaction-details-heading {
521
				display: inline-block;
522
			}
523
		</style>
524
		<?php
525
	}
526
}
527
528
add_action( 'give_payments_page_top', 'give_import_page_link_callback', 11 );
529
530
/**
531
 * Load donation import ajax callback
532
 * Fire when importing from CSV start
533
 *
534
 * @since  1.8.13
535
 *
536
 * @return json $json_data
537
 */
538
function give_donation_import_callback() {
539
540
	// Disable Give cache
541
	Give_Cache::get_instance()->disable();
542
543
	$import_setting = array();
544
	$fields         = isset( $_POST['fields'] ) ? $_POST['fields'] : null;
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-sanitized input variable: $_POST
Loading history...
545
546
	parse_str( $fields );
547
548
	$import_setting['create_user'] = $create_user;
549
	$import_setting['mode']        = $mode;
550
	$import_setting['delimiter']   = $delimiter;
551
	$import_setting['csv']         = $csv;
552
	$import_setting['delete_csv']  = $delete_csv;
553
554
	// Parent key id.
555
	$main_key = maybe_unserialize( $main_key );
556
557
	$current    = absint( $_REQUEST['current'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
558
	$total_ajax = absint( $_REQUEST['total_ajax'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
559
	$start      = absint( $_REQUEST['start'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
560
	$end        = absint( $_REQUEST['end'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
561
	$next       = absint( $_REQUEST['next'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
562
	$total      = absint( $_REQUEST['total'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
563
	$per_page   = absint( $_REQUEST['per_page'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
564
	if ( empty( $delimiter ) ) {
565
		$delimiter = ',';
566
	}
567
568
	// Processing done here.
569
	$raw_data = give_get_donation_data_from_csv( $csv, $start, $end, $delimiter );
570
	$raw_key  = maybe_unserialize( $mapto );
571
572
	// Prevent normal emails.
573
	remove_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 );
574
	remove_action( 'give_insert_user', 'give_new_user_notification', 10 );
575
	remove_action( 'give_insert_payment', 'give_payment_save_page_data' );
576
577
	$current_key = $start;
578
	foreach ( $raw_data as $row_data ) {
579
		$import_setting['donation_key'] = $current_key;
580
		give_save_import_donation_to_db( $raw_key, $row_data, $main_key, $import_setting );
581
		$current_key ++;
582
	}
583
584
	// Check if function exists or not.
585
	if ( function_exists( 'give_payment_save_page_data' ) ) {
586
		add_action( 'give_insert_payment', 'give_payment_save_page_data' );
587
	}
588
	add_action( 'give_insert_user', 'give_new_user_notification', 10, 2 );
589
	add_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 );
590
591
	if ( $next == false ) {
0 ignored issues
show
introduced by
Found "== false". Use Yoda Condition checks, you must
Loading history...
592
		$json_data = array(
593
			'success' => true,
594
			'message' => __( 'All donation uploaded successfully!', 'give' ),
595
		);
596
	} else {
597
		$index_start = $start;
598
		$index_end   = $end;
599
		$last        = false;
600
		$next        = true;
601
		if ( $next ) {
602
			$index_start = $index_start + $per_page;
603
			$index_end   = $per_page + ( $index_start - 1 );
604
		}
605
		if ( $index_end >= $total ) {
606
			$index_end = $total;
607
			$last      = true;
608
		}
609
		$json_data = array(
610
			'raw_data' => $raw_data,
611
			'raw_key'  => $raw_key,
612
			'next'     => $next,
613
			'start'    => $index_start,
614
			'end'      => $index_end,
615
			'last'     => $last,
616
		);
617
	}
618
619
	$url              = give_import_page_url( array(
620
		'step'          => '4',
621
		'importer-type' => 'import_donations',
622
		'csv'           => $csv,
623
		'total'         => $total,
624
		'delete_csv'    => $import_setting['delete_csv'],
625
		'success'       => ( isset( $json_data['success'] ) ? $json_data['success'] : '' ),
626
	) );
627
	$json_data['url'] = $url;
628
629
	$current ++;
630
	$json_data['current'] = $current;
631
632
	$percentage              = ( 100 / ( $total_ajax + 1 ) ) * $current;
633
	$json_data['percentage'] = $percentage;
634
635
	// Enable Give cache
636
	Give_Cache::get_instance()->enable();
637
638
	$json_data = apply_filters( 'give_import_ajax_responces', $json_data, $fields );
639
	wp_die( json_encode( $json_data ) );
640
}
641
642
add_action( 'wp_ajax_give_donation_import', 'give_donation_import_callback' );
643
644
/**
645
 * Load core settings import ajax callback
646
 * Fire when importing from JSON start
647
 *
648
 * @since  1.8.17
649
 *
650
 * @return json $json_data
651
 */
652
653
function give_core_settings_import_callback() {
654
	$fields = isset( $_POST['fields'] ) ? $_POST['fields'] : null;
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-sanitized input variable: $_POST
Loading history...
655
	parse_str( $fields, $fields );
656
657
	$json_data['success'] = false;
658
659
	/**
660
	 * Filter to Modify fields that are being pass by the ajax before importing of the core setting start.
661
	 *
662
	 * @access public
663
	 *
664
	 * @since  1.8.17
665
	 *
666
	 * @param array $fields
667
	 *
668
	 * @return array $fields
669
	 */
670
	$fields = (array) apply_filters( 'give_import_core_settings_fields', $fields );
671
672
	$file_name = ( ! empty( $fields['file_name'] ) ? give_clean( $fields['file_name'] ) : false );
673
674
	if ( ! empty( $file_name ) ) {
675
		$type = ( ! empty( $fields['type'] ) ? give_clean( $fields['type'] ) : 'merge' );
676
677
		// Get the json data from the file and then alter it in array format
678
		$json_string   = give_get_core_settings_json( $file_name );
0 ignored issues
show
Bug introduced by
It seems like $file_name defined by !empty($fields['file_nam...s['file_name']) : false on line 672 can also be of type array; however, give_get_core_settings_json() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
679
		$json_to_array = json_decode( $json_string, true );
680
681
		// get the current settign from the options table.
682
		$host_give_options = get_option( 'give_settings', array() );
683
684
		// Save old settins for backup.
685
		update_option( 'give_settings_old', $host_give_options );
686
687
		/**
688
		 * Filter to Modify Core Settings that are being going to get import in options table as give settings.
689
		 *
690
		 * @access public
691
		 *
692
		 * @since  1.8.17
693
		 *
694
		 * @param array $json_to_array     Setting that are being going to get imported
695
		 * @param array $type              Type of Import
696
		 * @param array $host_give_options Setting old setting that used to be in the options table.
697
		 * @param array $fields            Data that is being send from the ajax
698
		 *
699
		 * @return array $json_to_array Setting that are being going to get imported
700
		 */
701
		$json_to_array = (array) apply_filters( 'give_import_core_settings_data', $json_to_array, $type, $host_give_options, $fields );
702
703
		update_option( 'give_settings', $json_to_array );
704
705
		$json_data['success'] = true;
706
	}
707
708
	$json_data['percentage'] = 100;
709
710
	/**
711
	 * Filter to Modify core import setting page url
712
	 *
713
	 * @access public
714
	 *
715
	 * @since  1.8.17
716
	 *
717
	 * @return array $url
718
	 */
719
	$json_data['url'] = give_import_page_url( (array) apply_filters( 'give_import_core_settings_success_url', array(
720
		'step'          => ( empty( $json_data['success'] ) ? '1' : '3' ),
721
		'importer-type' => 'import_core_setting',
722
		'success'       => ( empty( $json_data['success'] ) ? '0' : '1' ),
723
	) ) );
724
725
	wp_send_json( $json_data );
726
}
727
728
add_action( 'wp_ajax_give_core_settings_import', 'give_core_settings_import_callback' );
729
730
/**
731
 * Initializes blank slate content if a list table is empty.
732
 *
733
 * @since 1.8.13
734
 */
735
function give_blank_slate() {
736
	$blank_slate = new Give_Blank_Slate();
737
	$blank_slate->init();
738
}
739
740
add_action( 'current_screen', 'give_blank_slate' );
741
742
/**
743
 * Validate Fields of User Profile
744
 *
745
 * @param object   $errors Object of WP Errors.
746
 * @param int|bool $update True or False.
747
 * @param object   $user   WP User Data.
748
 *
749
 * @since 2.0
750
 *
751
 * @return mixed
752
 */
753
function give_validate_user_profile( $errors, $update, $user ) {
0 ignored issues
show
Unused Code introduced by
The parameter $update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
754
755
	if ( ! empty( $_POST['action'] ) && ( 'adduser' === $_POST['action'] || 'createuser' === $_POST['action'] ) ) {
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-sanitized input variable: $_POST
Loading history...
756
		return;
757
	}
758
759
	if ( ! empty( $user->ID ) ) {
760
		$donor = Give()->donors->get_donor_by( 'user_id', $user->ID );
761
762
		if ( $donor ) {
763
			// If Donor is attached with User, then validate first name.
764
			if ( empty( $_POST['first_name'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
765
				$errors->add(
766
					'empty_first_name',
767
					sprintf(
768
						'<strong>%1$s:</strong> %2$s',
769
						__( 'ERROR', 'give' ),
770
						__( 'Please enter your first name.', 'give' )
771
					)
772
				);
773
			}
774
		}
775
	}
776
777
}
778
779
add_action( 'user_profile_update_errors', 'give_validate_user_profile', 10, 3 );
780
781
/**
782
 * Show Donor Information on User Profile Page.
783
 *
784
 * @param object $user User Object.
785
 *
786
 * @since 2.0
787
 */
788
function give_donor_information_profile_fields( $user ) {
789
	$donor = Give()->donors->get_donor_by( 'user_id', $user->ID );
790
791
	// Display Donor Information, only if donor is attached with User.
792
	if ( ! empty( $donor->user_id ) ) {
793
		?>
794
		<table class="form-table">
795
			<tbody>
796
			<tr>
797
				<th scope="row"><?php _e( 'Donor', 'give' ); ?></th>
798
				<td>
799
					<a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ); ?>">
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'admin_url'
Loading history...
800
						<?php _e( 'View Donor Information', 'give' ); ?>
801
					</a>
802
				</td>
803
			</tr>
804
			</tbody>
805
		</table>
806
		<?php
807
	}
808
}
809
810
add_action( 'personal_options', 'give_donor_information_profile_fields' );
811
/**
812
 * Get Array of WP User Roles.
813
 *
814
 * @since 1.8.13
815
 *
816
 * @return array
817
 */
818
function give_get_user_roles() {
819
	$user_roles = array();
820
821
	// Loop through User Roles.
822
	foreach ( get_editable_roles() as $role_name => $role_info ) :
823
		$user_roles[ $role_name ] = $role_info['name'];
824
	endforeach;
825
826
	return $user_roles;
827
}
828
829
830
/**
831
 * Ajax handle for donor address.
832
 *
833
 * @since 2.0
834
 *
835
 * @return string
836
 */
837
function __give_ajax_donor_manage_addresses() {
838
	// Bailout.
839
	if (
840
		empty( $_POST['form'] ) ||
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
841
		empty( $_POST['donorID'] )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
842
	) {
843
		wp_send_json_error( array(
844
			'error' => 1,
845
		) );
846
	}
847
848
	$post                  = give_clean( wp_parse_args( $_POST ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
849
	$donorID               = absint( $post['donorID'] );
850
	$form_data             = give_clean( wp_parse_args( $post['form'] ) );
851
	$is_multi_address_type = ( 'billing' === $form_data['address-id'] || false !== strpos( $form_data['address-id'], '_' ) );
852
	$address_type          = false !== strpos( $form_data['address-id'], '_' ) ?
853
		array_shift( explode( '_', $form_data['address-id'] ) ) :
854
		$form_data['address-id'];
855
	$address_id            = false !== strpos( $form_data['address-id'], '_' ) ?
856
		array_pop( explode( '_', $form_data['address-id'] ) ) :
857
		null;
858
	$response_data         = array(
859
		'action' => $form_data['address-action'],
860
		'id'     => $form_data['address-id'],
861
	);
862
863
	// Security check.
864 View Code Duplication
	if ( ! wp_verify_nonce( $form_data['_wpnonce'], 'give-manage-donor-addresses' ) ) {
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...
865
		wp_send_json_error( array(
866
				'error'     => 1,
867
				'error_msg' => wp_sprintf(
868
					'<div class="notice notice-error"><p>%s</p></div>',
869
					__( 'Error: Security issue.', 'give' )
870
				),
871
			)
872
		);
873
	}
874
875
	$donor = new Give_Donor( $donorID );
876
877
	// Verify donor.
878
	if ( ! $donor->id ) {
879
		wp_send_json_error( array(
880
			'error' => 3,
881
		) );
882
	}
883
884
	// Unset all data except address.
885
	unset(
886
		$form_data['_wpnonce'],
887
		$form_data['address-action'],
888
		$form_data['address-id']
889
	);
890
891
	// Process action.
892
	switch ( $response_data['action'] ) {
893
894
		case 'add':
895 View Code Duplication
			if ( ! $donor->add_address( "{$address_type}[]", $form_data ) ) {
0 ignored issues
show
Bug introduced by
It seems like $form_data defined by give_clean(wp_parse_args($post['form'])) on line 850 can also be of type string; however, Give_Donor::add_address() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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...
896
				wp_send_json_error( array(
897
						'error'     => 1,
898
						'error_msg' => wp_sprintf(
899
							'<div class="notice notice-error"><p>%s</p></div>',
900
							__( 'Error: Unable to save the address. Please check if address already exist.', 'give' )
901
						),
902
					)
903
				);
904
			}
905
906
			$total_addresses = count( $donor->address[ $address_type ] );
907
908
			$address_index = $is_multi_address_type ?
909
				$total_addresses - 1 :
910
				$address_type;
911
912
			$address_id = $is_multi_address_type ?
913
				end( array_keys( $donor->address[ $address_type ] ) ) :
914
				$address_type;
915
916
			$response_data['address_html'] = __give_get_format_address(
917
				end( $donor->address['billing'] ),
918
				array(
919
					// We can add only billing address from donor screen.
920
					'type'  => 'billing',
921
					'id'    => $address_id,
922
					'index' => ++ $address_index,
923
				)
924
			);
925
			$response_data['success_msg']  = wp_sprintf(
926
				'<div class="notice updated"><p>%s</p></div>',
927
				__( 'Successfully added a new address to the donor.', 'give' )
928
			);
929
930
			if ( $is_multi_address_type ) {
931
				$response_data['id'] = "{$response_data['id']}_{$address_index}";
932
			}
933
934
			break;
935
936
		case 'remove':
937 View Code Duplication
			if ( ! $donor->remove_address( $response_data['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...
938
				wp_send_json_error( array(
939
						'error'     => 2,
940
						'error_msg' => wp_sprintf(
941
							'<div class="notice notice-error"><p>%s</p></div>',
942
							__( 'Error: Unable to delete address.', 'give' )
943
						),
944
					)
945
				);
946
			}
947
948
			$response_data['success_msg'] = wp_sprintf(
949
				'<div class="notice updated"><p>%s</p></div>',
950
				__( 'Successfully removed a address of donor.', 'give' )
951
			);
952
953
			break;
954
955
		case 'update':
956 View Code Duplication
			if ( ! $donor->update_address( $response_data['id'], $form_data ) ) {
0 ignored issues
show
Bug introduced by
It seems like $form_data defined by give_clean(wp_parse_args($post['form'])) on line 850 can also be of type string; however, Give_Donor::update_address() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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...
957
				wp_send_json_error( array(
958
						'error'     => 3,
959
						'error_msg' => wp_sprintf(
960
							'<div class="notice notice-error"><p>%s</p></div>',
961
							__( 'Error: Unable to update address. Please check if address already exist.', 'give' )
962
						),
963
					)
964
				);
965
			}
966
967
			$response_data['address_html'] = __give_get_format_address(
968
				$is_multi_address_type ?
969
					$donor->address[ $address_type ][ $address_id ] :
970
					$donor->address[ $address_type ],
971
				array(
972
					'type'  => $address_type,
973
					'id'    => $address_id,
974
					'index' => $address_id,
975
				)
976
			);
977
			$response_data['success_msg']  = wp_sprintf(
978
				'<div class="notice updated"><p>%s</p></div>',
979
				__( 'Successfully updated a address of donor', 'give' )
980
			);
981
982
			break;
983
	}// End switch().
984
985
	wp_send_json_success( $response_data );
986
}
987
988
add_action( 'wp_ajax_donor_manage_addresses', '__give_ajax_donor_manage_addresses' );
989
990
/**
991
 * Admin donor billing address label
992
 *
993
 * @since 2.0
994
 *
995
 * @param string $address_label
996
 *
997
 * @return string
998
 */
999
function __give_donor_billing_address_label( $address_label ) {
0 ignored issues
show
Unused Code introduced by
The parameter $address_label is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1000
	$address_label = __( 'Billing Address', 'give' );
1001
1002
	return $address_label;
1003
}
1004
1005
add_action( 'give_donor_billing_address_label', '__give_donor_billing_address_label' );
1006
1007
/**
1008
 * Admin donor personal address label
1009
 *
1010
 * @since 2.0
1011
 *
1012
 * @param string $address_label
1013
 *
1014
 * @return string
1015
 */
1016
function __give_donor_personal_address_label( $address_label ) {
0 ignored issues
show
Unused Code introduced by
The parameter $address_label is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1017
	$address_label = __( 'Personal Address', 'give' );
1018
1019
	return $address_label;
1020
}
1021
1022
add_action( 'give_donor_personal_address_label', '__give_donor_personal_address_label' );
1023
1024
/**
1025
 * Update Donor Information when User Profile is updated from admin.
1026
 * Note: for internal use only.
1027
 *
1028
 * @param int $user_id
1029
 *
1030
 * @access public
1031
 * @since  2.0
1032
 *
1033
 * @return bool
1034
 */
1035
function give_update_donor_name_on_user_update( $user_id = 0 ) {
1036
1037
	if ( current_user_can( 'edit_user', $user_id ) ) {
1038
1039
		$donor = new Give_Donor( $user_id, true );
1040
1041
		// Bailout, if donor doesn't exists.
1042
		if ( ! $donor ) {
1043
			return false;
1044
		}
1045
1046
		// Get User First name and Last name.
1047
		$first_name = ( $_POST['first_name'] ) ? give_clean( $_POST['first_name'] ) : get_user_meta( $user_id, 'first_name', true );
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...
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1048
		$last_name  = ( $_POST['last_name'] ) ? give_clean( $_POST['last_name'] ) : get_user_meta( $user_id, 'last_name', true );
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...
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1049
		$full_name  = strip_tags( wp_unslash( trim( "{$first_name} {$last_name}" ) ) );
1050
1051
		// Assign User First name and Last name to Donor.
1052
		Give()->donors->update( $donor->id, array(
1053
			'name' => $full_name,
1054
		) );
1055
		Give()->donor_meta->update_meta( $donor->id, '_give_donor_first_name', $first_name );
1056
		Give()->donor_meta->update_meta( $donor->id, '_give_donor_last_name', $last_name );
1057
1058
	}
1059
}
1060
1061
add_action( 'edit_user_profile_update', 'give_update_donor_name_on_user_update', 10 );
1062
add_action( 'personal_options_update', 'give_update_donor_name_on_user_update', 10 );
1063
1064
1065
/**
1066
 * Updates the email address of a donor record when the email on a user is updated
1067
 * Note: for internal use only.
1068
 *
1069
 * @since  1.4.3
1070
 * @access public
1071
 *
1072
 * @param  int          $user_id       User ID.
1073
 * @param  WP_User|bool $old_user_data User data.
1074
 *
1075
 * @return bool
1076
 */
1077
function give_update_donor_email_on_user_update( $user_id = 0, $old_user_data = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $old_user_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1078
1079
	$donor = new Give_Donor( $user_id, true );
1080
1081
	if ( ! $donor ) {
1082
		return false;
1083
	}
1084
1085
	$user = get_userdata( $user_id );
1086
1087
	if ( ! empty( $user ) && $user->user_email !== $donor->email ) {
1088
1089
		$success = Give()->donors->update( $donor->id, array(
1090
			'email' => $user->user_email,
1091
		) );
1092
1093
		if ( $success ) {
1094
			// Update some payment meta if we need to
1095
			$payments_array = explode( ',', $donor->payment_ids );
1096
1097
			if ( ! empty( $payments_array ) ) {
1098
1099
				foreach ( $payments_array as $payment_id ) {
1100
1101
					give_update_payment_meta( $payment_id, 'email', $user->user_email );
1102
1103
				}
1104
			}
1105
1106
			/**
1107
			 * Fires after updating donor email on user update.
1108
			 *
1109
			 * @since 1.4.3
1110
			 *
1111
			 * @param  WP_User    $user  WordPress User object.
1112
			 * @param  Give_Donor $donor Give donor object.
1113
			 */
1114
			do_action( 'give_update_donor_email_on_user_update', $user, $donor );
1115
1116
		}
1117
	}
1118
1119
}
1120
1121
add_action( 'profile_update', 'give_update_donor_email_on_user_update', 10, 2 );
1122
1123
1124
/**
1125
 * Flushes Give's cache.
1126
 */
1127
function give_cache_flush() {
1128
	$result = Give_Cache::flush_cache();
1129
1130
	if ( $result ) {
1131
		wp_send_json_success( array(
1132
			'message' => __( 'Cache flushed successfully.', 'give' ),
1133
		));
1134
	} else {
1135
		wp_send_json_error( array(
1136
			'message' => __( 'An error occured while flushing the cache.', 'give' ),
1137
		));
1138
	}
1139
}
1140
1141
add_action( 'wp_ajax_give_cache_flush', 'give_cache_flush', 10, 0 );
1142