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