PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.6.8
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.6.8
2.7.1 2.7.2 2.7.0 2.6.26 2.6.25 2.6.24 2.6.23 2.6.22 2.6.21 2.6.20 2.6.19 2.6.18 2.6.17 2.6.16 2.6.15 2.6.14 2.6.13 2.6.12 2.6.11 2.6.10 2.6.9 2.6.8 2.6.7 2.6.6 2.6.5 All 141 releases
metasync / robots-txt / class-metasync-robots-txt-database.php

class-metasync-robots-txt-database.php in Search Atlas SEO – OTTO AI SEO Automation for WordPress 2.6.8, at robots-txt/class-metasync-robots-txt-database.php

275 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Robots.txt Database Class
5 *
6 * @package Search Atlas SEO
7 * @copyright Copyright (C) 2021-2025, Search Atlas Group - [email protected]
8 * @since 2.5.6
9 */
10
11 // If this file is called directly, abort.
12 if (!defined('WPINC')) {
13 die;
14 }
15
16 class Metasync_Robots_Txt_Database
17 {
18 /**
19 * Instance of this class
20 *
21 * @var Metasync_Robots_Txt_Database
22 */
23 private static $instance = null;
24
25 /**
26 * Table name
27 *
28 * @var string
29 */
30 private $table_name;
31
32 /**
33 * Get singleton instance
34 */
35 public static function get_instance()
36 {
37 if (null === self::$instance) {
38 self::$instance = new self();
39 }
40 return self::$instance;
41 }
42
43 /**
44 * Constructor
45 */
46 private function __construct()
47 {
48 global $wpdb;
49 $this->table_name = $wpdb->prefix . 'metasync_robots_txt_backups';
50 }
51
52 /**
53 * Create database table
54 */
55 public function create_table()
56 {
57 global $wpdb;
58
59 $charset_collate = $wpdb->get_charset_collate();
60
61 $sql = "CREATE TABLE IF NOT EXISTS {$this->table_name} (
62 id bigint(20) NOT NULL AUTO_INCREMENT,
63 content longtext NOT NULL,
64 created_at datetime DEFAULT CURRENT_TIMESTAMP,
65 created_by bigint(20) DEFAULT NULL,
66 PRIMARY KEY (id),
67 KEY created_at (created_at)
68 ) $charset_collate;";
69
70 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
71 dbDelta($sql);
72 }
73
74 /**
75 * Create a backup
76 *
77 * @param string $content Content to backup
78 * @return int|bool Backup ID on success, false on failure
79 */
80 public function create_backup($content)
81 {
82 global $wpdb;
83
84 $result = $wpdb->insert(
85 $this->table_name,
86 array(
87 'content' => $content,
88 'created_by' => get_current_user_id()
89 ),
90 array('%s', '%d')
91 );
92
93 if (false === $result) {
94 return false;
95 }
96
97 // Keep only last 50 backups
98 $this->cleanup_old_backups(50);
99
100 return $wpdb->insert_id;
101 }
102
103 /**
104 * Get backups
105 *
106 * @param int $limit Number of backups to retrieve
107 * @return array Array of backups
108 */
109 public function get_backups($limit = 10)
110 {
111 global $wpdb;
112
113 if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $this->table_name)) !== $this->table_name) {
114 $this->create_table();
115 return array();
116 }
117
118 $limit = absint($limit);
119
120 $results = $wpdb->get_results(
121 $wpdb->prepare(
122 "SELECT b.*, u.display_name as created_by_name
123 FROM {$this->table_name} b
124 LEFT JOIN {$wpdb->users} u ON b.created_by = u.ID
125 ORDER BY b.created_at DESC
126 LIMIT %d",
127 $limit
128 ),
129 ARRAY_A
130 );
131
132 return $results ? $results : array();
133 }
134
135 /**
136 * Get a single backup
137 *
138 * @param int $backup_id Backup ID
139 * @return array|null Backup data or null
140 */
141 public function get_backup($backup_id)
142 {
143 global $wpdb;
144
145 $result = $wpdb->get_row(
146 $wpdb->prepare(
147 "SELECT * FROM {$this->table_name} WHERE id = %d",
148 $backup_id
149 ),
150 ARRAY_A
151 );
152
153 return $result;
154 }
155
156 /**
157 * Delete a backup
158 *
159 * @param int $backup_id Backup ID
160 * @return bool True on success, false on failure
161 */
162 public function delete_backup($backup_id)
163 {
164 global $wpdb;
165
166 $result = $wpdb->delete(
167 $this->table_name,
168 array('id' => $backup_id),
169 array('%d')
170 );
171
172 return false !== $result;
173 }
174
175 /**
176 * Cleanup old backups
177 *
178 * @param int $keep_count Number of backups to keep
179 * @return bool True on success, false on failure
180 */
181 private function cleanup_old_backups($keep_count = 50)
182 {
183 global $wpdb;
184
185 // First, get the IDs to keep (avoids MySQL subquery deadlock)
186 $ids_to_keep = $wpdb->get_col(
187 $wpdb->prepare(
188 "SELECT id FROM {$this->table_name}
189 ORDER BY created_at DESC
190 LIMIT %d",
191 $keep_count
192 )
193 );
194
195 if (empty($ids_to_keep)) {
196 return true;
197 }
198
199 // Delete all records NOT in the keep list
200 $placeholders = implode(',', array_fill(0, count($ids_to_keep), '%d'));
201 $wpdb->query(
202 $wpdb->prepare(
203 "DELETE FROM {$this->table_name}
204 WHERE id NOT IN ($placeholders)",
205 $ids_to_keep
206 )
207 );
208
209 return true;
210 }
211
212 /**
213 * Drop table (for uninstall)
214 */
215 public function drop_table()
216 {
217 global $wpdb;
218 $wpdb->query("DROP TABLE IF EXISTS {$this->table_name}");
219 }
220
221 /**
222 * Store virtual robots.txt content (when physical file cannot be created)
223 *
224 * @param string $content Content to store
225 * @return bool True on success, false on failure
226 */
227 public function store_virtual_content($content)
228 {
229 return update_option('metasync_robots_txt_virtual', $content, false);
230 }
231
232 /**
233 * Get virtual robots.txt content
234 *
235 * @return string|false Content or false if not found
236 */
237 public function get_virtual_content()
238 {
239 return get_option('metasync_robots_txt_virtual', false);
240 }
241
242 /**
243 * Check if virtual mode is active
244 *
245 * @return bool True if virtual mode is active
246 */
247 public function is_virtual_mode()
248 {
249 return get_option('metasync_robots_txt_virtual_mode', false) === true;
250 }
251
252 /**
253 * Set virtual mode flag
254 *
255 * @param bool $enabled Whether virtual mode is enabled
256 * @return bool True on success
257 */
258 public function set_virtual_mode($enabled)
259 {
260 return update_option('metasync_robots_txt_virtual_mode', $enabled, false);
261 }
262
263 /**
264 * Clear virtual content and mode
265 *
266 * @return bool True on success
267 */
268 public function clear_virtual_content()
269 {
270 delete_option('metasync_robots_txt_virtual');
271 delete_option('metasync_robots_txt_virtual_mode');
272 return true;
273 }
274 }
275