PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.2.21
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.2.21
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 / EvergreenCampaign.php

EvergreenCampaign.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.2.21, at includes/EvergreenCampaign.php

151 lines 3.9 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 use http\Cookie;
6 use Hurrytimer\Utils\Helpers;
7
8 class EvergreenCampaign extends Campaign
9 {
10 /**
11 * @var CookieDetection
12 */
13 private $cookieDetection;
14
15 /**
16 * @var IPDetection
17 */
18 private $IPDetection;
19
20 const RESET_FLAG = '_hurrytimer_reset_compaign_flag';
21
22 public function __construct($id, $cookieDetection, $IPDetection)
23 {
24 parent::__construct($id);
25 $this->IPDetection = $IPDetection;
26 $this->cookieDetection = $cookieDetection;
27 }
28
29 /**
30 * Reset timer.
31 *
32 * @param string $scope
33 */
34
35 public function reset($scope = 'admin')
36 {
37 if ($scope === 'admin') {
38 $this->IPDetection->forget($this->get_id(), Helpers::ip_address());
39 } else {
40 $this->IPDetection->forget($this->get_id());
41 $this->resetBrowserCookie();
42 }
43 }
44
45 private function resetBrowserCookie()
46 {
47 $_cookieKey = CookieDetection::cookieName($this->get_id());
48 if (isset($_COOKIE[$_cookieKey])) {
49 unset($_COOKIE[$_cookieKey]);
50 }
51
52 setcookie($_cookieKey, '', time() - YEAR_IN_SECONDS);
53 // Force cookie deletion on next request.
54 update_post_meta($this->get_id(), self::RESET_FLAG, 1);
55 }
56
57 function resetAll($scope = 'admin')
58 {
59 if ($scope === 'admin') {
60 $this->IPDetection->forgetAll(Helpers::ip_address());
61 } else {
62 $this->IPDetection->forgetAll();
63 // Delete cookies
64 $campaigns = Helpers::getCampaigns();
65 foreach ($campaigns as $campaign) {
66 $_cookieKey = CookieDetection::cookieName($campaign->ID);
67 if (isset($_COOKIE[$_cookieKey])) {
68 unset($_COOKIE[$_cookieKey]);
69 }
70
71 setcookie($_cookieKey, '', time() - YEAR_IN_SECONDS);
72 // Force cookie deletion on next request.
73 update_post_meta($campaign->ID, self::RESET_FLAG, 1);
74 }
75 }
76 }
77
78 /**
79 * Returns client expiration time.
80 *
81 * @return int
82 */
83 public function getEndDate()
84 {
85 // Get expire timestamp if cookie exists.
86 $clientEndTimestamp = $this->cookieDetection->find($this->get_id());
87
88 // If cookie doesn't exist.
89 if (is_null($clientEndTimestamp)) {
90 // Fallback to IP detection
91 $result = $this->IPDetection->find($this->get_id());
92
93 if ($result) {
94 // Return IP `client_expires_at`.
95 return $result['client_expires_at'];
96 }
97
98 // A new cookie will be created from client side
99 // containing the expiration timestamp.
100 return null;
101 }
102
103 $result = $this->IPDetection->find($this->get_id());
104
105 if ($result) {
106 // Update IP expiration timestamp.
107 $this->IPDetection->update($result['id'], $clientEndTimestamp);
108 } else {
109 // We create an IP entry.
110 $this->IPDetection->create($this->get_id(), $clientEndTimestamp);
111 }
112
113 return $clientEndTimestamp;
114 }
115
116 function setEndDate($timestamp)
117 {
118 setcookie(CookieDetection::cookieName($this->get_id()), $timestamp,
119 time() + YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
120
121 return $this->getEndDate();
122 }
123
124 /**
125 * Returns true to force reset timer.
126 *
127 * @return bool
128 */
129 public function reseting()
130 {
131 $reset = filter_var(
132 get_post_meta($this->get_id(), self::RESET_FLAG, true),
133 FILTER_VALIDATE_BOOLEAN
134 );
135
136 delete_post_meta($this->get_id(), self::RESET_FLAG);
137
138 return $reset;
139 }
140
141 /**
142 * Returns given timer's cookie name.
143 *
144 * @return string
145 */
146 public function cookieName()
147 {
148 return CookieDetection::cookieName($this->get_id());
149 }
150 }
151