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