PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.14.6
GiveWP – Donation Plugin and Fundraising Platform v4.14.6
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 1 year ago Classic.php 1 year ago optionConfig.php 4 years ago
Classic.php
429 lines
1 <?php
2
3 namespace Give\Views\Form\Templates\Classic;
4
5 use Give\DonationForms\DonationQuery;
6 use Give\Form\Template;
7 use Give\Form\Template\Hookable;
8 use Give\Form\Template\Scriptable;
9 use Give\Helpers\Form\Template as FormTemplateUtils;
10 use Give\Helpers\Form\Template\Utils\Frontend;
11 use Give\MultiFormGoals\ProgressBar\Model as ProgressBarModal;
12 use Give\Receipt\DonationReceipt;
13 use Give_Donate_Form;
14 use InvalidArgumentException;
15
16 /**
17 * Classic Donation Form Template
18 *
19 * @since 2.18.0
20 */
21 class Classic extends Template implements Hookable, Scriptable
22 {
23 /**
24 * @var array
25 */
26 private $options;
27
28 /**
29 * @var bool
30 */
31 private $scriptsLoaded = false;
32
33 public function __construct()
34 {
35 $this->options = FormTemplateUtils::getOptions();
36 }
37
38 /**
39 * @inheritDoc
40 */
41 public function getID()
42 {
43 return 'classic';
44 }
45
46 /**
47 * @inheritDoc
48 */
49 public function getName()
50 {
51 return __('Classic Form', 'give');
52 }
53
54 /**
55 * @inheritDoc
56 */
57 public function getImage()
58 {
59 return GIVE_PLUGIN_URL . 'build/assets/dist/images/admin/template-preview-classic.png';
60 }
61
62 /**
63 * @inheritDoc
64 */
65 public function getOptionsConfig()
66 {
67 return require 'optionConfig.php';
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function loadHooks()
74 {
75 add_action('give_pre_form', [$this, 'renderIconDefinitions']);
76
77 // Display header
78 if ('enabled' === $this->options[ 'visual_appearance' ][ 'display_header' ]) {
79 add_action('give_pre_form', [$this, 'renderHeader'], 10, 3);
80 }
81
82 add_action('give_before_donation_levels', [$this, 'renderDonationAmountHeading'], 20);
83
84 $sections = [
85 [
86 'hooks' => ['give_donation_form_top'],
87 'class' => 'give-donation-amount-section',
88 ],
89 [
90 'hooks' => ['give_donation_form_register_login_fields'],
91 'class' => 'give-personal-info-section',
92 ],
93 [
94 'hooks' => ['give_payment_mode_top', 'give_payment_mode_bottom'],
95 'class' => 'give-payment-details-section',
96 ],
97 [
98 'hooks' => ['give_donation_form_before_submit', 'give_donation_form_after_submit'],
99 'class' => 'give-donate-now-button-section',
100 ],
101 [
102 'hooks' => ['give_donation_summary_top', 'give_donation_summary_bottom'],
103 'class' => 'give-donation-form-summary-section',
104 ],
105 ];
106
107 foreach ($sections as $section) {
108 [$start, $end] = array_pad($section['hooks'], 2, null);
109
110 add_action($start, function () use ($section) {
111 printf('<section class="give-form-section %s">', $section[ 'class' ]);
112 }, -10000);
113
114 add_action($end ? : $start, function () {
115 echo '</section>';
116 }, 10000);
117 }
118
119 /**
120 * Remove actions
121 */
122 // Remove goal.
123 remove_action('give_pre_form', 'give_show_goal_progress');
124 // Remove intermediate continue button which appears when display style set to anything other than "onpage".
125 remove_action('give_after_donation_levels', 'give_display_checkout_button');
126 // Hide title.
127 add_filter('give_form_title', '__return_empty_string');
128 }
129
130
131 /**
132 * @inheritDoc
133 *
134 * @since 4.1.0 Added custom form styles
135 */
136 public function loadScripts()
137 {
138 if ($this->scriptsLoaded) {
139 return;
140 }
141
142 $this->scriptsLoaded = true;
143
144 // Font
145 if ($primaryFont = $this->getPrimaryFont()) {
146 wp_enqueue_style(
147 'give-google-font',
148 'https://fonts.googleapis.com/css?family=' . urlencode($primaryFont) . ':400,500,600,700&display=swap',
149 [],
150 GIVE_VERSION
151 );
152 }
153
154 wp_enqueue_style(
155 'give-classic-template',
156 GIVE_PLUGIN_URL . 'build/assets/dist/css/give-classic-template.css',
157 [],
158 GIVE_VERSION
159 );
160
161 // We are replacing the Give styles with this template. Let’s not fight
162 // against ourselves. This will help us not need to write such specific
163 // styles so that users can still override ours.
164 add_action('wp_enqueue_scripts', function () {
165 wp_dequeue_style('give-styles');
166 wp_dequeue_style('give_recurring_css');
167 wp_dequeue_style('give-currency-switcher-style');
168 wp_dequeue_style('give-fee-recovery');
169 wp_dequeue_style('give-donation-summary-style-frontend');
170 wp_dequeue_style('give-authorize-css');
171 }, 10);
172
173 // CSS Variables
174 wp_add_inline_style(
175 'give-classic-template',
176 $this->loadFile('css/variables.php', [
177 'primaryColor' => $this->options[ 'visual_appearance' ][ 'primary_color' ],
178 'headerBackgroundImage' => $this->options[ 'visual_appearance' ][ 'header_background_image' ],
179 'headerBackgroundColor' => $this->options[ 'visual_appearance' ][ 'header_background_color' ],
180 'statsProgressBarColor' => give_get_meta(Frontend::getFormId(), '_give_goal_color', true),
181 'primaryFont' => $primaryFont ? : 'inherit'
182 ])
183 );
184
185 // Custom styles
186 wp_add_inline_style(
187 'give-classic-template',
188 wp_strip_all_tags(give_get_option('custom_form_styles', ''))
189 );
190
191 // JS
192 wp_enqueue_script(
193 'give-classic-template-js',
194 GIVE_PLUGIN_URL . 'build/assets/dist/js/give-classic-template.js',
195 ['give'],
196 GIVE_VERSION,
197 true
198 );
199
200 wp_localize_script(
201 'give-classic-template-js',
202 'classicTemplateOptions',
203 $this->options
204 );
205 }
206
207 /**
208 * @inheritDoc
209 *
210 * @since 2.30.0 Check if visual_appearance is set before accessing it.
211 */
212 public function getLoadingView()
213 {
214 return $this->loadFile('views/loading.php', [
215 'options' => array_key_exists('visual_appearance', $this->options)
216 ? $this->options['visual_appearance']
217 : [],
218 ]);
219 }
220
221 /**
222 * @inheritDoc
223 */
224 public function renderLoadingView($formId = null)
225 {
226 echo $this->getLoadingView();
227 }
228
229 /**
230 * @inheritDoc
231 */
232 public function getReceiptView()
233 {
234 return wp_doing_ajax()
235 ? $this->getFilePath('views/receipt.php')
236 : parent::getReceiptView();
237 }
238
239 /**
240 * Render donation form header
241 *
242 * @since 2.19.0 use trinary operator instead of Coalesce operator to make code php 5.6 compatible.
243 *
244 * @param int $formId
245 * @param array $args
246 * @param Give_Donate_Form $form
247 */
248 public function renderHeader($formId, $args, $form)
249 {
250 $hasGoal = $form->has_goal();
251
252 echo $this->loadFile('views/header.php', [
253 'title' => isset($this->options['visual_appearance']['main_heading']) ? $this->options['visual_appearance']['main_heading'] : $form->post_title,
254 'description' => $this->options[ 'visual_appearance' ][ 'description' ],
255 'isSecureBadgeEnabled' => $this->options[ 'visual_appearance' ][ 'secure_badge' ] === 'enabled',
256 'secureBadgeContent' => $this->options[ 'visual_appearance' ][ 'secure_badge_text' ],
257 'hasGoal' => $hasGoal,
258 'goalStats' => $hasGoal ? $this->getFormGoalStats($form) : []
259 ]);
260 }
261
262 /**
263 * Render donation amount heading
264 */
265 public function renderDonationAmountHeading()
266 {
267 echo $this->loadFile('views/donation-amount-heading.php', [
268 'content' => $this->options[ 'donation_amount' ][ 'headline' ],
269 ]);
270 }
271
272 /**
273 * Render the SVG icon definitions.
274 *
275 * @void
276 */
277 public function renderIconDefinitions()
278 {
279 echo $this->loadFile('views/icon-defs.php');
280 }
281
282 /**
283 * @inheritDoc
284 */
285 public function getReceiptDetails($donationId)
286 {
287 $receipt = new DonationReceipt($donationId);
288
289 $receipt->heading = esc_html(give_do_email_tags($this->options[ 'donation_receipt' ][ 'headline' ], ['payment_id' => $donationId]));
290 $receipt->message = wp_kses_post(give_do_email_tags($this->options[ 'donation_receipt' ][ 'description' ], ['payment_id' => $donationId]));
291
292 /**
293 * Fire the action for receipt object.
294 *
295 * @since 2.7.0
296 */
297 do_action('give_new_receipt', $receipt);
298
299 return $receipt;
300 }
301
302 /**
303 * @since 3.14.0 Use sumIntendedAmount() and getDonationCount() methods to retrieve the proper values for the raised amount and donations count
304 */
305 public function getFormGoalStats(Give_Donate_Form $form)
306 {
307 $goalStats = give_goal_progress_stats($form->get_ID());
308 $raisedRaw = (new DonationQuery())->form($form->ID)->sumIntendedAmount();
309
310 // Setup default raised value
311 $raised = give_currency_filter(
312 give_format_amount(
313 $raisedRaw,
314 [
315 'sanitize' => false,
316 'decimal' => false,
317 ]
318 )
319 );
320
321 // Setup default count value
322 $count = (new ProgressBarModal(['ids' => [$form->get_ID()]]))->getDonationCount();
323
324 // Setup default count label
325 $countLabel = _n('donation', 'donations', $count, 'give');
326
327 // Setup default goal value
328 $goal = give_currency_filter(
329 give_format_amount(
330 $form->get_goal(),
331 [
332 'sanitize' => false,
333 'decimal' => false,
334 ]
335 )
336 );
337
338 $stats = [
339 'raised' => $raised,
340 'raisedRaw' => $raisedRaw,
341 'progress' => $goalStats[ 'progress' ],
342 'count' => $count,
343 'countLabel' => $countLabel,
344 'goal' => $goal,
345 'goalRaw' => $goalStats[ 'raw_goal' ],
346 ];
347
348 switch ($goalStats[ 'format' ]) {
349 case 'donation':
350 return array_merge($stats, [
351 'count' => $goalStats[ 'actual' ],
352 'goal' => $goalStats[ 'goal' ],
353 ]);
354
355 case 'donors':
356 return array_merge($stats, [
357 'count' => $goalStats[ 'actual' ],
358 'countLabel' => _n('donor', 'donors', $count, 'give'),
359 'goal' => $goalStats[ 'goal' ],
360 ]);
361
362 default:
363 return $stats;
364 }
365 }
366
367 /**
368 * Load file
369 *
370 * @param string $file
371 * @param array $args
372 *
373 * @return string
374 * @throws InvalidArgumentException
375 *
376 */
377 protected function loadFile($file, $args = [])
378 {
379 $filePath = $this->getFilePath($file);
380
381 if (! file_exists($filePath)) {
382 throw new InvalidArgumentException("File {$filePath} does not exist");
383 }
384
385 ob_start();
386 extract($args);
387 include $filePath;
388
389 return ob_get_clean();
390 }
391
392 /**
393 * Get file path
394 *
395 * @param string $file
396 *
397 * @return string
398 */
399 protected function getFilePath($file = '')
400 {
401 return GIVE_PLUGIN_DIR . "src/Views/Form/Templates/Classic/resources/{$file}";
402 }
403
404 /**
405 * @return string|null
406 */
407 protected function getPrimaryFont()
408 {
409 $primaryFont = $this->options[ 'visual_appearance' ][ 'primary_font' ];
410
411 if ($primaryFont !== 'system') {
412 return $primaryFont;
413 }
414
415 return null;
416 }
417
418 /**
419 * @since 2.27.0 Return description from form settings.
420 * @param int $formId
421 *
422 * @return string
423 */
424 public function getFormExcerpt($formId)
425 {
426 return $this->options['visual_appearance']['description'];
427 }
428 }
429