| @@ -1,913 +1,531 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - die( 'You are not allowed to call this page directly.' ); | |
| 4 | -} | |
| 2 | +/** | |
| 3 | + * @package Formidable | |
| 4 | + */ | |
| 5 | 5 | |
| 6 | -class FrmFieldsController { | |
| 6 | +if(!defined('ABSPATH')) die('You are not allowed to call this page directly.'); | |
| 7 | 7 | |
| 8 | - public static function load_field() { | |
| 9 | - FrmAppHelper::permission_check( 'frm_edit_forms' ); | |
| 10 | - check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 8 | +if(class_exists('FrmFieldsController')) | |
| 9 | + return; | |
| 10 | + | |
| 11 | +class FrmFieldsController{ | |
| 12 | + public static function load_hooks(){ | |
| 13 | + add_action('wp_ajax_frm_load_field', 'FrmFieldsController::load_field'); | |
| 14 | + add_action('wp_ajax_frm_insert_field', 'FrmFieldsController::create'); | |
| 15 | + add_action('wp_ajax_frm_field_name_in_place_edit', 'FrmFieldsController::edit_name'); | |
| 16 | + add_action('wp_ajax_frm_field_desc_in_place_edit', 'FrmFieldsController::edit_description'); | |
| 17 | + add_action('wp_ajax_frm_update_ajax_option', 'FrmFieldsController::update_ajax_option'); | |
| 18 | + add_action('wp_ajax_frm_duplicate_field', 'FrmFieldsController::duplicate'); | |
| 19 | + add_action('wp_ajax_frm_delete_field', 'FrmFieldsController::destroy'); | |
| 20 | + add_action('wp_ajax_frm_add_field_option', 'FrmFieldsController::add_option'); | |
| 21 | + add_action('wp_ajax_frm_field_option_ipe', 'FrmFieldsController::edit_option'); | |
| 22 | + add_action('wp_ajax_frm_delete_field_option', 'FrmFieldsController::delete_option'); | |
| 23 | + add_action('wp_ajax_frm_import_choices', 'FrmFieldsController::import_choices'); | |
| 24 | + add_action('wp_ajax_frm_import_options', 'FrmFieldsController::import_options'); | |
| 25 | + add_action('wp_ajax_frm_update_field_order', 'FrmFieldsController::update_order'); | |
| 26 | + add_filter('frm_field_type', 'FrmFieldsController::change_type'); | |
| 27 | + add_filter('frm_display_field_options', 'FrmFieldsController::display_field_options'); | |
| 28 | + add_action('frm_field_input_html', 'FrmFieldsController::input_html'); | |
| 29 | + add_filter('frm_field_value_saved', 'FrmFieldsController::check_value', 50, 3); | |
| 30 | + add_filter('frm_field_label_seen', 'FrmFieldsController::check_label', 10, 3); | |
| 31 | + } | |
| 32 | + | |
| 33 | + public static function load_field(){ | |
| 34 | + $fields = $_POST['field']; | |
| 35 | + if ( empty($fields) ) { | |
| 36 | + die(); | |
| 37 | + } | |
| 38 | + | |
| 39 | + $_GET['page'] = 'formidable'; | |
| 40 | + $fields = stripslashes_deep($fields); | |
| 41 | + | |
| 42 | + $ajax = true; | |
| 43 | + $values = array(); | |
| 44 | + $path = FrmAppHelper::plugin_path(); | |
| 45 | + $field_html = array(); | |
| 46 | + | |
| 47 | + foreach ( $fields as $field ) { | |
| 48 | + $field = htmlspecialchars_decode(nl2br($field)); | |
| 49 | + $field = json_decode($field, true); | |
| 50 | + | |
| 51 | + $field_id = $field['id']; | |
| 52 | + | |
| 53 | + if ( !isset($field['value']) ) { | |
| 54 | + $field['value'] = ''; | |
| 55 | + } | |
| 56 | + | |
| 57 | + $field_name = "item_meta[$field_id]"; | |
| 58 | + | |
| 59 | + ob_start(); | |
| 60 | + include($path .'/classes/views/frm-forms/add_field.php'); | |
| 61 | + $field_html[$field_id] = ob_get_contents(); | |
| 62 | + ob_end_clean(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + unset($path); | |
| 66 | + | |
| 67 | + echo json_encode($field_html); | |
| 68 | + | |
| 69 | + die(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + public static function create(){ | |
| 73 | + $field_data = $_POST['field']; | |
| 74 | + $form_id = $_POST['form_id']; | |
| 75 | + $values = array(); | |
| 76 | + if(class_exists('FrmProForm')) | |
| 77 | + $values['post_type'] = FrmProFormsHelper::post_type($form_id); | |
| 78 | + | |
| 79 | + $field_values = apply_filters('frm_before_field_created', FrmFieldsHelper::setup_new_vars($field_data, $form_id)); | |
| 80 | + | |
| 81 | + $frm_field = new FrmField(); | |
| 82 | + $field_id = $frm_field->create( $field_values ); | |
| 83 | + | |
| 84 | + if ($field_id){ | |
| 85 | + $field = FrmFieldsHelper::setup_edit_vars($frm_field->getOne($field_id)); | |
| 86 | + $field_name = "item_meta[$field_id]"; | |
| 87 | + $id = $form_id; | |
| 88 | + require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/add_field.php'); | |
| 89 | + } | |
| 90 | + die(); | |
| 91 | + } | |
| 92 | + | |
| 93 | + public static function edit_name($field = 'name', $id = '') { | |
| 94 | + if ( empty($field) ) { | |
| 95 | + $field = 'name'; | |
| 96 | + } | |
| 97 | + | |
| 98 | + if ( empty($id) ) { | |
| 99 | + $id = str_replace('field_label_', '', $_POST['element_id']); | |
| 100 | + } | |
| 101 | + | |
| 102 | + $value = trim($_POST['update_value']); | |
| 103 | + if ( trim(strip_tags($value)) == '' ) { | |
| 104 | + // set blank value if there is no content | |
| 105 | + $value = ''; | |
| 106 | + } | |
| 107 | + | |
| 108 | + $frm_field = new FrmField(); | |
| 109 | + $form = $frm_field->update($id, array($field => $value)); | |
| 110 | + echo stripslashes($value); | |
| 111 | + die(); | |
| 112 | + } | |
| 113 | + | |
| 11 | 114 | |
| 12 | - // Javascript may be included in some field settings. | |
| 13 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 14 | - $fields = isset( $_POST['field'] ) ? wp_unslash( $_POST['field'] ) : array(); | |
| 15 | - if ( empty( $fields ) ) { | |
| 16 | - wp_die(); | |
| 17 | - } | |
| 115 | + public static function edit_description(){ | |
| 116 | + $id = str_replace('field_description_', '', $_POST['element_id']); | |
| 117 | + self::edit_name('description', $id); | |
| 118 | + } | |
| 119 | + | |
| 120 | + public static function update_ajax_option(){ | |
| 121 | + $frm_field = new FrmField(); | |
| 122 | + $field = $frm_field->getOne($_POST['field']); | |
| 123 | + foreach ( array('clear_on_focus', 'separate_value', 'default_blank') as $val ) { | |
| 124 | + if ( isset($_POST[$val]) ) { | |
| 125 | + $new_val = $_POST[$val]; | |
| 126 | + if ( $val == 'separate_value' ) { | |
| 127 | + $new_val = (isset($field->field_options[$val]) && $field->field_options[$val]) ? 0 : 1; | |
| 128 | + } | |
| 129 | + | |
| 130 | + $field->field_options[$val] = $new_val; | |
| 131 | + unset($new_val); | |
| 132 | + } | |
| 133 | + unset($val); | |
| 134 | + } | |
| 18 | 135 | |
| 19 | - $_GET['page'] = 'formidable'; | |
| 136 | + $frm_field->update($_POST['field'], array('field_options' => $field->field_options)); | |
| 137 | + die(); | |
| 138 | + } | |
| 139 | + | |
| 140 | + public static function duplicate(){ | |
| 141 | + global $wpdb; | |
| 142 | + | |
| 143 | + $frm_field = new FrmField(); | |
| 144 | + $copy_field = $frm_field->getOne($_POST['field_id']); | |
| 145 | + if (!$copy_field) return; | |
| 146 | + | |
| 147 | + $values = array(); | |
| 148 | + $values['field_key'] = FrmAppHelper::get_unique_key('', $wpdb->prefix . 'frm_fields', 'field_key'); | |
| 149 | + $values['options'] = maybe_serialize($copy_field->options); | |
| 150 | + $values['default_value'] = maybe_serialize($copy_field->default_value); | |
| 151 | + $values['form_id'] = $copy_field->form_id; | |
| 152 | + foreach (array('name', 'description', 'type', 'field_options', 'required') as $col) | |
| 153 | + $values[$col] = $copy_field->{$col}; | |
| 154 | + $field_count = FrmAppHelper::getRecordCount(array('form_id' => $copy_field->form_id), $wpdb->prefix . 'frm_fields'); | |
| 155 | + $values['field_order'] = $field_count + 1; | |
| 156 | + | |
| 157 | + $field_id = $frm_field->create($values); | |
| 158 | + | |
| 159 | + if ($field_id){ | |
| 160 | + $field = FrmFieldsHelper::setup_edit_vars($frm_field->getOne($field_id)); | |
| 161 | + $field_name = "item_meta[$field_id]"; | |
| 162 | + $id = $field['form_id']; | |
| 163 | + if($field['type'] == 'html') | |
| 164 | + $field['stop_filter'] = true; | |
| 165 | + require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/add_field.php'); | |
| 166 | + } | |
| 167 | + die(); | |
| 168 | + } | |
| 169 | + | |
| 170 | + public static function destroy(){ | |
| 171 | + $frm_field = new FrmField(); | |
| 172 | + $field_id = $frm_field->destroy($_POST['field_id']); | |
| 173 | + die(); | |
| 174 | + } | |
| 20 | 175 | |
| 21 | - $values = array( | |
| 22 | - 'id' => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ), | |
| 23 | - 'doing_ajax' => true, | |
| 24 | - ); | |
| 25 | - $field_html = array(); | |
| 176 | + /* Field Options */ | |
| 177 | + public static function add_option(){ | |
| 178 | + $frm_field = new FrmField(); | |
| 26 | 179 | |
| 27 | - foreach ( $fields as $field ) { | |
| 28 | - $field = htmlspecialchars_decode( nl2br( $field ) ); | |
| 29 | - $field = json_decode( $field ); | |
| 30 | - if ( ! isset( $field->id ) || ! is_numeric( $field->id ) ) { | |
| 31 | - // this field may have already been loaded | |
| 32 | - continue; | |
| 33 | - } | |
| 180 | + $id = $_POST['field_id']; | |
| 181 | + $field = $frm_field->getOne($id); | |
| 182 | + $options = maybe_unserialize($field->options); | |
| 183 | + if(!empty($options)) | |
| 184 | + $last = max(array_keys($options)); | |
| 185 | + else | |
| 186 | + $last = 0; | |
| 187 | + | |
| 188 | + $opt_key = $last + 1; | |
| 189 | + $first_opt = reset($options); | |
| 190 | + $next_opt = count($options); | |
| 191 | + if($first_opt != '') | |
| 192 | + $next_opt++; | |
| 193 | + $opt = __('Option', 'formidable') .' '. $next_opt; | |
| 194 | + unset($next_opt); | |
| 195 | + | |
| 196 | + $field_val = $opt; | |
| 197 | + $options[$opt_key] = $opt; | |
| 198 | + $frm_field->update($id, array('options' => maybe_serialize($options))); | |
| 199 | + $checked = ''; | |
| 34 | 200 | |
| 35 | - if ( ! isset( $field->value ) ) { | |
| 36 | - $field->value = ''; | |
| 37 | - } | |
| 38 | - $field->field_options = json_decode( json_encode( $field->field_options ), true ); | |
| 39 | - $field->options = json_decode( json_encode( $field->options ), true ); | |
| 40 | - $field->default_value = json_decode( json_encode( $field->default_value ), true ); | |
| 201 | + $field_data = $frm_field->getOne($id); | |
| 202 | + $field_data->field_options = maybe_unserialize($field_data->field_options); | |
| 203 | + $field = array(); | |
| 204 | + $field['type'] = $field_data->type; | |
| 205 | + $field['id'] = $id; | |
| 206 | + $field['separate_value'] = isset($field_data->field_options['separate_value']) ? $field_data->field_options['separate_value'] : 0; | |
| 207 | + $field_name = "item_meta[$id]"; | |
| 208 | + | |
| 209 | + require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/single-option.php'); | |
| 210 | + die(); | |
| 211 | + } | |
| 41 | 212 | |
| 42 | - ob_start(); | |
| 43 | - self::load_single_field( $field, $values ); | |
| 44 | - $field_html[ absint( $field->id ) ] = ob_get_contents(); | |
| 45 | - ob_end_clean(); | |
| 46 | - } | |
| 213 | + public static function edit_option(){ | |
| 214 | + $ids = explode('-', $_POST['element_id']); | |
| 215 | + $id = str_replace('field_', '', $ids[0]); | |
| 216 | + if ( strpos($_POST['element_id'], 'key_') ) { | |
| 217 | + $id = str_replace('key_', '', $id); | |
| 218 | + $new_value = trim($_POST['update_value']); | |
| 219 | + } else { | |
| 220 | + $new_label = trim($_POST['update_value']); | |
| 221 | + } | |
| 222 | + | |
| 223 | + $frm_field = new FrmField(); | |
| 224 | + $field = $frm_field->getOne($id); | |
| 225 | + $options = maybe_unserialize($field->options); | |
| 226 | + $this_opt = (array) $options[$ids[1]]; | |
| 227 | + | |
| 228 | + $label = isset($this_opt['label']) ? $this_opt['label'] : reset($this_opt); | |
| 229 | + if ( isset($this_opt['value']) ) { | |
| 230 | + $value = $this_opt['value']; | |
| 231 | + } | |
| 232 | + | |
| 233 | + if ( !isset($new_label) ) { | |
| 234 | + $new_label = $label; | |
| 235 | + } | |
| 236 | + | |
| 237 | + if ( isset($new_value) || isset($value) ) { | |
| 238 | + $update_value = isset($new_value) ? $new_value : $value; | |
| 239 | + } | |
| 240 | + | |
| 241 | + if ( isset($update_value) && $update_value != $new_label ) { | |
| 242 | + $options[$ids[1]] = array('value' => $update_value, 'label' => $new_label); | |
| 243 | + } else { | |
| 244 | + $options[$ids[1]] = trim($_POST['update_value']); | |
| 245 | + } | |
| 246 | + | |
| 247 | + $frm_field->update($id, array('options' => maybe_serialize($options))); | |
| 248 | + echo (trim($_POST['update_value']) == '') ? __('(Blank)', 'formidable') : stripslashes($_POST['update_value']); | |
| 249 | + die(); | |
| 250 | + } | |
| 47 | 251 | |
| 48 | - echo json_encode( $field_html ); | |
| 252 | + public static function delete_option(){ | |
| 253 | + $frm_field = new FrmField(); | |
| 254 | + $field = $frm_field->getOne($_POST['field_id']); | |
| 255 | + $options = maybe_unserialize($field->options); | |
| 256 | + unset($options[$_POST['opt_key']]); | |
| 257 | + $frm_field->update($_POST['field_id'], array('options' => maybe_serialize($options))); | |
| 258 | + die(); | |
| 259 | + } | |
| 260 | + | |
| 261 | + public static function import_choices(){ | |
| 262 | + if ( !current_user_can('frm_edit_forms') ) { | |
| 263 | + return; | |
| 264 | + } | |
| 265 | + | |
| 266 | + $field_id = $_REQUEST['field_id']; | |
| 267 | + | |
| 268 | + global $current_screen, $hook_suffix; | |
| 49 | 269 | |
| 50 | - wp_die(); | |
| 51 | - } | |
| 270 | + // Catch plugins that include admin-header.php before admin.php completes. | |
| 271 | + if ( empty( $current_screen ) && function_exists('set_current_screen') ) { | |
| 272 | + $hook_suffix = ''; | |
| 273 | + set_current_screen(); | |
| 274 | + } | |
| 275 | + | |
| 276 | + if ( function_exists('register_admin_color_schemes') ) { | |
| 277 | + register_admin_color_schemes(); | |
| 278 | + } | |
| 279 | + | |
| 280 | + $hook_suffix = $admin_body_class = ''; | |
| 281 | + | |
| 282 | + if ( get_user_setting('mfold') == 'f' ) | |
| 283 | + $admin_body_class .= ' folded'; | |
| 52 | 284 | |
| 53 | - /** | |
| 54 | - * Create a new field with ajax | |
| 55 | - */ | |
| 56 | - public static function create() { | |
| 57 | - FrmAppHelper::permission_check( 'frm_edit_forms' ); | |
| 58 | - check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 285 | + if ( function_exists('is_admin_bar_showing') && is_admin_bar_showing() ) { | |
| 286 | + $admin_body_class .= ' admin-bar'; | |
| 287 | + } | |
| 59 | 288 | |
| 60 | - $field_type = FrmAppHelper::get_post_param( 'field_type', '', 'sanitize_text_field' ); | |
| 61 | - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); | |
| 289 | + if ( is_rtl() ) | |
| 290 | + $admin_body_class .= ' rtl'; | |
| 62 | 291 | |
| 63 | - do_action( 'frm_before_create_field', $field_type, $form_id ); | |
| 292 | + $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' ); | |
| 293 | + $prepop = array(); | |
| 294 | + $prepop[__('Countries', 'formidable')] = FrmAppHelper::get_countries(); | |
| 295 | + | |
| 296 | + $states = FrmAppHelper::get_us_states(); | |
| 297 | + $state_abv = array_keys($states); | |
| 298 | + sort($state_abv); | |
| 299 | + $prepop[__('U.S. State Abbreviations', 'formidable')] = $state_abv; | |
| 300 | + $states = array_values($states); | |
| 301 | + sort($states); | |
| 302 | + $prepop[__('U.S. States', 'formidable')] = $states; | |
| 303 | + unset($state_abv); | |
| 304 | + unset($states); | |
| 305 | + | |
| 306 | + $prepop[__('Age', 'formidable')] = array( | |
| 307 | + __('Under 18', 'formidable'), __('18-24', 'formidable'), __('25-34', 'formidable'), | |
| 308 | + __('35-44', 'formidable'), __('45-54', 'formidable'), __('55-64', 'formidable'), | |
| 309 | + __('65 or Above', 'formidable'), __('Prefer Not to Answer', 'formidable') | |
| 310 | + ); | |
| 311 | + | |
| 312 | + $prepop[__('Satisfaction', 'formidable')] = array( | |
| 313 | + __('Very Satisfied', 'formidable'), __('Satisfied', 'formidable'), __('Neutral', 'formidable'), | |
| 314 | + __('Unsatisfied', 'formidable'), __('Very Unsatisfied', 'formidable'), __('N/A', 'formidable') | |
| 315 | + ); | |
| 64 | 316 | |
| 65 | - $field = self::include_new_field( $field_type, $form_id ); | |
| 317 | + $prepop[__('Importance', 'formidable')] = array( | |
| 318 | + __('Very Important', 'formidable'), __('Important', 'formidable'), __('Neutral', 'formidable'), | |
| 319 | + __('Somewhat Important', 'formidable'), __('Not at all Important', 'formidable'), __('N/A', 'formidable') | |
| 320 | + ); | |
| 321 | + | |
| 322 | + $prepop[__('Agreement', 'formidable')] = array( | |
| 323 | + __('Strongly Agree', 'formidable'), __('Agree', 'formidable'), __('Neutral', 'formidable'), | |
| 324 | + __('Disagree', 'formidable'), __('Strongly Disagree', 'formidable'), __('N/A', 'formidable') | |
| 325 | + ); | |
| 326 | + | |
| 327 | + $prepop = apply_filters('frm_bulk_field_choices', $prepop); | |
| 328 | + | |
| 329 | + $frm_field = new FrmField(); | |
| 330 | + $field = $frm_field->getOne($field_id); | |
| 331 | + | |
| 332 | + include(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/import_choices.php'); | |
| 333 | + die(); | |
| 334 | + } | |
| 335 | + | |
| 336 | + public static function import_options(){ | |
| 337 | + if(!is_admin() or !current_user_can('frm_edit_forms')) | |
| 338 | + return; | |
| 339 | + | |
| 340 | + extract(stripslashes_deep($_POST)); | |
| 341 | + | |
| 342 | + $frm_field = new FrmField(); | |
| 343 | + $field = $frm_field->getOne($field_id); | |
| 344 | + | |
| 345 | + if(!in_array($field->type, array('radio', 'checkbox', 'select'))) | |
| 346 | + return; | |
| 347 | + | |
| 348 | + $field = FrmFieldsHelper::setup_edit_vars($field); | |
| 349 | + $opts = explode("\n", rtrim($opts, "\n")); | |
| 350 | + if ( $field['separate_value'] ) { | |
| 351 | + foreach ( $opts as $opt_key => $opt ) { | |
| 352 | + if ( strpos($opt, '|') !== false ) { | |
| 353 | + $vals = explode('|', $opt); | |
| 354 | + if ( $vals[0] != $vals[1] ) { | |
| 355 | + $opts[$opt_key] = array('label' => trim($vals[0]), 'value' => trim($vals[1])); | |
| 356 | + } | |
| 357 | + unset($vals); | |
| 358 | + } | |
| 359 | + unset($opt_key); | |
| 360 | + unset($opt); | |
| 361 | + } | |
| 362 | + } | |
| 363 | + | |
| 364 | + $frm_field->update($field_id, array('options' => maybe_serialize($opts))); | |
| 365 | + | |
| 366 | + $field['options'] = $opts; | |
| 367 | + $field_name = $field['name']; | |
| 368 | + | |
| 369 | + if ( $field['type'] == 'radio' || $field['type'] == 'checkbox' ) { | |
| 370 | + require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/radio.php'); | |
| 371 | + } else { | |
| 372 | + foreach ( $field['options'] as $opt_key => $opt ) { | |
| 373 | + $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field); | |
| 374 | + $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field); | |
| 375 | + require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/single-option.php'); | |
| 376 | + } | |
| 377 | + } | |
| 378 | + | |
| 379 | + die(); | |
| 380 | + } | |
| 66 | 381 | |
| 67 | - // this hook will allow for multiple fields to be added at once | |
| 68 | - do_action( 'frm_after_field_created', $field, $form_id ); | |
| 382 | + public static function update_order(){ | |
| 383 | + if(isset($_POST) and isset($_POST['frm_field_id'])){ | |
| 384 | + $frm_field = new FrmField(); | |
| 385 | + | |
| 386 | + foreach ($_POST['frm_field_id'] as $position => $item) | |
| 387 | + $frm_field->update($item, array('field_order' => $position)); | |
| 388 | + } | |
| 389 | + die(); | |
| 390 | + } | |
| 391 | + | |
| 392 | + public static function change_type($type){ | |
| 393 | + global $frm_vars; | |
| 69 | 394 | |
| 70 | - wp_die(); | |
| 71 | - } | |
| 395 | + if ($frm_vars['pro_is_installed']) return $type; | |
| 396 | + | |
| 397 | + if($type == 'scale' || $type == '10radio') | |
| 398 | + $type = 'radio'; | |
| 399 | + else if($type == 'rte') | |
| 400 | + $type = 'textarea'; | |
| 401 | + | |
| 402 | + $frm_field_selection = FrmFieldsHelper::field_selection(); | |
| 403 | + $types = array_keys($frm_field_selection); | |
| 404 | + if (!in_array($type, $types) && $type != 'captcha') | |
| 405 | + $type = 'text'; | |
| 72 | 406 | |
| 73 | - /** | |
| 74 | - * Set up and create a new field | |
| 75 | - * | |
| 76 | - * @param string $field_type | |
| 77 | - * @param integer $form_id | |
| 78 | - * | |
| 79 | - * @return array|bool | |
| 80 | - */ | |
| 81 | - public static function include_new_field( $field_type, $form_id ) { | |
| 82 | - $field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id ); | |
| 83 | - $field_values = apply_filters( 'frm_before_field_created', $field_values ); | |
| 84 | - | |
| 85 | - $field_id = FrmField::create( $field_values ); | |
| 86 | - | |
| 87 | - if ( ! $field_id ) { | |
| 88 | - return false; | |
| 89 | - } | |
| 90 | - | |
| 91 | - $field = self::get_field_array_from_id( $field_id ); | |
| 92 | - | |
| 93 | - $values = array(); | |
| 94 | - if ( FrmAppHelper::pro_is_installed() ) { | |
| 95 | - $values['post_type'] = FrmProFormsHelper::post_type( $form_id ); | |
| 96 | - | |
| 97 | - $parent_form_id = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'parent_form_id' ); | |
| 98 | - if ( $parent_form_id ) { | |
| 99 | - $field['parent_form_id'] = $parent_form_id; | |
| 100 | - } | |
| 101 | - } | |
| 102 | - | |
| 103 | - self::load_single_field( $field, $values, $form_id ); | |
| 104 | - | |
| 105 | - return $field; | |
| 106 | - } | |
| 107 | - | |
| 108 | - public static function duplicate() { | |
| 109 | - FrmAppHelper::permission_check( 'frm_edit_forms' ); | |
| 110 | - check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 111 | - | |
| 112 | - $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' ); | |
| 113 | - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); | |
| 114 | - | |
| 115 | - $new_field = FrmField::duplicate_single_field( $field_id, $form_id ); | |
| 116 | - | |
| 117 | - if ( is_array( $new_field ) && ! empty( $new_field['field_id'] ) ) { | |
| 118 | - self::load_single_field( $new_field['field_id'], $new_field['values'] ); | |
| 119 | - } | |
| 120 | - | |
| 121 | - wp_die(); | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * @since 3.0 | |
| 126 | - * | |
| 127 | - * @param int $field_id | |
| 128 | - * | |
| 129 | - * @return array | |
| 130 | - */ | |
| 131 | - public static function get_field_array_from_id( $field_id ) { | |
| 132 | - $field = FrmField::getOne( $field_id ); | |
| 133 | - | |
| 134 | - return FrmFieldsHelper::setup_edit_vars( $field ); | |
| 135 | - } | |
| 136 | - | |
| 137 | - /** | |
| 138 | - * @since 3.0 | |
| 139 | - * | |
| 140 | - * @param int|array|object $field_object | |
| 141 | - * @param array $values | |
| 142 | - * @param int $form_id | |
| 143 | - */ | |
| 144 | - public static function load_single_field( $field_object, $values, $form_id = 0 ) { | |
| 145 | - global $frm_vars; | |
| 146 | - $frm_vars['is_admin'] = true; | |
| 147 | - | |
| 148 | - if ( is_numeric( $field_object ) ) { | |
| 149 | - $field_object = FrmField::getOne( $field_object ); | |
| 150 | - } elseif ( is_array( $field_object ) ) { | |
| 151 | - $field = $field_object; | |
| 152 | - $field_object = FrmField::getOne( $field['id'] ); | |
| 153 | - } | |
| 154 | - | |
| 155 | - $field_obj = FrmFieldFactory::get_field_factory( $field_object ); | |
| 156 | - $display = self::display_field_options( array(), $field_obj ); | |
| 157 | - | |
| 158 | - $ajax_loading = isset( $values['ajax_load'] ) && $values['ajax_load']; | |
| 159 | - $ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ) ); | |
| 160 | - | |
| 161 | - if ( $ajax_loading && $ajax_this_field ) { | |
| 162 | - $li_classes = self::get_classes_for_builder_field( array(), $display, $field_obj ); | |
| 163 | - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php' ); | |
| 164 | - } else { | |
| 165 | - if ( ! isset( $field ) && is_object( $field_object ) ) { | |
| 166 | - $field_object->parent_form_id = isset( $values['id'] ) ? $values['id'] : $field_object->form_id; | |
| 167 | - | |
| 168 | - $field = FrmFieldsHelper::setup_edit_vars( $field_object ); | |
| 169 | - } | |
| 170 | - | |
| 171 | - $li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj ); | |
| 172 | - $li_classes .= ' ui-state-default widgets-holder-wrap'; | |
| 173 | - | |
| 174 | - require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' ); | |
| 175 | - } | |
| 176 | - } | |
| 177 | - | |
| 178 | - /** | |
| 179 | - * @since 3.0 | |
| 180 | - */ | |
| 181 | - private static function get_classes_for_builder_field( $field, $display, $field_info ) { | |
| 182 | - $li_classes = $field_info->form_builder_classes( $display['type'] ); | |
| 183 | - $li_classes .= ' frm_form_field frmstart '; | |
| 184 | - | |
| 185 | - if ( isset( $field['classes'] ) ) { | |
| 186 | - $li_classes .= trim( $field['classes'] ) . ' '; | |
| 187 | - } | |
| 188 | - | |
| 189 | - $li_classes .= 'frmend'; | |
| 190 | - | |
| 191 | - if ( $field ) { | |
| 192 | - $li_classes = apply_filters( 'frm_build_field_class', $li_classes, $field ); | |
| 193 | - } | |
| 194 | - | |
| 195 | - return $li_classes; | |
| 196 | - } | |
| 197 | - | |
| 198 | - public static function destroy() { | |
| 199 | - FrmAppHelper::permission_check( 'frm_edit_forms' ); | |
| 200 | - check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 201 | - | |
| 202 | - $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' ); | |
| 203 | - FrmField::destroy( $field_id ); | |
| 204 | - wp_die(); | |
| 205 | - } | |
| 206 | - | |
| 207 | - /* Field Options */ | |
| 208 | - | |
| 209 | - public static function import_options() { | |
| 210 | - FrmAppHelper::permission_check( 'frm_edit_forms' ); | |
| 211 | - check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 212 | - | |
| 213 | - if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) { | |
| 214 | - return; | |
| 215 | - } | |
| 216 | - | |
| 217 | - $field_id = FrmAppHelper::get_param( 'field_id', '', 'post', 'absint' ); | |
| 218 | - $field = FrmField::getOne( $field_id ); | |
| 219 | - | |
| 220 | - if ( ! in_array( $field->type, array( 'radio', 'checkbox', 'select' ) ) ) { | |
| 221 | - return; | |
| 222 | - } | |
| 223 | - | |
| 224 | - $field = FrmFieldsHelper::setup_edit_vars( $field ); | |
| 225 | - $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' ); | |
| 226 | - $opts = explode( "\n", rtrim( $opts, "\n" ) ); | |
| 227 | - $opts = array_map( 'trim', $opts ); | |
| 228 | - | |
| 229 | - $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' ); | |
| 230 | - $field['separate_value'] = ( $separate === 'true' ); | |
| 231 | - | |
| 232 | - if ( $field['separate_value'] ) { | |
| 233 | - foreach ( $opts as $opt_key => $opt ) { | |
| 234 | - if ( strpos( $opt, '|' ) !== false ) { | |
| 235 | - $vals = explode( '|', $opt ); | |
| 236 | - $opts[ $opt_key ] = array( | |
| 237 | - 'label' => trim( $vals[0] ), | |
| 238 | - 'value' => trim( $vals[1] ), | |
| 239 | - ); | |
| 240 | - unset( $vals ); | |
| 241 | - } | |
| 242 | - unset( $opt_key, $opt ); | |
| 243 | - } | |
| 244 | - } | |
| 245 | - | |
| 246 | - // Keep other options after bulk update. | |
| 247 | - if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) { | |
| 248 | - $other_array = array(); | |
| 249 | - foreach ( $field['options'] as $opt_key => $opt ) { | |
| 250 | - if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) { | |
| 251 | - $other_array[ $opt_key ] = $opt; | |
| 252 | - } | |
| 253 | - unset( $opt_key, $opt ); | |
| 254 | - } | |
| 255 | - if ( ! empty( $other_array ) ) { | |
| 256 | - $opts = array_merge( $opts, $other_array ); | |
| 257 | - } | |
| 258 | - } | |
| 259 | - | |
| 260 | - $field['options'] = $opts; | |
| 261 | - | |
| 262 | - FrmFieldsHelper::show_single_option( $field ); | |
| 263 | - | |
| 264 | - wp_die(); | |
| 265 | - } | |
| 266 | - | |
| 267 | - /** | |
| 268 | - * @since 4.0 | |
| 269 | - * | |
| 270 | - * @param array $atts - Includes field array, field_obj, display array, values array. | |
| 271 | - */ | |
| 272 | - public static function load_single_field_settings( $atts ) { | |
| 273 | - $field = $atts['field']; | |
| 274 | - $field_obj = $atts['field_obj']; | |
| 275 | - $values = $atts['values']; | |
| 276 | - $display = $atts['display']; | |
| 277 | - unset( $atts ); | |
| 278 | - | |
| 279 | - if ( ! isset( $field['unique'] ) ) { | |
| 280 | - $field['unique'] = false; | |
| 281 | - } | |
| 282 | - | |
| 283 | - if ( ! isset( $field['read_only'] ) ) { | |
| 284 | - $field['read_only'] = false; | |
| 285 | - } | |
| 286 | - | |
| 287 | - $field_types = FrmFieldsHelper::get_field_types( $field['type'] ); | |
| 288 | - $pro_field_selection = FrmField::pro_field_selection(); | |
| 289 | - $all_field_types = array_merge( $pro_field_selection, FrmField::field_selection() ); | |
| 290 | - $disabled_fields = FrmAppHelper::pro_is_installed() ? array() : $pro_field_selection; | |
| 291 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 292 | - | |
| 293 | - if ( ! isset( $all_field_types[ $field['type'] ] ) ) { | |
| 294 | - // Add fallback for an add-on field type that has been deactivated. | |
| 295 | - $all_field_types[ $field['type'] ] = array( | |
| 296 | - 'name' => ucfirst( $field['type'] ), | |
| 297 | - 'icon' => 'frm_icon_font frm_pencil_icon', | |
| 298 | - ); | |
| 299 | - } elseif ( ! is_array( $all_field_types[ $field['type'] ] ) ) { | |
| 300 | - // Fallback for fields added in a more basic way. | |
| 301 | - FrmFormsHelper::prepare_field_type( $all_field_types[ $field['type'] ] ); | |
| 302 | - } | |
| 303 | - | |
| 304 | - $type_name = $all_field_types[ $field['type'] ]['name']; | |
| 305 | - if ( $field['type'] === 'divider' && FrmField::is_option_true( $field, 'repeat' ) ) { | |
| 306 | - $type_name = $all_field_types['divider|repeat']['name']; | |
| 307 | - } | |
| 308 | - | |
| 309 | - if ( $display['default'] ) { | |
| 310 | - $default_value_types = self::default_value_types( $field, compact( 'display' ) ); | |
| 311 | - } | |
| 312 | - | |
| 313 | - if ( $display['clear_on_focus'] && is_array( $field['placeholder'] ) ) { | |
| 314 | - $field['placeholder'] = implode( ', ', $field['placeholder'] ); | |
| 315 | - } | |
| 316 | - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php' ); | |
| 317 | - } | |
| 318 | - | |
| 319 | - /** | |
| 320 | - * Get the list of default value types that can be toggled in the builder. | |
| 321 | - * | |
| 322 | - * @since 4.0 | |
| 323 | - * @return array | |
| 324 | - */ | |
| 325 | - private static function default_value_types( $field, $atts ) { | |
| 326 | - $types = array( | |
| 327 | - 'default_value' => array( | |
| 328 | - 'class' => '', | |
| 329 | - 'icon' => 'frm_icon_font frm_text2_icon', | |
| 330 | - 'title' => __( 'Default Value (Text)', 'formidable' ), | |
| 331 | - 'data' => array( | |
| 332 | - 'frmshow' => '#default-value-for-', | |
| 333 | - ), | |
| 334 | - ), | |
| 335 | - 'calc' => array( | |
| 336 | - 'class' => 'frm_show_upgrade frm_noallow', | |
| 337 | - 'title' => __( 'Default Value (Calculation)', 'formidable' ), | |
| 338 | - 'icon' => 'frm_icon_font frm_calculator_icon', | |
| 339 | - 'data' => array( | |
| 340 | - 'medium' => 'calculations', | |
| 341 | - 'upgrade' => __( 'Calculator forms', 'formidable' ), | |
| 342 | - ), | |
| 343 | - ), | |
| 344 | - 'get_values_field' => array( | |
| 345 | - 'class' => 'frm_show_upgrade frm_noallow', | |
| 346 | - 'title' => __( 'Default Value (Lookup)', 'formidable' ), | |
| 347 | - 'icon' => 'frm_icon_font frm_search_icon', | |
| 348 | - 'data' => array( | |
| 349 | - 'medium' => 'lookup', | |
| 350 | - 'upgrade' => __( 'Lookup fields', 'formidable' ), | |
| 351 | - ), | |
| 352 | - ), | |
| 353 | - ); | |
| 354 | - | |
| 355 | - $types = apply_filters( 'frm_default_value_types', $types, $atts ); | |
| 356 | - | |
| 357 | - if ( FrmAppHelper::pro_is_installed() && ! FrmAppHelper::meets_min_pro_version( '4.0' ) ) { | |
| 358 | - // Prevent settings from showing in 2 spots. | |
| 359 | - unset( $types['calc'], $types['get_values_field'] ); | |
| 360 | - } | |
| 361 | - | |
| 362 | - // Set active class. | |
| 363 | - $settings = array_keys( $types ); | |
| 364 | - $active = 'default_value'; | |
| 365 | - | |
| 366 | - foreach ( $settings as $type ) { | |
| 367 | - if ( ! empty( $field[ $type ] ) ) { | |
| 368 | - $active = $type; | |
| 369 | - } | |
| 370 | - } | |
| 371 | - | |
| 372 | - $types[ $active ]['class'] .= ' current'; | |
| 373 | - $types[ $active ]['current'] = true; | |
| 374 | - | |
| 375 | - return $types; | |
| 376 | - } | |
| 377 | - | |
| 378 | - public static function change_type( $type ) { | |
| 379 | - $type_switch = array( | |
| 380 | - 'scale' => 'radio', | |
| 381 | - 'star' => 'radio', | |
| 382 | - '10radio' => 'radio', | |
| 383 | - 'rte' => 'textarea', | |
| 384 | - 'website' => 'url', | |
| 385 | - 'image' => 'url', | |
| 386 | - ); | |
| 387 | - if ( isset( $type_switch[ $type ] ) ) { | |
| 388 | - $type = $type_switch[ $type ]; | |
| 389 | - } | |
| 390 | - | |
| 391 | - $pro_fields = FrmField::pro_field_selection(); | |
| 392 | - $types = array_keys( $pro_fields ); | |
| 393 | - if ( in_array( $type, $types ) ) { | |
| 394 | - $type = 'text'; | |
| 395 | - } | |
| 396 | - | |
| 397 | - return $type; | |
| 398 | - } | |
| 399 | - | |
| 400 | - /** | |
| 401 | - * @param array $settings | |
| 402 | - * @param object $field_info | |
| 403 | - * | |
| 404 | - * @return array | |
| 405 | - */ | |
| 406 | - public static function display_field_options( $settings, $field_info = null ) { | |
| 407 | - if ( $field_info ) { | |
| 408 | - $settings = $field_info->display_field_settings(); | |
| 409 | - $settings['field_data'] = $field_info->field; | |
| 410 | - } | |
| 411 | - | |
| 412 | - return apply_filters( 'frm_display_field_options', $settings ); | |
| 413 | - } | |
| 414 | - | |
| 415 | - /** | |
| 416 | - * Display the format option | |
| 417 | - * | |
| 418 | - * @since 3.0 | |
| 419 | - * | |
| 420 | - * @param array $field | |
| 421 | - */ | |
| 422 | - public static function show_format_option( $field ) { | |
| 423 | - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php' ); | |
| 424 | - } | |
| 425 | - | |
| 426 | - public static function input_html( $field, $echo = true ) { | |
| 427 | - $class = array(); | |
| 428 | - self::add_input_classes( $field, $class ); | |
| 429 | - | |
| 430 | - $add_html = array(); | |
| 431 | - self::add_html_size( $field, $add_html ); | |
| 432 | - self::add_html_length( $field, $add_html ); | |
| 433 | - self::add_html_placeholder( $field, $add_html, $class ); | |
| 434 | - self::add_validation_messages( $field, $add_html ); | |
| 435 | - | |
| 436 | - $class = apply_filters( 'frm_field_classes', implode( ' ', $class ), $field ); | |
| 437 | - | |
| 438 | - FrmFormsHelper::add_html_attr( $class, 'class', $add_html ); | |
| 439 | - | |
| 440 | - self::add_shortcodes_to_html( $field, $add_html ); | |
| 441 | - self::add_pattern_attribute( $field, $add_html ); | |
| 442 | - | |
| 443 | - $add_html = apply_filters( 'frm_field_extra_html', $add_html, $field ); | |
| 444 | - $add_html = ' ' . implode( ' ', $add_html ) . ' '; | |
| 445 | - | |
| 446 | - if ( $echo ) { | |
| 447 | - echo $add_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 448 | - } | |
| 449 | - | |
| 450 | - return $add_html; | |
| 451 | - } | |
| 452 | - | |
| 453 | - private static function add_input_classes( $field, array &$class ) { | |
| 454 | - if ( isset( $field['input_class'] ) && ! empty( $field['input_class'] ) ) { | |
| 455 | - $class[] = $field['input_class']; | |
| 456 | - } | |
| 457 | - | |
| 458 | - if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) { | |
| 459 | - return; | |
| 460 | - } | |
| 461 | - | |
| 462 | - if ( isset( $field['size'] ) && $field['size'] > 0 ) { | |
| 463 | - $class[] = 'auto_width'; | |
| 464 | - } | |
| 465 | - } | |
| 466 | - | |
| 467 | - private static function add_html_size( $field, array &$add_html ) { | |
| 468 | - $size_fields = array( | |
| 469 | - 'select', | |
| 470 | - 'data', | |
| 471 | - 'time', | |
| 472 | - 'hidden', | |
| 473 | - 'file', | |
| 474 | - 'lookup', | |
| 475 | - ); | |
| 476 | - | |
| 477 | - if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields ) ) { | |
| 478 | - return; | |
| 479 | - } | |
| 480 | - | |
| 481 | - if ( FrmAppHelper::is_admin_page( 'formidable' ) ) { | |
| 482 | - return; | |
| 483 | - } | |
| 484 | - | |
| 485 | - if ( is_numeric( $field['size'] ) ) { | |
| 486 | - $field['size'] .= 'px'; | |
| 487 | - } | |
| 488 | - | |
| 489 | - $important = apply_filters( 'frm_use_important_width', 1, $field ); | |
| 490 | - // Note: This inline styling must stay since we cannot realistically set a class for every possible field size. | |
| 491 | - $add_html['style'] = 'style="width:' . esc_attr( $field['size'] ) . ( $important ? ' !important' : '' ) . '"'; | |
| 492 | - | |
| 493 | - self::add_html_cols( $field, $add_html ); | |
| 494 | - } | |
| 495 | - | |
| 496 | - private static function add_html_cols( $field, array &$add_html ) { | |
| 497 | - if ( ! in_array( $field['type'], array( 'textarea', 'rte' ) ) ) { | |
| 498 | - return; | |
| 499 | - } | |
| 500 | - | |
| 501 | - // Convert to cols for textareas. | |
| 502 | - $calc = array( | |
| 503 | - '' => 9, | |
| 504 | - 'px' => 9, | |
| 505 | - 'rem' => 0.444, | |
| 506 | - 'em' => 0.544, | |
| 507 | - ); | |
| 508 | - | |
| 509 | - // include "col" for valid html | |
| 510 | - $unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) ); | |
| 511 | - | |
| 512 | - if ( ! isset( $calc[ $unit ] ) ) { | |
| 513 | - return; | |
| 514 | - } | |
| 515 | - | |
| 516 | - $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ]; | |
| 517 | - | |
| 518 | - $add_html['cols'] = 'cols="' . absint( $size ) . '"'; | |
| 519 | - } | |
| 520 | - | |
| 521 | - private static function add_html_length( $field, array &$add_html ) { | |
| 522 | - // Check for max setting and if this field accepts maxlength. | |
| 523 | - $fields = array( | |
| 524 | - 'textarea', | |
| 525 | - 'rte', | |
| 526 | - 'hidden', | |
| 527 | - 'file', | |
| 528 | - ); | |
| 529 | - | |
| 530 | - if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields ) ) { | |
| 531 | - return; | |
| 532 | - } | |
| 533 | - | |
| 534 | - if ( FrmAppHelper::is_admin_page( 'formidable' ) ) { | |
| 535 | - // Don't load on form builder page. | |
| 536 | - return; | |
| 537 | - } | |
| 538 | - | |
| 539 | - $add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"'; | |
| 540 | - } | |
| 541 | - | |
| 542 | - private static function add_html_placeholder( $field, array &$add_html, array &$class ) { | |
| 543 | - if ( $field['default_value'] != '' ) { | |
| 544 | - if ( is_array( $field['default_value'] ) ) { | |
| 545 | - $add_html['data-frmval'] = 'data-frmval="' . esc_attr( json_encode( $field['default_value'] ) ) . '"'; | |
| 546 | - } else { | |
| 547 | - self::add_frmval_to_input( $field, $add_html ); | |
| 548 | - } | |
| 549 | - } | |
| 550 | - | |
| 551 | - $field['placeholder'] = self::prepare_placeholder( $field ); | |
| 552 | - if ( $field['placeholder'] == '' || is_array( $field['placeholder'] ) ) { | |
| 553 | - // don't include a json placeholder | |
| 554 | - return; | |
| 555 | - } | |
| 556 | - | |
| 557 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 558 | - | |
| 559 | - if ( $frm_settings->use_html ) { | |
| 560 | - self::add_placeholder_to_input( $field, $add_html ); | |
| 561 | - } else { | |
| 562 | - self::add_frmval_to_input( $field, $add_html ); | |
| 563 | - | |
| 564 | - $class[] = 'frm_toggle_default'; | |
| 565 | - | |
| 566 | - if ( $field['value'] == $field['placeholder'] ) { | |
| 567 | - $class[] = 'frm_default'; | |
| 568 | - } | |
| 569 | - } | |
| 570 | - } | |
| 571 | - | |
| 572 | - /** | |
| 573 | - * Prepares field placeholder. | |
| 574 | - * | |
| 575 | - * @since 5.4 This doesn't call `FrmFieldsController::get_default_value_from_name()` anymore. | |
| 576 | - * | |
| 577 | - * @param array $field Field array. | |
| 578 | - * @return string | |
| 579 | - */ | |
| 580 | - private static function prepare_placeholder( $field ) { | |
| 581 | - $placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : ''; | |
| 582 | - | |
| 583 | - return $placeholder; | |
| 584 | - } | |
| 585 | - | |
| 586 | - /** | |
| 587 | - * If the label position is "inside", | |
| 588 | - * get the label to use as the placeholder | |
| 589 | - * | |
| 590 | - * @since 2.05 | |
| 591 | - * @since 5.4 Remove the logic code for "inside" label position. | |
| 592 | - * | |
| 593 | - * @param array $field | |
| 594 | - * | |
| 595 | - * @return string | |
| 596 | - */ | |
| 597 | - public static function get_default_value_from_name( $field ) { | |
| 598 | - return ''; | |
| 599 | - } | |
| 600 | - | |
| 601 | - /** | |
| 602 | - * Maybe add a blank placeholder option before any options | |
| 603 | - * in a dropdown. | |
| 604 | - * | |
| 605 | - * @since 4.04 | |
| 606 | - * @return bool True if placeholder was added. | |
| 607 | - */ | |
| 608 | - public static function add_placeholder_to_select( $field ) { | |
| 609 | - $placeholder = FrmField::get_option( $field, 'placeholder' ); | |
| 610 | - if ( empty( $placeholder ) ) { | |
| 611 | - $placeholder = self::get_default_value_from_name( $field ); | |
| 612 | - } | |
| 613 | - | |
| 614 | - if ( $placeholder !== '' ) { | |
| 615 | - ?> | |
| 616 | - <option value=""> | |
| 617 | - <?php echo esc_html( FrmField::get_option( $field, 'autocom' ) ? '' : $placeholder ); ?> | |
| 618 | - </option> | |
| 619 | - <?php | |
| 620 | - return true; | |
| 621 | - } | |
| 622 | - | |
| 623 | - return false; | |
| 624 | - } | |
| 625 | - | |
| 626 | - /** | |
| 627 | - * Use HMTL5 placeholder with js fallback | |
| 628 | - * | |
| 629 | - * @param array $field | |
| 630 | - * @param array $add_html | |
| 631 | - */ | |
| 632 | - private static function add_placeholder_to_input( $field, &$add_html ) { | |
| 633 | - if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) { | |
| 634 | - $add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"'; | |
| 635 | - } | |
| 636 | - } | |
| 637 | - | |
| 638 | - private static function add_frmval_to_input( $field, &$add_html ) { | |
| 639 | - if ( $field['placeholder'] != '' ) { | |
| 640 | - $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['placeholder'] ) . '"'; | |
| 641 | - | |
| 642 | - if ( 'select' === $field['type'] ) { | |
| 643 | - $add_html['data-frmplaceholder'] = 'data-frmplaceholder="' . esc_attr( $field['placeholder'] ) . '"'; | |
| 644 | - } | |
| 645 | - } | |
| 646 | - | |
| 647 | - if ( $field['default_value'] != '' ) { | |
| 648 | - $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"'; | |
| 649 | - } | |
| 650 | - } | |
| 651 | - | |
| 652 | - private static function add_validation_messages( $field, array &$add_html ) { | |
| 653 | - if ( FrmField::is_required( $field ) ) { | |
| 654 | - $required_message = FrmFieldsHelper::get_error_msg( $field, 'blank' ); | |
| 655 | - $add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"'; | |
| 656 | - self::maybe_add_html_required( $field, $add_html ); | |
| 657 | - } | |
| 658 | - | |
| 659 | - if ( ! FrmField::is_option_empty( $field, 'invalid' ) ) { | |
| 660 | - $invalid_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); | |
| 661 | - $add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"'; | |
| 662 | - } | |
| 663 | - | |
| 664 | - if ( ! empty( $add_html['data-reqmsg'] ) || ! empty( $add_html['data-invmsg'] ) ) { | |
| 665 | - self::maybe_add_error_html_for_js_validation( $field, $add_html ); | |
| 666 | - } | |
| 667 | - } | |
| 668 | - | |
| 669 | - /** | |
| 670 | - * @since 5.0.03 | |
| 671 | - * | |
| 672 | - * @param array $field | |
| 673 | - * @param array $add_html | |
| 674 | - */ | |
| 675 | - private static function maybe_add_error_html_for_js_validation( $field, array &$add_html ) { | |
| 676 | - $form = self::get_form_for_js_validation( $field ); | |
| 677 | - if ( false === $form ) { | |
| 678 | - return; | |
| 679 | - } | |
| 680 | - | |
| 681 | - $error_body = self::pull_custom_error_body_from_custom_html( $form, $field ); | |
| 682 | - if ( false !== $error_body ) { | |
| 683 | - $error_body = urlencode( $error_body ); | |
| 684 | - $add_html['data-error-html'] = 'data-error-html="' . esc_attr( $error_body ) . '"'; | |
| 685 | - } | |
| 686 | - } | |
| 687 | - | |
| 688 | - /** | |
| 689 | - * @since 5.0.03 | |
| 690 | - * | |
| 691 | - * @param array $field | |
| 692 | - * @return stdClass|false false if there is no form object found with JS validation active. | |
| 693 | - */ | |
| 694 | - private static function get_form_for_js_validation( $field ) { | |
| 695 | - global $frm_vars; | |
| 696 | - if ( ! empty( $frm_vars['js_validate_forms'] ) ) { | |
| 697 | - if ( isset( $frm_vars['js_validate_forms'][ $field['form_id'] ] ) ) { | |
| 698 | - return $frm_vars['js_validate_forms'][ $field['form_id'] ]; | |
| 699 | - } | |
| 700 | - if ( ! empty( $field['parent_form_id'] ) && isset( $frm_vars['js_validate_forms'][ $field['parent_form_id'] ] ) ) { | |
| 701 | - return $frm_vars['js_validate_forms'][ $field['parent_form_id'] ]; | |
| 702 | - } | |
| 703 | - } | |
| 704 | - return false; | |
| 705 | - } | |
| 706 | - | |
| 707 | - /** | |
| 708 | - * @param stdClass $form | |
| 709 | - * @param array $field | |
| 710 | - * @param array $errors | |
| 711 | - * @return string|false | |
| 712 | - */ | |
| 713 | - public static function pull_custom_error_body_from_custom_html( $form, $field, $errors = array() ) { | |
| 714 | - if ( empty( $field['custom_html'] ) ) { | |
| 715 | - return false; | |
| 716 | - } | |
| 717 | - | |
| 718 | - $custom_html = $field['custom_html']; | |
| 719 | - $custom_html = apply_filters( 'frm_before_replace_shortcodes', $custom_html, $field, $errors, $form ); | |
| 720 | - | |
| 721 | - $start = strpos( $custom_html, '[if error]' ); | |
| 722 | - if ( false === $start ) { | |
| 723 | - return false; | |
| 724 | - } | |
| 725 | - | |
| 726 | - $end = strpos( $custom_html, '[/if error]' ); | |
| 727 | - if ( false === $end || $end < $start ) { | |
| 728 | - return false; | |
| 729 | - } | |
| 730 | - | |
| 731 | - $error_body = substr( $custom_html, $start + 10, $end - $start - 10 ); | |
| 732 | - $default_html = array( | |
| 733 | - '<div class="frm_error" id="frm_error_field_[key]">[error]</div>', | |
| 734 | - '<div class="frm_error" role="alert" id="frm_error_field_[key]">[error]</div>', | |
| 735 | - '<div class="frm_error">[error]</div>', | |
| 736 | - '<div class="frm_error" role="alert">[error]</div>', | |
| 737 | - ); | |
| 738 | - | |
| 739 | - if ( in_array( $error_body, $default_html, true ) ) { | |
| 740 | - return false; | |
| 741 | - } | |
| 742 | - | |
| 743 | - return $error_body; | |
| 744 | - } | |
| 745 | - | |
| 746 | - /** | |
| 747 | - * If 'required' is added to a conditionally hidden field, the form won't | |
| 748 | - * submit in many browsers. Check to make sure the javascript to conditionally | |
| 749 | - * remove it is present if needed. | |
| 750 | - * | |
| 751 | - * @since 3.06.01 | |
| 752 | - * @param array $field | |
| 753 | - * @param array $add_html | |
| 754 | - */ | |
| 755 | - private static function maybe_add_html_required( $field, array &$add_html ) { | |
| 756 | - if ( in_array( $field['type'], array( 'file', 'data', 'lookup' ), true ) ) { | |
| 757 | - return; | |
| 758 | - } | |
| 759 | - | |
| 760 | - $include_html = FrmAppHelper::meets_min_pro_version( '3.06.01' ); | |
| 761 | - if ( $include_html ) { | |
| 762 | - $add_html['aria-required'] = 'aria-required="true"'; | |
| 763 | - } | |
| 764 | - } | |
| 765 | - | |
| 766 | - private static function add_shortcodes_to_html( $field, array &$add_html ) { | |
| 767 | - if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) { | |
| 768 | - return; | |
| 769 | - } | |
| 770 | - | |
| 771 | - if ( ! empty( $field['autocomplete'] ) ) { | |
| 772 | - unset( $field['shortcodes']['autocomplete'] ); | |
| 773 | - } | |
| 774 | - | |
| 775 | - foreach ( $field['shortcodes'] as $k => $v ) { | |
| 776 | - if ( 'opt' === $k ) { | |
| 777 | - continue; | |
| 778 | - } | |
| 779 | - | |
| 780 | - if ( is_numeric( $k ) && strpos( $v, '=' ) ) { | |
| 781 | - $add_html[] = $v; | |
| 782 | - } elseif ( ! empty( $k ) && isset( $add_html[ $k ] ) ) { | |
| 783 | - $add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] ); | |
| 784 | - } else { | |
| 785 | - $add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"'; | |
| 786 | - } | |
| 787 | - | |
| 788 | - unset( $k, $v ); | |
| 789 | - } | |
| 790 | - } | |
| 791 | - | |
| 792 | - /** | |
| 793 | - * Add pattern attribute | |
| 794 | - * | |
| 795 | - * @since 3.0 | |
| 796 | - * | |
| 797 | - * @param array $field | |
| 798 | - * @param array $add_html | |
| 799 | - */ | |
| 800 | - private static function add_pattern_attribute( $field, array &$add_html ) { | |
| 801 | - $has_format = FrmField::is_option_true_in_array( $field, 'format' ); | |
| 802 | - $format_field = FrmField::is_field_type( $field, 'text' ); | |
| 803 | - | |
| 804 | - if ( $field['type'] == 'phone' || ( $has_format && $format_field ) ) { | |
| 805 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 806 | - | |
| 807 | - if ( $frm_settings->use_html ) { | |
| 808 | - $format = FrmEntryValidate::phone_format( $field ); | |
| 809 | - $format = substr( $format, 2, - 1 ); | |
| 810 | - | |
| 811 | - $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"'; | |
| 812 | - } | |
| 813 | - } | |
| 814 | - } | |
| 815 | - | |
| 816 | - public static function check_value( $opt, $opt_key, $field ) { | |
| 817 | - if ( is_array( $opt ) ) { | |
| 818 | - if ( FrmField::is_option_true( $field, 'separate_value' ) ) { | |
| 819 | - $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) ); | |
| 820 | - } else { | |
| 821 | - $opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt ); | |
| 822 | - } | |
| 823 | - } | |
| 824 | - | |
| 825 | - return $opt; | |
| 826 | - } | |
| 827 | - | |
| 828 | - public static function check_label( $opt ) { | |
| 829 | - if ( is_array( $opt ) ) { | |
| 830 | - $opt = ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) ); | |
| 831 | - } | |
| 832 | - | |
| 833 | - return $opt; | |
| 834 | - } | |
| 835 | - | |
| 836 | - /** | |
| 837 | - * @deprecated 4.0 | |
| 838 | - */ | |
| 839 | - public static function update_ajax_option() { | |
| 840 | - _deprecated_function( __METHOD__, '4.0' ); | |
| 841 | - FrmAppHelper::permission_check( 'frm_edit_forms' ); | |
| 842 | - check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 843 | - | |
| 844 | - $field_id = FrmAppHelper::get_post_param( 'field', 0, 'absint' ); | |
| 845 | - if ( ! $field_id ) { | |
| 846 | - wp_die(); | |
| 847 | - } | |
| 848 | - | |
| 849 | - $field = FrmField::getOne( $field_id ); | |
| 850 | - | |
| 851 | - if ( isset( $_POST['separate_value'] ) ) { | |
| 852 | - $new_val = FrmField::is_option_true( $field, 'separate_value' ) ? 0 : 1; | |
| 853 | - | |
| 854 | - $field->field_options['separate_value'] = $new_val; | |
| 855 | - unset( $new_val ); | |
| 856 | - } | |
| 857 | - | |
| 858 | - FrmField::update( | |
| 859 | - $field_id, | |
| 860 | - array( | |
| 861 | - 'field_options' => $field->field_options, | |
| 862 | - 'form_id' => $field->form_id, | |
| 863 | - ) | |
| 864 | - ); | |
| 865 | - wp_die(); | |
| 866 | - } | |
| 867 | - | |
| 868 | - /** | |
| 869 | - * @deprecated 4.0 | |
| 870 | - */ | |
| 871 | - public static function import_choices() { | |
| 872 | - _deprecated_function( __METHOD__, '4.0' ); | |
| 873 | - wp_die(); | |
| 874 | - } | |
| 875 | - | |
| 876 | - /** | |
| 877 | - * Add Single Option or Other Option. | |
| 878 | - * | |
| 879 | - * @deprecated 4.0 Moved to Pro for Other option only. | |
| 880 | - */ | |
| 881 | - public static function add_option() { | |
| 882 | - _deprecated_function( __METHOD__, '4.0', 'FrmProFieldsController::add_other_option' ); | |
| 883 | - } | |
| 884 | - | |
| 885 | - /** | |
| 886 | - * @deprecated 4.0 | |
| 887 | - */ | |
| 888 | - public static function update_order() { | |
| 889 | - FrmDeprecated::update_order(); | |
| 890 | - } | |
| 891 | - | |
| 892 | - /** | |
| 893 | - * @deprecated 3.0 | |
| 894 | - * @codeCoverageIgnore | |
| 895 | - */ | |
| 896 | - public static function edit_name( $field = 'name', $id = '' ) { | |
| 897 | - FrmDeprecated::edit_name( $field, $id ); | |
| 898 | - } | |
| 899 | - | |
| 900 | - /** | |
| 901 | - * @deprecated 3.0 | |
| 902 | - * @codeCoverageIgnore | |
| 903 | - * | |
| 904 | - * @param int $field_id | |
| 905 | - * @param array $values | |
| 906 | - * @param int $form_id | |
| 907 | - * | |
| 908 | - * @return array | |
| 909 | - */ | |
| 910 | - public static function include_single_field( $field_id, $values, $form_id = 0 ) { | |
| 911 | - return FrmDeprecated::include_single_field( $field_id, $values, $form_id ); | |
| 912 | - } | |
| 407 | + return $type; | |
| 408 | + } | |
| 409 | + | |
| 410 | + public static function display_field_options($display){ | |
| 411 | + switch($display['type']){ | |
| 412 | + case 'captcha': | |
| 413 | + $display['required'] = false; | |
| 414 | + $display['invalid'] = true; | |
| 415 | + $display['default_blank'] = false; | |
| 416 | + break; | |
| 417 | + case 'radio': | |
| 418 | + $display['default_blank'] = false; | |
| 419 | + break; | |
| 420 | + case 'text': | |
| 421 | + case 'textarea': | |
| 422 | + $display['size'] = true; | |
| 423 | + $display['clear_on_focus'] = true; | |
| 424 | + break; | |
| 425 | + case 'select': | |
| 426 | + $display['size'] = true; | |
| 427 | + break; | |
| 428 | + } | |
| 429 | + | |
| 430 | + return $display; | |
| 431 | + } | |
| 432 | + | |
| 433 | + public static function input_html($field, $echo=true){ | |
| 434 | + global $frm_settings, $frm_vars; | |
| 435 | + | |
| 436 | + $class = ''; //$field['type']; | |
| 437 | + | |
| 438 | + if ( is_admin() && (!isset($frm_vars['preview']) || !$frm_vars['preview']) && !in_array($field['type'], array('scale', 'radio', 'checkbox', 'data')) ) { | |
| 439 | + $class .= 'dyn_default_value'; | |
| 440 | + } | |
| 441 | + | |
| 442 | + $add_html = ''; | |
| 443 | + | |
| 444 | + if(isset($field['size']) and $field['size'] > 0){ | |
| 445 | + if(!in_array($field['type'], array('textarea', 'select', 'data', 'time', 'hidden'))) | |
| 446 | + $add_html .= ' size="'. $field['size'] .'"'; | |
| 447 | + $class .= " auto_width"; | |
| 448 | + } | |
| 449 | + | |
| 450 | + if ( isset($field['max']) && !in_array($field['type'], array('textarea', 'rte', 'hidden')) && !empty($field['max']) && (!is_admin() || !isset($_GET) || !isset($_GET['page']) || $_GET['page'] != 'formidable') ) { | |
| 451 | + $add_html .= ' maxlength="'. $field['max'] .'"'; | |
| 452 | + } | |
| 453 | + | |
| 454 | + if(!is_admin() or defined('DOING_AJAX') or !isset($_GET) or !isset($_GET['page']) or $_GET['page'] == 'formidable-entries'){ | |
| 455 | + /*if(isset($field['required']) and $field['required']){ | |
| 456 | + $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action'; | |
| 457 | + $action = FrmAppHelper::get_param($action); | |
| 458 | + | |
| 459 | + //if($field['type'] != 'checkbox') | |
| 460 | + // $add_html .= ' required="required"'; | |
| 461 | + | |
| 462 | + if($field['type'] == 'file' and $action == 'edit'){ | |
| 463 | + //don't add the required class if this is a file upload when editing | |
| 464 | + }else{ | |
| 465 | + $class .= " required"; | |
| 466 | + } | |
| 467 | + unset($action); | |
| 468 | + }*/ | |
| 469 | + | |
| 470 | + if(isset($field['clear_on_focus']) and $field['clear_on_focus'] and !empty($field['default_value'])){ | |
| 471 | + | |
| 472 | + if($frm_settings->use_html and !in_array($field['type'], array('select', 'radio', 'checkbox', 'hidden'))){ | |
| 473 | + $add_html .= ' placeholder="'. esc_attr($field['default_value']) .'"'; | |
| 474 | + FrmAppHelper::load_scripts('jquery-placeholder'); | |
| 475 | + }else if(!$frm_settings->use_html){ | |
| 476 | + $val = str_replace(array("\r\n", "\n"), '\r', addslashes(str_replace(''', "'", esc_attr($field['default_value'])))); | |
| 477 | + $add_html .= ' onfocus="frmClearDefault('."'". $val ."'". ',this)" onblur="frmReplaceDefault('."'". $val ."'". ',this)"'; | |
| 478 | + if($field['value'] == $field['default_value']) | |
| 479 | + $class .= ' frm_default'; | |
| 480 | + } | |
| 481 | + } | |
| 482 | + } | |
| 483 | + | |
| 484 | + if(isset($field['input_class']) and !empty($field['input_class'])) | |
| 485 | + $class .= ' '. $field['input_class']; | |
| 486 | + | |
| 487 | + $class = apply_filters('frm_field_classes', $class, $field); | |
| 488 | + | |
| 489 | + if(!empty($class)) | |
| 490 | + $add_html .= ' class="'. trim($class) .'"'; | |
| 491 | + | |
| 492 | + if ( isset($field['shortcodes']) && !empty($field['shortcodes']) ) { | |
| 493 | + foreach ( $field['shortcodes'] as $k => $v ) { | |
| 494 | + if ( 'opt' === $k || (!is_numeric($k) && strpos($add_html, " $k=")) ) { | |
| 495 | + continue; | |
| 496 | + } | |
| 497 | + | |
| 498 | + if ( is_numeric($k) && strpos($v, '=') ) { | |
| 499 | + $add_html .= ' '. $v; | |
| 500 | + } else { | |
| 501 | + $add_html .= ' '. $k .'="'. $v .'"'; | |
| 502 | + } | |
| 503 | + | |
| 504 | + unset($k, $v); | |
| 505 | + } | |
| 506 | + } | |
| 507 | + | |
| 508 | + if($echo) | |
| 509 | + echo $add_html; | |
| 510 | + | |
| 511 | + return $add_html; | |
| 512 | + } | |
| 513 | + | |
| 514 | + public static function check_value($opt, $opt_key, $field){ | |
| 515 | + if(is_array($opt)){ | |
| 516 | + if(isset($field['separate_value']) and $field['separate_value']){ | |
| 517 | + $opt = isset($opt['value']) ? $opt['value'] : (isset($opt['label']) ? $opt['label'] : reset($opt)); | |
| 518 | + }else{ | |
| 519 | + $opt = (isset($opt['label']) ? $opt['label'] : reset($opt)); | |
| 520 | + } | |
| 521 | + } | |
| 522 | + return $opt; | |
| 523 | + } | |
| 524 | + | |
| 525 | + public static function check_label($opt, $opt_key, $field){ | |
| 526 | + if(is_array($opt)) | |
| 527 | + $opt = (isset($opt['label']) ? $opt['label'] : reset($opt)); | |
| 528 | + | |
| 529 | + return $opt; | |
| 530 | + } | |
| 913 | 531 | } |