function upgrade($old_db_version = false) { global $wpdb, $frm_db_version; //$frm_db_version is the version of the database we're moving to $old_db_version = (double) $old_db_version; if (!$old_db_version) { $old_db_version = get_option('frm_db_version'); } if ($frm_db_version != $old_db_version) { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset_collate = ''; if ($wpdb->has_cap('collation')) { if (!empty($wpdb->charset)) { $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; } if (!empty($wpdb->collate)) { $charset_collate .= " COLLATE {$wpdb->collate}"; } } /* Create/Upgrade Fields Table */ $sql = "CREATE TABLE {$this->fields} (\n id int(11) NOT NULL auto_increment,\n field_key varchar(255) default NULL,\n name text default NULL,\n description text default NULL,\n type text default NULL,\n default_value longtext default NULL,\n options longtext default NULL,\n field_order int(11) default 0,\n required int(1) default NULL,\n field_options longtext default NULL,\n form_id int(11) default NULL,\n created_at datetime NOT NULL,\n PRIMARY KEY (id),\n KEY form_id (form_id),\n UNIQUE KEY field_key (field_key)\n ) {$charset_collate};"; dbDelta($sql); /* Create/Upgrade Forms Table */ $sql = "CREATE TABLE {$this->forms} (\n id int(11) NOT NULL auto_increment,\n form_key varchar(255) default NULL,\n name varchar(255) default NULL,\n description text default NULL,\n logged_in boolean default NULL,\n editable boolean default NULL,\n is_template boolean default 0,\n default_template boolean default 0,\n status varchar(255) default NULL,\n prli_link_id int(11) default NULL,\n options longtext default NULL,\n created_at datetime NOT NULL,\n PRIMARY KEY (id),\n UNIQUE KEY form_key (form_key)\n ) {$charset_collate};"; dbDelta($sql); /* Create/Upgrade Items Table */ $sql = "CREATE TABLE {$this->entries} (\n id int(11) NOT NULL auto_increment,\n item_key varchar(255) default NULL,\n name varchar(255) default NULL,\n description text default NULL,\n ip text default NULL,\n form_id int(11) default NULL,\n post_id int(11) default NULL,\n user_id int(11) default NULL,\n parent_item_id int(11) default NULL,\n updated_by int(11) default NULL,\n created_at datetime NOT NULL,\n updated_at datetime NOT NULL,\n PRIMARY KEY (id),\n KEY form_id (form_id),\n KEY post_id (post_id),\n KEY user_id (user_id),\n KEY parent_item_id (parent_item_id),\n UNIQUE KEY item_key (item_key)\n ) {$charset_collate};"; dbDelta($sql); /* Create/Upgrade Meta Table */ $sql = "CREATE TABLE {$this->entry_metas} (\n id int(11) NOT NULL auto_increment,\n meta_value longtext default NULL,\n field_id int(11) NOT NULL,\n item_id int(11) NOT NULL,\n created_at datetime NOT NULL,\n PRIMARY KEY (id),\n KEY field_id (field_id),\n KEY item_id (item_id)\n ) {$charset_collate};"; dbDelta($sql); /**** MIGRATE DATA ****/ if ($frm_db_version >= 1.03 and $old_db_version < 1.03) { global $frm_entry; $all_entries = $frm_entry->getAll(); foreach ($all_entries as $ent) { $opts = maybe_unserialize($ent->description); if (is_array($opts)) { $wpdb->update($this->entries, array('ip' => $opts['ip']), array('id' => $ent->id)); } } } if ($frm_db_version >= 4 and $old_db_version < 4) { $user_ids = FrmEntryMeta::getAll("fi.type='user_id'"); foreach ($user_ids as $user_id) { $wpdb->update($this->entries, array('user_id' => $user_id->meta_value), array('id' => $user_id->item_id)); } } if ($frm_db_version >= 6 and $old_db_version < 6) { $fields = $wpdb->get_results("SELECT id, field_options FROM {$this->fields} WHERE type not in ('hidden', 'user_id', 'break', 'divider', 'html', 'captcha', 'form')"); $default_html = <<<DEFAULT_HTML <div id="frm_field_[id]_container" class="form-field [required_class] [error_class]"> <label class="frm_pos_[label_position]">[field_name] <span class="frm_required">[required_label]</span> </label> [input] [if description]<div class="frm_description">[description]</div>[/if description] </div> DEFAULT_HTML; $old_default_html = <<<DEFAULT_HTML <div id="frm_field_[id]_container" class="form-field [required_class] [error_class]"> <label class="frm_pos_[label_position]">[field_name] <span class="frm_required">[required_label]</span> </label> [input] [if description]<p class="frm_description">[description]</p>[/if description] </div> DEFAULT_HTML; $new_default_html = FrmFieldsHelper::get_default_html('text'); foreach ($fields as $field) { $field->field_options = maybe_unserialize($field->field_options); if (!isset($field->field_options['custom_html']) or empty($field->field_options['custom_html']) or stripslashes($field->field_options['custom_html']) == $default_html or stripslashes($field->field_options['custom_html']) == $old_default_html) { $field->field_options['custom_html'] = $new_default_html; $wpdb->update($this->fields, array('field_options' => maybe_serialize($field->field_options)), array('id' => $field->id)); } unset($field); } unset($default_html); } /**** ADD/UPDATE DEFAULT TEMPLATES ****/ FrmFormsController::add_default_templates(FRM_TEMPLATES_PATH); /***** SAVE DB VERSION *****/ update_option('frm_db_version', $frm_db_version); } do_action('frm_after_install'); }
private function migrate_to_4() { global $wpdb; $user_ids = FrmEntryMeta::getAll(array('fi.type' => 'user_id')); foreach ($user_ids as $user_id) { $wpdb->update($this->entries, array('user_id' => $user_id->meta_value), array('id' => $user_id->item_id)); } }
public static function get_linked_options($values, $field, $entry_id = false) { global $user_ID, $wpdb; $metas = array(); $selected_field = FrmField::getOne($values['form_select']); if (!$selected_field) { return array(); } $linked_posts = isset($selected_field->field_options['post_field']) && $selected_field->field_options['post_field'] && $selected_field->field_options['post_field'] != ''; $post_ids = array(); if (is_numeric($values['hide_field']) && empty($values['hide_opt'])) { if (isset($_POST) && isset($_POST['item_meta'])) { $observed_field_val = isset($_POST['item_meta'][$values['hide_field']]) ? $_POST['item_meta'][$values['hide_field']] : ''; } else { if ($entry_id) { $observed_field_val = FrmEntryMeta::get_entry_meta_by_field($entry_id, $values['hide_field']); } else { $observed_field_val = ''; } } $observed_field_val = maybe_unserialize($observed_field_val); $metas = array(); FrmProEntryMetaHelper::meta_through_join($values['hide_field'], $selected_field, $observed_field_val, false, $metas); } else { if ($values['restrict'] && $user_ID) { $entry_user = $user_ID; if ($entry_id && FrmAppHelper::is_admin()) { $entry_user = FrmDb::get_var('frm_items', array('id' => $entry_id), 'user_id'); if (!$entry_user || empty($entry_user)) { $entry_user = $user_ID; } } if (isset($selected_field->form_id)) { $linked_where = array('form_id' => $selected_field->form_id, 'user_id' => $entry_user); if ($linked_posts) { $post_ids = FrmDb::get_results('frm_items', $linked_where, 'id, post_id'); } else { $entry_ids = FrmDb::get_col($wpdb->prefix . 'frm_items', $linked_where, 'id'); } unset($linked_where); } if (isset($entry_ids) && !empty($entry_ids)) { $metas = FrmEntryMeta::getAll(array('it.item_id' => $entry_ids, 'field_id' => (int) $values['form_select']), ' ORDER BY meta_value', ''); } } else { $limit = ''; if (FrmAppHelper::is_admin_page('formidable')) { $limit = 500; } $metas = FrmDb::get_results('frm_item_metas', array('field_id' => $values['form_select']), 'item_id, meta_value', array('order_by' => 'meta_value', 'limit' => $limit)); $post_ids = FrmDb::get_results('frm_items', array('form_id' => $selected_field->form_id), 'id, post_id', array('limit' => $limit)); } } if ($linked_posts && !empty($post_ids)) { foreach ($post_ids as $entry) { $meta_value = FrmProEntryMetaHelper::get_post_value($entry->post_id, $selected_field->field_options['post_field'], $selected_field->field_options['custom_field'], array('type' => $selected_field->type, 'form_id' => $selected_field->form_id, 'field' => $selected_field)); $metas[] = array('meta_value' => $meta_value, 'item_id' => $entry->id); } } $options = array(); foreach ($metas as $meta) { $meta = (array) $meta; if ($meta['meta_value'] == '') { continue; } if ($selected_field->type == 'image') { $options[$meta['item_id']] = $meta['meta_value']; } else { $options[$meta['item_id']] = FrmEntriesHelper::display_value($meta['meta_value'], $selected_field, array('type' => $selected_field->type, 'show_icon' => true, 'show_filename' => false)); } unset($meta); } $options = apply_filters('frm_data_sort', $options, array('metas' => $metas, 'field' => $selected_field)); unset($metas); if (self::include_blank_option($options, $field)) { $options = array('' => '') + (array) $options; } return stripslashes_deep($options); }
public static function trigger_email($action, $entry, $form) { if (defined('WP_IMPORTING') && WP_IMPORTING) { return; } global $wpdb; $notification = $action->post_content; $email_key = $action->ID; // Set the subject if (empty($notification['email_subject'])) { $notification['email_subject'] = sprintf(__('%1$s Form submitted on %2$s', 'formidable'), $form->name, '[sitename]'); } $plain_text = $notification['plain_text'] ? true : false; //Filter these fields $filter_fields = array('email_to', 'cc', 'bcc', 'reply_to', 'from', 'email_subject', 'email_message'); add_filter('frm_plain_text_email', $plain_text ? '__return_true' : '__return_false'); //Get all values in entry in order to get User ID field ID $values = FrmEntryMeta::getAll(array('it.field_id !' => 0, 'it.item_id' => $entry->id), ' ORDER BY fi.field_order'); $user_id_field = $user_id_key = ''; foreach ($values as $value) { if ($value->field_type == 'user_id') { $user_id_field = $value->field_id; $user_id_key = $value->field_key; break; } unset($value); } //Filter and prepare the email fields foreach ($filter_fields as $f) { //Don't allow empty From if ($f == 'from' && empty($notification[$f])) { $notification[$f] = '[admin_email]'; } else { if (in_array($f, array('email_to', 'cc', 'bcc', 'reply_to', 'from'))) { //Remove brackets //Add a space in case there isn't one $notification[$f] = str_replace('<', ' ', $notification[$f]); $notification[$f] = str_replace(array('"', '>'), '', $notification[$f]); //Switch userID shortcode to email address if (strpos($notification[$f], '[' . $user_id_field . ']') !== false || strpos($notification[$f], '[' . $user_id_key . ']') !== false) { $user_data = get_userdata($entry->metas[$user_id_field]); $user_email = $user_data->user_email; $notification[$f] = str_replace(array('[' . $user_id_field . ']', '[' . $user_id_key . ']'), $user_email, $notification[$f]); } } } $notification[$f] = FrmFieldsHelper::basic_replace_shortcodes($notification[$f], $form, $entry); } //Put recipients, cc, and bcc into an array if they aren't empty $to_emails = self::explode_emails($notification['email_to']); $cc = self::explode_emails($notification['cc']); $bcc = self::explode_emails($notification['bcc']); $to_emails = apply_filters('frm_to_email', $to_emails, $values, $form->id, compact('email_key', 'entry', 'form')); // Stop now if there aren't any recipients if (empty($to_emails) && empty($cc) && empty($bcc)) { return; } $to_emails = array_unique((array) $to_emails); $prev_mail_body = $mail_body = $notification['email_message']; $mail_body = FrmEntriesHelper::replace_default_message($mail_body, array('id' => $entry->id, 'entry' => $entry, 'plain_text' => $plain_text, 'user_info' => isset($notification['inc_user_info']) ? $notification['inc_user_info'] : false)); // Add the user info if it isn't already included if ($notification['inc_user_info'] && $prev_mail_body == $mail_body) { $data = maybe_unserialize($entry->description); $mail_body .= "\r\n\r\n" . __('User Information', 'formidable') . "\r\n"; $mail_body .= __('IP Address', 'formidable') . ': ' . $entry->ip . "\r\n"; $mail_body .= __('User-Agent (Browser/OS)', 'formidable') . ': ' . FrmEntryFormat::get_browser($data['browser']) . "\r\n"; $mail_body .= __('Referrer', 'formidable') . ': ' . $data['referrer'] . "\r\n"; } unset($prev_mail_body); // Add attachments $attachments = apply_filters('frm_notification_attachment', array(), $form, compact('entry', 'email_key')); if (!empty($notification['email_subject'])) { $notification['email_subject'] = apply_filters('frm_email_subject', $notification['email_subject'], compact('form', 'entry', 'email_key')); } // check for a phone number foreach ((array) $to_emails as $email_key => $e) { if ($e != '[admin_email]' && !is_email($e)) { $e = explode(' ', $e); //If to_email has name <*****@*****.**> format if (is_email(end($e))) { continue; } do_action('frm_send_to_not_email', array('e' => $e, 'subject' => $notification['email_subject'], 'mail_body' => $mail_body, 'reply_to' => $notification['reply_to'], 'from' => $notification['from'], 'plain_text' => $plain_text, 'attachments' => $attachments, 'form' => $form, 'email_key' => $email_key)); unset($to_emails[$email_key]); } } // Send the email now $sent_to = self::send_email(array('to_email' => $to_emails, 'subject' => $notification['email_subject'], 'message' => $mail_body, 'from' => $notification['from'], 'plain_text' => $plain_text, 'reply_to' => $notification['reply_to'], 'attachments' => $attachments, 'cc' => $cc, 'bcc' => $bcc)); return $sent_to; }
/** * Automatically load the form for editing when a draft exists * or the form is limited to one per user */ private static function front_auto_edit_entry($form, $fields, $title, $description, &$continue) { global $frm_vars, $wpdb; $user_ID = get_current_user_id(); if (is_numeric($frm_vars['editing_entry'])) { //get entry from shortcode $entry_id = $frm_vars['editing_entry']; } else { // get all entry ids for this user $entry_ids = FrmDb::get_col('frm_items', array('user_id' => $user_ID, 'form_id' => $form->id)); if (empty($entry_ids)) { return; } //$where_options = $frm_vars['editing_entry']; // Is is possible the entry_id parameter in the shortcode is sql? $get_meta = FrmEntryMeta::getAll(array('it.item_id' => $entry_ids), ' ORDER BY it.created_at DESC', ' LIMIT 1'); $entry_id = $get_meta ? $get_meta->item_id : false; } if (!$entry_id) { return; } if (!FrmProEntriesHelper::user_can_edit($entry_id, $form)) { return; } $frm_vars['editing_entry'] = $entry_id; self::show_responses($entry_id, $fields, $form, $title, $description); $continue = false; }
function show_frm_date($entry) { $metas = FrmEntryMeta::getAll("item_id={$entry->id} and field_id=0", ' ORDER BY it.created_at DESC'); $initials = ''; foreach ($metas as $meta) { if (!empty($initials)) { continue; } $value = maybe_unserialize($meta->meta_value); if (!isset($value['initials'])) { continue; } $initials = $value['initials']; $date = $meta->created_at; unset($meta); unset($value); } if (!empty($initials)) { TouAppHelper::show_date($date, $initials); } }
if (!isset($new_field) || !$new_field) { ?> <input type="text" name="<?php echo isset($current_field_id) ? 'field_options[hide_opt_' . $current_field_id . ']' : $field_name; ?> " value="" /> <?php return; } if (!isset($is_settings_page)) { $is_settings_page = FrmAppHelper::simple_get('frm_action') == 'settings'; $anything = $is_settings_page ? '' : __('Anything', 'formidable'); } if ($new_field->type == 'data') { if (isset($new_field->field_options['form_select']) && is_numeric($new_field->field_options['form_select'])) { $new_entries = FrmEntryMeta::getAll(array('it.field_id' => (int) $new_field->field_options['form_select']), '', ' LIMIT 300', true); } $new_field->options = array(); if (isset($new_entries) && !empty($new_entries)) { foreach ($new_entries as $ent) { $new_field->options[$ent->item_id] = $ent->meta_value; } } } else { if (isset($new_field->field_options['post_field']) && $new_field->field_options['post_field'] == 'post_status') { $new_field->options = FrmProFieldsHelper::get_status_options($new_field); } } if (isset($new_field->field_options['post_field']) && $new_field->field_options['post_field'] == 'post_category') { if (!isset($field_name)) { $field_name = 'field_options[hide_opt_' . $current_field_id . ']';
public static function meta_through_join($hide_field, $selected_field, $observed_field_val, $this_field = false, &$metas) { if (is_array($observed_field_val)) { $observed_field_val = array_filter($observed_field_val); } if (empty($observed_field_val) || !is_numeric($observed_field_val) && !is_array($observed_field_val)) { return; } $observed_info = FrmField::getOne($hide_field); if (!$selected_field || !$observed_info) { return; } $form_id = FrmProFieldsHelper::get_parent_form_id($selected_field); $join_fields = FrmField::get_all_types_in_form($form_id, 'data'); if (empty($join_fields)) { return; } foreach ($join_fields as $jf) { if (isset($jf->field_options['form_select']) && isset($observed_info->field_options['form_select']) && $jf->field_options['form_select'] == $observed_info->field_options['form_select']) { $join_field = $jf->id; } } if (!isset($join_field)) { return; } $observed_field_val = array_filter((array) $observed_field_val); $query = array('field_id' => (int) $join_field); $sub_query = array('it.meta_value' => $observed_field_val); foreach ($observed_field_val as $obs_val) { $sub_query['or'] = 1; $sub_query['it.meta_value LIKE'] = ':"' . $obs_val . '"'; } $query[] = $sub_query; $user_id = ''; if ($this_field && isset($this_field->field_options['restrict']) && $this_field->field_options['restrict']) { $query['e.user_id'] = get_current_user_id(); } // the ids of all the entries that have been selected in the linked form $entry_ids = FrmEntryMeta::getEntryIds($query); if (!empty($entry_ids)) { if ($form_id != $selected_field->form_id) { // this is a child field so we need to get the child entries global $wpdb; $entry_ids = FrmDb::get_col($wpdb->prefix . 'frm_items', array('parent_item_id' => $entry_ids)); } if (!empty($entry_ids)) { $metas = FrmEntryMeta::getAll(array('item_id' => $entry_ids, 'field_id' => $selected_field->id), ' ORDER BY meta_value'); } } }
private static function add_comments_to_csv(&$row) { if (!self::$comment_count) { // don't continue if we already know there are no comments return; } $comments = FrmEntryMeta::getAll(array('item_id' => (int) self::$entry->id, 'field_id' => 0), ' ORDER BY it.created_at ASC'); $i = 0; if ($comments) { foreach ($comments as $comment) { $c = maybe_unserialize($comment->meta_value); if (!isset($c['comment'])) { continue; } $row['comment' . $i] = $c['comment']; unset($co); $row['comment_user_id' . $i] = FrmProFieldsHelper::get_display_name($c['user_id'], 'user_login'); unset($c); $row['comment_created_at' . $i] = FrmAppHelper::get_formatted_time($comment->created_at, self::$wp_date_format, ' '); unset($v, $comment); $i++; } } for ($i; $i <= self::$comment_count; $i++) { $row['comment' . $i] = ''; $row['comment_user_id' . $i] = ''; $row['comment_created_at' . $i] = ''; } }