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