tableName = $wpdb->prefix . 'oneclickformsync_queue'; } /** * @inheritDoc */ public function find(int $id): ?QueueItem { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$this->tableName} WHERE id = %d", $id), ARRAY_A ); if (!$row) { return null; } return new QueueItem($row); } /** * @inheritDoc */ public function findPending(int $limit = 10): array { global $wpdb; $now = current_time('mysql', true); // Select items that are pending and ready to be processed. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$this->tableName} WHERE status = 'pending' AND (next_attempt_at IS NULL OR next_attempt_at <= %s) ORDER BY created_at ASC LIMIT %d", $now, $limit ), ARRAY_A ); $items = []; foreach ($rows as $row) { $items[] = new QueueItem($row); } return $items; } /** * @inheritDoc */ public function save(QueueItem $item): bool { global $wpdb; $data = $item->toArray(); $data['updated_at'] = current_time('mysql', true); if ($item->getId() !== null) { $result = $wpdb->update( $this->tableName, $data, ['id' => $item->getId()] ); return $result !== false; } else { // Remove null ID for auto-increment. unset($data['id']); $result = $wpdb->insert($this->tableName, $data); if ($result) { $item->setId((int)$wpdb->insert_id); return true; } return false; } } /** * @inheritDoc */ public function delete(int $id): bool { global $wpdb; $result = $wpdb->delete( $this->tableName, ['id' => $id] ); return $result !== false; } }