PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / DonorDashboards / Profile.php

Profile.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/DonorDashboards/Profile.php

205 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\DonorDashboards;
4
5 use Exception;
6 use Give\DonorDashboards\Factories\DonorFactory;
7 use Give\DonorDashboards\Helpers as DonorDashboardHelpers;
8 use Give\DonorDashboards\Pipeline\DonorProfilePipeline;
9 use Give\DonorDashboards\Pipeline\Stages\UpdateDonorAddresses;
10 use Give\DonorDashboards\Pipeline\Stages\UpdateDonorAnonymousGiving;
11 use Give\DonorDashboards\Pipeline\Stages\UpdateDonorAvatar;
12 use Give\DonorDashboards\Pipeline\Stages\UpdateDonorCompany;
13 use Give\Donors\Models\Donor;
14 use Give\Framework\Support\Facades\DateTime\Temporal;
15
16 /**
17 * @since 2.10.0
18 */
19 class Profile
20 {
21
22 protected $donor;
23 protected $id;
24
25 public function __construct()
26 {
27 $donorId = DonorDashboardHelpers::getCurrentDonorId();
28 if ($donorId) {
29 $donorFactory = new DonorFactory;
30 $this->donor = $donorFactory->make($donorId);
31 }
32 }
33
34 /**
35 * Handles updating relevant profile fields in donor database and meta database
36 *
37 * @since 4.17.0 Logged-in donors can no longer add unverified email addresses.
38 * @since 2.27.3 Use Donor model to update data used by webhooks addon to prevent multiple events creation
39 * @since 2.10.0
40 *
41 * @param object $data Object representing profile data to update
42 *
43 * @return array
44 *
45 * @throws Exception
46 */
47 public function update($data)
48 {
49 $donor = Donor::find($this->donor->id);
50
51 $allowed = array_merge($donor->additionalEmails ?? [], [$donor->email]);
52 $donor->additionalEmails = array_values(array_intersect($data['additionalEmails'] ?: [], $allowed));
53
54 $donor->email = $data['primaryEmail'];
55
56 if ( ! empty($data['firstName']) && ! empty($data['lastName'])) {
57 $firstName = $data['firstName'];
58 $lastName = $data['lastName'];
59 $donor->name = "{$firstName} {$lastName}";
60 $donor->firstName = $firstName;
61 $donor->lastName = $lastName;
62 }
63
64 $donor->save();
65
66 $pipeline = (new DonorProfilePipeline)
67 ->pipe(new UpdateDonorCompany)
68 ->pipe(new UpdateDonorAvatar)
69 ->pipe(new UpdateDonorAddresses)
70 ->pipe(new UpdateDonorAnonymousGiving);
71
72 $pipeline->process(
73 [
74 'data' => $data,
75 'donor' => $this->donor,
76 ]
77 );
78
79 // Return updated donor profile data
80 return $this->getProfileData();
81 }
82
83 /**
84 * Return array of donor profile data
85 *
86 * @since 3.20.0 Replace strtotime() with Temporal::getDateTimestamp() to prevent the use of dates with wrong timezones
87 * @since 2.10.0
88 *
89 * @return array
90 */
91 public function getProfileData()
92 {
93 if ( ! $this->donor) {
94 return null;
95 }
96
97 $titlePrefix = Give()->donor_meta->get_meta($this->donor->id, '_give_donor_title_prefix', true);
98
99 return [
100 'name' => give_get_donor_name_with_title_prefixes($titlePrefix, $this->donor->name),
101 'firstName' => $this->donor->get_first_name(),
102 'lastName' => $this->donor->get_last_name(),
103 'emails' => $this->donor->emails,
104 'sinceLastDonation' => ! empty($this->donor->get_last_donation_date()) ? human_time_diff(
105 Temporal::getDateTimestamp($this->donor->get_last_donation_date())
106 ) : '',
107 'avatarUrl' => $this->getAvatarUrl(),
108 'avatarId' => $this->getAvatarId(),
109 'sinceCreated' => human_time_diff(Temporal::getDateTimestamp($this->donor->date_created)),
110 'company' => $this->donor->get_company_name(),
111 'initials' => $this->donor->get_donor_initals(),
112 'titlePrefix' => $this->getTitlePrefix(),
113 'addresses' => $this->donor->address,
114 'isAnonymous' => $this->donor->get_meta('_give_anonymous_donor', true) !== '0' ? '1' : '0',
115 ];
116 }
117
118 /**
119 * Returns profile model's donor id
120 *
121 * @since 2.10.0
122 * @return int
123 *
124 */
125 public function getId()
126 {
127 if ( ! $this->donor) {
128 return null;
129 }
130
131 return $this->donor->id;
132 }
133
134 /**
135 * Returns donor's title prefix
136 * *
137 * @since 2.10.0
138 * @return string
139 *
140 */
141 public function getTitlePrefix()
142 {
143 return Give()->donor_meta->get_meta($this->donor->id, '_give_donor_title_prefix', true);
144 }
145
146 /**
147 * Returns profile's avatar URL
148 * *
149 * @since 2.10.0
150 * @return string
151 *
152 */
153 public function getAvatarUrl()
154 {
155 $avatarId = $this->getAvatarId();
156 if ($avatarId) {
157 return wp_get_attachment_url($avatarId);
158 } else {
159 return give_validate_gravatar($this->donor->email) ? get_avatar_url($this->donor->email, ['size' => 140]
160 ) : null;
161 }
162 }
163
164 /**
165 * Returns profile's avatar media ID
166 * *
167 * @since 2.10.0
168 * @return int
169 *
170 */
171 public function getAvatarId()
172 {
173 return $this->donor->get_meta('_give_donor_avatar_id');
174 }
175
176 /**
177 * Returns profile's stored country, or global default if none is set
178 * *
179 * @since 2.10.0
180 * @return string
181 *
182 */
183 public function getCountry()
184 {
185 if ( ! $this->donor) {
186 return give_get_country();
187 }
188
189 $address = $this->donor->get_donor_address();
190 if ($address) {
191 return $address['country'];
192 } else {
193 return give_get_country();
194 }
195 }
196
197 /**
198 * @since 3.14.2
199 */
200 public function avatarBelongsToCurrentUser(?int $avatarId = null): bool
201 {
202 return (int)get_post_field("post_author", $avatarId ?? $this->getAvatarId()) === get_current_user_id();
203 }
204 }
205