PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.33.1
GiveWP – Donation Plugin and Fundraising Platform v2.33.1
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 3 years ago Classic.php 2 years ago optionConfig.php 4 years ago
Classic.php
416 lines
1 <?php
2
3 namespace Give\Views\Form\Templates\Classic;
4
5 use Give\Form\Template;
6 use Give\Form\Template\Hookable;
7 use Give\Form\Template\Scriptable;
8 use Give\Helpers\Form\Template as FormTemplateUtils;
9 use Give\Helpers\Form\Template\Utils\Frontend;
10 use Give\Receipt\DonationReceipt;
11 use Give_Donate_Form;
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 appears when display style set to anything other than "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 * @since 2.30.0 Check if visual_appearance is set before accessing it.
201 */
202 public function getLoadingView()
203 {
204 return $this->loadFile('views/loading.php', [
205 'options' => array_key_exists('visual_appearance', $this->options)
206 ? $this->options['visual_appearance']
207 : [],
208 ]);
209 }
210
211 /**
212 * @inheritDoc
213 */
214 public function renderLoadingView($formId = null)
215 {
216 echo $this->getLoadingView();
217 }
218
219 /**
220 * @inheritDoc
221 */
222 public function getReceiptView()
223 {
224 return wp_doing_ajax()
225 ? $this->getFilePath('views/receipt.php')
226 : parent::getReceiptView();
227 }
228
229 /**
230 * Render donation form header
231 *
232 * @since 2.19.0 use trinary operator instead of Coalesce operator to make code php 5.6 compatible.
233 *
234 * @param int $formId
235 * @param array $args
236 * @param Give_Donate_Form $form
237 */
238 public function renderHeader($formId, $args, $form)
239 {
240 $hasGoal = $form->has_goal();
241
242 echo $this->loadFile('views/header.php', [
243 'title' => isset($this->options['visual_appearance']['main_heading']) ? $this->options['visual_appearance']['main_heading'] : $form->post_title,
244 'description' => $this->options[ 'visual_appearance' ][ 'description' ],
245 'isSecureBadgeEnabled' => $this->options[ 'visual_appearance' ][ 'secure_badge' ] === 'enabled',
246 'secureBadgeContent' => $this->options[ 'visual_appearance' ][ 'secure_badge_text' ],
247 'hasGoal' => $hasGoal,
248 'goalStats' => $hasGoal ? $this->getFormGoalStats($form) : []
249 ]);
250 }
251
252 /**
253 * Render donation amount heading
254 */
255 public function renderDonationAmountHeading()
256 {
257 echo $this->loadFile('views/donation-amount-heading.php', [
258 'content' => $this->options[ 'donation_amount' ][ 'headline' ],
259 ]);
260 }
261
262 /**
263 * Render the SVG icon definitions.
264 *
265 * @void
266 */
267 public function renderIconDefinitions()
268 {
269 echo $this->loadFile('views/icon-defs.php');
270 }
271
272 /**
273 * @inheritDoc
274 */
275 public function getReceiptDetails($donationId)
276 {
277 $receipt = new DonationReceipt($donationId);
278
279 $receipt->heading = esc_html(give_do_email_tags($this->options[ 'donation_receipt' ][ 'headline' ], ['payment_id' => $donationId]));
280 $receipt->message = wp_kses_post(give_do_email_tags($this->options[ 'donation_receipt' ][ 'description' ], ['payment_id' => $donationId]));
281
282 /**
283 * Fire the action for receipt object.
284 *
285 * @since 2.7.0
286 */
287 do_action('give_new_receipt', $receipt);
288
289 return $receipt;
290 }
291
292 public function getFormGoalStats(Give_Donate_Form $form)
293 {
294 $goalStats = give_goal_progress_stats($form->get_ID());
295 $raisedRaw = $form->get_earnings();
296
297 // Setup default raised value
298 $raised = give_currency_filter(
299 give_format_amount(
300 $raisedRaw,
301 [
302 'sanitize' => false,
303 'decimal' => false,
304 ]
305 )
306 );
307
308 // Setup default count value
309 $count = $form->get_sales();
310
311 // Setup default count label
312 $countLabel = _n('donation', 'donations', $count, 'give');
313
314 // Setup default goal value
315 $goal = give_currency_filter(
316 give_format_amount(
317 $form->get_goal(),
318 [
319 'sanitize' => false,
320 'decimal' => false,
321 ]
322 )
323 );
324
325 $stats = [
326 'raised' => $raised,
327 'raisedRaw' => $raisedRaw,
328 'progress' => $goalStats[ 'progress' ],
329 'count' => $count,
330 'countLabel' => $countLabel,
331 'goal' => $goal,
332 'goalRaw' => $goalStats[ 'raw_goal' ],
333 ];
334
335 switch ($goalStats[ 'format' ]) {
336 case 'donation':
337 return array_merge($stats, [
338 'count' => $goalStats[ 'actual' ],
339 'goal' => $goalStats[ 'goal' ],
340 ]);
341
342 case 'donors':
343 return array_merge($stats, [
344 'count' => $goalStats[ 'actual' ],
345 'countLabel' => _n('donor', 'donors', $count, 'give'),
346 'goal' => $goalStats[ 'goal' ],
347 ]);
348
349 default:
350 return $stats;
351 }
352 }
353
354 /**
355 * Load file
356 *
357 * @param string $file
358 * @param array $args
359 *
360 * @return string
361 * @throws InvalidArgumentException
362 *
363 */
364 protected function loadFile($file, $args = [])
365 {
366 $filePath = $this->getFilePath($file);
367
368 if (! file_exists($filePath)) {
369 throw new InvalidArgumentException("File {$filePath} does not exist");
370 }
371
372 ob_start();
373 extract($args);
374 include $filePath;
375
376 return ob_get_clean();
377 }
378
379 /**
380 * Get file path
381 *
382 * @param string $file
383 *
384 * @return string
385 */
386 protected function getFilePath($file = '')
387 {
388 return GIVE_PLUGIN_DIR . "src/Views/Form/Templates/Classic/resources/{$file}";
389 }
390
391 /**
392 * @return string|null
393 */
394 protected function getPrimaryFont()
395 {
396 $primaryFont = $this->options[ 'visual_appearance' ][ 'primary_font' ];
397
398 if ($primaryFont !== 'system') {
399 return $primaryFont;
400 }
401
402 return null;
403 }
404
405 /**
406 * @since 2.27.0 Return description from form settings.
407 * @param int $formId
408 *
409 * @return string
410 */
411 public function getFormExcerpt($formId)
412 {
413 return $this->options['visual_appearance']['description'];
414 }
415 }
416