PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.28.0
GiveWP – Donation Plugin and Fundraising Platform v2.28.0
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / shortcodes.php
give / includes Last commit date
admin 3 years ago api 3 years ago database 5 years ago deprecated 3 years ago donors 3 years ago emails 3 years ago forms 3 years ago frontend 6 years ago gateways 3 years ago libraries 4 years ago payments 4 years ago actions.php 5 years ago ajax-functions.php 3 years ago class-give-async-process.php 6 years ago class-give-background-updater.php 6 years ago class-give-cache-setting.php 4 years ago class-give-cache.php 3 years ago class-give-cli-commands.php 3 years ago class-give-comment.php 6 years ago class-give-cron.php 6 years ago class-give-donate-form.php 4 years ago class-give-donor.php 3 years ago class-give-email-access.php 5 years ago class-give-license-handler.php 4 years ago class-give-logging.php 5 years ago class-give-readme-parser.php 4 years ago class-give-roles.php 6 years ago class-give-scripts.php 3 years ago class-give-session.php 5 years ago class-give-stats.php 6 years ago class-give-template-loader.php 6 years ago class-give-tooltips.php 6 years ago class-give-translation.php 4 years ago class-notices.php 4 years ago country-functions.php 5 years ago currencies-list.php 3 years ago currency-functions.php 3 years ago error-tracking.php 6 years ago filters.php 3 years ago formatting.php 3 years ago install.php 4 years ago login-register.php 4 years ago misc-functions.php 3 years ago plugin-compatibility.php 6 years ago post-types.php 5 years ago price-functions.php 6 years ago process-donation.php 4 years ago setting-functions.php 6 years ago shortcodes.php 3 years ago template-functions.php 4 years ago user-functions.php 3 years ago
shortcodes.php
1045 lines
1 <?php
2 /**
3 * Give Shortcodes
4 *
5 * @package Give
6 * @subpackage Shortcodes
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.0
10 */
11
12 // Exit if accessed directly.
13 use Give\Helpers\Form\Template\Utils\Frontend as FrontendFormTemplateUtils;
14 use Give\Helpers\Form\Utils as FormUtils;
15 use Give\Helpers\Frontend\ConfirmDonation;
16 use Give\Helpers\Frontend\Shortcode as ShortcodeUtils;
17 use Give\Views\IframeView;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22
23 /**
24 * Donation History Shortcode
25 *
26 * Displays a user's donation history.
27 *
28 * @since 1.0
29 *
30 * @param array $atts
31 * @param string|bool $content
32 *
33 * @return string|bool
34 */
35 function give_donation_history( $atts, $content = false ) {
36
37 $donation_history_args = shortcode_atts(
38 [
39 'id' => true,
40 'date' => true,
41 'donor' => false,
42 'amount' => true,
43 'status' => false,
44 'payment_method' => false,
45 ],
46 $atts,
47 'donation_history'
48 );
49
50 // Always show receipt link.
51 $donation_history_args['details'] = true;
52
53 // Set Donation History Shortcode Arguments in session variable.
54 Give()->session->set( 'give_donation_history_args', $donation_history_args );
55
56 $get_data = give_clean( filter_input_array( INPUT_GET ) );
57
58 // If payment_key query arg exists, return receipt instead of donation history.
59 if (
60 ! empty( $get_data['donation_id'] ) ||
61 (
62 ! empty( $get_data['action'] ) &&
63 'view_in_browser' === $get_data['action']
64 )
65 ) {
66 ob_start();
67
68 echo do_shortcode( ShortcodeUtils::getReceiptShortcodeFromConfirmationPage() );
69
70 // Display donation history link only if Receipt Access Session is available.
71 if ( give_get_receipt_session() || is_user_logged_in() ) {
72 echo sprintf(
73 '<a href="%s">%s</a>',
74 esc_url($_SERVER['HTTP_REFERER'] ),
75 __( '&laquo; Return to All Donations', 'give' )
76 );
77 }
78
79 return ob_get_clean();
80 }
81
82 $email_access = give_get_option( 'email_access' );
83
84 ob_start();
85
86 /**
87 * Determine access
88 *
89 * A. Check if a user is logged in or does a session exists.
90 * B. Does an email-access token exist?
91 */
92 if (
93 is_user_logged_in()
94 || false !== Give()->session->get_session_expiration()
95 || ( give_is_setting_enabled( $email_access ) && Give()->email_access->token_exists )
96 || true === give_get_history_session()
97 ) {
98 give_get_template_part( 'history', 'donations' );
99
100 if ( ! empty( $content ) ) {
101 echo do_shortcode( $content );
102 }
103 } elseif ( give_is_setting_enabled( $email_access ) ) {
104 // Is Email-based access enabled?
105 give_get_template_part( 'email', 'login-form' );
106
107 } else {
108
109 echo 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 ) );
110 echo do_shortcode( '[give_login]' );
111 }
112
113 /**
114 * Filter to modify donation history HTMl
115 *
116 * @since 2.1
117 *
118 * @param string HTML content
119 * @param array $atts
120 * @param string $content content pass between enclose content
121 *
122 * @return string HTML content
123 */
124 return apply_filters( 'give_donation_history_shortcode_html', ob_get_clean(), $atts, $content );
125 }
126
127 add_shortcode( 'donation_history', 'give_donation_history' );
128
129 /**
130 * Donation Form Shortcode
131 *
132 * Show the Give donation form.
133 *
134 * @since 1.0
135 *
136 * @param array $atts Shortcode attributes
137 *
138 * @return string
139 */
140 function give_form_shortcode( $atts ) {
141 $atts = shortcode_atts( give_get_default_form_shortcode_args(), $atts, 'give_form' );
142
143 // Convert string to bool.
144 $atts['show_title'] = filter_var( $atts['show_title'], FILTER_VALIDATE_BOOLEAN );
145 $atts['show_goal'] = filter_var( $atts['show_goal'], FILTER_VALIDATE_BOOLEAN );
146
147 // Set form id.
148 $atts['id'] = $atts['id'] ?: FrontendFormTemplateUtils::getFormId();
149 $formId = absint( $atts['id'] );
150
151 // Fetch the Give Form.
152 ob_start();
153
154 if ( ! FormUtils::isLegacyForm( $formId ) ) {
155 $showIframeInModal = 'button' === $atts['display_style'];
156 $iframeView = new IframeView();
157
158 ConfirmDonation::storePostedDataInDonationSession();
159
160 echo $iframeView->setFormId( $formId )
161 ->showInModal( $showIframeInModal )
162 ->setButtonTitle( $atts['continue_button_title'] )
163 ->setButtonColor( $atts['button_color'] )
164 ->render();
165 } else {
166 give_get_donation_form( $atts );
167 }
168
169 $final_output = ob_get_clean();
170
171 return apply_filters( 'give_donate_form', $final_output, $atts );
172 }
173
174 add_shortcode( 'give_form', 'give_form_shortcode' );
175
176 /**
177 * Donation Form Goal Shortcode.
178 *
179 * Show the Give donation form goals.
180 *
181 * @since 1.0
182 *
183 * @param array $atts Shortcode attributes.
184 *
185 * @return string
186 */
187 function give_goal_shortcode( $atts ) {
188 $atts = shortcode_atts(
189 [
190 'id' => '',
191 'show_text' => true,
192 'show_bar' => true,
193 'color' => '',
194 ],
195 $atts,
196 'give_goal'
197 );
198
199 // get the Give Form.
200 ob_start();
201
202 // Sanity check 1: ensure there is an ID Provided.
203 if ( empty( $atts['id'] ) ) {
204 Give_Notices::print_frontend_notice( __( 'The shortcode is missing Donation Form ID attribute.', 'give' ), true );
205 }
206
207 // Sanity check 2: Check the form even has Goals enabled.
208 if ( ! give_is_setting_enabled( give_get_meta( $atts['id'], '_give_goal_option', true ) ) ) {
209
210 Give_Notices::print_frontend_notice( __( 'The form does not have Goals enabled.', 'give' ), true );
211 } else {
212 // Passed all sanity checks: output Goal.
213 give_show_goal_progress( $atts['id'], $atts );
214 }
215
216 $final_output = ob_get_clean();
217
218 return apply_filters( 'give_goal_shortcode_output', $final_output, $atts );
219 }
220
221 add_shortcode( 'give_goal', 'give_goal_shortcode' );
222
223
224 /**
225 * Login Shortcode.
226 *
227 * Shows a login form allowing users to users to log in. This function simply
228 * calls the give_login_form function to display the login form.
229 *
230 * @since 1.0
231 *
232 * @param array $atts Shortcode attributes.
233 *
234 * @uses give_login_form()
235 *
236 * @return string
237 */
238 function give_login_form_shortcode( $atts ) {
239
240 $atts = shortcode_atts(
241 [
242 // Add backward compatibility for redirect attribute.
243 'redirect' => '',
244 'login-redirect' => '',
245 'logout-redirect' => '',
246 ],
247 $atts,
248 'give_login'
249 );
250
251 // 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.
252 $atts['login-redirect'] = ! empty( $atts['login-redirect'] ) ? $atts['login-redirect'] : ( ! empty( $atts['redirect'] ) ? $atts['redirect'] : '' );
253
254 return give_login_form( $atts['login-redirect'], $atts['logout-redirect'] );
255 }
256
257 add_shortcode( 'give_login', 'give_login_form_shortcode' );
258
259 /**
260 * Register Shortcode.
261 *
262 * Shows a registration form allowing users to users to register for the site.
263 *
264 * @since 1.0
265 *
266 * @param array $atts Shortcode attributes.
267 *
268 * @uses give_register_form()
269 *
270 * @return string
271 */
272 function give_register_form_shortcode( $atts ) {
273 $atts = shortcode_atts(
274 [
275 'redirect' => '',
276 ],
277 $atts,
278 'give_register'
279 );
280
281 return give_register_form( $atts['redirect'] );
282 }
283
284 add_shortcode( 'give_register', 'give_register_form_shortcode' );
285
286 /**
287 * Receipt Shortcode.
288 *
289 * Shows a donation receipt.
290 *
291 * @since 1.0
292 *
293 * @param array $atts Shortcode attributes.
294 *
295 * @return string
296 */
297 function give_receipt_shortcode( $atts ) {
298
299 global $give_receipt_args;
300
301 $give_receipt_args = shortcode_atts(
302 [
303 'error' => __( 'You are missing the donation id to view this donation receipt.', 'give' ),
304 'price' => true,
305 'donor' => true,
306 'date' => true,
307 'payment_method' => true,
308 'payment_id' => true,
309 'payment_status' => false,
310 'company_name' => false,
311 'status_notice' => true,
312 ],
313 $atts,
314 'give_receipt'
315 );
316
317 ob_start();
318
319 $donation_id = false;
320 $receipt_type = false;
321 $get_data = give_clean( filter_input_array( INPUT_GET ) );
322 $session = give_get_purchase_session();
323
324 if ( ! empty( $get_data['donation_id'] ) ) {
325 $donation_id = $get_data['donation_id'];
326 } elseif ( ! empty( $get_data['action'] ) && 'view_in_browser' === $get_data['action'] ) {
327 $receipt_type = 'view_in_browser';
328 $donation_id = $get_data['_give_hash'];
329 } elseif ( isset( $session['donation_id'] ) ) {
330 $donation_id = $session['donation_id'];
331 } elseif ( ! empty( $give_receipt_args['id'] ) ) {
332 $donation_id = $give_receipt_args['id'];
333 }
334
335 // Display donation receipt placeholder while loading receipt via AJAX.
336 if ( ! wp_doing_ajax() ) {
337 give_get_template_part( 'receipt/placeholder' );
338
339 return sprintf(
340 '<div id="give-receipt" data-shortcode="%1$s" data-receipt-type="%2$s" data-donation-key="%3$s" >%4$s</div>',
341 htmlspecialchars( wp_json_encode( $give_receipt_args ) ),
342 $receipt_type,
343 $donation_id,
344 ob_get_clean()
345 );
346 }
347
348 return give_display_donation_receipt( $atts );
349 }
350
351 add_shortcode( 'give_receipt', 'give_receipt_shortcode' );
352
353 /**
354 * Profile Editor Shortcode.
355 *
356 * Outputs the Give Profile Editor to allow users to amend their details from the
357 * front-end. This function uses the Give templating system allowing users to
358 * override the default profile editor template. The profile editor template is located
359 * under templates/shortcode-profile-editor.php, however, it can be altered by creating a
360 * file called shortcode-profile-editor.php in the give_template directory in your active theme's
361 * folder. Please visit the Give Documentation for more information on how the
362 * templating system is used.
363 *
364 * @since 1.0
365 *
366 * @param array $atts Shortcode attributes.
367 *
368 * @return string Output generated from the profile editor
369 */
370 function give_profile_editor_shortcode( $atts ) {
371
372 ob_start();
373
374 // Restrict access to donor profile, if donor and user are disconnected.
375 $is_donor_disconnected = get_user_meta( get_current_user_id(), '_give_is_donor_disconnected', true );
376 if ( is_user_logged_in() && $is_donor_disconnected ) {
377 Give_Notices::print_frontend_notice( __( 'Your Donor and User profile are no longer connected. Please contact the site administrator.', 'give' ), true, 'error' );
378
379 return false;
380 }
381
382 give_get_template_part( 'shortcode', 'profile-editor' );
383
384 return ob_get_clean();
385 }
386
387 add_shortcode( 'give_profile_editor', 'give_profile_editor_shortcode' );
388
389 /**
390 * Process Profile Updater Form.
391 *
392 * Processes the profile updater form by updating the necessary fields.
393 *
394 * @since 1.0
395 *
396 * @param array $data Data sent from the profile editor.
397 *
398 * @return bool
399 */
400 function give_process_profile_editor_updates( $data ) {
401 // Profile field change request.
402 if ( empty( $_POST['give_profile_editor_submit'] ) && ! is_user_logged_in() ) {
403 return false;
404 }
405
406 // Nonce security.
407 if ( ! wp_verify_nonce( $data['give_profile_editor_nonce'], 'give-profile-editor-nonce' ) ) {
408 return false;
409 }
410
411 $user_id = get_current_user_id();
412 $old_user_data = get_userdata( $user_id );
413
414 /* @var Give_Donor $donor */
415 $donor = new Give_Donor( $user_id, true );
416 $old_company_name = $donor->get_company_name();
417
418 $display_name = isset( $data['give_display_name'] ) ? sanitize_text_field( $data['give_display_name'] ) : $old_user_data->display_name;
419 $first_name = isset( $data['give_first_name'] ) ? sanitize_text_field( $data['give_first_name'] ) : $old_user_data->first_name;
420 $last_name = isset( $data['give_last_name'] ) ? sanitize_text_field( $data['give_last_name'] ) : $old_user_data->last_name;
421 $company_name = ! empty( $data['give_company_name'] ) ? sanitize_text_field( $data['give_company_name'] ) : $old_company_name;
422 $email = isset( $data['give_email'] ) ? sanitize_email( $data['give_email'] ) : $old_user_data->user_email;
423 $password = ! empty( $data['give_new_user_pass1'] ) ? $data['give_new_user_pass1'] : '';
424 $confirm_password = ! empty( $data['give_new_user_pass2'] ) ? $data['give_new_user_pass2'] : '';
425
426 $userdata = [
427 'ID' => $user_id,
428 'first_name' => $first_name,
429 'last_name' => $last_name,
430 'display_name' => $display_name,
431 'user_email' => $email,
432 'user_pass' => $password,
433 'company_name' => $company_name,
434 ];
435
436 /**
437 * Fires before updating user profile.
438 *
439 * @since 1.0
440 *
441 * @param int $user_id The ID of the user.
442 * @param array $userdata User info, including ID, first name, last name, display name and email.
443 */
444 do_action( 'give_pre_update_user_profile', $user_id, $userdata );
445
446 // Make sure to validate first name of existing donors.
447 if ( empty( $first_name ) ) {
448 // Empty First Name.
449 give_set_error( 'empty_first_name', __( 'Please enter your first name.', 'give' ) );
450 }
451
452 // Make sure to validate passwords for existing Donors.
453 give_validate_user_password( $password, $confirm_password );
454
455 if ( empty( $email ) ) {
456 // Make sure email should not be empty.
457 give_set_error( 'email_empty', __( 'The email you entered is empty.', 'give' ) );
458
459 } elseif ( ! is_email( $email ) ) {
460 // Make sure email should be valid.
461 give_set_error( 'email_not_valid', __( 'The email you entered is not valid. Please use another', 'give' ) );
462
463 } elseif ( $email !== $old_user_data->user_email ) {
464 // Make sure the new email doesn't belong to another user.
465 if ( email_exists( $email ) ) {
466 give_set_error( 'user_email_exists', __( 'The email you entered belongs to another user. Please use another.', 'give' ) );
467 } elseif ( Give()->donors->get_donor_by( 'email', $email ) ) {
468 // Make sure the new email doesn't belong to another user.
469 give_set_error( 'donor_email_exists', __( 'The email you entered belongs to another donor. Please use another.', 'give' ) );
470 }
471 }
472
473 // Check for errors.
474 $errors = give_get_errors();
475
476 if ( $errors ) {
477 // Send back to the profile editor if there are errors.
478 wp_redirect( $data['give_redirect'] );
479 give_die();
480 }
481
482 // Update Donor First Name and Last Name.
483 Give()->donors->update(
484 $donor->id,
485 [
486 'name' => trim( "{$first_name} {$last_name}" ),
487 ]
488 );
489 Give()->donor_meta->update_meta( $donor->id, '_give_donor_first_name', $first_name );
490 Give()->donor_meta->update_meta( $donor->id, '_give_donor_last_name', $last_name );
491 Give()->donor_meta->update_meta( $donor->id, '_give_donor_company', $company_name );
492
493 $current_user = wp_get_current_user();
494
495 // Compares new values with old values to detect change in values.
496 $email_update = ( $email !== $current_user->user_email ) ? true : false;
497 $display_name_update = ( $display_name !== $current_user->display_name ) ? true : false;
498 $first_name_update = ( $first_name !== $current_user->first_name ) ? true : false;
499 $last_name_update = ( $last_name !== $current_user->last_name ) ? true : false;
500 $company_name_update = ( $company_name !== $old_company_name ) ? true : false;
501 $update_code = 0;
502
503 /**
504 * True if update is done in display name, first name, last name or email.
505 *
506 * @var boolean
507 */
508 $profile_update = ( $email_update || $display_name_update || $first_name_update || $last_name_update || $company_name_update );
509
510 /**
511 * True if password fields are filled.
512 *
513 * @var boolean
514 */
515 $password_update = ( ! empty( $password ) && ! empty( $confirm_password ) );
516
517 if ( $profile_update ) {
518
519 // If only profile fields are updated.
520 $update_code = '1';
521
522 if ( $password_update ) {
523
524 // If profile fields AND password both are updated.
525 $update_code = '2';
526 }
527 } elseif ( $password_update ) {
528
529 // If only password is updated.
530 $update_code = '3';
531 }
532
533 // Update the user.
534 $updated = wp_update_user( $userdata );
535
536 if ( $updated ) {
537
538 /**
539 * Fires after updating user profile.
540 *
541 * @since 1.0
542 *
543 * @param int $user_id The ID of the user.
544 * @param array $userdata User info, including ID, first name, last name, display name and email.
545 */
546 do_action( 'give_user_profile_updated', $user_id, $userdata );
547
548 $profile_edit_redirect_args = [
549 'updated' => 'true',
550 'update_code' => $update_code,
551 ];
552
553 /**
554 * Update codes '2' and '3' indicate a password change.
555 * If the password is changed, then logout and redirect to the same page.
556 */
557 if ( '2' === $update_code || '3' === $update_code ) {
558 wp_logout();
559 } else {
560 wp_redirect( esc_url_raw( add_query_arg( $profile_edit_redirect_args, $data['give_redirect'] ) ) );
561 }
562
563 give_die();
564 }
565
566 return false;
567 }
568
569 add_action( 'give_edit_user_profile', 'give_process_profile_editor_updates' );
570
571 /**
572 * Give totals Shortcode.
573 *
574 * Shows a donation total.
575 *
576 * @since 2.1
577 *
578 * @param array $atts Shortcode attributes.
579 *
580 * @return string
581 */
582 function give_totals_shortcode( $atts ) {
583 $total = get_option( 'give_earnings_total', false );
584
585 $message = apply_filters( 'give_totals_message', __( 'Hey! We\'ve raised {total} of the {total_goal} we are trying to raise for this campaign!', 'give' ) );
586
587 $atts = shortcode_atts(
588 [
589 'total_goal' => 0, // integer.
590 'ids' => 0, // integer|array.
591 'cats' => 0, // integer|array.
592 'tags' => 0, // integer|array.
593 'message' => $message,
594 'link' => '', // URL.
595 'link_text' => __( 'Donate Now', 'give' ), // string,
596 'progress_bar' => true, // boolean.
597 ],
598 $atts,
599 'give_totals'
600 );
601
602 // Total Goal.
603 $total_goal = give_maybe_sanitize_amount( $atts['total_goal'] );
604
605 /**
606 * Give Action fire before the shortcode is rendering is started.
607 *
608 * @since 2.1.4
609 *
610 * @param array $atts shortcode attribute.
611 */
612 do_action( 'give_totals_goal_shortcode_before_render', $atts );
613
614 // Build query based on cat, tag and Form ids.
615 if ( ! empty( $atts['cats'] ) || ! empty( $atts['tags'] ) || ! empty( $atts['ids'] ) ) {
616
617 $form_ids = [];
618 if ( ! empty( $atts['ids'] ) ) {
619 $form_ids = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
620 }
621
622 /**
623 * Filter to modify WP Query for Total Goal.
624 *
625 * @since 2.1.4
626 *
627 * @param array WP query argument for Total Goal.
628 */
629 $form_args = [
630 'post_type' => 'give_forms',
631 'post_status' => 'publish',
632 'post__in' => $form_ids,
633 'posts_per_page' => - 1,
634 'fields' => 'ids',
635 'tax_query' => [
636 'relation' => 'AND',
637 ],
638 ];
639
640 if ( ! empty( $atts['cats'] ) ) {
641 $cats = array_filter( array_map( 'trim', explode( ',', $atts['cats'] ) ) );
642 $form_args['tax_query'][] = [
643 'taxonomy' => 'give_forms_category',
644 'terms' => $cats,
645 ];
646 }
647
648 if ( ! empty( $atts['tags'] ) ) {
649 $tags = array_filter( array_map( 'trim', explode( ',', $atts['tags'] ) ) );
650 $form_args['tax_query'][] = [
651 'taxonomy' => 'give_forms_tag',
652 'terms' => $tags,
653 ];
654 }
655
656 /**
657 * Filter to modify WP Query for Total Goal.
658 *
659 * @since 2.1.4
660 *
661 * @param array $form_args WP query argument for Total Goal.
662 *
663 * @return array $form_args WP query argument for Total Goal.
664 */
665 $form_args = (array) apply_filters( 'give_totals_goal_shortcode_query_args', $form_args );
666
667 $forms = new WP_Query( $form_args );
668
669 if ( isset( $forms->posts ) ) {
670 $total = 0;
671 foreach ( $forms->posts as $post ) {
672 $form_earning = give_get_meta( $post, '_give_form_earnings', true );
673 $form_earning = ! empty( $form_earning ) ? $form_earning : 0;
674
675 /**
676 * Update Form earnings.
677 *
678 * @since 2.1
679 *
680 * @param int $post Form ID.
681 * @param string $form_earning Total earning of Form.
682 * @param array $atts shortcode attributes.
683 */
684 $total += apply_filters( 'give_totals_form_earning', $form_earning, $post, $atts );
685 }
686 }
687 } // End if().
688
689 // Append link with text.
690 $donate_link = '';
691 if ( ! empty( $atts['link'] ) ) {
692 $donate_link = sprintf( ' <a class="give-totals-text-link" href="%1$s">%2$s</a>', esc_url( $atts['link'] ), esc_html( $atts['link_text'] ) );
693 }
694
695 // Replace {total} in message.
696 $message = str_replace(
697 '{total}',
698 give_currency_filter(
699 give_format_amount(
700 $total,
701 [ 'sanitize' => false ]
702 )
703 ),
704 esc_html( $atts['message'] )
705 );
706
707 // Replace {total_goal} in message.
708 $message = str_replace(
709 '{total_goal}',
710 give_currency_filter(
711 give_format_amount(
712 $total_goal,
713 [ 'sanitize' => true ]
714 )
715 ),
716 $message
717 );
718
719 /**
720 * Update Give totals shortcode output.
721 *
722 * @since 2.1
723 *
724 * @param string $message Shortcode Message.
725 * @param array $atts ShortCode attributes.
726 */
727 $message = apply_filters( 'give_totals_shortcode_message', $message, $atts );
728
729 ob_start();
730 ?>
731 <div class="give-totals-shortcode-wrap">
732 <?php
733 // Show Progress Bar if progress_bar set true.
734 $show_progress_bar = isset( $atts['progress_bar'] ) ? filter_var( $atts['progress_bar'], FILTER_VALIDATE_BOOLEAN ) : true;
735 if ( $show_progress_bar ) {
736 give_show_goal_totals_progress( $total, $total_goal );
737 }
738
739 echo sprintf( $message ) . $donate_link;
740 ?>
741 </div>
742 <?php
743 $give_totals_output = ob_get_clean();
744
745 /**
746 * Give Action fire after the total goal shortcode rendering is end.
747 *
748 * @since 2.1.4
749 *
750 * @param array $atts shortcode attribute.
751 * @param string $give_totals_output shortcode output.
752 */
753 do_action( 'give_totals_goal_shortcode_after_render', $atts, $give_totals_output );
754
755 /**
756 * Give Totals Shortcode output.
757 *
758 * @since 2.1
759 *
760 * @param string $give_totals_output
761 */
762 return apply_filters( 'give_totals_shortcode_output', $give_totals_output );
763
764 }
765
766 add_shortcode( 'give_totals', 'give_totals_shortcode' );
767
768
769 /**
770 * Displays donation forms in a grid layout.
771 *
772 * @since 2.1.0
773 *
774 * @param array $atts {
775 * Optional. Attributes of the form grid shortcode.
776 *
777 * @type int $forms_per_page Number of forms per page. Default '12'.
778 * @type bool $paged Whether to paginate forms. Default 'true'.
779 * @type string $ids A comma-separated list of form IDs to display. Default empty.
780 * @type string exclude A comma-separated list of form IDs to exclude from display. Default empty.
781 * @type string $cats A comma-separated list of form categories to display. Default empty.
782 * @type string $tags A comma-separated list of form tags to display. Default empty.
783 * @type string $columns Maximum columns to display. Default 'best-fit'.
784 * Accepts 'best-fit', '1', '2', '3', '4'.
785 * @type bool $show_title Whether to display form title. Default 'true'.
786 * @type bool $show_goal Whether to display form goal. Default 'true'.
787 * @type bool $show_excerpt Whether to display form excerpt. Default 'true'.
788 * @type bool $show_featured_image Whether to display featured image. Default 'true'.
789 * @type string $image_size Featured image size. Default 'medium'. Accepts WordPress image sizes.
790 * @type string $image_height Featured image height. Default 'auto'. Accepts valid CSS heights.
791 * @type int $excerpt_length Number of words before excerpt is truncated. Default '16'.
792 * @type string $display_style How the form is displayed, either in new page or modal popup.
793 * Default 'redirect'. Accepts 'redirect', 'modal'.
794 *
795 * @since 2.23.1 Updated the default text color for the donate button, see #6591.
796 * @since 2.21.2 change tag_background_color, progress_bar_color to official green color #69b868.
797 * change tag_text_color color to #333333.
798 * @since 2.20.0 $show_donate_button Option to show donate button
799 * @since 2.20.0 $donate_button_text Default Donate
800 * @since 2.20.0 $donate_button_background_color Default #66bb6a
801 * @since 2.20.0 $donate_button_text_color Default #fff
802 * @since 2.20.0 $show_bar Default false
803 * @since 2.22.2 remove $show_bar attribute in favor of show_goal
804 *
805 * @return string|bool The markup of the form grid or false.
806 */
807 function give_form_grid_shortcode( $atts ) {
808
809 $give_settings = give_get_settings();
810
811 $atts = shortcode_atts(
812 [
813 'forms_per_page' => 12,
814 'paged' => true,
815 'ids' => '',
816 'exclude' => '',
817 'orderby' => 'date',
818 'order' => 'DESC',
819 'cats' => '',
820 'tags' => '',
821 'columns' => '1',
822 'show_title' => true,
823 'show_goal' => true,
824 'show_excerpt' => true,
825 'show_featured_image' => true,
826 'show_donate_button' => true,
827 'donate_button_text' => '',
828 'tag_background_color' => '#69b868',
829 'tag_text_color' => '#333333',
830 'donate_button_text_color' => '#000000',
831 'image_size' => 'medium',
832 'image_height' => 'auto',
833 'excerpt_length' => 16,
834 'display_style' => 'modal_reveal',
835 'progress_bar_color' => '#69b868',
836 'status' => '', // open or closed.
837 ],
838 $atts
839 );
840
841 // Validate integer attributes.
842 $atts['forms_per_page'] = intval( $atts['forms_per_page'] );
843 $atts['excerpt_length'] = intval( $atts['excerpt_length'] );
844
845 // Validate boolean attributes.
846 $boolean_attributes = [
847 'paged',
848 'show_title',
849 'show_goal',
850 'show_excerpt',
851 'show_featured_image',
852 ];
853
854 foreach ( $boolean_attributes as $att ) {
855 $atts[ $att ] = filter_var( $atts[ $att ], FILTER_VALIDATE_BOOLEAN );
856 }
857
858 // Set default form query args.
859 $form_args = [
860 'post_type' => 'give_forms',
861 'post_status' => 'publish',
862 'posts_per_page' => $atts['forms_per_page'],
863 'orderby' => $atts['orderby'],
864 'order' => $atts['order'],
865 'paged' => $atts['paged'],
866 'tax_query' => [
867 'relation' => 'AND',
868 ],
869 ];
870
871 // Filter results of form grid based on form status.
872 $form_closed_status = trim( $atts['status'] );
873
874 if ( ! empty( $form_closed_status ) ) {
875 $form_args['meta_query'] = [
876 [
877 'key' => '_give_form_status',
878 'value' => $form_closed_status,
879 ],
880 ];
881 }
882
883 // Maybe add pagination.
884 if ( true === $atts['paged'] ) {
885 $form_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
886 }
887
888 // Maybe filter forms by IDs.
889 if ( ! empty( $atts['ids'] ) ) {
890 $form_args['post__in'] = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
891 }
892
893 // Convert comma-separated form IDs into array.
894 if ( ! empty( $atts['exclude'] ) ) {
895 $form_args['post__not_in'] = array_filter(
896 array_map(
897 function( $item ) {
898 return intval( trim( $item ) );
899 },
900 explode( ',', $atts['exclude'] )
901 )
902 );
903 }
904
905 // Maybe filter by form category.
906 if ( ! empty( $atts['cats'] ) ) {
907 $cats = array_filter( array_map( 'trim', explode( ',', $atts['cats'] ) ) );
908
909 // Backward compatibility for term_ids.
910 $term_ids = array_unique( array_filter( $cats, 'is_numeric' ) );
911 if ( $term_ids ) {
912 $form_args['tax_query'][] = [
913 'taxonomy' => 'give_forms_category',
914 'terms' => $term_ids,
915 ];
916
917 }
918
919 $term_slug = array_unique( array_filter( array_diff( $cats, $term_ids ) ) );
920 if ( $term_slug ) {
921 $form_args['tax_query'][] = [
922 'taxonomy' => 'give_forms_category',
923 'field' => 'slug',
924 'terms' => $term_slug,
925 ];
926
927 }
928 }
929
930 // Maybe filter by form tag.
931 if ( ! empty( $atts['tags'] ) ) {
932 $tags = array_filter( array_map( 'trim', explode( ',', $atts['tags'] ) ) );
933
934 // Backward compatibility for term_ids.
935 $tag_ids = array_unique( array_filter( $tags, 'is_numeric' ) );
936 if ( $tag_ids ) {
937 $form_args['tax_query'][] = [
938 'taxonomy' => 'give_forms_tag',
939 'terms' => $tag_ids,
940 ];
941
942 }
943
944 $tag_slug = array_unique( array_filter( array_diff( $tags, $tag_ids ) ) );
945 if ( $tag_slug ) {
946 $form_args['tax_query'][] = [
947 'taxonomy' => 'give_forms_tag',
948 'field' => 'slug',
949 'terms' => $tag_slug,
950 ];
951
952 }
953 }
954
955 /**
956 * Filter to modify WP Query for Total Goal.
957 *
958 * @since 2.1.4
959 *
960 * @param array $form_args WP query argument for Grid.
961 *
962 * @return array $form_args WP query argument for Grid.
963 */
964 $form_args = (array) apply_filters( 'give_form_grid_shortcode_query_args', $form_args );
965
966 // Maybe filter by form Amount Donated or Number of Donations.
967 switch ( $atts['orderby'] ) {
968 case 'amount_donated':
969 $form_args['meta_key'] = '_give_form_earnings';
970 $form_args['orderby'] = 'meta_value_num';
971 break;
972 case 'number_donations':
973 $form_args['meta_key'] = '_give_form_sales';
974 $form_args['orderby'] = 'meta_value_num';
975 break;
976 case 'random':
977 $form_args['orderby'] = 'rand';
978 break;
979 case 'closest_to_goal':
980 if ( give_has_upgrade_completed( 'v240_update_form_goal_progress' ) ) {
981 $form_args['meta_key'] = '_give_form_goal_progress';
982 $form_args['orderby'] = 'meta_value_num';
983 }
984 break;
985 }
986
987 // Query to output donation forms.
988 $form_query = new WP_Query( $form_args );
989
990 if ( $form_query->have_posts() ) {
991 ob_start();
992
993 add_filter( 'add_give_goal_progress_class', 'add_give_goal_progress_class', 10, 1 );
994 add_filter( 'add_give_goal_progress_bar_class', 'add_give_goal_progress_bar_class', 10, 1 );
995 add_filter( 'give_form_wrap_classes', 'add_class_for_form_grid', 10, 3 );
996 add_action( 'give_donation_form_top', 'give_is_form_grid_page_hidden_field', 10, 3 );
997
998 echo '<div class="give-wrap">';
999 echo '<div class="give-grid give-grid--' . esc_attr( $atts['columns'] ) . '">';
1000
1001 while ( $form_query->have_posts() ) {
1002 $form_query->the_post();
1003
1004 // Give/templates/shortcode-form-grid.php.
1005 give_get_template( 'shortcode-form-grid', [ $give_settings, $atts ] );
1006
1007 }
1008
1009 wp_reset_postdata();
1010
1011 echo '</div><!-- .give-grid -->';
1012
1013 remove_filter( 'add_give_goal_progress_class', 'add_give_goal_progress_class' );
1014 remove_filter( 'add_give_goal_progress_bar_class', 'add_give_goal_progress_bar_class' );
1015 remove_filter( 'give_form_wrap_classes', 'add_class_for_form_grid', 10 );
1016 remove_action( 'give_donation_form_top', 'give_is_form_grid_page_hidden_field', 10 );
1017
1018 if ( false !== $atts['paged'] ) {
1019 $paginate_args = [
1020 'current' => max( 1, get_query_var( 'paged' ) ),
1021 'total' => $form_query->max_num_pages,
1022 'show_all' => false,
1023 'end_size' => 1,
1024 'mid_size' => 2,
1025 'prev_next' => true,
1026 'prev_text' => __( '&laquo; Previous', 'give' ),
1027 'next_text' => __( 'Next &raquo;', 'give' ),
1028 'type' => 'plain',
1029 'add_args' => false,
1030 ];
1031
1032 printf(
1033 '<div class="give-page-numbers">%s</div>',
1034 paginate_links( $paginate_args )
1035 );
1036 }
1037 echo '</div><!-- .give-wrap -->';
1038
1039 return ob_get_clean();
1040 } // End if().
1041 }
1042
1043 add_shortcode( 'give_form_grid', 'give_form_grid_shortcode' );
1044
1045