PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.2.0
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.2.0
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / Utils / Helpers.php

Helpers.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.2.0, at includes/Utils/Helpers.php

249 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if (($timezone = get_option('timezone_string'))) {
87 return $timezone;
88 }
89 if (0 === ($utc_offset = get_option('gmt_offset', 0))) {
90 return 'UTC';
91 }
92
93 return self::get_timezone_by_offset($utc_offset);
94 }
95
96 public static function get_timezone_by_offset($offset)
97 {
98 list($hours, $minutes)= wp_parse_args([0,0], explode(':', $offset));
99 $seconds = $hours * 60 * 60 + $minutes * 60;
100 return timezone_name_from_abbr(null, $seconds, true);
101 }
102
103 public static function date_later($seconds)
104 {
105 $now = current_time('mysql');
106
107 return date('Y-m-d H:i:s', strtotime($now) + $seconds);
108 }
109
110 public static function isNewPost($post_id)
111 {
112 return get_post_status($post_id) === "auto-draft";
113 }
114
115 public static function isPostActive($post_id)
116 {
117 return get_post_status($post_id) === "publish"
118 || get_post_status($post_id) === "future";
119 }
120
121 public static function activateUrl($post_id)
122 {
123 return wp_nonce_url(
124 add_query_arg(
125 array(
126 'hurrytimer_action' => 'activate-compaign',
127 'postid' => $post_id,
128 'post' => $post_id,
129 'action' => 'edit',
130 ),
131 network_admin_url('post.php')
132 ),
133 'activate-compaign'
134 );
135 }
136
137 public static function deactivateUrl($post_id)
138 {
139 return wp_nonce_url(
140 add_query_arg(
141 array(
142 'hurrytimer_action' => 'deactivate-compaign',
143 'postid' => $post_id,
144 'post' => $post_id,
145 'action' => 'edit',
146 ),
147 network_admin_url('post.php')
148 ),
149 'deactivate-compaign'
150 );
151 }
152
153 public static function camelToSnakeCase($str)
154 {
155 return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $str));
156 }
157
158 public static function snakeToCamelCase($str)
159 {
160 $arr = explode('_', $str);
161 $arr = array_map(function ($i, $word) {
162 if ($i === 0) {
163 return $word;
164 }
165
166 return ucfirst($word);
167 }, array_keys($arr), $arr);
168
169 return implode('', $arr);
170 }
171
172 public static function getCampaigns($args = [])
173 {
174 return get_posts(array_merge(
175 [
176 'post_type' => HURRYT_POST_TYPE,
177 'numberposts' => -1,
178 ],
179 $args));
180 }
181
182 static function isUsingDashboard()
183 {
184 return is_admin()
185 || is_preview()
186 || is_customize_preview()
187 || (strpos($_SERVER['REQUEST_URI'], 'preview=true') !== false
188 && strpos($_SERVER['REQUEST_URI'], 'preview_nonce=') !== false)
189 || (strpos($_SERVER['REQUEST_URI'], 'fl_builder') !== false);
190 }
191 public static function getSiteTimezone()
192 {
193 $current_offset = get_option('gmt_offset');
194 $tzstring = get_option('timezone_string');
195
196 $check_zone_info = true;
197
198 // Remove old Etc mappings. Fallback to gmt_offset.
199 if (false !== strpos($tzstring, 'Etc/GMT')) {
200 $tzstring = '';
201 }
202
203 if (empty($tzstring)) { // Create a UTC+- zone if no timezone string exists
204 $check_zone_info = false;
205 if (0 == $current_offset) {
206 $tzstring = 'UTC+0';
207 } elseif ($current_offset < 0) {
208 $tzstring = 'UTC' . $current_offset;
209 } else {
210 $tzstring = 'UTC+' . $current_offset;
211 }
212 }
213 return $tzstring;
214 }
215
216 public static function tzOffsetToName($tz)
217 {
218 $tz_name = $tz;
219 if (strpos($tz, 'UTC') !== false && $tz !== 'UTC') {
220 $tz_name = str_replace(array('.25', '.5', '.75', 'UTC'), array(':15', ':30', ':45', ''), $tz);
221 }
222
223 return $tz_name;
224 }
225
226 public static function wpWeekDayToRRuleDay($days = []){
227 $rruleDays = [ 'SA', 'MO' ,'TU', 'WE', 'TH', 'FR','SU' ];
228 return array_map(function($i) use($rruleDays){
229 return $rruleDays[$i];
230 },$days);
231
232 }
233
234 static function createResetAllEvergreenCampaignsUrl($scope = 'admin')
235 {
236 return wp_nonce_url(
237 add_query_arg(
238 array(
239 'hurryt-action' => 'reset-all-evergreen-compaigns',
240 'hurryt-scope' => $scope,
241 'hurryt-page' => 'hurrytimer_settings',
242 ),
243 network_admin_url('admin.php?page=hurrytimer_settings')
244 ),
245 'reset-all-evergreen-compaigns'
246 );
247 }
248 }
249