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; } } /** * Delete claim rows past their retention: {@see RETENTION_SECONDS} for * every status but the {@see HOLD_STATUSES}, {@see HELD_RETENTION_SECONDS} * for those. * * 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. * * A `held` row is a run hold waiting for something to release it (the Business * run watcher, which runs on the automation cron tick). On a quiet site WP-Cron * can idle for hours, so the hour limit would delete a hold before the * watcher ever saw it. A `released` row is a hand-over the watcher retries * until it finishes, and a `handed` row tells a tab that wakes up later where * its run went (the job that finishes it); without it the tab would find * nothing and generate those products again. The day limit keeps all of them * long enough and still clears the holds a site left behind after a downgrade * to the free build, which has no watcher. * * @return int|false Number of deleted rows or false on error. */ public function clean_expired_rows() { global $wpdb; try { // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table cleanup. $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE status <> 'held' AND status <> 'released' AND status <> 'handed' AND created_at < %d", $this->table, time() - self::RETENTION_SECONDS ) ); if ( false === $deleted ) { return false; } foreach ( self::HOLD_STATUSES as $status ) { $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE status = %s AND created_at < %d', $this->table, $status, time() - self::HELD_RETENTION_SECONDS ) ); if ( false === $result ) { return false; } $deleted += (int) $result; } // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching return (int) $deleted; } catch ( \Exception $e ) { return false; } } public static function cron_callback(): void { try { $cleaner = new self(); $cleaner->clean_expired_rows(); } catch ( \Throwable $e ) { Logger::error( 'cron', 'Request claim cleanup failed', array( 'error' => $e->getMessage() ) ); } } }