PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.14.5
GiveWP – Donation Plugin and Fundraising Platform v4.14.5
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 / user-functions.php
give / includes Last commit date
admin 4 months ago api 3 years ago database 5 months ago deprecated 9 months ago donors 5 months ago emails 9 months ago forms 9 months ago frontend 6 years ago gateways 9 months ago libraries 9 months ago payments 2 months ago actions.php 9 months ago ajax-functions.php 9 months ago class-give-async-process.php 1 year ago class-give-background-updater.php 9 months ago class-give-cache-setting.php 1 year ago class-give-cache.php 9 months ago class-give-cli-commands.php 1 year ago class-give-comment.php 9 months ago class-give-cron.php 9 months ago class-give-donate-form.php 1 year ago class-give-donor.php 2 years ago class-give-email-access.php 5 years ago class-give-license-handler.php 1 year ago class-give-logging.php 9 months ago class-give-readme-parser.php 4 years ago class-give-roles.php 5 months ago class-give-scripts.php 8 months ago class-give-session.php 9 months 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 9 months ago country-functions.php 7 months ago currencies-list.php 7 months ago currency-functions.php 3 years ago error-tracking.php 6 years ago filters.php 9 months ago formatting.php 9 months ago install.php 9 months ago login-register.php 2 years ago misc-functions.php 9 months ago plugin-compatibility.php 6 years ago post-types.php 1 year ago price-functions.php 6 years ago process-donation.php 1 year ago setting-functions.php 6 years ago shortcodes.php 1 year ago template-functions.php 1 year ago user-functions.php 3 years ago
user-functions.php
633 lines
1 <?php
2 /**
3 * User Functions
4 *
5 * Functions related to users / donors
6 *
7 * @package Give
8 * @subpackage Functions
9 * @copyright Copyright (c) 2016, GiveWP
10 * @license https://opensource.org/licenses/gpl-license GNU Public License
11 * @since 1.0
12 */
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Get Users Donations
21 *
22 * Retrieves a list of all donations by a specific user.
23 *
24 * @param int $user User ID or email address.
25 * @param int $number Number of donations to retrieve.
26 * @param bool $pagination Enable/Disable Pagination.
27 * @param string $status Donation Status.
28 *
29 * @since 1.0
30 *
31 * @return bool|array List of all user donations.
32 */
33 function give_get_users_donations( $user = 0, $number = 20, $pagination = false, $status = 'complete' ) {
34
35 if ( empty( $user ) ) {
36 $user = get_current_user_id();
37 }
38
39 if ( 0 === $user && ! Give()->email_access->token_exists ) {
40 return false;
41 }
42
43 $status = ( 'complete' === $status ) ? 'publish' : $status;
44 $paged = 1;
45
46 if ( $pagination ) {
47 if ( get_query_var( 'paged' ) ) {
48 $paged = get_query_var( 'paged' );
49 } elseif ( get_query_var( 'page' ) ) {
50 $paged = get_query_var( 'page' );
51 }
52 }
53
54 $args = apply_filters(
55 'give_get_users_donations_args',
56 [
57 'user' => $user,
58 'number' => $number,
59 'status' => $status,
60 'orderby' => 'date',
61 ]
62 );
63
64 if ( $pagination ) {
65 $args['page'] = $paged;
66 } else {
67 $args['nopaging'] = true;
68 }
69
70 $by_user_id = is_numeric( $user ) ? true : false;
71 $donor = new Give_Donor( $user, $by_user_id );
72
73 if ( ! empty( $donor->payment_ids ) ) {
74
75 unset( $args['user'] );
76 $args['post__in'] = array_map( 'absint', explode( ',', $donor->payment_ids ) );
77
78 }
79
80 $donations = give_get_payments( apply_filters( 'give_get_users_donations_args', $args ) );
81
82 // No donations.
83 if ( ! $donations ) {
84 return false;
85 }
86
87 return $donations;
88 }
89
90 /**
91 * Get Users Donations
92 *
93 * Returns a list of unique donation forms given to by a specific user.
94 *
95 * @param int $user User ID or email address
96 * @param string $status Donation Status.
97 *
98 * @since 1.0
99 *
100 * @return bool|object List of unique forms donated by user
101 */
102 function give_get_users_completed_donations( $user = 0, $status = 'complete' ) {
103 if ( empty( $user ) ) {
104 $user = get_current_user_id();
105 }
106
107 if ( empty( $user ) ) {
108 return false;
109 }
110
111 $by_user_id = is_numeric( $user ) ? true : false;
112
113 $donor = new Give_Donor( $user, $by_user_id );
114
115 if ( empty( $donor->payment_ids ) ) {
116 return false;
117 }
118
119 // Get all the items donated.
120 $payment_ids = array_reverse( explode( ',', $donor->payment_ids ) );
121 $limit_payments = apply_filters( 'give_users_completed_donations_payments', 50 );
122
123 if ( ! empty( $limit_payments ) ) {
124 $payment_ids = array_slice( $payment_ids, 0, $limit_payments );
125 Give_Payments_Query::update_meta_cache( $payment_ids );
126 }
127
128 $donation_data = [];
129 foreach ( $payment_ids as $payment_id ) {
130 $donation_data[] = give_get_payment_meta( $payment_id );
131 }
132
133 if ( empty( $donation_data ) ) {
134 return false;
135 }
136
137 // Grab only the post ids "form_id" of the forms donated on this order.
138 $completed_donations_ids = [];
139 foreach ( $donation_data as $donation_meta ) {
140 $completed_donations_ids[] = isset( $donation_meta['form_id'] ) ? $donation_meta['form_id'] : '';
141 }
142
143 if ( empty( $completed_donations_ids ) ) {
144 return false;
145 }
146
147 // Only include each donation once.
148 $form_ids = array_unique( $completed_donations_ids );
149
150 // Make sure we still have some products and a first item.
151 if ( empty( $form_ids ) || ! isset( $form_ids[0] ) ) {
152 return false;
153 }
154
155 $post_type = get_post_type( $form_ids[0] );
156
157 $args = apply_filters(
158 'give_get_users_completed_donations_args',
159 [
160 'include' => $form_ids,
161 'post_type' => $post_type,
162 'posts_per_page' => - 1,
163 ]
164 );
165
166 return apply_filters( 'give_users_completed_donations_list', get_posts( $args ) );
167 }
168
169
170 /**
171 * Has donations
172 *
173 * Checks to see if a user has donated to at least one form.
174 *
175 * @param int $user_id The ID of the user to check.
176 *
177 * @access public
178 * @since 1.0
179 *
180 * @return bool True if has donated, false other wise.
181 */
182 function give_has_donations( $user_id = null ) {
183 if ( empty( $user_id ) ) {
184 $user_id = get_current_user_id();
185 }
186
187 if ( give_get_users_donations( $user_id, 1 ) ) {
188 return true; // User has at least one donation.
189 }
190
191 // User has never donated anything.
192 return false;
193 }
194
195
196 /**
197 * Get Donation Status for User.
198 *
199 * Retrieves the donation count and the total amount spent for a specific user.
200 *
201 * @param int|string $user The ID or email of the donor to retrieve stats for.
202 *
203 * @access public
204 * @since 1.0
205 *
206 * @return array
207 */
208 function give_get_donation_stats_by_user( $user = '' ) {
209
210 $field = '';
211
212 if ( is_email( $user ) ) {
213 $field = 'email';
214 } elseif ( is_numeric( $user ) ) {
215 $field = 'user_id';
216 }
217
218 $stats = [];
219 $donor = Give()->donors->get_donor_by( $field, $user );
220
221 if ( $donor ) {
222 $donor = new Give_Donor( $donor->id );
223 $stats['purchases'] = absint( $donor->purchase_count );
224 $stats['total_spent'] = give_maybe_sanitize_amount( $donor->get_total_donation_amount() );
225 }
226
227 /**
228 * Filter the donation stats.
229 *
230 * @since 1.7
231 */
232 $stats = (array) apply_filters( 'give_donation_stats_by_user', $stats, $user );
233
234 return $stats;
235 }
236
237
238 /**
239 * Count number of donations of a donor.
240 *
241 * Returns total number of donations a donor has made.
242 *
243 * @param int|string $user The ID or email of the donor.
244 *
245 * @access public
246 * @since 1.0
247 *
248 * @return int The total number of donations.
249 */
250 function give_count_donations_of_donor( $user = null ) {
251
252 // Logged in?
253 if ( empty( $user ) ) {
254 $user = get_current_user_id();
255 }
256
257 // Email access?
258 if ( empty( $user ) && Give()->email_access->token_email ) {
259 $user = Give()->email_access->token_email;
260 }
261
262 $stats = ! empty( $user ) ? give_get_donation_stats_by_user( $user ) : false;
263
264 return isset( $stats['purchases'] ) ? $stats['purchases'] : 0;
265 }
266
267 /**
268 * Calculates the total amount spent by a user.
269 *
270 * @param int|string $user The ID or email of the donor.
271 *
272 * @access public
273 * @since 1.0
274 *
275 * @return float The total amount the user has spent
276 */
277 function give_donation_total_of_user( $user = null ) {
278
279 $stats = give_get_donation_stats_by_user( $user );
280
281 return $stats['total_spent'];
282 }
283
284
285 /**
286 * Validate a potential username.
287 *
288 * @param string $username The username to validate.
289 * @param int $form_id Donation Form ID.
290 *
291 * @since 1.0
292 *
293 * @return bool
294 */
295 function give_validate_username( $username, $form_id = 0 ) {
296 $valid = true;
297
298 // Validate username.
299 if ( ! empty( $username ) ) {
300
301 // Sanitize username.
302 $sanitized_user_name = sanitize_user( $username, false );
303
304 // We have an user name, check if it already exists.
305 if ( username_exists( $username ) ) {
306 // Username already registered.
307 give_set_error( 'username_unavailable', __( 'Username already taken.', 'give' ) );
308 $valid = false;
309
310 // Check if it's valid.
311 } elseif ( $sanitized_user_name !== $username ) {
312 // Invalid username.
313 if ( is_multisite() ) {
314 give_set_error( 'username_invalid', __( 'Invalid username. Only lowercase letters (a-z) and numbers are allowed.', 'give' ) );
315 $valid = false;
316 } else {
317 give_set_error( 'username_invalid', __( 'Invalid username.', 'give' ) );
318 $valid = false;
319 }
320 }
321 } else {
322 // Username is empty.
323 give_set_error( 'username_empty', __( 'Enter a username.', 'give' ) );
324 $valid = false;
325
326 // Check if guest checkout is disable for form.
327 if ( $form_id && give_logged_in_only( $form_id ) ) {
328 give_set_error( 'registration_required', __( 'You must register or log in to complete your donation.', 'give' ) );
329 $valid = false;
330 }
331 }
332
333 /**
334 * Filter the username validation result.
335 *
336 * @param bool $valid Username is valid or not.
337 * @param string $username Username to check.
338 * @param bool $form_id Donation Form ID.
339 *
340 * @since 1.8
341 */
342 $valid = (bool) apply_filters( 'give_validate_username', $valid, $username, $form_id );
343
344 return $valid;
345 }
346
347
348 /**
349 * Validate user email.
350 *
351 * @param string $email User email.
352 * @param bool $registering_new_user Flag to check user register or not.
353 *
354 * @since 1.8
355 *
356 * @return bool
357 */
358 function give_validate_user_email( $email, $registering_new_user = false ) {
359 $valid = true;
360
361 if ( empty( $email ) ) {
362 // No email.
363 give_set_error( 'email_empty', __( 'Enter an email.', 'give' ) );
364 $valid = false;
365
366 } elseif ( email_exists( $email ) ) {
367 // Email already exists.
368 give_set_error( 'email_exists', __( 'The email address provided is already in use by a registered account on this site. Please log in and try again.', 'give' ) );
369 $valid = false;
370
371 } elseif ( ! is_email( $email ) ) {
372 // Validate email.
373 give_set_error( 'email_invalid', __( 'Invalid email.', 'give' ) );
374 $valid = false;
375
376 } elseif ( $registering_new_user ) {
377
378 // If donor email is not primary.
379 if ( ! email_exists( $email ) && give_donor_email_exists( $email ) && give_is_additional_email( $email ) ) {
380 // Check if email exists.
381 give_set_error( 'email_used', __( 'The email address provided is already associated with another donor in the system. Please log in or use a different email', 'give' ) );
382 $valid = false;
383 }
384 }
385
386 /**
387 * Filter the email validation result.
388 *
389 * @param bool $valid Email is valid or not.
390 * @param string $email Email to check.
391 * @param bool $registering_new_user Registering New or Existing User.
392 *
393 * @since 1.8
394 */
395 $valid = (bool) apply_filters( 'give_validate_user_email', $valid, $email, $registering_new_user );
396
397 return $valid;
398 }
399
400 /**
401 * Validate password.
402 *
403 * @param string $password Password to Validate.
404 * @param string $confirm_password Password to Confirm Validation.
405 * @param bool $registering_new_user Registering New or Existing User.
406 *
407 * @since 1.8
408 *
409 * @return bool
410 */
411 function give_validate_user_password( $password = '', $confirm_password = '', $registering_new_user = false ) {
412 $valid = true;
413
414 // Passwords Validation For New Donors Only.
415 if ( $registering_new_user ) {
416 // Password or confirmation missing.
417 if ( ! $password ) {
418 // The password is invalid.
419 give_set_error( 'password_empty', __( 'Enter a password.', 'give' ) );
420 $valid = false;
421 } elseif ( ! $confirm_password ) {
422 // Confirmation password is invalid.
423 give_set_error( 'confirmation_empty', __( 'Enter the password confirmation.', 'give' ) );
424 $valid = false;
425 }
426 }
427 // Passwords Validation For New Donors as well as Existing Donors.
428 if ( $password || $confirm_password ) {
429 if ( strlen( $password ) < 6 || strlen( $confirm_password ) < 6 ) {
430 // Seems Weak Password.
431 give_set_error( 'password_weak', __( 'Passwords should have at least 6 characters.', 'give' ) );
432 $valid = false;
433 }
434 if ( $password && $confirm_password ) {
435 // Verify confirmation matches.
436 if ( $password !== $confirm_password ) {
437 // Passwords do not match.
438 give_set_error( 'password_mismatch', __( 'Passwords you entered do not match. Please try again.', 'give' ) );
439 $valid = false;
440 }
441 }
442 }
443
444 /**
445 * Filter the password validation result.
446 *
447 * @param bool $valid Password is Valid or not.
448 * @param string $password Password to check validation.
449 * @param string $confirm_password Password to confirm validation.
450 * @param bool $registering_new_user Registering New or Existing User.
451 *
452 * @since 1.8
453 */
454 $valid = (bool) apply_filters( 'give_validate_user_email', $valid, $password, $confirm_password, $registering_new_user );
455
456 return $valid;
457 }
458
459 /**
460 * Counts the total number of donors.
461 *
462 * @access public
463 * @since 1.0
464 *
465 * @return int The total number of donors.
466 */
467 function give_count_total_donors() {
468 return Give()->donors->count();
469 }
470
471 /**
472 * Returns the saved address for a donor
473 *
474 * @access public
475 * @since 1.0
476 *
477 * @param int/null $donor_id Donor ID.
478 * @param array $args {
479 *
480 * @type bool $by_user_id Flag to validate find donor by donor ID or user ID
481 * @type string $address_type Optional. Which type of donor address this function will return.
482 * }
483 *
484 * @return array The donor's address, if any
485 */
486 function give_get_donor_address( $donor_id = null, $args = [] ) {
487
488 $args['by_user_id'] = false;
489
490 // Set user id if donor is empty.
491 if ( empty( $donor_id ) ) {
492 $donor_id = get_current_user_id();
493 $args['by_user_id'] = true;
494 }
495
496 $donor = new Give_Donor( $donor_id, (bool) $args['by_user_id'] );
497
498 return $donor->get_donor_address( $args );
499 }
500
501 /**
502 * Give New User Notification
503 *
504 * Sends the new user notification email when a user registers within the donation form
505 *
506 * @param int $donation_id Donation ID.
507 * @param array $donation_data An Array of Donation Data.
508 *
509 * @access public
510 * @since 1.0
511 *
512 * @return void
513 */
514 function give_new_user_notification( $donation_id = 0, $donation_data = [] ) {
515 // Bailout.
516 if (
517 empty( $donation_id )
518 || empty( $donation_data )
519 || ! isset( $_POST['give_create_account'] )
520 || 'on' !== give_clean( $_POST['give_create_account'] )
521 ) {
522 return;
523 }
524
525 // For backward compatibility
526 $user = get_user_by( 'ID', $donation_data['user_info']['id'] );
527
528 $donation_data['user_info'] = array_merge(
529 $donation_data['user_info'],
530 [
531 'user_id' => $donation_data['user_info']['id'],
532 'user_first' => $donation_data['user_info']['first_name'],
533 'user_last' => $donation_data['user_info']['last_name'],
534 'user_email' => $donation_data['user_info']['email'],
535 'user_login' => $user->user_login,
536 ]
537 );
538
539 do_action( 'give_new-donor-register_email_notification', $donation_data['user_info']['id'], $donation_data['user_info'], $donation_id );
540 do_action( 'give_donor-register_email_notification', $donation_data['user_info']['id'], $donation_data['user_info'], $donation_id );
541 }
542
543 add_action( 'give_insert_payment', 'give_new_user_notification', 10, 2 );
544
545
546 /**
547 * Get Donor Name By
548 *
549 * Retrieves the donor name based on the id and the name of the user or donation
550 *
551 * @param int $id The ID of donation or donor.
552 * @param string $from From will be a string to be passed as donation or donor.
553 *
554 * @access public
555 * @since 1.8.9
556 *
557 * @return string
558 */
559 function give_get_donor_name_by( $id = 0, $from = 'donation' ) {
560
561 // ID shouldn't be empty.
562 if ( empty( $id ) ) {
563 return '';
564 }
565
566 $name = '';
567 $title_prefix = '';
568
569 switch ( $from ) {
570
571 case 'donation':
572 $title_prefix = give_get_meta( $id, '_give_payment_donor_title_prefix', true );
573 $first_name = give_get_meta( $id, '_give_donor_billing_first_name', true );
574 $last_name = give_get_meta( $id, '_give_donor_billing_last_name', true );
575
576 $name = "{$first_name} {$last_name}";
577
578 break;
579
580 case 'donor':
581 $name = Give()->donors->get_column( 'name', $id );
582 $title_prefix = Give()->donor_meta->get_meta( $id, '_give_donor_title_prefix', true );
583
584 break;
585
586 }
587
588 // If title prefix is set then prepend it to name.
589 $name = give_get_donor_name_with_title_prefixes( $title_prefix, $name );
590
591 return $name;
592
593 }
594
595 /**
596 * Checks whether the given donor email exists in users as well as additional_email of donors.
597 *
598 * @param string $email Donor Email.
599 *
600 * @since 1.8.9
601 *
602 * @return boolean The user's ID on success, and false on failure.
603 */
604 function give_donor_email_exists( $email ) {
605 if ( Give()->donors->get_donor_by( 'email', $email ) ) {
606 return true;
607 }
608 return false;
609 }
610
611 /**
612 * This function will check whether the donor email is primary or additional.
613 *
614 * @param string $email Donor Email.
615 *
616 * @since 1.8.13
617 *
618 * @return bool
619 */
620 function give_is_additional_email( $email ) {
621 global $wpdb;
622
623 $meta_table = Give()->donor_meta->table_name;
624 $meta_type = Give()->donor_meta->meta_type;
625 $donor_id = $wpdb->get_var( $wpdb->prepare( "SELECT {$meta_type}_id FROM {$meta_table} WHERE meta_key = 'additional_email' AND meta_value = %s LIMIT 1", $email ) );
626
627 if ( empty( $donor_id ) ) {
628 return false;
629 }
630
631 return true;
632 }
633