PluginProbe
Hostinger Tools / 2.1.0
Hostinger Tools v2.1.0
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / includes / class-hostinger-helper.php

class-hostinger-helper.php in Hostinger Tools 2.1.0, at includes/class-hostinger-helper.php

266 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Hostinger_Helper {
4 public const HOSTINGER_FREE_SUBDOMAIN_URL = 'hostingersite.com';
5 public const HOSTINGER_PAGE = '/wp-admin/admin.php?page=hostinger';
6 public const CLIENT_WOO_COMPLETED_ACTIONS = 'woocommerce_task_list_tracked_completed_tasks';
7 private const PROMOTIONAL_LINKS = array(
8 'fr_FR' => 'https://www.hostinger.fr/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
9 'es_ES' => 'https://www.hostinger.es/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
10 'ar' => 'https://www.hostinger.ae/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
11 'zh_CN' => 'https://www.hostinger.com.hk/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
12 'id_ID' => 'https://www.hostinger.co.id/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
13 'lt_LT' => 'https://www.hostinger.lt/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
14 'pt_PT' => 'https://www.hostinger.pt/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
15 'uk' => 'https://www.hostinger.com.ua/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
16 'tr_TR' => 'https://www.hostinger.com.tr/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
17 'en_US' => 'https://www.hostinger.com/cpanel-login?r=%2Fjump-to%2Fnew-panel%2Fsection%2Freferrals&utm_source=Banner&utm_medium=HostingerWPplugin',
18 );
19
20 private const HPANEL_DOMAIN_URL = 'https://hpanel.hostinger.com/websites/';
21
22 /**
23 *
24 * Check if plugin is active
25 *
26 * @since 1.0.0
27 * @access public
28 */
29 public static function is_plugin_active( $plugin_slug ): bool {
30 $active_plugins = (array) get_option( 'active_plugins', array() );
31 foreach ( $active_plugins as $active_plugin ) {
32 if ( strpos( $active_plugin, $plugin_slug . '.php' ) !== false ) {
33 return true;
34 }
35 }
36
37 return false;
38 }
39
40 public static function get_api_token(): string {
41 $api_token = '';
42 $token_file = HOSTINGER_WP_TOKEN;
43
44 if ( file_exists( $token_file ) && ! empty( file_get_contents( $token_file ) ) ) {
45 $api_token = file_get_contents( $token_file );
46 }
47
48 return $api_token;
49 }
50
51 /**
52 *
53 * Get the host info (domain, subdomain, subdirectory)
54 *
55 * @since 1.7.0
56 * @access public
57 */
58 public function get_host_info(): string {
59 $host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( $_SERVER['HTTP_HOST'] ) : '';
60 $site_url = get_site_url();
61 $site_url = preg_replace( '#^https?://#', '', $site_url );
62
63 if ( ! empty( $site_url ) && ! empty( $host ) && strpos( $site_url, $host ) === 0 ) {
64 if ( $site_url === $host ) {
65 return $host;
66 } else {
67 return substr( $site_url, strlen( $host ) + 1 );
68 }
69 }
70
71 return $host;
72 }
73
74 public function is_preview_domain(): bool {
75 if ( function_exists( 'getallheaders' ) ) {
76 $headers = getallheaders();
77 }
78
79 if ( isset( $headers['X-Preview-Indicator'] ) && $headers['X-Preview-Indicator'] ) {
80 return true;
81 }
82
83 return false;
84 }
85
86 public function is_free_subdomain(): bool {
87 $site_url = preg_replace( '#^https?://#', '', get_site_url() );
88
89 return ! empty( $site_url ) && strpos( $site_url, self::HOSTINGER_FREE_SUBDOMAIN_URL ) !== false;
90 }
91
92 public function is_hostinger_admin_page(): bool {
93
94 if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
95 return false;
96 }
97
98 $current_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] );
99
100 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
101 return false;
102 }
103
104 if ( isset( $current_uri ) && strpos( $current_uri, '/wp-json/' ) !== false ) {
105 return false;
106 }
107
108 if ( strpos( $current_uri, self::HOSTINGER_PAGE ) !== false ) {
109 return true;
110 }
111
112 return false;
113 }
114
115 /**
116 *
117 * Error log
118 *
119 * @since 1.9.6
120 * @access public
121 */
122 public function error_log( string $message ): void {
123 if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
124 error_log( print_r( $message, true ) );
125 }
126 }
127
128 public function default_woocommerce_survey_steps_completed( array $steps ): bool {
129 $completed_actions = get_option( self::CLIENT_WOO_COMPLETED_ACTIONS, array() );
130
131 return empty( array_diff( $steps, $completed_actions ) );
132 }
133
134 public function is_this_page( string $page ): bool {
135
136 if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
137 return false;
138 }
139
140 $current_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] );
141
142 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
143 return false;
144 }
145
146 if ( isset( $current_uri ) && strpos( $current_uri, '/wp-json/' ) !== false ) {
147 return false;
148 }
149
150 if ( strpos( $current_uri, $page ) !== false ) {
151 return true;
152 }
153
154 return false;
155 }
156
157 public function get_promotional_link_url( string $locale ): string {
158 if ( isset( self::PROMOTIONAL_LINKS[ $locale ] ) ) {
159 return self::PROMOTIONAL_LINKS[ $locale ];
160 }
161
162 return self::PROMOTIONAL_LINKS['en_US'];
163 }
164
165 public function get_domain_from_url(): string {
166 $host = parse_url( get_site_url(), PHP_URL_HOST );
167
168 return $host === false ? self::HPANEL_DOMAIN_URL : $host;
169 }
170
171 public function get_hpanel_domain_url(): string {
172 $domain_url = $this->get_domain_from_url();
173
174 if ( $domain_url === self::HPANEL_DOMAIN_URL ) {
175 return $domain_url;
176 }
177
178 return self::HPANEL_DOMAIN_URL . $domain_url;
179 }
180
181 public function check_transient_eligibility( $transient_request_key, $cache_time = 3600 ): bool {
182 try {
183 // Set transient
184 set_transient( $transient_request_key, true, $cache_time );
185
186 // Check if transient was set successfully
187 if ( false === get_transient( $transient_request_key ) ) {
188 throw new Exception( 'Unable to create transient in WordPress.' );
189 }
190
191 // If everything is fine, return true
192 return true;
193 } catch ( Exception $exception ) {
194 // If there's an exception, log the error and return false
195 $this->error_log( 'Error checking eligibility: ' . $exception->getMessage() );
196
197 return false;
198 }
199 }
200
201 public static function woocommerce_onboarding_choice(): bool {
202 return (bool) get_option( 'hostinger_woo_onboarding_choice', false );
203 }
204
205 /**
206 * @return bool
207 */
208 public static function is_woocommerce_site(): bool {
209 return class_exists( 'WooCommerce' );
210 }
211
212 /**
213 * @return bool
214 */
215 public static function show_woocommerce_onboarding(): bool {
216 $woo_onboarding_enabled = get_option( 'hostinger_woo_onboarding_enabled', false );
217 $woo_setup_wizard_completed = get_option( 'woocommerce_onboarding_profile', false );
218
219 return ( self::is_woocommerce_site() && ! self::woocommerce_onboarding_choice() && $woo_onboarding_enabled && ! $woo_setup_wizard_completed );
220 }
221
222 /**
223 * @return bool
224 */
225 public function can_show_store_ready_message(): bool {
226 if ( ! self::is_woocommerce_site() || ! self::woocommerce_onboarding_choice() ) {
227 return false;
228 }
229
230 $store_ready_message_shown = get_option( 'hostinger_woo_ready_message_shown', null );
231
232 if ( $store_ready_message_shown === null ) {
233 return false;
234 }
235
236 if ( (int) $store_ready_message_shown !== 0 ) {
237 return false;
238 }
239
240 if ( ! $this->default_woocommerce_survey_completed() ) {
241 return false;
242 }
243
244 return true;
245 }
246
247 public function default_woocommerce_survey_completed(): bool {
248 $completed_actions = get_option( self::CLIENT_WOO_COMPLETED_ACTIONS, array() );
249 $required_completed_actions = array( 'products', 'payments' );
250
251 return empty( array_diff( $required_completed_actions, $completed_actions ) );
252 }
253
254 public function register_session() {
255 if ( ! session_id() ) {
256 session_start();
257 }
258 }
259
260 public function destroy_session() {
261 session_destroy();
262 }
263 }
264
265 $hostinger_helper = new Hostinger_Helper();
266