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