PluginProbe
Hostinger Tools / 2.1.5
Hostinger Tools v2.1.5
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 / Helper.php

Helper.php in Hostinger Tools 2.1.5, at includes/Helper.php

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