Changeset 3790318 for codersly-visitor-tracker/trunk/includes/Install.php
- Timestamp:
- 11/08/2026 12:50:59 AM (21 hours ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
codersly-visitor-tracker/trunk/includes/Install.php
r3703338 r3790318 17 17 final class Install { 18 18 /** Internal schema marker. */ 19 private const DB_VERSION = '1.1. 1-schema3';19 private const DB_VERSION = '1.1.3-schema6'; 20 20 private const OPT_DB_VERSION = 'vtrt_db_version'; 21 21 … … 26 26 } 27 27 28 public static function deactivate() : void {} 28 public static function deactivate() : void { 29 wp_clear_scheduled_hook( 'vtrt_async_geo_lookup' ); 30 } 29 31 30 32 public static function maybe_upgrade() : void { … … 32 34 self::activate(); 33 35 } 36 } 37 38 public static function geo_cache_table() : string { 39 global $wpdb; 40 return $wpdb->prefix . 'vtrt_geo_cache'; 41 } 42 43 public static function geo_lock_name() : string { 44 global $wpdb; 45 return 'vtrt_geo_' . md5( DB_NAME . '|' . $wpdb->prefix ); 46 } 47 48 public static function geo_cache_ready() : bool { 49 return (bool) get_option( 'vtrt_geo_cache_initialized', false ); 34 50 } 35 51 … … 51 67 region VARCHAR(120) NULL, 52 68 city VARCHAR(120) NULL, 69 geo_retry_at DATETIME NULL, 70 geo_source VARCHAR(40) NULL, 53 71 timezone VARCHAR(80) NULL, 54 72 path VARCHAR(2048) NOT NULL DEFAULT '', … … 77 95 KEY ts (ts), 78 96 KEY ip (ip), 97 KEY ip_geo_source (ip,geo_source), 79 98 KEY user_id (user_id), 80 99 KEY is_bot (is_bot), … … 87 106 ) {$charset};"; 88 107 dbDelta( $sql ); 108 $geo = self::geo_cache_table(); 109 dbDelta( "CREATE TABLE {$geo} ( 110 ip VARCHAR(64) NOT NULL, 111 cache_scope VARCHAR(68) NOT NULL, 112 country VARCHAR(100) NOT NULL DEFAULT '', 113 country_code VARCHAR(2) NOT NULL DEFAULT '', 114 region VARCHAR(120) NULL, 115 city VARCHAR(120) NULL, 116 timezone VARCHAR(80) NULL, 117 geo_source VARCHAR(40) NULL, 118 provider VARCHAR(128) NULL, 119 expires_at BIGINT UNSIGNED NOT NULL DEFAULT 0, 120 PRIMARY KEY (ip,cache_scope), 121 KEY expires_at (expires_at) 122 ) {$charset};" ); 89 123 } 90 124 … … 119 153 private static function run_migrations() : void { 120 154 self::widen_url_columns(); 121 // Enable approximate location once for this requested build. The setting can 122 // still be turned off manually from Visitors Tracker → Settings. 123 if ( null === get_option( 'vtrt_geoip_auto_enabled_110', null ) ) { 124 update_option( 'vtrt_enable_geoip', true, false ); 125 add_option( 'vtrt_geoip_auto_enabled_110', 1, '', false ); 126 } 127 update_option( self::OPT_DB_VERSION, self::DB_VERSION, false ); 155 global $wpdb; 156 $table = $wpdb->prefix . ( defined( 'VTRT_TBL_VISITORS' ) ? \VTRT_TBL_VISITORS : 'vtrt_visitors' ); 157 if ( 'geo_retry_at' === $wpdb->get_var( $wpdb->prepare( 'SHOW COLUMNS FROM %i LIKE %s', $table, $wpdb->esc_like( 'geo_retry_at' ) ) ) 158 && 'geo_source' === $wpdb->get_var( $wpdb->prepare( 'SHOW COLUMNS FROM %i LIKE %s', $table, $wpdb->esc_like( 'geo_source' ) ) ) 159 && self::initialize_geo_cache( $table ) ) { 160 delete_option( 'vtrt_geo_cursor_113' ); 161 update_option( self::OPT_DB_VERSION, self::DB_VERSION, false ); 162 } 163 } 164 165 private static function initialize_geo_cache( string $visitors ) : bool { 166 global $wpdb; 167 $table = self::geo_cache_table(); 168 if ( $table !== $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) ) { 169 return false; 170 } 171 if ( self::geo_cache_ready() ) { 172 return true; 173 } 174 175 $lock = self::geo_lock_name(); 176 if ( 1 !== (int) $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, 10)', $lock ) ) ) { 177 return false; 178 } 179 180 try { 181 // Another request may have completed the migration while we waited. 182 wp_cache_delete( 'notoptions', 'options' ); 183 wp_cache_delete( 'vtrt_geo_cache_initialized', 'options' ); 184 if ( self::geo_cache_ready() ) { 185 return true; 186 } 187 $copied = $wpdb->query( $wpdb->prepare( 188 "INSERT IGNORE INTO %i (ip, cache_scope, country, country_code, region, city, timezone, geo_source, provider) 189 SELECT v.ip, v.geo_source, v.country, v.country_code, v.region, v.city, v.timezone, v.geo_source, v.geo_source 190 FROM %i v 191 INNER JOIN ( 192 SELECT MAX(id) AS id FROM %i 193 WHERE ip <> '' AND geo_source <> '' AND city <> '' AND country <> '' AND country_code <> '' 194 GROUP BY ip, geo_source 195 ) AS latest ON v.id = latest.id", 196 $table, $visitors, $visitors 197 ) ); 198 if ( false === $copied ) { 199 return false; 200 } 201 202 // These legacy result transients are superseded by the separate table. 203 $cleaned = $wpdb->query( $wpdb->prepare( 204 'DELETE FROM %i WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s', 205 $wpdb->options, 206 $wpdb->esc_like( '_transient_vtrt_geo_city_113_' ) . '%', 207 $wpdb->esc_like( '_transient_timeout_vtrt_geo_city_113_' ) . '%', 208 $wpdb->esc_like( '_transient_vtrt_geo_ipgeo_' ) . '%', 209 $wpdb->esc_like( '_transient_timeout_vtrt_geo_ipgeo_' ) . '%' 210 ) ); 211 if ( false === $cleaned ) { 212 return false; 213 } 214 return update_option( 'vtrt_geo_cache_initialized', true, false ); 215 } finally { 216 $wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $lock ) ); 217 } 128 218 } 129 219
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)