Plugin Directory


Ignore:
Timestamp:
11/08/2026 12:50:59 AM (21 hours ago)
Author:
codersly
Message:

Tracking Updated

File:
1 edited

Legend:

Unmodified
Added
Removed
  • codersly-visitor-tracker/trunk/includes/Install.php

    r3703338 r3790318  
    1717final class Install {
    1818        /** Internal schema marker. */
    19         private const DB_VERSION = '1.1.1-schema3';
     19        private const DB_VERSION = '1.1.3-schema6';
    2020        private const OPT_DB_VERSION = 'vtrt_db_version';
    2121
    … …  
    2626        }
    2727
    28         public static function deactivate() : void {}
     28        public static function deactivate() : void {
     29                wp_clear_scheduled_hook( 'vtrt_async_geo_lookup' );
     30        }
    2931
    3032        public static function maybe_upgrade() : void {
    … …  
    3234                        self::activate();
    3335                }
     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 );
    3450        }
    3551
    … …  
    5167                        region VARCHAR(120) NULL,
    5268                        city VARCHAR(120) NULL,
     69                        geo_retry_at DATETIME NULL,
     70                        geo_source VARCHAR(40) NULL,
    5371                        timezone VARCHAR(80) NULL,
    5472                        path VARCHAR(2048) NOT NULL DEFAULT '',
    … …  
    7795                        KEY ts (ts),
    7896                        KEY ip (ip),
     97                        KEY ip_geo_source (ip,geo_source),
    7998                        KEY user_id (user_id),
    8099                        KEY is_bot (is_bot),
    … …  
    87106                ) {$charset};";
    88107                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};" );
    89123        }
    90124
    … …  
    119153        private static function run_migrations() : void {
    120154                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                }
    128218        }
    129219
Note: See TracChangeset for help on using the changeset viewer.