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