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 / DonationForms / ViewModels / DonationConfirmationReceiptViewModel.php

DonationConfirmationReceiptViewModel.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/DonationForms/ViewModels/DonationConfirmationReceiptViewModel.php

276 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\DonationForms\ViewModels;
4
5 use Give\Campaigns\Models\Campaign;
6 use Give\DonationForms\FormDesigns\ClassicFormDesign\ClassicFormDesign;
7 use Give\DonationForms\Models\DonationForm;
8 use Give\DonationForms\Repositories\DonationFormRepository;
9 use Give\Donations\Models\Donation;
10 use Give\Framework\DesignSystem\Actions\RegisterDesignSystemStyles;
11 use Give\Framework\FormDesigns\Registrars\FormDesignRegistrar;
12 use Give\Framework\Receipts\DonationReceipt;
13 use Give\Framework\Support\Scripts\Concerns\HasScriptAssetFile;
14 use Give\Helpers\Language;
15
16 /**
17 * @since 3.0.0
18 */
19 class DonationConfirmationReceiptViewModel
20 {
21 use HasScriptAssetFile;
22
23 /**
24 * @var Donation
25 */
26 public $donation;
27
28 /**
29 * @since 3.0.0
30 */
31 public function __construct(Donation $donation)
32 {
33 $this->donation = $donation;
34 }
35
36 /**
37 * @since 3.0.0
38 */
39 public function getDonationForm(): DonationForm
40 {
41 return DonationForm::find($this->donation->formId);
42 }
43
44 /**
45 * @since 3.0.0
46 */
47 public function getReceipt(): DonationReceipt
48 {
49 return $this->donation->receipt();
50 }
51
52 /**
53 * @since 3.0.0
54 */
55 public function exports(): array
56 {
57 return [
58 'receipt' => $this->getReceipt()->toArray()
59 ];
60 }
61
62 /**
63 * @since 3.0.0
64 */
65 public function formExports(): array
66 {
67 /** @var DonationFormRepository $donationFormRepository */
68 $donationFormRepository = give(DonationFormRepository::class);
69
70 $formDataGateways = $donationFormRepository->getFormDataGateways($this->donation->formId);
71 $formApi = !$donationFormRepository->isLegacyForm(
72 $this->donation->formId
73 ) ? $donationFormRepository->getFormSchemaFromBlocks(
74 $this->donation->formId,
75 $this->getDonationForm()->blocks
76 )->jsonSerialize() : null;
77
78 return [
79 'registeredGateways' => $formDataGateways,
80 'form' => $formApi,
81 ];
82 }
83
84 /**
85 * @since 4.1.0 add campaign colors
86 * @since 3.11.0 Sanitize customCSS property
87 * @since 3.0.0
88 */
89 public function render(): string
90 {
91 /** @var DonationFormRepository $donationFormRepository */
92 $donationFormRepository = give(DonationFormRepository::class);
93
94 $donationForm = !$donationFormRepository->isLegacyForm(
95 $this->donation->formId
96 ) ? $this->getDonationForm() : null;
97
98 $formDesignId = $donationForm ? $donationForm->settings->designId : ClassicFormDesign::id();
99 $customCss = $donationForm && $donationForm->settings->customCss ? $donationForm->settings->customCss : null;
100 $primaryColor = $donationForm ? $donationForm->settings->primaryColor : '#69B868';
101 $secondaryColor = $donationForm ? $donationForm->settings->secondaryColor : '#000000';
102
103 if ($donationForm && $donationForm->settings->inheritCampaignColors){
104 $campaignColors = $this->getCampaignColors($donationForm->id);
105
106 if (isset($campaignColors['primaryColor'])) {
107 $primaryColor = $campaignColors['primaryColor'];
108 }
109
110 if (isset($campaignColors['secondaryColor'])) {
111 $secondaryColor = $campaignColors['secondaryColor'];
112 }
113 }
114
115 $this->enqueueGlobalStyles($primaryColor, $secondaryColor);
116
117 $this->enqueueFormScripts($formDesignId);
118
119 ob_start();
120 wp_print_styles();
121 wp_print_head_scripts();
122 ?>
123
124 <?php
125 if ($customCss): ?>
126 <style><?php echo wp_strip_all_tags($customCss); ?></style>
127 <?php
128 endif; ?>
129
130 <div data-theme="light" id="root-givewp-donation-confirmation-receipt"
131 data-iframe-height
132 class="givewp-donation-confirmation-receipt"
133 style="
134 --givewp-primary-color:<?= $primaryColor ?>;
135 --givewp-secondary-color:<?= $secondaryColor ?>;
136 "
137 ></div>
138
139 <?php
140 wp_print_footer_scripts();
141
142 echo ob_get_clean();
143
144 exit();
145 }
146
147 /**
148 * @since 3.0.0
149 */
150 public function enqueueGlobalStyles(string $primaryColor, string $secondaryColor)
151 {
152 (new RegisterDesignSystemStyles())();
153 wp_enqueue_style('givewp-design-system-foundation');
154
155 wp_register_style(
156 'givewp-base-form-styles',
157 GIVE_PLUGIN_URL . 'build/baseFormDesignCss.css'
158 );
159
160 wp_add_inline_style(
161 'givewp-base-form-styles',
162 ":root {
163 --givewp-primary-color:{$primaryColor};
164 --givewp-secondary-color:{$secondaryColor};
165 }"
166 );
167
168 wp_enqueue_style('givewp-base-form-styles');
169 }
170
171 /**
172 * Loads scripts in order: [Registrars, Designs, App]
173 *
174 * @since 3.0.0
175 *
176 * @return void
177 */
178 private function enqueueFormScripts(?string $formDesignId)
179 {
180 $handle = 'givewp-donation-form-registrars';
181 wp_enqueue_script(
182 $handle,
183 GIVE_PLUGIN_URL . 'build/donationFormRegistrars.js',
184 $this->getScriptAssetDependencies(GIVE_PLUGIN_DIR . 'build/donationFormRegistrars.asset.php'),
185 GIVE_VERSION,
186 true
187 );
188
189 Language::setScriptTranslations($handle);
190
191 wp_add_inline_script(
192 'givewp-donation-form-registrars',
193 'window.givewpDonationFormExports = ' . wp_json_encode($this->formExports()) . ';',
194 'before'
195 );
196
197 wp_add_inline_script(
198 'givewp-donation-form-registrars',
199 'window.givewpDonationConfirmationReceiptExports = ' . wp_json_encode($this->exports()) . ';',
200 'before'
201 );
202
203 // load template
204 /** @var FormDesignRegistrar $formDesignRegistrar */
205 $formDesignRegistrar = give(FormDesignRegistrar::class);
206
207 // silently fail if design is missing for some reason
208 if (!empty($formDesignId) && $formDesignRegistrar->hasDesign($formDesignId)) {
209 $design = $formDesignRegistrar->getDesign($formDesignId);
210
211 if ($design->css()) {
212 wp_enqueue_style('givewp-form-design-' . $design::id(), $design->css());
213 }
214
215 if ($design->js()) {
216 wp_enqueue_script(
217 'givewp-form-design-' . $design::id(),
218 $design->js(),
219 array_merge(
220 ['givewp-donation-form-registrars'],
221 $design->dependencies()
222 ),
223 false,
224 true
225 );
226 }
227 }
228
229 // load receipt app
230 wp_enqueue_script(
231 'givewp-donation-confirmation-receipt',
232 GIVE_PLUGIN_URL . 'build/donationConfirmationReceiptApp.js',
233 array_merge(
234 $this->getScriptAssetDependencies(GIVE_PLUGIN_DIR . 'build/donationConfirmationReceiptApp.asset.php'),
235 ['givewp-donation-form-registrars']
236 ),
237 GIVE_VERSION,
238 true
239 );
240
241 /**
242 * Load iframeResizer.contentWindow.min.js inside iframe
243 *
244 * @see https://github.com/davidjbradshaw/iframe-resizer
245 */
246 wp_enqueue_script(
247 'givewp-donation-form-embed-inside',
248 GIVE_PLUGIN_URL . 'build/donationFormEmbedInside.js',
249 [],
250 GIVE_VERSION,
251 true
252 );
253 }
254
255 /**
256 * @since 4.1.0
257 */
258 private function getCampaignColors(int $formId): array
259 {
260 /** @var Campaign|null $campaign */
261 $campaign = give()->campaigns->getByFormId($formId);
262
263 if ($campaign) {
264 return [
265 'primaryColor' => $campaign->primaryColor,
266 'secondaryColor' => $campaign->secondaryColor,
267 ];
268 }
269
270 return [
271 'primaryColor' => '',
272 'secondaryColor' => '',
273 ];
274 }
275 }
276