PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.11.1
JetBackup – Backup, Restore & Migrate v3.1.11.1
3.1.23.6 3.1.23.5 3.1.23.3 3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / Wordpress / Wordpress.php
backup / src / JetBackup / Wordpress Last commit date
.htaccess 1 year ago Blog.php 1 year ago Helper.php 1 year ago Init.php 1 year ago Installer.php 1 year ago MySQL.php 1 year ago UI.php 1 year ago Update.php 1 year ago Wordpress.php 1 year ago index.html 1 year ago web.config 1 year ago
Wordpress.php
354 lines
1 <?php
2
3 namespace JetBackup\Wordpress;
4
5 use JetBackup\Entities\Util;
6 use JetBackup\Exception\DBException;
7 use JetBackup\Factory;
8 use JetBackup\JetBackup;
9
10 if (!defined( '__JETBACKUP__')) die('Direct access is not allowed');
11
12 class Wordpress {
13
14 private function __construct() {}
15
16 const WP_CONTENT = 'wp-content';
17 const WP_PLUGINS = 'plugins';
18
19 public static function getLocale(): string {
20 return self::getLangCookieValue() ?? JetBackup::DEFAULT_LANGUAGE;
21 }
22
23 public static function getAuthSalt(): string {
24 return defined('AUTH_SALT') ? AUTH_SALT : Factory::getConfig()->getEncryptionKey();
25 }
26
27 public static function getAuthKey(): string {
28 return defined('AUTH_KEY') ? AUTH_KEY : Factory::getConfig()->getEncryptionKey();
29 }
30
31 public static function strContains(string $haystack, string $needle): bool {
32 if (function_exists('str_contains')) return str_contains($haystack, $needle);
33 return ( '' === $needle || false !== strpos( $haystack, $needle ) );
34 }
35
36 public static function getAlternateContentDir(): ?string {
37 return defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : null;
38 }
39
40 public static function getRemoteGet(string $url, array $args=[]) {
41 return wp_remote_get($url, $args);
42 }
43
44 public static function getCurrentScreen():?\WP_Screen {
45 if (function_exists('get_current_screen')) return get_current_screen();
46 return null;
47 }
48
49 public static function getBlogInfo($show):string {
50 return get_bloginfo($show);
51 }
52
53 public static function getUnslash($value) {
54 if (function_exists('wp_unslash')) return wp_unslash($value);
55 if (is_array($value)) return array_map([__CLASS__, 'getUnslash'], $value);
56 return is_string($value) ? stripslashes($value) : $value;
57 }
58
59 public static function getDateFormat() : string {
60 return Wordpress::getOption('date_format') ?: 'd/m/Y';
61 }
62
63 public static function getTimeFormat() : string {
64 return Wordpress::getOption('time_format') ?: 'H:i';
65 }
66
67 public static function getOption($option) {
68 if (function_exists('get_option')) return get_option($option);
69 return null;
70 }
71
72 public static function getPlugins(): array {
73
74 if (!function_exists('get_plugins')) {
75 $plugin_loader = Factory::getWPHelper()->getWordPressHomedir() . 'wp-admin' . JetBackup::SEP . 'includes' . JetBackup::SEP . 'plugin.php';
76 if (file_exists($plugin_loader)) require_once($plugin_loader);
77 }
78
79 return get_plugins();
80 }
81
82 public static function isPluginActive($plugin):bool {
83 return is_plugin_active($plugin);
84 }
85
86 public static function getTransient($transient) {
87 return get_transient($transient);
88 }
89
90 public static function setTransient($transient, $value, $expiration=0) {
91 set_transient($transient, $value, $expiration);
92 }
93
94 public static function isSuperAdmin($userid=false): bool {
95 return function_exists('is_super_admin') && is_super_admin($userid);
96 }
97
98 public static function getCurrentUser():?\WP_User {
99 if (function_exists('wp_get_current_user')) return wp_get_current_user();
100 return null;
101 }
102
103 public static function getUploadDir(): ?array {
104 if (function_exists('wp_get_upload_dir')) return wp_get_upload_dir();
105 return null;
106 }
107
108 public static function copyDir($source, $target) {
109 if (function_exists('copy_dir')) return copy_dir($source, $target);
110 return false;
111 }
112
113 public static function verifyNonce($nonce): bool {
114 if (!function_exists('wp_verify_nonce')) {
115 return false;
116 }
117 return wp_verify_nonce($nonce, 'jetbackup_nonce_' . (self::getNonceCookieValue() ?? ''));
118 }
119
120 public static function createNonce(): string {
121 if (!function_exists('wp_create_nonce')) {
122 return '';
123 }
124 return wp_create_nonce('jetbackup_nonce_' . (self::getNonceCookieValue() ?? ''));
125 }
126
127
128 public static function updateUserMeta ($user_id, $meta_key, $meta_value, $prev_value = '') {
129 if (function_exists('update_user_meta')) return update_user_meta($user_id, $meta_key, $meta_value, $prev_value);
130 return false;
131 }
132
133 public static function deleteUserMeta ($user_id, $meta_key): bool {
134 if (function_exists('delete_user_meta')) return delete_user_meta($user_id, $meta_key);
135 return false;
136 }
137
138 public static function getUserMeta ($user_id, $key, $single) {
139 if (function_exists('get_user_meta')) return get_user_meta($user_id, $key, $single);
140 return '';
141
142 }
143
144 public static function isDebugModeEnabled(): bool {
145 return defined('WP_DEBUG') && WP_DEBUG;
146 }
147
148
149 public static function wpLogout() : void {wp_logout();}
150 public static function wpRedirect($location) : void {wp_redirect($location);}
151 public static function wpLoginURL($redirect = '', $force_reauth = false) : string {
152 return wp_login_url($redirect, $force_reauth);
153 }
154
155 public static function text($text): string {
156 return esc_html__($text,'jetbackup-plugin');
157 }
158
159 private static function getNonceCookieValue(): ?string {
160 return isset($_COOKIE[JetBackup::NONCE_COOKIE_NAME])
161 ? self::getUnslash($_COOKIE[JetBackup::NONCE_COOKIE_NAME])
162 : null;
163 }
164
165 public static function setNonceCookie() {
166 if (!Helper::isCLI() && !self::getNonceCookieValue()) {
167 setcookie(
168 JetBackup::NONCE_COOKIE_NAME,
169 Util::generateRandomString(),
170 time() + 7200,
171 COOKIEPATH,
172 COOKIE_DOMAIN,
173 WordPress::isSSL(),
174 true
175 );
176 }
177 }
178
179
180 private static function getLangCookieValue(): ?string {
181 return isset($_COOKIE[JetBackup::LANG_COOKIE_NAME])
182 ? self::getUnslash($_COOKIE[JetBackup::LANG_COOKIE_NAME])
183 : null;
184 }
185
186 public static function setUserLanguageCookie() {
187 if (
188 !Helper::isCLI() &&
189 (
190 !self::getLangCookieValue() ||
191 self::getLocale() !== self::getUserLocale()
192 )
193 ) {
194 setcookie(
195 JetBackup::LANG_COOKIE_NAME,
196 self::getUserLocale(),
197 time() + 7200,
198 COOKIEPATH,
199 COOKIE_DOMAIN,
200 WordPress::isSSL(),
201 true
202 );
203 }
204 }
205
206
207 public static function getUserLocale(): string {
208 if (function_exists('get_user_locale')) return get_user_locale();
209 return JetBackup::DEFAULT_LANGUAGE;
210 }
211
212 public static function sendMail($to, $subject, $message, $headers = [], $attachments = '') {
213 return wp_mail($to, $subject, $message, $headers, $attachments);
214 }
215
216 public static function sanitizeEmail($email): string {
217 return sanitize_email($email);
218 }
219
220 public static function isSSL(): bool {
221 if (function_exists('is_ssl')) return is_ssl();
222 return false;
223 }
224
225 public static function isEmail($email): bool {
226 if (function_exists('is_email')) return is_email($email);
227 return filter_var($email, FILTER_VALIDATE_EMAIL);
228 }
229
230 public static function sanitizeTextField($str): string {
231 if (function_exists('sanitize_text_field')) return sanitize_text_field($str);
232
233 // Fallback sanitization: basic cleanup
234 $str = is_string($str) ? $str : (string) $str;
235 $str = strip_tags($str);
236 $str = trim($str);
237 $str = preg_replace('/[\r\n\t]+/', ' ', $str);
238
239 return $str;
240 }
241
242
243 public static function getVersion (): ?string {
244 global $wp_version;
245 return $wp_version;
246 }
247
248 public static function setOption($key, $value) {
249 update_option($key, $value);
250 }
251
252 /**
253 * @return string
254 * Returns clean domain only (not http prefix)
255 * Example: mydomain.com
256 */
257 public static function getSiteDomain():string {
258 return preg_replace('#^http[s]?://#', '', self::getSiteURL());
259 }
260
261
262 /**
263 * @return string
264 * Returns full site url, including http prefix
265 * Example: https://www.mydomain.com
266 */
267 public static function getSiteURL(): string {
268 $site_url = function_exists('get_site_url') ? get_site_url() : null;
269 if ($site_url) return $site_url;
270
271 // Fallback
272 $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
273 $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
274 $host = Wordpress::sanitizeTextField($host);
275
276 $script_path = $_SERVER['SCRIPT_NAME'] ?? $_SERVER['PHP_SELF'] ?? $_SERVER['REQUEST_URI'] ?? '';
277 $script_path = Wordpress::sanitizeTextField($script_path);
278 $script_dir = dirname($script_path);
279
280 return rtrim("$protocol://$host$script_dir", '/');
281 }
282
283 public static function getAdminURL():string {
284
285 $admin_url = function_exists('get_admin_url') ? get_admin_url() : null;
286 if(!$admin_url) {
287 $admin_url = self::getSiteURL() . '/wp-admin';
288 }
289 return $admin_url;
290 }
291
292 public static function getDB():MySQL {
293 static $i;
294 if(!$i) $i = new MySQL();
295 return $i;
296 }
297
298 /**
299 * @return Blog[]
300 */
301 public static function getMultisiteBlogs():array {
302 $output = [];
303
304 $db = self::getDB();
305 $prefix = $db->getPrefix();
306 $blogsTable = "{$prefix}blogs";
307
308 // Check if the blogs table exists
309 try {
310 $sql = "SHOW TABLES LIKE '" . $db->escapeSql($blogsTable) . "'";
311 $result = $db->query($sql, [], ARRAY_N);
312 if (empty($result)) return $output; // Return empty if table doesn't exist
313 } catch (\Exception $e) {
314 return $output;
315 }
316
317 try {
318 $sql = "SELECT blog_id AS id, domain FROM {$blogsTable}";
319 $blogs = $db->query($sql, [], ARRAY_A);
320 } catch (\Exception $e) {
321 return $output;
322 }
323
324 foreach ($blogs as $blog_details) {
325 if (!$blog_details['id'] || !$blog_details['domain']) continue;
326
327 $blog = new Blog();
328 $blog->setId($blog_details['id']);
329 $blog->setDomain($blog_details['domain']);
330
331 try {
332 $sql = "SHOW TABLES LIKE '" . $db->escapeSql($prefix . (!$blog->isMain() ? $blog->getId() . '_' : '')) . "%'";
333 $tables = $db->query($sql, [], ARRAY_N);
334 } catch(\Exception $e) {
335 $tables = [];
336 }
337
338 foreach ($tables as $table) {
339 if (
340 !isset($table[0]) ||
341 // Filter out tables not specific to the main site
342 ($blog->isMain() && preg_match('/_\d+_/', $table[0]))
343 ) continue;
344
345 $blog->addDatabaseTable($table[0]);
346 }
347
348 $output[] = $blog;
349 }
350
351 return $output;
352 }
353 }
354