| 1 |
<?php |
| 2 |
|
| 3 |
namespace Moloni\Models; |
| 4 |
|
| 5 |
use Moloni\Storage; |
| 6 |
|
| 7 |
class Logs |
| 8 |
{ |
| 9 |
private static $limit = 20; |
| 10 |
private static $totalPages = 1; |
| 11 |
private static $currentPage = 1; |
| 12 |
|
| 13 |
private static $filterDate = ''; |
| 14 |
private static $filterMessage = ''; |
| 15 |
private static $filterLevel = ''; |
| 16 |
private static $filterContext = ''; |
| 17 |
|
| 18 |
public static function getAllAvailable(): array |
| 19 |
{ |
| 20 |
self::$currentPage = (isset($_GET['paged']) && (int)($_GET['paged']) > 0) ? (int)$_GET['paged'] : 1; |
| 21 |
|
| 22 |
self::$filterDate = sanitize_text_field($_GET['filter_date'] ?? $_POST['filter_date'] ?? ''); |
| 23 |
self::$filterMessage = sanitize_text_field($_GET['filter_message'] ?? $_POST['filter_message'] ?? ''); |
| 24 |
self::$filterLevel = sanitize_text_field($_GET['filter_level'] ?? $_POST['filter_level'] ?? ''); |
| 25 |
self::$filterContext = sanitize_text_field($_GET['filter_context'] ?? $_POST['filter_context'] ?? ''); |
| 26 |
|
| 27 |
return self::getAll(); |
| 28 |
} |
| 29 |
|
| 30 |
public static function getPagination() |
| 31 |
{ |
| 32 |
$baseArguments = add_query_arg([ |
| 33 |
'paged' => '%#%', |
| 34 |
'filter_date' => self::$filterDate, |
| 35 |
'filter_message' => self::$filterMessage, |
| 36 |
'filter_level' => self::$filterLevel, |
| 37 |
]); |
| 38 |
|
| 39 |
$args = [ |
| 40 |
'base' => $baseArguments, |
| 41 |
'format' => '', |
| 42 |
'current' => self::$currentPage, |
| 43 |
'total' => self::$totalPages, |
| 44 |
]; |
| 45 |
|
| 46 |
return paginate_links($args); |
| 47 |
} |
| 48 |
|
| 49 |
public static function removeOlderLogs() |
| 50 |
{ |
| 51 |
global $wpdb; |
| 52 |
|
| 53 |
$query = $wpdb->prepare( |
| 54 |
"DELETE FROM `" . $wpdb->get_blog_prefix() . "moloni_logs` WHERE created_at < %s", |
| 55 |
date('Y-m-d H:i:s', strtotime("-1 week")) |
| 56 |
); |
| 57 |
|
| 58 |
$wpdb->query($query); |
| 59 |
} |
| 60 |
|
| 61 |
public static function removeDebugLogs() |
| 62 |
{ |
| 63 |
global $wpdb; |
| 64 |
|
| 65 |
$query = $wpdb->prepare( |
| 66 |
"DELETE FROM `" . $wpdb->get_blog_prefix() . "moloni_logs` WHERE log_level = %s", |
| 67 |
'debug' |
| 68 |
); |
| 69 |
|
| 70 |
$wpdb->query($query); |
| 71 |
} |
| 72 |
|
| 73 |
// Privates // |
| 74 |
|
| 75 |
/** |
| 76 |
* Fetch logs |
| 77 |
* |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
private static function getAll(): array |
| 81 |
{ |
| 82 |
global $wpdb; |
| 83 |
|
| 84 |
$limit = self::$limit; |
| 85 |
$offset = self::$currentPage <= 1 ? 0 : (self::$currentPage - 1) * self::$limit; |
| 86 |
|
| 87 |
/** Totals */ |
| 88 |
|
| 89 |
$query = "SELECT COUNT(*) FROM `" . $wpdb->get_blog_prefix() . "moloni_logs`"; |
| 90 |
$arguments = []; |
| 91 |
|
| 92 |
self::applyFilters($query, $arguments); |
| 93 |
|
| 94 |
$queryClean = $wpdb->prepare($query, ...$arguments); |
| 95 |
$queryResult = $wpdb->get_row($queryClean, ARRAY_A); |
| 96 |
|
| 97 |
$numLogs = (int)($queryResult['COUNT(*)'] ?? 0); |
| 98 |
|
| 99 |
/** Can safely return if there are no logs */ |
| 100 |
if ($numLogs === 0) { |
| 101 |
return []; |
| 102 |
} |
| 103 |
|
| 104 |
self::$totalPages = ceil($numLogs / self::$limit); |
| 105 |
|
| 106 |
/** Results */ |
| 107 |
|
| 108 |
$query = "SELECT * FROM `" . $wpdb->get_blog_prefix() . "moloni_logs`"; |
| 109 |
$arguments = []; |
| 110 |
|
| 111 |
self::applyFilters($query, $arguments); |
| 112 |
|
| 113 |
$query .= ' ORDER BY id DESC LIMIT %d OFFSET %d'; |
| 114 |
$arguments[] = $limit; |
| 115 |
$arguments[] = $offset; |
| 116 |
|
| 117 |
$queryClean = $wpdb->prepare($query, ...$arguments); |
| 118 |
|
| 119 |
return $wpdb->get_results($queryClean, ARRAY_A); |
| 120 |
} |
| 121 |
|
| 122 |
// Auxiliary // |
| 123 |
|
| 124 |
private static function applyFilters(&$sql, &$arguments) |
| 125 |
{ |
| 126 |
$sql .= ' WHERE company_id = %d'; |
| 127 |
$arguments[] = Storage::$MOLONI_COMPANY_ID ?? 0; |
| 128 |
|
| 129 |
if (!empty(self::$filterMessage)) { |
| 130 |
$sql .= ' AND message LIKE %s'; |
| 131 |
$arguments[] = '%' . self::$filterMessage . '%'; |
| 132 |
} |
| 133 |
|
| 134 |
if (!empty(self::$filterLevel)) { |
| 135 |
$sql .= ' AND log_level = %s'; |
| 136 |
$arguments[] = self::$filterLevel; |
| 137 |
} |
| 138 |
|
| 139 |
if (!empty(self::$filterDate)) { |
| 140 |
$sql .= ' AND created_at LIKE %s'; |
| 141 |
$arguments[] = self::$filterDate . '%'; |
| 142 |
} |
| 143 |
|
| 144 |
if (!empty(self::$filterContext)) { |
| 145 |
$sql .= ' AND context LIKE %s'; |
| 146 |
$arguments[] = '%' . self::$filterContext . '%'; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|