0, 'addcart_deleted' => 0, 'addcart_held' => 0, 'reco_archived' => 0, 'reco_deleted' => 0, 'reco_held' => 0, 'intent_archived' => 0, 'intent_deleted' => 0, 'intent_held' => 0, ); self::run_for_addcart_events( $gmt_ts, $summary ); self::run_for_reco_events( $gmt_ts, $summary ); self::run_for_intent_events( $gmt_ts, $summary ); aieo_debug_log( 'AIEO_Archive_Handlers: done. summary=' . wp_json_encode( $summary ) ); return $summary; } private static function run_for_addcart_events( $gmt_ts, array &$summary ) { global $wpdb; $hot = $wpdb->prefix . 'aieo_addcart_events'; $archive = $wpdb->prefix . 'aieo_addcart_events_archive'; if ( ! self::table_exists( $hot ) ) { return; } self::ensure_archive_table_clone( $hot, $archive ); $now = strtotime( $gmt_ts . ' UTC' ); $hot_cut = gmdate( 'Y-m-d H:i:s', $now - self::AE_HOT_DAYS * DAY_IN_SECONDS ); if ( ! AIEO_Remote_Export_Manager::can_purge_through( 'aieo_addcart_events', $hot_cut ) ) { aieo_debug_log( 'AIEO_Archive_Handlers: holding addcart_events purge — remote export not caught up to ' . $hot_cut ); $summary['addcart_held'] = 1; } else { list( $moved, $deleted ) = self::archive_then_delete_by_ts( $hot, $archive, 'ts_added', $hot_cut ); $summary['addcart_archived'] = $moved; $summary['addcart_deleted'] = $deleted; } } private static function run_for_reco_events( $gmt_ts, array &$summary ) { global $wpdb; $hot = $wpdb->prefix . 'aieo_reco_events'; $archive = $wpdb->prefix . 'aieo_reco_events_archive'; if ( ! self::table_exists( $hot ) ) { return; } self::ensure_archive_table_clone( $hot, $archive ); $now = strtotime( $gmt_ts . ' UTC' ); $hot_cut = gmdate( 'Y-m-d H:i:s', $now - self::RECO_HOT_DAYS * DAY_IN_SECONDS ); if ( ! AIEO_Remote_Export_Manager::can_purge_through( 'aieo_reco_events', $hot_cut ) ) { aieo_debug_log( 'AIEO_Archive_Handlers: holding reco_events purge — remote export not caught up to ' . $hot_cut ); $summary['reco_held'] = 1; } else { list( $moved, $deleted ) = self::archive_then_delete_by_ts( $hot, $archive, 'ts', $hot_cut ); $summary['reco_archived'] = $moved; $summary['reco_deleted'] = $deleted; if ( self::$copy_failed && class_exists( 'AIEO_Attribution' ) && method_exists( 'AIEO_Attribution', 'reco_schema_complete' ) && ! AIEO_Attribution::reco_schema_complete() && '' === (string) $wpdb->last_error ) { delete_option( AIEO_Attribution::RECO_SCHEMA_OPTION ); $summary['reco_stamp_dropped'] = 1; } } } private static function run_for_intent_events( $gmt_ts, array &$summary ) { global $wpdb; $hot = $wpdb->prefix . 'aieo_intent_events'; $archive = $wpdb->prefix . 'aieo_intent_events_archive'; if ( ! self::table_exists( $hot ) ) { return; } self::ensure_archive_table_clone( $hot, $archive ); $now = strtotime( $gmt_ts . ' UTC' ); $hot_cut = gmdate( 'Y-m-d H:i:s', $now - self::INTENT_HOT_DAYS * DAY_IN_SECONDS ); if ( ! AIEO_Remote_Export_Manager::can_purge_through( 'aieo_intent_events', $hot_cut ) ) { aieo_debug_log( 'AIEO_Archive_Handlers: holding intent_events purge — remote export not caught up to ' . $hot_cut ); $summary['intent_held'] = 1; } else { list( $moved, $deleted ) = self::archive_then_delete_by_ts( $hot, $archive, 'ts', $hot_cut ); $summary['intent_archived'] = $moved; $summary['intent_deleted'] = $deleted; } } private static function archive_then_delete_by_ts( $hot, $archive, $ts_col, $cutoff ) { global $wpdb; $moved = 0; $deleted = 0; self::$copy_failed = false; $cols = self::get_column_names( $hot ); if ( empty( $cols ) ) { return array( 0, 0 ); } $col_list = '`' . implode( '`,`', $cols ) . '`'; $ids = ''; if ( in_array( 'id', $cols, true ) ) { $max_id = (int) apply_filters( 'aieo_archive_move_max_id', (int) $wpdb->get_var( "SELECT COALESCE(MAX(id), 0) FROM {$hot}" ), substr( $hot, strlen( $wpdb->prefix ) ) ); if ( $max_id < 1 ) { return array( 0, 0 ); } $ids = ' AND id <= ' . $max_id; } $moved = self::copy_rows( $hot, $archive, $col_list, $wpdb->prepare( "{$ts_col} < %s", $cutoff ) . $ids ); if ( false === $moved ) { aieo_debug_log( 'AIEO_Archive_Handlers: copy into ' . $archive . ' failed, hot rows kept: ' . $wpdb->last_error ); self::$copy_failed = true; return array( 0, 0 ); } $moved = (int) $moved; for ( $i = 0; $i < self::MAX_ITERATIONS; $i++ ) { $d = (int) $wpdb->query( $wpdb->prepare( "DELETE FROM {$hot} WHERE {$ts_col} < %s{$ids} LIMIT %d", $cutoff, self::BATCH_SIZE ) ); $deleted += $d; if ( $d < self::BATCH_SIZE ) { break; } } return array( $moved, $deleted ); } private static function copy_rows( $hot, $archive, $col_list, $where ) { global $wpdb; return $wpdb->query( "INSERT IGNORE INTO {$archive} ({$col_list}, archived_at) SELECT {$col_list}, UTC_TIMESTAMP() FROM {$hot} WHERE {$where}" ); } public static function keep_sightings() { global $wpdb; $hot = $wpdb->prefix . 'aieo_reco_events'; $archive = $wpdb->prefix . 'aieo_reco_events_archive'; if ( ! self::table_exists( $hot ) ) { return 0; } $types = class_exists( 'AIEO_Sighting' ) ? array( AIEO_Sighting::SIGHTING, AIEO_Sighting::ENGAGED ) : array( 'offer_sighting', 'offer_engaged' ); $where = $wpdb->prepare( 'event_type IN (%s, %s)', $types[0], $types[1] ); $any = $wpdb->get_var( "SELECT 1 FROM {$hot} WHERE {$where} LIMIT 1" ); if ( null === $any ) { return '' === (string) $wpdb->last_error ? 0 : false; } self::ensure_archive_table_clone( $hot, $archive ); $cols = self::get_column_names( $hot ); if ( empty( $cols ) || ! self::table_exists( $archive ) ) { return false; } $copied = self::copy_rows( $hot, $archive, '`' . implode( '`,`', $cols ) . '`', $where ); if ( false === $copied ) { aieo_debug_log( 'AIEO_Archive_Handlers: the sightings\' copy into ' . $archive . ' failed, nothing cleared: ' . $wpdb->last_error ); return false; } $missing = $wpdb->get_var( "SELECT COUNT(*) FROM {$hot} h LEFT JOIN {$archive} a ON a.id = h.id AND a.event_id = h.event_id WHERE " . $wpdb->prepare( 'h.event_type IN (%s, %s)', $types[0], $types[1] ) . ' AND a.id IS NULL' ); if ( null === $missing || (int) $missing > 0 ) { aieo_debug_log( 'AIEO_Archive_Handlers: ' . ( null === $missing ? 'the sightings\' check failed: ' . $wpdb->last_error : (int) $missing . ' sightings not in ' . $archive . ' (an id it holds already)' ) . ', nothing cleared' ); return false; } return (int) $copied; } public static function highest_id( $table ) { global $wpdb; $id = $wpdb->get_var( "SELECT COALESCE(MAX(id), 0) FROM {$table}" ); return null === $id ? null : (int) $id; } public static function keep_ids_past( $hot, $archive, $before = 0 ) { global $wpdb; if ( ! self::table_exists( $hot ) ) { return true; } $high = max( 0, (int) $before ); foreach ( array( $hot, $archive ) as $table ) { if ( $table === $archive && ( '' === (string) $archive || ! self::table_exists( $archive ) ) ) { continue; } $id = self::highest_id( $table ); if ( null === $id ) { return false; } $high = max( $high, $id ); } $row = $wpdb->get_row( "SHOW CREATE TABLE {$hot}", ARRAY_N ); if ( empty( $row[1] ) ) { return false; } $next = $high + 1; $now = preg_match( '/\bAUTO_INCREMENT=(\d+)/', (string) $row[1], $m ) ? (int) $m[1] : 1; if ( $next <= $now ) { return true; } return false !== $wpdb->query( "ALTER TABLE {$hot} AUTO_INCREMENT = " . (int) $next ); } private static function ensure_archive_table_clone( $hot, $archive ) { global $wpdb; if ( self::table_exists( $archive ) ) { return; } $row = $wpdb->get_row( "SHOW CREATE TABLE {$hot}", ARRAY_N ); if ( empty( $row[1] ) ) { return; } $create = (string) $row[1]; $create = preg_replace( '/^CREATE TABLE `?[^`(\s]+`?\s*\(/', 'CREATE TABLE `' . $archive . '` (', $create, 1 ); $create = preg_replace( '/\s+AUTO_INCREMENT=\d+/i', '', $create ); $lines = preg_split( '/\r?\n/', $create ); $keep = array(); foreach ( $lines as $line ) { $ltrim = ltrim( $line ); if ( stripos( $ltrim, 'UNIQUE KEY' ) === 0 && stripos( $ltrim, 'event_id' ) !== false ) { continue; } $keep[] = $line; } $create = implode( "\n", $keep ); if ( preg_match( '/\)(\s*ENGINE=|\s*DEFAULT CHARSET=|\s*COLLATE=|\s*;|\s*$)/i', $create, $m, PREG_OFFSET_CAPTURE ) ) { $pos = $m[0][1]; $inject = ",\n `archived_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,\n KEY `archived_at` (`archived_at`)\n"; $create = substr( $create, 0, $pos ) . $inject . substr( $create, $pos ); } $wpdb->query( $create ); } private static function get_column_names( $table ) { global $wpdb; $rows = $wpdb->get_results( "SHOW COLUMNS FROM {$table}", ARRAY_A ); $cols = array(); foreach ( (array) $rows as $r ) { if ( ! empty( $r['Field'] ) && false === stripos( (string) ( $r['Extra'] ?? '' ), 'GENERATED' ) ) { $cols[] = (string) $r['Field']; } } return $cols; } private static function table_exists( $table ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table; } }