/**
 * Upgrade the plugin
 *
 * @return void
 */
function cx_upgrade($license = null)
{
    global $wpdb;
    require_once CX_PATH . '/core/fn.setup.php';
    // We need some functions inside
    // Check API
    cx_api(@$license, true);
    // Get last version
    $last_version = $wpdb->get_var("SELECT option_value FROM {$wpdb->options} WHERE option_name = 'cx_version' LIMIT 1");
    /**
     * Base installation / upgrading
     */
    // Drop old CX tables before 1.1
    $wpdb->query("DROP TABLE IF EXISTS `" . CX_PX . "chat_lines`;");
    $wpdb->query("DROP TABLE IF EXISTS `" . CX_PX . "online`;");
    $wpdb->query("DROP TABLE IF EXISTS `" . CX_PX . "conversations`;");
    $wpdb->query("DROP TABLE IF EXISTS `" . CX_PX . "blocked_ips`;");
    // Chat logs table
    $wpdb->query("CREATE TABLE IF NOT EXISTS `" . CX_PX . "chat_logs` (\n\t\t  `msg_id` varchar(30) NOT NULL DEFAULT '',\n\t\t  `cnv_id` varchar(30) NOT NULL,\n\t\t  `user_id` varchar(30) NOT NULL DEFAULT '',\n\t\t  `name` varchar(32) DEFAULT NULL,\n\t\t  `gravatar` char(32) DEFAULT NULL,\n\t\t  `msg` text NOT NULL,\n\t\t  `time` bigint(13) unsigned NOT NULL,\n\t\t  UNIQUE KEY `msg_id` (`msg_id`)\n\t\t) DEFAULT CHARSET=utf8;");
    // Conversation table
    $wpdb->query("CREATE TABLE IF NOT EXISTS `" . CX_PX . "conversations` (\n\t\t  `cnv_id` varchar(30) NOT NULL DEFAULT '',\n\t\t  `user_id` varchar(30) NOT NULL DEFAULT '',\n\t\t  `created_at` bigint(13) unsigned NOT NULL,\n\t\t  UNIQUE KEY `cnv_id` (`cnv_id`),\n\t\t  KEY `created_at` (`created_at`)\n\t\t) DEFAULT CHARSET=utf8;");
    // Users table
    $wpdb->query("CREATE TABLE IF NOT EXISTS `" . CX_PX . "users` (\n\t\t  `user_id` varchar(30) NOT NULL DEFAULT '',\n\t\t  `type` varchar(12) NOT NULL DEFAULT '',\n\t\t  `name` varchar(32) DEFAULT NULL,\n\t\t  `ip` int(11) unsigned DEFAULT NULL,\n\t\t  `email` varchar(90) DEFAULT NULL,\n\t\t  `last_online` bigint(13) unsigned DEFAULT NULL,\n\t\t  UNIQUE KEY `user_id` (`user_id`)\n\t\t) DEFAULT CHARSET=utf8;");
    // Get options
    $cx_opts = maybe_unserialize($wpdb->get_var("SELECT option_value FROM {$wpdb->options} WHERE option_name = '" . CX_SLUG . "-opts' LIMIT 1"));
    //
    // After CX 1.1
    //
    if (!empty($last_version)) {
        // Before 1.3
        if (version_compare($last_version, '1.3', '<')) {
            // Clean old sessions from DB
            $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_wp_session%'");
        }
        // CX 1.1.2 and later and before 1.3
        if (version_compare($last_version, '1.1.2', '>=') && version_compare($last_version, '1.3', '<')) {
            // Update time fields
            $wpdb->query("ALTER TABLE " . CX_PX . "chat_logs CHANGE `time` `time` BIGINT(13)  UNSIGNED  NOT NULL");
            $wpdb->query("ALTER TABLE " . CX_PX . "conversations CHANGE `created_at` `created_at` BIGINT(13)  UNSIGNED  NOT NULL");
            $wpdb->query("ALTER TABLE " . CX_PX . "users CHANGE `last_online` `last_online` BIGINT(13)  UNSIGNED  NOT NULL");
        }
    }
    // Update current version now
    update_option('cx_version', CX_VERSION);
}
 /**
  * Initialization Screets Chat X for back-end
  *
  * @access public
  * @return void
  */
 function admin_init()
 {
     // Load back-end styles and scripts
     add_action('admin_enqueue_scripts', array(&$this, 'backend_scripts'));
     // Get current page
     $current_page = !empty($_GET['page']) ? $_GET['page'] : null;
     // Check CX setup
     if ($current_page == CX_SLUG or $current_page == 'chat_x') {
         require CX_PATH . '/core/fn.setup.php';
         // Check CX configuration
         cx_check_setup();
     }
     // Settings page
     if ($current_page == CX_SLUG) {
         $l = !empty($this->opts['license_key']) ? $this->opts['license_key'] : null;
         // Check API
         cx_api($l);
     }
 }
 /**
  * Action to save/reset options
  */
 function manage_options()
 {
     // Check this is settings page
     if (!$this->is_settings() or empty($_REQUEST['action'])) {
         return;
     }
     // ACTION: RESET
     if ($_GET['action'] == 'reset') {
         // Remove lc
         delete_option('cx_c9f1a6384b1c466d4612f513bd8e13ea');
         delete_option('cx_error');
         // Prepare variables
         $new_options = array();
         // Prepare data
         foreach ($this->options as $value) {
             @($new_options[$value['id']] = $value['std']);
         }
         // Save new options
         if (update_option($this->option, $new_options)) {
             // Redirect
             wp_redirect($this->admin_url . '&message=1');
             exit;
         } else {
             // Redirect
             wp_redirect($this->admin_url . '&message=2');
             exit;
         }
     } elseif ($_POST['action'] == 'save') {
         // Prepare vars
         $new_options = array();
         // Prepare data
         foreach ($this->options as $value) {
             // Check api
             if ($value['id'] == 'license_key') {
                 cx_api($_POST[$value['id']], true);
             }
             // Update operator default role
             if ($value['id'] == 'op_role') {
                 cx_update_op_role($_POST[$value['id']]);
             }
             // Update operator additional role
             if ($value['id'] == 'op_add_role') {
                 cx_update_op_role($_POST[$value['id']], true);
             }
             if (is_array($_POST[$value['id']])) {
                 $new_options[$value['id']] = $_POST[$value['id']];
             } else {
                 $str = $_POST[$value['id']];
                 // Sanitize option
                 switch ($value['type']) {
                     case 'textarea':
                         if (!empty($value['html'])) {
                             $str = str_replace("\n", '<br/>', $str);
                         }
                         break;
                 }
                 $new_options[$value['id']] = $str;
             }
         }
         // Save new options
         if (update_option($this->option, $new_options)) {
             // Redirect
             wp_redirect($this->admin_url . '&message=3');
             exit;
         } else {
             // Redirect
             wp_redirect($this->admin_url . '&message=4');
             exit;
         }
     }
 }