PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.26
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.26
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / fields / class-abstract-fields.php

class-abstract-fields.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.6.26, at includes/fields/class-abstract-fields.php

574 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Form field abstract class
5 *
6 * @since 1.1.0
7 */
8 abstract class WeForms_Field_Contract {
9
10 /**
11 * The field name
12 *
13 * @var string
14 */
15 protected $name = '';
16
17 /**
18 * Type of the field
19 *
20 * @var string
21 */
22 protected $input_type = '';
23
24 /**
25 * Icon of the field
26 *
27 * @var string
28 */
29 protected $icon = '';
30
31 /**
32 * Get the name of the field
33 *
34 * @return string
35 */
36 public function get_name() {
37 return $this->name;
38 }
39
40 /**
41 * Get field type
42 *
43 * @return string
44 */
45 public function get_type() {
46 return $this->input_type;
47 }
48
49 /**
50 * Get the fontawesome icon for this field
51 *
52 * @return string
53 */
54 public function get_icon() {
55 return $this->icon;
56 }
57
58 /**
59 * Render of the field in the frontend
60 *
61 * @param array $field_settings The field configuration from the db
62 * @param int $form_id The form id
63 *
64 * @return void
65 */
66 abstract public function render( $field_settings, $form_id );
67
68 /**
69 * Get the field option settings for form builder
70 *
71 * @return array
72 */
73 abstract public function get_options_settings();
74
75 /**
76 * Get the field props
77 *
78 * Field props are the field properties that saves in the database
79 *
80 * @return array
81 */
82 abstract public function get_field_props();
83
84 /**
85 * The JS template for using in the form builder
86 *
87 * @return array
88 */
89 public function get_js_settings() {
90 $settings = [
91 'template' => $this->get_type(),
92 'title' => $this->get_name(),
93 'icon' => $this->get_icon(),
94 'pro_feature' => $this->is_pro(),
95 'settings' => $this->get_options_settings(),
96 'field_props' => $this->get_field_props(),
97 'is_full_width' => $this->is_full_width(),
98 ];
99
100 if ( $validator = $this->get_validator() ) {
101 $settings['validator'] = $validator;
102 }
103
104 return apply_filters( 'weforms_field_get_js_settings', $settings );
105 }
106
107 /**
108 * Custom field validator if exists
109 *
110 * @return bool|array
111 */
112 public function get_validator() {
113 return false;
114 }
115
116 /**
117 * Check if it's a pro feature
118 *
119 * @return bool
120 */
121 public function is_pro() {
122 return false;
123 }
124
125 /**
126 * If this field is full width
127 *
128 * Used in form builder preview (hides the label)
129 *
130 * @return bool
131 */
132 public function is_full_width() {
133 return false;
134 }
135
136 /**
137 * Conditional property for all fields
138 *
139 * @return array
140 */
141 public function default_conditional_prop() {
142 return [
143 'condition_status' => 'no',
144 'cond_field' => [],
145 'cond_operator' => [ '=' ],
146 'cond_option' => [ __( '- select -', 'weforms' ) ],
147 'cond_logic' => 'all',
148 ];
149 }
150
151 /**
152 * Default attributes of a field
153 *
154 * Child classes should use this default setting and extend it by using `get_field_settings()` function
155 *
156 * @return array
157 */
158 public function default_attributes() {
159 return [
160 'template' => $this->get_type(),
161 'name' => '',
162 'label' => $this->get_name(),
163 'required' => 'no',
164 'id' => 0,
165 'width' => 'large',
166 'css' => '',
167 'placeholder' => '',
168 'default' => '',
169 'size' => 40,
170 'help' => '',
171 'is_meta' => 'yes', // wpuf uses it to differentiate meta fields with core fields, maybe removed
172 'is_new' => true, // introduced by @edi, not sure what it does. Have to remove
173 'wpuf_cond' => $this->default_conditional_prop(),
174 ];
175 }
176
177 /**
178 * Common properties for all kinds of fields
179 *
180 * @param bool $is_meta
181 *
182 * @return array
183 */
184 public static function get_default_option_settings( $is_meta = true, $exclude = [] ) {
185 $common_properties = [
186 [
187 'name' => 'label',
188 'title' => __( 'Field Label', 'weforms' ),
189 'type' => 'text',
190 'section' => 'basic',
191 'priority' => 10,
192 'help_text' => __( 'Enter a title of this field', 'weforms' ),
193 ],
194
195 [
196 'name' => 'help',
197 'title' => __( 'Help text', 'weforms' ),
198 'type' => 'text',
199 'section' => 'basic',
200 'priority' => 20,
201 'help_text' => __( 'Give the user some information about this field', 'weforms' ),
202 ],
203
204 [
205 'name' => 'required',
206 'title' => __( 'Required', 'weforms' ),
207 'type' => 'radio',
208 'options' => [
209 'yes' => __( 'Yes', 'weforms' ),
210 'no' => __( 'No', 'weforms' ),
211 ],
212 'section' => 'basic',
213 'priority' => 21,
214 'default' => 'no',
215 'inline' => true,
216 'help_text' => __( 'Check this option to mark the field required. A form will not submit unless all required fields are provided.', 'weforms' ),
217 ],
218
219 [
220 'name' => 'width',
221 'title' => __( 'Field Size', 'weforms' ),
222 'type' => 'radio',
223 'options' => [
224 'small' => __( 'Small', 'weforms' ),
225 'medium' => __( 'Medium', 'weforms' ),
226 'large' => __( 'Large', 'weforms' ),
227 ],
228 'section' => 'advanced',
229 'priority' => 21,
230 'default' => 'large',
231 'inline' => true,
232 ],
233
234 [
235 'name' => 'css',
236 'title' => __( 'CSS Class Name', 'weforms' ),
237 'type' => 'text',
238 'section' => 'advanced',
239 'priority' => 22,
240 'help_text' => __( 'Provide a container class name for this field. Available classes: wpuf-col-half, wpuf-col-half-last, wpuf-col-one-third, wpuf-col-one-third-last', 'weforms' ),
241 ],
242
243 [
244 'name' => 'dynamic',
245 'title' => '',
246 'type' => 'dynamic-field',
247 'section' => 'advanced',
248 'priority' => 23,
249 'help_text' => __( 'Check this option to allow field to be populated dynamically using hooks/query string/shortcode', 'weforms' ),
250 ],
251 ];
252
253 if ( $is_meta ) {
254 $common_properties[] = [
255 'name' => 'name',
256 'title' => __( 'Meta Key', 'weforms' ),
257 'type' => 'text-meta',
258 'section' => 'basic',
259 'priority' => 11,
260 'help_text' => __( 'Name of the meta key this field will save to', 'weforms' ),
261 ];
262 }
263
264 if ( count( $exclude ) ) {
265 foreach ( $common_properties as $key => &$option ) {
266 if ( in_array( $option['name'], $exclude ) ) {
267 unset( $common_properties[$key] );
268 }
269 }
270 }
271
272 return $common_properties;
273 }
274
275 /**
276 * Common properties of a text input field
277 *
278 * @param bool $word_restriction
279 *
280 * @return array
281 */
282 public static function get_default_text_option_settings( $word_restriction = false ) {
283 $properties = [
284 [
285 'name' => 'placeholder',
286 'title' => __( 'Placeholder text', 'weforms' ),
287 'type' => 'text-with-tag',
288 'tag_filter' => 'no_fields', // we don't want to show any fields with merge tags, just basic tags
289 'section' => 'advanced',
290 'priority' => 10,
291 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ),
292 ],
293
294 [
295 'name' => 'default',
296 'title' => __( 'Default value', 'weforms' ),
297 'type' => 'text-with-tag',
298 'tag_filter' => 'no_fields',
299 'section' => 'advanced',
300 'priority' => 11,
301 'help_text' => __( 'The default value this field will have', 'weforms' ),
302 ],
303
304 [
305 'name' => 'size',
306 'title' => __( 'Size', 'weforms' ),
307 'type' => 'text',
308 'variation' => 'number',
309 'section' => 'advanced',
310 'priority' => 20,
311 'help_text' => __( 'Size of this input field', 'weforms' ),
312 ],
313 ];
314
315 if ( $word_restriction ) {
316 $properties[] = [
317 'name' => 'word_restriction',
318 'title' => __( 'Word Restriction', 'weforms' ),
319 'type' => 'text',
320 'section' => 'advanced',
321 'priority' => 15,
322 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ),
323 ];
324 }
325
326 return apply_filters( 'wpuf-form-builder-common-text-fields-properties', $properties );
327 }
328
329 /**
330 * Option data for option based fields
331 *
332 * @param bool $is_multiple
333 *
334 * @return array
335 */
336 public function get_default_option_dropdown_settings( $is_multiple = false ) {
337 return [
338 'name' => 'options',
339 'title' => __( 'Options', 'weforms' ),
340 'type' => 'option-data',
341 'is_multiple' => $is_multiple,
342 'section' => 'basic',
343 'priority' => 12,
344 'help_text' => __( 'Add options for the form field', 'weforms' ),
345 ];
346 }
347
348 /**
349 * Common properties of a textarea field
350 *
351 * @return array
352 */
353 public function get_default_textarea_option_settings() {
354 return [
355 [
356 'name' => 'rows',
357 'title' => __( 'Rows', 'weforms' ),
358 'type' => 'text',
359 'section' => 'advanced',
360 'priority' => 10,
361 'help_text' => __( 'Number of rows in textarea', 'weforms' ),
362 ],
363
364 [
365 'name' => 'cols',
366 'title' => __( 'Columns', 'weforms' ),
367 'type' => 'text',
368 'section' => 'advanced',
369 'priority' => 11,
370 'help_text' => __( 'Number of columns in textarea', 'weforms' ),
371 ],
372
373 [
374 'name' => 'placeholder',
375 'title' => __( 'Placeholder text', 'weforms' ),
376 'type' => 'text',
377 'section' => 'advanced',
378 'priority' => 12,
379 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ),
380 'dependencies' => [
381 'rich' => 'no',
382 ],
383 ],
384
385 [
386 'name' => 'default',
387 'title' => __( 'Default value', 'weforms' ),
388 'type' => 'text',
389 'section' => 'advanced',
390 'priority' => 13,
391 'help_text' => __( 'The default value this field will have', 'weforms' ),
392 ],
393
394 [
395 'name' => 'rich',
396 'title' => __( 'Textarea', 'weforms' ),
397 'type' => 'radio',
398 'options' => [
399 'no' => __( 'Normal', 'weforms' ),
400 'yes' => __( 'Rich textarea', 'weforms' ),
401 'teeny' => __( 'Teeny Rich textarea', 'weforms' ),
402 ],
403 'section' => 'advanced',
404 'priority' => 14,
405 'default' => 'no',
406 ],
407
408 [
409 'name' => 'word_restriction',
410 'title' => __( 'Word Restriction', 'weforms' ),
411 'type' => 'text',
412 'section' => 'advanced',
413 'priority' => 15,
414 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ),
415 ],
416 ];
417 }
418
419 /*
420 |--------------------------------------------------------------------------
421 | Field helper methods
422 |--------------------------------------------------------------------------
423 |
424 | Various helper method for rendering form fields
425 */
426
427 public function print_list_attributes( $field ) {
428 $label = isset( $field['label'] ) ? $field['label'] : '';
429 $el_name = !empty( $field['name'] ) ? $field['name'] : '';
430 $class_name = !empty( $field['css'] ) ? ' ' . $field['css'] : '';
431 $field_size = !empty( $field['width'] ) ? ' field-size-' . $field['width'] : '';
432
433 printf( 'class="wpuf-el %s%s%s" data-label="%s"', esc_attr( $el_name ), esc_attr( $class_name ), esc_attr( $field_size ),
434 esc_attr( $label ) );
435 }
436
437 /**
438 * Prints form input label
439 *
440 * @param array $attr
441 * @param int $form_id
442 */
443 public function print_label( $field, $form_id = 0 ) {
444 ?>
445 <div class="wpuf-label">
446 <label for="<?php echo isset( $field['name'] ) ? esc_attr( $field['name'] ) . '_' . esc_attr( $form_id ) : 'cls'; ?>"><?php echo esc_html( $field['label'] ) . wp_kses_post( $this->required_mark( $field ) ) ; ?></label>
447 </div>
448 <?php
449 }
450
451 /**
452 * Check if a field is required
453 *
454 * @param array $field
455 *
456 * @return bool
457 */
458 public function is_required( $field ) {
459 if ( isset( $field['required'] ) && $field['required'] == 'yes' ) {
460 return true;
461 }
462
463 return false;
464 }
465
466 /**
467 * Prints required field asterisk
468 *
469 * @param array $attr
470 *
471 * @return string
472 */
473 public function required_mark( $field ) {
474 if ( $this->is_required( $field ) ) {
475 return ' <span class="required">*</span>';
476 }
477
478 return '';
479 }
480
481 /**
482 * Prints help text for a field
483 *
484 * @param array $field
485 */
486 public function help_text( $field ) {
487 if ( empty( $field['help'] ) ) {
488 return;
489 }
490 ?>
491 <span class="wpuf-help"><?php echo esc_attr( $field['help'] ); ?></span>
492 <?php
493 }
494
495 /**
496 * Push logic to conditional array for processing
497 *
498 * @param array $form_field
499 * @param int $form_id
500 *
501 * @return void
502 */
503 public function conditional_logic( $form_field, $form_id ) {
504 if ( !isset( $form_field['wpuf_cond']['condition_status'] ) || $form_field['wpuf_cond']['condition_status'] != 'yes' ) {
505 return;
506 }
507
508 $cond_inputs = $form_field['wpuf_cond'];
509 $cond_inputs['condition_status'] = isset( $cond_inputs['condition_status'] ) ? $cond_inputs['condition_status'] : '';
510
511 if ( $cond_inputs['condition_status'] == 'yes' ) {
512 $cond_inputs['type'] = $form_field['template'];
513 $cond_inputs['name'] = $form_field['name'];
514 $cond_inputs['form_id'] = $form_id;
515 $condition = json_encode( $cond_inputs );
516 } else {
517 $condition = '';
518 } ?>
519 <?php
520 $script = "wpuf_conditional_items.push({$condition});";
521 wp_add_inline_script( 'wpuf-form', $script );
522 ?>
523 <?php
524 }
525
526 /**
527 * Prepare entry default, can be replaced through field classes
528 *
529 * @param $field
530 *
531 * @return mixed
532 */
533 public function prepare_entry( $field, $args = [] ) {
534 if( empty( $_POST['_wpnonce'] ) ) {
535 wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) );
536 }
537
538 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) {
539 wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) );
540 }
541
542 $args = ! empty( $args ) ? $args : weforms_clean( $_POST );
543 $value = !empty( $args[$field['name']] ) ? $args[$field['name']] : '';
544
545 if ( is_array( $value ) ) {
546 $entry_value = implode( WeForms::$field_separator, $args[$field['name']] );
547 } else {
548 $entry_value = trim( $value );
549 }
550
551 return $entry_value;
552 }
553
554 /**
555 * Function to check word restriction
556 *
557 * @param $word_nums number of words allowed
558 */
559 public function check_word_restriction_func( $word_nums, $rich_text, $field_name ) {
560 // bail out if it is dashboard
561 if ( is_admin() ) {
562 return;
563 } ?>
564 <script type="text/javascript">
565 ;(function($) {
566 $(document).ready( function(){
567 WP_User_Frontend.editorLimit.bind(<?php printf( '%d, "%s", "%s"', esc_attr( $word_nums ), esc_attr( $field_name ), esc_attr( $rich_text ) ); ?>);
568 });
569 })(jQuery);
570 </script>
571 <?php
572 }
573 }
574