← All changes
|
vendor/hostinger/hostinger-wp-helper/src/Utils.php
+172
-156
3.0.67
→
3.0.78
View file →
| @@ -1,59 +1,63 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Hostinger\WpHelper; |
| 4 | 4 | |
| 5 | -class Utils { | |
| 5 | +class Utils | |
| 6 | +{ | |
| 7 | + private static string $apiTokenFile; | |
| 6 | 8 | |
| 7 | - private static string $apiTokenFile; | |
| 9 | + private const HPANEL_DOMAIN_URL = 'https://hpanel.hostinger.com/websites/'; | |
| 10 | + private const HOSTINGER_SITE = '.hostingersite.com'; | |
| 8 | 11 | |
| 9 | - private const HPANEL_DOMAIN_URL = 'https://hpanel.hostinger.com/websites/'; | |
| 10 | - private const HOSTINGER_SITE = '.hostingersite.com'; | |
| 12 | + private static function getApiTokenPath(): void | |
| 13 | + { | |
| 14 | + $hostingerDirParts = explode('/', __DIR__); | |
| 15 | + if (count($hostingerDirParts) >= 3) { | |
| 16 | + $hostingerServerRootPath = '/' . $hostingerDirParts[1] . '/' . $hostingerDirParts[2]; | |
| 17 | + self::$apiTokenFile = $hostingerServerRootPath . '/.api_token'; | |
| 18 | + } | |
| 19 | + } | |
| 11 | 20 | |
| 12 | - private static function getApiTokenPath(): void { | |
| 13 | - $hostingerDirParts = explode( '/', __DIR__ ); | |
| 14 | - if ( count( $hostingerDirParts ) >= 3 ) { | |
| 15 | - $hostingerServerRootPath = '/' . $hostingerDirParts[1] . '/' . $hostingerDirParts[2]; | |
| 16 | - self::$apiTokenFile = $hostingerServerRootPath . '/.api_token'; | |
| 17 | - } | |
| 18 | - } | |
| 21 | + private static function get_override_site_url(): string | |
| 22 | + { | |
| 23 | + if (defined(Constants::SITE_URL_OVERRIDE_CONST)) { | |
| 24 | + $value = constant(Constants::SITE_URL_OVERRIDE_CONST); | |
| 25 | + if (is_string($value)) { | |
| 26 | + return $value; | |
| 27 | + } | |
| 28 | + } | |
| 19 | 29 | |
| 20 | - private static function get_override_site_url(): string { | |
| 21 | - if ( defined( Constants::SITE_URL_OVERRIDE_CONST ) ) { | |
| 22 | - $value = constant( Constants::SITE_URL_OVERRIDE_CONST ); | |
| 23 | - if ( is_string( $value ) ) { | |
| 24 | - return $value; | |
| 25 | - } | |
| 26 | - } | |
| 30 | + return ''; | |
| 31 | + } | |
| 27 | 32 | |
| 28 | - return ''; | |
| 29 | - } | |
| 33 | + private static function get_override_api_token(): string | |
| 34 | + { | |
| 35 | + if (defined(Constants::API_TOKEN_OVERRIDE_CONST)) { | |
| 36 | + $value = constant(Constants::API_TOKEN_OVERRIDE_CONST); | |
| 37 | + if (is_string($value)) { | |
| 38 | + return $value; | |
| 39 | + } | |
| 40 | + } | |
| 30 | 41 | |
| 31 | - private static function get_override_api_token(): string { | |
| 32 | - if ( defined( Constants::API_TOKEN_OVERRIDE_CONST ) ) { | |
| 33 | - $value = constant( Constants::API_TOKEN_OVERRIDE_CONST ); | |
| 34 | - if ( is_string( $value ) ) { | |
| 35 | - return $value; | |
| 36 | - } | |
| 37 | - } | |
| 42 | + return ''; | |
| 43 | + } | |
| 38 | 44 | |
| 39 | - return ''; | |
| 40 | - } | |
| 41 | - | |
| 42 | 45 | /** |
| 43 | 46 | * @param string $pluginSlug |
| 44 | 47 | * |
| 45 | 48 | * @return bool |
| 46 | 49 | */ |
| 47 | - // Check if a specific plugin is active by its slug | |
| 48 | - public static function isPluginActive( string $pluginSlug ): bool { | |
| 50 | + // Check if a specific plugin is active by its slug | |
| 51 | + public static function isPluginActive(string $pluginSlug): bool | |
| 52 | + { | |
| 49 | 53 | $plugin_relative_path = $pluginSlug . '/' . $pluginSlug . '.php'; |
| 50 | 54 | |
| 51 | - if ( is_multisite() ) { | |
| 52 | - return self::checkIsPluginActiveMultiSite( $plugin_relative_path ); | |
| 55 | + if (is_multisite()) { | |
| 56 | + return self::checkIsPluginActiveMultiSite($plugin_relative_path); | |
| 53 | 57 | } |
| 54 | 58 | |
| 55 | - return self::checkIsPluginActive( $plugin_relative_path ); | |
| 59 | + return self::checkIsPluginActive($plugin_relative_path); | |
| 56 | 60 | } |
| 57 | 61 | |
| 58 | 62 | /** |
| 59 | 63 | * @param string $plugin_relative_path |
| @@ -59,21 +63,22 @@ | ||
| 59 | 63 | * @param string $plugin_relative_path |
| 60 | 64 | * |
| 61 | 65 | * @return bool |
| 62 | 66 | */ |
| 63 | - public static function checkIsPluginActiveMultiSite( string $plugin_relative_path ): bool { | |
| 67 | + public static function checkIsPluginActiveMultiSite(string $plugin_relative_path): bool | |
| 68 | + { | |
| 64 | 69 | // Check network-wide active plugins |
| 65 | - $activePlugins = get_site_option( 'active_sitewide_plugins', [] ); | |
| 66 | - if ( in_array( $plugin_relative_path, $activePlugins ) ) { | |
| 70 | + $activePlugins = get_site_option('active_sitewide_plugins', []); | |
| 71 | + if (in_array($plugin_relative_path, $activePlugins)) { | |
| 67 | 72 | return true; |
| 68 | 73 | } |
| 69 | 74 | |
| 70 | 75 | // Check each site in the network |
| 71 | 76 | $sites = get_sites(); |
| 72 | - foreach ( $sites as $site ) { | |
| 73 | - switch_to_blog( $site->blog_id ); | |
| 74 | - $activePlugins = get_option( 'active_plugins', [] ); | |
| 75 | - if ( in_array( $plugin_relative_path, $activePlugins ) ) { | |
| 77 | + foreach ($sites as $site) { | |
| 78 | + switch_to_blog($site->blog_id); | |
| 79 | + $activePlugins = get_option('active_plugins', []); | |
| 80 | + if (in_array($plugin_relative_path, $activePlugins)) { | |
| 76 | 81 | restore_current_blog(); |
| 77 | 82 | |
| 78 | 83 | return true; |
| 79 | 84 | } |
| @@ -87,13 +92,14 @@ | ||
| 87 | 92 | * @param string $plugin_relative_path |
| 88 | 93 | * |
| 89 | 94 | * @return bool |
| 90 | 95 | */ |
| 91 | - public static function checkIsPluginActive( string $plugin_relative_path ): bool { | |
| 96 | + public static function checkIsPluginActive(string $plugin_relative_path): bool | |
| 97 | + { | |
| 92 | 98 | // Check active plugins in a single site |
| 93 | - $activePlugins = get_option( 'active_plugins', [] ); | |
| 99 | + $activePlugins = get_option('active_plugins', []); | |
| 94 | 100 | |
| 95 | - if ( in_array( $plugin_relative_path, $activePlugins ) ) { | |
| 101 | + if (in_array($plugin_relative_path, $activePlugins)) { | |
| 96 | 102 | return true; |
| 97 | 103 | } |
| 98 | 104 | |
| 99 | 105 | return false; |
| @@ -98,59 +104,63 @@ | ||
| 98 | 104 | |
| 99 | 105 | return false; |
| 100 | 106 | } |
| 101 | 107 | |
| 102 | - // Get the content of the API token file | |
| 103 | - public static function getApiToken(): string { | |
| 104 | - $override = self::get_override_api_token(); | |
| 105 | - if ( ! empty( $override ) ) { | |
| 106 | - return $override; | |
| 107 | - } | |
| 108 | + // Get the content of the API token file | |
| 109 | + public static function getApiToken(): string | |
| 110 | + { | |
| 111 | + $override = self::get_override_api_token(); | |
| 112 | + if (! empty($override)) { | |
| 113 | + return $override; | |
| 114 | + } | |
| 108 | 115 | |
| 109 | - self::getApiTokenPath(); | |
| 116 | + self::getApiTokenPath(); | |
| 110 | 117 | |
| 111 | - if ( file_exists( self::$apiTokenFile ) ) { | |
| 112 | - $apiToken = file_get_contents( self::$apiTokenFile ); | |
| 113 | - if ( ! empty( $apiToken ) ) { | |
| 114 | - return $apiToken; | |
| 115 | - } | |
| 116 | - } | |
| 118 | + if (file_exists(self::$apiTokenFile)) { | |
| 119 | + $apiToken = file_get_contents(self::$apiTokenFile); | |
| 120 | + if (! empty($apiToken)) { | |
| 121 | + return $apiToken; | |
| 122 | + } | |
| 123 | + } | |
| 117 | 124 | |
| 118 | - return ''; | |
| 119 | - } | |
| 125 | + return ''; | |
| 126 | + } | |
| 120 | 127 | |
| 121 | - // Get the host info (domain, subdomain, subdirectory) | |
| 122 | - public function getHostInfo(): string { | |
| 123 | - $host = $_SERVER['HTTP_HOST'] ?? ''; | |
| 124 | - $override = self::get_override_site_url(); | |
| 125 | - $site_url = ! empty( $override ) ? $override : get_site_url(); | |
| 126 | - $site_url = preg_replace( '#^https?://#', '', $site_url ); | |
| 127 | - $site_url = preg_replace( '/^www\./', '', $site_url ); | |
| 128 | + // Get the host info (domain, subdomain, subdirectory) | |
| 129 | + public function getHostInfo(): string | |
| 130 | + { | |
| 131 | + $host = $_SERVER['HTTP_HOST'] ?? ''; | |
| 132 | + $override = self::get_override_site_url(); | |
| 133 | + $site_url = ! empty($override) ? $override : get_site_url(); | |
| 134 | + $site_url = preg_replace('#^https?://#', '', $site_url); | |
| 135 | + $site_url = preg_replace('/^www\./', '', $site_url); | |
| 128 | 136 | |
| 129 | - if ( ! empty( $site_url ) && ! empty( $host ) && strpos( $site_url, $host ) === 0 ) { | |
| 130 | - if ( $site_url === $host ) { | |
| 131 | - return $host; | |
| 132 | - } else { | |
| 133 | - return substr( $site_url, strlen( $host ) + 1 ); | |
| 134 | - } | |
| 135 | - } | |
| 137 | + if (! empty($site_url) && ! empty($host) && strpos($site_url, $host) === 0) { | |
| 138 | + if ($site_url === $host) { | |
| 139 | + return $host; | |
| 140 | + } else { | |
| 141 | + return substr($site_url, strlen($host) + 1); | |
| 142 | + } | |
| 143 | + } | |
| 136 | 144 | |
| 137 | - return $host; | |
| 138 | - } | |
| 145 | + return $host; | |
| 146 | + } | |
| 139 | 147 | |
| 140 | - // Check if the current domain is a preview domain | |
| 141 | - public function isPreviewDomain(): bool { | |
| 142 | - if ( isset( $_SERVER['HTTP_X_PREVIEW_INDICATOR'] ) && filter_var( $_SERVER['HTTP_X_PREVIEW_INDICATOR'],FILTER_VALIDATE_BOOLEAN ) === true ) { | |
| 143 | - return true; | |
| 144 | - } | |
| 148 | + // Check if the current domain is a preview domain | |
| 149 | + public function isPreviewDomain(): bool | |
| 150 | + { | |
| 151 | + if (isset($_SERVER['HTTP_X_PREVIEW_INDICATOR']) && filter_var($_SERVER['HTTP_X_PREVIEW_INDICATOR'], FILTER_VALIDATE_BOOLEAN) === true) { | |
| 152 | + return true; | |
| 153 | + } | |
| 145 | 154 | |
| 146 | - return false; | |
| 147 | - } | |
| 155 | + return false; | |
| 156 | + } | |
| 148 | 157 | |
| 149 | - public function getSiteUrlFromDb() { | |
| 158 | + public function getSiteUrlFromDb() | |
| 159 | + { | |
| 150 | 160 | $override = self::get_override_site_url(); |
| 151 | - if ( ! empty( $override ) ) { | |
| 152 | - return preg_replace( '#^https?://#', '', $override ); | |
| 161 | + if (! empty($override)) { | |
| 162 | + return preg_replace('#^https?://#', '', $override); | |
| 153 | 163 | } |
| 154 | 164 | |
| 155 | 165 | global $wpdb; |
| 156 | 166 | |
| @@ -166,51 +176,53 @@ | ||
| 166 | 176 | |
| 167 | 177 | return $site_url; |
| 168 | 178 | } |
| 169 | 179 | |
| 170 | - // Check if the current page is the specified page | |
| 171 | - public function isThisPage( string $page ): bool { | |
| 180 | + // Check if the current page is the specified page | |
| 181 | + public function isThisPage(string $page): bool | |
| 182 | + { | |
| 172 | 183 | |
| 173 | - if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 174 | - return false; | |
| 175 | - } | |
| 184 | + if (! isset($_SERVER['REQUEST_URI'])) { | |
| 185 | + return false; | |
| 186 | + } | |
| 176 | 187 | |
| 177 | - $current_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); | |
| 188 | + $current_uri = sanitize_text_field($_SERVER['REQUEST_URI']); | |
| 178 | 189 | |
| 179 | - if ( defined( 'DOING_AJAX' ) && \DOING_AJAX ) { | |
| 180 | - return false; | |
| 181 | - } | |
| 190 | + if (defined('DOING_AJAX') && \DOING_AJAX) { | |
| 191 | + return false; | |
| 192 | + } | |
| 182 | 193 | |
| 183 | - if ( isset( $current_uri ) && strpos( $current_uri, '/wp-json/' ) !== false ) { | |
| 184 | - return false; | |
| 185 | - } | |
| 194 | + if (isset($current_uri) && strpos($current_uri, '/wp-json/') !== false) { | |
| 195 | + return false; | |
| 196 | + } | |
| 186 | 197 | |
| 187 | - if ( strpos( $current_uri, $page ) !== false ) { | |
| 188 | - return true; | |
| 189 | - } | |
| 198 | + if (strpos($current_uri, $page) !== false) { | |
| 199 | + return true; | |
| 200 | + } | |
| 190 | 201 | |
| 191 | - return false; | |
| 192 | - } | |
| 202 | + return false; | |
| 203 | + } | |
| 193 | 204 | |
| 194 | - // Get hPanel domain URL | |
| 195 | - public function getHpanelDomainUrl() : string { | |
| 205 | + // Get Hostinger dashboard domain URL | |
| 206 | + public function getHpanelDomainUrl(): string | |
| 207 | + { | |
| 196 | 208 | $override = self::get_override_site_url(); |
| 197 | - $source_url = ! empty( $override ) ? $override : get_site_url(); | |
| 198 | - $parsed_url = parse_url( $source_url ); | |
| 209 | + $source_url = ! empty($override) ? $override : get_site_url(); | |
| 210 | + $parsed_url = parse_url($source_url); | |
| 199 | 211 | $host = $parsed_url['host']; |
| 200 | 212 | $directory = __DIR__; |
| 201 | 213 | |
| 202 | 214 | // Remove 'www.' if it exists in the host |
| 203 | - if ( strpos( $host, 'www.' ) === 0 ) { | |
| 204 | - $host = substr( $host, 4 ); | |
| 215 | + if (strpos($host, 'www.') === 0) { | |
| 216 | + $host = substr($host, 4); | |
| 205 | 217 | } |
| 206 | 218 | |
| 207 | 219 | // Parse the host into parts |
| 208 | - $host_parts = explode( '.', $host ); | |
| 209 | - $is_subdomain = count( $host_parts ) > 2; | |
| 220 | + $host_parts = explode('.', $host); | |
| 221 | + $is_subdomain = count($host_parts) > 2; | |
| 210 | 222 | |
| 211 | 223 | // Helper to get the base domain (last two parts) |
| 212 | - $base_domain = implode( '.', array_slice( $host_parts, -2 ) ); | |
| 224 | + $base_domain = implode('.', array_slice($host_parts, -2)); | |
| 213 | 225 | |
| 214 | 226 | // System folders to ignore |
| 215 | 227 | $system_folders = [ 'wp-content', 'plugins', 'themes', 'uploads' ]; |
| 216 | 228 | |
| @@ -215,22 +227,22 @@ | ||
| 215 | 227 | $system_folders = [ 'wp-content', 'plugins', 'themes', 'uploads' ]; |
| 216 | 228 | |
| 217 | 229 | // Detect if there is a subdirectory immediately after 'public_html' |
| 218 | 230 | $subdirectory_name = ''; |
| 219 | - if ( preg_match( '/\/public_html\/([^\/]+)\//', $directory, $matches ) && ! in_array( $matches[1], $system_folders ) ) { | |
| 231 | + if (preg_match('/\/public_html\/([^\/]+)\//', $directory, $matches) && ! in_array($matches[1], $system_folders)) { | |
| 220 | 232 | $subdirectory_name = $matches[1]; |
| 221 | 233 | } |
| 222 | 234 | |
| 223 | 235 | // Handle preview domains |
| 224 | - if ( $this->isPreviewDomain() ) { | |
| 225 | - $host_parts = explode( '.', $host ); | |
| 226 | - $base_domain = str_replace( '-', '.', $host_parts[0] ); | |
| 236 | + if ($this->isPreviewDomain()) { | |
| 237 | + $host_parts = explode('.', $host); | |
| 238 | + $base_domain = str_replace('-', '.', $host_parts[0]); | |
| 227 | 239 | |
| 228 | - return self::HPANEL_DOMAIN_URL . "$base_domain." . end( $host_parts ); | |
| 240 | + return self::HPANEL_DOMAIN_URL . "$base_domain." . end($host_parts); | |
| 229 | 241 | } |
| 230 | 242 | |
| 231 | 243 | // Handle subdomain with a directory structure |
| 232 | - if ( $subdirectory_name !== '' ) { | |
| 244 | + if ($subdirectory_name !== '') { | |
| 233 | 245 | $full_domain = "$subdirectory_name.$base_domain"; |
| 234 | 246 | |
| 235 | 247 | return self::HPANEL_DOMAIN_URL . "$base_domain/wordpress/dashboard/$full_domain"; |
| 236 | 248 | } |
| @@ -235,9 +247,9 @@ | ||
| 235 | 247 | return self::HPANEL_DOMAIN_URL . "$base_domain/wordpress/dashboard/$full_domain"; |
| 236 | 248 | } |
| 237 | 249 | |
| 238 | 250 | // Handle top-level subdomain (no subdirectory structure) |
| 239 | - if ( $is_subdomain ) { | |
| 251 | + if ($is_subdomain) { | |
| 240 | 252 | return self::HPANEL_DOMAIN_URL . "$host"; |
| 241 | 253 | } |
| 242 | 254 | |
| 243 | 255 | // Default to handling top-level domains (without subdomains) |
| @@ -243,54 +255,58 @@ | ||
| 243 | 255 | // Default to handling top-level domains (without subdomains) |
| 244 | 256 | return self::HPANEL_DOMAIN_URL . "$host"; |
| 245 | 257 | } |
| 246 | 258 | |
| 247 | - // Check transient eligibility | |
| 248 | - public function checkTransientEligibility( $transient_request_key, $cache_time = 3600 ): bool { | |
| 249 | - try { | |
| 250 | - // Set transient | |
| 251 | - set_transient( $transient_request_key, true, $cache_time ); | |
| 259 | + // Check transient eligibility | |
| 260 | + public function checkTransientEligibility($transient_request_key, $cache_time = 3600): bool | |
| 261 | + { | |
| 262 | + try { | |
| 263 | + // Set transient | |
| 264 | + set_transient($transient_request_key, true, $cache_time); | |
| 252 | 265 | |
| 253 | - // Check if transient was set successfully | |
| 254 | - if ( false === get_transient( $transient_request_key ) ) { | |
| 255 | - throw new \Exception( 'Unable to create transient in WordPress.' ); | |
| 256 | - } | |
| 266 | + // Check if transient was set successfully | |
| 267 | + if (false === get_transient($transient_request_key)) { | |
| 268 | + throw new \Exception('Unable to create transient in WordPress.'); | |
| 269 | + } | |
| 257 | 270 | |
| 258 | - // If everything is fine, return true | |
| 259 | - return true; | |
| 260 | - } catch ( \Exception $exception ) { | |
| 261 | - // If there's an exception, log the error and return false | |
| 262 | - $this->errorLog( 'Error checking eligibility: ' . $exception->getMessage() ); | |
| 271 | + // If everything is fine, return true | |
| 272 | + return true; | |
| 273 | + } catch (\Exception $exception) { | |
| 274 | + // If there's an exception, log the error and return false | |
| 275 | + $this->errorLog('Error checking eligibility: ' . $exception->getMessage()); | |
| 263 | 276 | |
| 264 | - return false; | |
| 265 | - } | |
| 266 | - } | |
| 277 | + return false; | |
| 278 | + } | |
| 279 | + } | |
| 267 | 280 | |
| 268 | - public function errorLog( string $message ): void { | |
| 269 | - if ( defined( 'WP_DEBUG' ) && \WP_DEBUG === true ) { | |
| 270 | - error_log( print_r( $message, true ) ); | |
| 271 | - } | |
| 272 | - } | |
| 281 | + public function errorLog(string $message): void | |
| 282 | + { | |
| 283 | + if (defined('WP_DEBUG') && \WP_DEBUG === true) { | |
| 284 | + error_log(print_r($message, true)); | |
| 285 | + } | |
| 286 | + } | |
| 273 | 287 | |
| 274 | - public static function getSetting( string $setting ): string { | |
| 288 | + public static function getSetting(string $setting): string | |
| 289 | + { | |
| 275 | 290 | |
| 276 | - if ( $setting ) { | |
| 277 | - return get_option( 'hostinger_' . $setting, '' ); | |
| 278 | - } | |
| 291 | + if ($setting) { | |
| 292 | + return get_option('hostinger_' . $setting, ''); | |
| 293 | + } | |
| 279 | 294 | |
| 280 | - return ''; | |
| 281 | - } | |
| 295 | + return ''; | |
| 296 | + } | |
| 282 | 297 | |
| 283 | - public static function updateSetting( string $setting, $value, $autoload = null ): void { | |
| 298 | + public static function updateSetting(string $setting, $value, $autoload = null): void | |
| 299 | + { | |
| 284 | 300 | |
| 285 | - if ( $setting ) { | |
| 286 | - update_option( 'hostinger_' . $setting, $value, $autoload ); | |
| 287 | - } | |
| 288 | - } | |
| 301 | + if ($setting) { | |
| 302 | + update_option('hostinger_' . $setting, $value, $autoload); | |
| 303 | + } | |
| 304 | + } | |
| 289 | 305 | |
| 290 | - public static function flushLitespeedCache(): void { | |
| 291 | - if ( has_action( 'litespeed_purge_all' ) ) { | |
| 292 | - do_action( 'litespeed_purge_all' ); | |
| 306 | + public static function flushLitespeedCache(): void | |
| 307 | + { | |
| 308 | + if (has_action('litespeed_purge_all')) { | |
| 309 | + do_action('litespeed_purge_all'); | |
| 293 | 310 | } |
| 294 | 311 | } |
| 295 | - | |
| 296 | 312 | } |