| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
class Evergreen_Countdown |
| 6 |
{ |
| 7 |
public $cookie_detection; |
| 8 |
public $ip_detection; |
| 9 |
|
| 10 |
public function __construct() |
| 11 |
{ |
| 12 |
$this->cookie_detection = new Cookie_Detection(); |
| 13 |
$this->ip_detection = new IP_Detection(); |
| 14 |
} |
| 15 |
|
| 16 |
public function reset($countdown_id) |
| 17 |
{ |
| 18 |
$this->ip_detection->forget($countdown_id); |
| 19 |
update_post_meta($countdown_id, '_hurrytimer_reset_countdown', 1); |
| 20 |
} |
| 21 |
|
| 22 |
public function expires_at($countdown_id) |
| 23 |
{ |
| 24 |
// Get expire timestamp if cookie exists. |
| 25 |
$client_expires_at = $this->cookie_detection->find($countdown_id); |
| 26 |
|
| 27 |
// If cookie is not created or deleted. |
| 28 |
if (is_null($client_expires_at)) { |
| 29 |
|
| 30 |
// Fallback to IP detection |
| 31 |
$result = $this->ip_detection->find($countdown_id); |
| 32 |
|
| 33 |
if ($result) { |
| 34 |
|
| 35 |
// Return IP `client_expires_at`. |
| 36 |
return $result['client_expires_at']; |
| 37 |
} |
| 38 |
|
| 39 |
// A new cookie will be created from client side |
| 40 |
// containing the expiration timestamp. |
| 41 |
return null; |
| 42 |
} |
| 43 |
|
| 44 |
$result = $this->ip_detection->find($countdown_id); |
| 45 |
|
| 46 |
if ($result) { |
| 47 |
|
| 48 |
// Update IP expiration timestamp. |
| 49 |
$this->ip_detection->update($result['id'], $client_expires_at); |
| 50 |
} else { |
| 51 |
|
| 52 |
// We create an IP entry. |
| 53 |
$this->ip_detection->create($countdown_id, $client_expires_at); |
| 54 |
} |
| 55 |
|
| 56 |
return $client_expires_at; |
| 57 |
} |
| 58 |
} |
| 59 |
|