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