admin-actions.php ➔ give_get_user_roles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
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, GiveWP
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
	$message_notices = give_get_admin_messages_key();
206
	if ( ! empty( $message_notices ) ) {
207
		foreach ( $message_notices as $message_notice ) {
208
			// Donation reports errors.
209
			if ( current_user_can( 'view_give_reports' ) ) {
210
				switch ( $message_notice ) {
211 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...
212
						Give()->notices->register_notice( array(
213
							'id'          => 'give-donation-deleted',
214
							'type'        => 'updated',
215
							'description' => __( 'The donation has been deleted.', 'give' ),
216
							'show'        => true,
217
						) );
218
						break;
219 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...
220
						Give()->notices->register_notice( array(
221
							'id'          => 'give-email-sent',
222
							'type'        => 'updated',
223
							'description' => __( 'The donation receipt has been resent.', 'give' ),
224
							'show'        => true,
225
						) );
226
						break;
227 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...
228
						Give()->notices->register_notice( array(
229
							'id'          => 'give-refreshed-reports',
230
							'type'        => 'updated',
231
							'description' => __( 'The reports cache has been cleared.', 'give' ),
232
							'show'        => true,
233
						) );
234
						break;
235 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...
236
						Give()->notices->register_notice( array(
237
							'id'          => 'give-donation-note-deleted',
238
							'type'        => 'updated',
239
							'description' => __( 'The donation note has been deleted.', 'give' ),
240
							'show'        => true,
241
						) );
242
						break;
243
				}// End switch().
244
			}// End if().
245
246
			// Give settings notices and errors.
247
			if ( current_user_can( 'manage_give_settings' ) ) {
248
				switch ( $message_notice ) {
249 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...
250
						Give()->notices->register_notice( array(
251
							'id'          => 'give-settings-imported',
252
							'type'        => 'updated',
253
							'description' => __( 'The settings have been imported.', 'give' ),
254
							'show'        => true,
255
						) );
256
						break;
257 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...
258
						Give()->notices->register_notice( array(
259
							'id'          => 'give-api-key-generated',
260
							'type'        => 'updated',
261
							'description' => __( 'API keys have been generated.', 'give' ),
262
							'show'        => true,
263
						) );
264
						break;
265 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...
266
						Give()->notices->register_notice( array(
267
							'id'          => 'give-api-key-exists',
268
							'type'        => 'updated',
269
							'description' => __( 'The specified user already has API keys.', 'give' ),
270
							'show'        => true,
271
						) );
272
						break;
273 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...
274
						Give()->notices->register_notice( array(
275
							'id'          => 'give-api-key-regenerated',
276
							'type'        => 'updated',
277
							'description' => __( 'API keys have been regenerated.', 'give' ),
278
							'show'        => true,
279
						) );
280
						break;
281 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...
282
						Give()->notices->register_notice( array(
283
							'id'          => 'give-api-key-revoked',
284
							'type'        => 'updated',
285
							'description' => __( 'API keys have been revoked.', 'give' ),
286
							'show'        => true,
287
						) );
288
						break;
289 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...
290
						Give()->notices->register_notice( array(
291
							'id'          => 'give-sent-test-email',
292
							'type'        => 'updated',
293
							'description' => __( 'The test email has been sent.', 'give' ),
294
							'show'        => true,
295
						) );
296
						break;
297 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...
298
						Give()->notices->register_notice( array(
299
							'id'          => 'give-matched-success-failure-page',
300
							'type'        => 'updated',
301
							'description' => __( 'You cannot set the success and failed pages to the same page', 'give' ),
302
							'show'        => true,
303
						) );
304
						break;
305
				}// End switch().
306
			}// End if().
307
308
			// Payments errors.
309
			if ( current_user_can( 'edit_give_payments' ) ) {
310
				switch ( $message_notice ) {
311 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...
312
						Give()->notices->register_notice( array(
313
							'id'          => 'give-note-added',
314
							'type'        => 'updated',
315
							'description' => __( 'The donation note has been added.', 'give' ),
316
							'show'        => true,
317
						) );
318
						break;
319 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...
320
						Give()->notices->register_notice( array(
321
							'id'          => 'give-payment-updated',
322
							'type'        => 'updated',
323
							'description' => __( 'The donation has been updated.', 'give' ),
324
							'show'        => true,
325
						) );
326
						break;
327
				}// End switch().
328
			}// End if().
329
330
			// Donor Notices.
331
			if ( current_user_can( 'edit_give_payments' ) ) {
332
				switch ( $message_notice ) {
333 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...
334
						Give()->notices->register_notice( array(
335
							'id'          => 'give-donor-deleted',
336
							'type'        => 'updated',
337
							'description' => __( 'The selected donor(s) has been deleted.', 'give' ),
338
							'show'        => true,
339
						) );
340
						break;
341
342 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...
343
						Give()->notices->register_notice( array(
344
							'id'          => 'give-donor-donations-deleted',
345
							'type'        => 'updated',
346
							'description' => __( 'The selected donor(s) and the associated donation(s) has been deleted.', 'give' ),
347
							'show'        => true,
348
						) );
349
						break;
350
351 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...
352
						Give()->notices->register_notice( array(
353
							'id'          => 'give-confirm-delete-donor',
354
							'type'        => 'updated',
355
							'description' => __( 'You must confirm to delete the selected donor(s).', 'give' ),
356
							'show'        => true,
357
						) );
358
						break;
359
360 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...
361
						Give()->notices->register_notice( array(
362
							'id'          => 'give-invalid-donor-id',
363
							'type'        => 'updated',
364
							'description' => __( 'Invalid Donor ID.', 'give' ),
365
							'show'        => true,
366
						) );
367
						break;
368
369
					case 'donor-delete-failed' :
370
						Give()->notices->register_notice( array(
371
							'id'          => 'give-donor-delete-failed',
372
							'type'        => 'error',
373
							'description' => __( 'Unable to delete selected donor(s).', 'give' ),
374
							'show'        => true,
375
						) );
376
						break;
377
378 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...
379
						Give()->notices->register_notice( array(
380
							'id'          => 'give-email-added',
381
							'type'        => 'updated',
382
							'description' => __( 'Donor email added.', 'give' ),
383
							'show'        => true,
384
						) );
385
						break;
386
387 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...
388
						Give()->notices->register_notice( array(
389
							'id'          => 'give-email-removed',
390
							'type'        => 'updated',
391
							'description' => __( 'Donor email removed.', 'give' ),
392
							'show'        => true,
393
						) );
394
						break;
395
396 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...
397
						Give()->notices->register_notice( array(
398
							'id'          => 'give-email-remove-failed',
399
							'type'        => 'updated',
400
							'description' => __( 'Failed to remove donor email.', 'give' ),
401
							'show'        => true,
402
						) );
403
						break;
404
405 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...
406
						Give()->notices->register_notice( array(
407
							'id'          => 'give-primary-email-updated',
408
							'type'        => 'updated',
409
							'description' => __( 'Primary email updated for donor.', 'give' ),
410
							'show'        => true,
411
						) );
412
						break;
413
414 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...
415
						Give()->notices->register_notice( array(
416
							'id'          => 'give-primary-email-failed',
417
							'type'        => 'updated',
418
							'description' => __( 'Failed to set primary email.', 'give' ),
419
							'show'        => true,
420
						) );
421
						break;
422
423 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...
424
						Give()->notices->register_notice( array(
425
							'id'          => 'give-reconnect-user',
426
							'type'        => 'updated',
427
							'description' => __( 'User has been successfully connected with Donor.', 'give' ),
428
							'show'        => true,
429
						) );
430
						break;
431
432 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...
433
						Give()->notices->register_notice( array(
434
							'id'          => 'give-profile-updated',
435
							'type'        => 'updated',
436
							'description' => __( 'Donor information updated successfully.', 'give' ),
437
							'show'        => true,
438
						) );
439
						break;
440
				}// End switch().
441
			}// End if().
442
		}
443
	}
444
}
445
446
add_action( 'admin_notices', '_give_register_admin_notices', - 1 );
447
448
449
/**
450
 * Display admin bar when active.
451
 *
452
 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
453
 *
454
 * @return bool
455
 */
456
function _give_show_test_mode_notice_in_admin_bar( $wp_admin_bar ) {
457
	$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...
458
		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...
459
		give_is_test_mode();
460
461
	if (
462
		! current_user_can( 'view_give_reports' ) ||
463
		! $is_test_mode
464
	) {
465
		return false;
466
	}
467
468
	// Add the main site admin menu item.
469
	$wp_admin_bar->add_menu( array(
470
		'id'     => 'give-test-notice',
471
		'href'   => admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways' ),
472
		'parent' => 'top-secondary',
473
		'title'  => __( 'Give Test Mode Active', 'give' ),
474
		'meta'   => array(
475
			'class' => 'give-test-mode-active',
476
		),
477
	) );
478
479
	return true;
480
}
481
482
add_action( 'admin_bar_menu', '_give_show_test_mode_notice_in_admin_bar', 1000, 1 );
483
484
/**
485
 * Outputs the Give admin bar CSS.
486
 */
487
function _give_test_mode_notice_admin_bar_css() {
488
	if ( ! give_is_test_mode() ) {
489
		return;
490
	}
491
	?>
492
	<style>
493
		#wpadminbar .give-test-mode-active > .ab-item {
494
			color: #fff;
495
			background-color: #ffba00;
496
		}
497
498
		#wpadminbar .give-test-mode-active:hover > .ab-item, #wpadminbar .give-test-mode-active:hover > .ab-item {
499
			background-color: rgba(203, 144, 0, 1) !important;
500
			color: #fff !important;
501
		}
502
	</style>
503
	<?php
504
}
505
506
add_action( 'admin_head', '_give_test_mode_notice_admin_bar_css' );
507
508
509
/**
510
 * Add Link to Import page in from donation archive and donation single page
511
 *
512
 * @since 1.8.13
513
 */
514
function give_import_page_link_callback() {
515
	?>
516
	<a href="<?php echo esc_url( give_import_page_url() ); ?>"
517
	   class="page-import-action page-title-action"><?php _e( 'Import Donations', 'give' ); ?></a>
518
519
	<?php
520
	// Check if view donation single page only.
521
	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...
522
		?>
523
		<style type="text/css">
524
			.wrap #transaction-details-heading {
525
				display: inline-block;
526
			}
527
		</style>
528
		<?php
529
	}
530
}
531
532
add_action( 'give_payments_page_top', 'give_import_page_link_callback', 11 );
533
534
/**
535
 * Load donation import ajax callback
536
 * Fire when importing from CSV start
537
 *
538
 * @since  1.8.13
539
 */
540
function give_donation_import_callback() {
541
	// Bailout.
542
	if ( ! current_user_can( 'manage_give_settings' ) ) {
543
		give_die();
544
	}
545
546
	// Disable Give cache
547
	Give_Cache::get_instance()->disable();
548
549
	$import_setting = array();
550
	$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...
551
552
	parse_str( $fields, $output );
553
554
	$import_setting['create_user'] = $output['create_user'];
555
	$import_setting['mode']        = $output['mode'];
556
	$import_setting['delimiter']   = $output['delimiter'];
557
	$import_setting['csv']         = $output['csv'];
558
	$import_setting['delete_csv']  = $output['delete_csv'];
559
	$import_setting['dry_run']     = $output['dry_run'];
560
561
	// Parent key id.
562
	$main_key = maybe_unserialize( $output['main_key'] );
563
564
	$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...
565
	$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...
566
	$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...
567
	$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...
568
	$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...
569
	$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...
570
	$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...
571
	if ( empty( $output['delimiter'] ) ) {
572
		$delimiter = ',';
573
	} else {
574
		$delimiter = $output['delimiter'];
575
	}
576
577
	// Processing done here.
578
	$raw_data                  = give_get_donation_data_from_csv( $output['csv'], $start, $end, $delimiter );
579
	$raw_key                   = maybe_unserialize( $output['mapto'] );
580
	$import_setting['raw_key'] = $raw_key;
581
582
	if ( ! empty( $output['dry_run'] ) ) {
583
		$import_setting['csv_raw_data'] = give_get_donation_data_from_csv( $output['csv'], 1, $end, $delimiter );
584
585
		$import_setting['donors_list'] = Give()->donors->get_donors( array(
586
			'number' => - 1,
587
			'fields' => array( 'id', 'user_id', 'email' ),
588
		) );
589
	}
590
591
	// Prevent normal emails.
592
	remove_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 );
593
	remove_action( 'give_insert_user', 'give_new_user_notification', 10 );
594
	remove_action( 'give_insert_payment', 'give_payment_save_page_data' );
595
596
	$current_key = $start;
597
	foreach ( $raw_data as $row_data ) {
598
		$import_setting['donation_key'] = $current_key;
599
		give_save_import_donation_to_db( $raw_key, $row_data, $main_key, $import_setting );
600
		$current_key ++;
601
	}
602
603
	// Check if function exists or not.
604
	if ( function_exists( 'give_payment_save_page_data' ) ) {
605
		add_action( 'give_insert_payment', 'give_payment_save_page_data' );
606
	}
607
	add_action( 'give_insert_user', 'give_new_user_notification', 10, 2 );
608
	add_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 );
609
610
	if ( $next == false ) {
0 ignored issues
show
introduced by
Found "== false". Use Yoda Condition checks, you must
Loading history...
611
		$json_data = array(
612
			'success' => true,
613
			'message' => __( 'All donation uploaded successfully!', 'give' ),
614
		);
615
	} else {
616
		$index_start = $start;
617
		$index_end   = $end;
618
		$last        = false;
619
		$next        = true;
620
		if ( $next ) {
621
			$index_start = $index_start + $per_page;
622
			$index_end   = $per_page + ( $index_start - 1 );
623
		}
624
		if ( $index_end >= $total ) {
625
			$index_end = $total;
626
			$last      = true;
627
		}
628
		$json_data = array(
629
			'raw_data' => $raw_data,
630
			'raw_key'  => $raw_key,
631
			'next'     => $next,
632
			'start'    => $index_start,
633
			'end'      => $index_end,
634
			'last'     => $last,
635
		);
636
	}
637
638
	$url              = give_import_page_url( array(
639
		'step'          => '4',
640
		'importer-type' => 'import_donations',
641
		'csv'           => $output['csv'],
642
		'total'         => $total,
643
		'delete_csv'    => $import_setting['delete_csv'],
644
		'success'       => ( isset( $json_data['success'] ) ? $json_data['success'] : '' ),
645
		'dry_run'       => $output['dry_run'],
646
	) );
647
	$json_data['url'] = $url;
648
649
	$current ++;
650
	$json_data['current'] = $current;
651
652
	$percentage              = ( 100 / ( $total_ajax + 1 ) ) * $current;
653
	$json_data['percentage'] = $percentage;
654
655
	// Enable Give cache
656
	Give_Cache::get_instance()->enable();
657
658
	$json_data = apply_filters( 'give_import_ajax_responces', $json_data, $fields );
659
	wp_die( json_encode( $json_data ) );
660
}
661
662
add_action( 'wp_ajax_give_donation_import', 'give_donation_import_callback' );
663
664
/**
665
 * Load core settings import ajax callback
666
 * Fire when importing from JSON start
667
 *
668
 * @since  1.8.17
669
 */
670
671
function give_core_settings_import_callback() {
672
	// Bailout.
673
	if ( ! current_user_can( 'manage_give_settings' ) ) {
674
		give_die();
675
	}
676
677
	$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...
678
	parse_str( $fields, $fields );
679
680
	$json_data['success'] = false;
681
682
	/**
683
	 * Filter to Modify fields that are being pass by the ajax before importing of the core setting start.
684
	 *
685
	 * @access public
686
	 *
687
	 * @since  1.8.17
688
	 *
689
	 * @param array $fields
690
	 *
691
	 * @return array $fields
692
	 */
693
	$fields = (array) apply_filters( 'give_import_core_settings_fields', $fields );
694
695
	$file_name = ( ! empty( $fields['file_name'] ) ? give_clean( $fields['file_name'] ) : false );
696
697
	if ( ! empty( $file_name ) ) {
698
		$type = ( ! empty( $fields['type'] ) ? give_clean( $fields['type'] ) : 'merge' );
699
700
		// Get the json data from the file and then alter it in array format
701
		$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 695 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...
702
		$json_to_array = json_decode( $json_string, true );
703
704
		// get the current setting from the options table.
705
		$host_give_options = Give_Cache_Setting::get_settings();
706
707
		// Save old settins for backup.
708
		update_option( 'give_settings_old', $host_give_options, false );
709
710
		/**
711
		 * Filter to Modify Core Settings that are being going to get import in options table as give settings.
712
		 *
713
		 * @access public
714
		 *
715
		 * @since  1.8.17
716
		 *
717
		 * @param array $json_to_array     Setting that are being going to get imported
718
		 * @param array $type              Type of Import
719
		 * @param array $host_give_options Setting old setting that used to be in the options table.
720
		 * @param array $fields            Data that is being send from the ajax
721
		 *
722
		 * @return array $json_to_array Setting that are being going to get imported
723
		 */
724
		$json_to_array = (array) apply_filters( 'give_import_core_settings_data', $json_to_array, $type, $host_give_options, $fields );
725
726
		update_option( 'give_settings', $json_to_array, false );
727
728
		$json_data['success'] = true;
729
	}
730
731
	$json_data['percentage'] = 100;
732
733
	/**
734
	 * Filter to Modify core import setting page url
735
	 *
736
	 * @access public
737
	 *
738
	 * @since  1.8.17
739
	 *
740
	 * @return array $url
741
	 */
742
	$json_data['url'] = give_import_page_url( (array) apply_filters( 'give_import_core_settings_success_url', array(
743
		'step'          => ( empty( $json_data['success'] ) ? '1' : '3' ),
744
		'importer-type' => 'import_core_setting',
745
		'success'       => ( empty( $json_data['success'] ) ? '0' : '1' ),
746
	) ) );
747
748
	wp_send_json( $json_data );
749
}
750
751
add_action( 'wp_ajax_give_core_settings_import', 'give_core_settings_import_callback' );
752
753
/**
754
 * Initializes blank slate content if a list table is empty.
755
 *
756
 * @since 1.8.13
757
 */
758
function give_blank_slate() {
759
	$blank_slate = new Give_Blank_Slate();
760
	$blank_slate->init();
761
}
762
763
add_action( 'current_screen', 'give_blank_slate' );
764
765
/**
766
 * Validate Fields of User Profile
767
 *
768
 * @param object   $errors Object of WP Errors.
769
 * @param int|bool $update True or False.
770
 * @param object   $user   WP User Data.
771
 *
772
 * @since 2.0
773
 *
774
 * @return mixed
775
 */
776
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...
777
778
	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...
779
		return;
780
	}
781
782
	if ( ! empty( $user->ID ) ) {
783
		$donor = Give()->donors->get_donor_by( 'user_id', $user->ID );
784
785
		if ( $donor ) {
786
			// If Donor is attached with User, then validate first name.
787
			if ( empty( $_POST['first_name'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
788
				$errors->add(
789
					'empty_first_name',
790
					sprintf(
791
						'<strong>%1$s:</strong> %2$s',
792
						__( 'ERROR', 'give' ),
793
						__( 'Please enter your first name.', 'give' )
794
					)
795
				);
796
			}
797
		}
798
	}
799
800
}
801
802
add_action( 'user_profile_update_errors', 'give_validate_user_profile', 10, 3 );
803
804
/**
805
 * Show Donor Information on User Profile Page.
806
 *
807
 * @param object $user User Object.
808
 *
809
 * @since 2.0
810
 */
811
function give_donor_information_profile_fields( $user ) {
812
	$donor = Give()->donors->get_donor_by( 'user_id', $user->ID );
813
814
	// Display Donor Information, only if donor is attached with User.
815
	if ( ! empty( $donor->user_id ) ) {
816
		?>
817
		<table class="form-table">
818
			<tbody>
819
			<tr>
820
				<th scope="row"><?php _e( 'Donor', 'give' ); ?></th>
821
				<td>
822
					<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...
823
						<?php _e( 'View Donor Information', 'give' ); ?>
824
					</a>
825
				</td>
826
			</tr>
827
			</tbody>
828
		</table>
829
		<?php
830
	}
831
}
832
833
add_action( 'personal_options', 'give_donor_information_profile_fields' );
834
/**
835
 * Get Array of WP User Roles.
836
 *
837
 * @since 1.8.13
838
 *
839
 * @return array
840
 */
841
function give_get_user_roles() {
842
	$user_roles = array();
843
844
	// Loop through User Roles.
845
	foreach ( get_editable_roles() as $role_name => $role_info ) :
846
		$user_roles[ $role_name ] = $role_info['name'];
847
	endforeach;
848
849
	return $user_roles;
850
}
851
852
853
/**
854
 * Ajax handle for donor address.
855
 *
856
 * @since 2.0
857
 *
858
 * @return string
859
 */
860
function __give_ajax_donor_manage_addresses() {
861
	// Bailout.
862
	if (
863
		empty( $_POST['form'] ) ||
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
864
		empty( $_POST['donorID'] )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
865
	) {
866
		wp_send_json_error( array(
867
			'error' => 1,
868
		) );
869
	}
870
871
	$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...
872
	$donorID               = absint( $post['donorID'] );
873
	$form_data             = give_clean( wp_parse_args( $post['form'] ) );
874
	$is_multi_address_type = ( 'billing' === $form_data['address-id'] || false !== strpos( $form_data['address-id'], '_' ) );
875
	$exploded_address_id   = explode( '_', $form_data['address-id'] );
876
	$address_type          = false !== strpos( $form_data['address-id'], '_' ) ?
877
		array_shift( $exploded_address_id ) :
878
		$form_data['address-id'];
879
	$address_id            = false !== strpos( $form_data['address-id'], '_' ) ?
880
		array_pop( $exploded_address_id ) :
881
		null;
882
	$response_data         = array(
883
		'action' => $form_data['address-action'],
884
		'id'     => $form_data['address-id'],
885
	);
886
887
	// Security check.
888 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...
889
		wp_send_json_error( array(
890
				'error'     => 1,
891
				'error_msg' => wp_sprintf(
892
					'<div class="notice notice-error"><p>%s</p></div>',
893
					__( 'Error: Security issue.', 'give' )
894
				),
895
			)
896
		);
897
	}
898
899
	$donor = new Give_Donor( $donorID );
900
901
	// Verify donor.
902
	if ( ! $donor->id ) {
903
		wp_send_json_error( array(
904
			'error' => 3,
905
		) );
906
	}
907
908
	// Unset all data except address.
909
	unset(
910
		$form_data['_wpnonce'],
911
		$form_data['address-action'],
912
		$form_data['address-id']
913
	);
914
915
	// Process action.
916
	switch ( $response_data['action'] ) {
917
918
		case 'add':
919 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 873 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...
920
				wp_send_json_error( array(
921
						'error'     => 1,
922
						'error_msg' => wp_sprintf(
923
							'<div class="notice notice-error"><p>%s</p></div>',
924
							__( 'Error: Unable to save the address. Please check if address already exist.', 'give' )
925
						),
926
					)
927
				);
928
			}
929
930
			$total_addresses = count( $donor->address[ $address_type ] );
931
932
			$address_index = $is_multi_address_type ?
933
				$total_addresses - 1 :
934
				$address_type;
935
936
			$array_keys = array_keys( $donor->address[ $address_type ] );
937
938
			$address_id = $is_multi_address_type ?
939
				end( $array_keys ) :
940
				$address_type;
941
942
			$response_data['address_html'] = __give_get_format_address(
943
				end( $donor->address['billing'] ),
944
				array(
945
					// We can add only billing address from donor screen.
946
					'type'  => 'billing',
947
					'id'    => $address_id,
948
					'index' => ++ $address_index,
949
				)
950
			);
951
			$response_data['success_msg']  = wp_sprintf(
952
				'<div class="notice updated"><p>%s</p></div>',
953
				__( 'Successfully added a new address to the donor.', 'give' )
954
			);
955
956
			if ( $is_multi_address_type ) {
957
				$response_data['id'] = "{$response_data['id']}_{$address_index}";
958
			}
959
960
			break;
961
962
		case 'remove':
963 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...
964
				wp_send_json_error( array(
965
						'error'     => 2,
966
						'error_msg' => wp_sprintf(
967
							'<div class="notice notice-error"><p>%s</p></div>',
968
							__( 'Error: Unable to delete address.', 'give' )
969
						),
970
					)
971
				);
972
			}
973
974
			$response_data['success_msg'] = wp_sprintf(
975
				'<div class="notice updated"><p>%s</p></div>',
976
				__( 'Successfully removed a address of donor.', 'give' )
977
			);
978
979
			break;
980
981
		case 'update':
982 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 873 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...
983
				wp_send_json_error( array(
984
						'error'     => 3,
985
						'error_msg' => wp_sprintf(
986
							'<div class="notice notice-error"><p>%s</p></div>',
987
							__( 'Error: Unable to update address. Please check if address already exist.', 'give' )
988
						),
989
					)
990
				);
991
			}
992
993
			$response_data['address_html'] = __give_get_format_address(
994
				$is_multi_address_type ?
995
					$donor->address[ $address_type ][ $address_id ] :
996
					$donor->address[ $address_type ],
997
				array(
998
					'type'  => $address_type,
999
					'id'    => $address_id,
1000
					'index' => $address_id,
1001
				)
1002
			);
1003
			$response_data['success_msg']  = wp_sprintf(
1004
				'<div class="notice updated"><p>%s</p></div>',
1005
				__( 'Successfully updated a address of donor', 'give' )
1006
			);
1007
1008
			break;
1009
	}// End switch().
1010
1011
	wp_send_json_success( $response_data );
1012
}
1013
1014
add_action( 'wp_ajax_donor_manage_addresses', '__give_ajax_donor_manage_addresses' );
1015
1016
/**
1017
 * Admin donor billing address label
1018
 *
1019
 * @since 2.0
1020
 *
1021
 * @param string $address_label
1022
 *
1023
 * @return string
1024
 */
1025
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...
1026
	$address_label = __( 'Billing Address', 'give' );
1027
1028
	return $address_label;
1029
}
1030
1031
add_action( 'give_donor_billing_address_label', '__give_donor_billing_address_label' );
1032
1033
/**
1034
 * Admin donor personal address label
1035
 *
1036
 * @since 2.0
1037
 *
1038
 * @param string $address_label
1039
 *
1040
 * @return string
1041
 */
1042
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...
1043
	$address_label = __( 'Personal Address', 'give' );
1044
1045
	return $address_label;
1046
}
1047
1048
add_action( 'give_donor_personal_address_label', '__give_donor_personal_address_label' );
1049
1050
/**
1051
 * Update Donor Information when User Profile is updated from admin.
1052
 * Note: for internal use only.
1053
 *
1054
 * @param int $user_id
1055
 *
1056
 * @access public
1057
 * @since  2.0
1058
 *
1059
 * @return bool
1060
 */
1061
function give_update_donor_name_on_user_update( $user_id = 0 ) {
1062
1063
	if ( current_user_can( 'edit_user', $user_id ) ) {
1064
1065
		$donor = new Give_Donor( $user_id, true );
1066
1067
		// Bailout, if donor doesn't exists.
1068
		if ( ! $donor ) {
1069
			return false;
1070
		}
1071
1072
		// Get User First name and Last name.
1073
		$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...
1074
		$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...
1075
		$full_name  = strip_tags( wp_unslash( trim( "{$first_name} {$last_name}" ) ) );
1076
1077
		// Assign User First name and Last name to Donor.
1078
		Give()->donors->update( $donor->id, array(
1079
			'name' => $full_name,
1080
		) );
1081
		Give()->donor_meta->update_meta( $donor->id, '_give_donor_first_name', $first_name );
1082
		Give()->donor_meta->update_meta( $donor->id, '_give_donor_last_name', $last_name );
1083
1084
	}
1085
}
1086
1087
add_action( 'edit_user_profile_update', 'give_update_donor_name_on_user_update', 10 );
1088
add_action( 'personal_options_update', 'give_update_donor_name_on_user_update', 10 );
1089
1090
1091
/**
1092
 * Updates the email address of a donor record when the email on a user is updated
1093
 * Note: for internal use only.
1094
 *
1095
 * @since  1.4.3
1096
 * @access public
1097
 *
1098
 * @param  int          $user_id       User ID.
1099
 * @param  WP_User|bool $old_user_data User data.
1100
 *
1101
 * @return bool
1102
 */
1103
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...
1104
1105
	$donor = new Give_Donor( $user_id, true );
1106
1107
	if ( ! $donor ) {
1108
		return false;
1109
	}
1110
1111
	$user = get_userdata( $user_id );
1112
1113
	if ( ! empty( $user ) && $user->user_email !== $donor->email ) {
1114
1115
		$success = Give()->donors->update( $donor->id, array(
1116
			'email' => $user->user_email,
1117
		) );
1118
1119
		if ( $success ) {
1120
			// Update some payment meta if we need to
1121
			$payments_array = explode( ',', $donor->payment_ids );
1122
1123
			if ( ! empty( $payments_array ) ) {
1124
1125
				foreach ( $payments_array as $payment_id ) {
1126
1127
					give_update_payment_meta( $payment_id, 'email', $user->user_email );
1128
1129
				}
1130
			}
1131
1132
			/**
1133
			 * Fires after updating donor email on user update.
1134
			 *
1135
			 * @since 1.4.3
1136
			 *
1137
			 * @param  WP_User    $user  WordPress User object.
1138
			 * @param  Give_Donor $donor Give donor object.
1139
			 */
1140
			do_action( 'give_update_donor_email_on_user_update', $user, $donor );
1141
1142
		}
1143
	}
1144
1145
}
1146
1147
add_action( 'profile_update', 'give_update_donor_email_on_user_update', 10, 2 );
1148
1149
1150
/**
1151
 * Flushes Give's cache.
1152
 */
1153
function give_cache_flush() {
1154
	$result = Give_Cache::flush_cache();
1155
1156
	if ( $result ) {
1157
		wp_send_json_success( array(
1158
			'message' => __( 'Cache flushed successfully.', 'give' ),
1159
		));
1160
	} else {
1161
		wp_send_json_error( array(
1162
			'message' => __( 'An error occured while flushing the cache.', 'give' ),
1163
		));
1164
	}
1165
}
1166
1167
add_action( 'wp_ajax_give_cache_flush', 'give_cache_flush', 10, 0 );
1168