PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.1.7
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.1.7
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.1.7, at includes/IPDetection.php

238 lines 5.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 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 $countdown_id
167 *
168 * @return void
169 */
170 public function forget($countdown_id)
171 {
172 global $wpdb;
173
174 $delete = $wpdb->delete($this->table, compact('countdown_id'));
175
176 if ( ! $this->has_entries()) {
177 $this->clear_scheduled_destroy();
178 }
179 }
180
181 /**
182 * Clean up IP entries with passed `destroy_at`.
183 *
184 * @return void
185 */
186 private function cleanup_idle_entries()
187 {
188 global $wpdb;
189 $now = current_time('mysql');
190 $sql = "DELETE FROM {$this->table} WHERE destroy_at < $now";
191 $wpdb->query($sql);
192 }
193
194 /**
195 * Check if there is at least one IP entry.
196 *
197 * @return bool
198 */
199 private function has_entries()
200 {
201 global $wpdb;
202
203 $count = $wpdb->get_var("SELECT count(*) FROM {$this->table}");
204
205 if (is_null($count)) {
206 return false;
207 }
208
209 return $count > 0;
210 }
211
212 /**
213 * Schedule cleanup of IP entries with passed `destroy_at`.
214 *
215 * @return void
216 */
217 private function maybe_schedule_destroy()
218 {
219 if ( ! wp_next_scheduled(self::CLEANUP_ACTION)) {
220 wp_schedule_event(
221 current_time('timestamp'),
222 'daily',
223 self::CLEANUP_ACTION
224 );
225 }
226 }
227
228 /**
229 * Clear sheduled destroy event.
230 *
231 * @return void
232 */
233 private function clear_scheduled_destroy()
234 {
235 wp_clear_scheduled_hook(self::CLEANUP_ACTION);
236 }
237 }
238