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

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

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