PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.0
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.0
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 1.8.0 All 210 releases
charitable / includes / admin / campaign-builder / fields / class-html.php

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

415 lines 10.7 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 html field 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_HTML' ) ) :
18
19 /**
20 * Class to add campaign html field to a campaign form in the builder.
21 */
22 class Charitable_Field_HTML 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__( 'HTML', 'charitable' );
33 $this->type = 'html';
34 $this->icon = 'fa-code';
35 $this->order = 30;
36
37 // Edit/Duplication information.
38 $this->can_be_edited = true;
39 $this->can_be_deleted = true;
40 $this->can_be_duplicated = true;
41 $this->edit_label = esc_html__( 'Edit HTML', 'charitable' );
42 $this->edit_type = 'html';
43 $this->edit_section = 'standard';
44
45 // Misc.
46 $this->tooltip = '';
47
48 // Define additional field properties.
49 add_action( 'charitable_frontend_js', [ $this, 'frontend_js' ] );
50 add_action( 'charitable_builder_backend_scripts', [ $this, 'builder_js' ], 999 ); // admin_enqueue_scripts
51 }
52
53
54 /**
55 * Field options panel inside the builder.
56 *
57 * @since 1.8.0
58 *
59 * @param array $field Field settings.
60 */
61 public function field_options( $field ) {
62 /*
63 * Basic field options.
64 */
65
66 // Options open markup.
67 }
68
69 /**
70 * Render the field.
71 *
72 * @since 1.8.0
73 *
74 * @param array $field_data Any field data.
75 * @param array $campaign_data Amount data and settings.
76 * @param integer $field_id The field ID.
77 * @param string $mode Where the field is being displayed ("preview" or "template").
78 * @param array $template_data Tempalate data.
79 */
80 public function render( $field_data = false, $campaign_data = false, $field_id = false, $mode = 'template', $template_data = false ) {
81
82 // Define data.
83 $width_percentage = isset( $field['width_percentage'] ) ? absint( $field['width_percentage'] ) : 95;
84 if ( $width_percentage > 95 ) {
85 $width_percentage = 95;
86 }
87 if ( $width_percentage < 10 ) {
88 $width_percentage = 10;
89 }
90
91 $content = ! empty( $field_data['content'] ) ? $field_data['content'] : false;
92
93 $html = '<div class="charitable-field-preview-html" data-field-type="' . $this->type . '">
94 <span class="placeholder">
95 <div class="charitable-preview-notice">
96 <h6><i class="fa fa-code"></i>&nbsp;HTML / Code Block</h6>
97 <p>Contents of this field are not displayed in the campaign builder preview.</p>
98 </div>
99 </span>
100 </div>';
101
102 return $html;
103 }
104
105 /**
106 * HTML preview inside the builder.
107 *
108 * @since 1.8.0
109 *
110 * @param array $field_data Field data and settings.
111 * @param array $campaign_data Campaign data and settings.
112 * @param array $field_id Field ID.
113 * @param string $theme Template data.
114 */
115 public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) {
116
117 $html = $this->field_title( $this->name );
118 $html .= $this->field_wrapper( $this->render( $field_data, $campaign_data, $field_id ), $field_data );
119
120 echo $html;
121 }
122
123 /**
124 * The display on the campaign front-end.
125 *
126 * @since 1.8.0
127 *
128 * @param string $field The passed field type.
129 * @param array $field_data Any field data.
130 * @param array $campaign_data Form data and settings.
131 */
132 public function field_display( $field, $field_data = false, $campaign_data = false ) {
133
134 $campaign = isset( $campaign_data['id'] ) && 0 !== intval( $campaign_data['id'] ) ? charitable_get_campaign( $campaign_data['id'] ) : false;
135
136 if ( ! $campaign ) {
137 return;
138 }
139
140 $html_content = ! empty( $field_data['content'] ) ? $this->format( $field, $field_data['content'], $campaign_data ) : false;
141 $css_class = ! empty( $field_data['css_class'] ) ? ' class="' . esc_html( $field_data['css_class'] ) . '" ' : '';
142
143 ob_start();
144
145 ?>
146
147 <div class="charitable-campaign-field charitable-campaign-field_<?php echo esc_attr( $this->type ); ?>">
148 <div <?php echo esc_attr( $css_class ); ?>>
149 <?php echo $html_content; ?>
150 </div>
151 </div>
152
153 <?php
154
155 $html = ob_get_clean();
156
157 echo apply_filters( 'charitable_campaign_builder_' . $this->type . '_field_display', $html, $campaign_data );
158 }
159
160 /**
161 * HTML display on the form front-end.
162 *
163 * @since 1.8.0
164 *
165 * @param integer $field_id Number ID.
166 * @param array $campaign_data Form data and settings.
167 */
168 public function settings_display( $field_id = false, $campaign_data = false ) {
169
170 $charitable_builder_form_fields = new Charitable_Builder_Form_Fields();
171
172 $settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false;
173
174 ob_start();
175
176 ?>
177
178 <h4 class="charitable-panel-field" data-field-id="<?php echo esc_attr( $field_id ); ?>"><?php echo esc_html( $this->name ); ?> (ID: <?php echo intval( $field_id ); ?>)</h4>
179
180 <?php
181
182 echo $charitable_builder_form_fields->generate_textbox(
183 isset( $settings['content'] ) ? $settings['content'] : false,
184 esc_html__( 'HTML', 'charitable' ),
185 array(
186 'id' => 'field_' . esc_attr( $this->type ) . '_html' . '_' . intval( $field_id ), // phpcs:ignore
187 'name' => array( '_fields', intval( $field_id ), 'content' ),
188 'field_id' => intval( $field_id ),
189 'tooltip' => esc_html( $this->tooltip ),
190 'class' => 'campaign-builder-codeeditor',
191 'rows' => 30,
192 'html' => false,
193 'code' => true,
194 )
195 );
196
197 echo $charitable_builder_form_fields->generate_text(
198 isset( $settings['css_class'] ) ? $settings['css_class'] : false,
199 esc_html__( 'CSS Class', 'charitable' ),
200 array(
201 'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ),
202 'name' => array( '_fields', intval( $field_id ), 'css_class' ),
203 'field_id' => intval( $field_id ),
204 'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ),
205 )
206 );
207
208 ?>
209
210 <?php
211
212 $html = ob_get_clean();
213
214 return $html;
215 }
216
217 /**
218 * Generate field vix ajax.
219 *
220 * @since 1.8.0
221 */
222 public function settings_display_ajax() {
223
224 $field_id = intval( $_POST['field_id'] );
225 $campaign_id = intval( $_POST['campaign_id'] ); // todo: should this be added? see a few lines down.
226
227 $charitable_builder_form_fields = new Charitable_Builder_Form_Fields();
228
229 $campaign_data = get_post_meta( $campaign_id, 'campaign_settings_v2', true );
230 $settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false;
231
232 ob_start();
233
234 ?>
235
236 <?php
237
238 echo $charitable_builder_form_fields->generate_text(
239 $settings['css_class'],
240 esc_html__( 'CSS Class', 'charitable' ),
241 array(
242 'id' => 'field_' . esc_attr( $this->type ) . '_css_class',
243 'name' => array( 'fields', esc_attr( $this->type ), 'css_class' ),
244 'tooltip' => esc_html( $this->tooltip ),
245 )
246 );
247
248 ?>
249
250 <?php
251
252 $html = ob_get_clean();
253
254 wp_send_json_success( [ 'html' => $html ] );
255
256 exit;
257 }
258
259 /**
260 * Enqueue frontend js.
261 *
262 * @since 1.8.0
263 */
264 public function frontend_js() {
265 }
266
267 /**
268 * Enqueue frontend limit option js.
269 *
270 * @since 1.8.0
271 *
272 * @param array $forms Forms on the current page.
273 */
274 public function builder_js( $min = false ) {
275
276 /* CodeMirror editor */
277 $settings = wp_enqueue_code_editor( array( 'type' => 'text/html' ) );
278 if ( $settings ) {
279 wp_localize_script( 'jquery', 'charitable_campaign_builder_code_editor_settings', $settings );
280 }
281
282 /* Load Custom JS */
283
284 wp_enqueue_script(
285 'charitable-field-' . $this->type,
286 charitable()->get_path( 'directory', false ) . 'assets/js/campaign-builder/fields/' . $this->type . "{$min}.js",
287 [
288 'charitable-builder',
289 ],
290 charitable()->get_version()
291 );
292
293 /* Load Custom CSS */
294
295 wp_enqueue_style(
296 'charitable_campaign_builder_field_' . $this->type,
297 charitable()->get_path( 'directory', false ) . 'assets/css/campaign-builder/fields/' . $this->type . "{$min}.css",
298 null,
299 charitable()->get_version()
300 );
301
302 wp_enqueue_style(
303 'wp-codemirror',
304 '/wp-includes/js/codemirror/codemirror.min.css',
305 null,
306 charitable()->get_version()
307 );
308 }
309
310 /**
311 * Format and sanitize field.
312 *
313 * @since 1.8.0
314 *
315 * @param int $field_id Field type.
316 * @param mixed $field_submit Field value that was submitted.
317 * @param array $campaign_data Campaign data and settings.
318 */
319 public function format( $field_id, $field_submit, $campaign_data ) {
320
321 $allowed_html_tags = apply_filters(
322 'charitable_campaign_builder_html_allowed_tags',
323 array(
324 'a' => [
325 'href' => [],
326 'class' => [],
327 'target' => [],
328 ],
329 'p' => [
330 'class' => [],
331 ],
332 'span' => [
333 'class' => [],
334 ],
335 'div' => [
336 'class' => [],
337 ],
338 'strong' => [
339 'class' => [],
340 ],
341 'em' => [
342 'class' => [],
343 ],
344 'b' => [
345 'class' => [],
346 ],
347 'i' => [
348 'class' => [],
349 ],
350 'h1' => [
351 'class' => [],
352 ],
353 'h2' => [
354 'class' => [],
355 ],
356 'h3' => [
357 'class' => [],
358 ],
359 'h4' => [
360 'class' => [],
361 ],
362 'h5' => [
363 'class' => [],
364 ],
365 'h6' => [
366 'class' => [],
367 ],
368 ),
369 $campaign_data
370 );
371
372 return wp_kses( $field_submit, $allowed_html_tags );
373 }
374
375 /**
376 * Validate field on form submit.
377 *
378 * @since 1.8.0
379 *
380 * @param int $field_id Field ID.
381 * @param mixed $field_submit Field value that was submitted.
382 * @param array $campaign_data Campaign data and settings.
383 */
384 public function validate( $field_id, $field_submit, $campaign_data ) {
385 }
386
387 /**
388 * Possible depreciated function.
389 *
390 * @since 1.8.0
391 *
392 * @param string $field Field ID.
393 * @param array $field_atts Field value that was submitted.
394 * @param array $campaign_data Campaign data and settings.
395 */
396 public function section_top( $field, $field_atts, $campaign_data ) {
397 }
398
399 /**
400 * Possible depreciated function.
401 *
402 * @since 1.8.0
403 *
404 * @param string $field Field type.
405 * @param array $field_atts Field value that was submitted.
406 * @param array $campaign_data Campaign data and settings.
407 */
408 public function section_bottom( $field, $field_atts, $campaign_data ) {
409 }
410 }
411
412 new Charitable_Field_HTML();
413
414 endif;
415