PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.1.0
GiveWP – Donation Plugin and Fundraising Platform v4.1.0
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / Log / LogRepository.php
LogRepository.php
450 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Log;
4
5 use DateTime;
6 use Give\Framework\Database\DB;
7 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
8 use WP_REST_Request;
9
10 /**
11 * Class LogRepository
12 * @package Give\Log\Repositories
13 *
14 * @since 2.10.0
15 */
16 class LogRepository
17 {
18
19 const SORTABLE_COLUMNS = ['id', 'category', 'source', 'log_type', 'date'];
20
21 const LOGS_PER_PAGE = 10;
22
23 /**
24 * @var string
25 */
26 private $log_table;
27
28 /**
29 * LogRepository constructor.
30 */
31 public function __construct()
32 {
33 global $wpdb;
34 $this->log_table = "{$wpdb->prefix}give_log";
35 }
36
37 /**
38 * Insert Log into database
39 *
40 * @param LogModel $model
41 *
42 * @return int inserted log id
43 */
44 public function insertLog(LogModel $model)
45 {
46 DB::insert(
47 $this->log_table,
48 [
49 'log_type' => $model->getType(),
50 'data' => $model->getData($jsonEncode = true),
51 'category' => $model->getCategory(),
52 'source' => $model->getSource(),
53 'date' => $model->getDate(),
54 ],
55 null
56 );
57
58 return DB::last_insert_id();
59 }
60
61 /**
62 * Update log
63 *
64 * @param LogModel $model
65 *
66 * @return false|int
67 */
68 public function updateLog(LogModel $model)
69 {
70 return DB::update(
71 $this->log_table,
72 [
73 'log_type' => $model->getType(),
74 'data' => $model->getData($jsonEncode = true),
75 'category' => $model->getCategory(),
76 'source' => $model->getSource(),
77 'date' => $model->getDate(),
78 ],
79 [
80 'id' => $model->getId(),
81 ]
82 );
83 }
84
85 /**
86 * Get all logs
87 *
88 * @return LogModel[]
89 */
90 public function getLogs()
91 {
92 $logs = [];
93 $result = DB::get_results("SELECT * FROM {$this->log_table} ORDER BY id DESC");
94
95 if ($result) {
96 foreach ($result as $log) {
97 $data = json_decode($log->data, true);
98
99 $logs[] = LogFactory::make(
100 $log->log_type,
101 $data['message'],
102 $log->category,
103 $log->source,
104 $data['context'],
105 $log->id,
106 $log->date
107 );
108 }
109 }
110
111 return $logs;
112 }
113
114 /**
115 * Get all logs for request
116 *
117 * @param WP_REST_Request $request
118 *
119 * @return LogModel[]
120 */
121 public function getLogsForRequest(WP_REST_Request $request)
122 {
123 $logs = [];
124
125 $type = $request->get_param('type');
126 $category = $request->get_param('category');
127 $source = $request->get_param('source');
128 $page = $request->get_param('page');
129 $sortBy = $request->get_param('sort');
130 $startDate = $request->get_param('start');
131 $endDate = $request->get_param('end');
132 $sortDirection = $request->get_param('direction');
133
134 $offset = ($page - 1) * self::LOGS_PER_PAGE;
135
136 $query = "SELECT * FROM {$this->log_table} WHERE 1=1";
137
138 if ($type) {
139 $query .= sprintf(' AND log_type = "%s"', esc_sql($type));
140 }
141
142 if ($category) {
143 $query .= sprintf(' AND category = "%s"', esc_sql($category));
144 }
145
146 if ($source) {
147 $query .= sprintf(' AND source = "%s"', esc_sql($source));
148 }
149
150 if ($startDate) {
151 $startDate = new DateTime($startDate);
152 $query .= sprintf(" AND date(date) >= '%s'", $startDate->format('Y-m-d'));
153 }
154
155 if ($endDate) {
156 $endDate = new DateTime($endDate);
157 $query .= sprintf(" AND date(date) <= '%s'", $endDate->format('Y-m-d'));
158 }
159
160 if ($sortBy) {
161 $column = (in_array($sortBy, self::SORTABLE_COLUMNS, true)) ? $sortBy : 'id';
162 $direction = ($sortDirection && strtoupper($sortDirection) === 'ASC') ? 'ASC' : 'DESC';
163
164 $query .= " ORDER BY `{$column}` {$direction}";
165 } else {
166 $query .= ' ORDER BY id DESC';
167 }
168
169 // Limit
170 $query .= sprintf(' LIMIT %d', self::LOGS_PER_PAGE);
171
172 // Offset
173 if ($offset > 1) {
174 $query .= sprintf(' OFFSET %d', $offset);
175 }
176
177 $result = DB::get_results($query);
178
179 if ($result) {
180 foreach ($result as $log) {
181 $data = json_decode($log->data, true);
182
183 $logs[] = LogFactory::make(
184 $log->log_type,
185 $data['message'],
186 $log->category,
187 $log->source,
188 $data['context'],
189 $log->id,
190 $log->date
191 );
192 }
193 }
194
195 return $logs;
196 }
197
198 /**
199 * Get log by ID
200 *
201 * @param int $logId
202 *
203 * @return LogModel|null
204 */
205 public function getLog($logId)
206 {
207 $log = DB::get_row(
208 DB::prepare("SELECT * FROM {$this->log_table} WHERE id = %d", $logId)
209 );
210
211 if ($log) {
212 $data = json_decode($log->data, true);
213
214 return LogFactory::make(
215 $log->log_type,
216 $data['message'],
217 $log->category,
218 $log->source,
219 $data['context'],
220 $log->id,
221 $log->date
222 );
223 }
224
225 return null;
226 }
227
228 /**
229 * Get logs by type
230 *
231 * @param string $type
232 *
233 * @return LogModel[]
234 */
235 public function getLogsByType($type)
236 {
237 $logs = [];
238
239 $result = DB::get_results(
240 DB::prepare("SELECT * FROM {$this->log_table} WHERE log_type = %s", $type)
241 );
242
243 if ($result) {
244 foreach ($result as $log) {
245 $data = json_decode($log->data, true);
246
247 $logs[] = LogFactory::make(
248 $log->log_type,
249 $data['message'],
250 $log->category,
251 $log->source,
252 $data['context'],
253 $log->id,
254 $log->date
255 );
256 }
257 }
258
259 return $logs;
260 }
261
262 /**
263 * Get logs by category
264 *
265 * @param string $category
266 *
267 * @return LogModel[]
268 */
269 public function getLogsByCategory($category)
270 {
271 $logs = [];
272
273 $result = DB::get_results(
274 DB::prepare("SELECT * FROM {$this->log_table} WHERE category = %s", $category)
275 );
276
277 if ($result) {
278 foreach ($result as $log) {
279 $data = json_decode($log->data, true);
280
281 $logs[] = LogFactory::make(
282 $log->log_type,
283 $data['message'],
284 $log->category,
285 $log->source,
286 $data['context'],
287 $log->id,
288 $log->date
289 );
290 }
291 }
292
293 return $logs;
294 }
295
296 /**
297 * Get logs categories
298 *
299 * @return array
300 */
301 public function getCategories()
302 {
303 $categories = [];
304 $result = DB::get_results("SELECT DISTINCT category FROM {$this->log_table}");
305
306 if ($result) {
307 foreach ($result as $category) {
308 $categories[] = $category->category;
309 }
310 }
311
312 return $categories;
313 }
314
315 /**
316 * Get logs sources
317 *
318 * @return array
319 */
320 public function getSources()
321 {
322 $sources = [];
323 $result = DB::get_results("SELECT DISTINCT source FROM {$this->log_table}");
324
325 if ($result) {
326 foreach ($result as $source) {
327 $sources[] = $source->source;
328 }
329 }
330
331 return $sources;
332 }
333
334 /**
335 * Delete all logs
336 */
337 public function deleteLogs()
338 {
339 DB::query("DELETE FROM {$this->log_table}");
340 }
341
342 /**
343 * Delete log by ID
344 *
345 * @param int $logId
346 */
347 public function deleteLog($logId)
348 {
349 DB::query(
350 DB::prepare("DELETE FROM {$this->log_table} WHERE id = %d", $logId)
351 );
352 }
353
354 public function getTotalCount()
355 {
356 return DB::get_var("SELECT count(id) FROM {$this->log_table}");
357 }
358
359 /**
360 * Get log count by column name containing value
361 *
362 * @param string $columnName
363 * @param string $value
364 *
365 * @return string|null
366 */
367 public function getLogCountBy($columnName, $value)
368 {
369 if ( ! in_array($columnName, self::SORTABLE_COLUMNS, true)) {
370 throw new InvalidArgumentException(
371 sprintf('Invalid column %s', $columnName)
372 );
373 }
374
375 return DB::get_var(
376 DB::prepare("SELECT count(id) FROM {$this->log_table} WHERE {$columnName}=%s", $value)
377 );
378 }
379
380 /**
381 * Get log count for request
382 *
383 * @param WP_REST_Request $request
384 *
385 * @return int|null
386 */
387 public function getLogCountForRequest(WP_REST_Request $request)
388 {
389 $type = $request->get_param('type');
390 $category = $request->get_param('category');
391 $source = $request->get_param('source');
392 $startDate = $request->get_param('start');
393 $endDate = $request->get_param('end');
394
395 $query = "SELECT count(id) FROM {$this->log_table} WHERE 1=1";
396
397 if ($type) {
398 $query .= sprintf(' AND log_type = "%s"', esc_sql($type));
399 }
400
401 if ($category) {
402 $query .= sprintf(' AND category = "%s"', esc_sql($category));
403 }
404
405 if ($source) {
406 $query .= sprintf(' AND source = "%s"', esc_sql($source));
407 }
408
409 if ($startDate) {
410 $startDate = new DateTime($startDate);
411 $query .= sprintf(" AND date(date) >= '%s'", $startDate->format('Y-m-d'));
412 }
413
414 if ($endDate) {
415 $endDate = new DateTime($endDate);
416 $query .= sprintf(" AND date(date) <= '%s'", $endDate->format('Y-m-d'));
417 }
418
419 return DB::get_var($query);
420 }
421
422 /**
423 * Get sortable columns
424 *
425 * @return string[]
426 */
427 public function getSortableColumns()
428 {
429 return self::SORTABLE_COLUMNS;
430 }
431
432 /**
433 * Get logs per page limit
434 *
435 * @return int
436 */
437 public function getLogsPerPageLimit()
438 {
439 return self::LOGS_PER_PAGE;
440 }
441
442 /**
443 * Flush logs
444 */
445 public function flushLogs()
446 {
447 DB::query("DELETE FROM {$this->log_table}");
448 }
449 }
450