| @@ -1,9 +1,24 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if ( ! defined( 'ABSPATH' ) ) exit; | |
| 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 | + ); | |
| 4 | 19 | |
| 5 | -class Hostinger_Helper { | |
| 20 | + private const HPANEL_DOMAIN_URL = 'https://hpanel.hostinger.com/websites/'; | |
| 6 | 21 | |
| 7 | 22 | /** |
| 8 | 23 | * |
| 9 | 24 | * Check if plugin is active |
| @@ -22,20 +37,14 @@ | ||
| 22 | 37 | return false; |
| 23 | 38 | } |
| 24 | 39 | |
| 25 | 40 | public static function get_api_token(): string { |
| 26 | - require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; | |
| 27 | - require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; | |
| 28 | - | |
| 29 | 41 | $api_token = ''; |
| 30 | - $token_path = HOSTINGER_WP_TOKEN; | |
| 31 | - $filesystem = new WP_Filesystem_Direct( true ); | |
| 32 | - $token_file = $filesystem->get_contents( $token_path ); | |
| 42 | + $token_file = HOSTINGER_WP_TOKEN; | |
| 33 | 43 | |
| 34 | - if ( file_exists( $token_path ) && ! empty( $token_file ) ) { | |
| 35 | - $api_token = $token_file; | |
| 44 | + if ( file_exists( $token_file ) && ! empty( file_get_contents( $token_file ) ) ) { | |
| 45 | + $api_token = file_get_contents( $token_file ); | |
| 36 | 46 | } |
| 37 | - | |
| 38 | 47 | return $api_token; |
| 39 | 48 | } |
| 40 | 49 | |
| 41 | 50 | /** |
| @@ -44,11 +53,10 @@ | ||
| 44 | 53 | * |
| 45 | 54 | * @since 1.7.0 |
| 46 | 55 | * @access public |
| 47 | 56 | */ |
| 48 | - | |
| 49 | 57 | public function get_host_info(): string { |
| 50 | - $host = sanitize_text_field( $_SERVER['HTTP_HOST'] ?? '' ); | |
| 58 | + $host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( $_SERVER['HTTP_HOST'] ) : ''; | |
| 51 | 59 | $site_url = get_site_url(); |
| 52 | 60 | $site_url = preg_replace( '#^https?://#', '', $site_url ); |
| 53 | 61 | |
| 54 | 62 | if ( ! empty( $site_url ) && ! empty( $host ) && strpos( $site_url, $host ) === 0 ) { |
| @@ -61,8 +69,133 @@ | ||
| 61 | 69 | |
| 62 | 70 | return $host; |
| 63 | 71 | } |
| 64 | 72 | |
| 73 | + public function is_preview_domain(): bool { | |
| 74 | + if ( function_exists( 'getallheaders' ) ) { | |
| 75 | + $headers = getallheaders(); | |
| 76 | + } | |
| 65 | 77 | |
| 78 | + if ( isset( $headers['X-Preview-Indicator'] ) && $headers['X-Preview-Indicator'] ) { | |
| 79 | + return true; | |
| 80 | + } | |
| 81 | + | |
| 82 | + return false; | |
| 83 | + } | |
| 84 | + | |
| 85 | + public function is_free_subdomain(): bool { | |
| 86 | + $site_url = preg_replace( '#^https?://#', '', get_site_url() ); | |
| 87 | + | |
| 88 | + return ! empty( $site_url ) && strpos( $site_url, self::HOSTINGER_FREE_SUBDOMAIN_URL ) !== false; | |
| 89 | + } | |
| 90 | + | |
| 91 | + public function is_hostinger_admin_page(): bool { | |
| 92 | + | |
| 93 | + if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 94 | + return false; | |
| 95 | + } | |
| 96 | + | |
| 97 | + $current_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); | |
| 98 | + | |
| 99 | + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { | |
| 100 | + return false; | |
| 101 | + } | |
| 102 | + | |
| 103 | + if ( isset( $current_uri ) && strpos( $current_uri, '/wp-json/' ) !== false ) { | |
| 104 | + return false; | |
| 105 | + } | |
| 106 | + | |
| 107 | + if ( strpos( $current_uri, self::HOSTINGER_PAGE ) !== false ) { | |
| 108 | + return true; | |
| 109 | + } | |
| 110 | + | |
| 111 | + return false; | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * | |
| 116 | + * Error log | |
| 117 | + * | |
| 118 | + * @since 1.9.6 | |
| 119 | + * @access public | |
| 120 | + */ | |
| 121 | + public function error_log( string $message ): void { | |
| 122 | + if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) { | |
| 123 | + error_log( print_r( $message, true ) ); | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + public function default_woocommerce_survey_steps_completed( array $steps ): bool { | |
| 128 | + $completed_actions = get_option( self::CLIENT_WOO_COMPLETED_ACTIONS, array() ); | |
| 129 | + | |
| 130 | + return empty( array_diff( $steps, $completed_actions ) ); | |
| 131 | + } | |
| 132 | + | |
| 133 | + public function is_this_page( string $page ): bool { | |
| 134 | + | |
| 135 | + if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 136 | + return false; | |
| 137 | + } | |
| 138 | + | |
| 139 | + $current_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); | |
| 140 | + | |
| 141 | + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { | |
| 142 | + return false; | |
| 143 | + } | |
| 144 | + | |
| 145 | + if ( isset( $current_uri ) && strpos( $current_uri, '/wp-json/' ) !== false ) { | |
| 146 | + return false; | |
| 147 | + } | |
| 148 | + | |
| 149 | + if ( strpos( $current_uri, $page ) !== false ) { | |
| 150 | + return true; | |
| 151 | + } | |
| 152 | + | |
| 153 | + return false; | |
| 154 | + } | |
| 155 | + | |
| 156 | + public function get_promotional_link_url( string $locale ): string { | |
| 157 | + if ( isset( self::PROMOTIONAL_LINKS[ $locale ] ) ) { | |
| 158 | + return self::PROMOTIONAL_LINKS[ $locale ]; | |
| 159 | + } | |
| 160 | + | |
| 161 | + return self::PROMOTIONAL_LINKS['en_US']; | |
| 162 | + } | |
| 163 | + | |
| 164 | + public function get_domain_from_url(): string { | |
| 165 | + $host = parse_url( get_site_url(), PHP_URL_HOST ); | |
| 166 | + | |
| 167 | + return $host === false ? self::HPANEL_DOMAIN_URL : $host; | |
| 168 | + } | |
| 169 | + | |
| 170 | + public function get_hpanel_domain_url(): string { | |
| 171 | + $domain_url = $this->get_domain_from_url(); | |
| 172 | + | |
| 173 | + if( $domain_url === self::HPANEL_DOMAIN_URL ) { | |
| 174 | + return $domain_url; | |
| 175 | + } | |
| 176 | + | |
| 177 | + return self::HPANEL_DOMAIN_URL . $domain_url; | |
| 178 | + } | |
| 179 | + | |
| 180 | + public function check_transient_eligibility( $transient_request_key, $cache_time = 3600 ): bool { | |
| 181 | + try { | |
| 182 | + // Set transient | |
| 183 | + set_transient( $transient_request_key, true, $cache_time ); | |
| 184 | + | |
| 185 | + // Check if transient was set successfully | |
| 186 | + if ( false === get_transient( $transient_request_key ) ) { | |
| 187 | + throw new Exception( 'Unable to create transient in WordPress.' ); | |
| 188 | + } | |
| 189 | + | |
| 190 | + // If everything is fine, return true | |
| 191 | + return true; | |
| 192 | + | |
| 193 | + } catch ( Exception $exception ) { | |
| 194 | + // If there's an exception, log the error and return false | |
| 195 | + $this->helper->error_log( 'Error checking eligibility: ' . $exception->getMessage() ); | |
| 196 | + return false; | |
| 197 | + } | |
| 198 | + } | |
| 66 | 199 | } |
| 67 | 200 | |
| 68 | -$hostiner_helper = new Hostinger_Helper(); | |
| 201 | +$hostinger_helper = new Hostinger_Helper(); | |