PluginProbe
Auto Alt Text / trunk
Auto Alt Text vtrunk
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 / Infrastructure / Database / ErrorLogSchema.php

ErrorLogSchema.php in Auto Alt Text trunk, at src/App/Infrastructure/Database/ErrorLogSchema.php

103 lines 2.4 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\Infrastructure\Database;
4
5 /**
6 * Manages the database schema for error logs table
7 *
8 * This class is responsible for creating, dropping, and checking the existence
9 * of the error logs table in the WordPress database.
10 */
11 final class ErrorLogSchema
12 {
13 /**
14 * WordPress database abstraction object
15 *
16 * @var \wpdb
17 */
18 private $wpdb;
19
20 /**
21 * Table name without prefix
22 *
23 * @var string
24 */
25 private $tableName;
26
27 /**
28 * Constructor
29 *
30 * @param \wpdb $wpdb WordPress database object
31 */
32 public function __construct(\wpdb $wpdb)
33 {
34 $this->wpdb = $wpdb;
35 $this->tableName = 'aatxt_logs';
36 }
37
38 /**
39 * Create the error logs table
40 *
41 * Creates the table if it doesn't exist using dbDelta for safe schema updates.
42 *
43 * @return void
44 */
45 public function create(): void
46 {
47 if ($this->exists()) {
48 return;
49 }
50
51 $charset_collate = $this->wpdb->get_charset_collate();
52 $tableName = $this->getTableName();
53
54 $sql = "CREATE TABLE {$tableName} (
55 id mediumint(9) NOT NULL AUTO_INCREMENT,
56 time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
57 image_id mediumint(9) NOT NULL,
58 error_message text NOT NULL,
59 PRIMARY KEY (id)
60 ) $charset_collate;";
61
62 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
63 dbDelta($sql);
64 }
65
66 /**
67 * Drop the error logs table
68 *
69 * Permanently removes the table from the database.
70 *
71 * @return void
72 */
73 public function drop(): void
74 {
75 $tableName = $this->getTableName();
76 $sql = "DROP TABLE IF EXISTS {$tableName};";
77 $this->wpdb->query($sql);
78 }
79
80 /**
81 * Check if the error logs table exists
82 *
83 * @return bool True if table exists, false otherwise
84 */
85 public function exists(): bool
86 {
87 $tableName = $this->getTableName();
88 $query = $this->wpdb->prepare("SHOW TABLES LIKE %s", $tableName);
89
90 return $this->wpdb->get_var($query) === $tableName;
91 }
92
93 /**
94 * Get the full table name with WordPress prefix
95 *
96 * @return string Full table name including WordPress prefix
97 */
98 public function getTableName(): string
99 {
100 return $this->wpdb->prefix . $this->tableName;
101 }
102 }
103