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