| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer\Utils; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
|
| 7 |
class Helpers |
| 8 |
{ |
| 9 |
public static function ip_address() |
| 10 |
{ |
| 11 |
$ip = ''; |
| 12 |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
| 13 |
$ip = $_SERVER['HTTP_CLIENT_IP']; |
| 14 |
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
| 15 |
// HTTP_X_FORWARDED_FOR can contain multiple IPs; use the first one. |
| 16 |
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); |
| 17 |
$ip = trim($ips[0]); |
| 18 |
} else { |
| 19 |
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; |
| 20 |
} |
| 21 |
|
| 22 |
// Validate the IP address to prevent injection. |
| 23 |
$ip = filter_var($ip, FILTER_VALIDATE_IP); |
| 24 |
|
| 25 |
return $ip ? $ip : '0.0.0.0'; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Get admin preferences' date format. |
| 30 |
* |
| 31 |
* @param string $date |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public static function format_date($date) |
| 36 |
{ |
| 37 |
$date_format = get_option('date_format') ?: 'M d, Y'; |
| 38 |
|
| 39 |
return date($date_format, strtotime($date)); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Check if WC's active. |
| 44 |
* |
| 45 |
* @return boolean |
| 46 |
*/ |
| 47 |
public static function isWcActive() |
| 48 |
{ |
| 49 |
if (!is_admin()) { |
| 50 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 51 |
} |
| 52 |
|
| 53 |
return is_plugin_active('woocommerce/woocommerce.php'); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Check if WC's active. |
| 58 |
* |
| 59 |
* @return boolean |
| 60 |
*/ |
| 61 |
public static function isPluginInstalled() |
| 62 |
{ |
| 63 |
if (!is_admin()) { |
| 64 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 65 |
} |
| 66 |
|
| 67 |
return is_plugin_active('hurrytimer/hurrytimer.php'); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Check if WC's active. |
| 72 |
* |
| 73 |
* @return boolean |
| 74 |
*/ |
| 75 |
public static function isPluginProInstalled() |
| 76 |
{ |
| 77 |
if (!is_admin()) { |
| 78 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 79 |
} |
| 80 |
|
| 81 |
return is_plugin_active('hurrytimer-pro/hurrytimer.php'); |
| 82 |
} |
| 83 |
|
| 84 |
public static function date_time($datetime) |
| 85 |
{ |
| 86 |
return new DateTime($datetime); |
| 87 |
} |
| 88 |
|
| 89 |
public static function get_timezone_by_offset($offset) |
| 90 |
{ |
| 91 |
list($hours, $minutes) = wp_parse_args([0, 0], explode(':', $offset)); |
| 92 |
$seconds = $hours * 60 * 60 + $minutes * 60; |
| 93 |
|
| 94 |
return timezone_name_from_abbr(null, $seconds, true); |
| 95 |
} |
| 96 |
|
| 97 |
public static function date_later($seconds) |
| 98 |
{ |
| 99 |
$now = current_time('mysql'); |
| 100 |
|
| 101 |
return date('Y-m-d H:i:s', strtotime($now) + $seconds); |
| 102 |
} |
| 103 |
|
| 104 |
public static function isNewPost($post_id) |
| 105 |
{ |
| 106 |
return get_post_status($post_id) === "auto-draft"; |
| 107 |
} |
| 108 |
|
| 109 |
public static function isPostActive($post_id) |
| 110 |
{ |
| 111 |
return get_post_status($post_id) === "publish" |
| 112 |
|| get_post_status($post_id) === "future"; |
| 113 |
} |
| 114 |
|
| 115 |
public static function activateUrl($post_id) |
| 116 |
{ |
| 117 |
return wp_nonce_url( |
| 118 |
add_query_arg( |
| 119 |
array( |
| 120 |
'hurrytimer_action' => 'activate-compaign', |
| 121 |
'postid' => $post_id, |
| 122 |
'post' => $post_id, |
| 123 |
'action' => 'edit', |
| 124 |
), |
| 125 |
admin_url('post.php') |
| 126 |
), |
| 127 |
'activate-compaign' |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
public static function deactivateUrl($post_id) |
| 132 |
{ |
| 133 |
return wp_nonce_url( |
| 134 |
add_query_arg( |
| 135 |
array( |
| 136 |
'hurrytimer_action' => 'deactivate-compaign', |
| 137 |
'postid' => $post_id, |
| 138 |
'post' => $post_id, |
| 139 |
'action' => 'edit', |
| 140 |
), |
| 141 |
admin_url('post.php') |
| 142 |
), |
| 143 |
'deactivate-compaign' |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
public static function camelToSnakeCase($str) |
| 148 |
{ |
| 149 |
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $str)); |
| 150 |
} |
| 151 |
|
| 152 |
public static function snakeToCamelCase($str) |
| 153 |
{ |
| 154 |
$arr = explode('_', $str); |
| 155 |
$arr = array_map(function ($i, $word) { |
| 156 |
if ($i === 0) { |
| 157 |
return $word; |
| 158 |
} |
| 159 |
|
| 160 |
return ucfirst($word); |
| 161 |
}, array_keys($arr), $arr); |
| 162 |
|
| 163 |
return implode('', $arr); |
| 164 |
} |
| 165 |
|
| 166 |
public static function getCampaigns($args = []) |
| 167 |
{ |
| 168 |
return get_posts(array_merge( |
| 169 |
[ |
| 170 |
'post_type' => HURRYT_POST_TYPE, |
| 171 |
'numberposts' => -1, |
| 172 |
], |
| 173 |
$args |
| 174 |
)); |
| 175 |
} |
| 176 |
|
| 177 |
public static function are_urls_match($url1, $url2) |
| 178 |
{ |
| 179 |
$url1_path = untrailingslashit(parse_url($url1, PHP_URL_PATH)?: ''); |
| 180 |
$url2_path = untrailingslashit(parse_url($url2, PHP_URL_PATH)?: ''); |
| 181 |
|
| 182 |
return $url1_path === $url2_path; |
| 183 |
} |
| 184 |
public static function createResetAllEvergreenCampaignsUrl($scope = 'admin') |
| 185 |
{ |
| 186 |
return wp_nonce_url( |
| 187 |
add_query_arg( |
| 188 |
array( |
| 189 |
'hurryt-action' => 'reset-all-evergreen-compaigns', |
| 190 |
'hurryt-scope' => $scope, |
| 191 |
'hurryt-page' => 'hurrytimer_settings', |
| 192 |
), |
| 193 |
admin_url('admin.php?page=hurrytimer_settings') |
| 194 |
), |
| 195 |
'reset-all-evergreen-compaigns' |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
public static function sanitize_array($data) { |
| 200 |
if (is_array($data)) { |
| 201 |
foreach ($data as $key => $value) { |
| 202 |
// Check the type of each element and sanitize accordingly |
| 203 |
if (is_array($value)) { |
| 204 |
// If the element is an array, recursively call sanitize_array |
| 205 |
$data[$key] = static::sanitize_array($value); |
| 206 |
} elseif (is_string($value)) { |
| 207 |
$data[$key] = sanitize_text_field($value); |
| 208 |
} elseif (is_int($value)) { |
| 209 |
$data[$key] = intval($value); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return $data; |
| 215 |
} |
| 216 |
} |
| 217 |
|