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