PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.19.7
GiveWP – Donation Plugin and Fundraising Platform v2.19.7
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 4 years ago api 5 years ago database 5 years ago deprecated 4 years ago donors 5 years ago emails 4 years ago forms 4 years ago frontend 6 years ago gateways 4 years ago libraries 6 years ago payments 4 years ago actions.php 5 years ago ajax-functions.php 5 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 6 years ago class-give-cli-commands.php 6 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 5 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 6 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 6 years ago currency-functions.php 4 years ago error-tracking.php 6 years ago filters.php 5 years ago formatting.php 6 years ago install.php 4 years ago login-register.php 6 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 6 years ago template-functions.php 4 years ago user-functions.php 5 years ago
shortcodes.php
1025 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\Views\IframeView;
14 use Give\Helpers\Frontend\Shortcode as ShortcodeUtils;
15 use Give\Helpers\Form\Utils as FormUtils;
16 use Give\Helpers\Form\Template as FormTemplateUtils;
17 use Give\Helpers\Form\Template\Utils\Frontend as FrontendFormTemplateUtils;
18 use Give\Helpers\Frontend\ConfirmDonation;
19
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 /**
25 * Donation History Shortcode
26 *
27 * Displays a user's donation history.
28 *
29 * @since 1.0
30 *
31 * @param array $atts
32 * @param string|bool $content
33 *
34 * @return string|bool
35 */
36 function give_donation_history( $atts, $content = false ) {
37
38 $donation_history_args = shortcode_atts(
39 [
40 'id' => true,
41 'date' => true,
42 'donor' => false,
43 'amount' => true,
44 'status' => false,
45 'payment_method' => false,
46 ],
47 $atts,
48 'donation_history'
49 );
50
51 // Always show receipt link.
52 $donation_history_args['details'] = true;
53
54 // Set Donation History Shortcode Arguments in session variable.
55 Give()->session->set( 'give_donation_history_args', $donation_history_args );
56
57 $get_data = give_clean( filter_input_array( INPUT_GET ) );
58
59 // If payment_key query arg exists, return receipt instead of donation history.
60 if (
61 ! empty( $get_data['donation_id'] ) ||
62 (
63 ! empty( $get_data['action'] ) &&
64 'view_in_browser' === $get_data['action']
65 )
66 ) {
67 ob_start();
68
69 echo do_shortcode( ShortcodeUtils::getReceiptShortcodeFromConfirmationPage() );
70
71 // Display donation history link only if Receipt Access Session is available.
72 if ( give_get_receipt_session() || is_user_logged_in() ) {
73 echo sprintf(
74 '<a href="%s">%s</a>',
75 esc_url( give_get_history_page_uri() ),
76 __( '&laquo; Return to All Donations', 'give' )
77 );
78 }
79
80 return ob_get_clean();
81 }
82
83 $email_access = give_get_option( 'email_access' );
84
85 ob_start();
86
87 /**
88 * Determine access
89 *
90 * A. Check if a user is logged in or does a session exists.
91 * B. Does an email-access token exist?
92 */
93 if (
94 is_user_logged_in()
95 || false !== Give()->session->get_session_expiration()
96 || ( give_is_setting_enabled( $email_access ) && Give()->email_access->token_exists )
97 || true === give_get_history_session()
98 ) {
99 give_get_template_part( 'history', 'donations' );
100
101 if ( ! empty( $content ) ) {
102 echo do_shortcode( $content );
103 }
104 } elseif ( give_is_setting_enabled( $email_access ) ) {
105 // Is Email-based access enabled?
106 give_get_template_part( 'email', 'login-form' );
107
108 } else {
109
110 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 ) );
111 echo do_shortcode( '[give_login]' );
112 }
113
114 /**
115 * Filter to modify donation history HTMl
116 *
117 * @since 2.1
118 *
119 * @param string HTML content
120 * @param array $atts
121 * @param string $content content pass between enclose content
122 *
123 * @return string HTML content
124 */
125 return apply_filters( 'give_donation_history_shortcode_html', ob_get_clean(), $atts, $content );
126 }
127
128 add_shortcode( 'donation_history', 'give_donation_history' );
129
130 /**
131 * Donation Form Shortcode
132 *
133 * Show the Give donation form.
134 *
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 // Fetch the Give Form.
153 ob_start();
154
155 if ( ! FormUtils::isLegacyForm( $formId ) ) {
156 $showIframeInModal = 'button' === $atts['display_style'];
157 $iframeView = new IframeView();
158
159 ConfirmDonation::storePostedDataInDonationSession();
160
161 echo $iframeView->setFormId( $formId )
162 ->showInModal( $showIframeInModal )
163 ->setButtonTitle( $atts['continue_button_title'] )
164 ->setButtonColor( $atts['button_color'] )
165 ->render();
166 } else {
167 give_get_donation_form( $atts );
168 }
169
170 $final_output = ob_get_clean();
171
172 return apply_filters( 'give_donate_form', $final_output, $atts );
173 }
174
175 add_shortcode( 'give_form', 'give_form_shortcode' );
176
177 /**
178 * Donation Form Goal Shortcode.
179 *
180 * Show the Give donation form goals.
181 *
182 * @since 1.0
183 *
184 * @param array $atts Shortcode attributes.
185 *
186 * @return string
187 */
188 function give_goal_shortcode( $atts ) {
189 $atts = shortcode_atts(
190 [
191 'id' => '',
192 'show_text' => true,
193 'show_bar' => true,
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( wp_redirect( add_query_arg( $profile_edit_redirect_args, $data['give_redirect'] ) ) );
559 } else {
560 wp_redirect( 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 * @return string|bool The markup of the form grid or false.
796 */
797 function give_form_grid_shortcode( $atts ) {
798
799 $give_settings = give_get_settings();
800
801 $atts = shortcode_atts(
802 [
803 'forms_per_page' => 12,
804 'paged' => true,
805 'ids' => '',
806 'exclude' => '',
807 'orderby' => 'date',
808 'order' => 'DESC',
809 'cats' => '',
810 'tags' => '',
811 'columns' => 'best-fit',
812 'show_title' => true,
813 'show_goal' => true,
814 'show_excerpt' => true,
815 'show_featured_image' => true,
816 'image_size' => 'medium',
817 'image_height' => 'auto',
818 'excerpt_length' => 16,
819 'display_style' => 'modal_reveal',
820 'status' => '', // open or closed.
821 ],
822 $atts
823 );
824
825 // Validate integer attributes.
826 $atts['forms_per_page'] = intval( $atts['forms_per_page'] );
827 $atts['excerpt_length'] = intval( $atts['excerpt_length'] );
828
829 // Validate boolean attributes.
830 $boolean_attributes = [
831 'paged',
832 'show_title',
833 'show_goal',
834 'show_excerpt',
835 'show_featured_image',
836 ];
837
838 foreach ( $boolean_attributes as $att ) {
839 $atts[ $att ] = filter_var( $atts[ $att ], FILTER_VALIDATE_BOOLEAN );
840 }
841
842 // Set default form query args.
843 $form_args = [
844 'post_type' => 'give_forms',
845 'post_status' => 'publish',
846 'posts_per_page' => $atts['forms_per_page'],
847 'orderby' => $atts['orderby'],
848 'order' => $atts['order'],
849 'tax_query' => [
850 'relation' => 'AND',
851 ],
852 ];
853
854 // Filter results of form grid based on form status.
855 $form_closed_status = trim( $atts['status'] );
856
857 if ( ! empty( $form_closed_status ) ) {
858 $form_args['meta_query'] = [
859 [
860 'key' => '_give_form_status',
861 'value' => $form_closed_status,
862 ],
863 ];
864 }
865
866 // Maybe add pagination.
867 if ( true === $atts['paged'] ) {
868 $form_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
869 }
870
871 // Maybe filter forms by IDs.
872 if ( ! empty( $atts['ids'] ) ) {
873 $form_args['post__in'] = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
874 }
875
876 // Convert comma-separated form IDs into array.
877 if ( ! empty( $atts['exclude'] ) ) {
878 $form_args['post__not_in'] = array_filter(
879 array_map(
880 function( $item ) {
881 return intval( trim( $item ) );
882 },
883 explode( ',', $atts['exclude'] )
884 )
885 );
886 }
887
888 // Maybe filter by form category.
889 if ( ! empty( $atts['cats'] ) ) {
890 $cats = array_filter( array_map( 'trim', explode( ',', $atts['cats'] ) ) );
891
892 // Backward compatibility for term_ids.
893 $term_ids = array_unique( array_filter( $cats, 'is_numeric' ) );
894 if ( $term_ids ) {
895 $form_args['tax_query'][] = [
896 'taxonomy' => 'give_forms_category',
897 'terms' => $term_ids,
898 ];
899
900 }
901
902 $term_slug = array_unique( array_filter( array_diff( $cats, $term_ids ) ) );
903 if ( $term_slug ) {
904 $form_args['tax_query'][] = [
905 'taxonomy' => 'give_forms_category',
906 'field' => 'slug',
907 'terms' => $term_slug,
908 ];
909
910 }
911 }
912
913 // Maybe filter by form tag.
914 if ( ! empty( $atts['tags'] ) ) {
915 $tags = array_filter( array_map( 'trim', explode( ',', $atts['tags'] ) ) );
916
917 // Backward compatibility for term_ids.
918 $tag_ids = array_unique( array_filter( $tags, 'is_numeric' ) );
919 if ( $tag_ids ) {
920 $form_args['tax_query'][] = [
921 'taxonomy' => 'give_forms_tag',
922 'terms' => $tag_ids,
923 ];
924
925 }
926
927 $tag_slug = array_unique( array_filter( array_diff( $tags, $tag_ids ) ) );
928 if ( $tag_slug ) {
929 $form_args['tax_query'][] = [
930 'taxonomy' => 'give_forms_tag',
931 'field' => 'slug',
932 'terms' => $tag_slug,
933 ];
934
935 }
936 }
937
938 /**
939 * Filter to modify WP Query for Total Goal.
940 *
941 * @since 2.1.4
942 *
943 * @param array $form_args WP query argument for Grid.
944 *
945 * @return array $form_args WP query argument for Grid.
946 */
947 $form_args = (array) apply_filters( 'give_form_grid_shortcode_query_args', $form_args );
948
949 // Maybe filter by form Amount Donated or Number of Donations.
950 switch ( $atts['orderby'] ) {
951 case 'amount_donated':
952 $form_args['meta_key'] = '_give_form_earnings';
953 $form_args['orderby'] = 'meta_value_num';
954 break;
955 case 'number_donations':
956 $form_args['meta_key'] = '_give_form_sales';
957 $form_args['orderby'] = 'meta_value_num';
958 break;
959 case 'closest_to_goal':
960 if ( give_has_upgrade_completed( 'v240_update_form_goal_progress' ) ) {
961 $form_args['meta_key'] = '_give_form_goal_progress';
962 $form_args['orderby'] = 'meta_value_num';
963 }
964 break;
965 }
966
967 // Query to output donation forms.
968 $form_query = new WP_Query( $form_args );
969
970 if ( $form_query->have_posts() ) {
971 ob_start();
972
973 add_filter( 'add_give_goal_progress_class', 'add_give_goal_progress_class', 10, 1 );
974 add_filter( 'add_give_goal_progress_bar_class', 'add_give_goal_progress_bar_class', 10, 1 );
975 add_filter( 'give_form_wrap_classes', 'add_class_for_form_grid', 10, 3 );
976 add_action( 'give_donation_form_top', 'give_is_form_grid_page_hidden_field', 10, 3 );
977
978 echo '<div class="give-wrap">';
979 echo '<div class="give-grid give-grid--' . esc_attr( $atts['columns'] ) . '">';
980
981 while ( $form_query->have_posts() ) {
982 $form_query->the_post();
983
984 // Give/templates/shortcode-form-grid.php.
985 give_get_template( 'shortcode-form-grid', [ $give_settings, $atts ] );
986
987 }
988
989 wp_reset_postdata();
990
991 echo '</div><!-- .give-grid -->';
992
993 remove_filter( 'add_give_goal_progress_class', 'add_give_goal_progress_class' );
994 remove_filter( 'add_give_goal_progress_bar_class', 'add_give_goal_progress_bar_class' );
995 remove_filter( 'give_form_wrap_classes', 'add_class_for_form_grid', 10 );
996 remove_action( 'give_donation_form_top', 'give_is_form_grid_page_hidden_field', 10 );
997
998 if ( false !== $atts['paged'] ) {
999 $paginate_args = [
1000 'current' => max( 1, get_query_var( 'paged' ) ),
1001 'total' => $form_query->max_num_pages,
1002 'show_all' => false,
1003 'end_size' => 1,
1004 'mid_size' => 2,
1005 'prev_next' => true,
1006 'prev_text' => __( '&laquo; Previous', 'give' ),
1007 'next_text' => __( 'Next &raquo;', 'give' ),
1008 'type' => 'plain',
1009 'add_args' => false,
1010 ];
1011
1012 printf(
1013 '<div class="give-page-numbers">%s</div>',
1014 paginate_links( $paginate_args )
1015 );
1016 }
1017 echo '</div><!-- .give-wrap -->';
1018
1019 return ob_get_clean();
1020 } // End if().
1021 }
1022
1023 add_shortcode( 'give_form_grid', 'give_form_grid_shortcode' );
1024
1025