PluginProbe
Auto Alt Text / 2.5.2
Auto Alt Text v2.5.2
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
← All changes | src/App/Logging/DBLogger.php +71 -88 2.6.02.5.2 View file →
@@ -1,101 +1,71 @@
1 1 <?php
2 2
3 3 namespace AATXT\App\Logging;
4 4
5 -use AATXT\App\Domain\Entities\ErrorLog;
6 -use AATXT\App\Infrastructure\Database\ErrorLogSchema;
7 -use AATXT\App\Infrastructure\Repositories\ErrorLogRepositoryInterface;
8 -
9 -/**
10 - * Database Logger
11 - *
12 - * Logs error messages to the database using the ErrorLogRepository.
13 - * This class has been refactored to use dependency injection and the repository pattern.
14 - */
15 5 class DBLogger implements LoggerInterface
16 6 {
17 - /**
18 - * Error log repository
19 - *
20 - * @var ErrorLogRepositoryInterface
21 - */
22 - private $repository;
7 + private function __construct()
8 + {
23 9
24 - /**
25 - * Database schema manager
26 - *
27 - * @var ErrorLogSchema
28 - */
29 - private $schema;
30 -
31 - /**
32 - * Constructor
33 - *
34 - * @param ErrorLogRepositoryInterface $repository Error log repository
35 - * @param ErrorLogSchema $schema Database schema manager
36 - */
37 - public function __construct(ErrorLogRepositoryInterface $repository, ErrorLogSchema $schema)
38 - {
39 - $this->repository = $repository;
40 - $this->schema = $schema;
41 10 }
42 11
43 - /**
44 - * Create DBLogger instance (static factory method)
45 - *
46 - * @deprecated 2.6.0 Use dependency injection instead. This method will be removed in v3.0.0.
47 - * @return DBLogger
48 - */
49 12 public static function make(): DBLogger
50 13 {
51 - _deprecated_function(__METHOD__, '2.6.0', 'Dependency injection via Container');
52 -
53 - global $wpdb;
54 -
55 - // Create dependencies manually for backward compatibility
56 - $schema = new ErrorLogSchema($wpdb);
57 - $repository = new \AATXT\App\Infrastructure\Repositories\ErrorLogRepository($wpdb, $schema);
58 -
59 - return new self($repository, $schema);
14 + return new self();
60 15 }
61 16
62 17 /**
63 18 * Write a new record for the single error
64 - *
65 - * @param int $imageId WordPress attachment ID
66 - * @param string $errorMessage Error message text
19 + * @param int $imageId
20 + * @param string $errorMessage
67 21 * @return void
68 22 */
69 23 public function writeImageLog(int $imageId, string $errorMessage): void
70 24 {
71 - $errorLog = new ErrorLog($imageId, $errorMessage);
72 - $this->repository->save($errorLog);
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 + );
73 43 }
74 44
75 45 /**
76 46 * Get all error records from the logs table
77 - *
78 - * Returns a formatted string with all error logs.
79 - *
80 - * @return string Formatted error logs
47 + * @return string
81 48 */
82 49 public function getImageLog(): string
83 50 {
84 - $logs = $this->repository->findAll();
51 + global $wpdb;
52 + $output = "";
85 53
86 - if (empty($logs)) {
87 - return "";
54 + if(!$this->logTableExists()) {
55 + return $output;
88 56 }
89 57
90 - $output = "";
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 +
91 65 foreach ($logs as $log) {
92 - $output .= sprintf(
93 - "[%s] - Image ID: %d - Error: %s\n",
94 - $log->getOccurredAt()->format('Y-m-d H:i:s'),
95 - $log->getImageId(),
96 - $log->getErrorMessage()
97 - );
66 + $output .= sprintf("[%s] - Image ID: %d - Error: %s\n",
67 + $log['time'], $log['image_id'], $log['error_message']);
98 68 }
99 69
100 70 return $output;
101 71 }
@@ -100,39 +70,52 @@
100 70 return $output;
101 71 }
102 72
103 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 + /**
104 86 * Create the Log table
105 - *
106 - * @deprecated 2.6.0 Use ErrorLogSchema::create() instead. This method will be removed in v3.0.0.
107 87 * @return void
108 88 */
109 89 public function createLogTable(): void
110 90 {
111 - _deprecated_function(__METHOD__, '2.6.0', 'ErrorLogSchema::create()');
112 - $this->schema->create();
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 + }
113 108 }
114 109
115 110 /**
116 - * Drop the Log table
117 - *
118 - * @deprecated 2.6.0 Use ErrorLogSchema::drop() instead. This method will be removed in v3.0.0.
111 + * Dropt the Log table
119 112 * @return void
120 113 */
121 114 public function dropLogTable(): void
122 115 {
123 - _deprecated_function(__METHOD__, '2.6.0', 'ErrorLogSchema::drop()');
124 - $this->schema->drop();
116 + global $wpdb;
117 + $sql = "DROP TABLE IF EXISTS {$wpdb->prefix}aatxt_logs;";
118 + $wpdb->query($sql);
125 119 }
126 120
127 - /**
128 - * Check if Log table exists
129 - *
130 - * @deprecated 2.6.0 Use ErrorLogSchema::exists() instead. This method will be removed in v3.0.0.
131 - * @return bool
132 - */
133 - private function logTableExists(): bool
134 - {
135 - _deprecated_function(__METHOD__, '2.6.0', 'ErrorLogSchema::exists()');
136 - return $this->schema->exists();
137 - }
138 -}
121 +}