PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 1.1.1
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v1.1.1
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 / class-hurrytimer-countdown.php

class-hurrytimer-countdown.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 1.1.1, at includes/class-hurrytimer-countdown.php

386 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 class Hurrytimer_Countdown
6 {
7
8 private $post_id;
9 const DEFAULT_COLOR = "#333";
10 const DEFAULT_LABEL_SIZE = 12;
11 const DEFAULT_CDT_SIZE = 20;
12 const DEFAULT_HEADLINE_SIZE = 16;
13
14 public function __construct($post_id)
15 {
16 $this->post_id = $post_id;
17 }
18
19 public function get_id()
20 {
21 return $this->post_id;
22 }
23
24 public function get_all_settings()
25 {
26 $meta_keys = [
27 'mode',
28 'duration',
29 'end_datetime',
30 'text_color',
31 'text_size',
32 'label_size',
33 'headline_color',
34 'headline_size',
35 'headline_position',
36 'display_headline',
37 'redirect_url',
38 'products_type',
39 'products',
40 'wc_enable',
41 'end_action',
42 'position',
43 'display_labels',
44 'labels',
45
46 ];
47 $meta_values = [];
48 foreach ($meta_keys as $key) {
49 $meta_values[$key] = $this->{"get_$key"}();
50 }
51
52 return $meta_values;
53 }
54 private function default_labels()
55 {
56 return ['days' => 'days', 'hours' => 'hrs', 'minutes' => 'mins', 'seconds' => 'secs'];
57 }
58 /**
59 * Bulk save for countdown timer options
60 *
61 * @param $data
62 */
63 public function save($data)
64 {
65 foreach ($data as $key => $value) {
66 $method_name = "set_$key";
67 if (method_exists($this, $method_name)) {
68 $this->$method_name($value);
69 }
70 }
71 if (!array_key_exists('wc_enable', $data)) {
72 $this->set_wc_enable(false);
73 }
74 if (!array_key_exists('display_labels', $data)) {
75 $this->set_display_labels(false);
76 }
77 if (!array_key_exists('display_headline', $data)) {
78 $this->set_display_headline(false);
79 }
80 }
81
82 /**
83 * Get countdown timer WP post
84 * @return array|null|\WP_Post
85 */
86 public function get_countdown()
87 {
88 return get_post($this->post_id);
89 }
90
91 /**
92 * Check current countdown timer mode
93 * @return bool
94 */
95 public function is_evergreen()
96 {
97 return $this->get_mode() === CDT_Mode::EVERGREEN;
98 }
99
100 /**
101 * Save mode
102 *
103 * @param $mode
104 */
105 public function set_mode($mode)
106 {
107 update_post_meta($this->post_id, 'mode', $mode);
108 }
109
110 public function set_end_datetime($end_date = null)
111 {
112 if ($end_date === null) {
113 $end_date = $this->get_default_end_datetime();
114 }
115 update_post_meta($this->post_id, 'end_datetime', $end_date);
116 }
117
118 private function get_default_end_datetime()
119 {
120 return date('Y-m-d h:i A', strtotime('+1 week'));
121 }
122
123 public function set_end_action($end_action)
124 {
125 update_post_meta($this->post_id, 'end_action', $end_action);
126 }
127
128 public function set_redirect_url($redirectUrl)
129 {
130 update_post_meta($this->post_id, 'redirect_url', $redirectUrl);
131 }
132
133 public function set_text_color($text_color)
134 {
135 update_post_meta($this->post_id, 'text_color', $text_color ?: self::DEFAULT_COLOR);
136 }
137
138 public function set_headline_color($headline_color)
139 {
140 update_post_meta($this->post_id, 'headline_color', $headline_color ?: self::DEFAULT_COLOR);
141 }
142
143 public function set_wc_enable($wc_enable)
144 {
145 update_post_meta($this->post_id, 'wc_enable', $wc_enable);
146 }
147
148 public function set_display_headline($display_headline)
149 {
150 update_post_meta($this->post_id, 'display_headline', $display_headline);
151 }
152
153 public function get_display_headline()
154 {
155
156 return filter_var(get_post_meta($this->post_id, 'display_headline', true), FILTER_VALIDATE_BOOLEAN);
157 }
158 public function set_display_labels($display_labels)
159 {
160 update_post_meta($this->post_id, 'display_labels', $display_labels);
161 }
162 public function set_text_size($text_size)
163 {
164 update_post_meta($this->post_id, 'text_size', $text_size ?: self::DEFAULT_CDT_SIZE);
165 }
166
167 public function set_label_size($label_size)
168 {
169 update_post_meta($this->post_id, 'label_size', $label_size ?: self::DEFAULT_LABEL_SIZE);
170 }
171
172 public function set_headline_size($headline_size)
173 {
174 update_post_meta($this->post_id, 'headline_size', $headline_size ?: self::DEFAULT_HEADLINE_SIZE);
175 }
176
177 public function set_position($pos)
178 {
179 update_post_meta($this->post_id, 'position', $pos);
180 }
181
182 public function set_products_type($selection)
183 {
184 update_post_meta($this->post_id, 'products_type', $selection);
185 }
186
187 public function set_products($products)
188 {
189 update_post_meta($this->post_id, 'products', $products);
190 }
191
192 public function set_headline_position($position)
193 {
194 update_post_meta($this->post_id, 'headline_position', $position);
195 }
196
197 public function get_headline_position()
198 {
199 return intval(get_post_meta($this->post_id, 'headline_position', true)) ?: Headline_Position::ABOVE_TIMER;
200 }
201
202 public function get_duration()
203 {
204 $duration = get_post_meta($this->post_id, 'duration', true) ?: [0, 0, 0];
205 return array_map('intval', $duration);
206 }
207
208 public function set_duration($duration)
209 {
210 update_post_meta($this->post_id, 'duration', $duration);
211 }
212
213 public function get_duration_in_seconds()
214 {
215
216 list($d, $h, $m) = $this->get_duration();
217 return $d * DAY_IN_SECONDS + $h * HOUR_IN_SECONDS + $m * MINUTE_IN_SECONDS;
218 }
219
220 public function get_mode()
221 {
222 $mode = intval(get_post_meta($this->post_id, 'mode', true));
223 return $mode === 0 ? CDT_Mode::EVERGREEN : $mode;
224 }
225
226 public function get_end_action()
227 {
228 return intval(get_post_meta($this->post_id, 'end_action', true));
229 }
230
231 public function get_redirect_url()
232 {
233 return get_post_meta($this->post_id, 'redirect_url', true);
234 }
235
236 public function get_end_datetime()
237 {
238 return $this->get_meta_or_default('end_datetime', $this->get_default_end_datetime());
239 }
240
241 public function get_start_datetime()
242 {
243 return $this->get_countdown()->post_date;
244 }
245
246 public function get_text_color()
247 {
248 return $this->get_meta_or_default('text_color', self::DEFAULT_COLOR);
249 }
250
251 public function get_headline_color()
252 {
253 return $this->get_meta_or_default('headline_color', self::DEFAULT_COLOR);
254 }
255
256 public function get_position()
257 {
258 return floatval(get_post_meta($this->post_id, 'position', true));
259 }
260
261 public function get_text_size()
262 {
263 return $this->get_meta_or_default('text_size', self::DEFAULT_CDT_SIZE);
264 }
265
266 public function get_label_size()
267 {
268 return $this->get_meta_or_default('label_size', self::DEFAULT_LABEL_SIZE);
269 }
270
271 public function get_headline_size()
272 {
273 return $this->get_meta_or_default('headline_size', self::DEFAULT_HEADLINE_SIZE);
274 }
275
276 private function get_meta_or_default($name, $default = null)
277 {
278 return get_post_meta($this->post_id, $name, true) ?: $default;
279 }
280
281 public function get_wc_enable()
282 {
283 return boolval(get_post_meta($this->post_id, 'wc_enable', true));
284 }
285 public function get_display_labels()
286 {
287 return filter_var(get_post_meta($this->post_id, 'display_labels', true), FILTER_VALIDATE_BOOLEAN);
288 }
289 public function get_products_type()
290 {
291 return intval(get_post_meta($this->post_id, 'products_type', true));
292 }
293
294 /**
295 * @deprecated 1.0.0
296 *
297 * @return int
298 */
299 public function get_products_selection()
300 {
301 return intval(get_post_meta($this->post_id, 'products_selection', true));
302 }
303
304 public function get_products()
305 {
306 $products = get_post_meta($this->post_id, 'products', true) ?: [];
307
308 return array_map('intval', $products);
309 }
310
311 public function get_labels()
312 {
313 return get_post_meta($this->post_id, 'labels', true) ?: $this->default_labels();
314 }
315
316 public function set_labels($labels)
317 {
318 $labels = array_merge($this->default_labels(), array_filter($labels));
319 update_post_meta($this->post_id, 'labels', $labels);
320 }
321
322 /**
323 * unused
324 * @return mixed
325 */
326 public function get_theme()
327 {
328 return get_post_meta($this->post_id, 'theme', true);
329 }
330 public function is_active()
331 {
332 return get_post_status($this->post_id) === "publish" || get_post_status($this->post_id) === "future";
333 }
334
335 public function is_scheduled()
336 {
337 $start_timestamp = strtotime($this->get_start_datetime());
338 $curr_timestamp = current_time('timestamp');
339 return $start_timestamp > $curr_timestamp;
340 }
341 public function get_client_config()
342 {
343 $id = $this->get_id();
344 $is_evergreen = $this->is_evergreen();
345
346 if ($this->is_scheduled()) {
347 return null;
348 }
349 // common config
350 $client_config = [
351 'endAction' => $this->get_end_action(),
352 'redirectURL' => $this->get_redirect_url(),
353 'template' => $this->get_template(),
354 ];
355 // normal
356 if (!$is_evergreen) {
357 $client_config['endDate'] = $this->get_end_datetime();
358 return $client_config;
359 //evergeen
360 } else {
361 $end_after = $this->get_duration_in_seconds();
362 $evergreen = new Evergreen_Countdown($this->get_id(), $end_after);
363 $client_config['endAfter'] = $end_after;
364 $client_config['endDate'] = $evergreen->get_end_timestamp();
365 $client_config['cookieName'] = $evergreen->get_cookie_name();
366 $client_config['isDone'] = $evergreen->is_done();
367 return $client_config;
368 }
369 }
370
371 public function get_template()
372 {
373 $are_labels_visible = $this->get_display_labels();
374 $labels = $this->get_labels();
375 $headline = $this->get_countdown()->post_title;
376
377 $headline_position = $this->get_headline_position();
378 $display_headline = $this->get_display_headline();
379 ob_start();
380 include HURRYTIMER_PLUGIN_DIR . "/templates/countdown-timer.php";
381 return ob_get_clean();
382
383 }
384
385 }
386