| 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 2.27.3 Use Donor model to update data used by webhooks addon to prevent multiple events creation |
| 38 |
* @since 2.10.0 |
| 39 |
* |
| 40 |
* @param object $data Object representing profile data to update |
| 41 |
* |
| 42 |
* @return array |
| 43 |
* |
| 44 |
* @throws Exception |
| 45 |
*/ |
| 46 |
public function update($data) |
| 47 |
{ |
| 48 |
$donor = Donor::find($this->donor->id); |
| 49 |
|
| 50 |
$donor->email = $data['primaryEmail']; |
| 51 |
$donor->additionalEmails = $data['additionalEmails'] ?: []; |
| 52 |
|
| 53 |
if ( ! empty($data['firstName']) && ! empty($data['lastName'])) { |
| 54 |
$firstName = $data['firstName']; |
| 55 |
$lastName = $data['lastName']; |
| 56 |
$donor->name = "{$firstName} {$lastName}"; |
| 57 |
$donor->firstName = $firstName; |
| 58 |
$donor->lastName = $lastName; |
| 59 |
} |
| 60 |
|
| 61 |
$donor->save(); |
| 62 |
|
| 63 |
$pipeline = (new DonorProfilePipeline) |
| 64 |
->pipe(new UpdateDonorCompany) |
| 65 |
->pipe(new UpdateDonorAvatar) |
| 66 |
->pipe(new UpdateDonorAddresses) |
| 67 |
->pipe(new UpdateDonorAnonymousGiving); |
| 68 |
|
| 69 |
$pipeline->process( |
| 70 |
[ |
| 71 |
'data' => $data, |
| 72 |
'donor' => $this->donor, |
| 73 |
] |
| 74 |
); |
| 75 |
|
| 76 |
// Return updated donor profile data |
| 77 |
return $this->getProfileData(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Return array of donor profile data |
| 82 |
* |
| 83 |
* @since 3.20.0 Replace strtotime() with Temporal::getDateTimestamp() to prevent the use of dates with wrong timezones |
| 84 |
* @since 2.10.0 |
| 85 |
* |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function getProfileData() |
| 89 |
{ |
| 90 |
if ( ! $this->donor) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
$titlePrefix = Give()->donor_meta->get_meta($this->donor->id, '_give_donor_title_prefix', true); |
| 95 |
|
| 96 |
return [ |
| 97 |
'name' => give_get_donor_name_with_title_prefixes($titlePrefix, $this->donor->name), |
| 98 |
'firstName' => $this->donor->get_first_name(), |
| 99 |
'lastName' => $this->donor->get_last_name(), |
| 100 |
'emails' => $this->donor->emails, |
| 101 |
'sinceLastDonation' => ! empty($this->donor->get_last_donation_date()) ? human_time_diff( |
| 102 |
Temporal::getDateTimestamp($this->donor->get_last_donation_date()) |
| 103 |
) : '', |
| 104 |
'avatarUrl' => $this->getAvatarUrl(), |
| 105 |
'avatarId' => $this->getAvatarId(), |
| 106 |
'sinceCreated' => human_time_diff(Temporal::getDateTimestamp($this->donor->date_created)), |
| 107 |
'company' => $this->donor->get_company_name(), |
| 108 |
'initials' => $this->donor->get_donor_initals(), |
| 109 |
'titlePrefix' => $this->getTitlePrefix(), |
| 110 |
'addresses' => $this->donor->address, |
| 111 |
'isAnonymous' => $this->donor->get_meta('_give_anonymous_donor', true) !== '0' ? '1' : '0', |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns profile model's donor id |
| 117 |
* |
| 118 |
* @since 2.10.0 |
| 119 |
* @return int |
| 120 |
* |
| 121 |
*/ |
| 122 |
public function getId() |
| 123 |
{ |
| 124 |
if ( ! $this->donor) { |
| 125 |
return null; |
| 126 |
} |
| 127 |
|
| 128 |
return $this->donor->id; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns donor's title prefix |
| 133 |
* * |
| 134 |
* @since 2.10.0 |
| 135 |
* @return string |
| 136 |
* |
| 137 |
*/ |
| 138 |
public function getTitlePrefix() |
| 139 |
{ |
| 140 |
return Give()->donor_meta->get_meta($this->donor->id, '_give_donor_title_prefix', true); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns profile's avatar URL |
| 145 |
* * |
| 146 |
* @since 2.10.0 |
| 147 |
* @return string |
| 148 |
* |
| 149 |
*/ |
| 150 |
public function getAvatarUrl() |
| 151 |
{ |
| 152 |
$avatarId = $this->getAvatarId(); |
| 153 |
if ($avatarId) { |
| 154 |
return wp_get_attachment_url($avatarId); |
| 155 |
} else { |
| 156 |
return give_validate_gravatar($this->donor->email) ? get_avatar_url($this->donor->email, ['size' => 140] |
| 157 |
) : null; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Returns profile's avatar media ID |
| 163 |
* * |
| 164 |
* @since 2.10.0 |
| 165 |
* @return int |
| 166 |
* |
| 167 |
*/ |
| 168 |
public function getAvatarId() |
| 169 |
{ |
| 170 |
return $this->donor->get_meta('_give_donor_avatar_id'); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Returns profile's stored country, or global default if none is set |
| 175 |
* * |
| 176 |
* @since 2.10.0 |
| 177 |
* @return string |
| 178 |
* |
| 179 |
*/ |
| 180 |
public function getCountry() |
| 181 |
{ |
| 182 |
if ( ! $this->donor) { |
| 183 |
return give_get_country(); |
| 184 |
} |
| 185 |
|
| 186 |
$address = $this->donor->get_donor_address(); |
| 187 |
if ($address) { |
| 188 |
return $address['country']; |
| 189 |
} else { |
| 190 |
return give_get_country(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* @since 3.14.2 |
| 196 |
*/ |
| 197 |
public function avatarBelongsToCurrentUser(?int $avatarId = null): bool |
| 198 |
{ |
| 199 |
return (int)get_post_field("post_author", $avatarId ?? $this->getAvatarId()) === get_current_user_id(); |
| 200 |
} |
| 201 |
} |
| 202 |
|