Completed
Push — issues/1038 ( 529a5d...47bf78 )
by Ravinder
18:37
created

template.php ➔ give_get_donation_form()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 255
Code Lines 124

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 124
nc 24
nop 1
dl 0
loc 255
rs 5.034
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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     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
 * Get Donation Form.
19
 *
20
 * @since  1.0
21
 *
22
 * @param  array $args An array of form arguments.
23
 *
24
 * @return string Donation form.
0 ignored issues
show
Documentation introduced by
Should the return type not be false|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_get_donation_form( $args = array() ) {
27
28
	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
	$form_id = is_object( $post ) ? $post->ID : 0;
31
32
	if ( isset( $args['id'] ) ) {
33
		$form_id = $args['id'];
34
	}
35
36
	$defaults = apply_filters( 'give_form_args_defaults', array(
37
		'form_id' => $form_id,
38
	) );
39
40
	$args = wp_parse_args( $args, $defaults );
41
42
	$form = new Give_Donate_Form( $args['form_id'] );
43
44
	//bail if no form ID.
45
	if ( empty( $form->ID ) ) {
46
		return false;
47
	}
48
49
	$payment_mode = give_get_chosen_gateway( $form->ID );
50
51
	$form_action = add_query_arg( apply_filters( 'give_form_action_args', array(
52
		'payment-mode' => $payment_mode,
53
	) ),
54
		give_get_current_page_url()
55
	);
56
57
	//Sanity Check: Donation form not published or user doesn't have permission to view drafts.
58
	if (
59
		( 'publish' !== $form->post_status && ! current_user_can( 'edit_give_forms', $form->ID ) )
60
		|| ( 'trash' === $form->post_status )
61
	) {
62
		return false;
63
	}
64
65
	//Get the form wrap CSS classes.
66
	$form_wrap_classes = $form->get_form_wrap_classes( $args );
67
68
	//Get the <form> tag wrap CSS classes.
69
	$form_classes = $form->get_form_classes( $args );
70
71
	ob_start();
72
73
	/**
74
	 * Fires while outputting donation form, before the form wrapper div.
75
	 *
76
	 * @since 1.0
77
	 *
78
	 * @param int   $form_id The form ID.
79
	 * @param array $args    An array of form arguments.
80
	 */
81
	do_action( 'give_pre_form_output', $form->ID, $args );
82
83
	?>
84
	<div id="give-form-<?php echo $form->ID; ?>-wrap" class="<?php echo $form_wrap_classes; ?>">
85
86
		<?php
87
		if ( $form->is_close_donation_form() ) {
88
89
			// Get Goal thank you message.
90
			$goal_achieved_message = get_post_meta( $form->ID, '_give_form_goal_achieved_message', true );
91
			$goal_achieved_message = ! empty( $goal_achieved_message ) ? apply_filters( 'the_content', $goal_achieved_message ) : '';
92
93
			// Print thank you message.
94
			echo apply_filters( 'give_goal_closed_output', $goal_achieved_message, $form->ID );
95
96
		} else {
97
			/**
98
			 * Show form title:
99
			 * 1. if show_title params set to true
100
			 * 2. if admin set form display_style to button
101
			 */
102
			$form_title = apply_filters( 'give_form_title', '<h2 class="give-form-title">' . get_the_title( $form_id ) . '</h2>' );
103
			if (
104
				( isset( $args['show_title'] ) && $args['show_title'] == true )
105
				&& ! doing_action( 'give_single_form_summary' )
106
			) {
107
				echo $form_title;
108
			}
109
110
			/**
111
			 * Fires while outputing donation form, before the form.
112
			 *
113
			 * @since 1.0
114
			 *
115
			 * @param int   $form_id The form ID.
116
			 * @param array $args    An array of form arguments.
117
			 */
118
			do_action( 'give_pre_form', $form->ID, $args );
119
120
			$form_args = array(
121
				'id' => "give-form-{$form_id}",
122
				'action' => esc_url_raw( $form_action ),
123
				'method' => 'post',
124
125
				// Custom arguments.
126
				'donation_form_object' => $form,
127
				'donation_form_arguments' => $args,
128
129
				'form_attributes' => array(
130
					'class' => trim( $form_classes ),
131
				),
132
				'fields' => array(
133
					// Form id.
134
					array(
135
						'type'             => 'hidden',
136
						'id'               => 'give-form-id',
137
						'value'            => $form->ID,
138
						'field_attributes' => array(
139
							'id'    => '',
140
							'class' => '',
141
						),
142
					),
143
					// Form title.
144
					array(
145
						'type'             => 'hidden',
146
						'id'               => 'give-form-title',
147
						'value'            => htmlentities( $form->post_title ),
148
						'field_attributes' => array(
149
							'id'    => '',
150
							'class' => '',
151
						),
152
					),
153
					// Form current url.
154
					array(
155
						'type'             => 'hidden',
156
						'id'               => 'give-current-url',
157
						'value'            => htmlspecialchars( give_get_current_page_url() ),
158
						'field_attributes' => array(
159
							'id'    => '',
160
							'class' => '',
161
						),
162
					),
163
					// Form url.
164
					array(
165
						'type'             => 'hidden',
166
						'id'               => 'give-form-url',
167
						'value'            => htmlspecialchars( give_get_current_page_url() ),
168
						'field_attributes' => array(
169
							'id'    => '',
170
							'class' => '',
171
						),
172
					),
173
					// Donation minimum amount.
174
					array(
175
						'type'             => 'hidden',
176
						'id'               => 'give-form-minimum',
177
						'value'            => give_format_amount( give_get_form_minimum_price( $form->ID ) ),
178
						'field_attributes' => array(
179
							'id'    => '',
180
							'class' => '',
181
						),
182
					),
183
					// Security field.
184
					array(
185
						'type'               => 'text',
186
						'label'              => '',
187
						'id'                 => 'give-honeypot',
188
						'wrapper_type'       => 'span',
189
						'field_attributes'   => array(
190
							'id'    => "give-form-honeypot-{$form_id}",
191
							'class' => 'give-honeypot give-hidden',
192
						),
193
						'wrapper_attributes' => array(
194
							'class' => 'give-hidden',
195
							'style' => 'display: none !important;',
196
						),
197
					),
198
					// Price id.
199
					// This field will conditionally appear or disappear with help of filter.
200
					// @see includes/forms/filters.php:54
201
					array(
202
						'type'             => 'hidden',
203
						'id'               => 'give-price-id',
204
						'field_attributes' => array(
205
							'id'    => '',
206
							'class' => '',
207
						),
208
					),
209
					// Amount.
210
					// This field will conditionally appear or disappear with help of filter.
211
					// @see includes/forms/filters.php:54
212
					array(
213
						'type'  => 'hidden',
214
						'id'    => 'give-amount',
215
						'label' => esc_html__( 'Donation Amount:', 'give' ),
216
						'wrapper'          => false,
217
						'required'         => true,
218
						'label_attributes' => array(
219
							'class' => 'give-hidden',
220
						),
221
						'field_attributes' => array(
222
							'id'    => 'give-amount',
223
							'class' => 'give-amount-hidden',
224
						),
225
					),
226
					// Donation levels.
227
					// This field will conditionally appear or disappear with help of filter.
228
					// @see includes/forms/filters.php:273
229
					array(
230
						'type'  => 'give_donation_levels',
231
						'id'    => 'give-donation-levels',
232
						'wrapper'          => false,
233
						'required'         => true,
234
					)
235
				)
236
			);
237
238
			/**
239
			 * Fire the filter when defining form arguments to render donation form.
240
			 * Note: you can use this hook to edit form arguments like reordering form fields.
241
			 *
242
			 * @since 1.9
243
			 *
244
			 * @param array $form_args
245
			 */
246
			$form_args = apply_filters( 'give_form_args', $form_args );
247
248
			echo Give_Form_API::render_form(  $form_args );
249
250
251
			/**
252
			 * Fires while outputting donation form, after the form.
253
			 *
254
			 * @since 1.0
255
			 *
256
			 * @param int   $form_id The form ID.
257
			 * @param array $args    An array of form arguments.
258
			 */
259
			do_action( 'give_post_form', $form->ID, $args );
260
261
		}
262
		?>
263
264
    </div><!--end #give-form-<?php echo absint( $form->ID ); ?>-->
265
	<?php
266
267
	/**
268
	 * Fires while outputting donation form, after the form wapper div.
269
	 *
270
	 * @since 1.0
271
	 *
272
	 * @param int   $form_id The form ID.
273
	 * @param array $args    An array of form arguments.
274
	 */
275
	do_action( 'give_post_form_output', $form->ID, $args );
276
277
	$final_output = ob_get_clean();
278
279
	echo apply_filters( 'give_donate_form', $final_output, $args );
280
}
281
282
/**
283
 * Give Show Donation Form.
284
 *
285
 * Renders the Donation Form, hooks are provided to add to the checkout form.
286
 * The default Donation Form rendered displays a list of the enabled payment
287
 * gateways, a user registration form (if enable) and a credit card info form
288
 * if credit cards are enabled.
289
 *
290
 * @since  1.0
291
 *
292
 * @param  int $form_id The form ID.
293
 *
294
 * @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...
295
 */
296
function give_show_purchase_form( $form_id ) {
297
298
	$payment_mode = give_get_chosen_gateway( $form_id );
299
300
	if ( ! isset( $form_id ) && isset( $_POST['give_form_id'] ) ) {
301
		$form_id = $_POST['give_form_id'];
302
	}
303
304
	/**
305
	 * Fire before donation form render.
306
	 *
307
	 * @since 1.7
308
	 */
309
	do_action( 'give_donation_form_top', $form_id );
310
311
	if ( give_can_checkout() && isset( $form_id ) ) {
312
313
		/**
314
		 * Fires while displaying donation form, before registration login.
315
		 *
316
		 * @since 1.7
317
		 */
318
		do_action( 'give_donation_form_before_register_login', $form_id );
319
320
		/**
321
		 * Fire when register/login form fields render.
322
		 *
323
		 * @since 1.7
324
		 */
325
		do_action( 'give_donation_form_register_login_fields', $form_id );
326
327
		/**
328
		 * Fire when credit card form fields render.
329
		 *
330
		 * @since 1.7
331
		 */
332
		do_action( 'give_donation_form_before_cc_form', $form_id );
333
334
		// Load the credit card form and allow gateways to load their own if they wish.
335
		if ( has_action( 'give_' . $payment_mode . '_cc_form' ) ) {
336
			/**
337
			 * Fires while displaying donation form, credit card form fields for a given gateway.
338
			 *
339
			 * @since 1.0
340
			 *
341
			 * @param int $form_id The form ID.
342
			 */
343
			do_action( "give_{$payment_mode}_cc_form", $form_id );
344
		} else {
345
			/**
346
			 * Fires while displaying donation form, credit card form fields.
347
			 *
348
			 * @since 1.0
349
			 *
350
			 * @param int $form_id The form ID.
351
			 */
352
			do_action( 'give_cc_form', $form_id );
353
		}
354
355
		/**
356
		 * Fire after credit card form fields render.
357
		 *
358
		 * @since 1.7
359
		 */
360
		do_action( 'give_donation_form_after_cc_form', $form_id );
361
362
	} else {
363
		/**
364
		 * Fire if user can not donate.
365
		 *
366
		 * @since 1.7
367
		 */
368
		do_action( 'give_donation_form_no_access', $form_id );
369
370
	}
371
372
	/**
373
	 * Fire after donation form rendered.
374
	 *
375
	 * @since 1.7
376
	 */
377
	do_action( 'give_donation_form_bottom', $form_id );
378
}
379
380
add_action( 'give_donation_form', 'give_show_purchase_form' );
381
382
/**
383
 * Give Show Login/Register Form Fields.
384
 *
385
 * @since  1.4.1
386
 *
387
 * @param  int $form_id The form ID.
388
 *
389
 * @return void
390
 */
391
function give_show_register_login_fields( $form_id ) {
392
393
	$show_register_form = give_show_login_register_option( $form_id );
394
395
	if ( ( $show_register_form === 'registration' || ( $show_register_form === 'both' && ! isset( $_GET['login'] ) ) ) && ! is_user_logged_in() ) :
396
		?>
397
        <div id="give-checkout-login-register-<?php echo $form_id; ?>">
398
			<?php
399
			/**
400
			 * Fire if user registration form render.
401
			 *
402
			 * @since 1.7
403
			 */
404
			do_action( 'give_donation_form_register_fields', $form_id );
405
			?>
406
        </div>
407
		<?php
408
	elseif ( ( $show_register_form === 'login' || ( $show_register_form === 'both' && isset( $_GET['login'] ) ) ) && ! is_user_logged_in() ) :
409
		?>
410
        <div id="give-checkout-login-register-<?php echo $form_id; ?>">
411
			<?php
412
			/**
413
			 * Fire if user login form render.
414
			 *
415
			 * @since 1.7
416
			 */
417
			do_action( 'give_donation_form_login_fields', $form_id );
418
			?>
419
        </div>
420
		<?php
421
	endif;
422
423
	if ( ( ! isset( $_GET['login'] ) && is_user_logged_in() ) || ! isset( $show_register_form ) || 'none' === $show_register_form || 'login' === $show_register_form ) {
424
		/**
425
		 * Fire when user info render.
426
		 *
427
		 * @since 1.7
428
		 */
429
		do_action( 'give_donation_form_after_user_info', $form_id );
430
	}
431
}
432
433
add_action( 'give_donation_form_register_login_fields', 'give_show_register_login_fields' );
434
435
/**
436
 * Donation Amount Field.
437
 *
438
 * Outputs the donation amount field that appears at the top of the donation forms. If the user has custom amount
439
 * enabled the field will output as a customizable input.
440
 *
441
 * @since      1.0
442
 *
443
 * @deprecated 1.9 donation amount field will be render with form api.
444
 *
445
 * @param  int   $form_id The form ID.
446
 * @param  array $args    An array of form arguments.
447
 *
448
 * @return void
449
 */
450
function give_output_donation_amount_top( $form_id = 0, $args = array() ) {
451
452
	$give_options        = give_get_settings();
453
	$variable_pricing    = give_has_variable_prices( $form_id );
454
	$allow_custom_amount = get_post_meta( $form_id, '_give_custom_amount', true );
455
	$currency_position   = isset( $give_options['currency_position'] ) ? $give_options['currency_position'] : 'before';
456
	$symbol              = give_currency_symbol( give_get_currency() );
457
	$currency_output     = '<span class="give-currency-symbol give-currency-position-' . $currency_position . '">' . $symbol . '</span>';
458
	$default_amount      = give_format_amount( give_get_default_form_amount( $form_id ) );
459
	$custom_amount_text  = get_post_meta( $form_id, '_give_custom_amount_text', true );
460
461
	/**
462
	 * Fires while displaying donation form, before donation level fields.
463
	 *
464
	 * @since 1.0
465
	 *
466
	 * @param int   $form_id The form ID.
467
	 * @param array $args    An array of form arguments.
468
	 */
469
	do_action( 'give_before_donation_levels', $form_id, $args );
470
471
	//Set Price, No Custom Amount Allowed means hidden price field
472
	if ( ! give_is_setting_enabled( $allow_custom_amount ) ) {
473
		echo Give_Fields_API::render_tag(
474
			array(
475
				'type'  => 'hidden',
476
				'id'    => 'give-amount',
477
				'value' => $default_amount,
478
				'label' => esc_html__( 'Donation Amount:', 'give' ),
479
				'wrapper'          => false,
480
				'required'         => true,
481
				'label_attributes' => array(
482
					'class' => 'give-hidden',
483
				),
484
				'field_attributes' => array(
485
					'id'    => 'give-amount',
486
					'class' => 'give-amount-hidden',
487
				),
488
			)
489
		);
490
		?>
491
		<div class="set-price give-donation-amount form-row-wide">
492
			<?php
493
			echo sprintf(
494
				'%1$s<span id="give-amount-text" class="give-text-input give-amount-top">%2$s</span>%3$s',
495
				( $currency_position == 'before' ? $currency_output : '' ),
496
				$default_amount,
497
				( $currency_position == 'after' ? $currency_output : '' )
498
			);
499
			?>
500
		</div>
501
		<?php
502
	} else {
503
		//Custom Amount Allowed.
504
		echo Give_Fields_API::render_tag(
505
			array(
506
				'type'                 => 'text',
507
				'id'                   => 'give-amount',
508
				'value'                => $default_amount,
509
				'label'                => esc_html__( 'Donation Amount:', 'give' ),
510
				'required'             => true,
511
				'wrapper_type'         => 'div',
512
				'before_label'         => ( $currency_position == 'before' ? $currency_output : '' ),
513
				'after_field'          => ( $currency_position == 'after' ? $currency_output : '' ),
514
				'before_field_wrapper' => '<div class="give-total-wrap">',
515
				'after_field_wrapper'  => '</div>',
516
				'label_attributes'     => array(
517
					'class' => 'give-hidden',
518
				),
519
				'field_attributes'     => array(
520
					'id'    => 'give-amount',
521
					'class' => 'give-text-input give-amount-top',
522
				),
523
				'wrapper_attributes'   => array(
524
					'class' => 'give-donation-amount form-row-wide',
525
				),
526
			)
527
		);
528
	}
529
530
	/**
531
	 * Fires while displaying donation form, after donation amount field(s).
532
	 *
533
	 * @since 1.0
534
	 *
535
	 * @param int   $form_id The form ID.
536
	 * @param array $args    An array of form arguments.
537
	 */
538
	do_action( 'give_after_donation_amount', $form_id, $args );
539
540
	//Custom Amount Text
541
	if ( ! $variable_pricing && give_is_setting_enabled( $allow_custom_amount ) && ! empty( $custom_amount_text ) ) { ?>
542
        <p class="give-custom-amount-text"><?php echo $custom_amount_text; ?></p>
543
	<?php }
544
545
	//Output Variable Pricing Levels.
546
	if ( $variable_pricing ) {
547
		give_output_levels( $form_id );
548
	}
549
550
	/**
551
	 * Fires while displaying donation form, after donation level fields.
552
	 *
553
	 * @since 1.0
554
	 *
555
	 * @param int   $form_id The form ID.
556
	 * @param array $args    An array of form arguments.
557
	 */
558
	do_action( 'give_after_donation_levels', $form_id, $args );
559
}
560
561
// This hook has been removed, because after version 1.9 field will be render with form api.
562
// @see includes/forms/filters.php:12
563
add_action( 'give_checkout_form_top', 'give_output_donation_amount_top', 10, 2 );
564
565
/**
566
 * Outputs the Donation Levels in various formats such as dropdown, radios, and buttons.
567
 *
568
 * @since  1.0
569
 *
570
 * @param  int $form_id The form ID.
571
 *
572
 * @return string Donation levels.
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...
573
 */
574
function give_output_levels( $form_id ) {
575
576
	//Get variable pricing.
577
	$prices             = apply_filters( 'give_form_variable_prices', give_get_variable_prices( $form_id ), $form_id );
578
	$display_style      = get_post_meta( $form_id, '_give_display_style', true );
579
	$custom_amount      = get_post_meta( $form_id, '_give_custom_amount', true );
580
	$custom_amount_text = get_post_meta( $form_id, '_give_custom_amount_text', true );
581
	if ( empty( $custom_amount_text ) ) {
582
		$custom_amount_text = esc_html__( 'Give a Custom Amount', 'give' );
583
	}
584
585
	$output  = '';
586
	$counter = 0;
587
588
	switch ( $display_style ) {
589
		case 'buttons':
590
591
			$output .= '<ul id="give-donation-level-button-wrap" class="give-donation-levels-wrap give-list-inline">';
592
593
			foreach ( $prices as $price ) {
594
				$counter ++;
595
				$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 );
596
				$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 );
597
598
				$output .= '<li>';
599
				$output .= '<button type="button" data-price-id="' . $price['_give_id']['level_id'] . '" class=" ' . $level_classes . '" value="' . give_format_amount( $price['_give_amount'] ) . '">';
600
				$output .= $level_text;
601
				$output .= '</button>';
602
				$output .= '</li>';
603
604
			}
605
606
			//Custom Amount.
607
			if ( give_is_setting_enabled( $custom_amount ) && ! empty( $custom_amount_text ) ) {
608
				$output .= '<li>';
609
				$output .= '<button type="button" data-price-id="custom" class="give-donation-level-btn give-btn give-btn-level-custom" value="custom">';
610
				$output .= $custom_amount_text;
611
				$output .= '</button>';
612
				$output .= '</li>';
613
			}
614
615
			$output .= '</ul>';
616
617
			break;
618
619
		case 'radios':
620
			// @todo: render this with field api.
621
622
			$output .= '<ul id="give-donation-level-radio-list" class="give-donation-levels-wrap">';
623
624
			foreach ( $prices as $price ) {
625
				$counter ++;
626
				$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 );
627
				$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 );
628
629
				$output .= '<li>';
630
				$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'] ) . '">';
631
				$output .= '<label for="give-radio-level-' . $counter . '">' . $level_text . '</label>';
632
				$output .= '</li>';
633
634
			}
635
636
			//Custom Amount.
637
			if ( give_is_setting_enabled( $custom_amount ) && ! empty( $custom_amount_text ) ) {
638
				$output .= '<li>';
639
				$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">';
640
				$output .= '<label for="give-radio-level-custom">' . $custom_amount_text . '</label>';
641
				$output .= '</li>';
642
			}
643
644
			$output .= '</ul>';
645
646
			break;
647
648
		case 'dropdown':
649
			// @todo: render this with field api.
650
651
			$output .= '<label for="give-donation-level" class="give-hidden">' . esc_html__( 'Choose Your Donation Amount', 'give' ) . ':</label>';
652
			$output .= '<select id="give-donation-level-' . $form_id . '" class="give-select give-select-level give-donation-levels-wrap">';
653
654
			//first loop through prices.
655
			foreach ( $prices as $price ) {
656
				$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 );
657
				$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 );
658
659
				$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'] ) . '">';
660
				$output .= $level_text;
661
				$output .= '</option>';
662
663
			}
664
665
			//Custom Amount.
666
			if ( give_is_setting_enabled( $custom_amount ) && ! empty( $custom_amount_text ) ) {
667
				$output .= '<option data-price-id="custom" class="give-donation-level-custom" value="custom">' . $custom_amount_text . '</option>';
668
			}
669
670
			$output .= '</select>';
671
672
			break;
673
	}
674
675
	echo apply_filters( 'give_form_level_output', $output, $form_id );
676
}
677
678
/**
679
 * Display Reveal & Lightbox Button.
680
 *
681
 * Outputs a button to reveal form fields.
682
 *
683
 * @since  1.0
684
 *
685
 * @param  int   $form_id The form ID.
686
 * @param  array $args    An array of form arguments.
687
 *
688
 * @return string Checkout button.
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...
689
 */
690
function give_display_checkout_button( $form_id, $args ) {
691
692
	$display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
693
		? $args['display_style']
694
		: get_post_meta( $form_id, '_give_payment_display', true );
695
696
	if ( 'button' === $display_option ) {
697
		$display_option = 'modal';
698
	} elseif ( $display_option === 'onpage' ) {
699
		return '';
700
	}
701
702
	$display_label_field = get_post_meta( $form_id, '_give_reveal_label', true );
703
	$display_label       = ! empty( $args['continue_button_title'] ) ? $args['continue_button_title'] : ( ! empty( $display_label_field ) ? $display_label_field : esc_html__( 'Donate Now', 'give' ) );
704
705
	$output = '<button type="button" class="give-btn give-btn-' . $display_option . '">' . $display_label . '</button>';
706
707
	echo apply_filters( 'give_display_checkout_button', $output );
708
}
709
710
add_action( 'give_after_donation_levels', 'give_display_checkout_button', 10, 2 );
711
712
/**
713
 * Shows the User Info fields in the Personal Info box, more fields can be added via the hooks provided.
714
 *
715
 * @since  1.0
716
 *
717
 * @param  int $form_id The form ID.
718
 *
719
 * @return void
720
 */
721
function give_user_info_fields( $form_id ) {
722
	// Get user info.
723
	$give_user_info = _give_get_prefill_form_field_values( $form_id );
724
725
	/**
726
	 * Fire before user personal information fields
727
	 *
728
	 * @since 1.7
729
	 */
730
	do_action( 'give_donation_form_before_personal_info', $form_id );
731
	?>
732
    <fieldset id="give_checkout_user_info">
733
        <legend><?php echo apply_filters( 'give_checkout_personal_info_text', esc_html__( 'Personal Info', 'give' ) ); ?></legend>
734
		<?php
735
		// First name.
736
		$is_first_name_required = give_field_is_required( 'give_first', $form_id );
737
		echo Give_Fields_API::render_tag(
738
			array(
739
				'type'               => 'text',
740
				'id'                 => 'give_first',
741
				'value'              => isset( $give_user_info['give_first'] )
742
					? $give_user_info['give_first']
743
					: '',
744
				'label'              => esc_html__( 'First Name', 'give' ),
745
				'label_tooltip'      => esc_attr__( 'We will use this to personalize your account experience.', 'give' ),
746
				'required'           => $is_first_name_required,
747
				'field_attributes'   => array(
748
					'id'          => 'give-first',
749
					'class'       => $is_first_name_required
750
						? 'give-input required'
751
						: 'give-input',
752
					'placeholder' => esc_attr__( 'First Name', 'give' ),
753
				),
754
				'wrapper_attributes' => array(
755
					'id'    => 'give-first-name-wrap',
756
					'class' => 'form-row form-row-first form-row-responsive',
757
				),
758
			)
759
		);
760
761
		// Last name.
762
		$is_last_name_required = give_field_is_required( 'give_last', $form_id );
763
		echo Give_Fields_API::render_tag(
764
			array(
765
				'type'               => 'text',
766
				'id'                 => 'give_last',
767
				'value'              => isset( $give_user_info['give_last'] )
768
					? $give_user_info['give_last']
769
					: '',
770
				'label'              => esc_html__( 'Last Name', 'give' ),
771
				'label_tooltip'      => esc_attr__( 'We will use this to personalize your account experience.', 'give' ),
772
				'required'           => $is_last_name_required,
773
				'field_attributes'   => array(
774
					'id'          => 'give-last',
775
					'class'       => $is_last_name_required
776
						? 'give-input required'
777
						: 'give-input',
778
					'placeholder' => esc_attr__( 'Last Name', 'give' ),
779
				),
780
				'wrapper_attributes' => array(
781
					'id'    => 'give-last-name-wrap',
782
					'class' => 'form-row form-row-last form-row-responsive',
783
				),
784
			)
785
		);
786
787
		/**
788
		 * Fire before user email field
789
		 *
790
		 * @since 1.7
791
		 */
792
		do_action( 'give_donation_form_before_email', $form_id );
793
794
		// Email address.
795
		$is_email_name_required = give_field_is_required( 'give_email', $form_id );
796
		echo Give_Fields_API::render_tag(
797
			array(
798
				'type'               => 'text',
799
				'id'                 => 'give_email',
800
				'value'              => isset( $give_user_info['give_email'] )
801
					? $give_user_info['give_email']
802
					: '',
803
				'label'              => esc_html__( 'Email address', 'give' ),
804
				'label_tooltip'      => esc_attr__( 'We will send the donation receipt to this address.', 'give' ),
805
				'required'           => $is_email_name_required,
806
				'field_attributes'   => array(
807
					'id'          => 'give-email',
808
					'class'       => $is_email_name_required
809
						? 'give-input required'
810
						: 'give-input',
811
					'placeholder' => esc_attr__( 'Email Address', 'give' ),
812
				),
813
				'wrapper_attributes' => array(
814
					'id'    => 'give-email-wrap',
815
					'class' => 'form-row form-row-wide',
816
				),
817
			)
818
		);
819
820
		/**
821
		 * Fire after user email field
822
		 *
823
		 * @since 1.7
824
		 */
825
		do_action( 'give_donation_form_after_email', $form_id );
826
827
		/**
828
		 * Fire after personal email field
829
		 *
830
		 * @since 1.7
831
		 */
832
		do_action( 'give_donation_form_user_info', $form_id );
833
		?>
834
    </fieldset>
835
	<?php
836
	/**
837
	 * Fire after user personal information fields
838
	 *
839
	 * @since 1.7
840
	 */
841
	do_action( 'give_donation_form_after_personal_info', $form_id );
842
}
843
844
add_action( 'give_donation_form_after_user_info', 'give_user_info_fields' );
845
add_action( 'give_register_fields_before', 'give_user_info_fields' );
846
847
/**
848
 * Renders the credit card info form.
849
 *
850
 * @since  1.0
851
 *
852
 * @param  int $form_id The form ID.
853
 *
854
 * @return void
855
 */
856
function give_get_cc_form( $form_id ) {
857
858
	ob_start();
859
860
	/**
861
	 * Fires while rendering credit card info form, before the fields.
862
	 *
863
	 * @since 1.0
864
	 *
865
	 * @param int $form_id The form ID.
866
	 */
867
	do_action( 'give_before_cc_fields', $form_id );
868
	?>
869
    <fieldset id="give_cc_fields-<?php echo $form_id ?>" class="give-do-validate">
870
        <legend><?php echo apply_filters( 'give_credit_card_fieldset_heading', esc_html__( 'Credit Card Info', 'give' ) ); ?></legend>
871
		<?php if ( is_ssl() ) : ?>
872
            <div id="give_secure_site_wrapper-<?php echo $form_id ?>">
873
                <span class="give-icon padlock"></span>
874
                <span><?php esc_html_e( 'This is a secure SSL encrypted payment.', 'give' ); ?></span>
875
            </div>
876
		<?php endif; ?>
877
        <p id="give-card-number-wrap-<?php echo $form_id ?>" class="form-row form-row-two-thirds form-row-responsive">
878
            <label for="card_number-<?php echo $form_id ?>" class="give-label">
879
				<?php esc_html_e( 'Card Number', 'give' ); ?>
880
                <span class="give-required-indicator">*</span>
881
                <span class="give-tooltip give-icon give-icon-question"
882
                      data-tooltip="<?php esc_attr_e( 'The (typically) 16 digits on the front of your credit card.', 'give' ); ?>"></span>
883
                <span class="card-type"></span>
884
            </label>
885
886
            <input type="tel" autocomplete="off" name="card_number" id="card_number-<?php echo $form_id ?>"
887
                   class="card-number give-input required" placeholder="<?php esc_attr_e( 'Card number', 'give' ); ?>"
888
                   required aria-required="true"/>
889
        </p>
890
891
        <p id="give-card-cvc-wrap-<?php echo $form_id ?>" class="form-row form-row-one-third form-row-responsive">
892
            <label for="card_cvc-<?php echo $form_id ?>" class="give-label">
893
				<?php esc_html_e( 'CVC', 'give' ); ?>
894
                <span class="give-required-indicator">*</span>
895
                <span class="give-tooltip give-icon give-icon-question"
896
                      data-tooltip="<?php esc_attr_e( 'The 3 digit (back) or 4 digit (front) value on your card.', 'give' ); ?>"></span>
897
            </label>
898
899
            <input type="tel" size="4" autocomplete="off" name="card_cvc" id="card_cvc-<?php echo $form_id ?>"
900
                   class="card-cvc give-input required" placeholder="<?php esc_attr_e( 'Security code', 'give' ); ?>"
901
                   required aria-required="true"/>
902
        </p>
903
904
        <p id="give-card-name-wrap-<?php echo $form_id ?>" class="form-row form-row-two-thirds form-row-responsive">
905
            <label for="card_name-<?php echo $form_id ?>" class="give-label">
906
				<?php esc_html_e( 'Name on the Card', 'give' ); ?>
907
                <span class="give-required-indicator">*</span>
908
                <span class="give-tooltip give-icon give-icon-question"
909
                      data-tooltip="<?php esc_attr_e( 'The name printed on the front of your credit card.', 'give' ); ?>"></span>
910
            </label>
911
912
            <input type="text" autocomplete="off" name="card_name" id="card_name-<?php echo $form_id ?>"
913
                   class="card-name give-input required" placeholder="<?php esc_attr_e( 'Card name', 'give' ); ?>"
914
                   required aria-required="true"/>
915
        </p>
916
		<?php
917
		/**
918
		 * Fires while rendering credit card info form, before expiration fields.
919
		 *
920
		 * @since 1.0
921
		 *
922
		 * @param int $form_id The form ID.
923
		 */
924
		do_action( 'give_before_cc_expiration' );
925
		?>
926
        <p class="card-expiration form-row form-row-one-third form-row-responsive">
927
            <label for="card_expiry-<?php echo $form_id ?>" class="give-label">
928
				<?php esc_html_e( 'Expiration', 'give' ); ?>
929
                <span class="give-required-indicator">*</span>
930
                <span class="give-tooltip give-icon give-icon-question"
931
                      data-tooltip="<?php esc_attr_e( 'The date your credit card expires, typically on the front of the card.', 'give' ); ?>"></span>
932
            </label>
933
934
            <input type="hidden" id="card_exp_month-<?php echo $form_id ?>" name="card_exp_month"
935
                   class="card-expiry-month"/>
936
            <input type="hidden" id="card_exp_year-<?php echo $form_id ?>" name="card_exp_year"
937
                   class="card-expiry-year"/>
938
939
            <input type="tel" autocomplete="off" name="card_expiry" id="card_expiry-<?php echo $form_id ?>"
940
                   class="card-expiry give-input required" placeholder="<?php esc_attr_e( 'MM / YY', 'give' ); ?>"
941
                   required aria-required="true"/>
942
        </p>
943
		<?php
944
		/**
945
		 * Fires while rendering credit card info form, after expiration fields.
946
		 *
947
		 * @since 1.0
948
		 *
949
		 * @param int $form_id The form ID.
950
		 */
951
		do_action( 'give_after_cc_expiration', $form_id );
952
		?>
953
    </fieldset>
954
	<?php
955
	/**
956
	 * Fires while rendering credit card info form, before the fields.
957
	 *
958
	 * @since 1.0
959
	 *
960
	 * @param int $form_id The form ID.
961
	 */
962
	do_action( 'give_after_cc_fields', $form_id );
963
964
	echo ob_get_clean();
965
}
966
967
add_action( 'give_cc_form', 'give_get_cc_form' );
968
969
/**
970
 * Outputs the default credit card address fields.
971
 *
972
 * @since  1.0
973
 *
974
 * @param  int $form_id The form ID.
975
 *
976
 * @return void
977
 */
978
function give_default_cc_address_fields( $form_id ) {
979
	// Get user info.
980
	$give_user_info = _give_get_prefill_form_field_values( $form_id );
981
982
	$logged_in = is_user_logged_in();
983
984
	if ( $logged_in ) {
985
		$user_address = get_user_meta( get_current_user_id(), '_give_user_address', true );
986
	}
987
	$line1 = $logged_in && ! empty( $user_address['line1'] ) ? $user_address['line1'] : '';
988
	$line2 = $logged_in && ! empty( $user_address['line2'] ) ? $user_address['line2'] : '';
989
	$city  = $logged_in && ! empty( $user_address['city'] ) ? $user_address['city'] : '';
990
	$zip   = $logged_in && ! empty( $user_address['zip'] ) ? $user_address['zip'] : '';
991
992
	ob_start();
993
	?>
994
    <fieldset id="give_cc_address" class="cc-address">
995
        <legend><?php echo apply_filters( 'give_billing_details_fieldset_heading', esc_html__( 'Billing Details', 'give' ) ); ?></legend>
996
		<?php
997
		/**
998
		 * Fires while rendering credit card billing form, before address fields.
999
		 *
1000
		 * @since 1.0
1001
		 *
1002
		 * @param int $form_id The form ID.
1003
		 */
1004
		do_action( 'give_cc_billing_top' );
1005
		?>
1006
        <p id="give-card-address-wrap" class="form-row form-row-wide">
1007
            <label for="card_address" class="give-label">
1008
				<?php esc_html_e( 'Address 1', 'give' ); ?>
1009
				<?php
1010
				if ( give_field_is_required( 'card_address', $form_id ) ) : ?>
1011
                    <span class="give-required-indicator">*</span>
1012
				<?php endif; ?>
1013
                <span class="give-tooltip give-icon give-icon-question"
1014
                      data-tooltip="<?php esc_attr_e( 'The primary billing address for your credit card.', 'give' ); ?>"></span>
1015
            </label>
1016
1017
            <input
1018
                    type="text"
1019
                    id="card_address"
1020
                    name="card_address"
1021
                    class="card-address give-input<?php echo( give_field_is_required( 'card_address', $form_id ) ? ' required' : '' ); ?>"
1022
                    placeholder="<?php esc_attr_e( 'Address line 1', 'give' ); ?>"
1023
                    value="<?php echo isset( $give_user_info['card_address'] ) ? $give_user_info['card_address'] : ''; ?>"
1024
				<?php echo( give_field_is_required( 'card_address', $form_id ) ? '  required aria-required="true" ' : '' ); ?>
1025
            />
1026
        </p>
1027
1028
        <p id="give-card-address-2-wrap" class="form-row form-row-wide">
1029
            <label for="card_address_2" class="give-label">
1030
				<?php esc_html_e( 'Address 2', 'give' ); ?>
1031
				<?php if ( give_field_is_required( 'card_address_2', $form_id ) ) : ?>
1032
                    <span class="give-required-indicator">*</span>
1033
				<?php endif; ?>
1034
                <span class="give-tooltip give-icon give-icon-question"
1035
                      data-tooltip="<?php esc_attr_e( '(optional) The suite, apt no, PO box, etc, associated with your billing address.', 'give' ); ?>"></span>
1036
            </label>
1037
1038
            <input
1039
                    type="text"
1040
                    id="card_address_2"
1041
                    name="card_address_2"
1042
                    class="card-address-2 give-input<?php echo( give_field_is_required( 'card_address_2', $form_id ) ? ' required' : '' ); ?>"
1043
                    placeholder="<?php esc_attr_e( 'Address line 2', 'give' ); ?>"
1044
                    value="<?php echo isset( $give_user_info['card_address_2'] ) ? $give_user_info['card_address_2'] : ''; ?>"
1045
				<?php echo( give_field_is_required( 'card_address_2', $form_id ) ? ' required aria-required="true" ' : '' ); ?>
1046
            />
1047
        </p>
1048
1049
        <p id="give-card-city-wrap" class="form-row form-row-first form-row-responsive">
1050
            <label for="card_city" class="give-label">
1051
				<?php esc_html_e( 'City', 'give' ); ?>
1052
				<?php if ( give_field_is_required( 'card_city', $form_id ) ) : ?>
1053
                    <span class="give-required-indicator">*</span>
1054
				<?php endif; ?>
1055
                <span class="give-tooltip give-icon give-icon-question"
1056
                      data-tooltip="<?php esc_attr_e( 'The city for your billing address.', 'give' ); ?>"></span>
1057
            </label>
1058
            <input
1059
                    type="text"
1060
                    id="card_city"
1061
                    name="card_city"
1062
                    class="card-city give-input<?php echo( give_field_is_required( 'card_city', $form_id ) ? ' required' : '' ); ?>"
1063
                    placeholder="<?php esc_attr_e( 'City', 'give' ); ?>"
1064
                    value="<?php echo isset( $give_user_info['card_city'] ) ? $give_user_info['card_city'] : ''; ?>"
1065
				<?php echo( give_field_is_required( 'card_city', $form_id ) ? ' required aria-required="true" ' : '' ); ?>
1066
            />
1067
        </p>
1068
1069
        <p id="give-card-zip-wrap" class="form-row form-row-last form-row-responsive">
1070
            <label for="card_zip" class="give-label">
1071
				<?php esc_html_e( 'Zip / Postal Code', 'give' ); ?>
1072
				<?php if ( give_field_is_required( 'card_zip', $form_id ) ) : ?>
1073
                    <span class="give-required-indicator">*</span>
1074
				<?php endif; ?>
1075
                <span class="give-tooltip give-icon give-icon-question"
1076
                      data-tooltip="<?php esc_attr_e( 'The zip or postal code for your billing address.', 'give' ); ?>"></span>
1077
            </label>
1078
1079
            <input
1080
                    type="text"
1081
                    size="4"
1082
                    id="card_zip"
1083
                    name="card_zip"
1084
                    class="card-zip give-input<?php echo( give_field_is_required( 'card_zip', $form_id ) ? ' required' : '' ); ?>"
1085
                    placeholder="<?php esc_attr_e( 'Zip / Postal Code', 'give' ); ?>"
1086
                    value="<?php echo isset( $give_user_info['card_zip'] ) ? $give_user_info['card_zip'] : ''; ?>"
1087
				<?php echo( give_field_is_required( 'card_zip', $form_id ) ? ' required aria-required="true" ' : '' ); ?>
1088
            />
1089
        </p>
1090
1091
        <p id="give-card-country-wrap" class="form-row form-row-first form-row-responsive">
1092
            <label for="billing_country" class="give-label">
1093
				<?php esc_html_e( 'Country', 'give' ); ?>
1094
				<?php if ( give_field_is_required( 'billing_country', $form_id ) ) : ?>
1095
                    <span class="give-required-indicator">*</span>
1096
				<?php endif; ?>
1097
                <span class="give-tooltip give-icon give-icon-question"
1098
                      data-tooltip="<?php esc_attr_e( 'The country for your billing address.', 'give' ); ?>"></span>
1099
            </label>
1100
1101
            <select
1102
                    name="billing_country"
1103
                    id="billing_country"
1104
                    class="billing-country billing_country give-select<?php echo( give_field_is_required( 'billing_country', $form_id ) ? ' required' : '' ); ?>"
1105
				<?php echo( give_field_is_required( 'billing_country', $form_id ) ? ' required aria-required="true" ' : '' ); ?>
1106
            >
1107
				<?php
1108
1109
				$selected_country = give_get_country();
1110
1111
				if ( ! empty( $give_user_info['billing_country'] ) && '*' !== $give_user_info['billing_country'] ) {
1112
					$selected_country = $give_user_info['billing_country'];
1113
				}
1114
1115
				$countries = give_get_country_list();
1116
				foreach ( $countries as $country_code => $country ) {
1117
					echo '<option value="' . esc_attr( $country_code ) . '"' . selected( $country_code, $selected_country, false ) . '>' . $country . '</option>';
1118
				}
1119
				?>
1120
            </select>
1121
        </p>
1122
1123
        <p id="give-card-state-wrap" class="form-row form-row-last form-row-responsive">
1124
            <label for="card_state" class="give-label">
1125
				<?php esc_html_e( 'State / Province', 'give' ); ?>
1126
				<?php if ( give_field_is_required( 'card_state', $form_id ) ) : ?>
1127
                    <span class="give-required-indicator">*</span>
1128
				<?php endif; ?>
1129
                <span class="give-tooltip give-icon give-icon-question"
1130
                      data-tooltip="<?php esc_attr_e( 'The state or province for your billing address.', 'give' ); ?>"></span>
1131
            </label>
1132
1133
			<?php
1134
			$selected_state = give_get_state();
1135
			$states         = give_get_states( $selected_country );
1136
1137
			if ( ! empty( $give_user_info['card_state'] ) ) {
1138
				$selected_state = $give_user_info['card_state'];
1139
			}
1140
1141
			if ( ! empty( $states ) ) : ?>
1142
                <select
1143
                        name="card_state"
1144
                        id="card_state"
1145
                        class="card_state give-select<?php echo( give_field_is_required( 'card_state', $form_id ) ? ' required' : '' ); ?>"
1146
					<?php echo( give_field_is_required( 'card_state', $form_id ) ? ' required aria-required="true" ' : '' ); ?>>
1147
					<?php
1148
					foreach ( $states as $state_code => $state ) {
1149
						echo '<option value="' . $state_code . '"' . selected( $state_code, $selected_state, false ) . '>' . $state . '</option>';
1150
					}
1151
					?>
1152
                </select>
1153
			<?php else : ?>
1154
                <input type="text" size="6" name="card_state" id="card_state" class="card_state give-input"
1155
                       placeholder="<?php esc_attr_e( 'State / Province', 'give' ); ?>"/>
1156
			<?php endif; ?>
1157
        </p>
1158
		<?php
1159
		/**
1160
		 * Fires while rendering credit card billing form, after address fields.
1161
		 *
1162
		 * @since 1.0
1163
		 *
1164
		 * @param int $form_id The form ID.
1165
		 */
1166
		do_action( 'give_cc_billing_bottom' );
1167
		?>
1168
    </fieldset>
1169
	<?php
1170
	echo ob_get_clean();
1171
}
1172
1173
add_action( 'give_after_cc_fields', 'give_default_cc_address_fields' );
1174
1175
1176
/**
1177
 * Renders the user registration fields. If the user is logged in, a login form is displayed other a registration form
1178
 * is provided for the user to create an account.
1179
 *
1180
 * @since  1.0
1181
 *
1182
 * @param  int $form_id The form ID.
1183
 *
1184
 * @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...
1185
 */
1186
function give_get_register_fields( $form_id ) {
1187
1188
	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...
1189
1190
	if ( is_user_logged_in() ) {
1191
		$user_data = get_userdata( $user_ID );
1192
	}
1193
1194
	$show_register_form = give_show_login_register_option( $form_id );
1195
1196
	ob_start(); ?>
1197
    <fieldset id="give-register-fields-<?php echo $form_id; ?>">
1198
1199
		<?php if ( $show_register_form == 'both' ) { ?>
1200
            <div class="give-login-account-wrap">
1201
                <p class="give-login-message"><?php esc_html_e( 'Already have an account?', 'give' ); ?>&nbsp;
1202
                    <a href="<?php echo esc_url( add_query_arg( 'login', 1 ) ); ?>" class="give-checkout-login"
1203
                       data-action="give_checkout_login"><?php esc_html_e( 'Login', 'give' ); ?></a>
1204
                </p>
1205
                <p class="give-loading-text">
1206
                    <span class="give-loading-animation"></span>
1207
                </p>
1208
            </div>
1209
		<?php } ?>
1210
1211
		<?php
1212
		/**
1213
		 * Fires while rendering user registration form, before registration fields.
1214
		 *
1215
		 * @since 1.0
1216
		 *
1217
		 * @param int $form_id The form ID.
1218
		 */
1219
		do_action( 'give_register_fields_before', $form_id );
1220
		?>
1221
1222
        <fieldset id="give-register-account-fields-<?php echo $form_id; ?>">
1223
            <legend>
1224
				<?php
1225
				echo apply_filters( 'give_create_account_fieldset_heading', esc_html__( 'Create an account', 'give' ) );
1226
				if ( ! give_logged_in_only( $form_id ) ) {
1227
					echo ' <span class="sub-text">' . esc_html__( '(optional)', 'give' ) . '</span>';
1228
				}
1229
				?>
1230
            </legend>
1231
			<?php
1232
			/**
1233
			 * Fires while rendering user registration form, before account fields.
1234
			 *
1235
			 * @since 1.0
1236
			 *
1237
			 * @param int $form_id The form ID.
1238
			 */
1239
			do_action( 'give_register_account_fields_before', $form_id );
1240
			?>
1241
            <div id="give-user-login-wrap-<?php echo $form_id; ?>"
1242
                 class="form-row form-row-one-third form-row-first form-row-responsive">
1243
                <label for="give-user-login-<?php echo $form_id; ?>">
1244
					<?php esc_html_e( 'Username', 'give' ); ?>
1245
					<?php if ( give_logged_in_only( $form_id ) ) { ?>
1246
                        <span class="give-required-indicator">*</span>
1247
					<?php } ?>
1248
                    <span class="give-tooltip give-icon give-icon-question"
1249
                          data-tooltip="<?php esc_attr_e( 'The username you will use to log into your account.', 'give' ); ?>"></span>
1250
                </label>
1251
1252
                <input name="give_user_login" id="give-user-login-<?php echo $form_id; ?>" class="give-input"
1253
                       type="text"
1254
                       placeholder="<?php esc_attr_e( 'Username', 'give' ); ?>"<?php echo ( give_logged_in_only( $form_id ) ) ? ' required aria-required="true" ' : ''; ?>/>
1255
            </div>
1256
1257
            <div id="give-user-pass-wrap-<?php echo $form_id; ?>"
1258
                 class="form-row form-row-one-third form-row-responsive">
1259
                <label for="give-user-pass-<?php echo $form_id; ?>">
1260
					<?php esc_html_e( 'Password', 'give' ); ?>
1261
					<?php if ( give_logged_in_only( $form_id ) ) { ?>
1262
                        <span class="give-required-indicator">*</span>
1263
					<?php } ?>
1264
                    <span class="give-tooltip give-icon give-icon-question"
1265
                          data-tooltip="<?php esc_attr_e( 'The password used to access your account.', 'give' ); ?>"></span>
1266
                </label>
1267
1268
                <input name="give_user_pass" id="give-user-pass-<?php echo $form_id; ?>" class="give-input"
1269
                       placeholder="<?php esc_attr_e( 'Password', 'give' ); ?>"
1270
                       type="password"<?php echo ( give_logged_in_only( $form_id ) ) ? ' required aria-required="true" ' : ''; ?>/>
1271
            </div>
1272
1273
            <div id="give-user-pass-confirm-wrap-<?php echo $form_id; ?>"
1274
                 class="give-register-password form-row form-row-one-third form-row-responsive">
1275
                <label for="give-user-pass-confirm-<?php echo $form_id; ?>">
1276
					<?php esc_html_e( 'Confirm PW', 'give' ); ?>
1277
					<?php if ( give_logged_in_only( $form_id ) ) { ?>
1278
                        <span class="give-required-indicator">*</span>
1279
					<?php } ?>
1280
                    <span class="give-tooltip give-icon give-icon-question"
1281
                          data-tooltip="<?php esc_attr_e( 'Please retype your password to confirm.', 'give' ); ?>"></span>
1282
                </label>
1283
1284
                <input name="give_user_pass_confirm" id="give-user-pass-confirm-<?php echo $form_id; ?>"
1285
                       class="give-input" placeholder="<?php esc_attr_e( 'Confirm password', 'give' ); ?>"
1286
                       type="password"<?php echo ( give_logged_in_only( $form_id ) ) ? ' required aria-required="true" ' : ''; ?>/>
1287
            </div>
1288
			<?php
1289
			/**
1290
			 * Fires while rendering user registration form, after account fields.
1291
			 *
1292
			 * @since 1.0
1293
			 *
1294
			 * @param int $form_id The form ID.
1295
			 */
1296
			do_action( 'give_register_account_fields_after', $form_id );
1297
			?>
1298
        </fieldset>
1299
1300
		<?php
1301
		/**
1302
		 * Fires while rendering user registration form, after registration fields.
1303
		 *
1304
		 * @since 1.0
1305
		 *
1306
		 * @param int $form_id The form ID.
1307
		 */
1308
		do_action( 'give_register_fields_after', $form_id );
1309
		?>
1310
1311
        <input type="hidden" name="give-purchase-var" value="needs-to-register"/>
1312
1313
		<?php
1314
		/**
1315
		 * Fire after register or login form render
1316
		 *
1317
		 * @since 1.7
1318
		 */
1319
		do_action( 'give_donation_form_user_info', $form_id );
1320
		?>
1321
1322
    </fieldset>
1323
	<?php
1324
	echo ob_get_clean();
1325
}
1326
1327
add_action( 'give_donation_form_register_fields', 'give_get_register_fields' );
1328
1329
/**
1330
 * Gets the login fields for the login form on the checkout. This function hooks
1331
 * on the give_donation_form_login_fields to display the login form if a user already
1332
 * had an account.
1333
 *
1334
 * @since  1.0
1335
 *
1336
 * @param  int $form_id The form ID.
1337
 *
1338
 * @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...
1339
 */
1340
function give_get_login_fields( $form_id ) {
1341
1342
	$form_id            = isset( $_POST['form_id'] ) ? $_POST['form_id'] : $form_id;
1343
	$show_register_form = give_show_login_register_option( $form_id );
1344
1345
	ob_start();
1346
	?>
1347
    <fieldset id="give-login-fields-<?php echo $form_id; ?>">
1348
        <legend><?php echo apply_filters( 'give_account_login_fieldset_heading', esc_html__( 'Login to Your Account', 'give' ) );
1349
			if ( ! give_logged_in_only( $form_id ) ) {
1350
				echo ' <span class="sub-text">' . esc_html__( '(optional)', 'give' ) . '</span>';
1351
			} ?>
1352
        </legend>
1353
		<?php if ( $show_register_form == 'both' ) { ?>
1354
            <p class="give-new-account-link">
1355
				<?php esc_html_e( 'Need to create an account?', 'give' ); ?>&nbsp;
1356
                <a href="<?php echo remove_query_arg( 'login' ); ?>" class="give-checkout-register-cancel"
1357
                   data-action="give_checkout_register">
1358
					<?php esc_html_e( 'Register', 'give' );
1359
					if ( ! give_logged_in_only( $form_id ) ) {
1360
						echo ' ' . esc_html__( 'or checkout as a guest &raquo;', 'give' );
1361
					} ?>
1362
                </a>
1363
            </p>
1364
            <p class="give-loading-text">
1365
                <span class="give-loading-animation"></span>
1366
            </p>
1367
		<?php } ?>
1368
		<?php
1369
		/**
1370
		 * Fires while rendering checkout login form, before the fields.
1371
		 *
1372
		 * @since 1.0
1373
		 *
1374
		 * @param int $form_id The form ID.
1375
		 */
1376
		do_action( 'give_checkout_login_fields_before', $form_id );
1377
		?>
1378
        <div id="give-user-login-wrap-<?php echo $form_id; ?>" class="form-row form-row-first form-row-responsive">
1379
            <label class="give-label" for="give-user-login-<?php echo $form_id; ?>">
1380
				<?php esc_html_e( 'Username', 'give' ); ?>
1381
				<?php if ( give_logged_in_only( $form_id ) ) { ?>
1382
                    <span class="give-required-indicator">*</span>
1383
				<?php } ?>
1384
            </label>
1385
1386
            <input class="give-input<?php echo ( give_logged_in_only( $form_id ) ) ? ' required' : ''; ?>" type="text"
1387
                   name="give_user_login" id="give-user-login-<?php echo $form_id; ?>" value=""
1388
                   placeholder="<?php esc_attr_e( 'Your username', 'give' ); ?>"<?php echo ( give_logged_in_only( $form_id ) ) ? ' required aria-required="true" ' : ''; ?>/>
1389
        </div>
1390
1391
        <div id="give-user-pass-wrap-<?php echo $form_id; ?>"
1392
             class="give_login_password form-row form-row-last form-row-responsive">
1393
            <label class="give-label" for="give-user-pass-<?php echo $form_id; ?>">
1394
				<?php esc_html_e( 'Password', 'give' ); ?>
1395
				<?php if ( give_logged_in_only( $form_id ) ) { ?>
1396
                    <span class="give-required-indicator">*</span>
1397
				<?php } ?>
1398
            </label>
1399
            <input class="give-input<?php echo ( give_logged_in_only( $form_id ) ) ? ' required' : ''; ?>"
1400
                   type="password" name="give_user_pass" id="give-user-pass-<?php echo $form_id; ?>"
1401
                   placeholder="<?php esc_attr_e( 'Your password', 'give' ); ?>"<?php echo ( give_logged_in_only( $form_id ) ) ? ' required aria-required="true" ' : ''; ?>/>
1402
            <input type="hidden" name="give-purchase-var" value="needs-to-login"/>
1403
        </div>
1404
1405
        <div id="give-forgot-password-wrap-<?php echo $form_id; ?>" class="give_login_forgot_password">
1406
			 <span class="give-forgot-password ">
1407
				 <a href="<?php echo wp_lostpassword_url() ?>"
1408
                    target="_blank"><?php esc_html_e( 'Reset Password', 'give' ) ?></a>
1409
			 </span>
1410
        </div>
1411
1412
        <div id="give-user-login-submit-<?php echo $form_id; ?>" class="give-clearfix">
1413
            <input type="submit" class="give-submit give-btn button" name="give_login_submit"
1414
                   value="<?php esc_attr_e( 'Login', 'give' ); ?>"/>
1415
			<?php if ( $show_register_form !== 'login' ) { ?>
1416
                <input type="button" data-action="give_cancel_login"
1417
                       class="give-cancel-login give-checkout-register-cancel give-btn button" name="give_login_cancel"
1418
                       value="<?php esc_attr_e( 'Cancel', 'give' ); ?>"/>
1419
			<?php } ?>
1420
            <span class="give-loading-animation"></span>
1421
        </div>
1422
		<?php
1423
		/**
1424
		 * Fires while rendering checkout login form, after the fields.
1425
		 *
1426
		 * @since 1.0
1427
		 *
1428
		 * @param int $form_id The form ID.
1429
		 */
1430
		do_action( 'give_checkout_login_fields_after', $form_id );
1431
		?>
1432
    </fieldset><!--end #give-login-fields-->
1433
	<?php
1434
	echo ob_get_clean();
1435
}
1436
1437
add_action( 'give_donation_form_login_fields', 'give_get_login_fields', 10, 1 );
1438
1439
/**
1440
 * Payment Mode Select.
1441
 *
1442
 * Renders the payment mode form by getting all the enabled payment gateways and
1443
 * outputting them as radio buttons for the user to choose the payment gateway. If
1444
 * a default payment gateway has been chosen from the Give Settings, it will be
1445
 * automatically selected.
1446
 *
1447
 * @since  1.0
1448
 *
1449
 * @param  int $form_id The form ID.
1450
 *
1451
 * @return void
1452
 */
1453
function give_payment_mode_select( $form_id ) {
1454
1455
	$gateways = give_get_enabled_payment_gateways( $form_id );
1456
1457
	/**
1458
	 * Fires while selecting payment gateways, before the fields.
1459
	 *
1460
	 * @since 1.7
1461
	 *
1462
	 * @param int $form_id The form ID.
1463
	 */
1464
	do_action( 'give_payment_mode_top', $form_id );
1465
	?>
1466
1467
    <fieldset id="give-payment-mode-select" <?php if ( count( $gateways ) <= 1 ) {
1468
		echo 'style="display: none;"';
1469
	} ?>>
1470
		<?php
1471
		/**
1472
		 * Fires while selecting payment gateways, before the wrap div.
1473
		 *
1474
		 * @since 1.7
1475
		 *
1476
		 * @param int $form_id The form ID.
1477
		 */
1478
		do_action( 'give_payment_mode_before_gateways_wrap' );
1479
		?>
1480
        <legend
1481
                class="give-payment-mode-label"><?php echo apply_filters( 'give_checkout_payment_method_text', esc_html__( 'Select Payment Method', 'give' ) ); ?>
1482
            <span class="give-loading-text"><span
1483
                        class="give-loading-animation"></span>
1484
            </span>
1485
        </legend>
1486
1487
        <div id="give-payment-mode-wrap">
1488
			<?php
1489
			/**
1490
			 * Fires while selecting payment gateways, befire the gateways list.
1491
			 *
1492
			 * @since 1.7
1493
			 */
1494
			do_action( 'give_payment_mode_before_gateways' )
1495
			?>
1496
            <ul id="give-gateway-radio-list">
1497
				<?php
1498
				/**
1499
				 * Loop through the active payment gateways.
1500
				 */
1501
				$selected_gateway  = give_get_chosen_gateway( $form_id );
1502
1503
				foreach ( $gateways as $gateway_id => $gateway ) :
1504
					//Determine the default gateway.
1505
					$checked = checked( $gateway_id, $selected_gateway, false );
1506
					$checked_class = $checked ? ' class="give-gateway-option-selected"' : ''; ?>
1507
                    <li<?php echo $checked_class ?>>
1508
                        <input type="radio" name="payment-mode" class="give-gateway"
1509
                               id="give-gateway-<?php echo esc_attr( $gateway_id ) . '-' . $form_id; ?>"
1510
                               value="<?php echo esc_attr( $gateway_id ); ?>"<?php echo $checked; ?>>
1511
                        <label for="give-gateway-<?php echo esc_attr( $gateway_id ) . '-' . $form_id; ?>"
1512
                               class="give-gateway-option"
1513
                               id="give-gateway-option-<?php echo esc_attr( $gateway_id ); ?>"> <?php echo esc_html( $gateway['checkout_label'] ); ?></label>
1514
                    </li>
1515
					<?php
1516
				endforeach;
1517
				?>
1518
            </ul>
1519
			<?php
1520
			/**
1521
			 * Fires while selecting payment gateways, before the gateways list.
1522
			 *
1523
			 * @since 1.7
1524
			 */
1525
			do_action( 'give_payment_mode_after_gateways' );
1526
			?>
1527
        </div>
1528
		<?php
1529
		/**
1530
		 * Fires while selecting payment gateways, after the wrap div.
1531
		 *
1532
		 * @since 1.7
1533
		 *
1534
		 * @param int $form_id The form ID.
1535
		 */
1536
		do_action( 'give_payment_mode_after_gateways_wrap' );
1537
		?>
1538
    </fieldset>
1539
1540
	<?php
1541
	/**
1542
	 * Fires while selecting payment gateways, after the fields.
1543
	 *
1544
	 * @since 1.7
1545
	 *
1546
	 * @param int $form_id The form ID.
1547
	 */
1548
	do_action( 'give_payment_mode_bottom', $form_id );
1549
	?>
1550
1551
    <div id="give_purchase_form_wrap">
1552
1553
		<?php
1554
		/**
1555
		 * Fire after payment field render.
1556
		 *
1557
		 * @since 1.7
1558
		 */
1559
		do_action( 'give_donation_form', $form_id );
1560
		?>
1561
1562
    </div>
1563
1564
	<?php
1565
	/**
1566
	 * Fire after donation form render.
1567
	 *
1568
	 * @since 1.7
1569
	 */
1570
	do_action( 'give_donation_form_wrap_bottom', $form_id );
1571
}
1572
1573
add_action( 'give_payment_mode_select', 'give_payment_mode_select' );
1574
1575
/**
1576
 * Renders the Checkout Agree to Terms, this displays a checkbox for users to
1577
 * agree the T&Cs set in the Give Settings. This is only displayed if T&Cs are
1578
 * set in the Give Settings.
1579
 *
1580
 * @since  1.0
1581
 *
1582
 * @param  int $form_id The form ID.
1583
 *
1584
 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|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...
1585
 */
1586
function give_terms_agreement( $form_id ) {
1587
	$form_option = get_post_meta( $form_id, '_give_terms_option', true );
1588
1589
	// Bailout if per form and global term and conditions is not setup.
1590
	if (
1591
		give_is_setting_enabled( $form_option, 'global' )
1592
		&& give_is_setting_enabled( give_get_option( 'terms' ) )
1593
	) {
1594
		$label         = give_get_option( 'agree_to_terms_label', esc_html__( 'Agree to Terms?', 'give' ) );
1595
		$terms         = $terms = give_get_option( 'agreement_text', '' );
1596
		$edit_term_url = admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=display&section=term-and-conditions' );
1597
1598
	} elseif ( give_is_setting_enabled( $form_option ) ) {
1599
		$label         = ( $label = get_post_meta( $form_id, '_give_agree_label', true ) ) ? stripslashes( $label ) : esc_html__( 'Agree to Terms?', 'give' );
1600
		$terms         = get_post_meta( $form_id, '_give_agree_text', true );
1601
		$edit_term_url = admin_url( 'post.php?post=' . $form_id . '&action=edit#form_terms_options' );
1602
1603
	} else {
1604
		return false;
1605
	}
1606
1607
	// Bailout: Check if term and conditions text is empty or not.
1608
	if ( empty( $terms ) ) {
1609
		if ( is_user_logged_in() && current_user_can( 'edit_give_forms' ) ) {
1610
			echo sprintf( __( 'Please enter valid terms and conditions in <a href="%s">this form\'s settings</a>.', 'give' ), $edit_term_url );
1611
		}
1612
1613
		return false;
1614
	}
1615
1616
	?>
1617
    <fieldset id="give_terms_agreement">
1618
        <legend><?php echo apply_filters( 'give_terms_agreement_text', esc_html__( 'Terms', 'give' ) ); ?></legend>
1619
        <div id="give_terms" class="give_terms-<?php echo $form_id; ?>" style="display:none;">
1620
			<?php
1621
			/**
1622
			 * Fires while rendering terms of agreement, before the fields.
1623
			 *
1624
			 * @since 1.0
1625
			 */
1626
			do_action( 'give_before_terms' );
1627
1628
			echo wpautop( stripslashes( $terms ) );
1629
			/**
1630
			 * Fires while rendering terms of agreement, after the fields.
1631
			 *
1632
			 * @since 1.0
1633
			 */
1634
			do_action( 'give_after_terms' );
1635
			?>
1636
        </div>
1637
        <div id="give_show_terms">
1638
            <a href="#" class="give_terms_links give_terms_links-<?php echo $form_id; ?>" role="button"
1639
               aria-controls="give_terms"><?php esc_html_e( 'Show Terms', 'give' ); ?></a>
1640
            <a href="#" class="give_terms_links give_terms_links-<?php echo $form_id; ?>" role="button"
1641
               aria-controls="give_terms" style="display:none;"><?php esc_html_e( 'Hide Terms', 'give' ); ?></a>
1642
        </div>
1643
1644
        <input name="give_agree_to_terms" class="required" type="checkbox"
1645
               id="give_agree_to_terms-<?php echo $form_id; ?>" value="1" required aria-required="true"/>
1646
        <label for="give_agree_to_terms-<?php echo $form_id; ?>"><?php echo $label; ?></label>
1647
1648
    </fieldset>
1649
	<?php
1650
}
1651
1652
add_action( 'give_donation_form_after_cc_form', 'give_terms_agreement', 8888, 1 );
1653
1654
/**
1655
 * Checkout Final Total.
1656
 *
1657
 * Shows the final donation total at the bottom of the checkout page.
1658
 *
1659
 * @since  1.0
1660
 *
1661
 * @param  int $form_id The form ID.
1662
 *
1663
 * @return void
1664
 */
1665
function give_checkout_final_total( $form_id ) {
1666
1667
	if ( isset( $_POST['give_total'] ) ) {
1668
		$total = apply_filters( 'give_donation_total', $_POST['give_total'] );
1669
	} else {
1670
		//default total.
1671
		$total = give_get_default_form_amount( $form_id );
1672
	}
1673
	//Only proceed if give_total available.
1674
	if ( empty( $total ) ) {
1675
		return;
1676
	}
1677
	?>
1678
    <p id="give-final-total-wrap" class="form-wrap ">
1679
		<span
1680
                class="give-donation-total-label"><?php echo apply_filters( 'give_donation_total_label', esc_html__( 'Donation Total:', 'give' ) ); ?></span>
1681
        <span class="give-final-total-amount"
1682
              data-total="<?php echo give_format_amount( $total ); ?>"><?php echo give_currency_filter( give_format_amount( $total ) ); ?></span>
1683
    </p>
1684
	<?php
1685
}
1686
1687
add_action( 'give_donation_form_before_submit', 'give_checkout_final_total', 999 );
1688
1689
/**
1690
 * Renders the Checkout Submit section.
1691
 *
1692
 * @since  1.0
1693
 *
1694
 * @param  int $form_id The form ID.
1695
 *
1696
 * @return void
1697
 */
1698
function give_checkout_submit( $form_id ) {
1699
	?>
1700
    <fieldset id="give_purchase_submit">
1701
		<?php
1702
		/**
1703
		 * Fire before donation form submit.
1704
		 *
1705
		 * @since 1.7
1706
		 */
1707
		do_action( 'give_donation_form_before_submit', $form_id );
1708
1709
		give_checkout_hidden_fields( $form_id );
1710
1711
		echo give_checkout_button_purchase( $form_id );
1712
1713
		/**
1714
		 * Fire after donation form submit.
1715
		 *
1716
		 * @since 1.7
1717
		 */
1718
		do_action( 'give_donation_form_after_submit', $form_id );
1719
		?>
1720
    </fieldset>
1721
	<?php
1722
}
1723
1724
add_action( 'give_donation_form_after_cc_form', 'give_checkout_submit', 9999 );
1725
1726
/**
1727
 * Give Checkout Button.
1728
 *
1729
 * Renders the button on the Checkout.
1730
 *
1731
 * @since  1.0
1732
 *
1733
 * @param  int $form_id The form ID.
1734
 *
1735
 * @return string
1736
 */
1737
function give_checkout_button_purchase( $form_id ) {
1738
1739
	$display_label_field = get_post_meta( $form_id, '_give_checkout_label', true );
1740
	$display_label       = ( ! empty( $display_label_field ) ? $display_label_field : esc_html__( 'Donate Now', 'give' ) );
1741
	ob_start(); ?>
1742
    <div class="give-submit-button-wrap give-clearfix">
1743
        <input type="submit" class="give-submit give-btn" id="give-purchase-button" name="give-purchase"
1744
               value="<?php echo $display_label; ?>"/>
1745
        <span class="give-loading-animation"></span>
1746
    </div>
1747
	<?php
1748
	return apply_filters( 'give_checkout_button_purchase', ob_get_clean(), $form_id );
1749
}
1750
1751
/**
1752
 * Give Agree to Terms.
1753
 *
1754
 * Outputs the JavaScript code for the Agree to Terms section to toggle the T&Cs text.
1755
 *
1756
 * @since  1.0
1757
 *
1758
 * @param  int $form_id The form ID.
1759
 *
1760
 * @return void
1761
 */
1762
function give_agree_to_terms_js( $form_id ) {
1763
1764
	$form_option = get_post_meta( $form_id, '_give_terms_option', true );
1765
1766
	if ( give_is_setting_enabled( $form_option, array( 'enabled', 'global' ) ) ) {
1767
		?>
1768
        <script type="text/javascript">
1769
			jQuery(document).ready(function ($) {
1770
				$('body').on('click', '.give_terms_links-<?php echo $form_id;?>', function (e) {
1771
					e.preventDefault();
1772
					$('.give_terms-<?php echo $form_id;?>').slideToggle();
1773
					$('.give_terms_links-<?php echo $form_id;?>').toggle();
1774
					return false;
1775
				});
1776
			});
1777
        </script>
1778
		<?php
1779
	}
1780
}
1781
1782
add_action( 'give_checkout_form_top', 'give_agree_to_terms_js', 10, 2 );
1783
1784
/**
1785
 * Show Give Goals.
1786
 *
1787
 * @since  1.0
1788
 * @since  1.6   Add template for Give Goals Shortcode.
1789
 *               More info is on https://github.com/WordImpress/Give/issues/411
1790
 *
1791
 * @param  int   $form_id The form ID.
1792
 * @param  array $args    An array of form arguments.
1793
 *
1794
 * @return mixed
1795
 */
1796
function give_show_goal_progress( $form_id, $args ) {
1797
1798
	ob_start();
1799
	give_get_template( 'shortcode-goal', array( 'form_id' => $form_id, 'args' => $args ) );
1800
1801
	echo apply_filters( 'give_goal_output', ob_get_clean() );
1802
1803
	return true;
1804
}
1805
1806
add_action( 'give_pre_form', 'give_show_goal_progress', 10, 2 );
1807
1808
1809
/**
1810
 * Get form content position.
1811
 *
1812
 * @since  1.8
1813
 *
1814
 * @param  $form_id
1815
 * @param  $args
1816
 *
1817
 * @return mixed|string
1818
 */
1819
function give_get_form_content_placement( $form_id, $args ) {
1820
	$show_content = '';
1821
1822
	if ( isset( $args['show_content'] ) && ! empty( $args['show_content'] ) ) {
1823
		// Content positions.
1824
		$content_placement = array(
1825
			'above' => 'give_pre_form',
1826
			'below' => 'give_post_form',
1827
		);
1828
1829
		// Check if content position already decoded.
1830
		if ( in_array( $args['show_content'], $content_placement ) ) {
1831
			return $args['show_content'];
1832
		}
1833
1834
		$show_content = ( 'none' !== $args['show_content'] ? $content_placement[ $args['show_content'] ] : '' );
1835
1836
	} elseif ( give_is_setting_enabled( get_post_meta( $form_id, '_give_display_content', true ) ) ) {
1837
		$show_content = get_post_meta( $form_id, '_give_content_placement', true );
1838
1839
	} elseif ( 'none' !== get_post_meta( $form_id, '_give_content_option', true ) ) {
1840
		// Backward compatibility for _give_content_option for v18.
1841
		$show_content = get_post_meta( $form_id, '_give_content_option', true );
1842
	}
1843
1844
	return $show_content;
1845
}
1846
1847
/**
1848
 * Adds Actions to Render Form Content.
1849
 *
1850
 * @since  1.0
1851
 *
1852
 * @param  int   $form_id The form ID.
1853
 * @param  array $args    An array of form arguments.
1854
 *
1855
 * @return void|bool
1856
 */
1857
function give_form_content( $form_id, $args ) {
1858
1859
	$show_content = give_get_form_content_placement( $form_id, $args );
1860
1861
	// Bailout.
1862
	if ( empty( $show_content ) ) {
1863
		return false;
1864
	}
1865
1866
	// Add action according to value.
1867
	add_action( $show_content, 'give_form_display_content', 10, 2 );
1868
}
1869
1870
add_action( 'give_pre_form_output', 'give_form_content', 10, 2 );
1871
1872
/**
1873
 * Renders Post Form Content.
1874
 *
1875
 * Displays content for Give forms; fired by action from give_form_content.
1876
 *
1877
 * @since  1.0
1878
 *
1879
 * @param  int   $form_id The form ID.
1880
 * @param  array $args    An array of form arguments.
1881
 *
1882
 * @return void
1883
 */
1884
function give_form_display_content( $form_id, $args ) {
1885
1886
	$content      = wpautop( get_post_meta( $form_id, '_give_form_content', true ) );
1887
	$show_content = give_get_form_content_placement( $form_id, $args );
1888
1889
	if ( give_is_setting_enabled( give_get_option( 'the_content_filter' ) ) ) {
1890
		$content = apply_filters( 'the_content', $content );
1891
	}
1892
1893
	$output = '<div id="give-form-content-' . $form_id . '" class="give-form-content-wrap ' . $show_content . '-content">' . $content . '</div>';
1894
1895
	echo apply_filters( 'give_form_content_output', $output );
1896
1897
	//remove action to prevent content output on addition forms on page.
1898
	//@see: https://github.com/WordImpress/Give/issues/634.
1899
	remove_action( $show_content, 'give_form_display_content' );
1900
}
1901
1902
/**
1903
 * Renders the hidden Checkout fields.
1904
 *
1905
 * @since 1.0
1906
 *
1907
 * @param  int $form_id The form ID.
1908
 *
1909
 * @return void
1910
 */
1911
function give_checkout_hidden_fields( $form_id ) {
1912
1913
	/**
1914
	 * Fires while rendering hidden checkout fields, before the fields.
1915
	 *
1916
	 * @since 1.0
1917
	 *
1918
	 * @param int $form_id The form ID.
1919
	 */
1920
	do_action( 'give_hidden_fields_before', $form_id );
1921
1922
	if ( is_user_logged_in() ) { ?>
1923
        <input type="hidden" name="give-user-id" value="<?php echo get_current_user_id(); ?>"/>
1924
	<?php } ?>
1925
    <input type="hidden" name="give_action" value="purchase"/>
1926
    <input type="hidden" name="give-gateway" value="<?php echo give_get_chosen_gateway( $form_id ); ?>"/>
1927
	<?php
1928
	/**
1929
	 * Fires while rendering hidden checkout fields, after the fields.
1930
	 *
1931
	 * @since 1.0
1932
	 *
1933
	 * @param int $form_id The form ID.
1934
	 */
1935
	do_action( 'give_hidden_fields_after', $form_id );
1936
1937
}
1938
1939
/**
1940
 * Filter Success Page Content.
1941
 *
1942
 * Applies filters to the success page content.
1943
 *
1944
 * @since 1.0
1945
 *
1946
 * @param  string $content Content before filters.
1947
 *
1948
 * @return string $content Filtered content.
1949
 */
1950
function give_filter_success_page_content( $content ) {
1951
1952
	$give_options = give_get_settings();
1953
1954
	if ( isset( $give_options['success_page'] ) && isset( $_GET['payment-confirmation'] ) && is_page( $give_options['success_page'] ) ) {
1955
		if ( has_filter( 'give_payment_confirm_' . $_GET['payment-confirmation'] ) ) {
1956
			$content = apply_filters( 'give_payment_confirm_' . $_GET['payment-confirmation'], $content );
1957
		}
1958
	}
1959
1960
	return $content;
1961
}
1962
1963
add_filter( 'the_content', 'give_filter_success_page_content' );
1964
1965
/**
1966
 * Test Mode Frontend Warning.
1967
 *
1968
 * Displays a notice on the frontend for donation forms.
1969
 *
1970
 * @since 1.1
1971
 */
1972
function give_test_mode_frontend_warning() {
1973
1974
	if ( give_is_test_mode() ) {
1975
		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 donations are processed.', 'give' ) . '</p></div>';
1976
	}
1977
}
1978
1979
add_action( 'give_pre_form', 'give_test_mode_frontend_warning', 10 );
1980
1981
/**
1982
 * Members-only Form.
1983
 *
1984
 * If "Disable Guest Donations" and "Display Register / Login" is set to none.
1985
 *
1986
 * @since  1.4.1
1987
 *
1988
 * @param  string $final_output
1989
 * @param  array  $args
1990
 *
1991
 * @return string
1992
 */
1993
function give_members_only_form( $final_output, $args ) {
1994
1995
	$form_id = isset( $args['form_id'] ) ? $args['form_id'] : 0;
1996
1997
	//Sanity Check: Must have form_id & not be logged in.
1998
	if ( empty( $form_id ) || is_user_logged_in() ) {
1999
		return $final_output;
2000
	}
2001
2002
	//Logged in only and Register / Login set to none.
2003
	if ( give_logged_in_only( $form_id ) && give_show_login_register_option( $form_id ) == 'none' ) {
2004
2005
		$final_output = give_output_error( esc_html__( 'Please log in in order to complete your donation.', 'give' ), false );
2006
2007
		return apply_filters( 'give_members_only_output', $final_output, $form_id );
2008
2009
	}
2010
2011
	return $final_output;
2012
2013
}
2014
2015
add_filter( 'give_donate_form', 'give_members_only_form', 10, 2 );
2016