| 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 get_timezone_by_offset($offset) |
| 85 |
{ |
| 86 |
list($hours, $minutes) = wp_parse_args([0, 0], explode(':', $offset)); |
| 87 |
$seconds = $hours * 60 * 60 + $minutes * 60; |
| 88 |
|
| 89 |
return timezone_name_from_abbr(null, $seconds, true); |
| 90 |
} |
| 91 |
|
| 92 |
public static function date_later($seconds) |
| 93 |
{ |
| 94 |
$now = current_time('mysql'); |
| 95 |
|
| 96 |
return date('Y-m-d H:i:s', strtotime($now) + $seconds); |
| 97 |
} |
| 98 |
|
| 99 |
public static function isNewPost($post_id) |
| 100 |
{ |
| 101 |
return get_post_status($post_id) === "auto-draft"; |
| 102 |
} |
| 103 |
|
| 104 |
public static function isPostActive($post_id) |
| 105 |
{ |
| 106 |
return get_post_status($post_id) === "publish" |
| 107 |
|| get_post_status($post_id) === "future"; |
| 108 |
} |
| 109 |
|
| 110 |
public static function activateUrl($post_id) |
| 111 |
{ |
| 112 |
return wp_nonce_url( |
| 113 |
add_query_arg( |
| 114 |
array( |
| 115 |
'hurrytimer_action' => 'activate-compaign', |
| 116 |
'postid' => $post_id, |
| 117 |
'post' => $post_id, |
| 118 |
'action' => 'edit', |
| 119 |
), |
| 120 |
admin_url('post.php') |
| 121 |
), |
| 122 |
'activate-compaign' |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
public static function deactivateUrl($post_id) |
| 127 |
{ |
| 128 |
return wp_nonce_url( |
| 129 |
add_query_arg( |
| 130 |
array( |
| 131 |
'hurrytimer_action' => 'deactivate-compaign', |
| 132 |
'postid' => $post_id, |
| 133 |
'post' => $post_id, |
| 134 |
'action' => 'edit', |
| 135 |
), |
| 136 |
admin_url('post.php') |
| 137 |
), |
| 138 |
'deactivate-compaign' |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
public static function camelToSnakeCase($str) |
| 143 |
{ |
| 144 |
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $str)); |
| 145 |
} |
| 146 |
|
| 147 |
public static function snakeToCamelCase($str) |
| 148 |
{ |
| 149 |
$arr = explode('_', $str); |
| 150 |
$arr = array_map(function ($i, $word) { |
| 151 |
if ($i === 0) { |
| 152 |
return $word; |
| 153 |
} |
| 154 |
|
| 155 |
return ucfirst($word); |
| 156 |
}, array_keys($arr), $arr); |
| 157 |
|
| 158 |
return implode('', $arr); |
| 159 |
} |
| 160 |
|
| 161 |
public static function getCampaigns($args = []) |
| 162 |
{ |
| 163 |
return get_posts(array_merge( |
| 164 |
[ |
| 165 |
'post_type' => HURRYT_POST_TYPE, |
| 166 |
'numberposts' => -1, |
| 167 |
], |
| 168 |
$args |
| 169 |
)); |
| 170 |
} |
| 171 |
|
| 172 |
public static function are_urls_match($url1, $url2) |
| 173 |
{ |
| 174 |
$url1_path = untrailingslashit(parse_url($url1, PHP_URL_PATH)?: ''); |
| 175 |
$url2_path = untrailingslashit(parse_url($url2, PHP_URL_PATH)?: ''); |
| 176 |
|
| 177 |
return $url1_path === $url2_path; |
| 178 |
} |
| 179 |
public static function createResetAllEvergreenCampaignsUrl($scope = 'admin') |
| 180 |
{ |
| 181 |
return wp_nonce_url( |
| 182 |
add_query_arg( |
| 183 |
array( |
| 184 |
'hurryt-action' => 'reset-all-evergreen-compaigns', |
| 185 |
'hurryt-scope' => $scope, |
| 186 |
'hurryt-page' => 'hurrytimer_settings', |
| 187 |
), |
| 188 |
admin_url('admin.php?page=hurrytimer_settings') |
| 189 |
), |
| 190 |
'reset-all-evergreen-compaigns' |
| 191 |
); |
| 192 |
} |
| 193 |
} |
| 194 |
|