Completed
Pull Request — master (#834)
by Devin
18:40
created

template.php ➔ give_show_goal_progress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 4
b 0
f 1
nc 1
nop 2
dl 0
loc 9
rs 9.6666
ccs 4
cts 5
cp 0.8
crap 1.008
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 Form Template
4
 *
5
 * @package     Give
6
 * @subpackage  Forms
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Get Donation Form
19
 *
20
 * @since 1.0
21
 *
22
 * @param array $args Arguments for display
23
 *
24
 * @return string $purchase_form
25
 */
26
function give_get_donation_form( $args = array() ) {
27
28 1
	global $post;
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...
29
30 1
	$form_id = is_object( $post ) ? $post->ID : 0;
31
32 1
	if ( isset( $args['id'] ) ) {
33 1
		$form_id = $args['id'];
34 1
	}
35
36 1
	$defaults = apply_filters( 'give_form_args_defaults', array(
37
		'form_id' => $form_id
38 1
	) );
39
40 1
	$args = wp_parse_args( $args, $defaults );
41
42 1
	$form = new Give_Donate_Form( $args['form_id'] );
43
44
	//bail if no form ID
45 1
	if ( empty( $form->ID ) ) {
46
		return false;
47
	}
48
49 1
	$payment_mode = give_get_chosen_gateway( $form->ID );
50
51 1
	$form_action = esc_url( add_query_arg( apply_filters( 'give_form_action_args', array(
52 1
		'payment-mode' => $payment_mode,
53 1
	) ),
54 1
		give_get_current_page_url()
55 1
	) );
56
57
	//Sanity Check: Donation form not published or user doesn't have permission to view drafts
58 1
	if ( 'publish' !== $form->post_status && ! current_user_can( 'edit_give_forms', $form->ID ) ) {
59
		return false;
60
	}
61
62 1
	$display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
63 1
		? $args['display_style']
64 1
		: get_post_meta( $form->ID, '_give_payment_display', true );
65
66 1
	$float_labels_option = give_is_float_labels_enabled( $args )
67 1
		? 'float-labels-enabled'
68 1
		: '';
69
70
	//Form Wrap Classes
71 1
	$form_wrap_classes_array = apply_filters( 'give_form_wrap_classes', array(
72 1
		'give-form-wrap',
73
		'give-display-' . $display_option
74 1
	), $form->ID, $args );
75 1
	$form_wrap_classes       = implode( ' ', $form_wrap_classes_array );
76
77
	//Form Classes
78 1
	$form_classes_array = apply_filters( 'give_form_classes', array(
79 1
		'give-form',
80 1
		'give-form-' . $form->ID,
81
        'give-form-type-' . $form->get_type(),
82 1
		$float_labels_option
83 1
	), $form->ID, $args );
84
85
    // Remove empty class names.
86 1
    $form_classes_array = array_filter(
87
        $form_classes_array,
88
        function( $class ){
89
            return $class;
90
        }
91
    );
92
93
	$form_classes       = implode( ' ', $form_classes_array );
94
95
96
	ob_start();
97
98
	/**
99
	 * Fires before the post form outputs.
100
	 *
101
	 * @since 1.0
102
	 *
103
	 * @param int $form ->ID The current form ID
104
	 * @param array $args An array of form args
105
	 */
106
	do_action( 'give_pre_form_output', $form->ID, $args ); ?>
107
108
	<div id="give-form-<?php echo $form->ID; ?>-wrap" class="<?php echo $form_wrap_classes; ?>">
109
110
		<?php if ( $form->is_close_donation_form() ) {
111 1
112
			//Get Goal thank you message.
113
			$display_thankyou_message = get_post_meta( $form->ID, '_give_form_goal_achieved_message', true );
114
			$display_thankyou_message = ! empty( $display_thankyou_message ) ? $display_thankyou_message : esc_html__( 'Thank you to all our donors, we have met our fundraising goal.', 'give' );
115
116
			//Print thank you message.
117
			apply_filters( 'give_goal_closed_output', give_output_error( $display_thankyou_message, true, 'success' ) );
118
119
		} else {
120
121
			if ( isset( $args['show_title'] ) && $args['show_title'] == true ) {
122
123
				echo apply_filters( 'give_form_title', '<h2 class="give-form-title">' . get_the_title( $form_id ) . '</h2>' );
124
125
			}
126
127
			do_action( 'give_pre_form', $form->ID, $args ); ?>
128
129
			<form id="give-form-<?php echo $form_id; ?>" class="<?php echo $form_classes; ?>" action="<?php echo $form_action; ?>" method="post">
130
				<input type="hidden" name="give-form-id" value="<?php echo $form->ID; ?>"/>
131
				<input type="hidden" name="give-form-title" value="<?php echo htmlentities( $form->post_title ); ?>"/>
132
				<input type="hidden" name="give-current-url" value="<?php echo htmlspecialchars( give_get_current_page_url() ); ?>"/>
133
				<input type="hidden" name="give-form-url" value="<?php echo htmlspecialchars( give_get_current_page_url() ); ?>"/>
134
				<input type="hidden" name="give-form-minimum" value="<?php echo give_format_amount( give_get_form_minimum_price( $form->ID ) ); ?>"/>
135 1
136
				<!-- The following field is for robots only, invisible to humans: -->
137 1
				<span class="give-hidden" style="display: none !important;">
138 1
					<label for="give-form-honeypot-<?php echo $form_id; ?>"></label>
139
					<input id="give-form-honeypot-<?php echo $form_id; ?>" type="text" name="give-honeypot" class="give-honeypot give-hidden"/>
140 1
				</span>
141 1
142 1
				<?php
143 1
144 1
				//Price ID hidden field for variable (mult-level) donation forms
145
				if ( give_has_variable_prices( $form_id ) ) {
146
					//get default selected price ID
147
					$prices   = apply_filters( 'give_form_variable_prices', give_get_variable_prices( $form_id ), $form_id );
148
					$price_id = 0;
149 1
					//loop through prices
150
					foreach ( $prices as $price ) {
151 1
						if ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) {
152
							$price_id = $price['_give_id']['level_id'];
153 1
						};
154
					}
155
					?>
156
					<input type="hidden" name="give-price-id" value="<?php echo $price_id; ?>"/>
157
				<?php }
158
159
				do_action( 'give_checkout_form_top', $form->ID, $args );
160
161
				do_action( 'give_payment_mode_select', $form->ID, $args );
162
163
				do_action( 'give_checkout_form_bottom', $form->ID, $args );
164
165
				?>
166
			</form>
167
168
			<?php do_action( 'give_post_form', $form->ID, $args ); ?>
169
170
		<?php } ?>
171
172
		<!--end #give-form-<?php echo absint( $form->ID ); ?>--></div>
173 1
	<?php
174
175 1
	/**
176
	 * Fires after the post form outputs.
177 1
	 *
178 1
	 * @since 1.0
179
	 *
180
	 * @param int $form ->ID The current form ID
181
	 * @param array $args An array of form args
182
	 */
183
	do_action( 'give_post_form_output', $form->ID, $args );
184
185
	$final_output = ob_get_clean();
186
187
	echo apply_filters( 'give_donate_form', $final_output, $args );
188
}
189
190
191
/**
192
 *
193
 * Give Show Purchase Form
194
 *
195
 * Renders the Donation Form, hooks are provided to add to the checkout form.
196
 * The default Donation Form rendered displays a list of the enabled payment
197
 * gateways, a user registration form (if enable) and a credit card info form
198
 * if credit cards are enabled
199 1
 *
200
 * @since 1.0
201 1
 *
202
 * @param  int $form_id ID of the Give Form
203
 *
204
 * @return string
205 1
 */
206
function give_show_purchase_form( $form_id ) {
207 1
208
	$payment_mode = give_get_chosen_gateway( $form_id );
209 1
210
	if ( ! isset( $form_id ) && isset( $_POST['give_form_id'] ) ) {
211 1
		$form_id = $_POST['give_form_id'];
212
	}
213 1
214
	do_action( 'give_purchase_form_top', $form_id );
215
216 1
	if ( give_can_checkout() && isset( $form_id ) ) {
217 1
218 1
		do_action( 'give_purchase_form_before_register_login', $form_id );
219
220
		do_action( 'give_purchase_form_register_login_fields', $form_id );
221
222 1
		do_action( 'give_purchase_form_before_cc_form', $form_id );
223
224 1
		// Load the credit card form and allow gateways to load their own if they wish
225
		if ( has_action( 'give_' . $payment_mode . '_cc_form' ) ) {
226
			do_action( 'give_' . $payment_mode . '_cc_form', $form_id );
227
		} else {
228
			do_action( 'give_cc_form', $form_id );
229
		}
230 1
231 1
		do_action( 'give_purchase_form_after_cc_form', $form_id );
232
233
	} else {
234
		// Can't checkout
235
		do_action( 'give_purchase_form_no_access', $form_id );
236
237
	}
238
239
	do_action( 'give_purchase_form_bottom', $form_id );
240
}
241
242
add_action( 'give_purchase_form', 'give_show_purchase_form' );
243
244
/**
245
 *
246
 * Give Show Login/Register Form Fields
247 1
 *
248
 * @since  1.4.1
249
 *
250
 * @param  int   $form_id ID of the Give Form
251
 *
252
 * @return void
253
 */
254
function give_show_register_login_fields( $form_id ) {
255
256
	$show_register_form = give_show_login_register_option( $form_id );
257
258
	if ( ( $show_register_form === 'registration' || ( $show_register_form === 'both' && ! isset( $_GET['login'] ) ) ) && ! is_user_logged_in() ) : ?>
259
		<div id="give-checkout-login-register-<?php echo $form_id; ?>">
260
			<?php do_action( 'give_purchase_form_register_fields', $form_id ); ?>
261
		</div>
262 1
	<?php elseif ( ( $show_register_form === 'login' || ( $show_register_form === 'both' && isset( $_GET['login'] ) ) ) && ! is_user_logged_in() ) : ?>
263
		<div id="give-checkout-login-register-<?php echo $form_id; ?>">
264
			<?php do_action( 'give_purchase_form_login_fields', $form_id ); ?>
265
		</div>
266
	<?php endif; ?>
267
268
	<?php if ( ( ! isset( $_GET['login'] ) && is_user_logged_in() ) || ! isset( $show_register_form ) || 'none' === $show_register_form || 'login' === $show_register_form ) {
269
		do_action( 'give_purchase_form_after_user_info', $form_id );
270
	}
271
}
272
273
add_action( 'give_purchase_form_register_login_fields', 'give_show_register_login_fields' );
274
275
/**
276
 * Donation Amount Field
277
 *
278
 * Outputs the donation amount field that appears at the top of the donation forms. If the user has custom amount enabled the field will output as a customizable input
279
 *
280 1
 * @since  1.0
281
 *
282 1
 * @param  int   $form_id Give Form ID
283 1
 * @param  array $args
284 1
 *
285 1
 * @return void
286 1
 */
287 1
function give_output_donation_amount_top( $form_id = 0, $args = array() ) {
288 1
289
	global $give_options;
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...
290 1
291
	$variable_pricing    = give_has_variable_prices( $form_id );
292
	$allow_custom_amount = get_post_meta( $form_id, '_give_custom_amount', true );
293 1
	$currency_position   = isset( $give_options['currency_position'] ) ? $give_options['currency_position'] : 'before';
294
	$symbol              = give_currency_symbol( give_get_currency() );
295
	$currency_output     = '<span class="give-currency-symbol give-currency-position-' . $currency_position . '">' . $symbol . '</span>';
296
	$default_amount      = give_format_amount( give_get_default_form_amount( $form_id ) );
297
	$custom_amount_text  = get_post_meta( $form_id, '_give_custom_amount_text', true );
298
299
	do_action( 'give_before_donation_levels', $form_id, $args );
300
301
	//Set Price, No Custom Amount Allowed means hidden price field
302
	if ( $allow_custom_amount == 'no' ) {
303
		?>
304
305
		<label class="give-hidden" for="give-amount-hidden"><?php echo esc_html__( 'Donation Amount:', 'give' ); ?></label>
306
		<input id="give-amount" class="give-amount-hidden" type="hidden" name="give-amount"
307
		       value="<?php echo $default_amount; ?>" required>
308
		<div class="set-price give-donation-amount form-row-wide">
309
			<?php if ( $currency_position == 'before' ) {
310
				echo $currency_output;
311
			} ?>
312
			<span id="give-amount-text" class="give-text-input give-amount-top"><?php echo $default_amount; ?></span>
313
			<?php if ( $currency_position == 'after' ) {
314
				echo $currency_output;
315 1
			} ?>
316
		</div>
317
		<?php
318
	} else {
319
		//Custom Amount Allowed
320
		?>
321
		<div class="give-total-wrap">
322
			<div class="give-donation-amount form-row-wide">
323
				<?php if ( $currency_position == 'before' ) {
324
					echo $currency_output;
325
				} ?>
326 1
				<label class="give-hidden" for="give-amount"><?php echo esc_html__( 'Donation Amount:', 'give' ); ?></label>
327
				<input class="give-text-input give-amount-top" id="give-amount" name="give-amount" type="tel" placeholder="" value="<?php echo $default_amount; ?>" autocomplete="off">
328
				<?php if ( $currency_position == 'after' ) {
329
					echo $currency_output;
330
				} ?>
331
			</div>
332
		</div>
333
	<?php }
334 1
335 1
	do_action( 'give_after_donation_amount', $form_id, $args );
336 1
337
	//Custom Amount Text
338 1
	if ( ! $variable_pricing && $allow_custom_amount == 'yes' && ! empty( $custom_amount_text ) ) { ?>
339 1
		<p class="give-custom-amount-text"><?php echo $custom_amount_text; ?></p>
340
	<?php }
341
342
	//Output Variable Pricing Levels
343
	if ( $variable_pricing ) {
344
		give_output_levels( $form_id );
345
	}
346
347
	do_action( 'give_after_donation_levels', $form_id, $args );
348
}
349
350
add_action( 'give_checkout_form_top', 'give_output_donation_amount_top', 10, 2 );
351
352
353
/**
354
 * Outputs the Donation Levels in various formats such as dropdown, radios, and buttons
355
 *
356 1
 * @since  1.0
357 1
 *
358 1
 * @param  int $form_id Give Form ID
359 1
 *
360 1
 * @return string
361 1
 */
362 1
function give_output_levels( $form_id ) {
363
364 1
	//Get variable pricing
365 1
	$prices             = apply_filters( 'give_form_variable_prices', give_get_variable_prices( $form_id ), $form_id );
366
	$display_style      = get_post_meta( $form_id, '_give_display_style', true );
367
	$custom_amount      = get_post_meta( $form_id, '_give_custom_amount', true );
368 1
	$custom_amount_text = get_post_meta( $form_id, '_give_custom_amount_text', true );
369
	if ( empty( $custom_amount_text ) ) {
370 1
		$custom_amount_text = esc_html__( 'Give a Custom Amount', 'give' );
371
	}
372 1
373 1
	$output  = '';
374 1
	$counter = 0;
375 1
376
	switch ( $display_style ) {
377 1
		case 'buttons':
378 1
379 1
			$output .= '<ul id="give-donation-level-button-wrap" class="give-donation-levels-wrap give-list-inline">';
380 1
381 1
			foreach ( $prices as $price ) {
382
				$counter ++;
383 1
				$level_text    = apply_filters( 'give_form_level_text', ! empty( $price['_give_text'] ) ? $price['_give_text'] : give_currency_filter( give_format_amount( $price['_give_amount'] ) ), $form_id, $price );
384
				$level_classes = apply_filters( 'give_form_level_classes', 'give-donation-level-btn give-btn give-btn-level-' . $counter . ' ' . ( ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) ? 'give-default-level' : '' ), $form_id, $price );
385
386 1
				$output .= '<li>';
387
				$output .= '<button type="button" data-price-id="' . $price['_give_id']['level_id'] . '" class=" ' . $level_classes . '" value="' . give_format_amount( $price['_give_amount'] ) . '">';
388
				$output .= $level_text;
389
				$output .= '</button>';
390
				$output .= '</li>';
391
392
			}
393
394 1
			//Custom Amount
395
			if ( $custom_amount === 'yes' && ! empty( $custom_amount_text ) ) {
396 1
				$output .= '<li>';
397
				$output .= '<button type="button" data-price-id="custom" class="give-donation-level-btn give-btn give-btn-level-custom" value="custom">';
398
				$output .= $custom_amount_text;
399
				$output .= '</button>';
400
				$output .= '</li>';
401
			}
402
403
			$output .= '</ul>';
404
405
			break;
406
407
		case 'radios':
408
409
			$output .= '<ul id="give-donation-level-radio-list" class="give-donation-levels-wrap">';
410
411
			foreach ( $prices as $price ) {
412
				$counter ++;
413
				$level_text    = apply_filters( 'give_form_level_text', ! empty( $price['_give_text'] ) ? $price['_give_text'] : give_currency_filter( give_format_amount( $price['_give_amount'] ) ), $form_id, $price );
414
				$level_classes = apply_filters( 'give_form_level_classes', 'give-radio-input give-radio-input-level give-radio-level-' . $counter . ( ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) ? ' give-default-level' : '' ), $form_id, $price );
415
416
				$output .= '<li>';
417
				$output .= '<input type="radio" data-price-id="' . $price['_give_id']['level_id'] . '" class="' . $level_classes . '" name="give-radio-donation-level" id="give-radio-level-' . $counter . '" ' . ( ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) ? 'checked="checked"' : '' ) . ' value="' . give_format_amount( $price['_give_amount'] ) . '">';
418
				$output .= '<label for="give-radio-level-' . $counter . '">' . $level_text . '</label>';
419
				$output .= '</li>';
420
421
			}
422
423
			//Custom Amount
424
			if ( $custom_amount === 'yes' && ! empty( $custom_amount_text ) ) {
425
				$output .= '<li>';
426
				$output .= '<input type="radio" data-price-id="custom" class="give-radio-input give-radio-input-level give-radio-level-custom" name="give-radio-donation-level" id="give-radio-level-custom" value="custom">';
427
				$output .= '<label for="give-radio-level-custom">' . $custom_amount_text . '</label>';
428
				$output .= '</li>';
429
			}
430
431
			$output .= '</ul>';
432
433
			break;
434
435
		case 'dropdown':
436
437
			$output .= '<label for="give-donation-level" class="give-hidden">' . esc_html__( 'Choose Your Donation Amount', 'give' ) . ':</label>';
438
			$output .= '<select id="give-donation-level-' . $form_id . '" class="give-select give-select-level give-donation-levels-wrap">';
439
440
			//first loop through prices
441
			foreach ( $prices as $price ) {
442
				$level_text    = apply_filters( 'give_form_level_text', ! empty( $price['_give_text'] ) ? $price['_give_text'] : give_currency_filter( give_format_amount( $price['_give_amount'] ) ), $form_id, $price );
443
				$level_classes = apply_filters( 'give_form_level_classes', 'give-donation-level-' . $form_id . ( ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) ? ' give-default-level' : '' ), $form_id, $price );
444
445
				$output .= '<option data-price-id="' . $price['_give_id']['level_id'] . '" class="' . $level_classes . '" ' . ( ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) ? 'selected="selected"' : '' ) . ' value="' . give_format_amount( $price['_give_amount'] ) . '">';
446
				$output .= $level_text;
447
				$output .= '</option>';
448
449
			}
450
451
			//Custom Amount
452 1
			if ( $custom_amount === 'yes' && ! empty( $custom_amount_text ) ) {
453 1
				$output .= '<option data-price-id="custom" class="give-donation-level-custom" value="custom">' . $custom_amount_text . '</option>';
454
			}
455
456
			$output .= '</select>';
457
458
			break;
459
	}
460
461
	echo apply_filters( 'give_form_level_output', $output, $form_id );
462
}
463
464
/**
465
 * Display Reveal & Lightbox Button
466 1
 *
467 1
 * Outputs a button to reveal form fields
468 1
 *
469
 * @param int   $form_id
470
 * @param array $args
471 1
 *
472
 */
473
function give_display_checkout_button( $form_id, $args ) {
474
475 1
	$display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
476 1
		? $args['display_style']
477
		: get_post_meta( $form_id, '_give_payment_display', true );
478 1
479
	//no btn for onpage
480 1
	if ( $display_option === 'onpage' ) {
481 1
		return;
482
	}
483
484
	$display_label_field = get_post_meta( $form_id, '_give_reveal_label', true );
485
	$display_label       = ( ! empty( $display_label_field ) ? $display_label_field : esc_html__( 'Donate Now', 'give' ) );
486
487
	$output = '<button type="button" class="give-btn give-btn-' . $display_option . '">' . $display_label . '</button>';
488
489
	echo apply_filters( 'give_display_checkout_button', $output );
490
}
491
492
add_action( 'give_after_donation_levels', 'give_display_checkout_button', 10, 2 );
493
494
/**
495
 * Shows the User Info fields in the Personal Info box, more fields can be added via the hooks provided.
496
 *
497
 * @since  1.0
498
 *
499
 * @param  int $form_id
500
 *
501
 * @return void
502
 */
503
function give_user_info_fields( $form_id ) {
504
505
	if ( is_user_logged_in() ) :
506
		$user_data = get_userdata( get_current_user_id() );
507
	endif;
508
509
	do_action( 'give_purchase_form_before_personal_info', $form_id );
510
	?>
511
	<fieldset id="give_checkout_user_info">
512
		<legend><?php echo apply_filters( 'give_checkout_personal_info_text', esc_html__( 'Personal Info', 'give' ) ); ?></legend>
513
		<p id="give-first-name-wrap" class="form-row form-row-first">
514
			<label class="give-label" for="give-first">
515
				<?php esc_html_e( 'First Name', 'give' ); ?>
516
				<?php if ( give_field_is_required( 'give_first', $form_id ) ) { ?>
517
					<span class="give-required-indicator">*</span>
518
				<?php } ?>
519
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'We will use this to personalize your account experience.', 'give' ); ?>"></span>
520
			</label>
521
			<input class="give-input required" type="text" name="give_first" placeholder="<?php esc_html_e( 'First Name', 'give' ); ?>" id="give-first" value="<?php echo is_user_logged_in() ? $user_data->first_name : ''; ?>"<?php if ( give_field_is_required( 'give_first', $form_id ) ) {
0 ignored issues
show
Bug introduced by
The variable $user_data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
522
				echo ' required ';
523
			} ?>/>
524
		</p>
525
526
		<p id="give-last-name-wrap" class="form-row form-row-last">
527
			<label class="give-label" for="give-last">
528
				<?php esc_html_e( 'Last Name', 'give' ); ?>
529
				<?php if ( give_field_is_required( 'give_last', $form_id ) ) { ?>
530
					<span class="give-required-indicator">*</span>
531
				<?php } ?>
532
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'We will use this as well to personalize your account experience.', 'give' ); ?>"></span>
533
			</label>
534
535
			<input class="give-input<?php if ( give_field_is_required( 'give_last', $form_id ) ) {
536
				echo ' required';
537
			} ?>" type="text" name="give_last" id="give-last" placeholder="<?php esc_html_e( 'Last Name', 'give' ); ?>" value="<?php echo is_user_logged_in() ? $user_data->last_name : ''; ?>"<?php if ( give_field_is_required( 'give_last', $form_id ) ) {
538
				echo ' required ';
539
			} ?> />
540
		</p>
541
542
		<?php do_action( 'give_purchase_form_before_email', $form_id ); ?>
543
		<p id="give-email-wrap" class="form-row form-row-wide">
544
			<label class="give-label" for="give-email">
545
				<?php esc_html_e( 'Email Address', 'give' ); ?>
546
				<?php if ( give_field_is_required( 'give_email', $form_id ) ) { ?>
547
					<span class="give-required-indicator">*</span>
548
				<?php } ?>
549
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'We will send the purchase receipt to this address.', 'give' ); ?>"></span>
550
			</label>
551
552
			<input class="give-input required" type="email" name="give_email" placeholder="<?php esc_html_e( 'Email address', 'give' ); ?>" id="give-email" value="<?php echo is_user_logged_in() ? $user_data->user_email : ''; ?>"<?php if ( give_field_is_required( 'give_email', $form_id ) ) {
553
				echo ' required ';
554
			} ?>/>
555
556
		</p>
557
		<?php do_action( 'give_purchase_form_after_email', $form_id ); ?>
558
559
		<?php do_action( 'give_purchase_form_user_info', $form_id ); ?>
560
	</fieldset>
561
	<?php
562
	do_action( 'give_purchase_form_after_personal_info', $form_id );
563
564
}
565
566
add_action( 'give_purchase_form_after_user_info', 'give_user_info_fields' );
567
add_action( 'give_register_fields_before', 'give_user_info_fields' );
568
569
/**
570
 * Renders the credit card info form.
571
 *
572
 * @since  1.0
573
 *
574
 * @param  int $form_id
575
 *
576
 * @return void
577
 */
578
function give_get_cc_form( $form_id ) {
579
580
	ob_start();
581
582
	do_action( 'give_before_cc_fields', $form_id ); ?>
583
584
	<fieldset id="give_cc_fields-<?php echo $form_id ?>" class="give-do-validate">
585
		<legend><?php echo apply_filters( 'give_credit_card_fieldset_heading', esc_html__( 'Credit Card Info', 'give' ) ); ?></legend>
586
		<?php if ( is_ssl() ) : ?>
587
			<div id="give_secure_site_wrapper-<?php echo $form_id ?>">
588
				<span class="give-icon padlock"></span>
589
				<span><?php esc_html_e( 'This is a secure SSL encrypted payment.', 'give' ); ?></span>
590
			</div>
591
		<?php endif; ?>
592
		<p id="give-card-number-wrap-<?php echo $form_id ?>" class="form-row form-row-two-thirds">
593
			<label for="card_number-<?php echo $form_id ?>" class="give-label">
594
				<?php esc_html_e( 'Card Number', 'give' ); ?>
595
				<span class="give-required-indicator">*</span>
596
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The (typically) 16 digits on the front of your credit card.', 'give' ); ?>"></span>
597
				<span class="card-type"></span>
598
			</label>
599
600
			<input type="tel" autocomplete="off" name="card_number" id="card_number-<?php echo $form_id ?>" class="card-number give-input required" placeholder="<?php esc_html_e( 'Card number', 'give' ); ?>" required/>
601
		</p>
602
603
		<p id="give-card-cvc-wrap-<?php echo $form_id ?>" class="form-row form-row-one-third">
604
			<label for="card_cvc-<?php echo $form_id ?>" class="give-label">
605
				<?php esc_html_e( 'CVC', 'give' ); ?>
606
				<span class="give-required-indicator">*</span>
607
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The 3 digit (back) or 4 digit (front) value on your card.', 'give' ); ?>"></span>
608
			</label>
609
610
			<input type="tel" size="4" autocomplete="off" name="card_cvc" id="card_cvc-<?php echo $form_id ?>" class="card-cvc give-input required" placeholder="<?php esc_html_e( 'Security code', 'give' ); ?>" required/>
611
		</p>
612
613
		<p id="give-card-name-wrap-<?php echo $form_id ?>" class="form-row form-row-two-thirds">
614
			<label for="card_name-<?php echo $form_id ?>" class="give-label">
615
				<?php esc_html_e( 'Name on the Card', 'give' ); ?>
616
				<span class="give-required-indicator">*</span>
617
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The name printed on the front of your credit card.', 'give' ); ?>"></span>
618
			</label>
619
620
			<input type="text" autocomplete="off" name="card_name" id="card_name-<?php echo $form_id ?>" class="card-name give-input required" placeholder="<?php esc_html_e( 'Card name', 'give' ); ?>" required/>
621
		</p>
622
		<?php do_action( 'give_before_cc_expiration' ); ?>
623
		<p class="card-expiration form-row form-row-one-third">
624
			<label for="card_expiry-<?php echo $form_id ?>" class="give-label">
625
				<?php esc_html_e( 'Expiration', 'give' ); ?>
626
				<span class="give-required-indicator">*</span>
627
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The date your credit card expires, typically on the front of the card.', 'give' ); ?>"></span>
628
			</label>
629
630
			<input type="hidden" id="card_exp_month-<?php echo $form_id ?>" name="card_exp_month" class="card-expiry-month"/>
631
			<input type="hidden" id="card_exp_year-<?php echo $form_id ?>" name="card_exp_year" class="card-expiry-year"/>
632
633
			<input type="tel" autocomplete="off" name="card_expiry" id="card_expiry-<?php echo $form_id ?>" class="card-expiry give-input required" placeholder="<?php esc_html_e( 'MM / YY', 'give' ); ?>" required/>
634
		</p>
635
		<?php do_action( 'give_after_cc_expiration', $form_id ); ?>
636
637
	</fieldset>
638
	<?php
639
	do_action( 'give_after_cc_fields', $form_id );
640
641
	echo ob_get_clean();
642
}
643
644
add_action( 'give_cc_form', 'give_get_cc_form' );
645
646
/**
647
 * Outputs the default credit card address fields
648
 *
649
 * @since  1.0
650
 *
651
 * @param  int $form_id
652
 *
653
 * @return void
654
 */
655
function give_default_cc_address_fields( $form_id ) {
656
657
	$logged_in = is_user_logged_in();
658
659
	if ( $logged_in ) {
660
		$user_address = get_user_meta( get_current_user_id(), '_give_user_address', true );
661
	}
662
	$line1 = $logged_in && ! empty( $user_address['line1'] ) ? $user_address['line1'] : '';
663
	$line2 = $logged_in && ! empty( $user_address['line2'] ) ? $user_address['line2'] : '';
664
	$city  = $logged_in && ! empty( $user_address['city'] ) ? $user_address['city'] : '';
665
	$zip   = $logged_in && ! empty( $user_address['zip'] ) ? $user_address['zip'] : '';
666
	ob_start(); ?>
667
	<fieldset id="give_cc_address" class="cc-address">
668
		<legend><?php echo apply_filters( 'give_billing_details_fieldset_heading', esc_html__( 'Billing Details', 'give' ) ); ?></legend>
669
		<?php do_action( 'give_cc_billing_top' ); ?>
670
		<p id="give-card-address-wrap" class="form-row form-row-two-thirds">
671
			<label for="card_address" class="give-label">
672
				<?php esc_html_e( 'Address', 'give' ); ?>
673
				<?php
674
				if ( give_field_is_required( 'card_address', $form_id ) ) { ?>
675
					<span class="give-required-indicator">*</span>
676
				<?php } ?>
677
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The primary billing address for your credit card.', 'give' ); ?>"></span>
678
			</label>
679
680
			<input type="text" id="card_address" name="card_address" class="card-address give-input<?php if ( give_field_is_required( 'card_address', $form_id ) ) {
681
				echo ' required';
682
			} ?>" placeholder="<?php esc_html_e( 'Address line 1', 'give' ); ?>" value="<?php echo $line1; ?>"<?php if ( give_field_is_required( 'card_address', $form_id ) ) {
683
				echo '  required ';
684
			} ?>/>
685
		</p>
686
687
		<p id="give-card-address-2-wrap" class="form-row form-row-one-third">
688
			<label for="card_address_2" class="give-label">
689
				<?php esc_html_e( 'Address Line 2', 'give' ); ?>
690
				<?php if ( give_field_is_required( 'card_address_2', $form_id ) ) { ?>
691
					<span class="give-required-indicator">*</span>
692
				<?php } ?>
693
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( '(optional) The suite, apt no, PO box, etc, associated with your billing address.', 'give' ); ?>"></span>
694
			</label>
695
696
			<input type="text" id="card_address_2" name="card_address_2" class="card-address-2 give-input<?php if ( give_field_is_required( 'card_address_2', $form_id ) ) {
697
				echo ' required';
698
			} ?>" placeholder="<?php esc_html_e( 'Address line 2', 'give' ); ?>" value="<?php echo $line2; ?>"<?php if ( give_field_is_required( 'card_address_2', $form_id ) ) {
699
				echo ' required ';
700
			} ?>/>
701
		</p>
702
703
		<p id="give-card-city-wrap" class="form-row form-row-two-thirds">
704
			<label for="card_city" class="give-label">
705
				<?php esc_html_e( 'City', 'give' ); ?>
706
				<?php if ( give_field_is_required( 'card_city', $form_id ) ) { ?>
707
					<span class="give-required-indicator">*</span>
708
				<?php } ?>
709
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The city for your billing address.', 'give' ); ?>"></span>
710
			</label>
711
			<input type="text" id="card_city" name="card_city" class="card-city give-input<?php if ( give_field_is_required( 'card_city', $form_id ) ) {
712
				echo ' required';
713
			} ?>" placeholder="<?php esc_html_e( 'City', 'give' ); ?>" value="<?php echo $city; ?>"<?php if ( give_field_is_required( 'card_city', $form_id ) ) {
714
				echo ' required ';
715
			} ?>/>
716
		</p>
717
718
		<p id="give-card-zip-wrap" class="form-row form-row-one-third">
719
			<label for="card_zip" class="give-label">
720
				<?php esc_html_e( 'Zip / Postal Code', 'give' ); ?>
721
				<?php if ( give_field_is_required( 'card_zip', $form_id ) ) { ?>
722
					<span class="give-required-indicator">*</span>
723
				<?php } ?>
724
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The zip or postal code for your billing address.', 'give' ); ?>"></span>
725
			</label>
726
727
			<input type="text" size="4" id="card_zip" name="card_zip" class="card-zip give-input<?php if ( give_field_is_required( 'card_zip', $form_id ) ) {
728
				echo ' required';
729
			} ?>" placeholder="<?php esc_html_e( 'Zip / Postal Code', 'give' ); ?>" value="<?php echo $zip; ?>" <?php if ( give_field_is_required( 'card_zip', $form_id ) ) {
730
				echo ' required ';
731
			} ?>/>
732
		</p>
733
734
		<p id="give-card-country-wrap" class="form-row form-row-first">
735
			<label for="billing_country" class="give-label">
736
				<?php esc_html_e( 'Country', 'give' ); ?>
737
				<?php if ( give_field_is_required( 'billing_country', $form_id ) ) { ?>
738
					<span class="give-required-indicator">*</span>
739
				<?php } ?>
740
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The country for your billing address.', 'give' ); ?>"></span>
741
			</label>
742
743
			<select name="billing_country" id="billing_country" class="billing-country billing_country give-select<?php if ( give_field_is_required( 'billing_country', $form_id ) ) {
744
				echo ' required';
745
			} ?>"<?php if ( give_field_is_required( 'billing_country', $form_id ) ) {
746
				echo ' required ';
747
			} ?>>
748
				<?php
749
750
				$selected_country = give_get_country();
751
752
				if ( $logged_in && ! empty( $user_address['country'] ) && '*' !== $user_address['country'] ) {
753
					$selected_country = $user_address['country'];
754
				}
755
756
				$countries = give_get_country_list();
757
				foreach ( $countries as $country_code => $country ) {
758
					echo '<option value="' . esc_attr( $country_code ) . '"' . selected( $country_code, $selected_country, false ) . '>' . $country . '</option>';
759
				}
760
				?>
761
			</select>
762
		</p>
763
764
		<p id="give-card-state-wrap" class="form-row form-row-last">
765
			<label for="card_state" class="give-label">
766
				<?php esc_html_e( 'State / Province', 'give' ); ?>
767
				<?php if ( give_field_is_required( 'card_state', $form_id ) ) { ?>
768
					<span class="give-required-indicator">*</span>
769
				<?php } ?>
770
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The state or province for your billing address.', 'give' ); ?>"></span>
771
			</label>
772
773
			<?php
774
			$selected_state = give_get_state();
775
			$states         = give_get_states( $selected_country );
776
777
			if ( $logged_in && ! empty( $user_address['state'] ) ) {
778
				$selected_state = $user_address['state'];
779
			}
780
781
			if ( ! empty( $states ) ) : ?>
782
				<select name="card_state" id="card_state" class="card_state give-select<?php if ( give_field_is_required( 'card_state', $form_id ) ) {
783
					echo ' required';
784
				} ?>"<?php if ( give_field_is_required( 'card_state', $form_id ) ) {
785
					echo ' required ';
786
				} ?>>
787
					<?php
788
					foreach ( $states as $state_code => $state ) {
789
						echo '<option value="' . $state_code . '"' . selected( $state_code, $selected_state, false ) . '>' . $state . '</option>';
790
					}
791
					?>
792
				</select>
793
			<?php else : ?>
794
				<input type="text" size="6" name="card_state" id="card_state" class="card_state give-input" placeholder="<?php esc_html_e( 'State / Province', 'give' ); ?>"/>
795
			<?php endif; ?>
796
		</p>
797
		<?php do_action( 'give_cc_billing_bottom' ); ?>
798
	</fieldset>
799
	<?php
800
	echo ob_get_clean();
801
}
802
803
add_action( 'give_after_cc_fields', 'give_default_cc_address_fields' );
804
805
806
/**
807
 * Renders the user registration fields. If the user is logged in, a login form is displayed other a registration form is provided for the user to create an account.
808
 *
809
 * @since  1.0
810
 *
811
 * @param  int $form_id
812
 *
813
 * @return string
814
 */
815
function give_get_register_fields( $form_id ) {
816
817
	global $user_ID;
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...
818
819
	if ( is_user_logged_in() ) {
820
		$user_data = get_userdata( $user_ID );
821
	}
822
823
	$show_register_form = give_show_login_register_option( $form_id );
824
825
	ob_start(); ?>
826
	<fieldset id="give-register-fields-<?php echo $form_id; ?>">
827
828
		<?php if ( $show_register_form == 'both' ) { ?>
829
			<div class="give-login-account-wrap">
830
				<p class="give-login-message"><?php esc_html_e( 'Already have an account?', 'give' ); ?>&nbsp;
831
					<a href="<?php echo esc_url( add_query_arg( 'login', 1 ) ); ?>" class="give-checkout-login" data-action="give_checkout_login"><?php esc_html_e( 'Login', 'give' ); ?></a>
832
				</p>
833
				<p class="give-loading-text">
834
					<span class="give-loading-animation"></span> <?php esc_html_e( 'Loading...', 'give' ); ?></p>
835
			</div>
836
		<?php } ?>
837
838
		<?php do_action( 'give_register_fields_before', $form_id ); ?>
839
840
		<fieldset id="give-register-account-fields-<?php echo $form_id; ?>">
841
			<legend><?php echo apply_filters( 'give_create_account_fieldset_heading', esc_html__( 'Create an account', 'give' ) );
842
				if ( ! give_logged_in_only( $form_id ) ) {
843
					echo ' <span class="sub-text">' . esc_html__( '(optional)', 'give' ) . '</span>';
844
				} ?></legend>
845
			<?php do_action( 'give_register_account_fields_before', $form_id ); ?>
846
			<div id="give-user-login-wrap-<?php echo $form_id; ?>" class="form-row form-row-one-third form-row-first">
847
				<label for="give-user-login-<?php echo $form_id; ?>">
848
					<?php esc_html_e( 'Username', 'give' ); ?>
849
					<?php if ( give_logged_in_only( $form_id ) ) { ?>
850
						<span class="give-required-indicator">*</span>
851
					<?php } ?>
852
					<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The username you will use to log into your account.', 'give' ); ?>"></span>
853
				</label>
854
855
				<input name="give_user_login" id="give-user-login-<?php echo $form_id; ?>" class="<?php if ( give_logged_in_only( $form_id ) ) {
856
					echo 'required ';
857
				} ?>give-input" type="text" placeholder="<?php esc_html_e( 'Username', 'give' ); ?>" title="<?php esc_html_e( 'Username', 'give' ); ?>"/>
858
			</div>
859
860
			<div id="give-user-pass-wrap-<?php echo $form_id; ?>" class="form-row form-row-one-third">
861
				<label for="give-user-pass-<?php echo $form_id; ?>">
862
					<?php esc_html_e( 'Password', 'give' ); ?>
863
					<?php if ( give_logged_in_only( $form_id ) ) { ?>
864
						<span class="give-required-indicator">*</span>
865
					<?php } ?>
866
					<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'The password used to access your account.', 'give' ); ?>"></span>
867
				</label>
868
869
				<input name="give_user_pass" id="give-user-pass-<?php echo $form_id; ?>" class="<?php if ( give_logged_in_only( $form_id ) ) {
870
					echo 'required ';
871
				} ?>give-input" placeholder="<?php esc_html_e( 'Password', 'give' ); ?>" type="password"/>
872
			</div>
873
874
			<div id="give-user-pass-confirm-wrap-<?php echo $form_id; ?>" class="give-register-password form-row form-row-one-third">
875
				<label for="give-user-pass-confirm-<?php echo $form_id; ?>">
876
					<?php esc_html_e( 'Confirm PW', 'give' ); ?>
877
					<?php if ( give_logged_in_only( $form_id ) ) { ?>
878
						<span class="give-required-indicator">*</span>
879
					<?php } ?>
880
					<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php esc_html_e( 'Please retype your password to confirm.', 'give' ); ?>"></span>
881
				</label>
882
883
				<input name="give_user_pass_confirm" id="give-user-pass-confirm-<?php echo $form_id; ?>" class="<?php if ( give_logged_in_only( $form_id ) ) {
884
					echo 'required ';
885
				} ?>give-input" placeholder="<?php esc_html_e( 'Confirm password', 'give' ); ?>" type="password"/>
886
			</div>
887
			<?php do_action( 'give_register_account_fields_after', $form_id ); ?>
888
		</fieldset>
889
890
		<?php do_action( 'give_register_fields_after', $form_id ); ?>
891
892
		<input type="hidden" name="give-purchase-var" value="needs-to-register"/>
893
894
		<?php do_action( 'give_purchase_form_user_info', $form_id ); ?>
895
896
	</fieldset>
897
	<?php
898
	echo ob_get_clean();
899
}
900
901
add_action( 'give_purchase_form_register_fields', 'give_get_register_fields' );
902
903
/**
904
 * Gets the login fields for the login form on the checkout. This function hooks
905
 * on the give_purchase_form_login_fields to display the login form if a user already
906
 * had an account.
907
 *
908
 * @since  1.0
909
 *
910
 * @param  int $form_id
911
 *
912
 * @return string
913
 */
914
function give_get_login_fields( $form_id ) {
915
916
	$form_id            = isset( $_POST['form_id'] ) ? $_POST['form_id'] : $form_id;
917
	$show_register_form = give_show_login_register_option( $form_id );
918
919
	ob_start();
920
	?>
921
	<fieldset id="give-login-fields-<?php echo $form_id; ?>">
922
		<legend><?php echo apply_filters( 'give_account_login_fieldset_heading', esc_html__( 'Login to Your Account', 'give' ) );
923
			if ( ! give_logged_in_only( $form_id ) ) {
924
				echo ' <span class="sub-text">' . esc_html__( '(optional)', 'give' ) . '</span>';
925
			} ?>
926
		</legend>
927
		<?php if ( $show_register_form == 'both' ) { ?>
928
			<p class="give-new-account-link">
929
				<?php esc_html_e( 'Need to create an account?', 'give' ); ?>&nbsp;
930
				<a href="<?php echo remove_query_arg( 'login' ); ?>" class="give-checkout-register-cancel" data-action="give_checkout_register">
931
					<?php esc_html_e( 'Register', 'give' );
932
					if ( ! give_logged_in_only( $form_id ) ) {
933
						echo ' ' . esc_html__( 'or checkout as a guest &raquo;', 'give' );
934
					} ?>
935
				</a>
936
			</p>
937
			<p class="give-loading-text">
938
				<span class="give-loading-animation"></span> <?php esc_html_e( 'Loading...', 'give' ); ?> </p>
939
		<?php } ?>
940
		<?php do_action( 'give_checkout_login_fields_before', $form_id ); ?>
941
		<div id="give-user-login-wrap-<?php echo $form_id; ?>" class="form-row form-row-first">
942
			<label class="give-label" for="give-user-login-<?php echo $form_id; ?>">
943
				<?php esc_html_e( 'Username', 'give' ); ?>
944
				<?php if ( give_logged_in_only( $form_id ) ) { ?>
945
					<span class="give-required-indicator">*</span>
946
				<?php } ?>
947
			</label>
948
949
			<input class="<?php if ( give_logged_in_only( $form_id ) ) {
950
				echo 'required ';
951
			} ?>give-input" type="text" name="give_user_login" id="give-user-login-<?php echo $form_id; ?>" value="" placeholder="<?php esc_html_e( 'Your username', 'give' ); ?>"/>
952
		</div>
953
954
		<div id="give-user-pass-wrap-<?php echo $form_id; ?>" class="give_login_password form-row form-row-last">
955
			<label class="give-label" for="give-user-pass-<?php echo $form_id; ?>">
956
				<?php esc_html_e( 'Password', 'give' ); ?>
957
				<?php if ( give_logged_in_only( $form_id ) ) { ?>
958
					<span class="give-required-indicator">*</span>
959
				<?php } ?>
960
			</label>
961
			<input class="<?php if ( give_logged_in_only( $form_id ) ) {
962
				echo 'required ';
963
			} ?>give-input" type="password" name="give_user_pass" id="give-user-pass-<?php echo $form_id; ?>" placeholder="<?php esc_html_e( 'Your password', 'give' ); ?>"/>
964
			<input type="hidden" name="give-purchase-var" value="needs-to-login"/>
965
		</div>
966
967
		<div id="give-forgot-password-wrap-<?php echo $form_id; ?>" class="give_login_forgot_password">
968
			 <span class="give-forgot-password ">
969
				 <a href="<?php echo wp_lostpassword_url() ?>" target="_blank"><?php esc_html_e( 'Reset password?' ) ?></a>
970
			 </span>
971
		</div>
972
973
		<div id="give-user-login-submit-<?php echo $form_id; ?>" class="give-clearfix">
974
			<input type="submit" class="give-submit give-btn button" name="give_login_submit" value="<?php esc_html_e( 'Login', 'give' ); ?>"/>
975
			<?php if ( $show_register_form !== 'login' ) { ?>
976
				<input type="button" data-action="give_cancel_login" class="give-cancel-login give-checkout-register-cancel give-btn button" name="give_login_cancel" value="<?php esc_html_e( 'Cancel', 'give' ); ?>"/>
977
			<?php } ?>
978
			<span class="give-loading-animation"></span>
979
		</div>
980
		<?php do_action( 'give_checkout_login_fields_after', $form_id ); ?>
981
	</fieldset><!--end #give-login-fields-->
982
	<?php
983
	echo ob_get_clean();
984
}
985
986
add_action( 'give_purchase_form_login_fields', 'give_get_login_fields', 10, 1 );
987
988
/**
989
 * Payment Mode Select
990
 *
991
 * Renders the payment mode form by getting all the enabled payment gateways and
992
 * outputting them as radio buttons for the user to choose the payment gateway. If
993
 * a default payment gateway has been chosen from the Give Settings, it will be
994
 * automatically selected.
995 1
 *
996
 * @since  1.0
997
 *
998
 * @param  int $form_id
999
 *
1000
 * @return void
1001
 */
1002
function give_payment_mode_select( $form_id ) {
1003
1004
	$gateways = give_get_enabled_payment_gateways();
1005
1006
	do_action( 'give_payment_mode_top', $form_id ); ?>
1007
1008
	<fieldset id="give-payment-mode-select">
1009
		<?php do_action( 'give_payment_mode_before_gateways_wrap' ); ?>
1010
		<div id="give-payment-mode-wrap">
1011 1
			<legend class="give-payment-mode-label"><?php echo apply_filters( 'give_checkout_payment_method_text', esc_html__( 'Select Payment Method', 'give' ) ); ?>
1012 1
				<span class="give-loading-text"><span class="give-loading-animation"></span> <?php esc_html_e( 'Loading...', 'give' ); ?></span>
1013 1
			</legend>
1014 1
			<?php
1015 1
1016
			do_action( 'give_payment_mode_before_gateways' ) ?>
1017
1018
			<ul id="give-gateway-radio-list">
1019
				<?php foreach ( $gateways as $gateway_id => $gateway ) :
1020
					$checked       = checked( $gateway_id, give_get_default_gateway( $form_id ), false );
1021
					$checked_class = $checked ? ' give-gateway-option-selected' : '';
1022
					echo '<li><label for="give-gateway-' . esc_attr( $gateway_id ) . '-' . $form_id . '" class="give-gateway-option' . $checked_class . '" id="give-gateway-option-' . esc_attr( $gateway_id ) . '">';
1023
					echo '<input type="radio" name="payment-mode" class="give-gateway" id="give-gateway-' . esc_attr( $gateway_id ) . '-' . $form_id . '" value="' . esc_attr( $gateway_id ) . '"' . $checked . '>' . esc_html( $gateway['checkout_label'] );
1024
					echo '</label></li>';
1025
				endforeach; ?>
1026
			</ul>
1027
			<?php do_action( 'give_payment_mode_after_gateways' ); ?>
1028
		</div>
1029
		<?php do_action( 'give_payment_mode_after_gateways_wrap' ); ?>
1030
	</fieldset>
1031
1032
	<?php do_action( 'give_payment_mode_bottom', $form_id ); ?>
1033 1
1034
	<div id="give_purchase_form_wrap">
1035
1036
		<?php do_action( 'give_purchase_form', $form_id ); ?>
1037
1038
	</div><!-- the checkout fields are loaded into this-->
1039
1040
	<?php do_action( 'give_purchase_form_wrap_bottom', $form_id );
1041
1042
}
1043
1044
add_action( 'give_payment_mode_select', 'give_payment_mode_select' );
1045
1046
1047
/**
1048
 * Renders the Checkout Agree to Terms, this displays a checkbox for users to
1049
 * agree the T&Cs set in the Give Settings. This is only displayed if T&Cs are
1050
 * set in the Give Settings.
1051
 *
1052 1
 * @since  1.0
1053 1
 *
1054 1
 * @param  int   $form_id
1055
 *
1056
 * @return void
1057
 */
1058
function give_terms_agreement( $form_id ) {
1059
1060
	$form_option = get_post_meta( $form_id, '_give_terms_option', true );
1061
	$label       = get_post_meta( $form_id, '_give_agree_label', true );
1062
	$terms       = get_post_meta( $form_id, '_give_agree_text', true );
1063
1064
	if ( $form_option === 'yes' && ! empty( $terms ) ) { ?>
1065
		<fieldset id="give_terms_agreement">
1066
			<div id="give_terms" class= "give_terms-<?php echo $form_id;?>" style="display:none;">
1067
				<?php
1068
				do_action( 'give_before_terms' );
1069
				echo wpautop( stripslashes( $terms ) );
1070
				do_action( 'give_after_terms' );
1071
				?>
1072
			</div>
1073
			<div id="give_show_terms">
1074
				<a href="#" class="give_terms_links give_terms_links-<?php echo $form_id;?>"><?php esc_html_e( 'Show Terms', 'give' ); ?></a>
1075
				<a href="#" class="give_terms_links give_terms_links-<?php echo $form_id;?>" style="display:none;"><?php esc_html_e( 'Hide Terms', 'give' ); ?></a>
1076
			</div>
1077 1
1078
			<input name="give_agree_to_terms" class="required" type="checkbox" id="give_agree_to_terms" value="1"/>
1079
			<label
1080
				for="give_agree_to_terms"><?php echo ! empty( $label ) ? stripslashes( $label ) : esc_html__( 'Agree to Terms?', 'give' ); ?></label>
1081
1082
		</fieldset>
1083
		<?php
1084
	}
1085
}
1086
1087
add_action( 'give_purchase_form_before_submit', 'give_terms_agreement', 10, 1 );
1088
1089
/**
1090
 * Checkout Final Total
1091
 *
1092
 * Shows the final purchase total at the bottom of the checkout page
1093 1
 *
1094
 * @since  1.0
1095
 *
1096
 * @param int   $form_id
1097 1
 *
1098
 * @return void
1099
 */
1100 1
function give_checkout_final_total( $form_id ) {
1101
1102
	if ( isset( $_POST['give_total'] ) ) {
1103
		$total = apply_filters( 'give_donation_total', $_POST['give_total'] );
1104
	} else {
1105
		//default total
1106
		$total = give_get_default_form_amount( $form_id );
1107
	}
1108
	//Only proceed if give_total available
1109 1
	if ( empty( $total ) ) {
1110
		return;
1111
	}
1112
	?>
1113
	<p id="give-final-total-wrap" class="form-wrap ">
1114
		<span class="give-donation-total-label"><?php echo apply_filters( 'give_donation_total_label', esc_html__( 'Donation Total:', 'give' ) ); ?></span>
1115
		<span class="give-final-total-amount" data-total="<?php echo give_format_amount( $total ); ?>"><?php echo give_currency_filter( give_format_amount( $total ) ); ?></span>
1116
	</p>
1117
	<?php
1118
}
1119
1120
add_action( 'give_purchase_form_before_submit', 'give_checkout_final_total', 999 );
1121
1122
1123
/**
1124
 * Renders the Checkout Submit section
1125
 *
1126
 * @since  1.0
1127
 *
1128
 * @param  int $form_id
1129
 *
1130
 * @return void
1131
 */
1132
function give_checkout_submit( $form_id ) {
1133
	?>
1134
	<fieldset id="give_purchase_submit">
1135
		<?php do_action( 'give_purchase_form_before_submit', $form_id ); ?>
1136 1
1137
		<?php give_checkout_hidden_fields( $form_id ); ?>
1138
1139
		<?php echo give_checkout_button_purchase( $form_id ); ?>
1140
1141
		<?php do_action( 'give_purchase_form_after_submit', $form_id ); ?>
1142
1143
	</fieldset>
1144
	<?php
1145
}
1146
1147
add_action( 'give_purchase_form_after_cc_form', 'give_checkout_submit', 9999 );
1148
1149
1150
/**
1151
 * Give Checkout Button Purchase
1152
 *
1153 1
 * Renders the Purchase button on the Checkout
1154 1
 * @since  1.0
1155
 *
1156
 * @param  int $form_id
1157
 *
1158
 * @return string
1159
 */
1160
function give_checkout_button_purchase( $form_id ) {
1161 1
1162
	$display_label_field = get_post_meta( $form_id, '_give_checkout_label', true );
1163
	$display_label       = ( ! empty( $display_label_field ) ? $display_label_field : esc_html__( 'Donate Now', 'give' ) );
1164
	ob_start(); ?>
1165
	<div class="give-submit-button-wrap give-clearfix">
1166
		<input type="submit" class="give-submit give-btn" id="give-purchase-button" name="give-purchase" value="<?php echo $display_label; ?>"/>
1167
		<span class="give-loading-animation"></span>
1168
	</div>
1169
	<?php
1170
	return apply_filters( 'give_checkout_button_purchase', ob_get_clean(), $form_id );
1171
}
1172
1173
/**
1174
 * Give Agree to Terms
1175
 *
1176 1
 * Outputs the JavaScript code for the Agree to Terms section to toggle the T&Cs text
1177
 * @since  1.0
1178 1
 *
1179
 * @param  int $form_id
1180
 *
1181
 * @return void
1182
 */
1183
function give_agree_to_terms_js( $form_id ) {
1184
1185
	$form_option = get_post_meta( $form_id, '_give_terms_option', true );
1186
1187
	if ( $form_option === 'yes' ) {
1188
		?>
1189
		<script type="text/javascript">
1190
			jQuery(document).ready(function ($) {
1191
				$('body').on('click', '.give_terms_links-<?php echo $form_id;?>', function (e) {
1192 1
					e.preventDefault();
1193
					$('.give_terms-<?php echo $form_id;?>').slideToggle();
1194
					$('.give_terms_links-<?php echo $form_id;?>').toggle();
1195
					return false;
1196
				});
1197
			});
1198
		</script>
1199
		<?php
1200
	}
1201
}
1202
1203
add_action( 'give_checkout_form_top', 'give_agree_to_terms_js', 10, 2 );
1204
1205
/**
1206
 * Show Give Goals
1207
 *
1208
 * @since  1.0
1209 1
 * @since  1.6   Add template for Give Goals Shortcode.
1210 1
 *               More info is on https://github.com/WordImpress/Give/issues/411
1211 1
 *
1212 1
 * @param  int   $form_id
1213 1
 * @param  array $args
1214 1
 *
1215 1
 * @return mixed
1216 1
 */
1217
1218
function give_show_goal_progress( $form_id, $args ) {
1219 1
1220
    ob_start();
1221
    give_get_template( 'shortcode-goal' , array( 'form_id' => $form_id, 'args' => $args ) );
1222
1223
    echo apply_filters( 'give_goal_output', ob_get_clean() );
1224 1
1225 1
	return true;
1226 1
}
1227 1
1228 1
add_action( 'give_pre_form', 'give_show_goal_progress', 10, 2 );
1229
1230 1
/**
1231
 * Adds Actions to Render Form Content
1232
 *
1233
 * @since  1.0
1234
 *
1235
 * @param  int   $form_id
1236
 * @param  array $args
1237
 *
1238
 * @return void
1239
 */
1240
function give_form_content( $form_id, $args ) {
1241
1242
	$show_content = ( isset( $args['show_content'] ) && ! empty( $args['show_content'] ) )
1243
		? $args['show_content']
1244
		: get_post_meta( $form_id, '_give_content_option', true );
1245
1246
	if ( $show_content !== 'none' ) {
1247
		//add action according to value
1248
		add_action( $show_content, 'give_form_display_content', 10, 2 );
1249
	}
1250
}
1251
1252
add_action( 'give_pre_form_output', 'give_form_content', 10, 2 );
1253
1254
/**
1255
 * Renders Post Form Content
1256
 *
1257
 * Displays content for Give forms; fired by action from give_form_content
1258
 *
1259
 * @param int $form_id
1260
 *
1261
 * @return void
1262
 * @since      1.0
1263
 */
1264
function give_form_display_content( $form_id, $args ) {
1265
1266
	$content      = wpautop( get_post_meta( $form_id, '_give_form_content', true ) );
1267
	$show_content = ( isset( $args['show_content'] ) && ! empty( $args['show_content'] ) )
1268
		? $args['show_content']
1269
		: get_post_meta( $form_id, '_give_content_option', true );
1270
1271
	if ( give_get_option( 'disable_the_content_filter' ) !== 'on' ) {
1272
		$content = apply_filters( 'the_content', $content );
1273
	}
1274
1275
	$output = '<div id="give-form-content-' . $form_id . '" class="give-form-content-wrap" >' . $content . '</div>';
1276
1277
	echo apply_filters( 'give_form_content_output', $output );
1278
1279
	//remove action to prevent content output on addition forms on page
1280
	//@see: https://github.com/WordImpress/Give/issues/634
1281
	remove_action( $show_content, 'give_form_display_content' );
1282
}
1283
1284
1285
/**
1286
 * Renders the hidden Checkout fields
1287
 *
1288
 * @since 1.0
1289
 *
1290
 * @param int $form_id
1291
 *
1292
 * @return void
1293
 */
1294
function give_checkout_hidden_fields( $form_id ) {
1295
1296
	do_action( 'give_hidden_fields_before', $form_id );
1297
	if ( is_user_logged_in() ) { ?>
1298
		<input type="hidden" name="give-user-id" value="<?php echo get_current_user_id(); ?>"/>
1299
	<?php } ?>
1300
	<input type="hidden" name="give_action" value="purchase"/>
1301
	<input type="hidden" name="give-gateway" value="<?php echo give_get_chosen_gateway( $form_id ); ?>"/>
1302 1
	<?php
1303 1
	do_action( 'give_hidden_fields_after', $form_id );
1304 1
1305
}
1306 1
1307
/**
1308 1
 * Filter Success Page Content
1309 1
 *
1310 1
 * Applies filters to the success page content.
1311
 *
1312
 * @since 1.0
1313
 *
1314
 * @param string $content Content before filters
1315
 *
1316
 * @return string $content Filtered content
1317
 */
1318
function give_filter_success_page_content( $content ) {
1319
1320
	global $give_options;
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...
1321
1322
	if ( isset( $give_options['success_page'] ) && isset( $_GET['payment-confirmation'] ) && is_page( $give_options['success_page'] ) ) {
1323
		if ( has_filter( 'give_payment_confirm_' . $_GET['payment-confirmation'] ) ) {
1324
			$content = apply_filters( 'give_payment_confirm_' . $_GET['payment-confirmation'], $content );
1325
		}
1326
	}
1327
1328
	return $content;
1329
}
1330
1331
add_filter( 'the_content', 'give_filter_success_page_content' );
1332
1333
1334
/**
1335
 * Test Mode Frontend Warning
1336
 *
1337
 * Displays a notice on the frontend for donation forms
1338
 * @since 1.1
1339
 */
1340
1341
function give_test_mode_frontend_warning() {
1342
1343
	$test_mode = give_get_option( 'test_mode' );
1344
1345
	if ( $test_mode == 'on' ) {
1346
		echo '<div class="give_error give_warning" id="give_error_test_mode"><p><strong>' . esc_html__( 'Notice', 'give' ) . '</strong>: ' . esc_html__( 'Test mode is enabled. While in test mode no live transactions are processed.', 'give' ) . '</p></div>';
1347
	}
1348
}
1349
1350
add_action( 'give_pre_form', 'give_test_mode_frontend_warning', 10 );
1351
1352
1353
/**
1354
 * Members-only Form
1355
 *
1356 1
 * If "Disable Guest Donations" and "Display Register / Login" is set to none
1357
 *
1358
 * @since  1.4.1
1359
 *
1360
 * @param  string $final_output
1361
 * @param  array  $args
1362
 *
1363 1
 * @return string
1364
 */
1365 1
1366
function give_members_only_form( $final_output, $args ) {
1367
1368
	$form_id = isset( $args['form_id'] ) ? $args['form_id'] : 0;
1369
1370
	//Sanity Check: Must have form_id & not be logged in
1371
	if ( empty( $form_id ) || is_user_logged_in() ) {
1372
		return $final_output;
1373
	}
1374
1375
	//Logged in only and Register / Login set to none
1376
	if ( give_logged_in_only( $form_id ) && give_show_login_register_option( $form_id ) == 'none' ) {
1377
1378
		$final_output = give_output_error( esc_html__( 'Please login in order to complete your donation.', 'give' ), false );
1379
1380
		return apply_filters( 'give_members_only_output', $final_output, $form_id );
1381
1382
	}
1383
1384
	return $final_output;
1385
1386
}
1387
1388
add_filter( 'give_donate_form', 'give_members_only_form', 10, 2 );
1389