public static function update($field_options, $field, $values) { $defaults = FrmProFieldsHelper::get_default_field_opts(false, $field); unset($defaults['post_field'], $defaults['custom_field'], $defaults['taxonomy'], $defaults['exclude_cat']); $defaults['minnum'] = 0; $defaults['maxnum'] = 9999; foreach ($defaults as $opt => $default) { $field_options[$opt] = isset($values['field_options'][$opt . '_' . $field->id]) ? $values['field_options'][$opt . '_' . $field->id] : $default; unset($opt, $default); } foreach ($field_options['hide_field'] as $i => $f) { if (empty($f)) { unset($field_options['hide_field'][$i], $field_options['hide_field_cond'][$i]); if (isset($field_options['hide_opt']) && is_array($field_options['hide_opt'])) { unset($field_options['hide_opt'][$i]); } } unset($i, $f); } if ($field->type == 'scale') { if ((int) $field_options['maxnum'] >= 99) { $field_options['maxnum'] = 10; } $options = range($field_options['minnum'], $field_options['maxnum']); FrmField::update($field->id, array('options' => serialize($options))); } else { if ($field->type == 'hidden' && isset($field_options['required']) && $field_options['required']) { $field_options['required'] = false; } } return $field_options; }
/** * Remove form_select from all non-repeating sections */ private static function migrate_to_30() { // Get all section fields $dividers = FrmField::getAll(array('fi.type' => 'divider')); // Remove form_select for non-repeating sections foreach ($dividers as $d) { if (FrmField::is_repeating_field($d)) { continue; } if (FrmField::is_option_value_in_object($d, 'form_select')) { $d->field_options['form_select'] = ''; FrmField::update($d->id, array('field_options' => maybe_serialize($d->field_options))); } } }
/** * Update form_select for all imported repeating sections and embedded forms * * @since 2.0.09 * @param array $repeat_fields - old form ID as keys and section field IDs as items * @param array $imported_forms */ private static function update_repeat_field_options($repeat_fields, $imported_forms) { foreach ($repeat_fields as $old_form_id => $new_repeat_fields) { foreach ($new_repeat_fields as $repeat_field_id) { // Get section/embed form field $repeat_field = FrmField::getOne($repeat_field_id); $field_opts = maybe_unserialize($repeat_field->field_options); if (!isset($imported_forms[$old_form_id])) { return; } $field_opts['form_select'] = $imported_forms[$old_form_id]; // update form_select now FrmField::update($repeat_field_id, array('field_options' => maybe_serialize($field_opts))); } } }
public static function update_order() { check_ajax_referer('frm_ajax', 'nonce'); $fields = FrmAppHelper::get_post_param('frm_field_id'); foreach ((array) $fields as $position => $item) { FrmField::update(absint($item), array('field_order' => absint($position))); } wp_die(); }
function update_object($field_id, $values) { return FrmField::update($field_id, $values); }
/** * Change field size from character to pixel -- Multiply by 9 */ private function migrate_to_17() { global $wpdb; $pixel_conversion = 9; // Get query arguments $field_types = array('textarea', 'text', 'number', 'email', 'url', 'rte', 'date', 'phone', 'password', 'image', 'tag', 'file'); $query = array('type' => $field_types, 'field_options like' => 's:4:"size";', 'field_options not like' => 's:4:"size";s:0:'); // Get results $fields = FrmDb::get_results($this->fields, $query, 'id, field_options'); $updated = 0; foreach ($fields as $f) { $f->field_options = maybe_unserialize($f->field_options); if (empty($f->field_options['size']) || !is_numeric($f->field_options['size'])) { continue; } $f->field_options['size'] = round($pixel_conversion * (int) $f->field_options['size']); $f->field_options['size'] .= 'px'; $u = FrmField::update($f->id, array('field_options' => $f->field_options)); if ($u) { $updated++; } unset($f); } // Change the characters in widgets to pixels $widgets = get_option('widget_frm_show_form'); if (empty($widgets)) { return; } $widgets = maybe_unserialize($widgets); foreach ($widgets as $k => $widget) { if (!is_array($widget) || !isset($widget['size'])) { continue; } $size = round($pixel_conversion * (int) $widget['size']); $size .= 'px'; $widgets[$k]['size'] = $size; } update_option('widget_frm_show_form', $widgets); }
/** * Import all fields for a form * @since 2.0.13 * * TODO: Cut down on params */ private static function import_xml_fields($xml_fields, $form_id, $this_form, &$form_fields, &$imported) { foreach ($xml_fields as $field) { $f = array('id' => (int) $field->id, 'field_key' => (string) $field->field_key, 'name' => (string) $field->name, 'description' => (string) $field->description, 'type' => (string) $field->type, 'default_value' => FrmAppHelper::maybe_json_decode((string) $field->default_value), 'field_order' => (int) $field->field_order, 'form_id' => (int) $form_id, 'required' => (int) $field->required, 'options' => FrmAppHelper::maybe_json_decode((string) $field->options), 'field_options' => FrmAppHelper::maybe_json_decode((string) $field->field_options)); if (is_array($f['default_value']) && in_array($f['type'], array('text', 'email', 'url', 'textarea', 'number', 'phone', 'date', 'time', 'hidden', 'password', 'tag', 'image'))) { if (count($f['default_value']) === 1) { $f['default_value'] = '[' . reset($f['default_value']) . ']'; } else { $f['default_value'] = reset($f['default_value']); } } $f = apply_filters('frm_duplicated_field', $f); self::maybe_update_form_select($f, $imported); if (!empty($this_form)) { // check for field to edit by field id if (isset($form_fields[$f['id']])) { FrmField::update($f['id'], $f); $imported['updated']['fields']++; unset($form_fields[$f['id']]); //unset old field key if (isset($form_fields[$f['field_key']])) { unset($form_fields[$f['field_key']]); } } else { if (isset($form_fields[$f['field_key']])) { // check for field to edit by field key unset($f['id']); FrmField::update($form_fields[$f['field_key']], $f); $imported['updated']['fields']++; unset($form_fields[$form_fields[$f['field_key']]]); //unset old field id unset($form_fields[$f['field_key']]); //unset old field key } else { $new_id = FrmField::create($f); if ($new_id == false) { continue; } // if no matching field id or key in this form, create the field $imported['imported']['fields']++; } } } else { $new_id = FrmField::create($f); if ($new_id == false) { continue; } $imported['imported']['fields']++; } unset($field, $new_id); } }
function _modify_dynamic_field_for_test($test, $args) { $update_field = false; if (strpos($test, 'readonly') !== false) { $update_field = true; $new_field_options = $args['field']->field_options; $new_field_options['read_only'] = true; } if (in_array($test, array('radio', 'select'))) { $update_field = true; $new_field_options = $args['field']->field_options; $new_field_options['read_only'] = false; $new_field_options['data_type'] = $test; $args['field']->field_options['data_type'] = $test; } if ($update_field) { $field_values = array('field_options' => $new_field_options); FrmField::update($args['field']->id, $field_values); } }
public static function import_xml_forms($forms, $imported) { $frm_form = new FrmForm(); $frm_field = new FrmField(); foreach ($forms as $item) { $form = array('id' => (int) $item->id, 'form_key' => (string) $item->form_key, 'name' => (string) $item->name, 'description' => (string) $item->description, 'options' => (string) $item->options, 'logged_in' => (int) $item->logged_in, 'is_template' => (int) $item->is_template, 'default_template' => (int) $item->default_template, 'editable' => (int) $item->editable, 'status' => (string) $item->status, 'created_at' => date('Y-m-d H:i:s', strtotime((string) $item->created_at))); $form['options'] = FrmAppHelper::maybe_json_decode($form['options']); // if template, allow to edit if form keys match, otherwise, creation date must also match $edit_query = array('form_key' => $form['form_key'], 'is_template' => $form['is_template']); if (!$form['is_template']) { $edit_query['created_at'] = $form['created_at']; } $edit_query = apply_filters('frm_match_xml_form', $edit_query, $form); $this_form = $frm_form->getAll($edit_query, '', 1); unset($edit_query); if (!empty($this_form)) { $form_id = $this_form->id; $frm_form->update($form_id, $form); $imported['updated']['forms']++; $form_fields = $frm_field->getAll(array('fi.form_id' => $form_id), 'field_order'); $old_fields = array(); foreach ($form_fields as $f) { $old_fields[$f->id] = $f; $old_fields[$f->field_key] = $f->id; unset($f); } $form_fields = $old_fields; unset($old_fields); } else { //form does not exist, so create it if ($form_id = $frm_form->create($form)) { $imported['imported']['forms']++; } } foreach ($item->field as $field) { $f = array('id' => (int) $field->id, 'field_key' => (string) $field->field_key, 'name' => (string) $field->name, 'description' => (string) $field->description, 'type' => (string) $field->type, 'default_value' => FrmAppHelper::maybe_json_decode((string) $field->default_value), 'field_order' => (int) $field->field_order, 'form_id' => (int) $form_id, 'required' => (int) $field->required, 'options' => FrmAppHelper::maybe_json_decode((string) $field->options), 'field_options' => FrmAppHelper::maybe_json_decode((string) $field->field_options)); if (is_array($f['default_value']) && in_array($f['type'], array('text', 'email', 'url', 'textarea', 'number', 'phone', 'date', 'time', 'image', 'hidden', 'password', 'tag'))) { if (count($f['default_value']) === 1) { $f['default_value'] = '[' . reset($f['default_value']) . ']'; } else { $f['default_value'] = reset($f['default_value']); } } $f = apply_filters('frm_duplicated_field', $f); if ($this_form) { // check for field to edit by field id if (isset($form_fields[$f['id']])) { $frm_field->update($f['id'], $f); $imported['updated']['fields']++; unset($form_fields[$f['id']]); //unset old field key if (isset($form_fields[$f['field_key']])) { unset($form_fields[$f['field_key']]); } } else { if (isset($form_fields[$f['field_key']])) { // check for field to edit by field key unset($f['id']); $frm_field->update($form_fields[$f['field_key']], $f); $imported['updated']['fields']++; unset($form_fields[$form_fields[$f['field_key']]]); //unset old field id unset($form_fields[$f['field_key']]); //unset old field key } else { if ($frm_field->create($f)) { // if no matching field id or key in this form, create the field $imported['imported']['fields']++; } } } } else { if ($frm_field->create($f)) { $imported['imported']['fields']++; } } unset($field); } // Delete any fields attached to this form that were not included in the template if (isset($form_fields) && !empty($form_fields)) { foreach ($form_fields as $field) { if (is_object($field)) { $frm_field->destroy($field->id); } unset($field); } unset($form_fields); } // Update field ids/keys to new ones do_action('frm_after_duplicate_form', $form_id, $form); $imported['forms'][(int) $item->id] = $form_id; unset($form); unset($item); } unset($frm_form); unset($frm_field); return $imported; }
/** * Create end section field if it doesn't exist. This is for migration from < 2.0 * Fix any ordering that may be messed up */ public static function maybe_create_end_section(&$open, &$reset_fields, &$add_order, $end_section_values, $field, $move = 'no') { if (!$open) { return; } $end_section_values['field_order'] = $field->field_order + 1; FrmField::create($end_section_values); if ($move == 'move') { // bump the order of current field unless we're at the end of the form FrmField::update($field->id, array('field_order' => $field->field_order + 2)); } $add_order += 2; $open = false; $reset_fields = true; }
/** * Triggered when the repeat option is toggled on the form builder page * * When a field is changed to repeat: * - Get the metas for the fields in the section * - Create an entry and change the entry id on those metas * * When a field is changed to not repeat: * - Change the entry id on all metas for the first entry to the parent entry id * - Delete the other entries and meta * * @since 2.0 */ public static function update_for_repeat($args) { if ($args['checked']) { // Switching to repeatable self::move_fields_to_form($args['children'], $args['form_id']); self::move_entries_to_child_form($args); $form_select = $args['form_id']; } else { // Switching to non-repeatable self::move_fields_to_form($args['children'], $args['parent_form_id']); self::move_entries_to_parent_form($args); $form_select = ''; } // update the repeat setting and form_select $section = FrmField::getOne($args['field_id']); $section->field_options['repeat'] = $args['checked']; $section->field_options['form_select'] = $form_select; FrmField::update($args['field_id'], array('field_options' => $section->field_options)); }
/** * Import all fields for a form * @since 2.0.13 * * TODO: Cut down on params */ private static function import_xml_fields($xml_fields, $form_id, $this_form, &$form_fields, &$imported) { $in_section = 0; foreach ($xml_fields as $field) { $f = self::fill_field($field, $form_id); if (is_array($f['default_value']) && in_array($f['type'], array('text', 'email', 'url', 'textarea', 'number', 'phone', 'date', 'time', 'hidden', 'password', 'tag', 'image'))) { if (count($f['default_value']) === 1) { $f['default_value'] = '[' . reset($f['default_value']) . ']'; } else { $f['default_value'] = reset($f['default_value']); } } self::maybe_update_in_section_variable($in_section, $f); self::maybe_update_form_select($f, $imported); self::maybe_update_get_values_form_setting($imported, $f); if (!empty($this_form)) { // check for field to edit by field id if (isset($form_fields[$f['id']])) { FrmField::update($f['id'], $f); $imported['updated']['fields']++; unset($form_fields[$f['id']]); //unset old field key if (isset($form_fields[$f['field_key']])) { unset($form_fields[$f['field_key']]); } } else { if (isset($form_fields[$f['field_key']])) { // check for field to edit by field key unset($f['id']); FrmField::update($form_fields[$f['field_key']], $f); $imported['updated']['fields']++; unset($form_fields[$form_fields[$f['field_key']]]); //unset old field id unset($form_fields[$f['field_key']]); //unset old field key } else { // if no matching field id or key in this form, create the field self::create_imported_field($f, $imported); } } } else { self::create_imported_field($f, $imported); } } }
/** * @return array */ public static function update_fields($id, $values) { if (!isset($values['options']) && !isset($values['item_meta']) && !isset($values['field_options'])) { return $values; } $all_fields = FrmField::get_all_for_form($id); if (empty($all_fields)) { return $values; } if (!isset($values['item_meta'])) { $values['item_meta'] = array(); } $field_array = array(); $existing_keys = array_keys($values['item_meta']); foreach ($all_fields as $fid) { if (!in_array($fid->id, $existing_keys) && (isset($values['frm_fields_submitted']) && in_array($fid->id, $values['frm_fields_submitted'])) || isset($values['options'])) { $values['item_meta'][$fid->id] = ''; } $field_array[$fid->id] = $fid; } unset($all_fields); foreach ($values['item_meta'] as $field_id => $default_value) { if (isset($field_array[$field_id])) { $field = $field_array[$field_id]; } else { $field = FrmField::getOne($field_id); } if (!$field) { continue; } if (isset($values['options']) || isset($values['field_options']['custom_html_' . $field_id])) { //updating the settings page if (isset($values['field_options']['custom_html_' . $field_id])) { $field->field_options['custom_html'] = isset($values['field_options']['custom_html_' . $field_id]) ? $values['field_options']['custom_html_' . $field_id] : (isset($field->field_options['custom_html']) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html($field->type)); $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); FrmField::update($field_id, array('field_options' => $field->field_options)); } else { if ($field->type == 'hidden' || $field->type == 'user_id') { $prev_opts = $field->field_options; $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); if ($prev_opts != $field->field_options) { FrmField::update($field_id, array('field_options' => $field->field_options)); } unset($prev_opts); } } } if ((isset($values['options']) || isset($values['field_options']['custom_html_' . $field_id])) && !defined('WP_IMPORTING')) { continue; } //updating the form foreach (array('size', 'max', 'label', 'invalid', 'blank', 'classes', 'captcha_size') as $opt) { $field->field_options[$opt] = isset($values['field_options'][$opt . '_' . $field_id]) ? trim($values['field_options'][$opt . '_' . $field_id]) : ''; } $field->field_options['required_indicator'] = isset($values['field_options']['required_indicator_' . $field_id]) ? trim($values['field_options']['required_indicator_' . $field_id]) : '*'; $field->field_options['separate_value'] = isset($values['field_options']['separate_value_' . $field_id]) ? trim($values['field_options']['separate_value_' . $field_id]) : 0; $field->field_options = apply_filters('frm_update_field_options', $field->field_options, $field, $values); $default_value = maybe_serialize($values['item_meta'][$field_id]); $field_key = isset($values['field_options']['field_key_' . $field_id]) ? $values['field_options']['field_key_' . $field_id] : $field->field_key; $required = isset($values['field_options']['required_' . $field_id]) ? $values['field_options']['required_' . $field_id] : false; $field_type = isset($values['field_options']['type_' . $field_id]) ? $values['field_options']['type_' . $field_id] : $field->type; $field_description = isset($values['field_options']['description_' . $field_id]) ? $values['field_options']['description_' . $field_id] : $field->description; FrmField::update($field_id, array('field_key' => $field_key, 'type' => $field_type, 'default_value' => $default_value, 'field_options' => $field->field_options, 'description' => $field_description, 'required' => $required)); FrmField::delete_form_transient($field->form_id); } return $values; }
/** * @return array */ public static function update_fields($id, $values) { if (!isset($values['options']) && !isset($values['item_meta']) && !isset($values['field_options'])) { return $values; } $all_fields = FrmField::get_all_for_form($id); if (empty($all_fields)) { return $values; } if (!isset($values['item_meta'])) { $values['item_meta'] = array(); } $field_array = array(); $existing_keys = array_keys($values['item_meta']); foreach ($all_fields as $fid) { if (!in_array($fid->id, $existing_keys) && (isset($values['frm_fields_submitted']) && in_array($fid->id, $values['frm_fields_submitted'])) || isset($values['options'])) { $values['item_meta'][$fid->id] = ''; } $field_array[$fid->id] = $fid; } unset($all_fields); foreach ($values['item_meta'] as $field_id => $default_value) { if (isset($field_array[$field_id])) { $field = $field_array[$field_id]; } else { $field = FrmField::getOne($field_id); } if (!$field) { continue; } $is_settings_page = isset($values['options']) || isset($values['field_options']['custom_html_' . $field_id]); if ($is_settings_page) { //updating the settings page if (isset($values['field_options']['custom_html_' . $field_id])) { $field->field_options['custom_html'] = isset($values['field_options']['custom_html_' . $field_id]) ? $values['field_options']['custom_html_' . $field_id] : (isset($field->field_options['custom_html']) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html($field->type)); $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); FrmField::update($field_id, array('field_options' => $field->field_options)); } else { if ($field->type == 'hidden' || $field->type == 'user_id') { $prev_opts = $field->field_options; $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); if ($prev_opts != $field->field_options) { FrmField::update($field_id, array('field_options' => $field->field_options)); } unset($prev_opts); } } } if ($is_settings_page && !defined('WP_IMPORTING')) { continue; } //updating the form $update_options = FrmFieldsHelper::get_default_field_opts($field->type, $field, true); unset($update_options['custom_html']); // don't check for POST html $update_options = apply_filters('frm_field_options_to_update', $update_options); foreach ($update_options as $opt => $default) { $field->field_options[$opt] = isset($values['field_options'][$opt . '_' . $field_id]) ? trim(sanitize_text_field($values['field_options'][$opt . '_' . $field_id])) : $default; } $field->field_options = apply_filters('frm_update_field_options', $field->field_options, $field, $values); $default_value = maybe_serialize($values['item_meta'][$field_id]); $field_key = isset($values['field_options']['field_key_' . $field_id]) ? $values['field_options']['field_key_' . $field_id] : $field->field_key; $required = isset($values['field_options']['required_' . $field_id]) ? $values['field_options']['required_' . $field_id] : false; $field_type = isset($values['field_options']['type_' . $field_id]) ? $values['field_options']['type_' . $field_id] : $field->type; $field_description = isset($values['field_options']['description_' . $field_id]) ? $values['field_options']['description_' . $field_id] : $field->description; FrmField::update($field_id, array('field_key' => $field_key, 'type' => $field_type, 'default_value' => $default_value, 'field_options' => $field->field_options, 'description' => $field_description, 'required' => $required)); FrmField::delete_form_transient($field->form_id); } self::clear_form_cache(); return $values; }
public static function update_order() { if (isset($_POST) and isset($_POST['frm_field_id'])) { $frm_field = new FrmField(); foreach ($_POST['frm_field_id'] as $position => $item) { $frm_field->update($item, array('field_order' => $position)); } } die; }