| 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 = 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 |
|
| 20 |
return $ip; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get admin preferences' date format. |
| 25 |
* |
| 26 |
* @param string $date |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public static function format_date($date) |
| 31 |
{ |
| 32 |
$date_format = get_option('date_format') ?: 'M d, Y'; |
| 33 |
|
| 34 |
return date($date_format, strtotime($date)); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Check if WC's active. |
| 39 |
* |
| 40 |
* @return boolean |
| 41 |
*/ |
| 42 |
public static function isWcActive() |
| 43 |
{ |
| 44 |
if (!is_admin()) { |
| 45 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 46 |
} |
| 47 |
|
| 48 |
return is_plugin_active('woocommerce/woocommerce.php'); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Check if WC's active. |
| 53 |
* |
| 54 |
* @return boolean |
| 55 |
*/ |
| 56 |
public static function isPluginInstalled() |
| 57 |
{ |
| 58 |
if (!is_admin()) { |
| 59 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 60 |
} |
| 61 |
|
| 62 |
return is_plugin_active('hurrytimer/hurrytimer.php'); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Check if WC's active. |
| 67 |
* |
| 68 |
* @return boolean |
| 69 |
*/ |
| 70 |
public static function isPluginProInstalled() |
| 71 |
{ |
| 72 |
if (!is_admin()) { |
| 73 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 74 |
} |
| 75 |
|
| 76 |
return is_plugin_active('hurrytimer-pro/hurrytimer.php'); |
| 77 |
} |
| 78 |
|
| 79 |
public static function date_time($datetime) |
| 80 |
{ |
| 81 |
return new DateTime($datetime); |
| 82 |
} |
| 83 |
|
| 84 |
public static function timezone_string() |
| 85 |
{ |
| 86 |
$timezone_string = get_option('timezone_string'); |
| 87 |
if (!empty($timezone_string)) { |
| 88 |
return $timezone_string; |
| 89 |
} |
| 90 |
$offset = get_option('gmt_offset'); |
| 91 |
$hours = (int) $offset; |
| 92 |
$minutes = abs(($offset - (int) $offset) * 60); |
| 93 |
$offset = sprintf('%+03d:%02d', $hours, $minutes); |
| 94 |
return $offset; |
| 95 |
} |
| 96 |
|
| 97 |
public static function get_timezone_by_offset($offset) |
| 98 |
{ |
| 99 |
list($hours, $minutes) = wp_parse_args([0, 0], explode(':', $offset)); |
| 100 |
$seconds = $hours * 60 * 60 + $minutes * 60; |
| 101 |
|
| 102 |
return timezone_name_from_abbr(null, $seconds, true); |
| 103 |
} |
| 104 |
|
| 105 |
public static function date_later($seconds) |
| 106 |
{ |
| 107 |
$now = current_time('mysql'); |
| 108 |
|
| 109 |
return date('Y-m-d H:i:s', strtotime($now) + $seconds); |
| 110 |
} |
| 111 |
|
| 112 |
public static function isNewPost($post_id) |
| 113 |
{ |
| 114 |
return get_post_status($post_id) === "auto-draft"; |
| 115 |
} |
| 116 |
|
| 117 |
public static function isPostActive($post_id) |
| 118 |
{ |
| 119 |
return get_post_status($post_id) === "publish" |
| 120 |
|| get_post_status($post_id) === "future"; |
| 121 |
} |
| 122 |
|
| 123 |
public static function activateUrl($post_id) |
| 124 |
{ |
| 125 |
return wp_nonce_url( |
| 126 |
add_query_arg( |
| 127 |
array( |
| 128 |
'hurrytimer_action' => 'activate-compaign', |
| 129 |
'postid' => $post_id, |
| 130 |
'post' => $post_id, |
| 131 |
'action' => 'edit', |
| 132 |
), |
| 133 |
network_admin_url('post.php') |
| 134 |
), |
| 135 |
'activate-compaign' |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
public static function deactivateUrl($post_id) |
| 140 |
{ |
| 141 |
return wp_nonce_url( |
| 142 |
add_query_arg( |
| 143 |
array( |
| 144 |
'hurrytimer_action' => 'deactivate-compaign', |
| 145 |
'postid' => $post_id, |
| 146 |
'post' => $post_id, |
| 147 |
'action' => 'edit', |
| 148 |
), |
| 149 |
network_admin_url('post.php') |
| 150 |
), |
| 151 |
'deactivate-compaign' |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
public static function camelToSnakeCase($str) |
| 156 |
{ |
| 157 |
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $str)); |
| 158 |
} |
| 159 |
|
| 160 |
public static function snakeToCamelCase($str) |
| 161 |
{ |
| 162 |
$arr = explode('_', $str); |
| 163 |
$arr = array_map(function ($i, $word) { |
| 164 |
if ($i === 0) { |
| 165 |
return $word; |
| 166 |
} |
| 167 |
|
| 168 |
return ucfirst($word); |
| 169 |
}, array_keys($arr), $arr); |
| 170 |
|
| 171 |
return implode('', $arr); |
| 172 |
} |
| 173 |
|
| 174 |
public static function getCampaigns($args = []) |
| 175 |
{ |
| 176 |
return get_posts(array_merge( |
| 177 |
[ |
| 178 |
'post_type' => HURRYT_POST_TYPE, |
| 179 |
'numberposts' => -1, |
| 180 |
], |
| 181 |
$args)); |
| 182 |
} |
| 183 |
|
| 184 |
public static function isUsingDashboard() |
| 185 |
{ |
| 186 |
return is_admin() |
| 187 |
|| is_preview() |
| 188 |
|| is_customize_preview() |
| 189 |
|| (strpos($_SERVER['REQUEST_URI'], 'preview=true') !== false |
| 190 |
&& strpos($_SERVER['REQUEST_URI'], 'preview_nonce=') !== false) |
| 191 |
|| (strpos($_SERVER['REQUEST_URI'], 'fl_builder') !== false); |
| 192 |
} |
| 193 |
|
| 194 |
public static function getSiteTimezone() |
| 195 |
{ |
| 196 |
$current_offset = get_option('gmt_offset'); |
| 197 |
$tzstring = get_option('timezone_string'); |
| 198 |
|
| 199 |
$check_zone_info = true; |
| 200 |
|
| 201 |
// Remove old Etc mappings. Fallback to gmt_offset. |
| 202 |
if (false !== strpos($tzstring, 'Etc/GMT')) { |
| 203 |
$tzstring = ''; |
| 204 |
} |
| 205 |
|
| 206 |
if (empty($tzstring)) { // Create a UTC+- zone if no timezone string exists |
| 207 |
$check_zone_info = false; |
| 208 |
if (0 == $current_offset) { |
| 209 |
$tzstring = 'UTC+0'; |
| 210 |
} elseif ($current_offset < 0) { |
| 211 |
$tzstring = 'UTC' . $current_offset; |
| 212 |
} else { |
| 213 |
$tzstring = 'UTC+' . $current_offset; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return $tzstring; |
| 218 |
} |
| 219 |
|
| 220 |
public static function tzOffsetToName($tz) |
| 221 |
{ |
| 222 |
$tz_name = $tz; |
| 223 |
if (strpos($tz, 'UTC') !== false && $tz !== 'UTC') { |
| 224 |
$tz_name = str_replace(array('.25', '.5', '.75', 'UTC'), array(':15', ':30', ':45', ''), |
| 225 |
$tz); |
| 226 |
} |
| 227 |
|
| 228 |
return $tz_name; |
| 229 |
} |
| 230 |
|
| 231 |
public static function wpWeekDayToRRuleDay($days = []) |
| 232 |
{ |
| 233 |
$rruleDays = ['SA', 'MO', 'TU', 'WE', 'TH', 'FR', 'SU']; |
| 234 |
|
| 235 |
return array_map(function ($i) use ($rruleDays) { |
| 236 |
return $rruleDays[$i]; |
| 237 |
}, $days); |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
public static function createResetAllEvergreenCampaignsUrl($scope = 'admin') |
| 242 |
{ |
| 243 |
return wp_nonce_url( |
| 244 |
add_query_arg( |
| 245 |
array( |
| 246 |
'hurryt-action' => 'reset-all-evergreen-compaigns', |
| 247 |
'hurryt-scope' => $scope, |
| 248 |
'hurryt-page' => 'hurrytimer_settings', |
| 249 |
), |
| 250 |
network_admin_url('admin.php?page=hurrytimer_settings') |
| 251 |
), |
| 252 |
'reset-all-evergreen-compaigns' |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
|