PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
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 2.3.2 2.30.0 All 255 releases
give / src / API / REST / V3 / Routes / Campaigns / ViewModels / CampaignViewModel.php

CampaignViewModel.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/API/REST/V3/Routes/Campaigns/ViewModels/CampaignViewModel.php

116 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\API\REST\V3\Routes\Campaigns\ViewModels;
4
5 use Give\Campaigns\Models\Campaign;
6 use Give\Campaigns\Repositories\CampaignsDataRepository;
7 use Give\Campaigns\ValueObjects\CampaignPageMetaKeys;
8 use Give\Framework\Database\DB;
9 use Give\Framework\Support\Facades\DateTime\Temporal;
10
11 /**
12 * @since 4.0.0
13 */
14 class CampaignViewModel
15 {
16 /**
17 * @var Campaign
18 */
19 private $campaign;
20
21 /**
22 * @var CampaignsDataRepository|null
23 */
24 private $data;
25
26 /**
27 * @since 4.0.0
28 */
29 public function __construct(Campaign $campaign)
30 {
31 $this->campaign = $campaign;
32 }
33
34 /**
35 * Set data source
36 *
37 * @param CampaignsDataRepository $data
38 *
39 * @return CampaignViewModel
40 */
41 public function setData(CampaignsDataRepository $data): CampaignViewModel
42 {
43 $this->data = $data;
44
45 return $this;
46 }
47
48 /**
49 * @since 4.0.0
50 */
51 public function exports(): array
52 {
53 $pagePermalink = $this->getPagePermalink();
54
55 return [
56 'id' => $this->campaign->id,
57 'pageId' => $pagePermalink ? (int)$this->campaign->pageId : null,
58 'pagePermalink' => $pagePermalink,
59 'defaultFormId' => $this->campaign->defaultFormId,
60 'defaultFormTitle' => $this->campaign->defaultForm()->title,
61 'type' => $this->campaign->type->getValue(),
62 'title' => $this->campaign->title,
63 'shortDescription' => $this->campaign->shortDescription,
64 'longDescription' => $this->campaign->longDescription,
65 'logo' => $this->campaign->logo,
66 'image' => $this->campaign->image,
67 'primaryColor' => $this->campaign->primaryColor,
68 'secondaryColor' => $this->campaign->secondaryColor,
69 'goal' => $this->campaign->goal,
70 'goalType' => $this->campaign->goalType->getValue(),
71 'goalStats' => is_null($this->data)
72 ? $this->campaign->getGoalStats()
73 : $this->data->getGoalData($this->campaign),
74 'status' => $this->campaign->status->getValue(),
75 'startDate' => Temporal::getFormattedDateTimeUsingTimeZoneAndFormatSettings($this->campaign->startDate),
76 'endDate' => $this->campaign->endDate
77 ? Temporal::getFormattedDateTimeUsingTimeZoneAndFormatSettings($this->campaign->endDate)
78 : null,
79 'createdAt' => Temporal::getFormattedDateTimeUsingTimeZoneAndFormatSettings($this->campaign->createdAt),
80 ];
81 }
82
83 /**
84 * @since 4.0.0
85 */
86 protected function getPagePermalink(): ?string
87 {
88 $page = get_post($this->campaign->pageId);
89 if (!$page){
90 return null;
91 }
92
93 if ($page->post_status === 'trash') {
94 return null;
95 }
96
97 $permalink = get_permalink($this->campaign->pageId ?? 0);
98
99 if ($permalink){
100 return $permalink;
101 }
102
103 $query = DB::table('postmeta')
104 ->select('post_id')
105 ->where('meta_key', CampaignPageMetaKeys::CAMPAIGN_ID)
106 ->where('meta_value', $this->campaign->id)
107 ->get();
108
109 if (!$query) {
110 return null;
111 }
112
113 return get_permalink($query->post_id);
114 }
115 }
116