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

538 lines 15.4 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 'css' => '',
166 'placeholder' => '',
167 'default' => '',
168 'size' => 40,
169 'help' => '',
170 'is_meta' => 'yes', // wpuf uses it to differentiate meta fields with core fields, maybe removed
171 'is_new' => true, // introduced by @edi, not sure what it does. Have to remove
172 'wpuf_cond' => $this->default_conditional_prop()
173 );
174 }
175
176 /**
177 * Common properties for all kinds of fields
178 *
179 * @param boolean $is_meta
180 *
181 * @return array
182 */
183 public static function get_default_option_settings( $is_meta = true ) {
184 $common_properties = array(
185 array(
186 'name' => 'label',
187 'title' => __( 'Field Label', 'weforms' ),
188 'type' => 'text',
189 'section' => 'basic',
190 'priority' => 10,
191 'help_text' => __( 'Enter a title of this field', 'weforms' ),
192 ),
193
194 array(
195 'name' => 'help',
196 'title' => __( 'Help text', 'weforms' ),
197 'type' => 'text',
198 'section' => 'basic',
199 'priority' => 20,
200 'help_text' => __( 'Give the user some information about this field', 'weforms' ),
201 ),
202
203 array(
204 'name' => 'required',
205 'title' => __( 'Required', 'weforms' ),
206 'type' => 'radio',
207 'options' => array(
208 'yes' => __( 'Yes', 'weforms' ),
209 'no' => __( 'No', 'weforms' ),
210 ),
211 'section' => 'basic',
212 'priority' => 21,
213 'default' => 'no',
214 'inline' => true,
215 'help_text' => __( 'Check this option to mark the field required. A form will not submit unless all required fields are provided.', 'weforms' ),
216 ),
217
218 array(
219 'name' => 'css',
220 'title' => __( 'CSS Class Name', 'weforms' ),
221 'type' => 'text',
222 'section' => 'advanced',
223 'priority' => 22,
224 'help_text' => __( 'Provide a container class name for this field.', 'weforms' ),
225 ),
226 );
227
228 if ( $is_meta ) {
229 $common_properties[] = array(
230 'name' => 'name',
231 'title' => __( 'Meta Key', 'weforms' ),
232 'type' => 'text-meta',
233 'section' => 'basic',
234 'priority' => 11,
235 'help_text' => __( 'Name of the meta key this field will save to', 'weforms' ),
236 );
237 }
238
239 return $common_properties;
240 }
241
242 /**
243 * Common properties of a text input field
244 *
245 * @param boolean $word_restriction
246 *
247 * @return array
248 */
249 public static function get_default_text_option_settings( $word_restriction = false ) {
250 $properties = array(
251 array(
252 'name' => 'placeholder',
253 'title' => __( 'Placeholder text', 'weforms' ),
254 'type' => 'text',
255 'section' => 'advanced',
256 'priority' => 10,
257 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ),
258 ),
259
260 array(
261 'name' => 'default',
262 'title' => __( 'Default value', 'weforms' ),
263 'type' => 'text',
264 'section' => 'advanced',
265 'priority' => 11,
266 'help_text' => __( 'The default value this field will have', 'weforms' ),
267 ),
268
269 array(
270 'name' => 'size',
271 'title' => __( 'Size', 'weforms' ),
272 'type' => 'text',
273 'variation' => 'number',
274 'section' => 'advanced',
275 'priority' => 20,
276 'help_text' => __( 'Size of this input field', 'weforms' ),
277 ),
278 );
279
280 if ( $word_restriction ) {
281 $properties[] = array(
282 'name' => 'word_restriction',
283 'title' => __( 'Word Restriction', 'weforms' ),
284 'type' => 'text',
285 'section' => 'advanced',
286 'priority' => 15,
287 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ),
288 );
289 }
290
291 return apply_filters( 'wpuf-form-builder-common-text-fields-properties', $properties );
292 }
293
294 /**
295 * Option data for option based fields
296 *
297 * @param boolean $is_multiple
298 *
299 * @return array
300 */
301 public function get_default_option_dropdown_settings( $is_multiple = false ) {
302 return array(
303 'name' => 'options',
304 'title' => __( 'Options', 'wpuf' ),
305 'type' => 'option-data',
306 'is_multiple' => $is_multiple,
307 'section' => 'basic',
308 'priority' => 12,
309 'help_text' => __( 'Add options for the form field', 'wpuf' ),
310 );
311 }
312
313 /**
314 * Common properties of a textarea field
315 *
316 * @return array
317 */
318 public function get_default_textarea_option_settings() {
319 return array(
320 array(
321 'name' => 'rows',
322 'title' => __( 'Rows', 'weforms' ),
323 'type' => 'text',
324 'section' => 'advanced',
325 'priority' => 10,
326 'help_text' => __( 'Number of rows in textarea', 'weforms' ),
327 ),
328
329 array(
330 'name' => 'cols',
331 'title' => __( 'Columns', 'weforms' ),
332 'type' => 'text',
333 'section' => 'advanced',
334 'priority' => 11,
335 'help_text' => __( 'Number of columns in textarea', 'weforms' ),
336 ),
337
338 array(
339 'name' => 'placeholder',
340 'title' => __( 'Placeholder text', 'weforms' ),
341 'type' => 'text',
342 'section' => 'advanced',
343 'priority' => 12,
344 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ),
345 'dependencies' => array(
346 'rich' => 'no'
347 )
348 ),
349
350 array(
351 'name' => 'default',
352 'title' => __( 'Default value', 'weforms' ),
353 'type' => 'text',
354 'section' => 'advanced',
355 'priority' => 13,
356 'help_text' => __( 'The default value this field will have', 'weforms' ),
357 ),
358
359 array(
360 'name' => 'rich',
361 'title' => __( 'Textarea', 'weforms' ),
362 'type' => 'radio',
363 'options' => array(
364 'no' => __( 'Normal', 'weforms' ),
365 'yes' => __( 'Rich textarea', 'weforms' ),
366 'teeny' => __( 'Teeny Rich textarea', 'weforms' ),
367 ),
368 'section' => 'advanced',
369 'priority' => 14,
370 'default' => 'no',
371 ),
372
373 array(
374 'name' => 'word_restriction',
375 'title' => __( 'Word Restriction', 'weforms' ),
376 'type' => 'text',
377 'section' => 'advanced',
378 'priority' => 15,
379 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ),
380 ),
381 );
382 }
383
384 /*
385 |--------------------------------------------------------------------------
386 | Field helper methods
387 |--------------------------------------------------------------------------
388 |
389 | Various helper method for rendering form fields
390 */
391
392 public function print_list_attributes( $field ) {
393 $label = isset( $field['label'] ) ? $field['label'] : '';
394 $el_name = !empty( $field['name'] ) ? $field['name'] : '';
395 $class_name = !empty( $field['css'] ) ? ' ' . $field['css'] : '';
396
397 printf( 'class="wpuf-el %s%s" data-label="%s"', $el_name, $class_name, $label );
398 }
399
400 /**
401 * Prints form input label
402 *
403 * @param array $attr
404 * @param integer $form_id
405 */
406 function print_label( $field, $form_id = 0 ) {
407 ?>
408 <div class="wpuf-label">
409 <label for="<?php echo isset( $field['name'] ) ? $field['name'] . '_' . $form_id : 'cls'; ?>"><?php echo $field['label'] . $this->required_mark( $field ); ?></label>
410 </div>
411 <?php
412 }
413
414 /**
415 * Check if a field is required
416 *
417 * @param array $field
418 *
419 * @return boolean
420 */
421 public function is_required( $field ) {
422 if ( isset( $field['required'] ) && $field['required'] == 'yes' ) {
423 return true;
424 }
425
426 return false;
427 }
428
429 /**
430 * Prints required field asterisk
431 *
432 * @param array $attr
433 * @return string
434 */
435 function required_mark( $field ) {
436 if ( $this->is_required( $field ) ) {
437 return ' <span class="required">*</span>';
438 }
439 }
440
441 /**
442 * Prints help text for a field
443 *
444 * @param array $field
445 */
446 function help_text( $field ) {
447 if ( empty( $field['help'] ) ) {
448 return;
449 }
450 ?>
451 <span class="wpuf-help"><?php echo stripslashes( $field['help'] ); ?></span>
452 <?php
453 }
454
455 /**
456 * Push logic to conditional array for processing
457 *
458 * @param array $form_field
459 * @param integer $form_id
460 *
461 * @return void
462 */
463 function conditional_logic( $form_field, $form_id ) {
464
465 if ( !isset( $form_field['wpuf_cond']['condition_status'] ) || $form_field['wpuf_cond']['condition_status'] != 'yes' ) {
466 return;
467 }
468
469 $cond_inputs = $form_field['wpuf_cond'];
470 $cond_inputs['condition_status'] = isset( $cond_inputs['condition_status'] ) ? $cond_inputs['condition_status'] : '';
471
472 if ( $cond_inputs['condition_status'] == 'yes') {
473 $cond_inputs['type'] = $form_field['template'];
474 $cond_inputs['name'] = $form_field['name'];
475 $cond_inputs['form_id'] = $form_id;
476 $condition = json_encode( $cond_inputs );
477
478 } else {
479 $condition = '';
480 }
481
482 // for section break
483 if ( $form_field['template'] == 'section_break' ) {
484 $cond_inputs['name'] = $form_field['name'] .'_'. $form_field['id'];
485 $condition = json_encode( $cond_inputs );
486 }
487
488 ?>
489 <script type="text/javascript">
490 wpuf_conditional_items.push(<?php echo $condition; ?>);
491 </script>
492 <?php
493 }
494
495 /**
496 * Prepare entry default, can be replaced through field classes
497 *
498 * @param $field
499 *
500 * @return mixed
501 */
502 public function prepare_entry( $field ) {
503
504 if ( is_array( $_POST[$field['name']] ) ) {
505
506 $entry_value = implode( WeForms::$field_separator, $_POST[$field['name']] );
507
508 } else {
509 $entry_value = trim( $_POST[$field['name']] );
510 }
511
512 return $entry_value;
513 }
514
515
516
517 /**
518 * Function to check word restriction
519 *
520 * @param $word_nums number of words allowed
521 */
522 function check_word_restriction_func($word_nums, $rich_text, $field_name) {
523 // bail out if it is dashboard
524 if ( is_admin() ) {
525 return;
526 }
527 ?>
528 <script type="text/javascript">
529 ;(function($) {
530 $(document).ready( function(){
531 WP_User_Frontend.editorLimit.bind(<?php printf( '%d, "%s", "%s"', $word_nums, $field_name, $rich_text ); ?>);
532 });
533 })(jQuery);
534 </script>
535 <?php
536
537 }
538 }