PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.6.15
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.6.15
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 2.6.4 2.6.3 All 139 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.15, at robots-txt/class-metasync-robots-txt-database.php

276 lines 6.4 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_at' => current_time('mysql', true),
89 'created_by' => get_current_user_id()
90 ),
91 array('%s', '%s', '%d')
92 );
93
94 if (false === $result) {
95 return false;
96 }
97
98 // Keep only last 50 backups
99 $this->cleanup_old_backups(50);
100
101 return $wpdb->insert_id;
102 }
103
104 /**
105 * Get backups
106 *
107 * @param int $limit Number of backups to retrieve
108 * @return array Array of backups
109 */
110 public function get_backups($limit = 10)
111 {
112 global $wpdb;
113
114 if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $this->table_name)) !== $this->table_name) {
115 $this->create_table();
116 return array();
117 }
118
119 $limit = absint($limit);
120
121 $results = $wpdb->get_results(
122 $wpdb->prepare(
123 "SELECT b.*, u.display_name as created_by_name
124 FROM {$this->table_name} b
125 LEFT JOIN {$wpdb->users} u ON b.created_by = u.ID
126 ORDER BY b.created_at DESC
127 LIMIT %d",
128 $limit
129 ),
130 ARRAY_A
131 );
132
133 return $results ? $results : array();
134 }
135
136 /**
137 * Get a single backup
138 *
139 * @param int $backup_id Backup ID
140 * @return array|null Backup data or null
141 */
142 public function get_backup($backup_id)
143 {
144 global $wpdb;
145
146 $result = $wpdb->get_row(
147 $wpdb->prepare(
148 "SELECT * FROM {$this->table_name} WHERE id = %d",
149 $backup_id
150 ),
151 ARRAY_A
152 );
153
154 return $result;
155 }
156
157 /**
158 * Delete a backup
159 *
160 * @param int $backup_id Backup ID
161 * @return bool True on success, false on failure
162 */
163 public function delete_backup($backup_id)
164 {
165 global $wpdb;
166
167 $result = $wpdb->delete(
168 $this->table_name,
169 array('id' => $backup_id),
170 array('%d')
171 );
172
173 return false !== $result;
174 }
175
176 /**
177 * Cleanup old backups
178 *
179 * @param int $keep_count Number of backups to keep
180 * @return bool True on success, false on failure
181 */
182 private function cleanup_old_backups($keep_count = 50)
183 {
184 global $wpdb;
185
186 // First, get the IDs to keep (avoids MySQL subquery deadlock)
187 $ids_to_keep = $wpdb->get_col(
188 $wpdb->prepare(
189 "SELECT id FROM {$this->table_name}
190 ORDER BY created_at DESC
191 LIMIT %d",
192 $keep_count
193 )
194 );
195
196 if (empty($ids_to_keep)) {
197 return true;
198 }
199
200 // Delete all records NOT in the keep list
201 $placeholders = implode(',', array_fill(0, count($ids_to_keep), '%d'));
202 $wpdb->query(
203 $wpdb->prepare(
204 "DELETE FROM {$this->table_name}
205 WHERE id NOT IN ($placeholders)",
206 $ids_to_keep
207 )
208 );
209
210 return true;
211 }
212
213 /**
214 * Drop table (for uninstall)
215 */
216 public function drop_table()
217 {
218 global $wpdb;
219 $wpdb->query("DROP TABLE IF EXISTS {$this->table_name}");
220 }
221
222 /**
223 * Store virtual robots.txt content (when physical file cannot be created)
224 *
225 * @param string $content Content to store
226 * @return bool True on success, false on failure
227 */
228 public function store_virtual_content($content)
229 {
230 return update_option('metasync_robots_txt_virtual', $content, false);
231 }
232
233 /**
234 * Get virtual robots.txt content
235 *
236 * @return string|false Content or false if not found
237 */
238 public function get_virtual_content()
239 {
240 return get_option('metasync_robots_txt_virtual', false);
241 }
242
243 /**
244 * Check if virtual mode is active
245 *
246 * @return bool True if virtual mode is active
247 */
248 public function is_virtual_mode()
249 {
250 return (bool) get_option('metasync_robots_txt_virtual_mode', false);
251 }
252
253 /**
254 * Set virtual mode flag
255 *
256 * @param bool $enabled Whether virtual mode is enabled
257 * @return bool True on success
258 */
259 public function set_virtual_mode($enabled)
260 {
261 return update_option('metasync_robots_txt_virtual_mode', $enabled, false);
262 }
263
264 /**
265 * Clear virtual content and mode
266 *
267 * @return bool True on success
268 */
269 public function clear_virtual_content()
270 {
271 delete_option('metasync_robots_txt_virtual');
272 delete_option('metasync_robots_txt_virtual_mode');
273 return true;
274 }
275 }
276