PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.14
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.14
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.14, at includes/users/class-charitable-user.php

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