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