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