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

579 lines 17.0 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-with-tag',
290 'tag_filter' => 'no_fields', // we don't want to show any fields with merge tags, just basic tags
291 'section' => 'advanced',
292 'priority' => 10,
293 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ),
294 ),
295
296 array(
297 'name' => 'default',
298 'title' => __( 'Default value', 'weforms' ),
299 'type' => 'text-with-tag',
300 'tag_filter' => 'no_fields',
301 'section' => 'advanced',
302 'priority' => 11,
303 'help_text' => __( 'The default value this field will have', 'weforms' ),
304 ),
305
306 array(
307 'name' => 'size',
308 'title' => __( 'Size', 'weforms' ),
309 'type' => 'text',
310 'variation' => 'number',
311 'section' => 'advanced',
312 'priority' => 20,
313 'help_text' => __( 'Size of this input field', 'weforms' ),
314 ),
315 );
316
317 if ( $word_restriction ) {
318 $properties[] = array(
319 'name' => 'word_restriction',
320 'title' => __( 'Word Restriction', 'weforms' ),
321 'type' => 'text',
322 'section' => 'advanced',
323 'priority' => 15,
324 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ),
325 );
326 }
327
328 return apply_filters( 'wpuf-form-builder-common-text-fields-properties', $properties );
329 }
330
331 /**
332 * Option data for option based fields
333 *
334 * @param boolean $is_multiple
335 *
336 * @return array
337 */
338 public function get_default_option_dropdown_settings( $is_multiple = false ) {
339 return array(
340 'name' => 'options',
341 'title' => __( 'Options', 'weforms' ),
342 'type' => 'option-data',
343 'is_multiple' => $is_multiple,
344 'section' => 'basic',
345 'priority' => 12,
346 'help_text' => __( 'Add options for the form field', 'weforms' ),
347 );
348 }
349
350 /**
351 * Common properties of a textarea field
352 *
353 * @return array
354 */
355 public function get_default_textarea_option_settings() {
356 return array(
357 array(
358 'name' => 'rows',
359 'title' => __( 'Rows', 'weforms' ),
360 'type' => 'text',
361 'section' => 'advanced',
362 'priority' => 10,
363 'help_text' => __( 'Number of rows in textarea', 'weforms' ),
364 ),
365
366 array(
367 'name' => 'cols',
368 'title' => __( 'Columns', 'weforms' ),
369 'type' => 'text',
370 'section' => 'advanced',
371 'priority' => 11,
372 'help_text' => __( 'Number of columns in textarea', 'weforms' ),
373 ),
374
375 array(
376 'name' => 'placeholder',
377 'title' => __( 'Placeholder text', 'weforms' ),
378 'type' => 'text',
379 'section' => 'advanced',
380 'priority' => 12,
381 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ),
382 'dependencies' => array(
383 'rich' => 'no'
384 )
385 ),
386
387 array(
388 'name' => 'default',
389 'title' => __( 'Default value', 'weforms' ),
390 'type' => 'text',
391 'section' => 'advanced',
392 'priority' => 13,
393 'help_text' => __( 'The default value this field will have', 'weforms' ),
394 ),
395
396 array(
397 'name' => 'rich',
398 'title' => __( 'Textarea', 'weforms' ),
399 'type' => 'radio',
400 'options' => array(
401 'no' => __( 'Normal', 'weforms' ),
402 'yes' => __( 'Rich textarea', 'weforms' ),
403 'teeny' => __( 'Teeny Rich textarea', 'weforms' ),
404 ),
405 'section' => 'advanced',
406 'priority' => 14,
407 'default' => 'no',
408 ),
409
410 array(
411 'name' => 'word_restriction',
412 'title' => __( 'Word Restriction', 'weforms' ),
413 'type' => 'text',
414 'section' => 'advanced',
415 'priority' => 15,
416 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ),
417 ),
418 );
419 }
420
421 /*
422 |--------------------------------------------------------------------------
423 | Field helper methods
424 |--------------------------------------------------------------------------
425 |
426 | Various helper method for rendering form fields
427 */
428
429 public function print_list_attributes( $field ) {
430 $label = isset( $field['label'] ) ? $field['label'] : '';
431 $el_name = !empty( $field['name'] ) ? $field['name'] : '';
432 $class_name = !empty( $field['css'] ) ? ' ' . $field['css'] : '';
433 $field_size = !empty( $field['width'] ) ? ' field-size-' . $field['width'] : '';
434
435 printf( 'class="wpuf-el %s%s%s" data-label="%s"', $el_name, $class_name, $field_size, $label );
436 }
437
438 /**
439 * Prints form input label
440 *
441 * @param array $attr
442 * @param integer $form_id
443 */
444 function print_label( $field, $form_id = 0 ) {
445 ?>
446 <div class="wpuf-label">
447 <label for="<?php echo isset( $field['name'] ) ? $field['name'] . '_' . $form_id : 'cls'; ?>"><?php echo $field['label'] . $this->required_mark( $field ); ?></label>
448 </div>
449 <?php
450 }
451
452 /**
453 * Check if a field is required
454 *
455 * @param array $field
456 *
457 * @return boolean
458 */
459 public function is_required( $field ) {
460 if ( isset( $field['required'] ) && $field['required'] == 'yes' ) {
461 return true;
462 }
463
464 return false;
465 }
466
467 /**
468 * Prints required field asterisk
469 *
470 * @param array $attr
471 * @return string
472 */
473 function required_mark( $field ) {
474 if ( $this->is_required( $field ) ) {
475 return ' <span class="required">*</span>';
476 }
477 }
478
479 /**
480 * Prints help text for a field
481 *
482 * @param array $field
483 */
484 function help_text( $field ) {
485 if ( empty( $field['help'] ) ) {
486 return;
487 }
488 ?>
489 <span class="wpuf-help"><?php echo stripslashes( $field['help'] ); ?></span>
490 <?php
491 }
492
493 /**
494 * Push logic to conditional array for processing
495 *
496 * @param array $form_field
497 * @param integer $form_id
498 *
499 * @return void
500 */
501 function conditional_logic( $form_field, $form_id ) {
502
503 if ( !isset( $form_field['wpuf_cond']['condition_status'] ) || $form_field['wpuf_cond']['condition_status'] != 'yes' ) {
504 return;
505 }
506
507 $cond_inputs = $form_field['wpuf_cond'];
508 $cond_inputs['condition_status'] = isset( $cond_inputs['condition_status'] ) ? $cond_inputs['condition_status'] : '';
509
510 if ( $cond_inputs['condition_status'] == 'yes') {
511 $cond_inputs['type'] = $form_field['template'];
512 $cond_inputs['name'] = $form_field['name'];
513 $cond_inputs['form_id'] = $form_id;
514 $condition = json_encode( $cond_inputs );
515
516 } else {
517 $condition = '';
518 }
519
520 // for section break
521 if ( $form_field['template'] == 'section_break' ) {
522 $cond_inputs['name'] = $form_field['name'] .'_'. $form_field['id'];
523 $condition = json_encode( $cond_inputs );
524 }
525
526 ?>
527 <script type="text/javascript">
528 wpuf_conditional_items.push(<?php echo $condition; ?>);
529 </script>
530 <?php
531 }
532
533 /**
534 * Prepare entry default, can be replaced through field classes
535 *
536 * @param $field
537 *
538 * @return mixed
539 */
540 public function prepare_entry( $field ) {
541
542 $value = !empty( $_POST[$field['name']] ) ? $_POST[$field['name']] : '';
543
544 if ( is_array( $value ) ) {
545
546 $entry_value = implode( WeForms::$field_separator, $_POST[$field['name']] );
547
548 } else {
549 $entry_value = trim( $value );
550 }
551
552 return $entry_value;
553 }
554
555
556
557 /**
558 * Function to check word restriction
559 *
560 * @param $word_nums number of words allowed
561 */
562 function check_word_restriction_func($word_nums, $rich_text, $field_name) {
563 // bail out if it is dashboard
564 if ( is_admin() ) {
565 return;
566 }
567 ?>
568 <script type="text/javascript">
569 ;(function($) {
570 $(document).ready( function(){
571 WP_User_Frontend.editorLimit.bind(<?php printf( '%d, "%s", "%s"', $word_nums, $field_name, $rich_text ); ?>);
572 });
573 })(jQuery);
574 </script>
575 <?php
576
577 }
578 }
579