PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.18.0
GiveWP – Donation Plugin and Fundraising Platform v2.18.0
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 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / Views / Form / Templates / Classic / Classic.php
give / src / Views / Form / Templates / Classic Last commit date
resources 4 years ago Classic.php 4 years ago optionConfig.php 4 years ago
Classic.php
399 lines
1 <?php
2
3 namespace Give\Views\Form\Templates\Classic;
4
5 use Give\Helpers\Form\Template\Utils\Frontend;
6 use Give_Donate_Form;
7 use Give\Form\Template;
8 use Give\Form\Template\Hookable;
9 use Give\Form\Template\Scriptable;
10 use Give\Helpers\Form\Template as FormTemplateUtils;
11 use Give\Receipt\DonationReceipt;
12 use InvalidArgumentException;
13
14 /**
15 * Classic Donation Form Template
16 *
17 * @since 2.18.0
18 */
19 class Classic extends Template implements Hookable, Scriptable
20 {
21 /**
22 * @var array
23 */
24 private $options;
25
26 /**
27 * @var bool
28 */
29 private $scriptsLoaded = false;
30
31 public function __construct()
32 {
33 $this->options = FormTemplateUtils::getOptions();
34 }
35
36 /**
37 * @inheritDoc
38 */
39 public function getID()
40 {
41 return 'classic';
42 }
43
44 /**
45 * @inheritDoc
46 */
47 public function getName()
48 {
49 return __('Classic Form', 'give');
50 }
51
52 /**
53 * @inheritDoc
54 */
55 public function getImage()
56 {
57 return GIVE_PLUGIN_URL . 'assets/dist/images/admin/template-preview-classic.png';
58 }
59
60 /**
61 * @inheritDoc
62 */
63 public function getOptionsConfig()
64 {
65 return require 'optionConfig.php';
66 }
67
68 /**
69 * @inheritDoc
70 */
71 public function loadHooks()
72 {
73 add_action('give_pre_form', [$this, 'renderIconDefinitions']);
74
75 // Display header
76 if ('enabled' === $this->options[ 'visual_appearance' ][ 'display_header' ]) {
77 add_action('give_pre_form', [$this, 'renderHeader'], 10, 3);
78 }
79
80 add_action('give_before_donation_levels', [$this, 'renderDonationAmountHeading'], 20);
81
82 $sections = [
83 [
84 'hooks' => ['give_donation_form_top'],
85 'class' => 'give-donation-amount-section',
86 ],
87 [
88 'hooks' => ['give_donation_form_register_login_fields'],
89 'class' => 'give-personal-info-section',
90 ],
91 [
92 'hooks' => ['give_payment_mode_top', 'give_payment_mode_bottom'],
93 'class' => 'give-payment-details-section',
94 ],
95 [
96 'hooks' => ['give_donation_form_before_submit', 'give_donation_form_after_submit'],
97 'class' => 'give-donate-now-button-section',
98 ],
99 [
100 'hooks' => ['give_donation_summary_top', 'give_donation_summary_bottom'],
101 'class' => 'give-donation-form-summary-section',
102 ],
103 ];
104
105 foreach ($sections as $section) {
106 list ($start, $end) = array_pad($section[ 'hooks' ], 2, null);
107
108 add_action($start, function () use ($section) {
109 printf('<section class="give-form-section %s">', $section[ 'class' ]);
110 }, -10000);
111
112 add_action($end ? : $start, function () {
113 echo '</section>';
114 }, 10000);
115 }
116
117 /**
118 * Remove actions
119 */
120 // Remove goal.
121 remove_action('give_pre_form', 'give_show_goal_progress');
122 // Remove intermediate continue button which appear when display style set to other then onpage.
123 remove_action('give_after_donation_levels', 'give_display_checkout_button');
124 // Hide title.
125 add_filter('give_form_title', '__return_empty_string');
126 }
127
128
129 /**
130 * @inheritDoc
131 */
132 public function loadScripts()
133 {
134 if ($this->scriptsLoaded) {
135 return;
136 }
137
138 $this->scriptsLoaded = true;
139
140 // Font
141 if ($primaryFont = $this->getPrimaryFont()) {
142 wp_enqueue_style(
143 'give-google-font',
144 'https://fonts.googleapis.com/css?family=' . urlencode($primaryFont) . ':400,500,600,700&display=swap',
145 [],
146 GIVE_VERSION
147 );
148 }
149
150 wp_enqueue_style(
151 'give-classic-template',
152 GIVE_PLUGIN_URL . 'assets/dist/css/give-classic-template.css',
153 [],
154 GIVE_VERSION
155 );
156
157 // We are replacing the Give styles with this template. Let’s not fight
158 // against ourselves. This will help us not need to write such specific
159 // styles so that users can still override ours.
160 add_action('wp_enqueue_scripts', function () {
161 wp_dequeue_style('give-styles');
162 wp_dequeue_style('give_recurring_css');
163 wp_dequeue_style('give-currency-switcher-style');
164 wp_dequeue_style('give-fee-recovery');
165 wp_dequeue_style('give-donation-summary-style-frontend');
166 wp_dequeue_style('give-authorize-css');
167 }, 10);
168
169 // CSS Variables
170 wp_add_inline_style(
171 'give-classic-template',
172 $this->loadFile('css/variables.php', [
173 'primaryColor' => $this->options[ 'visual_appearance' ][ 'primary_color' ],
174 'headerBackgroundImage' => $this->options[ 'visual_appearance' ][ 'header_background_image' ],
175 'headerBackgroundColor' => $this->options[ 'visual_appearance' ][ 'header_background_color' ],
176 'statsProgressBarColor' => give_get_meta(Frontend::getFormId(), '_give_goal_color', true),
177 'primaryFont' => $primaryFont ? : 'inherit'
178 ])
179 );
180
181 // JS
182 wp_enqueue_script(
183 'give-classic-template-js',
184 GIVE_PLUGIN_URL . 'assets/dist/js/give-classic-template.js',
185 ['give'],
186 GIVE_VERSION,
187 true
188 );
189
190 wp_localize_script(
191 'give-classic-template-js',
192 'classicTemplateOptions',
193 $this->options
194 );
195 }
196
197 /**
198 * @inheritDoc
199 */
200 public function getLoadingView()
201 {
202 return $this->loadFile('views/loading.php', [
203 'options' => $this->options[ 'visual_appearance' ]
204 ]);
205 }
206
207 /**
208 * @inheritDoc
209 */
210 public function renderLoadingView($formId = null)
211 {
212 echo $this->getLoadingView();
213 }
214
215 /**
216 * @inheritDoc
217 */
218 public function getReceiptView()
219 {
220 return wp_doing_ajax()
221 ? $this->getFilePath('views/receipt.php')
222 : parent::getReceiptView();
223 }
224
225 /**
226 * Render donation form header
227 *
228 * @param int $formId
229 * @param array $args
230 * @param Give_Donate_Form $form
231 */
232 public function renderHeader($formId, $args, $form)
233 {
234 $hasGoal = $form->has_goal();
235
236 echo $this->loadFile('views/header.php', [
237 'title' => $this->options[ 'visual_appearance' ][ 'main_heading' ] ?? $form->post_title,
238 'description' => $this->options[ 'visual_appearance' ][ 'description' ],
239 'isSecureBadgeEnabled' => $this->options[ 'visual_appearance' ][ 'secure_badge' ] === 'enabled',
240 'secureBadgeContent' => $this->options[ 'visual_appearance' ][ 'secure_badge_text' ],
241 'hasGoal' => $hasGoal,
242 'goalStats' => $hasGoal ? $this->getFormGoalStats($form) : []
243 ]);
244 }
245
246 /**
247 * Render donation amount heading
248 */
249 public function renderDonationAmountHeading()
250 {
251 echo $this->loadFile('views/donation-amount-heading.php', [
252 'content' => $this->options[ 'donation_amount' ][ 'headline' ],
253 ]);
254 }
255
256 /**
257 * Render the SVG icon definitions.
258 *
259 * @void
260 */
261 public function renderIconDefinitions()
262 {
263 echo $this->loadFile('views/icon-defs.php');
264 }
265
266 /**
267 * @inheritDoc
268 */
269 public function getReceiptDetails($donationId)
270 {
271 $receipt = new DonationReceipt($donationId);
272
273 $receipt->heading = esc_html(give_do_email_tags($this->options[ 'donation_receipt' ][ 'headline' ], ['payment_id' => $donationId]));
274 $receipt->message = wp_kses_post(give_do_email_tags($this->options[ 'donation_receipt' ][ 'description' ], ['payment_id' => $donationId]));
275
276 /**
277 * Fire the action for receipt object.
278 *
279 * @since 2.7.0
280 */
281 do_action('give_new_receipt', $receipt);
282
283 return $receipt;
284 }
285
286 public function getFormGoalStats(Give_Donate_Form $form)
287 {
288 $goalStats = give_goal_progress_stats($form->get_ID());
289 $raisedRaw = $form->get_earnings();
290
291 // Setup default raised value
292 $raised = give_currency_filter(
293 give_format_amount(
294 $raisedRaw,
295 [
296 'sanitize' => false,
297 'decimal' => false,
298 ]
299 )
300 );
301
302 // Setup default count value
303 $count = $form->get_sales();
304
305 // Setup default count label
306 $countLabel = _n('donation', 'donations', $count, 'give');
307
308 // Setup default goal value
309 $goal = give_currency_filter(
310 give_format_amount(
311 $form->get_goal(),
312 [
313 'sanitize' => false,
314 'decimal' => false,
315 ]
316 )
317 );
318
319 $stats = [
320 'raised' => $raised,
321 'raisedRaw' => $raisedRaw,
322 'progress' => $goalStats[ 'progress' ],
323 'count' => $count,
324 'countLabel' => $countLabel,
325 'goal' => $goal,
326 'goalRaw' => $goalStats[ 'raw_goal' ],
327 ];
328
329 switch ($goalStats[ 'format' ]) {
330 case 'donation':
331 return array_merge($stats, [
332 'count' => $goalStats[ 'actual' ],
333 'goal' => $goalStats[ 'goal' ],
334 ]);
335
336 case 'donors':
337 return array_merge($stats, [
338 'count' => $goalStats[ 'actual' ],
339 'countLabel' => _n('donor', 'donors', $count, 'give'),
340 'goal' => $goalStats[ 'goal' ],
341 ]);
342
343 default:
344 return $stats;
345 }
346 }
347
348 /**
349 * Load file
350 *
351 * @param string $file
352 * @param array $args
353 *
354 * @return string
355 * @throws InvalidArgumentException
356 *
357 */
358 protected function loadFile($file, $args = [])
359 {
360 $filePath = $this->getFilePath($file);
361
362 if (! file_exists($filePath)) {
363 throw new InvalidArgumentException("File {$filePath} does not exist");
364 }
365
366 ob_start();
367 extract($args);
368 include $filePath;
369
370 return ob_get_clean();
371 }
372
373 /**
374 * Get file path
375 *
376 * @param string $file
377 *
378 * @return string
379 */
380 protected function getFilePath($file = '')
381 {
382 return GIVE_PLUGIN_DIR . "src/Views/Form/Templates/Classic/resources/{$file}";
383 }
384
385 /**
386 * @return string|null
387 */
388 protected function getPrimaryFont()
389 {
390 $primaryFont = $this->options[ 'visual_appearance' ][ 'primary_font' ];
391
392 if ($primaryFont !== 'system') {
393 return $primaryFont;
394 }
395
396 return null;
397 }
398 }
399