| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to add campaign description 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_Text' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Class to add campaign organizer text field to a campaign form in the builder. |
| 21 |
* |
| 22 |
* @version 1.8.9.1 |
| 23 |
*/ |
| 24 |
class Charitable_Field_Text extends Charitable_Builder_Field { |
| 25 |
|
| 26 |
/** |
| 27 |
* Character Limit to show in preview. |
| 28 |
* |
| 29 |
* @since 1.8.0 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $preview_character_limit = 500; |
| 34 |
|
| 35 |
/** |
| 36 |
* Primary class constructor. |
| 37 |
* |
| 38 |
* @since 1.8.0 |
| 39 |
*/ |
| 40 |
public function init() { |
| 41 |
|
| 42 |
// Define field type information. |
| 43 |
$this->name = esc_html__( 'Text', 'charitable' ); |
| 44 |
$this->type = 'text'; |
| 45 |
$this->icon = 'fa-list-ul'; |
| 46 |
$this->order = 100; |
| 47 |
|
| 48 |
// Edit/Duplication information. |
| 49 |
$this->can_be_edited = true; |
| 50 |
$this->can_be_deleted = true; |
| 51 |
$this->can_be_duplicated = true; |
| 52 |
$this->edit_label = esc_html__( 'Edit Text', 'charitable' ); |
| 53 |
$this->edit_type = 'text'; // was settings. |
| 54 |
$this->edit_section = 'standard'; |
| 55 |
$this->html_field = 'content'; |
| 56 |
|
| 57 |
// Misc. |
| 58 |
$this->tooltip = esc_html__( 'Placeholder tool tip', 'charitable' ); |
| 59 |
$this->preview_character_limit = 2500; |
| 60 |
|
| 61 |
// Define additional field properties. |
| 62 |
add_action( 'charitable_frontend_js', [ $this, 'frontend_js' ] ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Text options panel inside the builder. |
| 67 |
* |
| 68 |
* @since 1.8.0 |
| 69 |
* |
| 70 |
* @param array $field Text settings. |
| 71 |
*/ |
| 72 |
public function field_options( $field ) { |
| 73 |
/* |
| 74 |
* Basic field options. |
| 75 |
*/ |
| 76 |
|
| 77 |
// Options open markup. |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Render the field. |
| 82 |
* |
| 83 |
* @since 1.8.0 |
| 84 |
* |
| 85 |
* @param array $field_data Any field data. |
| 86 |
* @param array $campaign_data Amount data and settings. |
| 87 |
* @param integer $field_id The field ID. |
| 88 |
* @param string $mode Where the field is being displayed ("preview" or "template"). |
| 89 |
* @param array $template_data Tempalate data. |
| 90 |
*/ |
| 91 |
public function render( $field_data = false, $campaign_data = false, $field_id = false, $mode = 'template', $template_data = false ) { |
| 92 |
|
| 93 |
$content = ! empty( $field_data['content'] ) ? ( $field_data['content'] ) : ''; // todo: santitize |
| 94 |
$headline = ! empty( $field_data['headline'] ) ? esc_html( $field_data['headline'] ) : esc_html__( 'Headline', 'charitable' ); |
| 95 |
|
| 96 |
$field_preview_html = '<div class="charitable-field-text" data-field-type="' . $this->type . '"> |
| 97 |
<div class="placeholder"><h5 class="charitable-field-' . $mode . '-headline">' . $headline . '</h5> |
| 98 |
<div class="charitable-campaign-builder-placeholder-' . $mode . '-text charitable-prevent-select">'; |
| 99 |
|
| 100 |
if ( '' !== trim( $content ) ) { |
| 101 |
|
| 102 |
$field_preview_html .= wpautop( |
| 103 |
wp_kses( |
| 104 |
strlen( $content ) > $this->preview_character_limit ? substr( $content, 0, $this->preview_character_limit ) . '...' : $content, |
| 105 |
[ |
| 106 |
'a' => [ |
| 107 |
'href' => [], |
| 108 |
'class' => [], |
| 109 |
'target' => [], |
| 110 |
], |
| 111 |
'p' => [ |
| 112 |
'class' => [], |
| 113 |
], |
| 114 |
'span' => [ |
| 115 |
'class' => [], |
| 116 |
], |
| 117 |
'div' => [ |
| 118 |
'class' => [], |
| 119 |
], |
| 120 |
'strong' => [ |
| 121 |
'class' => [], |
| 122 |
], |
| 123 |
'em' => [ |
| 124 |
'class' => [], |
| 125 |
], |
| 126 |
'b' => [ |
| 127 |
'class' => [], |
| 128 |
], |
| 129 |
'i' => [ |
| 130 |
'class' => [], |
| 131 |
], |
| 132 |
'h1' => [ |
| 133 |
'class' => [], |
| 134 |
], |
| 135 |
'h2' => [ |
| 136 |
'class' => [], |
| 137 |
], |
| 138 |
'h3' => [ |
| 139 |
'class' => [], |
| 140 |
], |
| 141 |
'h4' => [ |
| 142 |
'class' => [], |
| 143 |
], |
| 144 |
'h5' => [ |
| 145 |
'class' => [], |
| 146 |
], |
| 147 |
'h6' => [ |
| 148 |
'class' => [], |
| 149 |
], |
| 150 |
] |
| 151 |
) |
| 152 |
) . '</div>'; |
| 153 |
|
| 154 |
} else { |
| 155 |
|
| 156 |
$field_preview_html .= ' |
| 157 |
<div><p>Add your text here.</p></div> |
| 158 |
</div>'; |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
$field_preview_html .= ' |
| 163 |
</div> |
| 164 |
</div>'; |
| 165 |
|
| 166 |
$html = $field_preview_html; |
| 167 |
|
| 168 |
return $html; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* Text preview inside the builder. |
| 174 |
* |
| 175 |
* @since 1.8.0 |
| 176 |
* |
| 177 |
* @param array $field_data Field data and settings. |
| 178 |
* @param array $campaign_data Campaign data and settings. |
| 179 |
* @param array $field_id Field ID. |
| 180 |
* @param string $theme Template data. |
| 181 |
*/ |
| 182 |
public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) { |
| 183 |
|
| 184 |
$html = $this->field_title( $this->name ); |
| 185 |
$html .= $this->field_wrapper( $this->render( $field_data, $campaign_data, $field_id, 'preview' ), $field_data ); |
| 186 |
|
| 187 |
echo wp_kses_post( $html ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* The display on the campaign front-end. |
| 192 |
* |
| 193 |
* @since 1.8.0 |
| 194 |
* |
| 195 |
* @param string $field_type The passed field type. |
| 196 |
* @param array $field_data Any field data. |
| 197 |
* @param array $campaign_data Amount data and settings. |
| 198 |
*/ |
| 199 |
public function field_display( $field_type = '', $field_data = false, $campaign_data = false ) { |
| 200 |
|
| 201 |
$html = $this->field_display_wrapper( $this->render( $field_data, $campaign_data ), $field_data ); |
| 202 |
|
| 203 |
echo apply_filters( 'charitable_campaign_builder_' . $this->type . '_field_display', $html, $campaign_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* The display on the form settings backend when the user clicks on the field/block. |
| 208 |
* |
| 209 |
* @since 1.8.0 |
| 210 |
* |
| 211 |
* @param array $field_id Field id. |
| 212 |
* @param array $campaign_data Campaign data and settings. |
| 213 |
*/ |
| 214 |
public function settings_display( $field_id = false, $campaign_data = false ) { |
| 215 |
|
| 216 |
$charitable_builder_form_fields = new Charitable_Builder_Form_Fields(); |
| 217 |
|
| 218 |
$settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false; |
| 219 |
|
| 220 |
ob_start(); |
| 221 |
|
| 222 |
?> |
| 223 |
|
| 224 |
<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> |
| 225 |
|
| 226 |
<?php |
| 227 |
|
| 228 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 229 |
isset( $settings['headline'] ) ? $settings['headline'] : esc_html__( 'Headline', 'charitable' ), |
| 230 |
esc_html__( 'Headline', 'charitable' ), |
| 231 |
array( |
| 232 |
'id' => 'field_' . esc_attr( $this->type ) . '_headline' . '_' . intval( $field_id ), // phpcs:ignore |
| 233 |
'name' => array( '_fields', intval( $field_id ), 'headline' ), |
| 234 |
'field_id' => intval( $field_id ), |
| 235 |
'class' => 'charitable-campaign-builder-headline', |
| 236 |
) |
| 237 |
); |
| 238 |
|
| 239 |
echo $charitable_builder_form_fields->generate_textbox( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 240 |
isset( $settings['content'] ) ? $settings['content'] : esc_html__( 'Add your text here', 'charitable' ), |
| 241 |
esc_html__( 'Text', 'charitable' ), |
| 242 |
array( |
| 243 |
'id' => 'field_' . esc_attr( $this->type ) . '_html' . '_' . intval( $field_id ), // phpcs:ignore |
| 244 |
'name' => array( '_fields', intval( $field_id ), 'content' ), |
| 245 |
'field_id' => intval( $field_id ), |
| 246 |
'tooltip' => esc_html( $this->tooltip ), |
| 247 |
'class' => 'campaign-builder-htmleditor', |
| 248 |
'rows' => 30, |
| 249 |
'html' => true, |
| 250 |
'code' => false, |
| 251 |
'special' => 'text', |
| 252 |
) |
| 253 |
); |
| 254 |
|
| 255 |
echo $charitable_builder_form_fields->generate_divider( false, false, array( 'field_id' => intval( $field_id ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 256 |
|
| 257 |
echo $charitable_builder_form_fields->generate_number_slider( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 258 |
isset( $settings['width_percentage'] ) ? $settings['width_percentage'] : 100, |
| 259 |
esc_html__( 'Width', 'charitable' ), |
| 260 |
array( |
| 261 |
'id' => 'field_' . esc_attr( $this->type ) . '_width_percentage' . '_' . intval( $field_id ), // phpcs:ignore |
| 262 |
'name' => array( '_fields', intval( $field_id ), 'width_percentage' ), |
| 263 |
'field_type' => esc_attr( $this->type ), |
| 264 |
'field_id' => intval( $field_id ), |
| 265 |
'symbol' => '%', |
| 266 |
'min' => 0, |
| 267 |
'min_actual' => 10, |
| 268 |
'css_class' => 'charitable-indicator-on-hover', |
| 269 |
'tooltip' => esc_html__( 'Adjust the width of the field within the column.', 'charitable' ), |
| 270 |
) |
| 271 |
); |
| 272 |
|
| 273 |
echo $charitable_builder_form_fields->generate_align( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 274 |
isset( $settings['align'] ) ? $settings['align'] : esc_attr( $this->align_default ), |
| 275 |
esc_html__( 'Align', 'charitable' ), |
| 276 |
array( |
| 277 |
'id' => 'field_' . esc_attr( $this->type ) . '_align' . '_' . intval( $field_id ), // phpcs:ignore |
| 278 |
'name' => array( '_fields', intval( $field_id ), 'align' ), |
| 279 |
'field_id' => intval( $field_id ), |
| 280 |
) |
| 281 |
); |
| 282 |
|
| 283 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 284 |
isset( $settings['css_class'] ) ? $settings['css_class'] : false, |
| 285 |
esc_html__( 'CSS Class', 'charitable' ), |
| 286 |
array( |
| 287 |
'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ), |
| 288 |
'name' => array( '_fields', intval( $field_id ), 'css_class' ), |
| 289 |
'field_id' => intval( $field_id ), |
| 290 |
'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ), |
| 291 |
) |
| 292 |
); |
| 293 |
|
| 294 |
$html = ob_get_clean(); |
| 295 |
|
| 296 |
return $html; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Enqueue frontend limit option js. |
| 301 |
* |
| 302 |
* @since 1.8.0 |
| 303 |
* |
| 304 |
* @param array $forms Forms on the current page. |
| 305 |
*/ |
| 306 |
public function frontend_js( $forms ) { |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Format and sanitize field. |
| 311 |
* |
| 312 |
* @since 1.8.0 |
| 313 |
* |
| 314 |
* @param int $field_id Text ID. |
| 315 |
* @param mixed $field_submit Text value that was submitted. |
| 316 |
* @param array $form_data Form data and settings. |
| 317 |
*/ |
| 318 |
public function format( $field_id, $field_submit, $form_data ) { |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Validate field on form submit. |
| 323 |
* |
| 324 |
* @since 1.8.0 |
| 325 |
* |
| 326 |
* @param int $field_id Text ID. |
| 327 |
* @param mixed $field_submit Text value that was submitted. |
| 328 |
* @param array $form_data Form data and settings. |
| 329 |
*/ |
| 330 |
public function validate( $field_id, $field_submit, $form_data ) { |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
new Charitable_Field_Text(); |
| 335 |
|
| 336 |
endif; |