/**
  * Updates the DB schema as needed
  */
 public static function update_tables_and_options()
 {
     $my_wpdb = apply_filters('slimstat_custom_wpdb', $GLOBALS['wpdb']);
     // --- Updates for version 3.8.4 ---
     if (version_compare(wp_slimstat::$options['version'], '3.8.4', '<')) {
         $my_wpdb->query("CREATE TABLE {$GLOBALS['wpdb']->prefix}slim_stats_archive LIKE {$GLOBALS['wpdb']->prefix}slim_stats");
     }
     // --- END: Updates for version 3.8.4 ---
     // --- Updates for version 3.9.6 ---
     if (version_compare(wp_slimstat::$options['version'], '3.9.6', '<')) {
         // Consolidate some settings
         $classes = wp_slimstat::string_to_array(wp_slimstat::$options['ignore_outbound_classes']);
         $rel = wp_slimstat::string_to_array(wp_slimstat::$options['ignore_outbound_rel']);
         $href = wp_slimstat::string_to_array(wp_slimstat::$options['ignore_outbound_href']);
         wp_slimstat::$options['ignore_outbound_classes_rel_href'] = implode(',', array_merge($classes, $rel, $href));
         $classes = wp_slimstat::string_to_array(wp_slimstat::$options['do_not_track_outbound_classes']);
         $rel = wp_slimstat::string_to_array(wp_slimstat::$options['do_not_track_outbound_rel']);
         $href = wp_slimstat::string_to_array(wp_slimstat::$options['do_not_track_outbound_href']);
         wp_slimstat::$options['do_not_track_outbound_classes_rel_href'] = implode(',', array_merge($classes, $rel, $href));
         // More secure secret key
         wp_slimstat::$options['secret'] = wp_hash(uniqid(time(), true));
     }
     // --- END: Updates for version 3.9.6 ---
     // --- Updates for version 3.9.8.2 ---
     if (version_compare(wp_slimstat::$options['version'], '3.9.8.2', '<')) {
         // The GeoLite DB is already installed, let's unzip it to improve the tracker's performance
         if (file_exists(wp_slimstat::$maxmind_path . '.gz')) {
             @unlink(wp_slimstat::$maxmind_path . '.gz');
             wp_slimstat::download_maxmind_database();
         }
     }
     // --- END: Updates for version 3.9.8.2 ---
     // --- Updates for version 4.0 ---
     if (version_compare(wp_slimstat::$options['version'], '4.0', '<')) {
         $GLOBALS['wpdb']->query("DELETE FROM {$GLOBALS['wpdb']->prefix}usermeta WHERE meta_key LIKE 'meta-box-order_slimstat%'");
         $have_innodb = $GLOBALS['wpdb']->get_results("SHOW VARIABLES LIKE 'have_innodb'", ARRAY_A);
         $use_innodb = !empty($have_innodb[0]) && $have_innodb[0]['Value'] == 'YES' ? 'ENGINE=InnoDB' : '';
         // Create the new table
         self::_create_table("\n\t\t\t\tCREATE TABLE IF NOT EXISTS {$GLOBALS['wpdb']->prefix}slim_stats_4 (\n\t\t\t\t\tid INT UNSIGNED NOT NULL auto_increment,\n\t\t\t\t\tip INT UNSIGNED DEFAULT 0,\n\t\t\t\t\tother_ip INT UNSIGNED DEFAULT 0,\n\t\t\t\t\tusername VARCHAR(255) DEFAULT NULL,\n\t\t\t\t\tcountry VARCHAR(16) DEFAULT NULL,\n\t\t\t\t\treferer VARCHAR(2048) DEFAULT NULL,\n\t\t\t\t\tresource VARCHAR(2048) DEFAULT NULL,\n\t\t\t\t\tsearchterms VARCHAR(2048) DEFAULT NULL,\n\t\t\t\t\tplugins VARCHAR(255) DEFAULT NULL,\n\t\t\t\t\tnotes VARCHAR(2048) DEFAULT NULL,\n\t\t\t\t\tvisit_id INT UNSIGNED NOT NULL DEFAULT 0,\n\t\t\t\t\tserver_latency INT(10) UNSIGNED DEFAULT 0,\n\t\t\t\t\tpage_performance INT(10) UNSIGNED DEFAULT 0,\n\n\t\t\t\t\tbrowser VARCHAR(40) DEFAULT NULL,\n\t\t\t\t\tbrowser_version VARCHAR(15) DEFAULT NULL,\n\t\t\t\t\tbrowser_type TINYINT UNSIGNED DEFAULT 0,\n\t\t\t\t\tplatform VARCHAR(15) DEFAULT NULL,\n\t\t\t\t\tlanguage VARCHAR(5) DEFAULT NULL,\n\t\t\t\t\tuser_agent VARCHAR(2048) DEFAULT NULL,\n\n\t\t\t\t\tresolution VARCHAR(12) DEFAULT NULL,\n\t\t\t\t\tscreen_width SMALLINT UNSIGNED DEFAULT 0,\n\t\t\t\t\tscreen_height SMALLINT UNSIGNED DEFAULT 0,\n\n\t\t\t\t\tcontent_type VARCHAR(64) DEFAULT NULL,\n\t\t\t\t\tcategory VARCHAR(256) DEFAULT NULL,\n\t\t\t\t\tauthor VARCHAR(64) DEFAULT NULL,\n\t\t\t\t\tcontent_id BIGINT(20) UNSIGNED DEFAULT 0,\n\t\t\t\t\t\n\t\t\t\t\toutbound_resource VARCHAR(2048) DEFAULT NULL,\n\n\t\t\t\t\tdt INT(10) UNSIGNED DEFAULT 0,\n\t\t\t\t\tCONSTRAINT PRIMARY KEY (id),\n\t\t\t\t\tINDEX idx_{$GLOBALS['wpdb']->prefix}slim_stats_dt (dt)\n\t\t\t\t) COLLATE utf8_general_ci {$use_innodb}", $GLOBALS['wpdb']->prefix . 'slim_stats_4', $my_wpdb);
         // Create the archive table
         $my_wpdb->query("CREATE TABLE IF NOT EXISTS {$GLOBALS['wpdb']->prefix}slim_stats_archive_4 LIKE {$GLOBALS['wpdb']->prefix}slim_stats_4");
         // Rename old and new tables
         $my_wpdb->query("RENAME TABLE {$GLOBALS['wpdb']->prefix}slim_stats TO {$GLOBALS['wpdb']->prefix}slim_stats_3");
         $my_wpdb->query("RENAME TABLE {$GLOBALS['wpdb']->prefix}slim_stats_4 TO {$GLOBALS['wpdb']->prefix}slim_stats");
         $my_wpdb->query("RENAME TABLE {$GLOBALS['wpdb']->prefix}slim_stats_archive TO {$GLOBALS['wpdb']->prefix}slim_stats_archive_3");
         $my_wpdb->query("RENAME TABLE {$GLOBALS['wpdb']->prefix}slim_stats_archive_4 TO {$GLOBALS['wpdb']->prefix}slim_stats_archive");
         // Create the new events table
         $my_wpdb->query("\n\t\t\t\tCREATE TABLE IF NOT EXISTS {$GLOBALS['wpdb']->prefix}slim_events (\n\t\t\t\t\tevent_id INT(10) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\ttype TINYINT UNSIGNED DEFAULT 0,\n\t\t\t\t\tevent_description VARCHAR(64) DEFAULT NULL,\n\t\t\t\t\tnotes VARCHAR(256) DEFAULT NULL,\n\t\t\t\t\tposition VARCHAR(32) DEFAULT NULL,\n\t\t\t\t\tid INT UNSIGNED NOT NULL DEFAULT 0,\n\t\t\t\t\tdt INT(10) UNSIGNED DEFAULT 0,\n\t\t\t\t\t\n\t\t\t\t\tCONSTRAINT PRIMARY KEY (event_id),\n\t\t\t\t\tINDEX idx_{$GLOBALS['wpdb']->prefix}slim_events (dt),\n\t\t\t\t\tCONSTRAINT fk_{$GLOBALS['wpdb']->prefix}id FOREIGN KEY (id) REFERENCES {$GLOBALS['wpdb']->prefix}slim_stats(id) ON UPDATE CASCADE ON DELETE CASCADE\n\t\t\t\t) COLLATE utf8_general_ci {$use_innodb}");
         // Copy the data if less than 750k records
         $count_records = $my_wpdb->get_var("SELECT COUNT(*) FROM {$GLOBALS['wpdb']->prefix}slim_stats");
         if ($count_records <= 750000) {
             $my_wpdb->query("\n\t\t\t\t\tINSERT INTO {$GLOBALS['wpdb']->prefix}slim_stats (\n\t\t\t\t\t\tid,\n\t\t\t\t\t\tip,\n\t\t\t\t\t\tother_ip,\n\t\t\t\t\t\tusername,\n\t\t\t\t\t\tcountry,\n\t\t\t\t\t\treferer,\n\t\t\t\t\t\tresource,\n\t\t\t\t\t\tsearchterms,\n\t\t\t\t\t\tplugins,\n\t\t\t\t\t\tnotes,\n\t\t\t\t\t\tvisit_id,\n\t\t\t\t\t\tserver_latency,\n\t\t\t\t\t\tpage_performance,\n\n\t\t\t\t\t\tbrowser,\n\t\t\t\t\t\tbrowser_version,\n\t\t\t\t\t\tbrowser_type,\n\t\t\t\t\t\tplatform,\n\t\t\t\t\t\tlanguage,\n\t\t\t\t\t\tuser_agent,\n\n\t\t\t\t\t\tscreen_width,\n\t\t\t\t\t\tscreen_height,\n\n\t\t\t\t\t\tcontent_type,\n\t\t\t\t\t\tcategory,\n\t\t\t\t\t\tauthor,\n\t\t\t\t\t\tcontent_id,\n\n\t\t\t\t\t\toutbound_resource,\n\n\t\t\t\t\t\tdt\n\t\t\t\t\t)\n\t\t\t\t\tSELECT \n\t\t\t\t\t\tt1.id,\n\t\t\t\t\t\tt1.ip,\n\t\t\t\t\t\tt1.other_ip,\n\t\t\t\t\t\tNULLIF(t1.user, ''),\n\t\t\t\t\t\tNULLIF(t1.country, ''),\n\t\t\t\t\t\tNULLIF(t1.referer, ''),\n\t\t\t\t\t\tNULLIF(t1.resource, ''),\n\t\t\t\t\t\tNULLIF(t1.searchterms, ''),\n\t\t\t\t\t\tNULLIF(t1.plugins, ''),\n\t\t\t\t\t\tNULLIF(t1.notes, ''),\n\t\t\t\t\t\tt1.visit_id,\n\t\t\t\t\t\tt1.server_latency,\n\t\t\t\t\t\tt1.page_performance,\n\n\t\t\t\t\t\tNULLIF(tb.browser, ''),\n\t\t\t\t\t\tNULLIF(tb.version, ''),\n\t\t\t\t\t\ttb.type,\n\t\t\t\t\t\tNULLIF(tb.platform, ''),\n\t\t\t\t\t\tNULLIF(t1.language, ''),\n\t\t\t\t\t\tNULLIF(tb.user_agent, ''),\n\n\t\t\t\t\t\t9812,\n\t\t\t\t\t\t9812,\n\n\t\t\t\t\t\tNULLIF(tci.content_type, ''),\n\t\t\t\t\t\tNULLIF(tci.category, ''),\n\t\t\t\t\t\tNULLIF(tci.author, ''),\n\t\t\t\t\t\ttci.content_id,\n\n\t\t\t\t\t\tNULL,\n\n\t\t\t\t\t\tt1.dt\n\n\t\t\t\t\tFROM {$GLOBALS['wpdb']->prefix}slim_stats_3 AS t1\n\t\t\t\t\tINNER JOIN {$GLOBALS['wpdb']->base_prefix}slim_browsers AS tb ON t1.browser_id = tb.browser_id\n\t\t\t\t\tINNER JOIN {$GLOBALS['wpdb']->base_prefix}slim_content_info AS tci ON t1.content_info_id = tci.content_info_id");
             // Copy the events
             $my_wpdb->query("\n\t\t\t\t\tINSERT INTO {$GLOBALS['wpdb']->prefix}slim_events (\n\t\t\t\t\t\ttype,\n\t\t\t\t\t\tevent_description,\n\t\t\t\t\t\tnotes,\n\t\t\t\t\t\tposition,\n\t\t\t\t\t\tid,\n\t\t\t\t\t\tdt\n\t\t\t\t\t)\n\t\t\t\t\tSELECT\n\t\t\t\t\t\ttob.type,\n\t\t\t\t\t\tSUBSTRING(tob.notes, LOCATE('Event:', tob.notes)+6, LOCATE(',', tob.notes, LOCATE('Event:', tob.notes)+6) - LOCATE('Event:', tob.notes)-6),\n\t\t\t\t\t\tSUBSTRING(tob.notes, 1, LOCATE('Event:', tob.notes) - 3),\n\t\t\t\t\t\ttob.position,\n\t\t\t\t\t\ttob.id,\n\t\t\t\t\t\ttob.dt\n\t\t\t\t\tFROM {$GLOBALS['wpdb']->prefix}slim_outbound AS tob");
         }
     }
     // --- END: Updates for version 4.0 ---
     // Now we can update the version stored in the database
     wp_slimstat::$options['version'] = wp_slimstat::$version;
     return true;
 }
     wp_slimstat::$wpdb->query("ALTER TABLE {$GLOBALS['wpdb']->prefix}slim_stats DROP INDEX {$GLOBALS['wpdb']->prefix}stats_searchterms_idx");
     wp_slimstat_admin::show_alert_message(__('Indexing has been disabled. Enjoy the extra database space!', 'wp-slimstat'), 'wp-ui-highlight below-h2');
     break;
 case 'delete-records':
     $rows_affected = 0;
     if (key_exists($_REQUEST['f'], wp_slimstat_db::$columns_names)) {
         $rows_affected = wp_slimstat::$wpdb->query("\n\t\t\t\t\tDELETE t1.* \n\t\t\t\t\tFROM {$GLOBALS['wpdb']->prefix}slim_stats t1\n\t\t\t\t\tWHERE " . wp_slimstat_db::get_combined_where('', '*', false));
     }
     wp_slimstat_admin::show_alert_message(intval($rows_affected) . ' ' . __('records deleted from your database.', 'wp-slimstat'), 'wp-ui-highlight below-h2');
     break;
 case 'delete-maxmind':
     @unlink(wp_slimstat::$maxmind_path);
     wp_slimstat_admin::show_alert_message(__('The geolocation database has been uninstalled from your server.', 'wp-slimstat'), 'wp-ui-highlight below-h2');
     break;
 case 'download-maxmind':
     $error = wp_slimstat::download_maxmind_database();
     if (!empty($error)) {
         wp_slimstat_admin::show_alert_message($error, 'wp-ui-notification below-h2');
     } else {
         wp_slimstat_admin::show_alert_message(__('The geolocation database has been installed on your server.', 'wp-slimstat'), 'wp-ui-highlight below-h2');
     }
     break;
 case 'import-settings':
     $new_options = @unserialize(stripslashes($_POST['import-slimstat-settings']));
     $new_options = array_intersect_key($new_options, wp_slimstat::$options);
     if (!empty($new_options)) {
         foreach ($new_options as $a_option_name => $a_option_value) {
             wp_slimstat::$options[$a_option_name] = $a_option_value;
         }
     }
     break;