tableName = $wpdb->prefix . 'oneclickformsync_logs'; } /** * @inheritDoc */ public function save(LogEntry $entry): bool { global $wpdb; $data = $entry->toArray(); // Always strip target auto-increment id to prevent primary key conflicts. unset($data['id']); $submissionId = $entry->getSubmissionId(); if (!empty($submissionId)) { $existingId = $wpdb->get_var($wpdb->prepare( "SELECT id FROM {$this->tableName} WHERE submission_id = %s LIMIT 1", $submissionId )); if ($existingId) { $result = $wpdb->update($this->tableName, $data, ['id' => (int)$existingId]); $entry->setId((int)$existingId); return $result !== false; } } $result = $wpdb->insert($this->tableName, $data); if ($result) { $entry->setId((int)$wpdb->insert_id); return true; } return false; } /** * @inheritDoc */ public function findAll(int $limit = 50, int $offset = 0): array { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$this->tableName} ORDER BY created_at DESC LIMIT %d OFFSET %d", $limit, $offset ), ARRAY_A ); $entries = []; foreach ($rows as $row) { $entries[] = new LogEntry($row); } return $entries; } /** * @inheritDoc */ public function countAll(): int { global $wpdb; return (int)$wpdb->get_var("SELECT COUNT(*) FROM {$this->tableName}"); } /** * @inheritDoc */ public function clearOldLogs(int $daysRetention): int { global $wpdb; // Calculate date limit. $cutoffDate = gmdate('Y-m-d H:i:s', time() - ($daysRetention * DAY_IN_SECONDS)); $result = $wpdb->query( $wpdb->prepare("DELETE FROM {$this->tableName} WHERE created_at < %s", $cutoffDate) ); return $result !== false ? (int)$result : 0; } /** * @inheritDoc */ public function clearAll(): bool { global $wpdb; $result = $wpdb->query("TRUNCATE TABLE {$this->tableName}"); if ($result === false) { $result = $wpdb->query("DELETE FROM {$this->tableName}"); } return $result !== false; } }