| 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 |
|
| 7 |
/** |
| 8 |
* |
| 9 |
* Check if plugin is active |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
* @access public |
| 13 |
*/ |
| 14 |
public static function is_plugin_active( $plugin_slug ): bool { |
| 15 |
$active_plugins = (array) get_option( 'active_plugins', array() ); |
| 16 |
foreach ( $active_plugins as $active_plugin ) { |
| 17 |
if ( strpos( $active_plugin, $plugin_slug . '.php' ) !== false ) { |
| 18 |
return true; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
return false; |
| 23 |
} |
| 24 |
|
| 25 |
public static function get_api_token(): string { |
| 26 |
$api_token = ''; |
| 27 |
$token_file = HOSTINGER_WP_TOKEN; |
| 28 |
|
| 29 |
if ( file_exists( $token_file ) && ! empty( file_get_contents( $token_file ) ) ) { |
| 30 |
$api_token = file_get_contents( $token_file ); |
| 31 |
} |
| 32 |
|
| 33 |
return $api_token; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* |
| 38 |
* Get the host info (domain, subdomain, subdirectory) |
| 39 |
* |
| 40 |
* @since 1.7.0 |
| 41 |
* @access public |
| 42 |
*/ |
| 43 |
|
| 44 |
public function get_host_info(): string { |
| 45 |
$host = $_SERVER['HTTP_HOST'] ?? ''; |
| 46 |
$site_url = get_site_url(); |
| 47 |
$site_url = preg_replace( '#^https?://#', '', $site_url ); |
| 48 |
|
| 49 |
if ( ! empty( $site_url ) && ! empty( $host ) && strpos( $site_url, $host ) === 0 ) { |
| 50 |
if ( $site_url === $host ) { |
| 51 |
return $host; |
| 52 |
} else { |
| 53 |
return substr( $site_url, strlen( $host ) + 1 ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return $host; |
| 58 |
} |
| 59 |
|
| 60 |
public function is_preview_domain(): bool { |
| 61 |
if ( function_exists( 'getallheaders' ) ) { |
| 62 |
$headers = getallheaders(); |
| 63 |
} |
| 64 |
|
| 65 |
if ( isset( $headers['X-Preview-Indicator'] ) && $headers['X-Preview-Indicator'] ) { |
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
public function is_free_subdomain(): bool { |
| 73 |
$site_url = preg_replace( '#^https?://#', '', get_site_url() ); |
| 74 |
|
| 75 |
return ! empty( $site_url ) && strpos( $site_url, self::HOSTINGER_FREE_SUBDOMAIN_URL ) !== false; |
| 76 |
} |
| 77 |
|
| 78 |
public function is_hostinger_admin_page(): bool { |
| 79 |
|
| 80 |
if( ! isset( $_SERVER['REQUEST_URI'] ) ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
$current_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); |
| 85 |
|
| 86 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
if ( isset( $current_uri ) && strpos( $current_uri, '/wp-json/' ) !== false ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
if ( strpos( $current_uri, self::HOSTINGER_PAGE ) !== false ) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* |
| 103 |
* Error log |
| 104 |
* |
| 105 |
* @since 1.9.6 |
| 106 |
* @access public |
| 107 |
*/ |
| 108 |
public function error_log( string $message ): void { |
| 109 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) { |
| 110 |
error_log( print_r( $message, true ) ); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
$hostinger_helper = new Hostinger_Helper(); |
| 116 |
|