Test Failed
Pull Request — master (#2225)
by Devin
04:40
created

shortcodes.php ➔ give_donation_history()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 58
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 31
nc 4
nop 1
dl 0
loc 58
ccs 0
cts 28
cp 0
crap 56
rs 7.6045
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
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
		echo '<a href="' . esc_url( give_get_history_page_uri() ) . '">&laquo; ' . __( 'Return to All Donations', 'give' ) . '</a>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
48
49
		return ob_get_clean();
50
	}
51
52
	$email_access = give_get_option( 'email_access' );
53
54
	/**
55
	 * Determine access
56
	 *
57
	 * a. Check if a user is logged in or does a session exist?
58
	 * b. Does an email-access token exist?
59
	 */
60
	if (
61
		is_user_logged_in() || false !== Give()->session->get_session_expiration()
62
		|| ( give_is_setting_enabled( $email_access ) && Give()->email_access->token_exists )
63
	) {
64
		ob_start();
65
		give_get_template_part( 'history', 'donations' );
66
67
		return ob_get_clean();
68
69
	} elseif ( give_is_setting_enabled( $email_access ) ) {
70
		// Is Email-based access enabled?
71
		ob_start();
72
		give_get_template_part( 'email', 'login-form' );
73
74
		return ob_get_clean();
75
76
	} else {
77
78
		$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 ) );
79
		$output .= do_shortcode( '[give_login]' );
80
81
		return $output;
82
	}
83
}
84
85
add_shortcode( 'donation_history', 'give_donation_history' );
86
87
/**
88
 * Donation Form Shortcode
89
 *
90
 * Show the Give donation form.
91
 *
92
 * @since  1.0
93
 *
94
 * @param  array $atts Shortcode attributes
95
 *
96
 * @return string
97
 */
98
function give_form_shortcode( $atts ) {
99
	$atts = shortcode_atts( array(
100
		'id'                    => '',
101
		'show_title'            => true,
102
		'show_goal'             => true,
103
		'show_content'          => '',
104
		'float_labels'          => '',
105
		'display_style'         => '',
106
		'continue_button_title' => '',
107
	), $atts, 'give_form' );
108
109
	// Convert string to bool.
110
	$atts['show_title'] = filter_var( $atts['show_title'], FILTER_VALIDATE_BOOLEAN );
111
	$atts['show_goal']  = filter_var( $atts['show_goal'], FILTER_VALIDATE_BOOLEAN );
112
113
	//get the Give Form
114
	ob_start();
115
	give_get_donation_form( $atts );
116
	$final_output = ob_get_clean();
117
118
	return apply_filters( 'give_donate_form', $final_output, $atts );
119
}
120
121
add_shortcode( 'give_form', 'give_form_shortcode' );
122
123
/**
124
 * Donation Form Goal Shortcode.
125
 *
126
 * Show the Give donation form goals.
127
 *
128
 * @since  1.0
129
 *
130
 * @param  array $atts Shortcode attributes.
131
 *
132
 * @return string
133
 */
134
function give_goal_shortcode( $atts ) {
135
	$atts = shortcode_atts( array(
136
		'id'        => '',
137
		'show_text' => true,
138
		'show_bar'  => true,
139
	), $atts, 'give_goal' );
140
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
141
142
	//get the Give Form.
143
	ob_start();
144
145
	//Sanity check 1: ensure there is an ID Provided.
146
	if ( empty( $atts['id'] ) ) {
147
		Give()->notices->print_frontend_notice( __( 'The shortcode is missing Donation Form ID attribute.', 'give' ), true );
148
	}
149
150
	//Sanity check 2: Check the form even has Goals enabled.
151
	if ( ! give_is_setting_enabled( give_get_meta( $atts['id'], '_give_goal_option', true ) ) ) {
152
153
		Give()->notices->print_frontend_notice( __( 'The form does not have Goals enabled.', 'give' ), true );
154
	} else {
155
		//Passed all sanity checks: output Goal.
156
		give_show_goal_progress( $atts['id'], $atts );
157
	}
158
159
	$final_output = ob_get_clean();
160
161
	return apply_filters( 'give_goal_shortcode_output', $final_output, $atts );
162
}
163
164
add_shortcode( 'give_goal', 'give_goal_shortcode' );
165
166
167
/**
168
 * Login Shortcode.
169
 *
170
 * Shows a login form allowing users to users to log in. This function simply
171
 * calls the give_login_form function to display the login form.
172
 *
173
 * @since  1.0
174
 *
175
 * @param  array $atts Shortcode attributes.
176
 *
177
 * @uses   give_login_form()
178
 *
179
 * @return string
180
 */
181
function give_login_form_shortcode( $atts ) {
182
	$atts = shortcode_atts( array(
183
		// Add backward compatibility for redirect attribute.
184
		'redirect' => '',
185
186
		'login-redirect'  => '',
187
		'logout-redirect' => '',
188
	), $atts, 'give_login' );
189
190
	// 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.
191
	$atts['login-redirect'] = ! empty( $atts['login-redirect'] ) ? $atts['login-redirect'] : ( ! empty( $atts['redirect'] ) ? $atts['redirect'] : '' );
192
193
	return give_login_form( $atts['login-redirect'], $atts['logout-redirect'] );
194
}
195
196
add_shortcode( 'give_login', 'give_login_form_shortcode' );
197
198
/**
199
 * Register Shortcode.
200
 *
201
 * Shows a registration form allowing users to users to register for the site.
202
 *
203
 * @since  1.0
204
 *
205
 * @param  array $atts Shortcode attributes.
206
 *
207
 * @uses   give_register_form()
208
 *
209
 * @return string
210
 */
211
function give_register_form_shortcode( $atts ) {
212
	$atts = shortcode_atts( array(
213
		'redirect' => '',
214
	), $atts, 'give_register' );
215
216
	return give_register_form( $atts['redirect'] );
217
}
218
219
add_shortcode( 'give_register', 'give_register_form_shortcode' );
220
221
/**
222
 * Receipt Shortcode.
223
 *
224
 * Shows a donation receipt.
225
 *
226
 * @since  1.0
227
 *
228
 * @param  array $atts Shortcode attributes.
229
 *
230
 * @return string
231
 */
232
function give_receipt_shortcode( $atts ) {
233
234
	global $give_receipt_args, $payment;
235
236
	$give_receipt_args = shortcode_atts( array(
237
		'error'          => __( 'You are missing the payment key to view this donation receipt.', 'give' ),
238
		'price'          => true,
239
		'donor'          => true,
240
		'date'           => true,
241
		'payment_key'    => false,
242
		'payment_method' => true,
243
		'payment_id'     => true,
244
		'payment_status' => false,
245
		'status_notice'  => true,
246
	), $atts, 'give_receipt' );
247
248
	//set $session var
249
	$session = give_get_purchase_session();
250
251
	//set payment key var
252
	if ( isset( $_GET['payment_key'] ) ) {
253
		$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...
254
	} elseif ( $session ) {
255
		$payment_key = $session['purchase_key'];
256
	} elseif ( $give_receipt_args['payment_key'] ) {
257
		$payment_key = $give_receipt_args['payment_key'];
258
	}
259
260
	$email_access = give_get_option( 'email_access' );
261
262
	// No payment_key found & Email Access is Turned on:
263
	if ( ! isset( $payment_key ) && give_is_setting_enabled( $email_access ) && ! Give()->email_access->token_exists ) {
264
265
		ob_start();
266
267
		give_get_template_part( 'email-login-form' );
268
269
		return ob_get_clean();
270
271
	} elseif ( ! isset( $payment_key ) ) {
272
273
		return Give()->notices->print_frontend_notice( $give_receipt_args['error'], false, 'error' );
274
275
	}
276
277
	$payment_id    = give_get_purchase_id_by_key( $payment_key );
0 ignored issues
show
Unused Code introduced by
$payment_id is not used, you could remove the assignment.

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

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

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

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

Loading history...
278
	$user_can_view = give_can_view_receipt( $payment_key );
279
280
	// Key was provided, but user is logged out. Offer them the ability to login and view the receipt.
281
	if ( ! $user_can_view && give_is_setting_enabled( $email_access ) && ! Give()->email_access->token_exists ) {
282
283
		ob_start();
284
285
		give_get_template_part( 'email-login-form' );
286
287
		return ob_get_clean();
288
289
	} elseif ( ! $user_can_view ) {
290
291
		global $give_login_redirect;
292
293
		$give_login_redirect = give_get_current_page_url();
294
295
		ob_start();
296
297
		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' ) ) );
298
299
		give_get_template_part( 'shortcode', 'login' );
300
301
		$login_form = ob_get_clean();
302
303
		return $login_form;
304
	}
305
306
	/**
307
	 * Check if the user has permission to view the receipt.
308
	 *
309
	 * If user is logged in, user ID is compared to user ID of ID stored in payment meta
310
	 * or if user is logged out and donation was made as a guest, the donation session is checked for
311
	 * or if user is logged in and the user can view sensitive shop data.
312
	 */
313
	if ( ! apply_filters( 'give_user_can_view_receipt', $user_can_view, $give_receipt_args ) ) {
314
		return Give()->notices->print_frontend_notice( $give_receipt_args['error'], false, 'error' );
315
	}
316
317
	ob_start();
318
319
	give_get_template_part( 'shortcode', 'receipt' );
320
321
	$display = ob_get_clean();
322
323
	return $display;
324
}
325
326
add_shortcode( 'give_receipt', 'give_receipt_shortcode' );
327
328
/**
329
 * Profile Editor Shortcode.
330
 *
331
 * Outputs the Give Profile Editor to allow users to amend their details from the
332
 * front-end. This function uses the Give templating system allowing users to
333
 * override the default profile editor template. The profile editor template is located
334
 * under templates/profile-editor.php, however, it can be altered by creating a
335
 * file called profile-editor.php in the give_template directory in your active theme's
336
 * folder. Please visit the Give Documentation for more information on how the
337
 * templating system is used.
338
 *
339
 * @since  1.0
340
 *
341
 * @param  array $atts Shortcode attributes.
342
 *
343
 * @return string Output generated from the profile editor
344
 */
345
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...
346
347
	ob_start();
348
349
	// Restrict access to donor profile, if donor and user are disconnected.
350
	$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...
351
	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...
352
		Give()->notices->print_frontend_notice( __( 'Your Donor and User profile are no longer connected. Please contact the site administrator.', 'give' ), true, 'error' );
353
		return false;
354
	}
355
356
	give_get_template_part( 'shortcode', 'profile-editor' );
357
358
	$display = ob_get_clean();
359
360
	return $display;
361
}
362
363
add_shortcode( 'give_profile_editor', 'give_profile_editor_shortcode' );
364
365
/**
366
 * Process Profile Updater Form.
367
 *
368
 * Processes the profile updater form by updating the necessary fields.
369
 *
370
 * @since  1.0
371
 *
372
 * @param  array $data Data sent from the profile editor.
373
 *
374
 * @return bool
375
 */
376
function give_process_profile_editor_updates( $data ) {
377
	// Profile field change request
378
	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...
379
		return false;
380
	}
381
382
	// Nonce security
383
	if ( ! wp_verify_nonce( $data['give_profile_editor_nonce'], 'give-profile-editor-nonce' ) ) {
384
		return false;
385
	}
386
387
	$user_id       = get_current_user_id();
388
	$old_user_data = get_userdata( $user_id );
389
390
	$display_name = isset( $data['give_display_name'] ) ? sanitize_text_field( $data['give_display_name'] ) : $old_user_data->display_name;
391
	$first_name   = isset( $data['give_first_name'] ) ? sanitize_text_field( $data['give_first_name'] ) : $old_user_data->first_name;
392
	$last_name    = isset( $data['give_last_name'] ) ? sanitize_text_field( $data['give_last_name'] ) : $old_user_data->last_name;
393
	$email        = isset( $data['give_email'] ) ? sanitize_email( $data['give_email'] ) : $old_user_data->user_email;
394
	$line1        = ( isset( $data['give_address_line1'] ) ? sanitize_text_field( $data['give_address_line1'] ) : '' );
395
	$line2        = ( isset( $data['give_address_line2'] ) ? sanitize_text_field( $data['give_address_line2'] ) : '' );
396
	$city         = ( isset( $data['give_address_city'] ) ? sanitize_text_field( $data['give_address_city'] ) : '' );
397
	$state        = ( isset( $data['give_address_state'] ) ? sanitize_text_field( $data['give_address_state'] ) : '' );
398
	$zip          = ( isset( $data['give_address_zip'] ) ? sanitize_text_field( $data['give_address_zip'] ) : '' );
399
	$country      = ( isset( $data['give_address_country'] ) ? sanitize_text_field( $data['give_address_country'] ) : '' );
400
401
	$userdata = array(
402
		'ID'           => $user_id,
403
		'first_name'   => $first_name,
404
		'last_name'    => $last_name,
405
		'display_name' => $display_name,
406
		'user_email'   => $email,
407
	);
408
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
409
410
	$address = array(
411
		'line1'   => $line1,
412
		'line2'   => $line2,
413
		'city'    => $city,
414
		'state'   => $state,
415
		'zip'     => $zip,
416
		'country' => $country,
417
	);
418
419
	/**
420
	 * Fires before updating user profile.
421
	 *
422
	 * @since 1.0
423
	 *
424
	 * @param int $user_id The ID of the user.
425
	 * @param array $userdata User info, including ID, first name, last name, display name and email.
426
	 */
427
	do_action( 'give_pre_update_user_profile', $user_id, $userdata );
428
429
	// Make sure to validate passwords for existing Donors
430
	give_validate_user_password( $data['give_new_user_pass1'], $data['give_new_user_pass2'] );
431
432
	if ( empty( $email ) ) {
433
		// Make sure email should not be empty.
434
		give_set_error( 'email_empty', __( 'The email you entered is empty.', 'give' ) );
435
436
	} else if ( ! is_email( $email ) ) {
437
		// Make sure email should be valid.
438
		give_set_error( 'email_not_valid', __( 'The email you entered is not valid. Please use another', 'give' ) );
439
440
	} else if ( $email != $old_user_data->user_email ) {
441
		// Make sure the new email doesn't belong to another user
442
		if ( email_exists( $email ) ) {
443
			give_set_error( 'email_exists', __( 'The email you entered belongs to another user. Please use another.', 'give' ) );
444
		}
445
	}
446
447
	// Check for errors
448
	$errors = give_get_errors();
449
450
	if ( $errors ) {
451
		// Send back to the profile editor if there are errors
452
		wp_redirect( $data['give_redirect'] );
453
		give_die();
454
	}
455
456
	// Update the user
457
	$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...
458
	$updated = wp_update_user( $userdata );
459
460
	if ( $updated ) {
461
462
		/**
463
		 * Fires after updating user profile.
464
		 *
465
		 * @since 1.0
466
		 *
467
		 * @param int $user_id The ID of the user.
468
		 * @param array $userdata User info, including ID, first name, last name, display name and email.
469
		 */
470
		do_action( 'give_user_profile_updated', $user_id, $userdata );
471
		wp_redirect( add_query_arg( 'updated', 'true', $data['give_redirect'] ) );
472
		give_die();
473
	}
474
475
	return false;
476
}
477
478
add_action( 'give_edit_user_profile', 'give_process_profile_editor_updates' );
479