function upgrade()
 {
     global $wpdb;
     $db_version = 14;
     // this is the version of the database we're moving to
     $old_db_version = get_option('frmpro_db_version');
     if ($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 Display Table */
         $sql = "CREATE TABLE {$this->displays} (\n                id int(11) NOT NULL auto_increment,\n                display_key varchar(255) default NULL,\n                name varchar(255) default NULL,\n                description text default NULL,\n                content longtext default NULL,\n                dyncontent longtext default NULL,\n                insert_loc varchar(255) default NULL,\n                param varchar(255) default NULL,\n                type varchar(255) default NULL,\n                show_count varchar(255) default NULL,\n                options longtext default NULL,\n                form_id int(11) default NULL,\n                entry_id int(11) default NULL,\n                post_id int(11) default NULL,\n                created_at datetime NOT NULL,\n                PRIMARY KEY id (id),\n                KEY form_id (form_id),\n                KEY entry_id (entry_id),\n                KEY post_id (post_id),\n                UNIQUE KEY display_key (display_key)\n          ) {$charset_collate};";
         dbDelta($sql);
         if ($db_version >= 3 and $old_db_version < 3) {
             //migrate hidden field data into the parent field
             $wpdb->update($frmdb->fields, array('type' => 'scale'), array('type' => '10radio'));
             $fields = FrmField::getAll();
             foreach ($fields as $field) {
                 $field->field_options = maybe_unserialize($field->field_options);
                 if (isset($field->field_options['hide_field']) and is_numeric($field->field_options['hide_field']) and (isset($field->field_options['hide_opt']) and !empty($field->field_options['hide_opt']) or isset($field->field_options['form_select']) and !empty($field->field_options['form_select']))) {
                     global $frm_field;
                     //save hidden fields to parent field
                     $parent_field = FrmField::getOne($field->field_options['hide_field']);
                     if ($parent_field) {
                         $parent_options = maybe_unserialize($parent_field->field_options);
                         if (!isset($parent_options['dependent_fields'])) {
                             $parent_options['dependent_fields'] = array();
                         } else {
                             foreach ($parent_options['dependent_fields'] as $child_id => $child_opt) {
                                 if (empty($child_opt) or $child_opt == '') {
                                     unset($parent_options['dependent_fields'][$child_id]);
                                 } else {
                                     if ($child_id != $field->id) {
                                         //check to make sure this field is still dependent
                                         $check_field = FrmField::getOne($child_id);
                                         $check_options = maybe_unserialize($check_field->field_options);
                                         if (!is_numeric($check_options['hide_field']) or $check_options['hide_field'] != $parent_field->id or empty($check_options['hide_opt']) and empty($check_options['form_select'])) {
                                             unset($parent_options['dependent_fields'][$child_id]);
                                         }
                                     }
                                 }
                             }
                         }
                         $dep_fields = array();
                         if ($field->type == 'data' and isset($field->field_options['form_select']) and is_numeric($field_options['form_select'])) {
                             $dep_fields[] = $field->field_options['form_select'];
                             $dep_fields[] = $field->field_options['data_type'];
                         } else {
                             if (isset($field->field_options['hide_opt']) and !empty($field->field_options['hide_opt'])) {
                                 $dep_fields[] = $field->field_options['hide_opt'];
                             }
                         }
                         if (!empty($dep_fields)) {
                             $parent_options['dependent_fields'][$field->id] = $dep_fields;
                         }
                         $frm_field->update($parent_field->id, array('field_options' => $parent_options));
                     }
                 }
             }
         }
         /**** ADD DEFAULT TEMPLATES ****/
         FrmFormsController::add_default_templates(FRMPRO_TEMPLATES_PATH);
         update_option('frmpro_db_version', $db_version);
         global $frmpro_settings;
         $frmpro_settings->store();
         //update the styling settings
     }
 }
Example #2
0
    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');
    }
 function import_templates()
 {
     if (current_user_can('frm_edit_forms')) {
         global $frmpro_settings;
         FrmFormsController::add_default_templates($frmpro_settings->template_path, false);
     } else {
         global $frm_settings;
         wp_die($frm_settings->admin_permission);
     }
 }