| 1 |
<?php |
| 2 |
namespace Hurrytimer; |
| 3 |
|
| 4 |
use DateTime; |
| 5 |
|
| 6 |
class Helper |
| 7 |
{ |
| 8 |
|
| 9 |
public static function ip_address() |
| 10 |
{ |
| 11 |
$ip = null; |
| 12 |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
| 13 |
$ip = $_SERVER['HTTP_CLIENT_IP']; |
| 14 |
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
| 15 |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 16 |
} else { |
| 17 |
$ip = $_SERVER['REMOTE_ADDR']; |
| 18 |
} |
| 19 |
return $ip; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Get admin preferences' date format. |
| 24 |
* |
| 25 |
* @param string $date |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public static function format_date($date) |
| 29 |
{ |
| 30 |
$date_format = get_option('date_format') ?: 'M d, Y'; |
| 31 |
return date($date_format, strtotime($date)); |
| 32 |
} |
| 33 |
|
| 34 |
public static function get_view($file_path, $data = []) |
| 35 |
{ |
| 36 |
return self::view($file_path, false, $data); |
| 37 |
} |
| 38 |
public static function render_view($file_path, $data = []) |
| 39 |
{ |
| 40 |
self::view($file_path, true, $data); |
| 41 |
} |
| 42 |
public static function view($file_path, $render, $data) |
| 43 |
{ |
| 44 |
extract($data); |
| 45 |
if ($render) { |
| 46 |
include $file_path; |
| 47 |
} else { |
| 48 |
ob_start(); |
| 49 |
$content = include $file_path; |
| 50 |
return ob_get_clean(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Check if WC's active. |
| 56 |
* |
| 57 |
* @return boolean |
| 58 |
*/ |
| 59 |
public static function is_wc_active() |
| 60 |
{ |
| 61 |
if (!is_admin()) { |
| 62 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 63 |
} |
| 64 |
|
| 65 |
return is_plugin_active('woocommerce/woocommerce.php'); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @param string $datetime |
| 70 |
* |
| 71 |
* @return DateTime |
| 72 |
*/ |
| 73 |
public static function date_time($datetime) |
| 74 |
{ |
| 75 |
return new DateTime($datetime); |
| 76 |
} |
| 77 |
|
| 78 |
public static function timezone_string() |
| 79 |
{ |
| 80 |
if ($timezone = get_option('timezone_string')) { |
| 81 |
return $timezone; |
| 82 |
} |
| 83 |
if (0 === ($utc_offset = get_option('gmt_offset', 0))) { |
| 84 |
return 'UTC'; |
| 85 |
} |
| 86 |
|
| 87 |
return self::get_timezone_by_offset($utc_offset); |
| 88 |
} |
| 89 |
|
| 90 |
public static function get_timezone_by_offset($offset) |
| 91 |
{ |
| 92 |
list($hours, $minutes) = explode(':', $offset); |
| 93 |
$seconds = $hours * 60 * 60 + $minutes * 60; |
| 94 |
|
| 95 |
return timezone_name_from_abbr(null, $seconds, true); |
| 96 |
} |
| 97 |
} |
| 98 |
|