/** * Save form in database * * @since 1.2.3 * * @param array $form Form config * @param int|null $entryid Optional. ID of entry to send. If not provided, will be determined from current POST data '_entry_id' key. */ public static function save_in_db($form, $entryid = null) { global $wpdb, $processed_meta; if (!empty($form['db_support'])) { // new entry or update if (empty($entryid)) { $entryid = Caldera_Forms::get_field_data('_entry_id', $form); foreach ($form['fields'] as $field_id => $field) { // add new and update Caldera_Forms::save_field_data($field, $entryid, $form); } // save form meta if any if (isset($processed_meta[$form['ID']])) { foreach ($processed_meta[$form['ID']] as $process_id => $meta_data) { foreach ($meta_data as $meta_key => $meta_value) { if (is_array($meta_value)) { foreach ($meta_value as &$check_value) { if (is_array($check_value)) { foreach ($check_value as &$sub_check_value) { if (!is_array($sub_check_value)) { $sub_check_value = Caldera_Forms::do_magic_tags($sub_check_value); } } } else { $check_value = Caldera_Forms::do_magic_tags($check_value); } } } else { $meta_value = Caldera_Forms::do_magic_tags($meta_value); } if (count($meta_value) > 1) { $meta_value = json_encode($meta_value); } else { $meta_value = $meta_value[0]; if (is_array($meta_value) || is_object($meta_value)) { $meta_value = json_encode($meta_value); } } $meta_entry = array('entry_id' => $entryid, 'process_id' => $process_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value); $wpdb->insert($wpdb->prefix . 'cf_form_entry_meta', $meta_entry); } } } // update status $wpdb->update($wpdb->prefix . 'cf_form_entries', array('status' => 'active'), array('id' => $entryid)); } else { // do update foreach ($form['fields'] as $field_id => $field) { // add new and update Caldera_Forms::update_field_data($field, $entryid, $form); } if (isset($processed_meta[$form['ID']])) { foreach ($processed_meta[$form['ID']] as $process_id => $meta_data) { foreach ($meta_data as $meta_key => $meta_value) { if (count($meta_value) > 1) { $meta_value = json_encode($meta_value); } else { $meta_value = $meta_value[0]; } $meta_entry = array('entry_id' => $entryid, 'process_id' => $process_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value); $wpdb->insert($wpdb->prefix . 'cf_form_entry_meta', $meta_entry); } } } // return return; } } }