| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Campaigns\Blocks\CampaignDonors; |
| 4 |
|
| 5 |
use Give\Campaigns\Models\Campaign; |
| 6 |
use Give\Framework\Support\ValueObjects\Money; |
| 7 |
use Give\Framework\Views\View; |
| 8 |
|
| 9 |
/** |
| 10 |
* @since 4.0.0 |
| 11 |
*/ |
| 12 |
class CampaignDonorsBlockViewModel |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var Campaign $campaign |
| 16 |
*/ |
| 17 |
private $campaign; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private $donors; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var array $attributes |
| 26 |
*/ |
| 27 |
private $attributes; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 4.0.0 |
| 31 |
*/ |
| 32 |
public function __construct(Campaign $campaign, array $donors, array $attributes) |
| 33 |
{ |
| 34 |
$this->attributes = $attributes; |
| 35 |
$this->campaign = $campaign; |
| 36 |
$this->donors = $donors; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @since 4.0.0 |
| 41 |
*/ |
| 42 |
public function render(): void |
| 43 |
{ |
| 44 |
View::render('Campaigns/Blocks/CampaignDonors.render', [ |
| 45 |
'campaign' => $this->campaign, |
| 46 |
'donors' => $this->formatDonorsData($this->donors), |
| 47 |
'attributes' => $this->attributes, |
| 48 |
]); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* @since 4.16.9 Added additional sanitization to donor data. |
| 54 |
* @since 4.14.0 add avatar URL to donors data |
| 55 |
* @since 4.0.0 |
| 56 |
*/ |
| 57 |
private function formatDonorsData(array $donors): array |
| 58 |
{ |
| 59 |
return array_map(static function ($entry) { |
| 60 |
$entry->name = give_strip_shortcodes_deep($entry->name ?? ''); |
| 61 |
$entry->company = give_strip_shortcodes_deep($entry->company ?? ''); |
| 62 |
|
| 63 |
if (isset($entry->date)) { |
| 64 |
$entry->date = human_time_diff(strtotime($entry->date)); |
| 65 |
} |
| 66 |
$entry->amount = Money::fromDecimal($entry->amount, give_get_currency()); |
| 67 |
if ($entry->isAnonymous) { |
| 68 |
$entry->avatarUrl = get_avatar_url(0, ['size' => 80]); |
| 69 |
} else { |
| 70 |
$entry->avatarUrl = (int) $entry->avatarId > 0 |
| 71 |
? wp_get_attachment_image_url($entry->avatarId, ['width' => '80', 'height' => '80']) |
| 72 |
: get_avatar_url($entry->email, ['size' => 80]); |
| 73 |
} |
| 74 |
|
| 75 |
return $entry; |
| 76 |
}, $donors); |
| 77 |
} |
| 78 |
} |
| 79 |
|