PluginProbe
Auto Alt Text / 2.2.0
Auto Alt Text v2.2.0
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
auto-alt-text / src / App / Logging / DBLogger.php

DBLogger.php in Auto Alt Text 2.2.0, at src/App/Logging/DBLogger.php

121 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }