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 / IPDetection.php

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

261 lines 5.8 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 IPDetection
6 {
7 /**
8 * Evergreen IP entries table name.
9 *
10 * @var string
11 */
12 private $table;
13
14 /**
15 * Cleanup idle IP entries action name.
16 */
17 const CLEANUP_ACTION = "hurrytimer_evergreen_daily_cleanup";
18
19 public function __construct()
20 {
21 global $wpdb;
22 $this->table = "{$wpdb->prefix}hurrytimer_evergreen";
23 add_action(self::CLEANUP_ACTION, [$this, 'cleanup_idle_entries']);
24 }
25
26 /**
27 * Find IP log.
28 *
29 * @param int $countdown_id
30 * @param string $client_ip_address
31 *
32 * @return array|null
33 */
34 public function find($countdown_id, $client_ip_address = null)
35 {
36 global $wpdb;
37 if (is_null($client_ip_address)) {
38 $client_ip_address = Utils\Helpers::ip_address();
39 }
40
41 $sql = $wpdb->prepare(
42 "SELECT * FROM {$this->table}
43 WHERE countdown_id = %d
44 AND client_ip_address = %s",
45 $countdown_id,
46 $client_ip_address
47 );
48
49 $result = $wpdb->get_row($sql, ARRAY_A);
50
51 if ( ! $result) {
52 $result = $this->legacy_find($countdown_id, $client_ip_address);
53 }
54
55 return $result;
56 }
57
58 /**
59 * This provide compatibility for prior versions.
60 *
61 * @param int $countdown_id
62 * @param string $client_id_address
63 *
64 * @return array|null
65 *
66 */
67 private function legacy_find($countdown_id, $client_id_address)
68 {
69 $client_id_address_key = sanitize_key($client_id_address);
70
71 $transient = sprintf(
72 'ht_cdt_%d_%s',
73 $countdown_id,
74 $client_id_address_key
75 );
76
77 $expires_at_transient = get_transient($transient);
78
79 if ( ! $expires_at_transient) {
80 return null;
81 }
82
83 $result = $this->create(
84 $countdown_id,
85 $expires_at_transient,
86 $client_id_address
87 );
88 if ($result) {
89 // Clear transients.
90 delete_transient($transient);
91 delete_transient("{$transient}_status");
92
93 return $this->find($countdown_id, $client_id_address);
94 }
95
96 return null;
97 }
98
99 /**
100 * Create a new IP log.
101 *
102 * @param int $countdown_id
103 * @param int $client_expires_at
104 * @param string $client_ip_address
105 *
106 * @return bool
107 */
108 public function create(
109 $countdown_id,
110 $client_expires_at,
111 $client_ip_address = null
112 ) {
113 global $wpdb;
114
115 if (is_null($client_ip_address)) {
116 $client_ip_address = Utils\Helpers::ip_address();
117 }
118
119 // Auto-destroy after one month.
120 $destroy_at = Utils\Helpers::date_later(MONTH_IN_SECONDS);
121
122 $result = $wpdb->insert(
123 $this->table,
124 compact(
125 'countdown_id',
126 'client_ip_address',
127 'client_expires_at',
128 'destroy_at'
129 )
130 );
131
132 if ($result !== false) {
133 $this->maybe_schedule_destroy();
134
135 return true;
136 }
137
138 return false;
139 }
140
141 /**
142 *
143 * Update client expiration time.
144 *
145 * @param $id
146 * @param $client_expires_at
147 *
148 * @return false|int
149 */
150 public function update($id, $client_expires_at)
151 {
152 global $wpdb;
153
154 $destroy_at = Utils\Helpers::date_later(MONTH_IN_SECONDS);
155
156 return $wpdb->update(
157 $this->table,
158 compact('client_expires_at', 'destroy_at'),
159 compact('id')
160 );
161 }
162
163 /**
164 * Delete given countdown and IP entry.
165 *
166 * @param $campaignId
167 * @param null|string $ipAddress
168 *
169 * @return void
170 */
171 public function forget($campaignId, $ipAddress = null)
172 {
173 global $wpdb;
174 $where = [
175 'countdown_id' => $campaignId,
176 ];
177 if ($ipAddress !== null) {
178 $where['client_ip_address'] = $ipAddress;
179 }
180 $wpdb->delete($this->table, $where);
181 if ($ipAddress === null) {
182 if ( ! $this->has_entries()) {
183 $this->clear_scheduled_destroy();
184 }
185 }
186
187 }
188
189 function forgetAll($ipAddress = null)
190 {
191 global $wpdb;
192 if ($ipAddress !== null) {
193 $where['client_ip_address'] = $ipAddress;
194 $wpdb->delete($this->table, $where);
195 }
196 else{
197 $wpdb->query("delete from {$this->table}");
198 }
199 if ( ! $this->has_entries()) {
200 $this->clear_scheduled_destroy();
201 }
202 }
203
204 /**
205 * Clean up IP entries with passed `destroy_at`.
206 *
207 * @return void
208 */
209 private function cleanup_idle_entries()
210 {
211 global $wpdb;
212 $now = current_time('mysql');
213 $sql = "DELETE FROM {$this->table} WHERE destroy_at < $now";
214 $wpdb->query($sql);
215 }
216
217 /**
218 * Check if there is at least one IP entry.
219 *
220 * @return bool
221 */
222 private function has_entries()
223 {
224 global $wpdb;
225
226 $count = $wpdb->get_var("SELECT count(*) FROM {$this->table}");
227
228 if (is_null($count)) {
229 return false;
230 }
231
232 return $count > 0;
233 }
234
235 /**
236 * Schedule cleanup of IP entries with passed `destroy_at`.
237 *
238 * @return void
239 */
240 private function maybe_schedule_destroy()
241 {
242 if ( ! wp_next_scheduled(self::CLEANUP_ACTION)) {
243 wp_schedule_event(
244 current_time('timestamp'),
245 'daily',
246 self::CLEANUP_ACTION
247 );
248 }
249 }
250
251 /**
252 * Clear sheduled destroy event.
253 *
254 * @return void
255 */
256 private function clear_scheduled_destroy()
257 {
258 wp_clear_scheduled_hook(self::CLEANUP_ACTION);
259 }
260 }
261