Completed
Push — es6/issue-1475 ( 93c1ad )
by Ravinder
1139:39 queued 1133:44
created

shortcodes.php ➔ give_totals_shortcode()   C

Complexity

Conditions 13
Paths 136

Size

Total Lines 132

Duplication

Lines 14
Ratio 10.61 %

Importance

Changes 0
Metric Value
cc 13
nc 136
nop 1
dl 14
loc 132
rs 5.0533
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
2
/**
3
 * Give Shortcodes
4
 *
5
 * @package     Give
6
 * @subpackage  Shortcodes
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Donation History Shortcode
19
 *
20
 * Displays a user's donation history.
21
 *
22
 * @since  1.0
23
 *
24
 * @return string|bool
25
 */
26
function give_donation_history( $atts ) {
27
28
	$donation_history_args = shortcode_atts( array(
29
		'id'             => true,
30
		'date'           => true,
31
		'donor'          => false,
32
		'amount'         => true,
33
		'status'         => false,
34
		'payment_method' => false,
35
	), $atts, 'donation_history' );
36
37
	// Always show receipt link.
38
	$donation_history_args['details'] = true;
39
40
	// Set Donation History Shortcode Arguments in session variable.
41
	Give()->session->set( 'give_donation_history_args', $donation_history_args );
42
43
	// If payment_key query arg exists, return receipt instead of donation history.
44
	if ( isset( $_GET['payment_key'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
45
		ob_start();
46
47
		echo give_receipt_shortcode( array() );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_receipt_shortcode'
Loading history...
48
49
		// Display donation history link only if Receipt Access Session is available.
50
		if ( give_get_receipt_session() ) {
51
			echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
52
				'<a href="%s">%s</a>',
53
				esc_url( give_get_history_page_uri() ),
54
				__( '&laquo; Return to All Donations', 'give' )
55
			);
56
		}
57
		return ob_get_clean();
58
	}
59
60
	$email_access = give_get_option( 'email_access' );
61
62
	/**
63
	 * Determine access
64
	 *
65
	 * a. Check if a user is logged in or does a session exists
66
	 * b. Does an email-access token exist?
67
	 */
68
	if (
69
		is_user_logged_in() ||
70
		false !== Give()->session->get_session_expiration() ||
71
		( give_is_setting_enabled( $email_access ) && Give()->email_access->token_exists ) ||
72
		true === give_get_history_session()
73
	) {
74
		ob_start();
75
		give_get_template_part( 'history', 'donations' );
76
77
		return ob_get_clean();
78
79
	} elseif ( give_is_setting_enabled( $email_access ) ) {
80
		// Is Email-based access enabled?
81
		ob_start();
82
		give_get_template_part( 'email', 'login-form' );
83
84
		return ob_get_clean();
85
86
	} else {
87
88
		$output = apply_filters( 'give_donation_history_nonuser_message', Give()->notices->print_frontend_notice( __( 'You must be logged in to view your donation history. Please login using your account or create an account using the same email you used to donate with.', 'give' ), false ) );
89
		$output .= do_shortcode( '[give_login]' );
90
91
		return $output;
92
	}
93
}
94
95
add_shortcode( 'donation_history', 'give_donation_history' );
96
97
/**
98
 * Donation Form Shortcode
99
 *
100
 * Show the Give donation form.
101
 *
102
 * @since  1.0
103
 *
104
 * @param  array $atts Shortcode attributes
105
 *
106
 * @return string
107
 */
108
function give_form_shortcode( $atts ) {
109
	$atts = shortcode_atts( array(
110
		'id'                    => '',
111
		'show_title'            => true,
112
		'show_goal'             => true,
113
		'show_content'          => '',
114
		'float_labels'          => '',
115
		'display_style'         => '',
116
		'continue_button_title' => '',
117
	), $atts, 'give_form' );
118
119
	// Convert string to bool.
120
	$atts['show_title'] = filter_var( $atts['show_title'], FILTER_VALIDATE_BOOLEAN );
121
	$atts['show_goal']  = filter_var( $atts['show_goal'], FILTER_VALIDATE_BOOLEAN );
122
123
	// get the Give Form
124
	ob_start();
125
	give_get_donation_form( $atts );
126
	$final_output = ob_get_clean();
127
128
	return apply_filters( 'give_donate_form', $final_output, $atts );
129
}
130
131
add_shortcode( 'give_form', 'give_form_shortcode' );
132
133
/**
134
 * Donation Form Goal Shortcode.
135
 *
136
 * Show the Give donation form goals.
137
 *
138
 * @since  1.0
139
 *
140
 * @param  array $atts Shortcode attributes.
141
 *
142
 * @return string
143
 */
144
function give_goal_shortcode( $atts ) {
145
	$atts = shortcode_atts( array(
146
		'id'        => '',
147
		'show_text' => true,
148
		'show_bar'  => true,
149
	), $atts, 'give_goal' );
150
151
	// get the Give Form.
152
	ob_start();
153
154
	// Sanity check 1: ensure there is an ID Provided.
155
	if ( empty( $atts['id'] ) ) {
156
		Give()->notices->print_frontend_notice( __( 'The shortcode is missing Donation Form ID attribute.', 'give' ), true );
157
	}
158
159
	// Sanity check 2: Check the form even has Goals enabled.
160
	if ( ! give_is_setting_enabled( give_get_meta( $atts['id'], '_give_goal_option', true ) ) ) {
161
162
		Give()->notices->print_frontend_notice( __( 'The form does not have Goals enabled.', 'give' ), true );
163
	} else {
164
		// Passed all sanity checks: output Goal.
165
		give_show_goal_progress( $atts['id'], $atts );
166
	}
167
168
	$final_output = ob_get_clean();
169
170
	return apply_filters( 'give_goal_shortcode_output', $final_output, $atts );
171
}
172
173
add_shortcode( 'give_goal', 'give_goal_shortcode' );
174
175
176
/**
177
 * Login Shortcode.
178
 *
179
 * Shows a login form allowing users to users to log in. This function simply
180
 * calls the give_login_form function to display the login form.
181
 *
182
 * @since  1.0
183
 *
184
 * @param  array $atts Shortcode attributes.
185
 *
186
 * @uses   give_login_form()
187
 *
188
 * @return string
189
 */
190
function give_login_form_shortcode( $atts ) {
191
192
	$atts = shortcode_atts( array(
193
		// Add backward compatibility for redirect attribute.
194
		'redirect' => '',
195
		'login-redirect'  => '',
196
		'logout-redirect' => '',
197
	), $atts, 'give_login' );
198
199
	// Check login-redirect attribute first, if it empty or not found then check for redirect attribute and add value of this to login-redirect attribute.
200
	$atts['login-redirect'] = ! empty( $atts['login-redirect'] ) ? $atts['login-redirect'] : ( ! empty( $atts['redirect'] ) ? $atts['redirect'] : '' );
201
202
	return give_login_form( $atts['login-redirect'], $atts['logout-redirect'] );
203
}
204
205
add_shortcode( 'give_login', 'give_login_form_shortcode' );
206
207
/**
208
 * Register Shortcode.
209
 *
210
 * Shows a registration form allowing users to users to register for the site.
211
 *
212
 * @since  1.0
213
 *
214
 * @param  array $atts Shortcode attributes.
215
 *
216
 * @uses   give_register_form()
217
 *
218
 * @return string
219
 */
220
function give_register_form_shortcode( $atts ) {
221
	$atts = shortcode_atts( array(
222
		'redirect' => '',
223
	), $atts, 'give_register' );
224
225
	return give_register_form( $atts['redirect'] );
226
}
227
228
add_shortcode( 'give_register', 'give_register_form_shortcode' );
229
230
/**
231
 * Receipt Shortcode.
232
 *
233
 * Shows a donation receipt.
234
 *
235
 * @since  1.0
236
 *
237
 * @param  array $atts Shortcode attributes.
238
 *
239
 * @return string
240
 */
241
function give_receipt_shortcode( $atts ) {
242
243
	global $give_receipt_args;
244
245
	$give_receipt_args = shortcode_atts( array(
246
		'error'          => __( 'You are missing the payment key to view this donation receipt.', 'give' ),
247
		'price'          => true,
248
		'donor'          => true,
249
		'date'           => true,
250
		'payment_key'    => false,
251
		'payment_method' => true,
252
		'payment_id'     => true,
253
		'payment_status' => false,
254
		'company_name'   => false,
255
		'status_notice'  => true,
256
	), $atts, 'give_receipt' );
257
258
	// set $session var
259
	$session = give_get_purchase_session();
260
261
	// set payment key var
262
	if ( isset( $_GET['payment_key'] ) ) {
263
		$payment_key = urldecode( $_GET['payment_key'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
264
	} elseif ( $session ) {
265
		$payment_key = $session['purchase_key'];
266
	} elseif ( $give_receipt_args['payment_key'] ) {
267
		$payment_key = $give_receipt_args['payment_key'];
268
	}
269
270
	$email_access = give_get_option( 'email_access' );
271
272
	// No payment_key found & Email Access is Turned on.
273
	if ( ! isset( $payment_key ) && give_is_setting_enabled( $email_access ) && ! Give()->email_access->token_exists ) {
274
275
		ob_start();
276
277
		give_get_template_part( 'email-login-form' );
278
279
		return ob_get_clean();
280
281
	} elseif ( ! isset( $payment_key ) ) {
282
283
		return Give()->notices->print_frontend_notice( $give_receipt_args['error'], false, 'error' );
284
285
	}
286
287
	$user_can_view = give_can_view_receipt( $payment_key );
288
289
	// Key was provided, but user is logged out. Offer them the ability to login and view the receipt.
290
	if ( ! $user_can_view && give_is_setting_enabled( $email_access ) && ! Give()->email_access->token_exists ) {
291
292
		ob_start();
293
294
		give_get_template_part( 'email-login-form' );
295
296
		return ob_get_clean();
297
298
	} elseif ( ! $user_can_view ) {
299
300
		global $give_login_redirect;
301
302
		$give_login_redirect = give_get_current_page_url();
303
304
		ob_start();
305
306
		Give()->notices->print_frontend_notice( apply_filters( 'give_must_be_logged_in_error_message', __( 'You must be logged in to view this donation receipt.', 'give' ) ) );
307
308
		give_get_template_part( 'shortcode', 'login' );
309
310
		$login_form = ob_get_clean();
311
312
		return $login_form;
313
	}
314
315
	/**
316
	 * Check if the user has permission to view the receipt.
317
	 *
318
	 * If user is logged in, user ID is compared to user ID of ID stored in payment meta
319
	 * or if user is logged out and donation was made as a guest, the donation session is checked for
320
	 * or if user is logged in and the user can view sensitive shop data.
321
	 */
322
	if ( ! apply_filters( 'give_user_can_view_receipt', $user_can_view, $give_receipt_args ) ) {
323
		return Give()->notices->print_frontend_notice( $give_receipt_args['error'], false, 'error' );
324
	}
325
326
	ob_start();
327
328
	give_get_template_part( 'shortcode', 'receipt' );
329
330
	$display = ob_get_clean();
331
332
	return $display;
333
}
334
335
add_shortcode( 'give_receipt', 'give_receipt_shortcode' );
336
337
/**
338
 * Profile Editor Shortcode.
339
 *
340
 * Outputs the Give Profile Editor to allow users to amend their details from the
341
 * front-end. This function uses the Give templating system allowing users to
342
 * override the default profile editor template. The profile editor template is located
343
 * under templates/profile-editor.php, however, it can be altered by creating a
344
 * file called profile-editor.php in the give_template directory in your active theme's
345
 * folder. Please visit the Give Documentation for more information on how the
346
 * templating system is used.
347
 *
348
 * @since  1.0
349
 *
350
 * @param  array $atts Shortcode attributes.
351
 *
352
 * @return string Output generated from the profile editor
353
 */
354
function give_profile_editor_shortcode( $atts ) {
0 ignored issues
show
Unused Code introduced by
The parameter $atts is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
355
356
	ob_start();
357
358
	// Restrict access to donor profile, if donor and user are disconnected.
359
	$is_donor_disconnected = get_user_meta( get_current_user_id(), '_give_is_donor_disconnected', true );
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
360
	if ( is_user_logged_in() && $is_donor_disconnected ) {
361
		Give()->notices->print_frontend_notice( __( 'Your Donor and User profile are no longer connected. Please contact the site administrator.', 'give' ), true, 'error' );
362
		return false;
363
	}
364
365
	give_get_template_part( 'shortcode', 'profile-editor' );
366
367
	$display = ob_get_clean();
368
369
	return $display;
370
}
371
372
add_shortcode( 'give_profile_editor', 'give_profile_editor_shortcode' );
373
374
/**
375
 * Process Profile Updater Form.
376
 *
377
 * Processes the profile updater form by updating the necessary fields.
378
 *
379
 * @since  1.0
380
 *
381
 * @param  array $data Data sent from the profile editor.
382
 *
383
 * @return bool
384
 */
385
function give_process_profile_editor_updates( $data ) {
386
	// Profile field change request.
387
	if ( empty( $_POST['give_profile_editor_submit'] ) && ! is_user_logged_in() ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
388
		return false;
389
	}
390
391
	// Nonce security.
392
	if ( ! wp_verify_nonce( $data['give_profile_editor_nonce'], 'give-profile-editor-nonce' ) ) {
393
		return false;
394
	}
395
396
	$user_id       = get_current_user_id();
397
	$old_user_data = get_userdata( $user_id );
398
399
	/* @var Give_Donor $donor */
400
	$donor            = new Give_Donor( $user_id, true );
401
	$old_company_name = $donor->get_company_name();
402
403
	$display_name     = isset( $data['give_display_name'] ) ? sanitize_text_field( $data['give_display_name'] ) : $old_user_data->display_name;
404
	$first_name       = isset( $data['give_first_name'] ) ? sanitize_text_field( $data['give_first_name'] ) : $old_user_data->first_name;
405
	$last_name        = isset( $data['give_last_name'] ) ? sanitize_text_field( $data['give_last_name'] ) : $old_user_data->last_name;
406
	$company_name     = ! empty( $data['give_company_name'] ) ? sanitize_text_field( $data['give_company_name'] ) : $old_company_name;
407
	$email            = isset( $data['give_email'] ) ? sanitize_email( $data['give_email'] ) : $old_user_data->user_email;
408
	$password         = ! empty( $data['give_new_user_pass1'] ) ? $data['give_new_user_pass1'] : '';
409
	$confirm_password = ! empty( $data['give_new_user_pass2'] ) ? $data['give_new_user_pass2'] : '';
410
411
	$userdata = array(
412
		'ID'           => $user_id,
413
		'first_name'   => $first_name,
414
		'last_name'    => $last_name,
415
		'display_name' => $display_name,
416
		'user_email'   => $email,
417
		'user_pass'    => $password,
418
		'company_name' => $company_name,
419
	);
420
421
	/**
422
	 * Fires before updating user profile.
423
	 *
424
	 * @since 1.0
425
	 *
426
	 * @param int $user_id The ID of the user.
427
	 * @param array $userdata User info, including ID, first name, last name, display name and email.
428
	 */
429
	do_action( 'give_pre_update_user_profile', $user_id, $userdata );
430
431
	// Make sure to validate first name of existing donors.
432
	if ( empty( $first_name ) ) {
433
		// Empty First Name.
434
		give_set_error( 'empty_first_name', __( 'Please enter your first name.', 'give' ) );
435
	}
436
437
	// Make sure to validate passwords for existing Donors.
438
	give_validate_user_password( $password, $confirm_password );
439
440
	if ( empty( $email ) ) {
441
		// Make sure email should not be empty.
442
		give_set_error( 'email_empty', __( 'The email you entered is empty.', 'give' ) );
443
444
	} elseif ( ! is_email( $email ) ) {
445
		// Make sure email should be valid.
446
		give_set_error( 'email_not_valid', __( 'The email you entered is not valid. Please use another', 'give' ) );
447
448
	} elseif ( $email != $old_user_data->user_email ) {
449
		// Make sure the new email doesn't belong to another user.
450
		if ( email_exists( $email ) ) {
451
			give_set_error( 'user_email_exists', __( 'The email you entered belongs to another user. Please use another.', 'give' ) );
452
		} elseif ( Give()->donors->get_donor_by( 'email', $email ) ) {
453
			// Make sure the new email doesn't belong to another user.
454
			give_set_error( 'donor_email_exists', __( 'The email you entered belongs to another donor. Please use another.', 'give' ) );
455
		}
456
	}
457
458
	// Check for errors.
459
	$errors = give_get_errors();
460
461
	if ( $errors ) {
462
		// Send back to the profile editor if there are errors.
463
		wp_redirect( $data['give_redirect'] );
464
		give_die();
465
	}
466
467
	// Update Donor First Name and Last Name.
468
	Give()->donors->update( $donor->id, array(
469
		'name' => trim( "{$first_name} {$last_name}" ),
470
	) );
471
	Give()->donor_meta->update_meta( $donor->id, '_give_donor_first_name', $first_name );
472
	Give()->donor_meta->update_meta( $donor->id, '_give_donor_last_name', $last_name );
473
	Give()->donor_meta->update_meta( $donor->id, '_give_donor_company', $company_name );
474
475
	$current_user = wp_get_current_user();
476
477
	// Compares new values with old values to detect change in values.
478
	$email_update        = ( $email !== $current_user->user_email ) ? true : false;
479
	$display_name_update = ( $display_name !== $current_user->display_name ) ? true : false;
480
	$first_name_update   = ( $first_name !== $current_user->first_name ) ? true : false;
481
	$last_name_update    = ( $last_name !== $current_user->last_name ) ? true : false;
482
	$company_name_update = ( $company_name !== $old_company_name ) ? true : false;
483
	$update_code         = 0;
484
485
	/**
486
	 * True if update is done in display name, first name, last name or email.
487
	 *
488
	 * @var boolean
489
	 */
490
	$profile_update = ( $email_update || $display_name_update || $first_name_update || $last_name_update || $company_name_update );
491
492
	/**
493
	 * True if password fields are filled.
494
	 *
495
	 * @var boolean
496
	 */
497
	$password_update = ( ! empty( $password ) && ! empty( $confirm_password ) );
498
499
	if ( $profile_update ) {
500
501
		// If only profile fields are updated.
502
		$update_code = '1';
503
504
		if ( $password_update ) {
505
506
			// If profile fields AND password both are updated.
507
			$update_code = '2';
508
		}
509
	} elseif ( $password_update ) {
510
511
		// If only password is updated.
512
		$update_code = '3';
513
	}
514
515
	// Update the user.
516
	$updated = wp_update_user( $userdata );
517
518
	if ( $updated ) {
519
520
		/**
521
		 * Fires after updating user profile.
522
		 *
523
		 * @since 1.0
524
		 *
525
		 * @param int $user_id The ID of the user.
526
		 * @param array $userdata User info, including ID, first name, last name, display name and email.
527
		 */
528
		do_action( 'give_user_profile_updated', $user_id, $userdata );
529
530
		$profile_edit_redirect_args = array(
531
			'updated'     => 'true',
532
			'update_code' => $update_code,
533
		);
534
535
		/**
536
		 * Update codes '2' and '3' indicate a password change.
537
		 * If the password is changed, then logout and redirect to the same page.
538
		 */
539
		if ( '2' === $update_code || '3' === $update_code ) {
540
			wp_logout( wp_redirect( add_query_arg( $profile_edit_redirect_args, $data['give_redirect'] ) ) );
541
		} else {
542
			wp_redirect( add_query_arg( $profile_edit_redirect_args, $data['give_redirect'] ) );
543
		}
544
545
		give_die();
546
	}
547
548
	return false;
549
}
550
551
add_action( 'give_edit_user_profile', 'give_process_profile_editor_updates' );
552
553
554
/**
555
 * Give totals Shortcode.
556
 *
557
 * Shows a donation total.
558
 *
559
 * @since  2.1
560
 *
561
 * @param  array $atts Shortcode attributes.
562
 *
563
 * @return string
564
 */
565
function give_totals_shortcode( $atts ) {
566
	$total = get_option( 'give_earnings_total', false );
567
568
	$message = apply_filters( 'give_totals_message', __( 'Hey! We\'ve raised {total} of the {total_goal} we are trying to raise for this campaign!', 'give' ) );
569
570
	$atts = shortcode_atts( array(
571
		'total_goal'   => 0, // integer
572
		'ids'          => 0, // integer|array
573
		'cats'         => 0, // integer|array
574
		'tags'         => 0, // integer|array
575
		'message'      => $message,
576
		'link'         => '', // URL
577
		'link_text'    => __( 'Donate Now', 'give' ), // string,
578
		'progress_bar' => true, // boolean
579
	), $atts, 'give_totals' );
580
581
	// Total Goal.
582
	$total_goal = give_maybe_sanitize_amount( $atts['total_goal'] );
583
584
	// Build query based on cat, tag and Form ids.
585
	if ( ! empty( $atts['cats'] ) || ! empty( $atts['tags'] ) || ! empty( $atts['ids'] ) ) {
586
587
		$form_ids = array();
588
		if ( ! empty( $atts['ids'] ) ) {
589
			$form_ids = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
590
		}
591
592
		$form_args = array(
593
			'post_type'      => 'give_forms',
594
			'post_status'    => 'publish',
595
			'post__in'       => $form_ids,
596
			'posts_per_page' => - 1,
597
			'fields'         => 'ids',
598
			'tax_query'      => array(
0 ignored issues
show
introduced by
Detected usage of tax_query, possible slow query.
Loading history...
599
				'relation' => 'AND',
600
			),
601
		);
602
603 View Code Duplication
		if ( ! empty( $atts['cats'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
604
			$cats                     = array_filter( array_map( 'trim', explode( ',', $atts['cats'] ) ) );
605
			$form_args['tax_query'][] = array(
606
				'taxonomy' => 'give_forms_category',
607
				'terms'    => $cats,
608
			);
609
		}
610
611 View Code Duplication
		if ( ! empty( $atts['tags'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
612
			$tags                     = array_filter( array_map( 'trim', explode( ',', $atts['tags'] ) ) );
613
			$form_args['tax_query'][] = array(
614
				'taxonomy' => 'give_forms_tag',
615
				'terms'    => $tags,
616
			);
617
		}
618
619
		$forms = new WP_Query( $form_args );
620
621
		if ( isset( $forms->posts ) ) {
622
			$total = 0;
623
			foreach ( $forms->posts as $post ) {
624
				$form_earning = give_get_meta( $post, '_give_form_earnings', true );
625
				$form_earning = ! empty( $form_earning ) ? $form_earning : 0;
626
627
				/**
628
				 * Update Form earnings.
629
				 *
630
				 * @since 2.1
631
				 *
632
				 * @param int    $post         Form ID.
633
				 * @param string $form_earning Total earning of Form.
634
				 */
635
				$total += apply_filters( 'give_totals_form_earning', $form_earning, $post );
636
			}
637
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
638
639
	}
640
641
	// Append link with text.
642
	$donate_link = '';
643
	if ( ! empty( $atts['link'] ) ) {
644
		$donate_link = sprintf( ' <a class="give-totals-text-link" href="%1$s">%2$s</a>', esc_url( $atts['link'] ), esc_html( $atts['link_text'] ) );
645
	}
646
647
	// Replace {total} in message.
648
	$message = str_replace( '{total}', give_currency_filter(
649
		give_format_amount( $total,
650
			array( 'sanitize' => false )
651
		)
652
	), esc_html( $atts['message'] ) );
653
654
	// Replace {total_goal} in message.
655
	$message = str_replace( '{total_goal}', give_currency_filter(
656
		give_format_amount( $total_goal,
657
			array( 'sanitize' => true )
658
		)
659
	), $message );
660
661
	/**
662
	 * Update Give totals shortcode output.
663
	 *
664
	 * @since 2.1
665
	 *
666
	 * @param string $message Shortcode Message.
667
	 * @param array  $atts    ShortCode attributes.
668
	 */
669
	$message = apply_filters( 'give_totals_shortcode_message', $message, $atts );
670
671
	ob_start();
672
	?>
673
	<div class="give-totals-shortcode-wrap">
674
		<?php
675
		// Show Progress Bar if progress_bar set true.
676
		$show_progress_bar = isset( $atts['progress_bar'] ) ? filter_var( $atts['progress_bar'], FILTER_VALIDATE_BOOLEAN ) : true;
677
		if ( $show_progress_bar ) {
678
			give_show_goal_totals_progress( $total, $total_goal );
679
		}
680
681
		echo sprintf( $message ) . $donate_link;
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$donate_link'
Loading history...
682
		?>
683
	</div>
684
	<?php
685
	$give_totals_output = ob_get_clean();
686
687
	/**
688
	 * Give Totals Shortcode output.
689
	 *
690
	 * @since 2.1
691
	 *
692
	 * @param string $give_totals_output
693
	 */
694
	return apply_filters( 'give_totals_shortcode_output', $give_totals_output );
695
696
}
697
698
add_shortcode( 'give_totals', 'give_totals_shortcode' );
699