PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.7.2
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.7.2
1.7.2 1.7.1 1.7.0 1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / vendor / hostinger / hostinger-wp-helper / src / Utils.php
hostinger-reach / vendor / hostinger / hostinger-wp-helper / src Last commit date
Requests 1 month ago Config.php 1 month ago Constants.php 1 month ago Utils.php 1 month ago
Utils.php
313 lines
1 <?php
2
3 namespace Hostinger\WpHelper;
4
5 class Utils
6 {
7 private static string $apiTokenFile;
8
9 private const HPANEL_DOMAIN_URL = 'https://hpanel.hostinger.com/websites/';
10 private const HOSTINGER_SITE = '.hostingersite.com';
11
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 }
20
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 }
29
30 return '';
31 }
32
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 }
41
42 return '';
43 }
44
45 /**
46 * @param string $pluginSlug
47 *
48 * @return bool
49 */
50 // Check if a specific plugin is active by its slug
51 public static function isPluginActive(string $pluginSlug): bool
52 {
53 $plugin_relative_path = $pluginSlug . '/' . $pluginSlug . '.php';
54
55 if (is_multisite()) {
56 return self::checkIsPluginActiveMultiSite($plugin_relative_path);
57 }
58
59 return self::checkIsPluginActive($plugin_relative_path);
60 }
61
62 /**
63 * @param string $plugin_relative_path
64 *
65 * @return bool
66 */
67 public static function checkIsPluginActiveMultiSite(string $plugin_relative_path): bool
68 {
69 // Check network-wide active plugins
70 $activePlugins = get_site_option('active_sitewide_plugins', []);
71 if (in_array($plugin_relative_path, $activePlugins)) {
72 return true;
73 }
74
75 // Check each site in the network
76 $sites = get_sites();
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)) {
81 restore_current_blog();
82
83 return true;
84 }
85 restore_current_blog();
86 }
87
88 return false;
89 }
90
91 /**
92 * @param string $plugin_relative_path
93 *
94 * @return bool
95 */
96 public static function checkIsPluginActive(string $plugin_relative_path): bool
97 {
98 // Check active plugins in a single site
99 $activePlugins = get_option('active_plugins', []);
100
101 if (in_array($plugin_relative_path, $activePlugins)) {
102 return true;
103 }
104
105 return false;
106 }
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 }
115
116 self::getApiTokenPath();
117
118 if (file_exists(self::$apiTokenFile)) {
119 $apiToken = file_get_contents(self::$apiTokenFile);
120 if (! empty($apiToken)) {
121 return $apiToken;
122 }
123 }
124
125 return '';
126 }
127
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);
136
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 }
144
145 return $host;
146 }
147
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 }
154
155 return false;
156 }
157
158 public function getSiteUrlFromDb()
159 {
160 $override = self::get_override_site_url();
161 if (! empty($override)) {
162 return preg_replace('#^https?://#', '', $override);
163 }
164
165 global $wpdb;
166
167 $site_url = $wpdb->get_var(
168 $wpdb->prepare(
169 "SELECT REPLACE(REPLACE(option_value, 'https://', ''), 'http://', '')
170 FROM {$wpdb->options}
171 WHERE option_name = %s
172 LIMIT 1",
173 'siteurl'
174 )
175 );
176
177 return $site_url;
178 }
179
180 // Check if the current page is the specified page
181 public function isThisPage(string $page): bool
182 {
183
184 if (! isset($_SERVER['REQUEST_URI'])) {
185 return false;
186 }
187
188 $current_uri = sanitize_text_field($_SERVER['REQUEST_URI']);
189
190 if (defined('DOING_AJAX') && \DOING_AJAX) {
191 return false;
192 }
193
194 if (isset($current_uri) && strpos($current_uri, '/wp-json/') !== false) {
195 return false;
196 }
197
198 if (strpos($current_uri, $page) !== false) {
199 return true;
200 }
201
202 return false;
203 }
204
205 // Get hPanel domain URL
206 public function getHpanelDomainUrl(): string
207 {
208 $override = self::get_override_site_url();
209 $source_url = ! empty($override) ? $override : get_site_url();
210 $parsed_url = parse_url($source_url);
211 $host = $parsed_url['host'];
212 $directory = __DIR__;
213
214 // Remove 'www.' if it exists in the host
215 if (strpos($host, 'www.') === 0) {
216 $host = substr($host, 4);
217 }
218
219 // Parse the host into parts
220 $host_parts = explode('.', $host);
221 $is_subdomain = count($host_parts) > 2;
222
223 // Helper to get the base domain (last two parts)
224 $base_domain = implode('.', array_slice($host_parts, -2));
225
226 // System folders to ignore
227 $system_folders = [ 'wp-content', 'plugins', 'themes', 'uploads' ];
228
229 // Detect if there is a subdirectory immediately after 'public_html'
230 $subdirectory_name = '';
231 if (preg_match('/\/public_html\/([^\/]+)\//', $directory, $matches) && ! in_array($matches[1], $system_folders)) {
232 $subdirectory_name = $matches[1];
233 }
234
235 // Handle preview domains
236 if ($this->isPreviewDomain()) {
237 $host_parts = explode('.', $host);
238 $base_domain = str_replace('-', '.', $host_parts[0]);
239
240 return self::HPANEL_DOMAIN_URL . "$base_domain." . end($host_parts);
241 }
242
243 // Handle subdomain with a directory structure
244 if ($subdirectory_name !== '') {
245 $full_domain = "$subdirectory_name.$base_domain";
246
247 return self::HPANEL_DOMAIN_URL . "$base_domain/wordpress/dashboard/$full_domain";
248 }
249
250 // Handle top-level subdomain (no subdirectory structure)
251 if ($is_subdomain) {
252 return self::HPANEL_DOMAIN_URL . "$host";
253 }
254
255 // Default to handling top-level domains (without subdomains)
256 return self::HPANEL_DOMAIN_URL . "$host";
257 }
258
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);
265
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 }
270
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());
276
277 return false;
278 }
279 }
280
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 }
287
288 public static function getSetting(string $setting): string
289 {
290
291 if ($setting) {
292 return get_option('hostinger_' . $setting, '');
293 }
294
295 return '';
296 }
297
298 public static function updateSetting(string $setting, $value, $autoload = null): void
299 {
300
301 if ($setting) {
302 update_option('hostinger_' . $setting, $value, $autoload);
303 }
304 }
305
306 public static function flushLitespeedCache(): void
307 {
308 if (has_action('litespeed_purge_all')) {
309 do_action('litespeed_purge_all');
310 }
311 }
312 }
313