PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
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.13, at includes/admin/campaign-builder/fields/class-progress-bar.php

479 lines 17.8 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 // A newly dragged-in field has neither 'show_hide' (legacy multi-check key) nor the
154 // newer 'show_donated'/'show_goal' scalars until it is first saved, so every key is
155 // checked with isset()/! empty() before use.
156 $show_donated_css = ! empty( $field_data ) &&
157 (
158 ( ! empty( $field_data['show_hide'] ) && is_array( $field_data['show_hide'] ) && in_array( 'show_donated', $field_data['show_hide'] ) ) || // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
159 ( isset( $field_data['show_donated'] ) && is_string( $field_data['show_donated'] ) && 'show_donated' === $field_data['show_donated'] )
160 )
161 ? '' : 'charitable-hidden';
162 $show_goal_css = ! empty( $field_data ) &&
163
164 (
165 ( ! empty( $field_data['show_hide'] ) && is_array( $field_data['show_hide'] ) && in_array( 'show_goal', $field_data['show_hide'] ) ) || // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
166 ( isset( $field_data['show_goal'] ) && is_string( $field_data['show_goal'] ) && 'show_goal' === $field_data['show_goal'] )
167 )
168 ? '' : 'charitable-hidden';
169
170 $progress_bar_css = $show_progress_bar ? '' : ' charitable-campaign-preview-not-available charitable-hidden';
171
172 $width = ( 'preview' === $mode ) ? $this->keep_in_range( absint( $percent ), 5, 100 ) : $this->keep_in_range( absint( $percent ), 0, 100 );
173
174 $preview_text = '';
175
176 if ( isset( $field_data['meta_position'] ) && 'top' === $field_data['meta_position'] ) {
177
178 $preview_text .= '<div class="placeholder charitable-placeholder meta-top">' . $headline;
179 $preview_text .= '<div class="progress-bar-info-row">';
180 $preview_text .= '<div class="campaign-percent-raised ' . $show_donated_css . '">' . $show_donated . '</div><div class="campaign-goal ' . $show_goal_css . '"> ' . $goal . '</div></div>';
181 $preview_text .= '<div class="progress' . $progress_bar_css . '"><div class="progress-bar" style="width: ' . $width . '%"><span></span></div></div>';
182 $preview_text .= '</div>';
183
184 } else {
185
186 $preview_text .= '<div class="placeholder charitable-placeholder meta-bottom">' . $headline;
187 $preview_text .= '<div class="progress' . $progress_bar_css . '"><div class="progress-bar" style="width: ' . $width . '%"><span></span></div></div>';
188 $preview_text .= '<div class="progress-bar-info-row">';
189 $preview_text .= '<div class="campaign-percent-raised ' . $show_donated_css . '">' . $show_donated . '</div><div class="campaign-goal ' . $show_goal_css . '"> ' . $goal . '</div></div>';
190 $preview_text .= '</div>';
191
192 }
193
194 return $preview_text;
195 }
196
197 /**
198 * ProgressBar preview inside the builder.
199 *
200 * @since 1.8.0
201 *
202 * @param array $field_data Field data and settings.
203 * @param array $campaign_data Campaign data and settings.
204 * @param array $field_id Field ID.
205 * @param string $theme Template data.
206 */
207 public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) {
208
209 $html = $this->field_title( $this->name );
210 $html .= $this->field_wrapper( $this->render( $field_data, $campaign_data, $field_id, 'preview' ), $field_data );
211
212 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
213 }
214
215 /**
216 * ProgressBar display on the form front-end.
217 *
218 * @since 1.8.0
219 *
220 * @param string $field_type Field Type.
221 * @param array $field_data Deprecated.
222 * @param array $campaign_data Form data and settings.
223 */
224 public function field_display( $field_type = '', $field_data = false, $campaign_data = false ) {
225
226 $html = $this->field_display_wrapper( $this->render( $field_data, $campaign_data ), $field_data );
227
228 echo apply_filters( 'charitable_campaign_builder_' . $this->type . '_field_display', $html, $campaign_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
229 }
230
231 /**
232 * The display on the form settings backend when the user clicks on the field/block.
233 *
234 * @since 1.8.0
235 *
236 * @param array $field_id Social Links settings.
237 * @param array $campaign_data Campaign data and settings.
238 */
239 public function settings_display( $field_id = false, $campaign_data = false ) {
240
241 $charitable_builder_form_fields = new Charitable_Builder_Form_Fields();
242
243 $settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false;
244
245 ob_start();
246
247 ?>
248
249 <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>
250
251 <?php
252
253 echo $charitable_builder_form_fields->generate_headline( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
254 esc_html__( 'Note: Progress bars will be visible only when there is a goal set for the campaign.', 'charitable' ),
255 array(
256 'field_id' => esc_attr( $field_id ),
257 'class' => 'charitable-campaign-builder-headline-light',
258 )
259 );
260
261 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
262 isset( $settings['headline'] ) ? $settings['headline'] : false,
263 esc_html__( 'Headline', 'charitable' ),
264 array(
265 'id' => 'field_' . esc_attr( $this->type ) . '_headline' . '_' . intval( $field_id ), // phpcs:ignore
266 'name' => array( '_fields', intval( $field_id ), 'headline' ),
267 'field_id' => intval( $field_id ),
268 'class' => 'charitable-campaign-builder-headline',
269 )
270 );
271
272 echo $charitable_builder_form_fields->generate_toggles( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
273 isset( $settings['show_hide'] ) ? $settings['show_hide'] : false,
274 esc_html__( 'Campaign Information', 'charitable' ),
275 array(
276 'id' => 'field_' . esc_attr( $this->type ) . '_show_hide' . '_' . intval( $field_id ), // phpcs:ignore
277 'name' => array( '_fields', intval( $field_id ), 'show_hide' ),
278 'field_id' => esc_attr( $field_id ),
279 'tooltip' => esc_html( $this->tooltip ),
280 'container_class' => 'charitable-campaign-builder-progress-bar',
281 'options' => array(
282 'Show Donated' => 'show_donated',
283 'Show Goal' => 'show_goal',
284 ),
285 'default' => array(
286 'show_donated',
287 'show_goal',
288 ),
289 'use_defaults' => ( false === $settings ) ? true : false,
290 )
291 );
292
293 /* 1.8.6.1 */
294 echo $charitable_builder_form_fields->generate_checkbox( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
295 isset( $settings['round_donation'] ) ? $settings['round_donation'] : false,
296 esc_html__( 'Round Amounts', 'charitable' ),
297 array(
298 'id' => 'field_' . esc_attr( $this->type ) . '_round_donation' . '_' . intval( $field_id ), // phpcs:ignore
299 'name' => array( '_fields', intval( $field_id ), 'round_donation' ),
300 'checked_value' => 'show',
301 'field_id' => intval( $field_id ),
302 'value' => '1',
303 )
304 );
305
306 echo $charitable_builder_form_fields->generate_hidden_field( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
307 isset( $settings['meta_position'] ) ? $settings['meta_position'] : false,
308 array(
309 'id' => 'field_' . esc_attr( $this->type ) . '_meta_position' . '_' . intval( $field_id ), // phpcs:ignore
310 'name' => array( '_fields', intval( $field_id ), 'meta_position' ),
311 'field_id' => intval( $field_id ),
312 )
313 );
314
315 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
316 isset( $settings['label_donate'] ) ? $settings['label_donate'] : esc_html__( 'Donated:', 'charitable' ),
317 esc_html__( 'Donate Label:', 'charitable' ),
318 array(
319 'id' => 'field_' . esc_attr( $this->type ) . '_label_donate' . '_' . intval( $field_id ), // phpcs:ignore
320 'name' => array( '_fields', intval( $field_id ), 'label_donate' ),
321 'field_id' => intval( $field_id ),
322 'class' => 'donate_label',
323 )
324 );
325
326 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
327 isset( $settings['label_goal'] ) ? $settings['label_goal'] : esc_html__( 'Goal:', 'charitable' ),
328 esc_html__( 'Goal Label:', 'charitable' ),
329 array(
330 'id' => 'field_' . esc_attr( $this->type ) . '_label_goal' . '_' . intval( $field_id ), // phpcs:ignore
331 'name' => array( '_fields', intval( $field_id ), 'label_goal' ),
332 'field_id' => intval( $field_id ),
333 'class' => 'donate_goal',
334 )
335 );
336
337 echo $charitable_builder_form_fields->generate_divider( false, false, array( 'field_id' => $field_id ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
338
339 echo $charitable_builder_form_fields->generate_number_slider( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
340 isset( $settings['width_percentage'] ) ? $settings['width_percentage'] : 100,
341 esc_html__( 'Width', 'charitable' ),
342 array(
343 'id' => 'field_' . esc_attr( $this->type ) . '_width_percentage' . '_' . intval( $field_id ), // phpcs:ignore
344 'name' => array( '_fields', intval( $field_id ), 'width_percentage' ),
345 'field_type' => esc_attr( $this->type ),
346 'field_id' => intval( $field_id ),
347 'css_class' => 'charitable-indicator-on-hover',
348 'symbol' => '%',
349 'min' => 0,
350 'min_actual' => 30,
351 'tooltip' => esc_html__( 'Adjust the width of the field within the column.', 'charitable' ),
352 )
353 );
354
355 echo $charitable_builder_form_fields->generate_align( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
356 isset( $settings['align'] ) ? $settings['align'] : esc_attr( $this->align_default ),
357 esc_html__( 'Align', 'charitable' ),
358 array(
359 'id' => 'field_' . esc_attr( $this->type ) . '_align' . '_' . intval( $field_id ), // phpcs:ignore
360 'name' => array( '_fields', intval( $field_id ), 'align' ),
361 'field_id' => intval( $field_id ),
362 )
363 );
364
365 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
366 isset( $settings['css_class'] ) ? $settings['css_class'] : false,
367 esc_html__( 'CSS Class', 'charitable' ),
368 array(
369 'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ), // phpcs:ignore
370 'name' => array( '_fields', intval( $field_id ), 'css_class' ),
371 'field_id' => intval( $field_id ),
372 'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ),
373 )
374 );
375
376 ?>
377
378 <?php
379
380 $html = ob_get_clean();
381
382 return $html;
383 }
384
385 /**
386 * Return HTML for display via ajax.
387 *
388 * @since 1.8.0
389 */
390 public function settings_display_ajax() {
391
392 $field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : false; // phpcs:ignore
393 $campaign_id = isset( $_POST['campaign_id'] ) ? intval( $_POST['campaign_id'] ) : false; // phpcs:ignore
394
395 if ( false === $field_id ) {
396 return;
397 }
398
399 $charitable_builder_form_fields = new Charitable_Builder_Form_Fields();
400
401 $campaign_data = get_post_meta( $campaign_id, 'campaign_settings_v2', true );
402 $settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false;
403
404 ob_start();
405
406 ?>
407
408 <?php
409
410 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore
411 $settings['css_class'],
412 esc_html__( 'CSS Class', 'charitable' ),
413 array(
414 'id' => 'field_' . esc_attr( $this->type ) . '_css_class',
415 'name' => array( 'fields', esc_attr( $this->type ), 'css_class' ),
416 'tooltip' => esc_html( $this->tooltip ),
417 )
418 );
419
420 ?>
421
422 <?php
423
424 $html = ob_get_clean();
425
426 wp_send_json_success( [ 'html' => $html ] );
427
428 exit;
429 }
430
431 /**
432 * Enqueue frontend limit option js.
433 *
434 * @since 1.8.0
435 *
436 * @param array $min Min string.
437 */
438 public function builder_js( $min ) {
439 }
440
441 /**
442 * Enqueue frontend limit option js.
443 *
444 * @since 1.8.0
445 */
446 public function frontend_js() {
447 }
448
449 /**
450 * Format and sanitize field.
451 *
452 * @since 1.8.0
453 *
454 * @param int $field_id Field type.
455 * @param mixed $field_submit Field value that was submitted.
456 * @param array $campaign_data Campaign data and settings.
457 */
458 public function format( $field_id, $field_submit, $campaign_data ) {
459 }
460
461 /**
462 * Validate field on form submit.
463 *
464 * @since 1.8.0
465 *
466 * @param int $field_id Field ID.
467 * @param mixed $field_submit Field value that was submitted.
468 * @param array $campaign_data Campaign data and settings.
469 */
470 public function validate( $field_id, $field_submit, $campaign_data ) {
471 }
472 }
473
474 if ( is_admin() ) {
475 new Charitable_Field_Progress_Bar();
476 }
477
478 endif;
479