table = $wpdb->prefix . 'aipt_request_claims'; } /** * Schedule the daily cleanup cron job. * * @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; } } /** * 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; } } /** * Delete claim rows older than {@see RETENTION_SECONDS}, regardless of * status: a 'done' row this old has long since fallen out of its * cache-serve window, and a 'pending' row this old is an abandoned * claim RequestClaimStore's own reclaim path would already treat as * stale. * * @return int|false Number of deleted rows or false on error. */ public function clean_expired_rows() { global $wpdb; try { return $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE created_at < %d', $this->table, time() - self::RETENTION_SECONDS ) ); } catch ( \Exception $e ) { return false; } } public static function cron_callback(): void { try { $cleaner = new self(); $cleaner->clean_expired_rows(); } catch ( \Exception $e ) { } } }