Test Failed
Push — issues/370 ( da4d1b...967cb5 )
by Ravinder
05:08
created

admin-actions.php ➔ __give_ajax_donor_manage_addresses()   F

Complexity

Conditions 15
Paths 416

Size

Total Lines 114
Code Lines 67

Duplication

Lines 30
Ratio 26.32 %

Importance

Changes 0
Metric Value
cc 15
eloc 67
nc 416
nop 0
dl 30
loc 114
rs 3.6499
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Admin 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";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal _give_hide_outdated_php_notices_shortly does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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
	// Add payment bulk notice.
131
	if (
132
		current_user_can( 'edit_give_payments' )
133
		&& isset( $_GET['action'] )
134
		&& ! empty( $_GET['action'] )
135
		&& isset( $_GET['payment'] )
136
		&& ! empty( $_GET['payment'] )
137
	) {
138
		$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...
139
140
		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...
141 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...
142
				Give()->notices->register_notice( array(
143
					'id'          => 'bulk_action_delete',
144
					'type'        => 'updated',
145
					'description' => sprintf(
146
						_n(
147
							'Successfully deleted one transaction.',
148
							'Successfully deleted %d transactions.',
149
							$payment_count,
150
							'give'
151
						),
152
						$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 20 spaces, but found 24.
Loading history...
153
					'show'        => true,
154
				) );
155
156
				break;
157
158 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...
159
				Give()->notices->register_notice( array(
160
					'id'          => 'bulk_action_resend_receipt',
161
					'type'        => 'updated',
162
					'description' => sprintf(
163
						_n(
164
							'Successfully sent email receipt to one recipient.',
165
							'Successfully sent email receipts to %d recipients.',
166
							$payment_count,
167
							'give'
168
						),
169
						$payment_count
170
					),
171
					'show'        => true,
172
				) );
173
				break;
174
		}
175
	}
176
177
	// Add give message notices.
178
	if ( ! empty( $_GET['give-message'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
179
		// Donation reports errors.
180
		if ( current_user_can( 'view_give_reports' ) ) {
181
			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...
182 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...
183
					Give()->notices->register_notice( array(
184
						'id'          => 'give-donation-deleted',
185
						'type'        => 'updated',
186
						'description' => __( 'The donation has been deleted.', 'give' ),
187
						'show'        => true,
188
					) );
189
					break;
190 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...
191
					Give()->notices->register_notice( array(
192
						'id'          => 'give-payment-sent',
193
						'type'        => 'updated',
194
						'description' => __( 'The donation receipt has been resent.', 'give' ),
195
						'show'        => true,
196
					) );
197
					break;
198 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...
199
					Give()->notices->register_notice( array(
200
						'id'          => 'give-refreshed-reports',
201
						'type'        => 'updated',
202
						'description' => __( 'The reports cache has been cleared.', 'give' ),
203
						'show'        => true,
204
					) );
205
					break;
206 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...
207
					Give()->notices->register_notice( array(
208
						'id'          => 'give-donation-note-deleted',
209
						'type'        => 'updated',
210
						'description' => __( 'The donation note has been deleted.', 'give' ),
211
						'show'        => true,
212
					) );
213
					break;
214
			}
215
		}
216
217
		// Give settings notices and errors.
218
		if ( current_user_can( 'manage_give_settings' ) ) {
219
			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...
220 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...
221
					Give()->notices->register_notice( array(
222
						'id'          => 'give-settings-imported',
223
						'type'        => 'updated',
224
						'description' => __( 'The settings have been imported.', 'give' ),
225
						'show'        => true,
226
					) );
227
					break;
228 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...
229
					Give()->notices->register_notice( array(
230
						'id'          => 'give-api-key-generated',
231
						'type'        => 'updated',
232
						'description' => __( 'API keys have been generated.', 'give' ),
233
						'show'        => true,
234
					) );
235
					break;
236 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...
237
					Give()->notices->register_notice( array(
238
						'id'          => 'give-api-key-exists',
239
						'type'        => 'updated',
240
						'description' => __( 'The specified user already has API keys.', 'give' ),
241
						'show'        => true,
242
					) );
243
					break;
244 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...
245
					Give()->notices->register_notice( array(
246
						'id'          => 'give-api-key-regenerated',
247
						'type'        => 'updated',
248
						'description' => __( 'API keys have been regenerated.', 'give' ),
249
						'show'        => true,
250
					) );
251
					break;
252 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...
253
					Give()->notices->register_notice( array(
254
						'id'          => 'give-api-key-revoked',
255
						'type'        => 'updated',
256
						'description' => __( 'API keys have been revoked.', 'give' ),
257
						'show'        => true,
258
					) );
259
					break;
260 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...
261
					Give()->notices->register_notice( array(
262
						'id'          => 'give-sent-test-email',
263
						'type'        => 'updated',
264
						'description' => __( 'The test email has been sent.', 'give' ),
265
						'show'        => true,
266
					) );
267
					break;
268 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...
269
					Give()->notices->register_notice( array(
270
						'id'          => 'give-matched-success-failure-page',
271
						'type'        => 'updated',
272
						'description' => __( 'You cannot set the success and failed pages to the same page', 'give' ),
273
						'show'        => true,
274
					) );
275
					break;
276
			}
277
		}
278
		// Payments errors.
279
		if ( current_user_can( 'edit_give_payments' ) ) {
280
			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...
281 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...
282
					Give()->notices->register_notice( array(
283
						'id'          => 'give-note-added',
284
						'type'        => 'updated',
285
						'description' => __( 'The donation note has been added.', 'give' ),
286
						'show'        => true,
287
					) );
288
					break;
289 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...
290
					Give()->notices->register_notice( array(
291
						'id'          => 'give-payment-updated',
292
						'type'        => 'updated',
293
						'description' => __( 'The donation has been updated.', 'give' ),
294
						'show'        => true,
295
					) );
296
					break;
297
			}
298
		}
299
300
		// Donor Notices.
301
		if ( current_user_can( 'edit_give_payments' ) ) {
302
			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...
303 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...
304
					Give()->notices->register_notice( array(
305
						'id'          => 'give-donor-deleted',
306
						'type'        => 'updated',
307
						'description' => __( 'The donor has been deleted.', 'give' ),
308
						'show'        => true,
309
					) );
310
					break;
311
312 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...
313
					Give()->notices->register_notice( array(
314
						'id'          => 'give-donor-email-added',
315
						'type'        => 'updated',
316
						'description' => __( 'Donor email added.', 'give' ),
317
						'show'        => true,
318
					) );
319
					break;
320
321 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...
322
					Give()->notices->register_notice( array(
323
						'id'          => 'give-donor-email-removed',
324
						'type'        => 'updated',
325
						'description' => __( 'Donor email removed.', 'give' ),
326
						'show'        => true,
327
					) );
328
					break;
329
330 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...
331
					Give()->notices->register_notice( array(
332
						'id'          => 'give-donor-email-remove-failed',
333
						'type'        => 'updated',
334
						'description' => __( 'Failed to remove donor email.', 'give' ),
335
						'show'        => true,
336
					) );
337
					break;
338
339 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...
340
					Give()->notices->register_notice( array(
341
						'id'          => 'give-donor-primary-email-updated',
342
						'type'        => 'updated',
343
						'description' => __( 'Primary email updated for donor.', 'give' ),
344
						'show'        => true,
345
					) );
346
					break;
347
348 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...
349
					Give()->notices->register_notice( array(
350
						'id'          => 'give-donor-primary-email-failed',
351
						'type'        => 'updated',
352
						'description' => __( 'Failed to set primary email.', 'give' ),
353
						'show'        => true,
354
					) );
355
					break;
356
357 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...
358
					Give()->notices->register_notice( array(
359
						'id'          => 'give-donor-reconnect-user',
360
						'type'        => 'updated',
361
						'description' => __( 'User has been successfully connected with Donor.', 'give' ),
362
						'show'        => true,
363
					) );
364
					break;
365
			}
366
		}
367
	}
368
}
369
370
add_action( 'admin_notices', '_give_register_admin_notices', - 1 );
371
372
373
/**
374
 * Display admin bar when active.
375
 *
376
 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
377
 *
378
 * @return bool
379
 */
380
function _give_show_test_mode_notice_in_admin_bar( $wp_admin_bar ) {
381
	$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...
382
		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...
383
		give_is_test_mode();
384
385
	if (
386
		! current_user_can( 'view_give_reports' ) ||
387
		! $is_test_mode
388
	) {
389
		return false;
390
	}
391
392
	// Add the main site admin menu item.
393
	$wp_admin_bar->add_menu( array(
394
		'id'     => 'give-test-notice',
395
		'href'   => admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways' ),
396
		'parent' => 'top-secondary',
397
		'title'  => __( 'Give Test Mode Active', 'give' ),
398
		'meta'   => array( 'class' => 'give-test-mode-active' ),
399
	) );
400
401
	return true;
402
}
403
404
add_action( 'admin_bar_menu', '_give_show_test_mode_notice_in_admin_bar', 1000, 1 );
405
406
/**
407
 * Add Link to Import page in from donation archive and donation single page
408
 *
409
 * @since 1.8.13
410
 */
411
function give_import_page_link_callback() {
412
	?>
413
	<a href="<?php echo esc_url( give_import_page_url() ); ?>"
414
	   class="page-import-action page-title-action"><?php esc_html_e( 'Import Donations', 'give' ); ?></a>
415
416
	<?php
417
	// Check if view donation single page only.
418
	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...
419
		?>
420
		<style type="text/css">
421
			.wrap #transaction-details-heading {
422
				display: inline-block;
423
			}
424
		</style>
425
		<?php
426
	}
427
}
428
429
add_action( 'give_payments_page_top', 'give_import_page_link_callback', 11 );
430
431
/**
432
 * Load donation import ajax callback
433
 * Fire when importing from CSV start
434
 *
435
 * @since  1.8.13
436
 *
437
 * @return json $json_data
438
 */
439
function give_donation_import_callback() {
440
	$import_setting = array();
441
	$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...
442
443
	parse_str( $fields );
444
445
	$import_setting['create_user'] = $create_user;
446
	$import_setting['mode']        = $mode;
447
	$import_setting['delimiter']   = $delimiter;
448
	$import_setting['csv']         = $csv;
449
	$import_setting['delete_csv']  = $delete_csv;
450
451
	// Parent key id.
452
	$main_key = maybe_unserialize( $main_key );
453
454
	$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...
455
	$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...
456
	$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...
457
	$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...
458
	$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...
459
	$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...
460
	$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...
461
	if ( empty( $delimiter ) ) {
462
		$delimiter = ',';
463
	}
464
465
	// processing done here.
466
	$raw_data = give_get_donation_data_from_csv( $csv, $start, $end, $delimiter );
467
	$raw_key  = maybe_unserialize( $mapto );
468
469
	//Prevent normal emails
470
	remove_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 );
471
	remove_action( 'give_insert_user', 'give_new_user_notification', 10 );
472
	remove_action( 'give_insert_payment', 'give_payment_save_page_data' );
473
474
	foreach ( $raw_data as $row_data ) {
475
		give_save_import_donation_to_db( $raw_key, $row_data, $main_key, $import_setting );
476
	}
477
478
	// Check if function exists or not.
479
	if ( function_exists( 'give_payment_save_page_data' ) ) {
480
		add_action( 'give_insert_payment', 'give_payment_save_page_data' );
481
	}
482
	add_action( 'give_insert_user', 'give_new_user_notification', 10, 2 );
483
	add_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 );
484
485
	if ( $next == false ) {
0 ignored issues
show
introduced by
Found "== false". Use Yoda Condition checks, you must
Loading history...
486
		$json_data = array(
487
			'success' => true,
488
			'message' => __( 'All donation uploaded successfully!', 'give' ),
489
		);
490
	} else {
491
		$index_start = $start;
492
		$index_end   = $end;
493
		$last        = false;
494
		$next        = true;
495
		if ( $next ) {
496
			$index_start = $index_start + $per_page;
497
			$index_end   = $per_page + ( $index_start - 1 );
498
		}
499
		if ( $index_end >= $total ) {
500
			$index_end = $total;
501
			$last      = true;
502
		}
503
		$json_data = array(
504
			'raw_data' => $raw_data,
505
			'raw_key'  => $raw_key,
506
			'next'     => $next,
507
			'start'    => $index_start,
508
			'end'      => $index_end,
509
			'last'     => $last,
510
		);
511
	}
512
513
	$url              = give_import_page_url( array(
514
		'step'          => '4',
515
		'importer-type' => 'import_donations',
516
		'csv'           => $csv,
517
		'total'         => $total,
518
		'delete_csv'    => $import_setting['delete_csv'],
519
		'success'       => ( isset( $json_data['success'] ) ? $json_data['success'] : '' ),
520
	) );
521
	$json_data['url'] = $url;
522
523
	$current ++;
524
	$json_data['current'] = $current;
525
526
	$percentage              = ( 100 / ( $total_ajax + 1 ) ) * $current;
527
	$json_data['percentage'] = $percentage;
528
529
	$json_data = apply_filters( 'give_import_ajax_responces', $json_data, $fields );
530
	wp_die( json_encode( $json_data ) );
531
}
532
533
add_action( 'wp_ajax_give_donation_import', 'give_donation_import_callback' );
534
535
536
/**
537
 * Initializes blank slate content if a list table is empty.
538
 *
539
 * @since 1.8.13
540
 */
541
function give_blank_slate() {
542
	$blank_slate = new Give_Blank_Slate();
543
	$blank_slate->init();
544
}
545
546
add_action( 'current_screen', 'give_blank_slate' );
547
548
/**
549
 * Validate Fields of User Profile
550
 *
551
 * @param object   $errors Object of WP Errors.
552
 * @param int|bool $update True or False.
553
 * @param object   $user   WP User Data.
554
 *
555
 * @since 2.0
556
 *
557
 * @return mixed
558
 */
559
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...
560
561
	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...
562
		return;
563
	}
564
565
	if( ! empty( $user->ID ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
566
		$donor = Give()->donors->get_donor_by( 'user_id', $user->ID );
567
568
		if( $donor ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
569
			// If Donor is attached with User, then validate first name.
570
			if ( empty( $_POST['first_name'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
571
				$errors->add(
572
					'empty_first_name',
573
					sprintf(
574
						'<strong>%1$s:</strong> %2$s',
575
						__( 'ERROR', 'give' ),
576
						__( 'Please enter your first name.', 'give' )
577
					)
578
				);
579
			}
580
		}
581
	}
582
583
}
584
585
add_action( 'user_profile_update_errors', 'give_validate_user_profile', 10, 3 );
586
587
/**
588
 * Show Donor Information on User Profile Page.
589
 *
590
 * @param object $user User Object.
591
 *
592
 * @since 2.0
593
 */
594
function give_donor_information_profile_fields( $user ) {
595
	$donor = Give()->donors->get_donor_by( 'user_id', $user->ID );
596
597
	// Display Donor Information, only if donor is attached with User.
598
	if( ! empty( $donor->user_id ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
599
		?>
600
		<table class="form-table">
601
			<tbody>
602
			<tr>
603
				<th scope="row"><?php _e( 'Donor', 'give' ); ?></th>
604
				<td>
605
					<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...
606
						<?php _e( 'View Donor Information', 'give' ); ?>
607
					</a>
608
				</td>
609
			</tr>
610
			</tbody>
611
		</table>
612
		<?php
613
	}
614
}
615
616
add_action( 'personal_options', 'give_donor_information_profile_fields' );
617
/**
618
 * Get Array of WP User Roles.
619
 *
620
 * @since 1.8.13
621
 *
622
 * @return array
623
 */
624
function give_get_user_roles() {
625
	$user_roles = array();
626
627
	// Loop through User Roles.
628
	foreach ( get_editable_roles() as $role_name => $role_info ):
629
		$user_roles[ $role_name ] = $role_info['name'];
630
	endforeach;
631
632
	return $user_roles;
633
}
634
635
636
/**
637
 * Ajax handle for donor address.
638
 *
639
 * @since 2.0
640
 *
641
 * @return string
642
 */
643
function __give_ajax_donor_manage_addresses() {
644
	// Bailout.
645
	if (
646
		empty( $_POST['form'] ) ||
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
647
		empty( $_POST['donorID'] )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
648
	) {
649
		wp_send_json_error( array( 'error' => 1 ) );
650
	}
651
652
	$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...
653
	$donorID               = absint( $post['donorID'] );
654
	$form_data             = give_clean( wp_parse_args( $post['form'] ) );
655
	$is_multi_address_type = ( 'billing' === $form_data['address-id'] || false !== strpos( $form_data['address-id'], '_' ) );
656
	$address_type          = false !== strpos( $form_data['address-id'], '_' ) ?
657
		array_shift( explode( '_', $form_data['address-id'] ) ) :
658
		$form_data['address-id'];
659
	$address_id            = false !== strpos( $form_data['address-id'], '_' ) ?
660
		array_pop( explode( '_', $form_data['address-id'] ) ) :
661
		$form_data['address-id'];
662
	$response_data         = array(
663
		'action' => $form_data['address-action'],
664
		'id'     => $form_data['address-id'],
665
	);
666
667
	// Security check.
668
	// check_ajax_referer( 'give-manage-donor-addresses' );
669
670
	$donor = new Give_Donor( $donorID );
671
672
	// Verify donor.
673
	if ( ! $donor->id ) {
674
		wp_send_json_error( array( 'error' => 3 ) );
675
	}
676
677
	// Unset all data except address.
678
	unset(
679
		$form_data['_wpnonce'],
680
		$form_data['_wp_http_referer'],
681
		$form_data['address-action'],
682
		$form_data['address-id']
683
	);
684
685
	// Process action.
686
	switch ( $response_data['action'] ) {
687
688
		case 'add':
689 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 654 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...
690
				wp_send_json_error( array(
691
						'error' => 4,
692
						'error_msg' => wp_sprintf(
693
							'<div class="notice notice-error"><p>%s</p></div>',
694
							__( 'Error: Address already exist.', 'give' )
695
						)
696
					)
697
				);
698
			}
699
700
			$address_index = $is_multi_address_type ?
701
				array_pop( array_keys( $donor->address[ $address_type ] ) ) :
702
				$address_type;
703
704
			$response_data['address_html'] = __give_get_format_address(
705
				end( $donor->address['billing'] ),
706
				array(
707
					'type'  => 'billing',
708
					'index' => $address_index,
709
				)
710
			);
711
712
			if( $is_multi_address_type ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
713
				$response_data['id'] = "{$response_data['id']}_{$address_index}";
714
			}
715
716
			break;
717
718
		case 'remove':
719 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...
720
				wp_send_json_error( array(
721
						'error' => 5,
722
						'error_msg' => wp_sprintf(
723
							'<div class="notice notice-error"><p>%s</p></div>',
724
							__( 'Error: could not able to delete address. please try after sometime.', 'give' )
725
						)
726
					)
727
				);
728
			}
729
730
			break;
731
732
		case 'update':
733 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 654 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...
734
				wp_send_json_error( array(
735
						'error' => 6,
736
						'error_msg' => wp_sprintf(
737
							'<div class="notice notice-error"><p>%s</p></div>',
738
							__( 'Error: could not able to update address. please try after sometime.', 'give' )
739
						)
740
					)
741
				);
742
			}
743
744
			$response_data['address_html'] = __give_get_format_address(
745
				$donor->address['billing'][$address_id],
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
746
				array(
747
					'type'  => 'billing',
748
					'index' => $address_id,
749
				)
750
			);
751
752
			break;
753
	}
754
755
	wp_send_json_success( $response_data );
756
}
757
758
add_action( 'wp_ajax_donor_manage_addresses', '__give_ajax_donor_manage_addresses' );