Completed
Pull Request — master (#1412)
by Ravinder
17:25
created

shortcodes.php ➔ give_form_shortcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
ccs 0
cts 16
cp 0
crap 2
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
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
	// Convert string to bool.
82
	$atts['show_title'] = (bool) $atts['show_title'];
83
	$atts['show_goal']  = (bool) $atts['show_goal'];
84
85
	//get the Give Form
86
	ob_start();
87
	give_get_donation_form( $atts );
88
	$final_output = ob_get_clean();
89
90
	return apply_filters( 'give_donate_form', $final_output, $atts );
91
}
92
93
add_shortcode( 'give_form', 'give_form_shortcode' );
94
95
/**
96
 * Donation Form Goal Shortcode.
97
 *
98
 * Show the Give donation form goals.
99
 *
100
 * @since  1.0
101
 *
102
 * @param  array $atts Shortcode attributes.
103
 *
104
 * @return string
105
 */
106
function give_goal_shortcode( $atts ) {
107
	$atts = shortcode_atts( array(
108
		'id'        => '',
109
		'show_text' => true,
110
		'show_bar'  => true,
111
	), $atts, 'give_goal' );
112
113
114
	//get the Give Form.
115
	ob_start();
116
117
	//Sanity check 1: ensure there is an ID Provided.
118
	if ( empty( $atts['id'] ) ) {
119
		give_output_error( esc_html__( 'The shortcode is missing Donation Form ID attribute.', 'give' ), true );
120
	}
121
122
	//Sanity check 2: Check the form even has Goals enabled.
123
	if ( ! give_is_setting_enabled( get_post_meta( $atts['id'], '_give_goal_option', true ) ) ) {
124
125
		give_output_error( esc_html__( 'The form does not have Goals enabled.', 'give' ), true );
126
	} else {
127
		//Passed all sanity checks: output Goal.
128
		give_show_goal_progress( $atts['id'], $atts );
129
	}
130
131
	$final_output = ob_get_clean();
132
133
	return apply_filters( 'give_goal_shortcode_output', $final_output, $atts );
134
}
135
136
add_shortcode( 'give_goal', 'give_goal_shortcode' );
137
138
139
/**
140
 * Login Shortcode.
141
 *
142
 * Shows a login form allowing users to users to log in. This function simply
143
 * calls the give_login_form function to display the login form.
144
 *
145
 * @since  1.0
146
 *
147
 * @param  array $atts Shortcode attributes.
148
 *
149
 * @uses   give_login_form()
150
 *
151
 * @return string
152
 */
153
function give_login_form_shortcode( $atts ) {
154
	$atts = shortcode_atts( array(
155
		// Add backward compatibility for redirect attribute.
156
		'redirect' => '',
157
158
		'login-redirect'  => '',
159
		'logout-redirect' => '',
160
	), $atts, 'give_login' );
161
162
	// 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.
163
	$atts['login-redirect'] = ! empty( $atts['login-redirect'] ) ? $atts['login-redirect'] : ( ! empty( $atts['redirect'] ) ? $atts['redirect'] : '' );
164
165
	return give_login_form( $atts['login-redirect'], $atts['logout-redirect'] );
166
}
167
168
add_shortcode( 'give_login', 'give_login_form_shortcode' );
169
170
/**
171
 * Register Shortcode.
172
 *
173
 * Shows a registration form allowing users to users to register for the site.
174
 *
175
 * @since  1.0
176
 *
177
 * @param  array $atts Shortcode attributes.
178
 *
179
 * @uses   give_register_form()
180
 *
181
 * @return string
182
 */
183
function give_register_form_shortcode( $atts ) {
184
	$atts = shortcode_atts( array(
185
		'redirect' => '',
186
	), $atts, 'give_register' );
187
188
	return give_register_form( $atts['redirect'] );
189
}
190
191
add_shortcode( 'give_register', 'give_register_form_shortcode' );
192
193
/**
194
 * Receipt Shortcode.
195
 *
196
 * Shows a donation receipt.
197
 *
198
 * @since  1.0
199
 *
200
 * @param  array $atts Shortcode attributes.
201
 *
202
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
203
 */
204
function give_receipt_shortcode( $atts ) {
205
206
	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...
207
208
	$give_receipt_args = shortcode_atts( array(
209
		'error'          => esc_html__( 'You are missing the payment key to view this donation receipt.', 'give' ),
210
		'price'          => true,
211
		'donor'          => true,
212
		'date'           => true,
213
		'payment_key'    => false,
214
		'payment_method' => true,
215
		'payment_id'     => true,
216
		'payment_status' => false,
217
		'status_notice'  => true,
218
	), $atts, 'give_receipt' );
219
220
	//set $session var
221
	$session = give_get_purchase_session();
222
223
	//set payment key var
224
	if ( isset( $_GET['payment_key'] ) ) {
225
		$payment_key = urldecode( $_GET['payment_key'] );
226
	} elseif ( $session ) {
227
		$payment_key = $session['purchase_key'];
228
	} elseif ( $give_receipt_args['payment_key'] ) {
229
		$payment_key = $give_receipt_args['payment_key'];
230
	}
231
232
	$email_access = give_get_option( 'email_access' );
233
234
	// No payment_key found & Email Access is Turned on:
235
	if ( ! isset( $payment_key ) && $email_access == 'on' && ! Give()->email_access->token_exists ) {
236
237
		ob_start();
238
239
		give_get_template_part( 'email-login-form' );
240
241
		return ob_get_clean();
242
243
	} elseif ( ! isset( $payment_key ) ) {
244
245
		return give_output_error( $give_receipt_args['error'], false, 'error' );
246
247
	}
248
249
	$payment_id    = give_get_purchase_id_by_key( $payment_key );
250
	$user_can_view = give_can_view_receipt( $payment_key );
251
252
	// Key was provided, but user is logged out. Offer them the ability to login and view the receipt.
253
	if ( ! $user_can_view && $email_access == 'on' && ! Give()->email_access->token_exists ) {
254
255
		ob_start();
256
257
		give_get_template_part( 'email-login-form' );
258
259
		return ob_get_clean();
260
261
	} elseif ( ! $user_can_view ) {
262
263
		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...
264
265
		$give_login_redirect = give_get_current_page_url();
266
267
		ob_start();
268
269
		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' ) ) );
270
271
		give_get_template_part( 'shortcode', 'login' );
272
273
		$login_form = ob_get_clean();
274
275
		return $login_form;
276
	}
277
278
	/*
279
	 * Check if the user has permission to view the receipt.
280
	 *
281
	 * If user is logged in, user ID is compared to user ID of ID stored in payment meta
282
	 * or if user is logged out and donation was made as a guest, the donation session is checked for
283
	 * or if user is logged in and the user can view sensitive shop data.
284
	 *
285
	 */
286
	if ( ! apply_filters( 'give_user_can_view_receipt', $user_can_view, $give_receipt_args ) ) {
287
		return give_output_error( $give_receipt_args['error'], false, 'error' );
288
	}
289
290
	ob_start();
291
292
	give_get_template_part( 'shortcode', 'receipt' );
293
294
	$display = ob_get_clean();
295
296
	return $display;
297
}
298
299
add_shortcode( 'give_receipt', 'give_receipt_shortcode' );
300
301
/**
302
 * Profile Editor Shortcode.
303
 *
304
 * Outputs the Give Profile Editor to allow users to amend their details from the
305
 * front-end. This function uses the Give templating system allowing users to
306
 * override the default profile editor template. The profile editor template is located
307
 * under templates/profile-editor.php, however, it can be altered by creating a
308
 * file called profile-editor.php in the give_template directory in your active theme's
309
 * folder. Please visit the Give Documentation for more information on how the
310
 * templating system is used.
311
 *
312
 * @since  1.0
313
 *
314
 * @param  array $atts Shortcode attributes.
315
 *
316
 * @return string Output generated from the profile editor
317
 */
318
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...
319
320
	ob_start();
321
322
	give_get_template_part( 'shortcode', 'profile-editor' );
323
324
	$display = ob_get_clean();
325
326
	return $display;
327
}
328
329
add_shortcode( 'give_profile_editor', 'give_profile_editor_shortcode' );
330
331
/**
332
 * Process Profile Updater Form.
333
 *
334
 * Processes the profile updater form by updating the necessary fields.
335
 *
336
 * @since  1.0
337
 *
338
 * @param  array $data Data sent from the profile editor.
339
 *
340
 * @return bool
341
 */
342
function give_process_profile_editor_updates( $data ) {
343
	// Profile field change request
344
	if ( empty( $_POST['give_profile_editor_submit'] ) && ! is_user_logged_in() ) {
345
		return false;
346
	}
347
348
	// Nonce security
349
	if ( ! wp_verify_nonce( $data['give_profile_editor_nonce'], 'give-profile-editor-nonce' ) ) {
350
		return false;
351
	}
352
353
	$user_id       = get_current_user_id();
354
	$old_user_data = get_userdata( $user_id );
355
356
	$display_name = isset( $data['give_display_name'] ) ? sanitize_text_field( $data['give_display_name'] ) : $old_user_data->display_name;
357
	$first_name   = isset( $data['give_first_name'] ) ? sanitize_text_field( $data['give_first_name'] ) : $old_user_data->first_name;
358
	$last_name    = isset( $data['give_last_name'] ) ? sanitize_text_field( $data['give_last_name'] ) : $old_user_data->last_name;
359
	$email        = isset( $data['give_email'] ) ? sanitize_email( $data['give_email'] ) : $old_user_data->user_email;
360
	$line1        = ( isset( $data['give_address_line1'] ) ? sanitize_text_field( $data['give_address_line1'] ) : '' );
361
	$line2        = ( isset( $data['give_address_line2'] ) ? sanitize_text_field( $data['give_address_line2'] ) : '' );
362
	$city         = ( isset( $data['give_address_city'] ) ? sanitize_text_field( $data['give_address_city'] ) : '' );
363
	$state        = ( isset( $data['give_address_state'] ) ? sanitize_text_field( $data['give_address_state'] ) : '' );
364
	$zip          = ( isset( $data['give_address_zip'] ) ? sanitize_text_field( $data['give_address_zip'] ) : '' );
365
	$country      = ( isset( $data['give_address_country'] ) ? sanitize_text_field( $data['give_address_country'] ) : '' );
366
367
	$userdata = array(
368
		'ID'           => $user_id,
369
		'first_name'   => $first_name,
370
		'last_name'    => $last_name,
371
		'display_name' => $display_name,
372
		'user_email'   => $email,
373
	);
374
375
376
	$address = array(
377
		'line1'   => $line1,
378
		'line2'   => $line2,
379
		'city'    => $city,
380
		'state'   => $state,
381
		'zip'     => $zip,
382
		'country' => $country,
383
	);
384
385
	/**
386
	 * Fires before updating user profile.
387
	 *
388
	 * @since 1.0
389
	 *
390
	 * @param int   $user_id  The ID of the user.
391
	 * @param array $userdata User info, including ID, first name, last name, display name and email.
392
	 */
393
	do_action( 'give_pre_update_user_profile', $user_id, $userdata );
394
395
	// New password
396
	if ( ! empty( $data['give_new_user_pass1'] ) ) {
397
		if ( $data['give_new_user_pass1'] !== $data['give_new_user_pass2'] ) {
398
			give_set_error( 'password_mismatch', esc_html__( 'The passwords you entered do not match. Please try again.', 'give' ) );
399
		} else {
400
			$userdata['user_pass'] = $data['give_new_user_pass1'];
401
		}
402
	}
403
404
	if ( empty( $email ) ) {
405
		// Make sure email should not be empty.
406
		give_set_error( 'email_empty', esc_html__( 'The email you entered is empty.', 'give' ) );
407
408
	} else if ( ! is_email( $email ) ) {
409
		// Make sure email should be valid.
410
		give_set_error( 'email_not_valid', esc_html__( 'The email you entered is not valid. Please use another', 'give' ) );
411
412
	} else if ( $email != $old_user_data->user_email ) {
413
		// Make sure the new email doesn't belong to another user
414
		if ( email_exists( $email ) ) {
415
			give_set_error( 'email_exists', esc_html__( 'The email you entered belongs to another user. Please use another.', 'give' ) );
416
		}
417
	}
418
419
	// Check for errors
420
	$errors = give_get_errors();
421
422
	if ( $errors ) {
423
		// Send back to the profile editor if there are errors
424
		wp_redirect( $data['give_redirect'] );
425
		give_die();
426
	}
427
428
	// Update the user
429
	$meta    = update_user_meta( $user_id, '_give_user_address', $address );
430
	$updated = wp_update_user( $userdata );
431
432
	if ( $updated ) {
433
434
		/**
435
		 * Fires after updating user profile.
436
		 *
437
		 * @since 1.0
438
		 *
439
		 * @param int   $user_id  The ID of the user.
440
		 * @param array $userdata User info, including ID, first name, last name, display name and email.
441
		 */
442
		do_action( 'give_user_profile_updated', $user_id, $userdata );
443
		wp_redirect( add_query_arg( 'updated', 'true', $data['give_redirect'] ) );
444
		give_die();
445
	}
446
447
	return false;
448
}
449
450
add_action( 'give_edit_user_profile', 'give_process_profile_editor_updates' );
451