PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.9
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.9
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / users / class-charitable-user.php

class-charitable-user.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.9, at includes/users/class-charitable-user.php

1,134 lines 28.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Contains the class that models users in Charitable.
4 *
5 * There are several different user roles in Charitable, and one user
6 * may be more than one. People who make donations get the "donor" role;
7 * people who create campaigns (via Charitable Ambassadors) get
8 * the "campaign_creator" role; people who create fundraisers for campaigns
9 * get the "fundraiser" role.
10 *
11 * @package Charitable/Classes/Charitable_User
12 * @author David Bisset
13 * @copyright Copyright (c) 2022, WP Charitable LLC
14 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
15 * @since 1.0.0
16 * @version 1.6.44
17 */
18
19 // Exit if accessed directly.
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 if ( ! class_exists( 'Charitable_User' ) ) :
25
26 /**
27 * Charitable_User
28 *
29 * @since 1.0.0
30 *
31 * @property string $nickname
32 * @property string $description
33 * @property string $user_description
34 * @property string $first_name
35 * @property string $user_firstname
36 * @property string $last_name
37 * @property string $user_lastname
38 * @property string $user_login
39 * @property string $user_pass
40 * @property string $user_nicename
41 * @property string $user_email
42 * @property string $user_url
43 * @property string $user_registered
44 * @property string $user_activation_key
45 * @property string $user_status
46 * @property int $user_level
47 * @property string $display_name
48 * @property string $spam
49 * @property string $deleted
50 * @property string $locale
51 */
52 class Charitable_User extends WP_User {
53
54 /**
55 * The donor ID.
56 *
57 * NOTE: This is not the same as the user ID.
58 *
59 * @since 1.0.0
60 *
61 * @var int
62 */
63 protected $donor_id;
64
65 /**
66 * A mapping of user keys.
67 *
68 * @since 1.4.0
69 *
70 * @var string[]
71 */
72 protected $mapped_keys;
73
74 /**
75 * Core keys.
76 *
77 * @since 1.4.0
78 *
79 * @var string[]
80 */
81 protected $core_keys;
82
83 /**
84 * Create class object.
85 *
86 * @since 1.0.0
87 *
88 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
89 * @param string $name Optional. User's username.
90 * @param int $blog_id Optional Blog ID, defaults to current blog.
91 * @return void
92 */
93 public function __construct( $id = 0, $name = '', $blog_id = '' ) {
94 parent::__construct( $id, $name, $blog_id );
95 }
96
97 /**
98 * Create object using a donor ID.
99 *
100 * @since 1.0.0
101 *
102 * @param int $donor_id The donor ID.
103 * @return Charitable_user
104 */
105 public static function init_with_donor( $donor_id ) {
106 $user_id = charitable_get_table( 'donors' )->get_user_id( $donor_id );
107 $user = charitable_get_user( $user_id );
108 $user->set_donor_id( $donor_id );
109 return $user;
110 }
111
112 /**
113 * Magic getter method. Looks for the specified key in the mapped keys before using WP_User's __get method.
114 *
115 * @since 1.0.0
116 *
117 * @param string $key The key to retrieve.
118 * @return mixed
119 */
120 public function __get( $key ) {
121 return parent::__get( $this->map_key( $key ) );
122 }
123
124 /**
125 * Display the donor name when printing the object.
126 *
127 * @since 1.0.0
128 *
129 * @return string
130 */
131 public function __toString() {
132 return $this->get_name();
133 }
134
135 /**
136 * Determine whether a property or meta key is set
137 *
138 * Consults the users and usermeta tables.
139 *
140 * @since 1.5.0
141 *
142 * @param string $key Property.
143 * @return boolean
144 */
145 public function has_prop( $key ) {
146 return parent::has_prop( $this->map_key( $key ) );
147 }
148
149 /**
150 * Returns whether the user is logged in.
151 *
152 * @since 1.0.0
153 *
154 * @return boolean
155 */
156 public function is_logged_in() {
157 return 0 !== $this->ID;
158 }
159
160 /**
161 * Set the donor ID of this user.
162 *
163 * @since 1.0.0
164 *
165 * @param int $donor_id The Donor ID.
166 * @return void
167 */
168 public function set_donor_id( $donor_id ) {
169 $this->donor_id = $donor_id;
170 }
171
172 /**
173 * Return the donor ID of this user.
174 *
175 * @since 1.0.0
176 *
177 * @return int|false $donor_id
178 */
179 public function get_donor_id() {
180 if ( isset( $this->donor_id ) || ! is_null( $this->get_donor() ) ) {
181 return $this->donor_id;
182 }
183
184 return false;
185 }
186
187 /**
188 * Return the donor record.
189 *
190 * @since 1.0.0
191 *
192 * @return Object|null Object if a donor record could be matched. null if user is logged out or no donor record found.
193 */
194 public function get_donor() {
195 if ( ! $this->is_logged_in() && ! isset( $this->donor_id ) ) {
196 return null;
197 }
198
199 if ( isset( $this->donor_id ) ) {
200
201 $donor = wp_cache_get( $this->donor_id, 'donors' );
202
203 if ( is_object( $donor ) ) {
204 return $donor;
205 }
206
207 $donor = charitable_get_table( 'donors' )->get( $this->donor_id );
208
209 } else {
210
211 $donor = charitable_get_table( 'donors' )->get_by( 'email', $this->get( 'user_email' ) );
212
213 if ( ! is_object( $donor ) ) {
214 return null;
215 }
216
217 $this->donor_id = $donor->donor_id;
218
219 }//end if
220
221 wp_cache_add( $this->donor_id, $donor, 'donors' );
222
223 return $donor;
224 }
225
226 /**
227 * Returns whether the user has ever made a donation.
228 *
229 * @since 1.0.0
230 *
231 * @return boolean
232 */
233 public function is_donor() {
234 return ! is_null( $this->get_donor() );
235 }
236
237 /**
238 * Returns whether the user has verified their email address.
239 *
240 * @since 1.5.0
241 *
242 * @return int If verified, return 1. Otherwise return 0.
243 */
244 public function is_verified() {
245 return absint( get_user_meta( $this->ID, '_charitable_user_verified', true ) );
246 }
247
248 /**
249 * Mark a user as verified.
250 *
251 * @since 1.5.0
252 *
253 * @param boolean $verified Whether the user is verified.
254 * @return int|boolean Meta ID if the key didn't exist, true on successful update, false on failure.
255 */
256 public function mark_as_verified( $verified ) {
257 $verified = update_user_meta( $this->ID, '_charitable_user_verified', $verified );
258
259 if ( ! $verified ) {
260 return $verified;
261 }
262
263 /* Delete the time that the verification email was sent. */
264 delete_user_meta( $this->ID, '_charitable_email_verification_email_send_time' );
265
266 /* Check for a notice about having sent a verification email and remove it. */
267 charitable_get_notices()->clear_notice( "/We have sent you an email to confirm your email address. Haven\'t received the email\?/" );
268
269 /* Check for an existing donor account. */
270 $donor_id = $this->get_donor_id();
271
272 if ( $donor_id ) {
273 $donor = new Charitable_Donor( $donor_id );
274 $donor->set_user_id( $this->ID );
275 }
276
277 /**
278 * Do something when a user is verified.
279 *
280 * @since 1.6.23
281 *
282 * @param Charitable_User $user The user object.
283 */
284 do_action( 'charitable_user_verified', $this );
285
286 return $verified;
287 }
288
289 /**
290 * Returns the email address of the donor.
291 *
292 * @since 1.0.0
293 *
294 * @return string
295 */
296 public function get_email() {
297 if ( $this->get_donor() ) {
298 $email = $this->get_donor()->email;
299 } else {
300 $email = $this->get( 'user_email' );
301 }
302
303 /**
304 * Filter the user email address.
305 *
306 * @since 1.0.0
307 *
308 * @param string $email The email address.
309 * @param Charitable_User $user This instance of `Charitable_User`.
310 */
311 return apply_filters( 'charitable_user_email', $email, $this );
312 }
313
314 /**
315 * Returns the display name of the user.
316 *
317 * @since 1.0.0
318 *
319 * @return string
320 */
321 public function get_name() {
322 if ( $this->is_donor() ) {
323 $name = rtrim( sprintf( '%s %s', $this->get_donor()->first_name, $this->get_donor()->last_name ) );
324 } else {
325 $name = $this->display_name;
326 }
327
328 if ( ! $name ) {
329 $name = '';
330 }
331
332 return apply_filters( 'charitable_user_name', $name, $this );
333 }
334
335 /**
336 * Returns the first name of the user.
337 *
338 * @since 1.1.0
339 *
340 * @return string
341 */
342 public function get_first_name() {
343 if ( $this->is_donor() && $this->get_donor()->first_name ) {
344 $name = $this->get_donor()->first_name;
345 } else {
346 $name = $this->first_name;
347 }
348
349 return apply_filters( 'charitable_user_first_name', $name, $this );
350 }
351
352 /**
353 * Returns the last name of the user.
354 *
355 * @since 1.1.0
356 *
357 * @return string
358 */
359 public function get_last_name() {
360 if ( $this->is_donor() && $this->get_donor()->last_name ) {
361 $name = $this->get_donor()->last_name;
362 } else {
363 $name = $this->last_name;
364 }
365 return apply_filters( 'charitable_user_last_name', $name, $this );
366 }
367
368 /**
369 * Returns the user's location.
370 *
371 * @since 1.0.0
372 *
373 * @return string
374 */
375 public function get_location() {
376 $city = $this->get( 'donor_city' );
377 $state = $this->get( 'donor_state' );
378 $country = $this->get( 'donor_country' );
379 $location = '';
380
381 if ( strlen( $city ) || strlen( $state ) ) {
382 $region = strlen( $city ) ? $city : $state;
383
384 if ( strlen( $country ) ) {
385 $location = sprintf( '%s, %s', $region, $country );
386 } else {
387 $location = $region;
388 }
389 } elseif ( strlen( $country ) ) {
390 $location = $country;
391 }
392
393 return apply_filters( 'charitable_user_location', $location, $this );
394 }
395
396 /**
397 * Returns printable address of donor.
398 *
399 * @since 1.0.0
400 *
401 * @param int $donation_id Optional. If set, will return the address provided for the specific donation. Otherwise, returns the current address for the user.
402 * @return string
403 */
404 public function get_address( $donation_id = '' ) {
405 $address_details = false;
406
407 if ( $donation_id ) {
408 $address_details = get_post_meta( $donation_id, 'donor', true );
409 }
410
411 /* If the address fields were not set by the check above, get them from the user meta. */
412 if ( ! is_array( $address_details ) ) {
413
414 $address_details = array(
415 'first_name' => $this->get( 'first_name' ),
416 'last_name' => $this->get( 'last_name' ),
417 'company' => $this->get( 'donor_company' ),
418 'address' => $this->get( 'donor_address' ),
419 'address_2' => $this->get( 'donor_address_2' ),
420 'city' => $this->get( 'donor_city' ),
421 'state' => $this->get( 'donor_state' ),
422 'postcode' => $this->get( 'donor_postcode' ),
423 'country' => $this->get( 'donor_country' ),
424 );
425
426 }
427
428 /**
429 * Filter the address details for the current user.
430 *
431 * Please note that prior to 1.6.44, the hook used here was `charitable_user_address_fields`,
432 * which is used elsewhere in a different context and thus resulted in unexpected results.
433 *
434 * @since 1.6.44
435 *
436 * @param array $address_details The address details.
437 * @param Charitable_User $user The user object.
438 * @param int $donation_id The donation ID.
439 */
440 $address_details = apply_filters( 'charitable_user_address_details', $address_details, $this, $donation_id );
441
442 return charitable_get_location_helper()->get_formatted_address( $address_details );
443 }
444
445 /**
446 * Return all donations made by donor.
447 *
448 * @since 1.0.0
449 *
450 * @param boolean $distinct_donations If true, will only count unique donations.
451 * @return object[]
452 */
453 public function get_donations( $distinct_donations = false ) {
454 return charitable_get_table( 'campaign_donations' )->get_donations_by_donor( $this->get_donor_id(), $distinct_donations );
455 }
456
457 /**
458 * Return the number of donations made by the donor.
459 *
460 * @since 1.0.0
461 *
462 * @param boolean $distinct_donations If true, will only count unique donations.
463 * @return int
464 */
465 public function count_donations( $distinct_donations = false ) {
466 return charitable_get_table( 'campaign_donations' )->count_donations_by_donor( $this->get_donor_id(), $distinct_donations );
467 }
468
469 /**
470 * Return the number of campaigns that the donor has supported.
471 *
472 * @since 1.0.0
473 *
474 * @return int
475 */
476 public function count_campaigns_supported() {
477 return charitable_get_table( 'campaign_donations' )->count_campaigns_supported_by_donor( $this->get_donor_id() );
478 }
479
480 /**
481 * Return the total amount donated by the donor.
482 *
483 * @since 1.0.0
484 *
485 * @param int $campaign_id Optional. If set, returns total donated to this particular campaign.
486 * @return float
487 */
488 public function get_total_donated( $campaign_id = false ) {
489 $amount = wp_cache_get( $this->get_donor_id(), 'charitable_donor_total_donation_amount_' . $campaign_id );
490
491 if ( false === $amount ) {
492
493 $args = apply_filters(
494 'charitable_user_total_donated_query_args',
495 array(
496 'output' => 'raw',
497 'donor_id' => $this->get_donor_id(),
498 'distinct_donors' => true,
499 'fields' => 'amount',
500 'campaign' => (int) $campaign_id,
501 ),
502 $this
503 );
504
505 $query = new Charitable_Donor_Query( $args );
506
507 $amount = $query->current()->amount;
508
509 wp_cache_set( $this->get_donor_id(), $amount, 'charitable_donor_total_donation_amount_' . $campaign_id );
510 }
511
512 return (float) $amount;
513 }
514
515 /**
516 * Returns the user's avatar as a fully formatted <img> tag.
517 *
518 * By default, this will return the gravatar, but it can
519 * be extended to add support for locally hosted avatars.
520 *
521 * @since 1.0.0
522 *
523 * @param int $size The length and width of the avatar.
524 * @return string
525 */
526 public function get_avatar( $size = 100, $donor_id = false ) {
527 /**If you use this filter, return an attachment ID. */
528 // get_user_meta( $user_id, 'avatar', true )
529 $avatar_attachment_id = get_user_meta( $this->ID, 'avatar', true );
530
531 // print_r( $this );
532
533 // echo 'avatar_attachment_id: ' . $avatar_attachment_id; exit;
534
535 $avatar_attachment_id = apply_filters( 'charitable_user_avatar', $avatar_attachment_id, $this );
536
537 // echo 'user id being passed in is ' . $donor_id . ' and the user id from this class is '.$this->ID.' and right now the attachment id is ' . $avatar_attachment_id;
538
539 // echo '------';
540
541 /* If we don't have an attachment ID, display the gravatar. */
542 if ( ! $avatar_attachment_id ) {
543 return get_avatar( $this->ID, $size );
544 }
545
546 $img_size = apply_filters( 'charitable_user_avatar_media_size', array( $size, $size ), $size );
547
548 $attachment_src = wp_get_attachment_image_src( $avatar_attachment_id, $img_size );
549
550 /* No image for the given attachment ID? Fall back to the gravatar. */
551 if ( ! $attachment_src ) {
552 return get_avatar( $this->ID, $size );
553 }
554
555 return apply_filters(
556 'charitable_user_avatar_custom',
557 sprintf(
558 '<img src="%s" alt="%s" class="avatar photo" width="%s" height="%s" />',
559 $attachment_src[0],
560 esc_attr( $this->display_name ),
561 $attachment_src[1],
562 $attachment_src[2]
563 ),
564 $avatar_attachment_id,
565 $size,
566 $this
567 );
568 }
569
570 /**
571 * Return the src of the avatar.
572 *
573 * @since 1.0.0
574 *
575 * @param int $size The length and the width of the avatar.
576 * @return string
577 */
578 public function get_avatar_src( $size = 100 ) {
579 /* If this returns something, we don't need to deal with the gravatar. */
580 $avatar = apply_filters( 'charitable_user_avatar_src', false, $this, $size );
581
582 if ( false === $avatar ) {
583
584 /* The gravatars are returned as fully formatted img tags, so we need to pull out the src. */
585 $gravatar = get_avatar( $this->ID, $size );
586
587 preg_match( "@src='([^']+)'@", $gravatar, $matches );
588
589 $avatar = array_pop( $matches );
590 }
591
592 return $avatar;
593 }
594
595 /**
596 * Return the campaigns created by the user.
597 *
598 * @since 1.0.0
599 *
600 * @param array $args Optional. Any arguments accepted by WP_Query.
601 * @return WP_Query
602 */
603 public function get_campaigns( $args = array() ) {
604 $defaults = array(
605 'author' => $this->ID,
606 );
607
608 $args = wp_parse_args( $args, $defaults );
609
610 return Charitable_Campaigns::query( $args );
611 }
612
613 /**
614 * Checks whether the user has any current campaigns (i.e. non-expired).
615 *
616 * @since 1.0.0
617 *
618 * @param array $args Query arguments.
619 * @return WP_Query
620 */
621 public function get_current_campaigns( $args = array() ) {
622 $defaults = array(
623 'author' => $this->ID,
624 'meta_query' => array(
625 'relation' => 'OR',
626 array(
627 'key' => '_campaign_end_date',
628 'value' => date( 'Y-m-d H:i:s' ),
629 'compare' => '>=',
630 'type' => 'datetime',
631 ),
632 array(
633 'key' => '_campaign_end_date',
634 'value' => '0',
635 ),
636 ),
637 );
638
639 $args = wp_parse_args( $args, $defaults );
640
641 return Charitable_Campaigns::query( $args );
642 }
643
644 /**
645 * Returns all current campaigns by the user.
646 *
647 * @since 1.0.0
648 *
649 * @return WP_Query
650 */
651 public function has_current_campaigns() {
652 return $this->get_current_campaigns()->found_posts;
653 }
654
655 /**
656 * Returns the user's donation and campaign creation activity.
657 *
658 * @see WP_Query
659 * @since 1.0.0
660 *
661 * @param array $args Optional. Any arguments accepted by WP_Query.
662 * @return WP_Query
663 */
664 public function get_activity( $args = array() ) {
665 $defaults = array(
666 'author' => $this->ID,
667 'post_status' => array( 'charitable-completed', 'charitable-preapproved', 'publish' ),
668 'post_type' => array( 'donation', 'campaign' ),
669 'order' => 'DESC',
670 'orderby' => 'date',
671 );
672
673 $args = wp_parse_args( $args, $defaults );
674
675 $args = apply_filters( 'charitable_user_activity_args', $args, $this );
676
677 return new WP_Query( $args );
678 }
679
680 /**
681 * Add a new donor. This may also create a user account for them.
682 *
683 * @since 1.0.0
684 *
685 * @param array $submitted Values submitted by the user.
686 * @return int $donor_id Donor ID.
687 */
688 public function add_donor( $submitted = array() ) {
689 $email = false;
690
691 if ( isset( $submitted['email'] ) ) {
692 $email = $submitted['email'];
693 }
694
695 if ( ! $email && $this->is_logged_in() ) {
696 $email = $this->user_email;
697 }
698
699 /**
700 * Filter whether donors can be added without an email address.
701 *
702 * Prior to Charitable 1.6, this was never permitted. As of Charitable 1.6, donors
703 * without an email address can be added by default, primarily to support manual
704 * donations without an email address. Use this filter to disable that ability.
705 *
706 * NOTE: By default, the public donation form still requires an email address, so this
707 * primarily affects programmatically created donors, or donors created via manual
708 * donations in the admin.
709 *
710 * @see https://github.com/Charitable/Charitable/issues/535
711 *
712 * @since 1.6.0
713 *
714 * @param boolean $permitted Whether donors can be added without an email address.
715 */
716 if ( ! $email && ! charitable_permit_donor_without_email() ) {
717 charitable_get_deprecated()->doing_it_wrong(
718 __METHOD__,
719 __( 'Unable to add donor. Email not set for logged out user.', 'charitable' ),
720 '1.0.0'
721 );
722
723 return 0;
724 }
725
726 /**
727 * Filter the donor values.
728 *
729 * @since 1.0.0
730 *
731 * @param array $donor_values The donor values.
732 * @param Charitable_User $user This instance of `Charitable_User`.
733 * @param array $submitted Submitted values.
734 */
735 $donor_values = apply_filters(
736 'charitable_donor_values',
737 array(
738 'user_id' => $this->ID,
739 'email' => $email,
740 'first_name' => array_key_exists( 'first_name', $submitted ) ? $submitted['first_name'] : $this->first_name,
741 'last_name' => array_key_exists( 'last_name', $submitted ) ? $submitted['last_name'] : $this->last_name,
742 ),
743 $this,
744 $submitted
745 );
746
747 $donor_id = charitable_get_table( 'donors' )->insert( $donor_values );
748
749 /**
750 * If the user is logged in, add the "Donor" role to their account.
751 */
752 if ( $this->is_logged_in() ) {
753 $this->add_role( 'donor' );
754 }
755
756 do_action( 'charitable_after_insert_donor', $donor_id, $this );
757
758 return $donor_id;
759 }
760
761 /**
762 * Insert a new donor with submitted values.
763 *
764 * @since 1.4.0
765 *
766 * @param array $submitted The submitted values.
767 * @param array $keys The keys of fields that are to be updated.
768 * @return int
769 */
770 public static function create_profile( $submitted = array(), $keys = array() ) {
771 $user = new Charitable_User();
772 $user_id = $user->update_profile( $submitted, $keys );
773 return new Charitable_User( $user_id );
774 }
775
776 /**
777 * Update the user's details with submitted values.
778 *
779 * @since 1.0.0
780 *
781 * @param array $submitted The submitted values.
782 * @param array $keys The keys of fields that are to be updated.
783 * @return int
784 */
785 public function update_profile( $submitted = array(), $keys = array() ) {
786 if ( empty( $submitted ) ) {
787 $submitted = $_POST;
788 }
789
790 if ( empty( $keys ) ) {
791 $keys = array_keys( $submitted );
792 }
793
794 $user_id = $this->update_core_user( $submitted );
795
796 /* If there were problems with creating the user, stop here. */
797 if ( ! $user_id ) {
798 return $user_id;
799 }
800
801 /* Create or update the user's donor record. */
802 $this->update_donor_record( $submitted, $keys );
803
804 $this->update_user_meta( $submitted, $keys );
805
806 return $user_id;
807 }
808
809 /**
810 * Create or update a donor record for the current user.
811 *
812 * @since 1.6.2
813 *
814 * @param array $submitted The submitted values.
815 * @param array $keys The keys of fields that are to be updated.
816 * @return int The donor ID, or 0 if none was created or edited.
817 */
818 public function update_donor_record( $submitted, $keys ) {
819 $donor_fields = array_intersect( $keys, charitable_get_donor_keys() );
820
821 if ( array_key_exists( 'user_email', $submitted ) ) {
822 $donor_fields[] = 'email';
823 $submitted['email'] = $submitted['user_email'];
824 }
825
826 if ( empty( $donor_fields ) ) {
827 return 0;
828 }
829
830 $donor_data = array(
831 'user_id' => $this->ID,
832 );
833
834 foreach ( $donor_fields as $field ) {
835 if ( array_key_exists( $field, $submitted ) ) {
836 $value = $submitted[ $field ];
837 } elseif ( 'contact_consent' == $field ) {
838 $value = 0;
839 } else {
840 $value = '';
841 }
842
843 $donor_data[ $field ] = $value;
844 }
845
846 if ( array_key_exists( 'donor_id', $donor_data ) ) {
847 $donor_id = $donor_data['donor_id'];
848 } else {
849 $donor_id = $this->get_donor_id();
850 }
851
852 /* Clear out the cache */
853 wp_cache_delete( $donor_id, 'donors' );
854
855 if ( $donor_id ) {
856 charitable_get_table( 'donors' )->update( $donor_id, $donor_data );
857
858 return $donor_id;
859 }
860
861 return charitable_get_table( 'donors' )->insert( $donor_data );
862 }
863
864 /**
865 * Save core fields of the user (i.e. the wp_users data)
866 *
867 * @uses wp_insert_user
868 *
869 * @since 1.0.0
870 *
871 * @param array $submitted Values submitted by the user.
872 * @return int User ID
873 */
874 public function update_core_user( $submitted ) {
875 $core_fields = array_intersect( array_keys( $submitted ), $this->get_core_keys() );
876
877 if ( empty( $core_fields ) ) {
878 return 0;
879 }
880
881 $values = array();
882
883 /* If we're updating an active user, set the ID */
884 if ( 0 !== $this->ID ) {
885 $values['ID'] = $this->ID;
886 }
887
888 foreach ( $core_fields as $field ) {
889 $values[ $field ] = $submitted[ $field ];
890 }
891
892 /* Set the user's display name based on their name. */
893 $display_name = $this->sanitize_display_name( $values );
894
895 if ( $display_name ) {
896 $values['display_name'] = $display_name;
897 }
898
899 /* Insert the user */
900 if ( 0 == $this->ID ) {
901
902 if ( ! isset( $values['user_pass'] ) || strlen( $values['user_pass'] ) == 0 ) {
903 charitable_get_notices()->add_error( '<strong>ERROR:</strong> Password field is required.' );
904 return false;
905 }
906
907 if ( ! isset( $values['user_login'] ) ) {
908 $values['user_login'] = $values['user_email'];
909 }
910
911 /**
912 * `wp_insert_user` calls `sanitize_user` internally - make the
913 * same call here so `$values['user_login']` matches what is
914 * eventually saved to the database
915 */
916 $values['user_login'] = sanitize_user( $values['user_login'], true );
917
918 $user_id = wp_insert_user( $values );
919
920 if ( is_wp_error( $user_id ) ) {
921 charitable_get_notices()->add_errors_from_wp_error( $user_id );
922 return false;
923 }
924
925 $this->init( self::get_data_by( 'id', $user_id ) );
926
927 $signon = Charitable_User::signon( $values['user_login'], $values['user_pass'] );
928
929 if ( is_wp_error( $signon ) ) {
930 charitable_get_notices()->add_errors_from_wp_error( $signon );
931 return false;
932 }
933
934 /**
935 * Do something after a user has been registered.
936 *
937 * @since 1.0.0
938 *
939 * @param int $user_id The new user's ID.
940 * @param array $values Values submitted to register user.
941 */
942 do_action( 'charitable_after_insert_user', $user_id, $values );
943
944 } else {
945 $values['ID'] = $this->ID;
946 $user_id = wp_update_user( $values );
947 }//end if
948
949 /* If there was an error when inserting or updating the user, lodge the error. */
950 if ( is_wp_error( $user_id ) ) {
951 charitable_get_notices()->add_errors_from_wp_error( $user_id );
952 return false;
953 }
954
955 /**
956 * Do something after a user's account has been updated or created.
957 *
958 * @since 1.0.0
959 *
960 * @param int $user_id The user's ID.
961 * @param array $values Values submitted to save user.
962 */
963 do_action( 'charitable_after_save_user', $user_id, $values );
964
965 return $user_id;
966 }
967
968 /**
969 * Save the user's meta fields.
970 *
971 * @since 1.0.0
972 *
973 * @param array $submitted The submitted values.
974 * @param array $keys The keys of fields that are to be updated.
975 * @return int Number of fields updated.
976 */
977 public function update_user_meta( $submitted, $keys ) {
978 /* Exclude the core keys */
979 $meta_fields = array_diff( $keys, $this->get_core_keys() );
980 $updated = 0;
981
982 foreach ( $meta_fields as $field ) {
983
984 if ( isset( $submitted[ $field ] ) ) {
985 $meta_key = $this->map_key( $field );
986 $meta_value = sanitize_meta( $meta_key, $submitted[ $field ], 'user' );
987
988 update_user_meta( $this->ID, $meta_key, $meta_value );
989
990 $updated++;
991 }
992 }
993
994 return $updated;
995 }
996
997 /**
998 * Log a user is with their username and password.
999 *
1000 * @since 1.0.0
1001 *
1002 * @param string $username The user's username.
1003 * @param string $password User password.
1004 * @return WP_User|WP_Error|false WP_User on login, WP_Error on failure. False if feature is disabled.
1005 */
1006 public static function signon( $username, $password ) {
1007 if ( ! apply_filters( 'charitable_auto_login_after_registration', true, $username ) ) {
1008 return false;
1009 }
1010
1011 if ( is_user_logged_in() ) {
1012 return false;
1013 }
1014
1015 $creds = array(
1016 'user_login' => $username,
1017 'user_password' => $password,
1018 'remember' => true,
1019 );
1020
1021 return wp_signon( $creds, false );
1022 }
1023
1024 /**
1025 * Return a display name for the user.
1026 *
1027 * @since 1.5.2
1028 *
1029 * @param array $values Submitted values.
1030 * @return string|false String if we received a display_name
1031 * or had a first name or last name.
1032 */
1033 public function sanitize_display_name( $values ) {
1034 if ( array_key_exists( 'display_name', $values ) ) {
1035 return $values['display_name'];
1036 }
1037
1038 $first_name = array_key_exists( 'first_name', $values ) ? $values['first_name'] : $this->first_name;
1039 $last_name = array_key_exists( 'last_name', $values ) ? $values['last_name'] : $this->last_name;
1040 $display = trim( sprintf( '%s %s', $first_name, $last_name ) );
1041
1042 return strlen( $display ) ? $display : false;
1043 }
1044
1045 /**
1046 * Return the array of mapped keys, where the key is mapped to a meta_key in the user meta table.
1047 *
1048 * @since 1.0.0
1049 *
1050 * @return array
1051 */
1052 public function get_mapped_keys() {
1053 if ( ! isset( $this->mapped_keys ) ) {
1054 $this->mapped_keys = charitable_get_user_mapped_keys();
1055 }
1056
1057 return $this->mapped_keys;
1058 }
1059
1060 /**
1061 * Given a key, returns the mapped key or itself if it is not mapped.
1062 *
1063 * @since 1.5.0
1064 *
1065 * @param string $key The key to map.
1066 * @return string
1067 */
1068 public function map_key( $key ) {
1069 $mapped_keys = $this->get_mapped_keys();
1070
1071 return array_key_exists( $key, $mapped_keys ) ? $mapped_keys[ $key ] : $key;
1072 }
1073
1074 /**
1075 * Return the array of core keys.
1076 *
1077 * @since 1.0.0
1078 *
1079 * @return array
1080 */
1081 public function get_core_keys() {
1082 if ( ! isset( $this->core_keys ) ) {
1083 $this->core_keys = charitable_get_user_core_keys();
1084 }
1085
1086 return $this->core_keys;
1087 }
1088
1089 /**
1090 * Deprecated function.
1091 *
1092 * Return an array of fields used for the address.
1093 *
1094 * @deprecated 2.2.0
1095 *
1096 * @since 1.0.0
1097 * @since 1.6.44 Deprecated.
1098 *
1099 * @return array
1100 */
1101 public function get_address_fields() {
1102 charitable_get_deprecated()->deprecated_function(
1103 __METHOD__,
1104 '1.6.44',
1105 ''
1106 );
1107
1108 /**
1109 * Deprecated filter in deprecated method.
1110 *
1111 * Please note that prior to 1.6.44, the hook used here was `charitable_user_address_fields`,
1112 * which is used elsewhere in a different context and thus resulted in unexpected results.
1113 *
1114 * @since 1.6.44
1115 *
1116 * @param array $address_fields The address fields.
1117 */
1118 return apply_filters(
1119 'charitable_user_address_fields_deprecated',
1120 array(
1121 'donor_address',
1122 'donor_address_2',
1123 'donor_city',
1124 'donor_state',
1125 'donor_postcode',
1126 'donor_country',
1127 )
1128 );
1129 }
1130
1131 }
1132
1133 endif;
1134