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-progress-bar.php

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

476 lines 17.5 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 a campaign progress bar 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_Progress_Bar' ) ) :
18
19 /**
20 * Class to add campaign progress bar to a campaign form in the builder.
21 */
22 class Charitable_Field_Progress_Bar extends Charitable_Builder_Field {
23
24 /**
25 * Primary class constructor.
26 *
27 * @since 1.8.0
28 */
29 public function init() {
30
31 // Define field type information.
32 $this->name = esc_html__( 'Progress Bar', 'charitable' );
33 $this->type = 'progress-bar';
34 $this->icon = 'fa-arrows-h';
35 $this->order = 30;
36
37 $this->align_default = 'center';
38
39 // Edit/Duplication information.
40 $this->edit_label = esc_html__( 'Edit Progress Bar', 'charitable' );
41 $this->edit_type = 'progress-bar';
42 $this->edit_section = 'standard';
43 $this->can_be_edited = true;
44 $this->can_be_deleted = true;
45 $this->can_be_duplicated = true;
46
47 // Misc.
48 $this->tooltip = '';
49
50 // Define additional field properties.
51 add_action( 'charitable_builder_frontend_js', [ $this, 'frontend_js' ] );
52 add_action( 'charitable_builder_backend_scripts', [ $this, 'builder_js' ] );
53
54 // Defaults.
55 add_filter( 'charitable_field_new_default', [ $this, 'new_field_defaults' ] );
56 }
57
58 /**
59 * Set default values for the field.
60 *
61 * @since 1.8.0
62 *
63 * @param array $field Field settings.
64 */
65 public function new_field_defaults( $field ) {
66
67 if ( isset( $field['type'] ) && 'progress-bar' !== $field['type'] ) {
68 return $field;
69 }
70
71 $field['show_donated'] = 'show_donated';
72 $field['show_goal'] = 'show_goal';
73
74 return $field;
75 }
76
77
78 /**
79 * ProgressBar options panel inside the builder.
80 *
81 * @since 1.8.0
82 *
83 * @param array $field ProgressBar settings.
84 */
85 public function field_options( $field ) {
86 /*
87 * Basic field options.
88 */
89
90 // Options open markup.
91 }
92
93 /**
94 * Render the field.
95 *
96 * @since 1.8.0
97 * @version 1.8.1.12
98 * @version 1.8.4.2 updated infinity display / goal.
99 * @version 1.8.6 added round_donation option.
100 *
101 * @param array $field_data Any field data.
102 * @param array $campaign_data Amount data and settings.
103 * @param integer $field_id The field ID.
104 * @param string $mode Where the field is being displayed ("preview" or "template").
105 * @param array $template_data Tempalate data.
106 */
107 public function render( $field_data = false, $campaign_data = false, $field_id = false, $mode = 'template', $template_data = false ) {
108
109 $show_donated_css = false;
110 $show_goal_css = false;
111 $headline = ! empty( $field_data['headline'] ) ? '<h5 class="charitable-field-preview-headline">' . esc_html( $field_data['headline'] ) . '</h5>' : '';
112
113 $goal = isset( $campaign_data['id'] ) ? get_post_meta( $campaign_data['id'], '_campaign_goal', true ) : false;
114 if ( ! $goal || 0 === intval( $goal ) ) {
115 $goal = '∞';
116 } else {
117 $goal = Charitable_Currency::get_instance()->get_sanitized_and_localized_amount( $goal );
118 $goal = charitable_get_currency_helper()->get_monetary_amount( $goal ); // Add appropriate currency to the goal.
119 }
120 $goal = ! empty( $field_data['label_goal'] ) ? '<span>' . esc_html( $field_data['label_goal'] ) . '</span> ' . $goal : '<span>' . esc_html__( 'Goal: ', 'charitable' ) . '</span> ' . $goal;
121 $goal = apply_filters( 'charitable_campaign_builder_progress_bar_goal', $goal, $field_data, $campaign_data );
122
123 $campaign_id = ! empty( $campaign_data['id'] ) ? intval( $campaign_data['id'] ) : false;
124 $campaign = 0 === intval( $campaign_id ) ? false : charitable_get_campaign( $campaign_id );
125 $amount_donated = false !== $campaign ? $campaign->get_donated_amount_formatted() : charitable_format_money( '0' );
126 $percent = false !== $campaign ? $campaign->get_percent_donated() : 0;
127 // Round the percent to 0 decimal places if the round_donation is checked. But if there is a percent and it's below 1, round to 1.
128 if ( isset( $field_data['round_donation'] ) && $field_data['round_donation'] ) {
129 // remove the % sign from the percent.
130 $percent = str_replace( '%', '', $percent );
131 if ( $percent < 1 ) {
132 $percent = 1;
133 } else {
134 $percent = round( $percent, 0 );
135 }
136 $percent = $percent . '%';
137 }
138
139 $label_donated = ! empty( $field_data['label_donate'] ) ? '<span>' . esc_html( $field_data['label_donate'] ) . '</span> ' : '<span>' . esc_html__( 'Donated: ', 'charitable' ) . '</span> ';
140 $show_donated = $label_donated . $amount_donated;
141 $show_progress_bar = false;
142
143 if ( false !== $campaign ) {
144 if ( ! $campaign->has_goal() ) {
145 $show_progress_bar = false;
146 } else {
147 $show_donated = $label_donated . $percent;
148 $show_progress_bar = true;
149 }
150 }
151
152 // if the "show_goal" is set to "show_goal" then show the goal, otherise do not but hidding it by setting 'charitable-hidden' class.
153 $show_donated_css = ! empty( $field_data ) &&
154 (
155 ( is_array( $field_data['show_hide'] ) && ! empty( $field_data['show_hide'] ) && in_array( 'show_donated', $field_data['show_hide'] ) ) || // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
156 ( is_string( $field_data['show_donated'] ) && 'show_donated' === $field_data['show_donated'] )
157 )
158 ? '' : 'charitable-hidden';
159 $show_goal_css = ! empty( $field_data ) &&
160
161 (
162 ( is_array( $field_data['show_hide'] ) && ! empty( $field_data['show_hide'] ) && in_array( 'show_goal', $field_data['show_hide'] ) ) || // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
163 ( is_string( $field_data['show_goal'] ) && 'show_goal' === $field_data['show_goal'] )
164 )
165 ? '' : 'charitable-hidden';
166
167 $progress_bar_css = $show_progress_bar ? '' : ' charitable-campaign-preview-not-available charitable-hidden';
168
169 $width = ( 'preview' === $mode ) ? $this->keep_in_range( absint( $percent ), 5, 100 ) : $this->keep_in_range( absint( $percent ), 0, 100 );
170
171 $preview_text = '';
172
173 if ( isset( $field_data['meta_position'] ) && 'top' === $field_data['meta_position'] ) {
174
175 $preview_text .= '<div class="placeholder charitable-placeholder meta-top">' . $headline;
176 $preview_text .= '<div class="progress-bar-info-row">';
177 $preview_text .= '<div class="campaign-percent-raised ' . $show_donated_css . '">' . $show_donated . '</div><div class="campaign-goal ' . $show_goal_css . '"> ' . $goal . '</div></div>';
178 $preview_text .= '<div class="progress' . $progress_bar_css . '"><div class="progress-bar" style="width: ' . $width . '%"><span></span></div></div>';
179 $preview_text .= '</div>';
180
181 } else {
182
183 $preview_text .= '<div class="placeholder charitable-placeholder meta-bottom">' . $headline;
184 $preview_text .= '<div class="progress' . $progress_bar_css . '"><div class="progress-bar" style="width: ' . $width . '%"><span></span></div></div>';
185 $preview_text .= '<div class="progress-bar-info-row">';
186 $preview_text .= '<div class="campaign-percent-raised ' . $show_donated_css . '">' . $show_donated . '</div><div class="campaign-goal ' . $show_goal_css . '"> ' . $goal . '</div></div>';
187 $preview_text .= '</div>';
188
189 }
190
191 return $preview_text;
192 }
193
194 /**
195 * ProgressBar preview inside the builder.
196 *
197 * @since 1.8.0
198 *
199 * @param array $field_data Field data and settings.
200 * @param array $campaign_data Campaign data and settings.
201 * @param array $field_id Field ID.
202 * @param string $theme Template data.
203 */
204 public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) {
205
206 $html = $this->field_title( $this->name );
207 $html .= $this->field_wrapper( $this->render( $field_data, $campaign_data, $field_id, 'preview' ), $field_data );
208
209 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
210 }
211
212 /**
213 * ProgressBar display on the form front-end.
214 *
215 * @since 1.8.0
216 *
217 * @param string $field_type Field Type.
218 * @param array $field_data Deprecated.
219 * @param array $campaign_data Form data and settings.
220 */
221 public function field_display( $field_type = '', $field_data = false, $campaign_data = false ) {
222
223 $html = $this->field_display_wrapper( $this->render( $field_data, $campaign_data ), $field_data );
224
225 echo apply_filters( 'charitable_campaign_builder_' . $this->type . '_field_display', $html, $campaign_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
226 }
227
228 /**
229 * The display on the form settings backend when the user clicks on the field/block.
230 *
231 * @since 1.8.0
232 *
233 * @param array $field_id Social Links settings.
234 * @param array $campaign_data Campaign data and settings.
235 */
236 public function settings_display( $field_id = false, $campaign_data = false ) {
237
238 $charitable_builder_form_fields = new Charitable_Builder_Form_Fields();
239
240 $settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false;
241
242 ob_start();
243
244 ?>
245
246 <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>
247
248 <?php
249
250 echo $charitable_builder_form_fields->generate_headline( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
251 esc_html__( 'Note: Progress bars will be visible only when there is a goal set for the campaign.', 'charitable' ),
252 array(
253 'field_id' => esc_attr( $field_id ),
254 'class' => 'charitable-campaign-builder-headline-light',
255 )
256 );
257
258 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
259 isset( $settings['headline'] ) ? $settings['headline'] : false,
260 esc_html__( 'Headline', 'charitable' ),
261 array(
262 'id' => 'field_' . esc_attr( $this->type ) . '_headline' . '_' . intval( $field_id ), // phpcs:ignore
263 'name' => array( '_fields', intval( $field_id ), 'headline' ),
264 'field_id' => intval( $field_id ),
265 'class' => 'charitable-campaign-builder-headline',
266 )
267 );
268
269 echo $charitable_builder_form_fields->generate_toggles( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
270 isset( $settings['show_hide'] ) ? $settings['show_hide'] : false,
271 esc_html__( 'Campaign Information', 'charitable' ),
272 array(
273 'id' => 'field_' . esc_attr( $this->type ) . '_show_hide' . '_' . intval( $field_id ), // phpcs:ignore
274 'name' => array( '_fields', intval( $field_id ), 'show_hide' ),
275 'field_id' => esc_attr( $field_id ),
276 'tooltip' => esc_html( $this->tooltip ),
277 'container_class' => 'charitable-campaign-builder-progress-bar',
278 'options' => array(
279 'Show Donated' => 'show_donated',
280 'Show Goal' => 'show_goal',
281 ),
282 'default' => array(
283 'show_donated',
284 'show_goal',
285 ),
286 'use_defaults' => ( false === $settings ) ? true : false,
287 )
288 );
289
290 /* 1.8.6.1 */
291 echo $charitable_builder_form_fields->generate_checkbox( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
292 isset( $settings['round_donation'] ) ? $settings['round_donation'] : false,
293 esc_html__( 'Round Amounts', 'charitable' ),
294 array(
295 'id' => 'field_' . esc_attr( $this->type ) . '_round_donation' . '_' . intval( $field_id ), // phpcs:ignore
296 'name' => array( '_fields', intval( $field_id ), 'round_donation' ),
297 'checked_value' => 'show',
298 'field_id' => intval( $field_id ),
299 'value' => '1',
300 )
301 );
302
303 echo $charitable_builder_form_fields->generate_hidden_field( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
304 isset( $settings['meta_position'] ) ? $settings['meta_position'] : false,
305 array(
306 'id' => 'field_' . esc_attr( $this->type ) . '_meta_position' . '_' . intval( $field_id ), // phpcs:ignore
307 'name' => array( '_fields', intval( $field_id ), 'meta_position' ),
308 'field_id' => intval( $field_id ),
309 )
310 );
311
312 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
313 isset( $settings['label_donate'] ) ? $settings['label_donate'] : esc_html__( 'Donated:', 'charitable' ),
314 esc_html__( 'Donate Label:', 'charitable' ),
315 array(
316 'id' => 'field_' . esc_attr( $this->type ) . '_label_donate' . '_' . intval( $field_id ), // phpcs:ignore
317 'name' => array( '_fields', intval( $field_id ), 'label_donate' ),
318 'field_id' => intval( $field_id ),
319 'class' => 'donate_label',
320 )
321 );
322
323 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
324 isset( $settings['label_goal'] ) ? $settings['label_goal'] : esc_html__( 'Goal:', 'charitable' ),
325 esc_html__( 'Goal Label:', 'charitable' ),
326 array(
327 'id' => 'field_' . esc_attr( $this->type ) . '_label_goal' . '_' . intval( $field_id ), // phpcs:ignore
328 'name' => array( '_fields', intval( $field_id ), 'label_goal' ),
329 'field_id' => intval( $field_id ),
330 'class' => 'donate_goal',
331 )
332 );
333
334 echo $charitable_builder_form_fields->generate_divider( false, false, array( 'field_id' => $field_id ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
335
336 echo $charitable_builder_form_fields->generate_number_slider( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
337 isset( $settings['width_percentage'] ) ? $settings['width_percentage'] : 100,
338 esc_html__( 'Width', 'charitable' ),
339 array(
340 'id' => 'field_' . esc_attr( $this->type ) . '_width_percentage' . '_' . intval( $field_id ), // phpcs:ignore
341 'name' => array( '_fields', intval( $field_id ), 'width_percentage' ),
342 'field_type' => esc_attr( $this->type ),
343 'field_id' => intval( $field_id ),
344 'css_class' => 'charitable-indicator-on-hover',
345 'symbol' => '%',
346 'min' => 0,
347 'min_actual' => 30,
348 'tooltip' => esc_html__( 'Adjust the width of the field within the column.', 'charitable' ),
349 )
350 );
351
352 echo $charitable_builder_form_fields->generate_align( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
353 isset( $settings['align'] ) ? $settings['align'] : esc_attr( $this->align_default ),
354 esc_html__( 'Align', 'charitable' ),
355 array(
356 'id' => 'field_' . esc_attr( $this->type ) . '_align' . '_' . intval( $field_id ), // phpcs:ignore
357 'name' => array( '_fields', intval( $field_id ), 'align' ),
358 'field_id' => intval( $field_id ),
359 )
360 );
361
362 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
363 isset( $settings['css_class'] ) ? $settings['css_class'] : false,
364 esc_html__( 'CSS Class', 'charitable' ),
365 array(
366 'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ), // phpcs:ignore
367 'name' => array( '_fields', intval( $field_id ), 'css_class' ),
368 'field_id' => intval( $field_id ),
369 'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ),
370 )
371 );
372
373 ?>
374
375 <?php
376
377 $html = ob_get_clean();
378
379 return $html;
380 }
381
382 /**
383 * Return HTML for display via ajax.
384 *
385 * @since 1.8.0
386 */
387 public function settings_display_ajax() {
388
389 $field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : false; // phpcs:ignore
390 $campaign_id = isset( $_POST['campaign_id'] ) ? intval( $_POST['campaign_id'] ) : false; // phpcs:ignore
391
392 if ( false === $field_id ) {
393 return;
394 }
395
396 $charitable_builder_form_fields = new Charitable_Builder_Form_Fields();
397
398 $campaign_data = get_post_meta( $campaign_id, 'campaign_settings_v2', true );
399 $settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false;
400
401 ob_start();
402
403 ?>
404
405 <?php
406
407 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore
408 $settings['css_class'],
409 esc_html__( 'CSS Class', 'charitable' ),
410 array(
411 'id' => 'field_' . esc_attr( $this->type ) . '_css_class',
412 'name' => array( 'fields', esc_attr( $this->type ), 'css_class' ),
413 'tooltip' => esc_html( $this->tooltip ),
414 )
415 );
416
417 ?>
418
419 <?php
420
421 $html = ob_get_clean();
422
423 wp_send_json_success( [ 'html' => $html ] );
424
425 exit;
426 }
427
428 /**
429 * Enqueue frontend limit option js.
430 *
431 * @since 1.8.0
432 *
433 * @param array $min Min string.
434 */
435 public function builder_js( $min ) {
436 }
437
438 /**
439 * Enqueue frontend limit option js.
440 *
441 * @since 1.8.0
442 */
443 public function frontend_js() {
444 }
445
446 /**
447 * Format and sanitize field.
448 *
449 * @since 1.8.0
450 *
451 * @param int $field_id Field type.
452 * @param mixed $field_submit Field value that was submitted.
453 * @param array $campaign_data Campaign data and settings.
454 */
455 public function format( $field_id, $field_submit, $campaign_data ) {
456 }
457
458 /**
459 * Validate field on form submit.
460 *
461 * @since 1.8.0
462 *
463 * @param int $field_id Field ID.
464 * @param mixed $field_submit Field value that was submitted.
465 * @param array $campaign_data Campaign data and settings.
466 */
467 public function validate( $field_id, $field_submit, $campaign_data ) {
468 }
469 }
470
471 if ( is_admin() ) {
472 new Charitable_Field_Progress_Bar();
473 }
474
475 endif;
476