PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 1.2.3
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v1.2.3
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 / class-ip-detection.php

class-ip-detection.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 1.2.3, at includes/class-ip-detection.php

206 lines 4.7 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 IP_Detection
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, [__CLASS__, '_cleanup_idle_entries']);
24
25 }
26
27 /**
28 * Get IP log.
29 *
30 * @param int $countdown_id
31 * @param string $client_ip_address
32 *
33 * @return array
34 */
35 public function find($countdown_id, $client_ip_address = null)
36 {
37 global $wpdb;
38 if (is_null($client_ip_address)) {
39 $client_ip_address = Helper::ip_address();
40 }
41 $sql = $wpdb->prepare(implode(' ', [
42 "SELECT * FROM {$this->table}",
43 "WHERE countdown_id = %d",
44 "AND client_ip_address = %s",
45 ]),
46 $countdown_id, $client_ip_address);
47 $result = $wpdb->get_row($sql, ARRAY_A);
48 if (!$result) {
49 $result = $this->_find_compat($countdown_id, $client_ip_address);
50 }
51
52 return $result;
53 }
54
55 /**
56 * This provide compatibility for prior versions.
57 *
58 * @param int $countdown_id
59 * @param string $client_id_address
60 *
61 * @return array
62 *
63 */
64 public function _find_compat($countdown_id, $client_id_address)
65 {
66 $client_id_address_key = sanitize_key($client_id_address);
67 $transient = sprintf(
68 'ht_cdt_%d_%s',
69 $countdown_id,
70 $client_id_address_key);
71
72 $expires_at_transient = get_transient($transient);
73 if (!$expires_at_transient) {
74 return null;
75 }
76 $created = $this->create($countdown_id, $expires_at_transient, $client_id_address);
77 if ($created) {
78 delete_transient($transient);
79 delete_transient("{$transient}_status");
80 }
81
82 return $this->find($countdown_id, $client_id_address);
83 }
84
85 /**
86 * Create a new IP log.
87 *
88 * @param int $countdown_id
89 * @param int $client_expires_at
90 * @param string $client_ip_address
91 *
92 * @return void
93 */
94 public function create(
95 $countdown_id,
96 $client_expires_at,
97 $client_ip_address = null
98 ) {
99 global $wpdb;
100
101 if (is_null($client_ip_address)) {
102 $client_ip_address = Helper::ip_address();
103 }
104
105 // Set an auto-destroy datetime.
106 $destroy_at = $this->_destroy_at();
107
108 $wpdb->insert($this->table, compact(
109 'countdown_id',
110 'client_ip_address',
111 'client_expires_at',
112 'destroy_at'
113 ));
114
115 $this->_maybe_schedule_destroy();
116 }
117
118 /**
119 * @param $id
120 * @param $client_expires_at
121 *
122 * @return false|int
123 */
124 public function update($id, $client_expires_at)
125 {
126 global $wpdb;
127
128 $destroy_at = $this->_destroy_at();
129
130 return $wpdb->update($this->table,
131 compact('client_expires_at', 'destroy_at'),
132 compact('id'));
133 }
134
135 /**
136 * Delete given countdown and IP entry.
137 *
138 * @param $countdown_id
139 * @param null $client_ip_address
140 */
141 public function forget($countdown_id)
142 {
143 global $wpdb;
144
145 $delete = $wpdb->delete($this->table,
146 compact('countdown_id'));
147
148 if (!$this->_has_entries()) {
149 $this->_cancel_schedule_destroy();
150 }
151 }
152
153 /**
154 * IP entry TTL date.
155 *
156 * @return false|string
157 */
158 private function _destroy_at()
159 {
160 $now = current_time('mysql');
161
162 return date('Y-m-d H:i:s', strtotime($now) + MONTH_IN_SECONDS);
163 }
164
165 /**
166 * Clean up IP entries with passed `destroy_at`.
167 */
168 private function _cleanup_idle_entries()
169 {
170 global $wpdb;
171 $now = current_time('mysql');
172 $sql = "DELETE FROM {$this->table} WHERE destroy_at < $now";
173 $wpdb->query($sql);
174 }
175
176 /**
177 * Check if there is at least one IP entry.
178 *
179 * @return bool
180 */
181 private function _has_entries()
182 {
183 global $wpdb;
184
185 return $wpdb->get_var("SELECT count(*) FROM {$this->table}") > 0;
186 }
187
188 /**
189 * Schedule cleanup of IP entries with passed `destroy_at`.
190 */
191 private function _maybe_schedule_destroy()
192 {
193
194 if (!wp_next_scheduled(self::CLEANUP_ACTION)) {
195 wp_schedule_event(current_time('timestamp'), 'daily',
196 self::CLEANUP_ACTION);
197 }
198 }
199
200 private function _cancel_schedule_destroy()
201 {
202 wp_clear_scheduled_hook(self::CLEANUP_ACTION);
203 }
204
205 }
206