Completed
Pull Request — master (#1201)
by Ravinder
23:20
created

shortcodes.php ➔ give_donation_history()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 4
nop 0
dl 0
loc 31
ccs 0
cts 12
cp 0
crap 56
rs 6.7272
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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() {
27
28
	// If payment_key query arg exists, return receipt instead of donation history.
29
	if ( isset( $_GET['payment_key'] ) ) {
30
		ob_start();
31
		echo give_receipt_shortcode( array() );
32
		echo '<a href="' . esc_url( give_get_history_page_uri() ) . '">&laquo; ' . esc_html__( 'Return to All Donations', 'give' ) . '</a>';
33
34
		return ob_get_clean();
35
	}
36
37
	$email_access = give_get_option( 'email_access' );
38
39
	//Is user logged in? Does a session exist? Does an email-access token exist?
40
	if ( is_user_logged_in() || Give()->session->get_session_expiration() !== false || ( $email_access == 'on' && Give()->email_access->token_exists ) ) {
41
		ob_start();
42
		give_get_template_part( 'history', 'donations' );
43
44
		return ob_get_clean();
45
	} //Is Email-based access enabled?
46
	elseif ( $email_access == 'on' ) {
47
48
		ob_start();
49
		give_get_template_part( 'email', 'login-form' );
50
51
		return ob_get_clean();
52
	} else {
53
		$message = esc_html__( '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' );
54
		echo apply_filters( 'give_donation_history_nonuser_message', give_output_error( $message, false ), $message );
55
	}
56
}
57
58
add_shortcode( 'donation_history', 'give_donation_history' );
59
60
/**
61
 * Donation Form Shortcode
62
 *
63
 * Show the Give donation form.
64
 *
65
 * @since  1.0
66
 *
67
 * @param  array  $atts Shortcode attributes
68
 *
69
 * @return string
70
 */
71
function give_form_shortcode( $atts ) {
72
	$atts = shortcode_atts( array(
73
		'id'            => '',
74
		'show_title'    => true,
75
		'show_goal'     => true,
76
		'show_content'  => '',
77
		'float_labels'  => '',
78
		'display_style' => '',
79
	), $atts, 'give_form' );
80
81
	foreach ( $atts as $key => $value ) {
82
		//convert shortcode_atts values to booleans
83
		if ( $key == 'show_title' ) {
84
			$atts[ $key ] = filter_var( $atts[ $key ], FILTER_VALIDATE_BOOLEAN );
85
		} elseif ( $key == 'show_goal' ) {
86
			$atts[ $key ] = filter_var( $atts[ $key ], FILTER_VALIDATE_BOOLEAN );
87
		}
88
89
		//validate show_content value
90
		if ( $key == 'show_content' ) {
91
			if ( ! in_array( $value, array( 'none', 'above', 'below' ) ) ) {
92
				$atts[ $key ] = '';
93
			} else if ( $value == 'above' ) {
94
				$atts[ $key ] = 'give_pre_form';
95
			} else if ( $value == 'below' ) {
96
				$atts[ $key ] = 'give_post_form';
97
			}
98
		}
99
100
		//validate display_style and float_labels value
101
		if ( ( $key == 'display_style' && ! in_array( $value, array( 'onpage', 'reveal', 'modal' ) ) )
102
		     || ( $key == 'float_labels' && ! in_array( $value, array( 'enabled', 'disabled' ) ) )
103
		) {
104
105
			$atts[ $key ] = '';
106
		}
107
	}
108
109
	//get the Give Form
110
	ob_start();
111
	give_get_donation_form( $atts );
112
	$final_output = ob_get_clean();
113
114
	return apply_filters( 'give_donate_form', $final_output, $atts );
115
}
116
117
add_shortcode( 'give_form', 'give_form_shortcode' );
118
119
/**
120
 * Donation Form Goal Shortcode.
121
 *
122
 * Show the Give donation form goals.
123
 *
124
 * @since  1.0
125
 *
126
 * @param  array  $atts Shortcode attributes.
127
 *
128
 * @return string
129
 */
130
function give_goal_shortcode( $atts ) {
131
	$atts = shortcode_atts( array(
132
		'id'        => '',
133
		'show_text' => true,
134
		'show_bar'  => true,
135
	), $atts, 'give_goal' );
136
137
138
	//get the Give Form.
139
	ob_start();
140
141
	//Sanity check 1: ensure there is an ID Provided.
142
	if ( empty( $atts['id'] ) ) {
143
		give_output_error( esc_html__( 'The shortcode is missing Donation Form ID attribute.', 'give' ), true );
144
	}
145
146
	//Sanity check 2: Check the form even has Goals enabled.
147
	$goal_option = get_post_meta( $atts['id'], '_give_goal_option', true );
148
	if ( empty( $goal_option ) || $goal_option !== 'yes' ) {
149
		give_output_error( esc_html__( 'The form does not have Goals enabled.', 'give' ), true );
150
	} else {
151
		//Passed all sanity checks: output Goal.
152
		give_show_goal_progress( $atts['id'], $atts );
153
	}
154
155
	$final_output = ob_get_clean();
156
157
	return apply_filters( 'give_goal_shortcode_output', $final_output, $atts );
158
}
159
160
add_shortcode( 'give_goal', 'give_goal_shortcode' );
161
162
163
/**
164
 * Login Shortcode.
165
 *
166
 * Shows a login form allowing users to users to log in. This function simply
167
 * calls the give_login_form function to display the login form.
168
 *
169
 * @since  1.0
170
 *
171
 * @param  array  $atts Shortcode attributes.
172
 *
173
 * @uses   give_login_form()
174
 *
175
 * @return string
176
 */
177
function give_login_form_shortcode( $atts ) {
178
	$atts = shortcode_atts( array(
179
        // Add backward compatibility for redirect attribute.
180
        'redirect'          => '',
181
182
		'login-redirect'    => '',
183
		'logout-redirect'   => '',
184
	), $atts, 'give_login' );
185
186
    // 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.
187
    $atts['login-redirect'] = ! empty( $atts['login-redirect'] ) ? $atts['login-redirect'] : ( ! empty( $atts['redirect' ] ) ? $atts['redirect'] : '' );
188
189
	return give_login_form( $atts['login-redirect'], $atts['logout-redirect'] );
190
}
191
192
add_shortcode( 'give_login', 'give_login_form_shortcode' );
193
194
/**
195
 * Register Shortcode.
196
 *
197
 * Shows a registration form allowing users to users to register for the site.
198
 *
199
 * @since  1.0
200
 *
201
 * @param  array  $atts Shortcode attributes.
202
 *
203
 * @uses   give_register_form()
204
 *
205
 * @return string
206
 */
207
function give_register_form_shortcode( $atts ) {
208
	$atts = shortcode_atts( array(
209
		'redirect' => '',
210
	), $atts, 'give_register' );
211
212
	return give_register_form( $atts['redirect'] );
213
}
214
215
add_shortcode( 'give_register', 'give_register_form_shortcode' );
216
217
/**
218
 * Receipt Shortcode.
219
 *
220
 * Shows a donation receipt.
221
 *
222
 * @since  1.0
223
 *
224
 * @param  array  $atts Shortcode attributes.
225
 *
226
 * @return string
227
 */
228
function give_receipt_shortcode( $atts ) {
229
230
	global $give_receipt_args, $payment;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
231
232
	$give_receipt_args = shortcode_atts( array(
233
		'error'          => esc_html__( 'You are missing the payment key to view this donation receipt.', 'give' ),
234
		'price'          => true,
235
		'donor'          => true,
236
		'date'           => true,
237
		'payment_key'    => false,
238
		'payment_method' => true,
239
		'payment_id'     => true,
240
		'payment_status' => false,
241
		'status_notice'  => true,
242
	), $atts, 'give_receipt' );
243
244
	//set $session var
245
	$session = give_get_purchase_session();
246
247
	//set payment key var
248
	if ( isset( $_GET['payment_key'] ) ) {
249
		$payment_key = urldecode( $_GET['payment_key'] );
250
	} elseif ( $session ) {
251
		$payment_key = $session['purchase_key'];
252
	} elseif ( $give_receipt_args['payment_key'] ) {
253
		$payment_key = $give_receipt_args['payment_key'];
254
	}
255
256
	$email_access = give_get_option( 'email_access' );
257
258
	// No payment_key found & Email Access is Turned on:
259
	if ( ! isset( $payment_key ) && $email_access == 'on' && ! Give()->email_access->token_exists ) {
260
261
		ob_start();
262
263
		give_get_template_part( 'email-login-form' );
264
265
		return ob_get_clean();
266
267
	} elseif ( ! isset( $payment_key ) ) {
268
269
		return give_output_error( $give_receipt_args['error'], false, 'error' );
270
271
	}
272
273
	$payment_id    = give_get_purchase_id_by_key( $payment_key );
274
	$user_can_view = give_can_view_receipt( $payment_key );
275
276
	// Key was provided, but user is logged out. Offer them the ability to login and view the receipt.
277
	if ( ! $user_can_view && $email_access == 'on' && ! Give()->email_access->token_exists ) {
278
279
		ob_start();
280
281
		give_get_template_part( 'email-login-form' );
282
283
		return ob_get_clean();
284
285
	} elseif ( ! $user_can_view ) {
286
287
		global $give_login_redirect;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

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