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