Test Failed
Push — issues/2397 ( f367c1...92dbfa )
by Ravinder
04:29
created

shortcodes.php ➔ give_donation_history()   C

Complexity

Conditions 9
Paths 5

Size

Total Lines 67
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 37
nc 5
nop 1
dl 0
loc 67
rs 6.3448
c 0
b 0
f 0

How to fix   Long Method   

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
 * Give Shortcodes
4
 *
5
 * @package     Give
6
 * @subpackage  Shortcodes
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
 * Donation History Shortcode
19
 *
20
 * Displays a user's donation history.
21
 *
22
 * @since  1.0
23
 *
24
 * @return string|bool
25
 */
26
function give_donation_history( $atts ) {
27
28
	$donation_history_args = shortcode_atts( array(
29
		'id'             => true,
30
		'date'           => true,
31
		'donor'          => false,
32
		'amount'         => true,
33
		'status'         => false,
34
		'payment_method' => false,
35
	), $atts, 'donation_history' );
36
37
	// Always show receipt link.
38
	$donation_history_args['details'] = true;
39
40
	// Set Donation History Shortcode Arguments in session variable.
41
	Give()->session->set( 'give_donation_history_args', $donation_history_args );
42
43
	// If payment_key query arg exists, return receipt instead of donation history.
44
	if ( isset( $_GET['payment_key'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
45
		ob_start();
46
		echo give_receipt_shortcode( array() );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_receipt_shortcode'
Loading history...
47
48
		// Display donation history link only if it is not accessed via Receipt Access Link.
49
		if ( ! give_get_receipt_session() ) {
50
			echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
51
				'<a href="%s">%s</a>',
52
				esc_url( give_get_history_page_uri() ),
53
				__( '&laquo; Return to All Donations', 'give' )
54
			);
55
		}
56
		return ob_get_clean();
57
	}
58
59
	$email_access = give_get_option( 'email_access' );
60
61
	/**
62
	 * Determine access
63
	 *
64
	 * a. Check if a user is logged in or does a session exists
65
	 * b. Does an email-access token exist?
66
	 */
67
	if (
68
		is_user_logged_in() ||
69
		false !== Give()->session->get_session_expiration() ||
70
		( give_is_setting_enabled( $email_access ) && Give()->email_access->token_exists ) ||
71
		true === give_get_history_session()
72
	) {
73
		ob_start();
74
		give_get_template_part( 'history', 'donations' );
75
76
		return ob_get_clean();
77
78
	} elseif ( give_is_setting_enabled( $email_access ) ) {
79
		// Is Email-based access enabled?
80
		ob_start();
81
		give_get_template_part( 'email', 'login-form' );
82
83
		return ob_get_clean();
84
85
	} else {
86
87
		$output = apply_filters( 'give_donation_history_nonuser_message', Give()->notices->print_frontend_notice( __( 'You must be logged in to view your donation history. Please login using your account or create an account using the same email you used to donate with.', 'give' ), false ) );
88
		$output .= do_shortcode( '[give_login]' );
89
90
		return $output;
91
	}
92
}
93
94
add_shortcode( 'donation_history', 'give_donation_history' );
95
96
/**
97
 * Donation Form Shortcode
98
 *
99
 * Show the Give donation form.
100
 *
101
 * @since  1.0
102
 *
103
 * @param  array $atts Shortcode attributes
104
 *
105
 * @return string
106
 */
107
function give_form_shortcode( $atts ) {
108
	$atts = shortcode_atts( array(
109
		'id'                    => '',
110
		'show_title'            => true,
111
		'show_goal'             => true,
112
		'show_content'          => '',
113
		'float_labels'          => '',
114
		'display_style'         => '',
115
		'continue_button_title' => '',
116
	), $atts, 'give_form' );
117
118
	// Convert string to bool.
119
	$atts['show_title'] = filter_var( $atts['show_title'], FILTER_VALIDATE_BOOLEAN );
120
	$atts['show_goal']  = filter_var( $atts['show_goal'], FILTER_VALIDATE_BOOLEAN );
121
122
	//get the Give Form
123
	ob_start();
124
	give_get_donation_form( $atts );
125
	$final_output = ob_get_clean();
126
127
	return apply_filters( 'give_donate_form', $final_output, $atts );
128
}
129
130
add_shortcode( 'give_form', 'give_form_shortcode' );
131
132
/**
133
 * Donation Form Goal Shortcode.
134
 *
135
 * Show the Give donation form goals.
136
 *
137
 * @since  1.0
138
 *
139
 * @param  array $atts Shortcode attributes.
140
 *
141
 * @return string
142
 */
143
function give_goal_shortcode( $atts ) {
144
	$atts = shortcode_atts( array(
145
		'id'        => '',
146
		'show_text' => true,
147
		'show_bar'  => true,
148
	), $atts, 'give_goal' );
149
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
150
151
	//get the Give Form.
152
	ob_start();
153
154
	//Sanity check 1: ensure there is an ID Provided.
155
	if ( empty( $atts['id'] ) ) {
156
		Give()->notices->print_frontend_notice( __( 'The shortcode is missing Donation Form ID attribute.', 'give' ), true );
157
	}
158
159
	//Sanity check 2: Check the form even has Goals enabled.
160
	if ( ! give_is_setting_enabled( give_get_meta( $atts['id'], '_give_goal_option', true ) ) ) {
161
162
		Give()->notices->print_frontend_notice( __( 'The form does not have Goals enabled.', 'give' ), true );
163
	} else {
164
		//Passed all sanity checks: output Goal.
165
		give_show_goal_progress( $atts['id'], $atts );
166
	}
167
168
	$final_output = ob_get_clean();
169
170
	return apply_filters( 'give_goal_shortcode_output', $final_output, $atts );
171
}
172
173
add_shortcode( 'give_goal', 'give_goal_shortcode' );
174
175
176
/**
177
 * Login Shortcode.
178
 *
179
 * Shows a login form allowing users to users to log in. This function simply
180
 * calls the give_login_form function to display the login form.
181
 *
182
 * @since  1.0
183
 *
184
 * @param  array $atts Shortcode attributes.
185
 *
186
 * @uses   give_login_form()
187
 *
188
 * @return string
189
 */
190
function give_login_form_shortcode( $atts ) {
191
	$atts = shortcode_atts( array(
192
		// Add backward compatibility for redirect attribute.
193
		'redirect' => '',
194
195
		'login-redirect'  => '',
196
		'logout-redirect' => '',
197
	), $atts, 'give_login' );
198
199
	// Check login-redirect attribute first, if it empty or not found then check for redirect attribute and add value of this to login-redirect attribute.
200
	$atts['login-redirect'] = ! empty( $atts['login-redirect'] ) ? $atts['login-redirect'] : ( ! empty( $atts['redirect'] ) ? $atts['redirect'] : '' );
201
202
	return give_login_form( $atts['login-redirect'], $atts['logout-redirect'] );
203
}
204
205
add_shortcode( 'give_login', 'give_login_form_shortcode' );
206
207
/**
208
 * Register Shortcode.
209
 *
210
 * Shows a registration form allowing users to users to register for the site.
211
 *
212
 * @since  1.0
213
 *
214
 * @param  array $atts Shortcode attributes.
215
 *
216
 * @uses   give_register_form()
217
 *
218
 * @return string
219
 */
220
function give_register_form_shortcode( $atts ) {
221
	$atts = shortcode_atts( array(
222
		'redirect' => '',
223
	), $atts, 'give_register' );
224
225
	return give_register_form( $atts['redirect'] );
226
}
227
228
add_shortcode( 'give_register', 'give_register_form_shortcode' );
229
230
/**
231
 * Receipt Shortcode.
232
 *
233
 * Shows a donation receipt.
234
 *
235
 * @since  1.0
236
 *
237
 * @param  array $atts Shortcode attributes.
238
 *
239
 * @return string
240
 */
241
function give_receipt_shortcode( $atts ) {
242
243
	global $give_receipt_args;
244
245
	$give_receipt_args = shortcode_atts( array(
246
		'error'          => __( 'You are missing the payment key to view this donation receipt.', 'give' ),
247
		'price'          => true,
248
		'donor'          => true,
249
		'date'           => true,
250
		'payment_key'    => false,
251
		'payment_method' => true,
252
		'payment_id'     => true,
253
		'payment_status' => false,
254
		'status_notice'  => true,
255
	), $atts, 'give_receipt' );
256
257
	//set $session var
258
	$session = give_get_purchase_session();
259
260
	//set payment key var
261
	if ( isset( $_GET['payment_key'] ) ) {
262
		$payment_key = urldecode( $_GET['payment_key'] );
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...
263
	} elseif ( $session ) {
264
		$payment_key = $session['purchase_key'];
265
	} elseif ( $give_receipt_args['payment_key'] ) {
266
		$payment_key = $give_receipt_args['payment_key'];
267
	}
268
269
	$email_access = give_get_option( 'email_access' );
270
271
	// No payment_key found & Email Access is Turned on:
272
	if ( ! isset( $payment_key ) && give_is_setting_enabled( $email_access ) && ! Give()->email_access->token_exists ) {
273
274
		ob_start();
275
276
		give_get_template_part( 'email-login-form' );
277
278
		return ob_get_clean();
279
280
	} elseif ( ! isset( $payment_key ) ) {
281
282
		return Give()->notices->print_frontend_notice( $give_receipt_args['error'], false, 'error' );
283
284
	}
285
286
	$user_can_view = give_can_view_receipt( $payment_key );
287
288
	// Key was provided, but user is logged out. Offer them the ability to login and view the receipt.
289
	if ( ! $user_can_view && give_is_setting_enabled( $email_access ) && ! Give()->email_access->token_exists ) {
290
291
		ob_start();
292
293
		give_get_template_part( 'email-login-form' );
294
295
		return ob_get_clean();
296
297
	} elseif ( ! $user_can_view ) {
298
299
		global $give_login_redirect;
300
301
		$give_login_redirect = give_get_current_page_url();
302
303
		ob_start();
304
305
		Give()->notices->print_frontend_notice( apply_filters( 'give_must_be_logged_in_error_message', __( 'You must be logged in to view this donation receipt.', 'give' ) ) );
306
307
		give_get_template_part( 'shortcode', 'login' );
308
309
		$login_form = ob_get_clean();
310
311
		return $login_form;
312
	}
313
314
	/**
315
	 * Check if the user has permission to view the receipt.
316
	 *
317
	 * If user is logged in, user ID is compared to user ID of ID stored in payment meta
318
	 * or if user is logged out and donation was made as a guest, the donation session is checked for
319
	 * or if user is logged in and the user can view sensitive shop data.
320
	 */
321
	if ( ! apply_filters( 'give_user_can_view_receipt', $user_can_view, $give_receipt_args ) ) {
322
		return Give()->notices->print_frontend_notice( $give_receipt_args['error'], false, 'error' );
323
	}
324
325
	ob_start();
326
327
	give_get_template_part( 'shortcode', 'receipt' );
328
329
	$display = ob_get_clean();
330
331
	return $display;
332
}
333
334
add_shortcode( 'give_receipt', 'give_receipt_shortcode' );
335
336
/**
337
 * Profile Editor Shortcode.
338
 *
339
 * Outputs the Give Profile Editor to allow users to amend their details from the
340
 * front-end. This function uses the Give templating system allowing users to
341
 * override the default profile editor template. The profile editor template is located
342
 * under templates/profile-editor.php, however, it can be altered by creating a
343
 * file called profile-editor.php in the give_template directory in your active theme's
344
 * folder. Please visit the Give Documentation for more information on how the
345
 * templating system is used.
346
 *
347
 * @since  1.0
348
 *
349
 * @param  array $atts Shortcode attributes.
350
 *
351
 * @return string Output generated from the profile editor
352
 */
353
function give_profile_editor_shortcode( $atts ) {
0 ignored issues
show
Unused Code introduced by
The parameter $atts 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...
354
355
	ob_start();
356
357
	// Restrict access to donor profile, if donor and user are disconnected.
358
	$is_donor_disconnected = get_user_meta( get_current_user_id(), '_give_is_donor_disconnected', true );
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
359
	if( is_user_logged_in() && $is_donor_disconnected ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
360
		Give()->notices->print_frontend_notice( __( 'Your Donor and User profile are no longer connected. Please contact the site administrator.', 'give' ), true, 'error' );
361
		return false;
362
	}
363
364
	give_get_template_part( 'shortcode', 'profile-editor' );
365
366
	$display = ob_get_clean();
367
368
	return $display;
369
}
370
371
add_shortcode( 'give_profile_editor', 'give_profile_editor_shortcode' );
372
373
/**
374
 * Process Profile Updater Form.
375
 *
376
 * Processes the profile updater form by updating the necessary fields.
377
 *
378
 * @since  1.0
379
 *
380
 * @param  array $data Data sent from the profile editor.
381
 *
382
 * @return bool
383
 */
384
function give_process_profile_editor_updates( $data ) {
385
	// Profile field change request
386
	if ( empty( $_POST['give_profile_editor_submit'] ) && ! is_user_logged_in() ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
387
		return false;
388
	}
389
390
	// Nonce security
391
	if ( ! wp_verify_nonce( $data['give_profile_editor_nonce'], 'give-profile-editor-nonce' ) ) {
392
		return false;
393
	}
394
395
	$user_id       = get_current_user_id();
396
	$old_user_data = get_userdata( $user_id );
397
398
	$display_name     = isset( $data['give_display_name'] ) ? sanitize_text_field( $data['give_display_name'] ) : $old_user_data->display_name;
399
	$first_name       = isset( $data['give_first_name'] ) ? sanitize_text_field( $data['give_first_name'] ) : $old_user_data->first_name;
400
	$last_name        = isset( $data['give_last_name'] ) ? sanitize_text_field( $data['give_last_name'] ) : $old_user_data->last_name;
401
	$email            = isset( $data['give_email'] ) ? sanitize_email( $data['give_email'] ) : $old_user_data->user_email;
402
	$line1            = ( isset( $data['give_address_line1'] ) ? sanitize_text_field( $data['give_address_line1'] ) : '' );
403
	$line2            = ( isset( $data['give_address_line2'] ) ? sanitize_text_field( $data['give_address_line2'] ) : '' );
404
	$city             = ( isset( $data['give_address_city'] ) ? sanitize_text_field( $data['give_address_city'] ) : '' );
405
	$state            = ( isset( $data['give_address_state'] ) ? sanitize_text_field( $data['give_address_state'] ) : '' );
406
	$zip              = ( isset( $data['give_address_zip'] ) ? sanitize_text_field( $data['give_address_zip'] ) : '' );
407
	$country          = ( isset( $data['give_address_country'] ) ? sanitize_text_field( $data['give_address_country'] ) : '' );
408
	$password         = ! empty( $data['give_new_user_pass1'] ) ? $data['give_new_user_pass1'] : '';
409
	$confirm_password = ! empty( $data['give_new_user_pass2'] ) ? $data['give_new_user_pass2'] : '';
410
411
	$userdata = array(
412
		'ID'           => $user_id,
413
		'first_name'   => $first_name,
414
		'last_name'    => $last_name,
415
		'display_name' => $display_name,
416
		'user_email'   => $email,
417
		'user_pass'    => $password,
418
	);
419
420
	if( empty( $line1 ) || empty( $city ) || empty( $state ) || empty( $zip ) || empty( $country ) ) {
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...
421
		give_set_error( 'give-empty-address-fields', __( 'Please fill in the required address fields.', 'give' ) );
422
	}
423
424
	$address = array(
425
		'line1'   => $line1,
426
		'line2'   => $line2,
427
		'city'    => $city,
428
		'state'   => $state,
429
		'zip'     => $zip,
430
		'country' => $country,
431
	);
432
433
	/**
434
	 * Fires before updating user profile.
435
	 *
436
	 * @since 1.0
437
	 *
438
	 * @param int $user_id The ID of the user.
439
	 * @param array $userdata User info, including ID, first name, last name, display name and email.
440
	 */
441
	do_action( 'give_pre_update_user_profile', $user_id, $userdata );
442
443
	// Validate First Name.
444
	if( empty( $first_name ) ) {
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...
445
		give_set_error( 'give-empty-first-name', __( 'Please enter first name.', 'give' ) );
446
	}
447
448
	// Make sure to validate passwords for existing Donors.
449
	give_validate_user_password( $password, $confirm_password );
450
451
	if ( empty( $email ) ) {
452
		// Make sure email should not be empty.
453
		give_set_error( 'email_empty', __( 'The email you entered is empty.', 'give' ) );
454
455
	} else if ( ! is_email( $email ) ) {
456
		// Make sure email should be valid.
457
		give_set_error( 'email_not_valid', __( 'The email you entered is not valid. Please use another', 'give' ) );
458
459
	} else if ( $email != $old_user_data->user_email ) {
460
		// Make sure the new email doesn't belong to another user
461
		if ( email_exists( $email ) ) {
462
			give_set_error( 'user_email_exists', __( 'The email you entered belongs to another user. Please use another.', 'give' ) );
463
		} elseif ( Give()->donors->get_donor_by( 'email', $email ) ){
464
			// Make sure the new email doesn't belong to another user
465
			give_set_error( 'donor_email_exists', __( 'The email you entered belongs to another donor. Please use another.', 'give' ) );
466
		}
467
	}
468
469
	// Check for errors
470
	$errors = give_get_errors();
471
472
	if ( $errors ) {
473
		// Send back to the profile editor if there are errors
474
		wp_redirect( $data['give_redirect'] );
475
		give_die();
476
	}
477
478
	// Update the user
479
	$meta    = update_user_meta( $user_id, '_give_user_address', $address );
0 ignored issues
show
Unused Code introduced by
$meta is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
480
	$updated = wp_update_user( $userdata );
481
482
	if ( $updated ) {
483
484
		/**
485
		 * Fires after updating user profile.
486
		 *
487
		 * @since 1.0
488
		 *
489
		 * @param int $user_id The ID of the user.
490
		 * @param array $userdata User info, including ID, first name, last name, display name and email.
491
		 */
492
		do_action( 'give_user_profile_updated', $user_id, $userdata );
493
		wp_redirect( add_query_arg( 'updated', 'true', $data['give_redirect'] ) );
494
		give_die();
495
	}
496
497
	return false;
498
}
499
500
add_action( 'give_edit_user_profile', 'give_process_profile_editor_updates' );
501