PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 1.07.11
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v1.07.11
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmFieldsHelper.php

FrmFieldsHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 1.07.11, at classes/helpers/FrmFieldsHelper.php

500 lines 21.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!defined('ABSPATH')) die('You are not allowed to call this page directly.');
4
5 if(class_exists('FrmFieldsHelper'))
6 return;
7
8 class FrmFieldsHelper{
9
10 public static function field_selection(){
11 $fields = apply_filters('frm_available_fields', array(
12 'text' => __('Single Line Text', 'formidable'),
13 'textarea' => __('Paragraph Text', 'formidable'),
14 'checkbox' => __('Checkboxes', 'formidable'),
15 'radio' => __('Radio Buttons', 'formidable'),
16 'select' => __('Dropdown', 'formidable'),
17 'captcha' => __('reCAPTCHA', 'formidable')
18 ));
19
20 return $fields;
21 }
22
23 public static function pro_field_selection(){
24 return apply_filters('frm_pro_available_fields', array(
25 'email' => __('Email Address', 'formidable'),
26 'url' => __('Website/URL', 'formidable'),
27 'divider' => __('Section Heading', 'formidable'),
28 'break' => __('Page Break', 'formidable'),
29 'file' => __('File Upload', 'formidable'),
30 'rte' => __('Rich Text', 'formidable'),
31 'number' => __('Number', 'formidable'),
32 'phone' => __('Phone Number', 'formidable'),
33 'date' => __('Date', 'formidable'),
34 'time' => __('Time', 'formidable'),
35 'image' => __('Image URL', 'formidable'),
36 'scale' => __('Scale', 'formidable'),
37 //'grid' => __('Grid', 'formidable'),
38 'data' => __('Data from Entries', 'formidable'),
39 //'form' => __('SubForm', 'formidable'),
40 'hidden' => __('Hidden Field', 'formidable'),
41 'user_id' => __('User ID (hidden)', 'formidable'),
42 'password' => __('Password', 'formidable'),
43 'html' => __('HTML', 'formidable'),
44 'tag' => __('Tags', 'formidable')
45 //'address' => 'Address' //Address line 1, Address line 2, City, State/Providence, Postal Code, Select Country
46 //'city_selector' => 'US State/County/City selector',
47 //'full_name' => 'First and Last Name',
48 //'quiz' => 'Question and Answer' // for captcha alternative
49 ));
50 }
51
52 public static function setup_new_vars($type='', $form_id=''){
53 global $frm_settings;
54
55 $defaults = FrmFieldsHelper::get_default_field_opts($type, $form_id);
56 $defaults['field_options']['custom_html'] = FrmFieldsHelper::get_default_html($type);
57
58 $values = array();
59
60 foreach ($defaults as $var => $default){
61 if($var == 'field_options'){
62 $values['field_options'] = array();
63 foreach ($default as $opt_var => $opt_default){
64 $values['field_options'][$opt_var] = $opt_default;
65 unset($opt_var);
66 unset($opt_default);
67 }
68 }else{
69 $values[$var] = $default;
70 }
71 unset($var);
72 unset($default);
73 }
74
75 if ($type == 'radio' || ($type == 'checkbox'))
76 $values['options'] = serialize(array(__('Option 1', 'formidable'), __('Option 2', 'formidable')));
77 else if ( $type == 'select')
78 $values['options'] = serialize(array('', __('Option 1', 'formidable')));
79 else if ($type == 'textarea')
80 $values['field_options']['max'] = '5';
81 else if ($type == 'captcha')
82 $values['invalid'] = $frm_settings->re_msg;
83
84 $fields = self::field_selection();
85 $fields = array_merge($fields, self::pro_field_selection());
86
87 if(isset($fields[$type]))
88 $values['name'] = $fields[$type];
89
90 unset($fields);
91
92 return $values;
93 }
94
95 public static function setup_edit_vars($record, $doing_ajax=false){
96 global $frm_entry_meta;
97
98 $values = array('id' => $record->id, 'form_id' => $record->form_id);
99 $defaults = array('name' => $record->name, 'description' => $record->description);
100 $default_opts = array(
101 'field_key' => $record->field_key, 'type' => $record->type,
102 'default_value'=> $record->default_value, 'field_order' => $record->field_order,
103 'required' => $record->required
104 );
105
106 if($doing_ajax){
107 $values = $values + $defaults + $default_opts;
108 $values['form_name'] = '';
109 }else{
110 foreach ($defaults as $var => $default){
111 $values[$var] = htmlspecialchars(FrmAppHelper::get_param($var, $default));
112 unset($var);
113 unset($default);
114 }
115
116 foreach (array('field_key' => $record->field_key, 'type' => $record->type, 'default_value'=> $record->default_value, 'field_order' => $record->field_order, 'required' => $record->required) as $var => $default){
117 $values[$var] = FrmAppHelper::get_param($var, $default);
118 unset($var);
119 unset($default);
120 }
121
122 $frm_form = new FrmForm();
123 $values['form_name'] = ($record->form_id) ? $frm_form->getName( $record->form_id ) : '';
124 unset($frm_form);
125 }
126
127 unset($defaults);
128 unset($default_opts);
129
130 $values['options'] = $record->options;
131 $values['field_options'] = $record->field_options;
132
133 $defaults = self::get_default_field_opts($values['type'], $record, true);
134
135 if($values['type'] == 'captcha'){
136 global $frm_settings;
137 $defaults['invalid'] = $frm_settings->re_msg;
138 }
139
140 foreach($defaults as $opt => $default){
141 $values[$opt] = (isset($record->field_options[$opt])) ? $record->field_options[$opt] : $default;
142 unset($opt);
143 unset($default);
144 }
145
146 $values['custom_html'] = (isset($record->field_options['custom_html'])) ? $record->field_options['custom_html'] : self::get_default_html($record->type);
147
148 return apply_filters('frm_setup_edit_field_vars', $values, array('doing_ajax' => $doing_ajax));
149 }
150
151 public static function get_default_field_opts($type, $field, $limit=false){
152 $field_options = array(
153 'size' => '', 'max' => '', 'label' => '', 'blank' => '',
154 'required_indicator' => '*', 'invalid' => '', 'separate_value' => 0,
155 'clear_on_focus' => 0, 'default_blank' => 0, 'classes' => '',
156 'custom_html' => ''
157 );
158
159 if($limit)
160 return $field_options;
161
162 global $wpdb, $frm_settings;
163
164 $form_id = (is_numeric($field)) ? $field : $field->form_id;
165
166 $key = is_numeric($field) ? FrmAppHelper::get_unique_key('', $wpdb->prefix .'frm_fields', 'field_key') : $field->field_key;
167 $field_count = FrmAppHelper::getRecordCount(array('form_id' => $form_id), $wpdb->prefix .'frm_fields');
168
169 return array(
170 'name' => __('Untitled', 'formidable'), 'description' => '',
171 'field_key' => $key, 'type' => $type, 'options'=>'', 'default_value'=>'',
172 'field_order' => $field_count+1, 'required' => false,
173 'blank' => $frm_settings->blank_msg, 'unique_msg' => $frm_settings->unique_msg,
174 'invalid' => __('This field is invalid', 'formidable'), 'form_id' => $form_id,
175 'field_options' => $field_options
176 );
177 }
178
179 public static function get_form_fields($form_id, $error=false){
180 global $frm_field;
181 $fields = $frm_field->getAll(array('fi.form_id' => $form_id), 'field_order');
182 $fields = apply_filters('frm_get_paged_fields', $fields, $form_id, $error);
183 return $fields;
184 }
185
186 public static function get_default_html($type='text'){
187 if (apply_filters('frm_normal_field_type_html', true, $type)){
188 $input = (in_array($type, array('radio', 'checkbox', 'data'))) ? '<div class="frm_opt_container">[input]</div>' : '[input]';
189 $for = '';
190 if(!in_array($type, array('radio', 'checkbox', 'data', 'scale')))
191 $for = 'for="field_[key]"';
192
193 $default_html = <<<DEFAULT_HTML
194 <div id="frm_field_[id]_container" class="frm_form_field form-field [required_class][error_class]">
195 <label $for class="frm_primary_label">[field_name]
196 <span class="frm_required">[required_label]</span>
197 </label>
198 $input
199 [if description]<div class="frm_description">[description]</div>[/if description]
200 [if error]<div class="frm_error">[error]</div>[/if error]
201 </div>
202 DEFAULT_HTML;
203 }else
204 $default_html = apply_filters('frm_other_custom_html', '', $type);
205
206 return apply_filters('frm_custom_html', $default_html, $type);
207 }
208
209 public static function replace_shortcodes($html, $field, $errors=array(), $form=false){
210 $html = apply_filters('frm_before_replace_shortcodes', $html, $field, $errors, $form);
211
212 $field_name = 'item_meta['. $field['id'] .']';
213 if(isset($field['multiple']) and $field['multiple'] and ($field['type'] == 'select' or ($field['type'] == 'data' and isset($field['data_type']) and $field['data_type'] == 'select')))
214 $field_name .= '[]';
215
216 //replace [id]
217 $html = str_replace('[id]', $field['id'], $html);
218
219 //replace [key]
220 $html = str_replace('[key]', $field['field_key'], $html);
221
222 //replace [description] and [required_label] and [error]
223 $required = ($field['required'] == '0') ? '' : $field['required_indicator'];
224 if(!is_array($errors))
225 $errors = array();
226 $error = isset($errors['field'. $field['id']]) ? $errors['field'. $field['id']] : false;
227 foreach (array('description' => $field['description'], 'required_label' => $required, 'error' => $error) as $code => $value){
228 if (!$value or $value == '')
229 $html = preg_replace('/(\[if\s+'.$code.'\])(.*?)(\[\/if\s+'.$code.'\])/mis', '', $html);
230 else{
231 $html = str_replace('[if '.$code.']', '', $html);
232 $html = str_replace('[/if '.$code.']', '', $html);
233 }
234
235 $html = str_replace('['.$code.']', $value, $html);
236 }
237
238 //replace [required_class]
239 $required_class = ($field['required'] == '0') ? '' : ' frm_required_field';
240 $html = str_replace('[required_class]', $required_class, $html);
241
242 //replace [label_position]
243 $field['label'] = apply_filters('frm_html_label_position', $field['label'], $field);
244 $field['label'] = ($field['label'] and $field['label'] != '') ? $field['label'] : 'top';
245 $html = str_replace('[label_position]', (($field['type'] == 'divider' or $field['type'] == 'break') ? $field['label'] : ' frm_primary_label'), $html);
246
247 //replace [field_name]
248 $html = str_replace('[field_name]', $field['name'], $html);
249
250 //replace [error_class]
251 $error_class = isset($errors['field'. $field['id']]) ? ' frm_blank_field' : '';
252 $error_class .= ' frm_'. $field['label'] .'_container' ;
253 //insert custom CSS classes
254 if(!empty($field['classes'])){
255 if(!strpos($html, 'frm_form_field '))
256 $error_class .= ' frm_form_field';
257 $error_class .= ' '. $field['classes'];
258 }
259 $html = str_replace('[error_class]', $error_class, $html);
260
261 //replace [entry_key]
262 $entry_key = (isset($_GET) and isset($_GET['entry'])) ? $_GET['entry'] : '';
263 $html = str_replace('[entry_key]', $entry_key, $html);
264
265 //replace [input]
266 preg_match_all("/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $html, $shortcodes, PREG_PATTERN_ORDER);
267 global $frm_vars;
268
269 foreach ($shortcodes[0] as $short_key => $tag){
270 $atts = shortcode_parse_atts( $shortcodes[2][$short_key] );
271
272 if(!empty($shortcodes[2][$short_key])){
273 $tag = str_replace('[', '',$shortcodes[0][$short_key]);
274 $tag = str_replace(']', '', $tag);
275 $tags = explode(' ', $tag);
276 if(is_array($tags))
277 $tag = $tags[0];
278 }else
279 $tag = $shortcodes[1][$short_key];
280
281 $replace_with = '';
282
283 if($tag == 'input'){
284 if(isset($atts['opt'])) $atts['opt']--;
285 $field['input_class'] = isset($atts['class']) ? $atts['class'] : '';
286 if(isset($atts['class']))
287 unset($atts['class']);
288 $field['shortcodes'] = $atts;
289 ob_start();
290 include(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/input.php');
291 $replace_with = ob_get_contents();
292 ob_end_clean();
293 }else if($tag == 'deletelink' and class_exists('FrmProEntriesController'))
294 $replace_with = FrmProEntriesController::entry_delete_link($atts);
295
296 $html = str_replace($shortcodes[0][$short_key], $replace_with, $html);
297 }
298
299 if($form){
300 $form = (array)$form;
301
302 //replace [form_key]
303 $html = str_replace('[form_key]', $form['form_key'], $html);
304
305 //replace [form_name]
306 $html = str_replace('[form_name]', $form['name'], $html);
307 }
308 $html .= "\n";
309
310 $html = apply_filters('frm_replace_shortcodes', $html, $field, array('errors' => $errors, 'form' => $form));
311
312 // remove [collapse_this] when running the free version
313 if (preg_match('/\[(collapse_this)\]/s', $html))
314 $html = str_replace('[collapse_this]', '', $html);
315
316 return $html;
317 }
318
319 public static function display_recaptcha($field, $error=null){
320 global $frm_settings, $frm_vars;
321
322 if(!function_exists('recaptcha_get_html'))
323 require(FrmAppHelper::plugin_path().'/classes/recaptchalib.php');
324
325 $lang = apply_filters('frm_recaptcha_lang', $frm_settings->re_lang, $field);
326
327 if(defined('DOING_AJAX') and (!isset($frm_vars['preview']) or !$frm_vars['preview'])){
328 if(!isset($frm_vars['recaptcha_loaded']) or !$frm_vars['recaptcha_loaded'])
329 $frm_vars['recaptcha_loaded'] = '';
330
331 $frm_vars['recaptcha_loaded'] .= "Recaptcha.create('". $frm_settings->pubkey ."','field_". $field['field_key'] ."',{theme:'". $frm_settings->re_theme ."',lang:'". $lang ."'". apply_filters('frm_recaptcha_custom', '', $field) ."});";
332 ?>
333 <div id="field_<?php echo $field['field_key'] ?>"></div>
334 <?php }else{ ?>
335 <script type="text/javascript">var RecaptchaOptions={theme:'<?php echo $frm_settings->re_theme ?>',lang:'<?php echo $lang ?>'<?php echo apply_filters('frm_recaptcha_custom', '', $field) ?>};</script>
336 <?php echo recaptcha_get_html($frm_settings->pubkey .'&hl='. $lang, $error, is_ssl());
337 }
338 }
339
340 public static function dropdown_categories($args){
341 global $frm_vars;
342
343 $defaults = array('field' => false, 'name' => false, 'show_option_all' => ' ');
344 extract(wp_parse_args($args, $defaults));
345
346 if(!$field) return;
347 if(!$name) $name = "item_meta[$field[id]]";
348 $id = 'field_'. $field['field_key'];
349 $class = $field['type'];
350
351 $exclude = (is_array($field['exclude_cat'])) ? implode(',', $field['exclude_cat']) : $field['exclude_cat'];
352 $exclude = apply_filters('frm_exclude_cats', $exclude, $field);
353
354 if(is_array($field['value'])){
355 if(!empty($exclude))
356 $field['value'] = array_diff($field['value'], explode(',', $exclude));
357 $selected = reset($field['value']);
358 }else{
359 $selected = $field['value'];
360 }
361
362 $args = array(
363 'show_option_all' => $show_option_all, 'hierarchical' => 1, 'name' => $name,
364 'id' => $id, 'exclude' => $exclude, 'class' => $class, 'selected' => $selected,
365 'hide_empty' => false, 'echo' => 0, 'orderby' => 'name',
366 );
367
368 $args = apply_filters('frm_dropdown_cat', $args, $field);
369
370 if ( class_exists('FrmProFormsHelper') ) {
371 $post_type = FrmProFormsHelper::post_type($field['form_id']);
372 $args['taxonomy'] = FrmProAppHelper::get_custom_taxonomy($post_type, $field);
373 if ( ! $args['taxonomy'] ) {
374 return;
375 }
376
377 if ( is_taxonomy_hierarchical($args['taxonomy']) ) {
378 $args['exclude_tree'] = $exclude;
379 }
380 }
381
382 $dropdown = wp_dropdown_categories($args);
383
384 $add_html = FrmFieldsController::input_html($field, false);
385
386 if($frm_vars['pro_is_installed'])
387 $add_html .= FrmProFieldsController::input_html($field, false);
388
389 $dropdown = str_replace("<select name='$name' id='$id' class='$class'", "<select name='$name' id='$id' ". $add_html, $dropdown);
390
391 if(is_array($field['value'])){
392 $skip = true;
393 foreach($field['value'] as $v){
394 if($skip){
395 $skip = false;
396 continue;
397 }
398 $dropdown = str_replace(' value="'. $v. '"', ' value="'. $v .'" selected="selected"', $dropdown);
399 unset($v);
400 }
401 }
402
403 return $dropdown;
404 }
405
406 public static function get_term_link($tax_id) {
407 $tax = get_taxonomy($tax_id);
408 if ( !$tax ) {
409 return;
410 }
411
412 $link = sprintf(
413 __('Please add options from the WordPress "%1$s" page', 'formidable'),
414 '<a href="'. esc_url(admin_url('edit-tags.php?taxonomy='. $tax->name)) .'" target="_blank">'. ( empty($tax->labels->name) ? __('Categories') : $tax->labels->name ) .'</a>'
415 );
416 unset($tax);
417
418 return $link;
419 }
420
421 public static function get_field_types($type){
422 $frm_field_selection = FrmFieldsHelper::field_selection();
423 $field_types = array();
424 $single_input = array(
425 'text', 'textarea', 'rte', 'number', 'email', 'url',
426 'image', 'file', 'date', 'phone', 'hidden', 'time',
427 'user_id', 'tag', 'password'
428 );
429 $multiple_input = array('radio', 'checkbox', 'select', 'scale');
430 $other_type = array('divider', 'html', 'break');
431 $frm_pro_field_selection = FrmFieldsHelper::pro_field_selection();
432
433 if (in_array($type, $single_input)){
434 foreach($single_input as $input){
435 if (isset($frm_pro_field_selection[$input]))
436 $field_types[$input] = $frm_pro_field_selection[$input];
437 else
438 $field_types[$input] = $frm_field_selection[$input];
439 }
440 }else if (in_array($type, $multiple_input)){
441 foreach($multiple_input as $input){
442 if (isset($frm_pro_field_selection[$input]))
443 $field_types[$input] = $frm_pro_field_selection[$input];
444 else
445 $field_types[$input] = $frm_field_selection[$input];
446 }
447 }else if (in_array($type, $other_type)){
448 foreach($other_type as $input){
449 if (isset($frm_pro_field_selection[$input]))
450 $field_types[$input] = $frm_pro_field_selection[$input];
451 else
452 $field_types[$input] = $frm_field_selection[$input];
453 }
454 }
455
456 return $field_types;
457 }
458
459 public static function show_onfocus_js($field_id, $clear_on_focus){ ?>
460 <a class="frm_bstooltip <?php echo ($clear_on_focus) ? '' : 'frm_inactive_icon '; ?>frm_default_val_icons frm_action_icon frm_reload_icon frm_icon_font" id="clear_field_<?php echo $field_id; ?>" title="<?php echo esc_attr($clear_on_focus ? __('Clear default value when typing', 'formidable') : __('Do not clear default value when typing', 'formidable')); ?>"></a>
461 <?php
462 }
463
464 public static function show_default_blank_js($field_id, $default_blank){ ?>
465 <a class="frm_bstooltip <?php echo ($default_blank) ? '' :'frm_inactive_icon '; ?>frm_default_val_icons frm_action_icon frm_error_icon frm_icon_font" id="default_blank_<?php echo $field_id; ?>" title="<?php echo $default_blank ? __('Default value will NOT pass form validation', 'formidable') : __('Default value will pass form validation', 'formidable'); ?>"></a>
466 <?php
467 }
468
469 public static function switch_field_ids($val){
470 global $frm_duplicate_ids;
471 $replace = array();
472 $replace_with = array();
473 foreach((array)$frm_duplicate_ids as $old => $new){
474 $replace[] = '[if '. $old .']';
475 $replace_with[] = '[if '. $new .']';
476 $replace[] = '[if '. $old .' ';
477 $replace_with[] = '[if '. $new .' ';
478 $replace[] = '[/if '. $old .']';
479 $replace_with[] = '[/if '. $new .']';
480 $replace[] = '['. $old .']';
481 $replace_with[] = '['. $new .']';
482 $replace[] = '['. $old .' ';
483 $replace_with[] = '['. $new .' ';
484 unset($old);
485 unset($new);
486 }
487 if(is_array($val)){
488 foreach($val as $k => $v){
489 $val[$k] = str_replace($replace, $replace_with, $v);
490 unset($k);
491 unset($v);
492 }
493 }else{
494 $val = str_replace($replace, $replace_with, $val);
495 }
496
497 return $val;
498 }
499 }
500