PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.33.0
GiveWP – Donation Plugin and Fundraising Platform v2.33.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 2 years ago api 3 years ago database 5 years ago deprecated 3 years ago donors 2 years ago emails 3 years ago forms 3 years ago frontend 6 years ago gateways 2 years ago libraries 2 years ago payments 4 years ago actions.php 5 years ago ajax-functions.php 2 years ago class-give-async-process.php 2 years ago class-give-background-updater.php 2 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 2 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 2 years ago template-functions.php 4 years ago user-functions.php 3 years ago
shortcodes.php
1053 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 2.30.0 Add short-circuit filter to allow for custom output.
135 * @since 1.0
136
137 * @param array $atts Shortcode attributes
138 *
139 * @return string
140 */
141 function give_form_shortcode( $atts ) {
142 $atts = shortcode_atts( give_get_default_form_shortcode_args(), $atts, 'give_form' );
143
144 // Convert string to bool.
145 $atts['show_title'] = filter_var( $atts['show_title'], FILTER_VALIDATE_BOOLEAN );
146 $atts['show_goal'] = filter_var( $atts['show_goal'], FILTER_VALIDATE_BOOLEAN );
147
148 // Set form id.
149 $atts['id'] = $atts['id'] ?: FrontendFormTemplateUtils::getFormId();
150 $formId = absint( $atts['id'] );
151
152 // Short-circuit the shortcode output if the filter returns a non-empty string.
153 $output = apply_filters('givewp_form_shortcode_output', '', $atts);
154
155 if ($output) {
156 return $output;
157 }
158
159 // Fetch the Give Form.
160 ob_start();
161
162 if ( ! FormUtils::isLegacyForm( $formId ) ) {
163 $showIframeInModal = 'button' === $atts['display_style'];
164 $iframeView = new IframeView();
165
166 ConfirmDonation::storePostedDataInDonationSession();
167
168 echo $iframeView->setFormId( $formId )
169 ->showInModal( $showIframeInModal )
170 ->setButtonTitle( $atts['continue_button_title'] )
171 ->setButtonColor( $atts['button_color'] )
172 ->render();
173 } else {
174 give_get_donation_form( $atts );
175 }
176
177 $final_output = ob_get_clean();
178
179 return apply_filters( 'give_donate_form', $final_output, $atts );
180 }
181
182 add_shortcode( 'give_form', 'give_form_shortcode' );
183
184 /**
185 * Donation Form Goal Shortcode.
186 *
187 * Show the Give donation form goals.
188 *
189 * @since 1.0
190 *
191 * @param array $atts Shortcode attributes.
192 *
193 * @return string
194 */
195 function give_goal_shortcode( $atts ) {
196 $atts = shortcode_atts(
197 [
198 'id' => '',
199 'show_text' => true,
200 'show_bar' => true,
201 'color' => '',
202 ],
203 $atts,
204 'give_goal'
205 );
206
207 // get the Give Form.
208 ob_start();
209
210 // Sanity check 1: ensure there is an ID Provided.
211 if ( empty( $atts['id'] ) ) {
212 Give_Notices::print_frontend_notice( __( 'The shortcode is missing Donation Form ID attribute.', 'give' ), true );
213 }
214
215 // Sanity check 2: Check the form even has Goals enabled.
216 if ( ! give_is_setting_enabled( give_get_meta( $atts['id'], '_give_goal_option', true ) ) ) {
217
218 Give_Notices::print_frontend_notice( __( 'The form does not have Goals enabled.', 'give' ), true );
219 } else {
220 // Passed all sanity checks: output Goal.
221 give_show_goal_progress( $atts['id'], $atts );
222 }
223
224 $final_output = ob_get_clean();
225
226 return apply_filters( 'give_goal_shortcode_output', $final_output, $atts );
227 }
228
229 add_shortcode( 'give_goal', 'give_goal_shortcode' );
230
231
232 /**
233 * Login Shortcode.
234 *
235 * Shows a login form allowing users to users to log in. This function simply
236 * calls the give_login_form function to display the login form.
237 *
238 * @since 1.0
239 *
240 * @param array $atts Shortcode attributes.
241 *
242 * @uses give_login_form()
243 *
244 * @return string
245 */
246 function give_login_form_shortcode( $atts ) {
247
248 $atts = shortcode_atts(
249 [
250 // Add backward compatibility for redirect attribute.
251 'redirect' => '',
252 'login-redirect' => '',
253 'logout-redirect' => '',
254 ],
255 $atts,
256 'give_login'
257 );
258
259 // 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.
260 $atts['login-redirect'] = ! empty( $atts['login-redirect'] ) ? $atts['login-redirect'] : ( ! empty( $atts['redirect'] ) ? $atts['redirect'] : '' );
261
262 return give_login_form( $atts['login-redirect'], $atts['logout-redirect'] );
263 }
264
265 add_shortcode( 'give_login', 'give_login_form_shortcode' );
266
267 /**
268 * Register Shortcode.
269 *
270 * Shows a registration form allowing users to users to register for the site.
271 *
272 * @since 1.0
273 *
274 * @param array $atts Shortcode attributes.
275 *
276 * @uses give_register_form()
277 *
278 * @return string
279 */
280 function give_register_form_shortcode( $atts ) {
281 $atts = shortcode_atts(
282 [
283 'redirect' => '',
284 ],
285 $atts,
286 'give_register'
287 );
288
289 return give_register_form( $atts['redirect'] );
290 }
291
292 add_shortcode( 'give_register', 'give_register_form_shortcode' );
293
294 /**
295 * Receipt Shortcode.
296 *
297 * Shows a donation receipt.
298 *
299 * @since 1.0
300 *
301 * @param array $atts Shortcode attributes.
302 *
303 * @return string
304 */
305 function give_receipt_shortcode( $atts ) {
306
307 global $give_receipt_args;
308
309 $give_receipt_args = shortcode_atts(
310 [
311 'error' => __( 'You are missing the donation id to view this donation receipt.', 'give' ),
312 'price' => true,
313 'donor' => true,
314 'date' => true,
315 'payment_method' => true,
316 'payment_id' => true,
317 'payment_status' => false,
318 'company_name' => false,
319 'status_notice' => true,
320 ],
321 $atts,
322 'give_receipt'
323 );
324
325 ob_start();
326
327 $donation_id = false;
328 $receipt_type = false;
329 $get_data = give_clean( filter_input_array( INPUT_GET ) );
330 $session = give_get_purchase_session();
331
332 if ( ! empty( $get_data['donation_id'] ) ) {
333 $donation_id = $get_data['donation_id'];
334 } elseif ( ! empty( $get_data['action'] ) && 'view_in_browser' === $get_data['action'] ) {
335 $receipt_type = 'view_in_browser';
336 $donation_id = $get_data['_give_hash'];
337 } elseif ( isset( $session['donation_id'] ) ) {
338 $donation_id = $session['donation_id'];
339 } elseif ( ! empty( $give_receipt_args['id'] ) ) {
340 $donation_id = $give_receipt_args['id'];
341 }
342
343 // Display donation receipt placeholder while loading receipt via AJAX.
344 if ( ! wp_doing_ajax() ) {
345 give_get_template_part( 'receipt/placeholder' );
346
347 return sprintf(
348 '<div id="give-receipt" data-shortcode="%1$s" data-receipt-type="%2$s" data-donation-key="%3$s" >%4$s</div>',
349 htmlspecialchars( wp_json_encode( $give_receipt_args ) ),
350 $receipt_type,
351 $donation_id,
352 ob_get_clean()
353 );
354 }
355
356 return give_display_donation_receipt( $atts );
357 }
358
359 add_shortcode( 'give_receipt', 'give_receipt_shortcode' );
360
361 /**
362 * Profile Editor Shortcode.
363 *
364 * Outputs the Give Profile Editor to allow users to amend their details from the
365 * front-end. This function uses the Give templating system allowing users to
366 * override the default profile editor template. The profile editor template is located
367 * under templates/shortcode-profile-editor.php, however, it can be altered by creating a
368 * file called shortcode-profile-editor.php in the give_template directory in your active theme's
369 * folder. Please visit the Give Documentation for more information on how the
370 * templating system is used.
371 *
372 * @since 1.0
373 *
374 * @param array $atts Shortcode attributes.
375 *
376 * @return string Output generated from the profile editor
377 */
378 function give_profile_editor_shortcode( $atts ) {
379
380 ob_start();
381
382 // Restrict access to donor profile, if donor and user are disconnected.
383 $is_donor_disconnected = get_user_meta( get_current_user_id(), '_give_is_donor_disconnected', true );
384 if ( is_user_logged_in() && $is_donor_disconnected ) {
385 Give_Notices::print_frontend_notice( __( 'Your Donor and User profile are no longer connected. Please contact the site administrator.', 'give' ), true, 'error' );
386
387 return false;
388 }
389
390 give_get_template_part( 'shortcode', 'profile-editor' );
391
392 return ob_get_clean();
393 }
394
395 add_shortcode( 'give_profile_editor', 'give_profile_editor_shortcode' );
396
397 /**
398 * Process Profile Updater Form.
399 *
400 * Processes the profile updater form by updating the necessary fields.
401 *
402 * @since 1.0
403 *
404 * @param array $data Data sent from the profile editor.
405 *
406 * @return bool
407 */
408 function give_process_profile_editor_updates( $data ) {
409 // Profile field change request.
410 if ( empty( $_POST['give_profile_editor_submit'] ) && ! is_user_logged_in() ) {
411 return false;
412 }
413
414 // Nonce security.
415 if ( ! wp_verify_nonce( $data['give_profile_editor_nonce'], 'give-profile-editor-nonce' ) ) {
416 return false;
417 }
418
419 $user_id = get_current_user_id();
420 $old_user_data = get_userdata( $user_id );
421
422 /* @var Give_Donor $donor */
423 $donor = new Give_Donor( $user_id, true );
424 $old_company_name = $donor->get_company_name();
425
426 $display_name = isset( $data['give_display_name'] ) ? sanitize_text_field( $data['give_display_name'] ) : $old_user_data->display_name;
427 $first_name = isset( $data['give_first_name'] ) ? sanitize_text_field( $data['give_first_name'] ) : $old_user_data->first_name;
428 $last_name = isset( $data['give_last_name'] ) ? sanitize_text_field( $data['give_last_name'] ) : $old_user_data->last_name;
429 $company_name = ! empty( $data['give_company_name'] ) ? sanitize_text_field( $data['give_company_name'] ) : $old_company_name;
430 $email = isset( $data['give_email'] ) ? sanitize_email( $data['give_email'] ) : $old_user_data->user_email;
431 $password = ! empty( $data['give_new_user_pass1'] ) ? $data['give_new_user_pass1'] : '';
432 $confirm_password = ! empty( $data['give_new_user_pass2'] ) ? $data['give_new_user_pass2'] : '';
433
434 $userdata = [
435 'ID' => $user_id,
436 'first_name' => $first_name,
437 'last_name' => $last_name,
438 'display_name' => $display_name,
439 'user_email' => $email,
440 'user_pass' => $password,
441 'company_name' => $company_name,
442 ];
443
444 /**
445 * Fires before updating user profile.
446 *
447 * @since 1.0
448 *
449 * @param int $user_id The ID of the user.
450 * @param array $userdata User info, including ID, first name, last name, display name and email.
451 */
452 do_action( 'give_pre_update_user_profile', $user_id, $userdata );
453
454 // Make sure to validate first name of existing donors.
455 if ( empty( $first_name ) ) {
456 // Empty First Name.
457 give_set_error( 'empty_first_name', __( 'Please enter your first name.', 'give' ) );
458 }
459
460 // Make sure to validate passwords for existing Donors.
461 give_validate_user_password( $password, $confirm_password );
462
463 if ( empty( $email ) ) {
464 // Make sure email should not be empty.
465 give_set_error( 'email_empty', __( 'The email you entered is empty.', 'give' ) );
466
467 } elseif ( ! is_email( $email ) ) {
468 // Make sure email should be valid.
469 give_set_error( 'email_not_valid', __( 'The email you entered is not valid. Please use another', 'give' ) );
470
471 } elseif ( $email !== $old_user_data->user_email ) {
472 // Make sure the new email doesn't belong to another user.
473 if ( email_exists( $email ) ) {
474 give_set_error( 'user_email_exists', __( 'The email you entered belongs to another user. Please use another.', 'give' ) );
475 } elseif ( Give()->donors->get_donor_by( 'email', $email ) ) {
476 // Make sure the new email doesn't belong to another user.
477 give_set_error( 'donor_email_exists', __( 'The email you entered belongs to another donor. Please use another.', 'give' ) );
478 }
479 }
480
481 // Check for errors.
482 $errors = give_get_errors();
483
484 if ( $errors ) {
485 // Send back to the profile editor if there are errors.
486 wp_redirect( $data['give_redirect'] );
487 give_die();
488 }
489
490 // Update Donor First Name and Last Name.
491 Give()->donors->update(
492 $donor->id,
493 [
494 'name' => trim( "{$first_name} {$last_name}" ),
495 ]
496 );
497 Give()->donor_meta->update_meta( $donor->id, '_give_donor_first_name', $first_name );
498 Give()->donor_meta->update_meta( $donor->id, '_give_donor_last_name', $last_name );
499 Give()->donor_meta->update_meta( $donor->id, '_give_donor_company', $company_name );
500
501 $current_user = wp_get_current_user();
502
503 // Compares new values with old values to detect change in values.
504 $email_update = ( $email !== $current_user->user_email ) ? true : false;
505 $display_name_update = ( $display_name !== $current_user->display_name ) ? true : false;
506 $first_name_update = ( $first_name !== $current_user->first_name ) ? true : false;
507 $last_name_update = ( $last_name !== $current_user->last_name ) ? true : false;
508 $company_name_update = ( $company_name !== $old_company_name ) ? true : false;
509 $update_code = 0;
510
511 /**
512 * True if update is done in display name, first name, last name or email.
513 *
514 * @var boolean
515 */
516 $profile_update = ( $email_update || $display_name_update || $first_name_update || $last_name_update || $company_name_update );
517
518 /**
519 * True if password fields are filled.
520 *
521 * @var boolean
522 */
523 $password_update = ( ! empty( $password ) && ! empty( $confirm_password ) );
524
525 if ( $profile_update ) {
526
527 // If only profile fields are updated.
528 $update_code = '1';
529
530 if ( $password_update ) {
531
532 // If profile fields AND password both are updated.
533 $update_code = '2';
534 }
535 } elseif ( $password_update ) {
536
537 // If only password is updated.
538 $update_code = '3';
539 }
540
541 // Update the user.
542 $updated = wp_update_user( $userdata );
543
544 if ( $updated ) {
545
546 /**
547 * Fires after updating user profile.
548 *
549 * @since 1.0
550 *
551 * @param int $user_id The ID of the user.
552 * @param array $userdata User info, including ID, first name, last name, display name and email.
553 */
554 do_action( 'give_user_profile_updated', $user_id, $userdata );
555
556 $profile_edit_redirect_args = [
557 'updated' => 'true',
558 'update_code' => $update_code,
559 ];
560
561 /**
562 * Update codes '2' and '3' indicate a password change.
563 * If the password is changed, then logout and redirect to the same page.
564 */
565 if ( '2' === $update_code || '3' === $update_code ) {
566 wp_logout();
567 } else {
568 wp_redirect( esc_url_raw( add_query_arg( $profile_edit_redirect_args, $data['give_redirect'] ) ) );
569 }
570
571 give_die();
572 }
573
574 return false;
575 }
576
577 add_action( 'give_edit_user_profile', 'give_process_profile_editor_updates' );
578
579 /**
580 * Give totals Shortcode.
581 *
582 * Shows a donation total.
583 *
584 * @since 2.1
585 *
586 * @param array $atts Shortcode attributes.
587 *
588 * @return string
589 */
590 function give_totals_shortcode( $atts ) {
591 $total = get_option( 'give_earnings_total', false );
592
593 $message = apply_filters( 'give_totals_message', __( 'Hey! We\'ve raised {total} of the {total_goal} we are trying to raise for this campaign!', 'give' ) );
594
595 $atts = shortcode_atts(
596 [
597 'total_goal' => 0, // integer.
598 'ids' => 0, // integer|array.
599 'cats' => 0, // integer|array.
600 'tags' => 0, // integer|array.
601 'message' => $message,
602 'link' => '', // URL.
603 'link_text' => __( 'Donate Now', 'give' ), // string,
604 'progress_bar' => true, // boolean.
605 ],
606 $atts,
607 'give_totals'
608 );
609
610 // Total Goal.
611 $total_goal = give_maybe_sanitize_amount( $atts['total_goal'] );
612
613 /**
614 * Give Action fire before the shortcode is rendering is started.
615 *
616 * @since 2.1.4
617 *
618 * @param array $atts shortcode attribute.
619 */
620 do_action( 'give_totals_goal_shortcode_before_render', $atts );
621
622 // Build query based on cat, tag and Form ids.
623 if ( ! empty( $atts['cats'] ) || ! empty( $atts['tags'] ) || ! empty( $atts['ids'] ) ) {
624
625 $form_ids = [];
626 if ( ! empty( $atts['ids'] ) ) {
627 $form_ids = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
628 }
629
630 /**
631 * Filter to modify WP Query for Total Goal.
632 *
633 * @since 2.1.4
634 *
635 * @param array WP query argument for Total Goal.
636 */
637 $form_args = [
638 'post_type' => 'give_forms',
639 'post_status' => 'publish',
640 'post__in' => $form_ids,
641 'posts_per_page' => - 1,
642 'fields' => 'ids',
643 'tax_query' => [
644 'relation' => 'AND',
645 ],
646 ];
647
648 if ( ! empty( $atts['cats'] ) ) {
649 $cats = array_filter( array_map( 'trim', explode( ',', $atts['cats'] ) ) );
650 $form_args['tax_query'][] = [
651 'taxonomy' => 'give_forms_category',
652 'terms' => $cats,
653 ];
654 }
655
656 if ( ! empty( $atts['tags'] ) ) {
657 $tags = array_filter( array_map( 'trim', explode( ',', $atts['tags'] ) ) );
658 $form_args['tax_query'][] = [
659 'taxonomy' => 'give_forms_tag',
660 'terms' => $tags,
661 ];
662 }
663
664 /**
665 * Filter to modify WP Query for Total Goal.
666 *
667 * @since 2.1.4
668 *
669 * @param array $form_args WP query argument for Total Goal.
670 *
671 * @return array $form_args WP query argument for Total Goal.
672 */
673 $form_args = (array) apply_filters( 'give_totals_goal_shortcode_query_args', $form_args );
674
675 $forms = new WP_Query( $form_args );
676
677 if ( isset( $forms->posts ) ) {
678 $total = 0;
679 foreach ( $forms->posts as $post ) {
680 $form_earning = give_get_meta( $post, '_give_form_earnings', true );
681 $form_earning = ! empty( $form_earning ) ? $form_earning : 0;
682
683 /**
684 * Update Form earnings.
685 *
686 * @since 2.1
687 *
688 * @param int $post Form ID.
689 * @param string $form_earning Total earning of Form.
690 * @param array $atts shortcode attributes.
691 */
692 $total += apply_filters( 'give_totals_form_earning', $form_earning, $post, $atts );
693 }
694 }
695 } // End if().
696
697 // Append link with text.
698 $donate_link = '';
699 if ( ! empty( $atts['link'] ) ) {
700 $donate_link = sprintf( ' <a class="give-totals-text-link" href="%1$s">%2$s</a>', esc_url( $atts['link'] ), esc_html( $atts['link_text'] ) );
701 }
702
703 // Replace {total} in message.
704 $message = str_replace(
705 '{total}',
706 give_currency_filter(
707 give_format_amount(
708 $total,
709 [ 'sanitize' => false ]
710 )
711 ),
712 esc_html( $atts['message'] )
713 );
714
715 // Replace {total_goal} in message.
716 $message = str_replace(
717 '{total_goal}',
718 give_currency_filter(
719 give_format_amount(
720 $total_goal,
721 [ 'sanitize' => true ]
722 )
723 ),
724 $message
725 );
726
727 /**
728 * Update Give totals shortcode output.
729 *
730 * @since 2.1
731 *
732 * @param string $message Shortcode Message.
733 * @param array $atts ShortCode attributes.
734 */
735 $message = apply_filters( 'give_totals_shortcode_message', $message, $atts );
736
737 ob_start();
738 ?>
739 <div class="give-totals-shortcode-wrap">
740 <?php
741 // Show Progress Bar if progress_bar set true.
742 $show_progress_bar = isset( $atts['progress_bar'] ) ? filter_var( $atts['progress_bar'], FILTER_VALIDATE_BOOLEAN ) : true;
743 if ( $show_progress_bar ) {
744 give_show_goal_totals_progress( $total, $total_goal );
745 }
746
747 echo sprintf( $message ) . $donate_link;
748 ?>
749 </div>
750 <?php
751 $give_totals_output = ob_get_clean();
752
753 /**
754 * Give Action fire after the total goal shortcode rendering is end.
755 *
756 * @since 2.1.4
757 *
758 * @param array $atts shortcode attribute.
759 * @param string $give_totals_output shortcode output.
760 */
761 do_action( 'give_totals_goal_shortcode_after_render', $atts, $give_totals_output );
762
763 /**
764 * Give Totals Shortcode output.
765 *
766 * @since 2.1
767 *
768 * @param string $give_totals_output
769 */
770 return apply_filters( 'give_totals_shortcode_output', $give_totals_output );
771
772 }
773
774 add_shortcode( 'give_totals', 'give_totals_shortcode' );
775
776
777 /**
778 * Displays donation forms in a grid layout.
779 *
780 * @since 2.1.0
781 *
782 * @param array $atts {
783 * Optional. Attributes of the form grid shortcode.
784 *
785 * @type int $forms_per_page Number of forms per page. Default '12'.
786 * @type bool $paged Whether to paginate forms. Default 'true'.
787 * @type string $ids A comma-separated list of form IDs to display. Default empty.
788 * @type string exclude A comma-separated list of form IDs to exclude from display. Default empty.
789 * @type string $cats A comma-separated list of form categories to display. Default empty.
790 * @type string $tags A comma-separated list of form tags to display. Default empty.
791 * @type string $columns Maximum columns to display. Default 'best-fit'.
792 * Accepts 'best-fit', '1', '2', '3', '4'.
793 * @type bool $show_title Whether to display form title. Default 'true'.
794 * @type bool $show_goal Whether to display form goal. Default 'true'.
795 * @type bool $show_excerpt Whether to display form excerpt. Default 'true'.
796 * @type bool $show_featured_image Whether to display featured image. Default 'true'.
797 * @type string $image_size Featured image size. Default 'medium'. Accepts WordPress image sizes.
798 * @type string $image_height Featured image height. Default 'auto'. Accepts valid CSS heights.
799 * @type int $excerpt_length Number of words before excerpt is truncated. Default '16'.
800 * @type string $display_style How the form is displayed, either in new page or modal popup.
801 * Default 'redirect'. Accepts 'redirect', 'modal'.
802 *
803 * @since 2.23.1 Updated the default text color for the donate button, see #6591.
804 * @since 2.21.2 change tag_background_color, progress_bar_color to official green color #69b868.
805 * change tag_text_color color to #333333.
806 * @since 2.20.0 $show_donate_button Option to show donate button
807 * @since 2.20.0 $donate_button_text Default Donate
808 * @since 2.20.0 $donate_button_background_color Default #66bb6a
809 * @since 2.20.0 $donate_button_text_color Default #fff
810 * @since 2.20.0 $show_bar Default false
811 * @since 2.22.2 remove $show_bar attribute in favor of show_goal
812 *
813 * @return string|bool The markup of the form grid or false.
814 */
815 function give_form_grid_shortcode( $atts ) {
816
817 $give_settings = give_get_settings();
818
819 $atts = shortcode_atts(
820 [
821 'forms_per_page' => 12,
822 'paged' => true,
823 'ids' => '',
824 'exclude' => '',
825 'orderby' => 'date',
826 'order' => 'DESC',
827 'cats' => '',
828 'tags' => '',
829 'columns' => '1',
830 'show_title' => true,
831 'show_goal' => true,
832 'show_excerpt' => true,
833 'show_featured_image' => true,
834 'show_donate_button' => true,
835 'donate_button_text' => '',
836 'tag_background_color' => '#69b868',
837 'tag_text_color' => '#333333',
838 'donate_button_text_color' => '#000000',
839 'image_size' => 'medium',
840 'image_height' => 'auto',
841 'excerpt_length' => 16,
842 'display_style' => 'modal_reveal',
843 'progress_bar_color' => '#69b868',
844 'status' => '', // open or closed.
845 ],
846 $atts
847 );
848
849 // Validate integer attributes.
850 $atts['forms_per_page'] = intval( $atts['forms_per_page'] );
851 $atts['excerpt_length'] = intval( $atts['excerpt_length'] );
852
853 // Validate boolean attributes.
854 $boolean_attributes = [
855 'paged',
856 'show_title',
857 'show_goal',
858 'show_excerpt',
859 'show_featured_image',
860 ];
861
862 foreach ( $boolean_attributes as $att ) {
863 $atts[ $att ] = filter_var( $atts[ $att ], FILTER_VALIDATE_BOOLEAN );
864 }
865
866 // Set default form query args.
867 $form_args = [
868 'post_type' => 'give_forms',
869 'post_status' => 'publish',
870 'posts_per_page' => $atts['forms_per_page'],
871 'orderby' => $atts['orderby'],
872 'order' => $atts['order'],
873 'paged' => $atts['paged'],
874 'tax_query' => [
875 'relation' => 'AND',
876 ],
877 ];
878
879 // Filter results of form grid based on form status.
880 $form_closed_status = trim( $atts['status'] );
881
882 if ( ! empty( $form_closed_status ) ) {
883 $form_args['meta_query'] = [
884 [
885 'key' => '_give_form_status',
886 'value' => $form_closed_status,
887 ],
888 ];
889 }
890
891 // Maybe add pagination.
892 if ( true === $atts['paged'] ) {
893 $form_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
894 }
895
896 // Maybe filter forms by IDs.
897 if ( ! empty( $atts['ids'] ) ) {
898 $form_args['post__in'] = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
899 }
900
901 // Convert comma-separated form IDs into array.
902 if ( ! empty( $atts['exclude'] ) ) {
903 $form_args['post__not_in'] = array_filter(
904 array_map(
905 function( $item ) {
906 return intval( trim( $item ) );
907 },
908 explode( ',', $atts['exclude'] )
909 )
910 );
911 }
912
913 // Maybe filter by form category.
914 if ( ! empty( $atts['cats'] ) ) {
915 $cats = array_filter( array_map( 'trim', explode( ',', $atts['cats'] ) ) );
916
917 // Backward compatibility for term_ids.
918 $term_ids = array_unique( array_filter( $cats, 'is_numeric' ) );
919 if ( $term_ids ) {
920 $form_args['tax_query'][] = [
921 'taxonomy' => 'give_forms_category',
922 'terms' => $term_ids,
923 ];
924
925 }
926
927 $term_slug = array_unique( array_filter( array_diff( $cats, $term_ids ) ) );
928 if ( $term_slug ) {
929 $form_args['tax_query'][] = [
930 'taxonomy' => 'give_forms_category',
931 'field' => 'slug',
932 'terms' => $term_slug,
933 ];
934
935 }
936 }
937
938 // Maybe filter by form tag.
939 if ( ! empty( $atts['tags'] ) ) {
940 $tags = array_filter( array_map( 'trim', explode( ',', $atts['tags'] ) ) );
941
942 // Backward compatibility for term_ids.
943 $tag_ids = array_unique( array_filter( $tags, 'is_numeric' ) );
944 if ( $tag_ids ) {
945 $form_args['tax_query'][] = [
946 'taxonomy' => 'give_forms_tag',
947 'terms' => $tag_ids,
948 ];
949
950 }
951
952 $tag_slug = array_unique( array_filter( array_diff( $tags, $tag_ids ) ) );
953 if ( $tag_slug ) {
954 $form_args['tax_query'][] = [
955 'taxonomy' => 'give_forms_tag',
956 'field' => 'slug',
957 'terms' => $tag_slug,
958 ];
959
960 }
961 }
962
963 /**
964 * Filter to modify WP Query for Total Goal.
965 *
966 * @since 2.1.4
967 *
968 * @param array $form_args WP query argument for Grid.
969 *
970 * @return array $form_args WP query argument for Grid.
971 */
972 $form_args = (array) apply_filters( 'give_form_grid_shortcode_query_args', $form_args );
973
974 // Maybe filter by form Amount Donated or Number of Donations.
975 switch ( $atts['orderby'] ) {
976 case 'amount_donated':
977 $form_args['meta_key'] = '_give_form_earnings';
978 $form_args['orderby'] = 'meta_value_num';
979 break;
980 case 'number_donations':
981 $form_args['meta_key'] = '_give_form_sales';
982 $form_args['orderby'] = 'meta_value_num';
983 break;
984 case 'random':
985 $form_args['orderby'] = 'rand';
986 break;
987 case 'closest_to_goal':
988 if ( give_has_upgrade_completed( 'v240_update_form_goal_progress' ) ) {
989 $form_args['meta_key'] = '_give_form_goal_progress';
990 $form_args['orderby'] = 'meta_value_num';
991 }
992 break;
993 }
994
995 // Query to output donation forms.
996 $form_query = new WP_Query( $form_args );
997
998 if ( $form_query->have_posts() ) {
999 ob_start();
1000
1001 add_filter( 'add_give_goal_progress_class', 'add_give_goal_progress_class', 10, 1 );
1002 add_filter( 'add_give_goal_progress_bar_class', 'add_give_goal_progress_bar_class', 10, 1 );
1003 add_filter( 'give_form_wrap_classes', 'add_class_for_form_grid', 10, 3 );
1004 add_action( 'give_donation_form_top', 'give_is_form_grid_page_hidden_field', 10, 3 );
1005
1006 echo '<div class="give-wrap">';
1007 echo '<div class="give-grid give-grid--' . esc_attr( $atts['columns'] ) . '">';
1008
1009 while ( $form_query->have_posts() ) {
1010 $form_query->the_post();
1011
1012 // Give/templates/shortcode-form-grid.php.
1013 give_get_template( 'shortcode-form-grid', [ $give_settings, $atts ] );
1014
1015 }
1016
1017 wp_reset_postdata();
1018
1019 echo '</div><!-- .give-grid -->';
1020
1021 remove_filter( 'add_give_goal_progress_class', 'add_give_goal_progress_class' );
1022 remove_filter( 'add_give_goal_progress_bar_class', 'add_give_goal_progress_bar_class' );
1023 remove_filter( 'give_form_wrap_classes', 'add_class_for_form_grid', 10 );
1024 remove_action( 'give_donation_form_top', 'give_is_form_grid_page_hidden_field', 10 );
1025
1026 if ( false !== $atts['paged'] ) {
1027 $paginate_args = [
1028 'current' => max( 1, get_query_var( 'paged' ) ),
1029 'total' => $form_query->max_num_pages,
1030 'show_all' => false,
1031 'end_size' => 1,
1032 'mid_size' => 2,
1033 'prev_next' => true,
1034 'prev_text' => __( '&laquo; Previous', 'give' ),
1035 'next_text' => __( 'Next &raquo;', 'give' ),
1036 'type' => 'plain',
1037 'add_args' => false,
1038 ];
1039
1040 printf(
1041 '<div class="give-page-numbers">%s</div>',
1042 paginate_links( $paginate_args )
1043 );
1044 }
1045 echo '</div><!-- .give-wrap -->';
1046
1047 return ob_get_clean();
1048 } // End if().
1049 }
1050
1051 add_shortcode( 'give_form_grid', 'give_form_grid_shortcode' );
1052
1053