PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.2.1
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.2.1
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.2.1, at includes/fields/class-abstract-fields.php

576 lines 16.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 integer $form_id The form id
63 *
64 * @return void
65 */
66 abstract function render( $field_settings, $form_id );
67
68 /**
69 * Get the field option settings for form builder
70 *
71 * @return array
72 */
73 abstract 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 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 = array(
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 boolean|array
111 */
112 public function get_validator() {
113 return false;
114 }
115
116 /**
117 * Check if it's a pro feature
118 *
119 * @return boolean
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 boolean
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 array(
143 'condition_status' => 'no',
144 'cond_field' => array(),
145 'cond_operator' => array( '=' ),
146 'cond_option' => array( __( '- 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 array(
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 boolean $is_meta
181 *
182 * @return array
183 */
184 public static function get_default_option_settings( $is_meta = true, $exclude = array() ) {
185 $common_properties = array(
186 array(
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 array(
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 array(
205 'name' => 'required',
206 'title' => __( 'Required', 'weforms' ),
207 'type' => 'radio',
208 'options' => array(
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 array(
220 'name' => 'width',
221 'title' => __( 'Field Size', 'weforms' ),
222 'type' => 'radio',
223 'options' => array(
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 array(
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.', 'weforms' ),
241 ),
242
243 array(
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[] = array(
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
267 if ( in_array( $option['name'] , $exclude) ) {
268 unset( $common_properties[$key] );
269 }
270 }
271
272 }
273
274 return $common_properties;
275 }
276
277 /**
278 * Common properties of a text input field
279 *
280 * @param boolean $word_restriction
281 *
282 * @return array
283 */
284 public static function get_default_text_option_settings( $word_restriction = false ) {
285 $properties = array(
286 array(
287 'name' => 'placeholder',
288 'title' => __( 'Placeholder text', 'weforms' ),
289 'type' => 'text',
290 'section' => 'advanced',
291 'priority' => 10,
292 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ),
293 ),
294
295 array(
296 'name' => 'default',
297 'title' => __( 'Default value', 'weforms' ),
298 'type' => 'text',
299 'section' => 'advanced',
300 'priority' => 11,
301 'help_text' => __( 'The default value this field will have', 'weforms' ),
302 ),
303
304 array(
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[] = array(
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 boolean $is_multiple
333 *
334 * @return array
335 */
336 public function get_default_option_dropdown_settings( $is_multiple = false ) {
337 return array(
338 'name' => 'options',
339 'title' => __( 'Options', 'wpuf' ),
340 'type' => 'option-data',
341 'is_multiple' => $is_multiple,
342 'section' => 'basic',
343 'priority' => 12,
344 'help_text' => __( 'Add options for the form field', 'wpuf' ),
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 array(
355 array(
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 array(
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 array(
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' => array(
381 'rich' => 'no'
382 )
383 ),
384
385 array(
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 array(
395 'name' => 'rich',
396 'title' => __( 'Textarea', 'weforms' ),
397 'type' => 'radio',
398 'options' => array(
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 array(
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"', $el_name, $class_name, $field_size, $label );
434 }
435
436 /**
437 * Prints form input label
438 *
439 * @param array $attr
440 * @param integer $form_id
441 */
442 function print_label( $field, $form_id = 0 ) {
443 ?>
444 <div class="wpuf-label">
445 <label for="<?php echo isset( $field['name'] ) ? $field['name'] . '_' . $form_id : 'cls'; ?>"><?php echo $field['label'] . $this->required_mark( $field ); ?></label>
446 </div>
447 <?php
448 }
449
450 /**
451 * Check if a field is required
452 *
453 * @param array $field
454 *
455 * @return boolean
456 */
457 public function is_required( $field ) {
458 if ( isset( $field['required'] ) && $field['required'] == 'yes' ) {
459 return true;
460 }
461
462 return false;
463 }
464
465 /**
466 * Prints required field asterisk
467 *
468 * @param array $attr
469 * @return string
470 */
471 function required_mark( $field ) {
472 if ( $this->is_required( $field ) ) {
473 return ' <span class="required">*</span>';
474 }
475 }
476
477 /**
478 * Prints help text for a field
479 *
480 * @param array $field
481 */
482 function help_text( $field ) {
483 if ( empty( $field['help'] ) ) {
484 return;
485 }
486 ?>
487 <span class="wpuf-help"><?php echo stripslashes( $field['help'] ); ?></span>
488 <?php
489 }
490
491 /**
492 * Push logic to conditional array for processing
493 *
494 * @param array $form_field
495 * @param integer $form_id
496 *
497 * @return void
498 */
499 function conditional_logic( $form_field, $form_id ) {
500
501 if ( !isset( $form_field['wpuf_cond']['condition_status'] ) || $form_field['wpuf_cond']['condition_status'] != 'yes' ) {
502 return;
503 }
504
505 $cond_inputs = $form_field['wpuf_cond'];
506 $cond_inputs['condition_status'] = isset( $cond_inputs['condition_status'] ) ? $cond_inputs['condition_status'] : '';
507
508 if ( $cond_inputs['condition_status'] == 'yes') {
509 $cond_inputs['type'] = $form_field['template'];
510 $cond_inputs['name'] = $form_field['name'];
511 $cond_inputs['form_id'] = $form_id;
512 $condition = json_encode( $cond_inputs );
513
514 } else {
515 $condition = '';
516 }
517
518 // for section break
519 if ( $form_field['template'] == 'section_break' ) {
520 $cond_inputs['name'] = $form_field['name'] .'_'. $form_field['id'];
521 $condition = json_encode( $cond_inputs );
522 }
523
524 ?>
525 <script type="text/javascript">
526 wpuf_conditional_items.push(<?php echo $condition; ?>);
527 </script>
528 <?php
529 }
530
531 /**
532 * Prepare entry default, can be replaced through field classes
533 *
534 * @param $field
535 *
536 * @return mixed
537 */
538 public function prepare_entry( $field ) {
539
540 $value = !empty( $_POST[$field['name']] ) ? $_POST[$field['name']] : '';
541
542 if ( is_array( $value ) ) {
543
544 $entry_value = implode( WeForms::$field_separator, $_POST[$field['name']] );
545
546 } else {
547 $entry_value = trim( $value );
548 }
549
550 return $entry_value;
551 }
552
553
554
555 /**
556 * Function to check word restriction
557 *
558 * @param $word_nums number of words allowed
559 */
560 function check_word_restriction_func($word_nums, $rich_text, $field_name) {
561 // bail out if it is dashboard
562 if ( is_admin() ) {
563 return;
564 }
565 ?>
566 <script type="text/javascript">
567 ;(function($) {
568 $(document).ready( function(){
569 WP_User_Frontend.editorLimit.bind(<?php printf( '%d, "%s", "%s"', $word_nums, $field_name, $rich_text ); ?>);
570 });
571 })(jQuery);
572 </script>
573 <?php
574
575 }
576 }