table = $wpdb->prefix . 'aipt_job_logs'; } /** * Schedule the daily cleanup cron job (activation hook entry). * * @return bool Whether scheduling succeeded. */ public function schedule(): bool { try { if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { $scheduled = wp_schedule_event( time(), 'daily', self::CRON_HOOK ); if ( false === $scheduled ) { return false; } } return true; } catch ( \Exception $e ) { return false; } } /** * Re-arm the cleanup schedule if it has been lost. * * Hooked from `admin_init` so that any admin page load on a site whose * cron event was dropped (host outage, race during plugin update, a * misbehaving uninstall hook) restores the schedule without requiring * a reactivation cycle. No-op when the event is already armed. * * @return bool True when the event is (re-)scheduled or already armed. */ public static function ensure_scheduled(): bool { try { if ( wp_next_scheduled( self::CRON_HOOK ) ) { return true; } return false !== wp_schedule_event( time() + MINUTE_IN_SECONDS, 'daily', self::CRON_HOOK ); } catch ( \Exception $e ) { return false; } } /** * Unschedule the cleanup cron job. * * @return bool Whether unscheduling succeeded. */ public function unschedule(): bool { try { $timestamp = wp_next_scheduled( self::CRON_HOOK ); if ( $timestamp ) { $unscheduled = wp_unschedule_event( $timestamp, self::CRON_HOOK ); if ( false === $unscheduled ) { return false; } } return true; } catch ( \Exception $e ) { return false; } } /** * Resolve the active retention threshold in days. * * @return int A positive integer; defaults to {@see self::RETENTION_DAYS} * if the filter returns a non-positive value. */ private function retention_days(): int { $value = (int) apply_filters( 'aipt_automation_job_log_retention_days', self::RETENTION_DAYS ); return $value > 0 ? $value : self::RETENTION_DAYS; } /** * Delete job log records older than the retention period. * * Iterates with `DELETE ... LIMIT` chunks so the table is never held * under a long write lock; a per-run cap bounds the worst-case runtime * so a single cron tick can never stall on multi-gigabyte bloat. * * @return int|false Total number of deleted records (across chunks) * or false on a database-level error. */ public function clean_old_records() { global $wpdb; if ( get_transient( self::LOCK_TRANSIENT ) ) { return 0; } if ( ! set_transient( self::LOCK_TRANSIENT, time(), self::LOCK_TTL_SECONDS ) ) { error_log( '[AIPT] JobLogCleaner: failed to acquire lock transient; aborting run to avoid race.' ); return 0; } $total_deleted = 0; try { $cutoff_date = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $this->retention_days() . ' days' ) ); while ( $total_deleted < self::MAX_ROWS_PER_RUN ) { $query = $wpdb->prepare( "DELETE FROM {$this->table} WHERE created_at < %s LIMIT %d", $cutoff_date, self::DELETE_CHUNK_SIZE ); $deleted = $wpdb->query( $query ); if ( false === $deleted ) { return false; } if ( 0 === (int) $deleted ) { break; } $total_deleted += (int) $deleted; } if ( $total_deleted >= self::MAX_ROWS_PER_RUN ) { error_log( sprintf( '[AIPT] JobLogCleaner: hit MAX_ROWS_PER_RUN=%d in one tick; subsequent ticks will finish the backlog.', self::MAX_ROWS_PER_RUN ) ); } return $total_deleted; } catch ( \Exception $e ) { return false; } finally { delete_transient( self::LOCK_TRANSIENT ); } } /** * Static cron callback. */ public static function cron_callback(): void { try { $cleaner = new self(); $cleaner->clean_old_records(); } catch ( \Exception $e ) { } } }