PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.55
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.55
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 1.7.0.9 1.8.0 All 210 releases
charitable / includes / users / class-charitable-user.php

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

1,123 lines 28.2 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 Eric Daams
13 * @copyright Copyright (c) 2022, Studio 164a
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 ) {
527 /**If you use this filter, return an attachment ID. */
528 $avatar_attachment_id = apply_filters( 'charitable_user_avatar', false, $this );
529
530 /* If we don't have an attachment ID, display the gravatar. */
531 if ( ! $avatar_attachment_id ) {
532 return get_avatar( $this->ID, $size );
533 }
534
535 $img_size = apply_filters( 'charitable_user_avatar_media_size', array( $size, $size ), $size );
536
537 $attachment_src = wp_get_attachment_image_src( $avatar_attachment_id, $img_size );
538
539 /* No image for the given attachment ID? Fall back to the gravatar. */
540 if ( ! $attachment_src ) {
541 return get_avatar( $this->ID, $size );
542 }
543
544 return apply_filters(
545 'charitable_user_avatar_custom',
546 sprintf(
547 '<img src="%s" alt="%s" class="avatar photo" width="%s" height="%s" />',
548 $attachment_src[0],
549 esc_attr( $this->display_name ),
550 $attachment_src[1],
551 $attachment_src[2]
552 ),
553 $avatar_attachment_id,
554 $size,
555 $this
556 );
557 }
558
559 /**
560 * Return the src of the avatar.
561 *
562 * @since 1.0.0
563 *
564 * @param int $size The length and the width of the avatar.
565 * @return string
566 */
567 public function get_avatar_src( $size = 100 ) {
568 /* If this returns something, we don't need to deal with the gravatar. */
569 $avatar = apply_filters( 'charitable_user_avatar_src', false, $this, $size );
570
571 if ( false === $avatar ) {
572
573 /* The gravatars are returned as fully formatted img tags, so we need to pull out the src. */
574 $gravatar = get_avatar( $this->ID, $size );
575
576 preg_match( "@src='([^']+)'@", $gravatar, $matches );
577
578 $avatar = array_pop( $matches );
579 }
580
581 return $avatar;
582 }
583
584 /**
585 * Return the campaigns created by the user.
586 *
587 * @since 1.0.0
588 *
589 * @param array $args Optional. Any arguments accepted by WP_Query.
590 * @return WP_Query
591 */
592 public function get_campaigns( $args = array() ) {
593 $defaults = array(
594 'author' => $this->ID,
595 );
596
597 $args = wp_parse_args( $args, $defaults );
598
599 return Charitable_Campaigns::query( $args );
600 }
601
602 /**
603 * Checks whether the user has any current campaigns (i.e. non-expired).
604 *
605 * @since 1.0.0
606 *
607 * @param array $args Query arguments.
608 * @return WP_Query
609 */
610 public function get_current_campaigns( $args = array() ) {
611 $defaults = array(
612 'author' => $this->ID,
613 'meta_query' => array(
614 'relation' => 'OR',
615 array(
616 'key' => '_campaign_end_date',
617 'value' => date( 'Y-m-d H:i:s' ),
618 'compare' => '>=',
619 'type' => 'datetime',
620 ),
621 array(
622 'key' => '_campaign_end_date',
623 'value' => '0',
624 ),
625 ),
626 );
627
628 $args = wp_parse_args( $args, $defaults );
629
630 return Charitable_Campaigns::query( $args );
631 }
632
633 /**
634 * Returns all current campaigns by the user.
635 *
636 * @since 1.0.0
637 *
638 * @return WP_Query
639 */
640 public function has_current_campaigns() {
641 return $this->get_current_campaigns()->found_posts;
642 }
643
644 /**
645 * Returns the user's donation and campaign creation activity.
646 *
647 * @see WP_Query
648 * @since 1.0.0
649 *
650 * @param array $args Optional. Any arguments accepted by WP_Query.
651 * @return WP_Query
652 */
653 public function get_activity( $args = array() ) {
654 $defaults = array(
655 'author' => $this->ID,
656 'post_status' => array( 'charitable-completed', 'charitable-preapproved', 'publish' ),
657 'post_type' => array( 'donation', 'campaign' ),
658 'order' => 'DESC',
659 'orderby' => 'date',
660 );
661
662 $args = wp_parse_args( $args, $defaults );
663
664 $args = apply_filters( 'charitable_user_activity_args', $args, $this );
665
666 return new WP_Query( $args );
667 }
668
669 /**
670 * Add a new donor. This may also create a user account for them.
671 *
672 * @since 1.0.0
673 *
674 * @param array $submitted Values submitted by the user.
675 * @return int $donor_id Donor ID.
676 */
677 public function add_donor( $submitted = array() ) {
678 $email = false;
679
680 if ( isset( $submitted['email'] ) ) {
681 $email = $submitted['email'];
682 }
683
684 if ( ! $email && $this->is_logged_in() ) {
685 $email = $this->user_email;
686 }
687
688 /**
689 * Filter whether donors can be added without an email address.
690 *
691 * Prior to Charitable 1.6, this was never permitted. As of Charitable 1.6, donors
692 * without an email address can be added by default, primarily to support manual
693 * donations without an email address. Use this filter to disable that ability.
694 *
695 * NOTE: By default, the public donation form still requires an email address, so this
696 * primarily affects programmatically created donors, or donors created via manual
697 * donations in the admin.
698 *
699 * @see https://github.com/Charitable/Charitable/issues/535
700 *
701 * @since 1.6.0
702 *
703 * @param boolean $permitted Whether donors can be added without an email address.
704 */
705 if ( ! $email && ! charitable_permit_donor_without_email() ) {
706 charitable_get_deprecated()->doing_it_wrong(
707 __METHOD__,
708 __( 'Unable to add donor. Email not set for logged out user.', 'charitable' ),
709 '1.0.0'
710 );
711
712 return 0;
713 }
714
715 /**
716 * Filter the donor values.
717 *
718 * @since 1.0.0
719 *
720 * @param array $donor_values The donor values.
721 * @param Charitable_User $user This instance of `Charitable_User`.
722 * @param array $submitted Submitted values.
723 */
724 $donor_values = apply_filters(
725 'charitable_donor_values',
726 array(
727 'user_id' => $this->ID,
728 'email' => $email,
729 'first_name' => array_key_exists( 'first_name', $submitted ) ? $submitted['first_name'] : $this->first_name,
730 'last_name' => array_key_exists( 'last_name', $submitted ) ? $submitted['last_name'] : $this->last_name,
731 ),
732 $this,
733 $submitted
734 );
735
736 $donor_id = charitable_get_table( 'donors' )->insert( $donor_values );
737
738 /**
739 * If the user is logged in, add the "Donor" role to their account.
740 */
741 if ( $this->is_logged_in() ) {
742 $this->add_role( 'donor' );
743 }
744
745 do_action( 'charitable_after_insert_donor', $donor_id, $this );
746
747 return $donor_id;
748 }
749
750 /**
751 * Insert a new donor with submitted values.
752 *
753 * @since 1.4.0
754 *
755 * @param array $submitted The submitted values.
756 * @param array $keys The keys of fields that are to be updated.
757 * @return int
758 */
759 public static function create_profile( $submitted = array(), $keys = array() ) {
760 $user = new Charitable_User();
761 $user_id = $user->update_profile( $submitted, $keys );
762 return new Charitable_User( $user_id );
763 }
764
765 /**
766 * Update the user's details with submitted values.
767 *
768 * @since 1.0.0
769 *
770 * @param array $submitted The submitted values.
771 * @param array $keys The keys of fields that are to be updated.
772 * @return int
773 */
774 public function update_profile( $submitted = array(), $keys = array() ) {
775 if ( empty( $submitted ) ) {
776 $submitted = $_POST;
777 }
778
779 if ( empty( $keys ) ) {
780 $keys = array_keys( $submitted );
781 }
782
783 $user_id = $this->update_core_user( $submitted );
784
785 /* If there were problems with creating the user, stop here. */
786 if ( ! $user_id ) {
787 return $user_id;
788 }
789
790 /* Create or update the user's donor record. */
791 $this->update_donor_record( $submitted, $keys );
792
793 $this->update_user_meta( $submitted, $keys );
794
795 return $user_id;
796 }
797
798 /**
799 * Create or update a donor record for the current user.
800 *
801 * @since 1.6.2
802 *
803 * @param array $submitted The submitted values.
804 * @param array $keys The keys of fields that are to be updated.
805 * @return int The donor ID, or 0 if none was created or edited.
806 */
807 public function update_donor_record( $submitted, $keys ) {
808 $donor_fields = array_intersect( $keys, charitable_get_donor_keys() );
809
810 if ( array_key_exists( 'user_email', $submitted ) ) {
811 $donor_fields[] = 'email';
812 $submitted['email'] = $submitted['user_email'];
813 }
814
815 if ( empty( $donor_fields ) ) {
816 return 0;
817 }
818
819 $donor_data = array(
820 'user_id' => $this->ID,
821 );
822
823 foreach ( $donor_fields as $field ) {
824 if ( array_key_exists( $field, $submitted ) ) {
825 $value = $submitted[ $field ];
826 } elseif ( 'contact_consent' == $field ) {
827 $value = 0;
828 } else {
829 $value = '';
830 }
831
832 $donor_data[ $field ] = $value;
833 }
834
835 if ( array_key_exists( 'donor_id', $donor_data ) ) {
836 $donor_id = $donor_data['donor_id'];
837 } else {
838 $donor_id = $this->get_donor_id();
839 }
840
841 /* Clear out the cache */
842 wp_cache_delete( $donor_id, 'donors' );
843
844 if ( $donor_id ) {
845 charitable_get_table( 'donors' )->update( $donor_id, $donor_data );
846
847 return $donor_id;
848 }
849
850 return charitable_get_table( 'donors' )->insert( $donor_data );
851 }
852
853 /**
854 * Save core fields of the user (i.e. the wp_users data)
855 *
856 * @uses wp_insert_user
857 *
858 * @since 1.0.0
859 *
860 * @param array $submitted Values submitted by the user.
861 * @return int User ID
862 */
863 public function update_core_user( $submitted ) {
864 $core_fields = array_intersect( array_keys( $submitted ), $this->get_core_keys() );
865
866 if ( empty( $core_fields ) ) {
867 return 0;
868 }
869
870 $values = array();
871
872 /* If we're updating an active user, set the ID */
873 if ( 0 !== $this->ID ) {
874 $values['ID'] = $this->ID;
875 }
876
877 foreach ( $core_fields as $field ) {
878 $values[ $field ] = $submitted[ $field ];
879 }
880
881 /* Set the user's display name based on their name. */
882 $display_name = $this->sanitize_display_name( $values );
883
884 if ( $display_name ) {
885 $values['display_name'] = $display_name;
886 }
887
888 /* Insert the user */
889 if ( 0 == $this->ID ) {
890
891 if ( ! isset( $values['user_pass'] ) || strlen( $values['user_pass'] ) == 0 ) {
892 charitable_get_notices()->add_error( '<strong>ERROR:</strong> Password field is required.' );
893 return false;
894 }
895
896 if ( ! isset( $values['user_login'] ) ) {
897 $values['user_login'] = $values['user_email'];
898 }
899
900 /**
901 * `wp_insert_user` calls `sanitize_user` internally - make the
902 * same call here so `$values['user_login']` matches what is
903 * eventually saved to the database
904 */
905 $values['user_login'] = sanitize_user( $values['user_login'], true );
906
907 $user_id = wp_insert_user( $values );
908
909 if ( is_wp_error( $user_id ) ) {
910 charitable_get_notices()->add_errors_from_wp_error( $user_id );
911 return false;
912 }
913
914 $this->init( self::get_data_by( 'id', $user_id ) );
915
916 $signon = Charitable_User::signon( $values['user_login'], $values['user_pass'] );
917
918 if ( is_wp_error( $signon ) ) {
919 charitable_get_notices()->add_errors_from_wp_error( $signon );
920 return false;
921 }
922
923 /**
924 * Do something after a user has been registered.
925 *
926 * @since 1.0.0
927 *
928 * @param int $user_id The new user's ID.
929 * @param array $values Values submitted to register user.
930 */
931 do_action( 'charitable_after_insert_user', $user_id, $values );
932
933 } else {
934 $values['ID'] = $this->ID;
935 $user_id = wp_update_user( $values );
936 }//end if
937
938 /* If there was an error when inserting or updating the user, lodge the error. */
939 if ( is_wp_error( $user_id ) ) {
940 charitable_get_notices()->add_errors_from_wp_error( $user_id );
941 return false;
942 }
943
944 /**
945 * Do something after a user's account has been updated or created.
946 *
947 * @since 1.0.0
948 *
949 * @param int $user_id The user's ID.
950 * @param array $values Values submitted to save user.
951 */
952 do_action( 'charitable_after_save_user', $user_id, $values );
953
954 return $user_id;
955 }
956
957 /**
958 * Save the user's meta fields.
959 *
960 * @since 1.0.0
961 *
962 * @param array $submitted The submitted values.
963 * @param array $keys The keys of fields that are to be updated.
964 * @return int Number of fields updated.
965 */
966 public function update_user_meta( $submitted, $keys ) {
967 /* Exclude the core keys */
968 $meta_fields = array_diff( $keys, $this->get_core_keys() );
969 $updated = 0;
970
971 foreach ( $meta_fields as $field ) {
972
973 if ( isset( $submitted[ $field ] ) ) {
974 $meta_key = $this->map_key( $field );
975 $meta_value = sanitize_meta( $meta_key, $submitted[ $field ], 'user' );
976
977 update_user_meta( $this->ID, $meta_key, $meta_value );
978
979 $updated++;
980 }
981 }
982
983 return $updated;
984 }
985
986 /**
987 * Log a user is with their username and password.
988 *
989 * @since 1.0.0
990 *
991 * @param string $username The user's username.
992 * @param string $password User password.
993 * @return WP_User|WP_Error|false WP_User on login, WP_Error on failure. False if feature is disabled.
994 */
995 public static function signon( $username, $password ) {
996 if ( ! apply_filters( 'charitable_auto_login_after_registration', true, $username ) ) {
997 return false;
998 }
999
1000 if ( is_user_logged_in() ) {
1001 return false;
1002 }
1003
1004 $creds = array(
1005 'user_login' => $username,
1006 'user_password' => $password,
1007 'remember' => true,
1008 );
1009
1010 return wp_signon( $creds, false );
1011 }
1012
1013 /**
1014 * Return a display name for the user.
1015 *
1016 * @since 1.5.2
1017 *
1018 * @param array $values Submitted values.
1019 * @return string|false String if we received a display_name
1020 * or had a first name or last name.
1021 */
1022 public function sanitize_display_name( $values ) {
1023 if ( array_key_exists( 'display_name', $values ) ) {
1024 return $values['display_name'];
1025 }
1026
1027 $first_name = array_key_exists( 'first_name', $values ) ? $values['first_name'] : $this->first_name;
1028 $last_name = array_key_exists( 'last_name', $values ) ? $values['last_name'] : $this->last_name;
1029 $display = trim( sprintf( '%s %s', $first_name, $last_name ) );
1030
1031 return strlen( $display ) ? $display : false;
1032 }
1033
1034 /**
1035 * Return the array of mapped keys, where the key is mapped to a meta_key in the user meta table.
1036 *
1037 * @since 1.0.0
1038 *
1039 * @return array
1040 */
1041 public function get_mapped_keys() {
1042 if ( ! isset( $this->mapped_keys ) ) {
1043 $this->mapped_keys = charitable_get_user_mapped_keys();
1044 }
1045
1046 return $this->mapped_keys;
1047 }
1048
1049 /**
1050 * Given a key, returns the mapped key or itself if it is not mapped.
1051 *
1052 * @since 1.5.0
1053 *
1054 * @param string $key The key to map.
1055 * @return string
1056 */
1057 public function map_key( $key ) {
1058 $mapped_keys = $this->get_mapped_keys();
1059
1060 return array_key_exists( $key, $mapped_keys ) ? $mapped_keys[ $key ] : $key;
1061 }
1062
1063 /**
1064 * Return the array of core keys.
1065 *
1066 * @since 1.0.0
1067 *
1068 * @return array
1069 */
1070 public function get_core_keys() {
1071 if ( ! isset( $this->core_keys ) ) {
1072 $this->core_keys = charitable_get_user_core_keys();
1073 }
1074
1075 return $this->core_keys;
1076 }
1077
1078 /**
1079 * Deprecated function.
1080 *
1081 * Return an array of fields used for the address.
1082 *
1083 * @deprecated 2.2.0
1084 *
1085 * @since 1.0.0
1086 * @since 1.6.44 Deprecated.
1087 *
1088 * @return array
1089 */
1090 public function get_address_fields() {
1091 charitable_get_deprecated()->deprecated_function(
1092 __METHOD__,
1093 '1.6.44',
1094 ''
1095 );
1096
1097 /**
1098 * Deprecated filter in deprecated method.
1099 *
1100 * Please note that prior to 1.6.44, the hook used here was `charitable_user_address_fields`,
1101 * which is used elsewhere in a different context and thus resulted in unexpected results.
1102 *
1103 * @since 1.6.44
1104 *
1105 * @param array $address_fields The address fields.
1106 */
1107 return apply_filters(
1108 'charitable_user_address_fields_deprecated',
1109 array(
1110 'donor_address',
1111 'donor_address_2',
1112 'donor_city',
1113 'donor_state',
1114 'donor_postcode',
1115 'donor_country',
1116 )
1117 );
1118 }
1119
1120 }
1121
1122 endif;
1123