| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Logging; |
| 4 |
|
| 5 |
class DBLogger implements LoggerInterface |
| 6 |
{ |
| 7 |
private function __construct() |
| 8 |
{ |
| 9 |
|
| 10 |
} |
| 11 |
|
| 12 |
public static function make(): DBLogger |
| 13 |
{ |
| 14 |
return new self(); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Write a new record for the single error |
| 19 |
* @param int $imageId |
| 20 |
* @param string $errorMessage |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function writeImageLog(int $imageId, string $errorMessage): void |
| 24 |
{ |
| 25 |
global $wpdb; |
| 26 |
|
| 27 |
if(!$this->logTableExists()) { |
| 28 |
$this->createLogTable(); |
| 29 |
} |
| 30 |
|
| 31 |
$sanitizedErrorMessage = sanitize_text_field($errorMessage); |
| 32 |
|
| 33 |
$currentDateTime = current_time('mysql'); |
| 34 |
$wpdb->insert( |
| 35 |
$wpdb->prefix . 'aatxt_logs', |
| 36 |
[ |
| 37 |
'time' => $currentDateTime, |
| 38 |
'image_id' => $imageId, |
| 39 |
'error_message' => $sanitizedErrorMessage |
| 40 |
], |
| 41 |
['%s', '%d', '%s'] |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get all error records from the logs table |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function getImageLog(): string |
| 50 |
{ |
| 51 |
global $wpdb; |
| 52 |
$output = ""; |
| 53 |
|
| 54 |
if(!$this->logTableExists()) { |
| 55 |
return $output; |
| 56 |
} |
| 57 |
|
| 58 |
$query = "SELECT * FROM {$wpdb->prefix}aatxt_logs ORDER BY time DESC"; |
| 59 |
$logs = $wpdb->get_results($query, ARRAY_A); |
| 60 |
|
| 61 |
if(empty($logs)) { |
| 62 |
return $output; |
| 63 |
} |
| 64 |
|
| 65 |
foreach ($logs as $log) { |
| 66 |
$output .= sprintf("[%s] - Image ID: %d - Error: %s\n", |
| 67 |
$log['time'], $log['image_id'], $log['error_message']); |
| 68 |
} |
| 69 |
|
| 70 |
return $output; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Check if Log table exists |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
private function logTableExists(): bool |
| 78 |
{ |
| 79 |
global $wpdb; |
| 80 |
$tableCheckQuery = $wpdb->prepare("SHOW TABLES LIKE %s", $wpdb->prefix . 'aatxt_logs'); |
| 81 |
|
| 82 |
return $wpdb->get_var($tableCheckQuery) == $wpdb->prefix . 'aatxt_logs'; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Create the Log table |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function createLogTable(): void |
| 90 |
{ |
| 91 |
global $wpdb; |
| 92 |
$tableCheckQuery = $wpdb->prepare("SHOW TABLES LIKE %s", $wpdb->prefix . 'aatxt_logs'); |
| 93 |
|
| 94 |
if ($wpdb->get_var($tableCheckQuery) != $wpdb->prefix . 'aatxt_logs') { |
| 95 |
$charset_collate = $wpdb->get_charset_collate(); |
| 96 |
|
| 97 |
$sql = "CREATE TABLE {$wpdb->prefix}aatxt_logs ( |
| 98 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 99 |
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, |
| 100 |
image_id mediumint(9) NOT NULL, |
| 101 |
error_message text NOT NULL, |
| 102 |
PRIMARY KEY (id) |
| 103 |
) $charset_collate;"; |
| 104 |
|
| 105 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 106 |
dbDelta($sql); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Dropt the Log table |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
public function dropLogTable(): void |
| 115 |
{ |
| 116 |
global $wpdb; |
| 117 |
$sql = "DROP TABLE IF EXISTS {$wpdb->prefix}aatxt_logs;"; |
| 118 |
$wpdb->query($sql); |
| 119 |
} |
| 120 |
|
| 121 |
} |