PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.11
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / admin / campaign-builder / fields / class-donate-amount.php

class-donate-amount.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.11, at includes/admin/campaign-builder/fields/class-donate-amount.php

520 lines 19.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to add donation amount (oringially from the widget) to a campaign form in the builder.
4 *
5 * @package Charitable
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.8.0
10 * @version 1.8.0
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Field_Donate_Amount' ) ) :
18
19 /**
20 * Class to add campaign field donate amount field to a campaign form in the builder.
21 */
22 class Charitable_Field_Donate_Amount extends Charitable_Builder_Field {
23
24 /**
25 * Primary class constructor.
26 *
27 * @since 1.8.0
28 */
29 public function init() {
30
31 // Basic information.
32 $this->name = esc_html__( 'Donate Amount', 'charitable' );
33 $this->type = 'donate-amount';
34 $this->icon = 'fa fa-dollar';
35 $this->order = 100;
36 $this->group = 'standard';
37
38 $this->align_default = 'center';
39
40 // Edit/Duplication information.
41 $this->can_be_edited = true;
42 $this->can_be_deleted = true;
43 $this->can_be_duplicated = false;
44 $this->edit_label = esc_html__( 'Edit Donate Amount', 'charitable' );
45 $this->edit_type = 'donate-amount';
46 $this->edit_section = 'standard';
47 $this->max_allowed = 1;
48
49 // Misc.
50 $this->tooltip = '';
51
52 // Define additional field properties.
53 // add_action( 'charitable_frontend_js', [ $this, 'frontend_js' ] );
54 // add_action( 'charitable_builder_backend_scripts', [ $this, 'builder_js' ]); // admin_enqueue_scripts.
55 add_filter( 'charitable_builder_field_button_attributes', [ $this, 'make_unique_button' ], 10, 4 );
56 }
57
58 /**
59 * Field options panel inside the builder.
60 *
61 * @since 1.8.0
62 *
63 * @param array $field Field settings.
64 */
65 public function field_options( $field ) {
66 /*
67 * Basic field options.
68 */
69
70 // Options open markup.
71 }
72
73 /**
74 * Render the field.
75 *
76 * @since 1.8.0
77 *
78 * @param array $field_data Any field data.
79 * @param array $campaign_data Amount data and settings.
80 * @param integer $field_id The field ID.
81 * @param string $mode Where the field is being displayed ("preview" or "template").
82 * @param array $template_data Tempalate data.
83 */
84 public function render( $field_data = false, $campaign_data = false, $field_id = false, $mode = 'template', $template_data = false ) {
85
86 $headline = ! empty( $field_data['headline'] ) ? esc_html( $field_data['headline'] ) : false;
87 $campaign_id = isset( $campaign_data['id'] ) && 0 !== intval( $campaign_data['id'] ) ? intval( $campaign_data['id'] ) : false;
88
89 $preview_or_template_css = ( 'preview' === $mode ) ? 'preview' : 'template';
90 $input_disabled_attr = ( 'preview' === $mode ) ? 'disabled="true"' : false;
91 $donation_amount_found = false;
92
93 $currency_helper = charitable_get_currency_helper();
94
95 ob_start();
96
97 if ( 'preview' === $mode ) :
98 echo '<div class="placeholder">';
99 endif;
100
101 if ( 'preview' === $mode || ( 'template' === $mode && false !== $headline ) ) :
102 echo '<h5 class="charitable-field-' . esc_attr( $preview_or_template_css ) . '-headline">' . esc_html( $headline ) . '</h5>';
103 endif;
104
105 // if there is no campaign id, then we assume this is a new campaign and assign the default donation amounts.
106 if ( false === $campaign_id ) {
107
108 echo '
109 <div class="charitable-' . esc_attr( $preview_or_template_css ) . '-donation-options">
110 <ul class="charitable-' . esc_attr( $preview_or_template_css ) . '-donation-amounts">
111 <li class="charitable-' . esc_attr( $preview_or_template_css ) . '-donation-amount suggested-donation-amount">
112 <label><span class="amount">' . esc_html( $currency_helper->get_monetary_amount( '5.00' ) ) . '</span></label>
113 </li>
114 <li class="charitable-' . esc_attr( $preview_or_template_css ) . '-donation-amount suggested-donation-amount">
115 <label><span class="amount">' . esc_html( $currency_helper->get_monetary_amount( '10.00' ) ) . '</span></label>
116 </li>
117 <li class="charitable-' . esc_attr( $preview_or_template_css ) . '-donation-amount suggested-donation-amount selected">
118 <label><span class="amount">' . esc_html( $currency_helper->get_monetary_amount( '15.00' ) ) . '</span></label>
119 </li>
120 <li class="charitable-' . esc_attr( $preview_or_template_css ) . '-donation-amount suggested-donation-amount">
121 <label><span class="amount">' . esc_html( $currency_helper->get_monetary_amount( '20.00' ) ) . '</span></label>
122 </li>
123 <li class="charitable-' . esc_attr( $preview_or_template_css ) . '-donation-amount custom-donation-amount">
124 <span class="custom-donation-amount-wrapper">
125 <label>
126 <input type="radio" name="donation_amount" value="custom"><span class="description">' . esc_html__( 'Custom Amount', 'charitable' ) . '</span>
127 </label>
128 <input type="text" ' . esc_attr( $input_disabled_attr ) . ' class="custom-donation-input" name="custom_donation_amount" placeholder="' . esc_html__( 'Custom Donation Amount', 'charitable' ) . '" value="">
129 </span>
130 </li>
131 </ul>
132 </div>';
133
134 } else {
135
136 $campaign = charitable_get_campaign( $campaign_id );
137
138 // We want the button to contain a donation amount, but we go with what is in the SESSION first, then the DEFAULT amount.
139 $donation_amount_in_session = $campaign->get_donation_amount_in_session();
140
141 if ( ! $campaign ) :
142 echo '<p>Cannot recieve donations.</p>';
143 echo '</div> <!-- placeholder -->';
144 $html = ob_get_clean();
145 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
146 return;
147 endif;
148
149 $suggested_donations = $campaign->get_suggested_donations();
150
151 if ( empty( $suggested_donations ) && ! $campaign->get( 'allow_custom_donations' ) ) :
152 echo '<p>No donation options available.</p>';
153 echo '</div> <!-- placeholder -->';
154 $html = ob_get_clean();
155 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
156 return;
157 endif;
158
159 echo '
160 <div class="charitable-' . esc_attr( $preview_or_template_css ) . '-donation-options">
161 <ul class="charitable-' . esc_attr( $preview_or_template_css ) . '-donation-amounts">';
162
163 if ( ! empty( $suggested_donations ) ) :
164
165 $suggested_donations_default = ! empty( $campaign_data['settings']['donation-options']['suggested_donations_default'] ) ? intval( $campaign_data['settings']['donation-options']['suggested_donations_default'] ) : 0;
166
167 foreach ( $suggested_donations as $suggested_donation_id => $suggested_donation ) :
168
169 $selected_css = false;
170 $checked_attribute = false;
171
172 if ( ( $donation_amount_in_session && $donation_amount_in_session === $suggested_donation['amount'] )
173 || ( ! $donation_amount_in_session && $suggested_donation_id === $suggested_donations_default ) ) {
174 $selected_css = 'selected';
175 $checked_attribute = 'checked="true"';
176 $donation_amount_found = true;
177 }
178
179 ?>
180 <li class="charitable-<?php echo esc_attr( $preview_or_template_css ); ?>-donation-amount suggested-donation-amount <?php echo esc_attr( $selected_css ); ?>">
181 <label>
182 <input type="radio" name="donation_amount" value="<?php echo esc_attr( $suggested_donation['amount'] ); ?>" <?php echo esc_attr( $checked_attribute ); ?>>
183 <span class="amount"><?php echo esc_html( $currency_helper->get_monetary_amount( $suggested_donation['amount'] ) ); ?></span> <?php /* <span class="description"><?php echo esc_html( $suggested_donation['description'] ); ?></span> */ ?>
184 </label>
185 </li>
186 <?php
187 endforeach;
188 endif;
189 ?>
190
191 <?php
192
193 $custom_donation_amount_class = ! empty( $campaign_data['settings']['donation-options']['allow_custom_donations'] ) ? false : 'charitable-hidden';
194
195 $value = false === $donation_amount_found && $donation_amount_in_session !== 0 ? $currency_helper->get_monetary_amount( $donation_amount_in_session ) : false;
196
197 ?>
198
199 <li class="charitable-<?php echo esc_attr( $preview_or_template_css ); ?>-donation-amount custom-donation-amount <?php echo $custom_donation_amount_class; ?>"> <?php // phpcs:ignore ?>
200 <span class="custom-donation-amount-wrapper">
201 <label>
202 <input type="radio" name="donation_amount" value="custom"><span class="description"><?php echo esc_html__( 'Custom amount', 'charitable' ); ?></span>
203 </label>
204 <input type="text" <?php echo esc_attr( $input_disabled_attr ); ?> class="custom-donation-input" name="custom_donation_amount" placeholder="<?php echo esc_html__( 'Custom Donation Amount', 'charitable' ); ?>" value="<?php echo esc_attr( $value ); ?>">
205 </span>
206 </li>
207
208 <?php
209
210 echo '
211 </ul>
212 </div>';
213
214 }
215
216 if ( 'preview' === $mode ) :
217 echo '</div> <!-- placeholder -->';
218 endif;
219
220 $html = ob_get_clean();
221
222 return $html;
223 }
224
225 /**
226 * Donate Amount preview inside the builder.
227 *
228 * @since 1.8.0
229 *
230 * @param array $field_data Field data and settings.
231 * @param array $campaign_data Campaign data and settings.
232 * @param array $field_id Field ID.
233 * @param string $theme Template data.
234 */
235 public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) {
236
237 $html = $this->field_title( $this->name );
238 $html .= $this->field_wrapper( $this->render( $field_data, $campaign_data, $field_id, 'preview' ), $field_data );
239
240 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
241 }
242
243 /**
244 * The display on the campaign front-end.
245 *
246 * @since 1.8.0
247 *
248 * @param string $field_type The passed field type.
249 * @param array $field_data Any field data.
250 * @param array $campaign_data Amount data and settings.
251 */
252 public function field_display( $field_type = '', $field_data = false, $campaign_data = false ) {
253
254 $html = $this->field_display_wrapper( $this->render( $field_data, $campaign_data ), $field_data );
255
256 echo apply_filters( 'charitable_campaign_builder_' . $this->type . '_field_display', $html, $campaign_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
257 }
258
259 /**
260 * Donate Amount display on the form front-end.
261 *
262 * @since 1.8.0
263 *
264 * @param integer $field_id Number ID.
265 * @param array $campaign_data Amount data and settings.
266 */
267 public function settings_display( $field_id = false, $campaign_data = false ) {
268
269 if ( ! class_exists( 'Charitable_Builder_Form_Fields' ) ) {
270 return;
271 }
272
273 $charitable_builder_form_fields = new Charitable_Builder_Form_Fields();
274
275 $settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false;
276
277 ob_start();
278
279 ?>
280
281 <h4 class="charitable-panel-field" data-field-id="<?php echo intval( $field_id ); ?>"><?php echo esc_html( $this->name ); ?> (ID: <?php echo intval( $field_id ); ?>)</h4>
282
283 <?php
284
285 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
286 isset( $settings['headline'] ) ? $settings['headline'] : '',
287 esc_html__( 'Headline', 'charitable' ),
288 array(
289 'id' => 'field_' . esc_attr( $this->type ) . '_headline' . '_' . intval( $field_id ), // phpcs:ignore
290 'name' => array( '_fields', intval( $field_id ), 'headline' ),
291 'field_id' => intval( $field_id ),
292 'tooltip' => esc_html__( 'Add a headline to this field.', 'charitable' ),
293 'class' => 'charitable-campaign-builder-headline',
294 )
295 );
296
297 echo $charitable_builder_form_fields->generate_divider( false, false, array( 'field_id' => $field_id ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
298
299 echo $charitable_builder_form_fields->generate_donation_amounts_mini( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
300 $campaign_data,
301 esc_html__( 'Suggested Donation Amounts', 'charitable' ),
302 array(
303 'from' => 'field',
304 'id' => 'field_' . esc_attr( $this->type ) . '_donation_amounts' . '_' . intval( $field_id ), // phpcs:ignore
305 'name' => array( '_fields', intval( $field_id ), 'donation_amounts' ),
306 'field_id' => intval( $field_id ),
307 'default' => false,
308 )
309 );
310
311 echo $charitable_builder_form_fields->generate_divider( false, false, array( 'field_id' => $field_id ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
312
313 $campaign_settings = false;
314
315 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display/routing parameter
316 if ( ! empty( $_GET['campaign_id'] ) ) {
317 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display/routing parameter
318 $campaign_id = intval( wp_unslash( $_GET['campaign_id'] ) );
319 if ( $campaign_id > 0 ) {
320 $campaign_settings = get_post_meta( $campaign_id, 'campaign_settings_v2', true );
321 }
322 }
323
324 $default_allow_custom = false !== $campaign_settings && ! isset( $campaign_settings['settings']['donation-options']['allow_custom_donations'] ) ? false : '1';
325
326 echo $charitable_builder_form_fields->generate_toggle( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
327 $default_allow_custom,
328 esc_html__( 'Allow Custom Donations', 'charitable' ),
329 array(
330 'id' => 'field_' . esc_attr( $this->type ) . '_allow_custom_donations' . '_' . intval( $field_id ), // phpcs:ignore
331 'name' => array( '_fields', intval( $field_id ), 'allow_custom_donations' ),
332 'container_class' => 'charitable-campaign-builder-allow-custom-donations',
333 'field_id' => esc_attr( $field_id ),
334 'checked_value' => '1',
335 )
336 );
337
338 echo $charitable_builder_form_fields->generate_divider( false, false, array( 'field_id' => $field_id ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
339
340 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
341 isset( $settings['css_class'] ) ? $settings['css_class'] : false,
342 esc_html__( 'CSS Class', 'charitable' ),
343 array(
344 'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ),
345 'name' => array( '_fields', intval( $field_id ), 'css_class' ),
346 'field_id' => intval( $field_id ),
347 'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ),
348 )
349 );
350
351 ?>
352
353 <?php
354
355 $html = ob_get_clean();
356
357 return $html;
358 }
359
360 /**
361 * Display settings for AJAX requests.
362 *
363 * @since 1.8.0
364 * @version 1.8.9.1
365 *
366 * @return void
367 */
368 public function settings_display_ajax() {
369 // phpcs:disable WordPress.Security.NonceVerification.Missing
370 $field_id = isset( $_POST['field_id'] ) ? intval( wp_unslash( $_POST['field_id'] ) ) : 0;
371 $campaign_id = isset( $_POST['campaign_id'] ) ? intval( wp_unslash( $_POST['campaign_id'] ) ) : 0; // todo: should this be added? see a few lines down.
372 // phpcs:enable WordPress.Security.NonceVerification.Missing
373
374 if ( ! class_exists( 'Charitable_Builder_Form_Fields' ) ) {
375 wp_send_json_error( esc_html__( 'Something went wrong while performing this action.', 'charitable' ) );
376 }
377
378 $charitable_builder_form_fields = new Charitable_Builder_Form_Fields();
379
380 $campaign_data = get_post_meta( $campaign_id, 'campaign_settings_v2', true );
381 $settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false;
382
383 ob_start();
384
385 ?>
386
387 <?php
388
389 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
390 isset( $settings['headline'] ) ? $settings['headline'] : '',
391 esc_html__( 'Headline', 'charitable' ),
392 array(
393 'id' => 'field_' . esc_attr( $this->type ) . '_headline' . '_' . intval( $field_id ), // phpcs:ignore
394 'name' => array( '_fields', intval( $field_id ), 'headline' ),
395 'field_id' => intval( $field_id ),
396 'tooltip' => esc_html__( 'Add a headline to this field.', 'charitable' ),
397 'class' => 'charitable-campaign-builder-headline',
398 )
399 );
400
401 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
402 $settings['css_class'],
403 esc_html__( 'CSS Class', 'charitable' ),
404 array(
405 'id' => 'field_' . esc_attr( $this->type ) . '_css_class',
406 'name' => array( 'fields', esc_attr( $this->type ), 'css_class' ),
407 'tooltip' => esc_html( $this->tooltip ),
408 )
409 );
410
411 ?>
412
413 <?php
414
415 $html = ob_get_clean();
416
417 wp_send_json_success( [ 'html' => $html ] );
418
419 exit;
420 }
421
422 /**
423 * Enqueue frontend js.
424 *
425 * @since 1.8.0
426 */
427 public function frontend_js() {
428 }
429
430 /**
431 * Enqueue frontend limit option js.
432 *
433 * @since 1.8.0
434 *
435 * @param array $min Min or not.
436 */
437 public function builder_js( $min ) {
438 }
439
440 /**
441 * Amountat and sanitize field.
442 *
443 * @since 1.8.0
444 *
445 * @param int $field_id Field type.
446 * @param mixed $field_submit Field value that was submitted.
447 * @param array $campaign_data Campaign data and settings.
448 */
449 public function format( $field_id, $field_submit, $campaign_data ) {
450 }
451
452 /**
453 * Validate field on form submit.
454 *
455 * @since 1.8.0
456 *
457 * @param int $field_id Field ID.
458 * @param mixed $field_submit Field value that was submitted.
459 * @param array $campaign_data Campaign data and settings.
460 */
461 public function validate( $field_id, $field_submit, $campaign_data ) {
462 }
463
464
465 /**
466 * Return attributes that disable the field button if campaign_data shows one already exists in the template.
467 *
468 * @since 1.8.0
469 *
470 * @param array $field_attr All the settings for this field button.
471 * @param array $field Field data.
472 * @param array $campaign_data Campaign data and settings.
473 * @param array $current_fields Current fields according to saved campaign data.
474 */
475 public function make_unique_button( $field_attr = false, $field = false, $campaign_data = false, $current_fields = false ) {
476
477 if ( false === $field || false === $campaign_data || false === $current_fields || ! is_array( $campaign_data ) || ! is_array( $current_fields ) || empty( $field['type'] ) ) {
478 return $field_attr;
479 }
480
481 if ( ! empty( $field['type'] ) && $field['type'] !== $this->type ) {
482 return $field_attr;
483 }
484
485 // is this already in the template?
486 if ( in_array( $this->type, $current_fields, true ) ) {
487 $field_attr['class'][] = 'charitable-disabled';
488 }
489
490 return $field_attr;
491 }
492
493 /**
494 * Possible depreciated function.
495 *
496 * @since 1.8.0
497 *
498 * @param string $field Field type.
499 * @param array $field_atts Field value that was submitted.
500 * @param array $campaign_data Campaign data and settings.
501 */
502 public function section_top( $field, $field_atts, $campaign_data ) {
503 }
504
505 /**
506 * Possible depreciated function.
507 *
508 * @since 1.8.0
509 *
510 * @param string $field Field type.
511 * @param array $field_atts Field value that was submitted.
512 * @param array $campaign_data Campaign data and settings.
513 */
514 public function section_bottom( $field, $field_atts, $campaign_data ) {
515 }
516 }
517
518 new Charitable_Field_Donate_Amount();
519
520 endif;