1 | <?php |
||
13 | class Nonce_Handler { |
||
14 | |||
15 | /** |
||
16 | * How long the scheduled cleanup can run (in seconds). |
||
17 | * Can be modified using the filter `jetpack_connection_nonce_scheduled_cleanup_limit`. |
||
18 | */ |
||
19 | const SCHEDULED_CLEANUP_TIME_LIMIT = 5; |
||
20 | |||
21 | /** |
||
22 | * How many nonces should be removed per batch during the `clean_all()` run. |
||
23 | */ |
||
24 | const CLEAN_ALL_LIMIT_PER_BATCH = 1000; |
||
25 | |||
26 | /** |
||
27 | * Nonce lifetime in seconds. |
||
28 | */ |
||
29 | const LIFETIME = HOUR_IN_SECONDS; |
||
30 | |||
31 | /** |
||
32 | * The nonces used during the request are stored here to keep them valid. |
||
33 | * The property is static to keep the nonces accessible between the `Nonce_Handler` instances. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private static $nonces_used_this_request = array(); |
||
38 | |||
39 | /** |
||
40 | * The database object. |
||
41 | * |
||
42 | * @var \wpdb |
||
43 | */ |
||
44 | private $db; |
||
45 | |||
46 | /** |
||
47 | * Initializing the object. |
||
48 | */ |
||
49 | public function __construct() { |
||
54 | |||
55 | /** |
||
56 | * Scheduling the WP-cron cleanup event. |
||
57 | */ |
||
58 | public function init_schedule() { |
||
64 | |||
65 | /** |
||
66 | * Reschedule the WP-cron cleanup event to make it start sooner. |
||
67 | */ |
||
68 | public function reschedule() { |
||
72 | |||
73 | /** |
||
74 | * Adds a used nonce to a list of known nonces. |
||
75 | * |
||
76 | * @param int $timestamp the current request timestamp. |
||
77 | * @param string $nonce the nonce value. |
||
78 | * |
||
79 | * @return bool whether the nonce is unique or not. |
||
80 | */ |
||
81 | public function add( $timestamp, $nonce ) { |
||
119 | |||
120 | /** |
||
121 | * Removing all existing nonces, or at least as many as possible. |
||
122 | * Capped at 20 seconds to avoid breaking the site. |
||
123 | * |
||
124 | * @param int $cutoff_timestamp All nonces added before this timestamp will be removed. |
||
125 | * @param int $time_limit How long the cleanup can run (in seconds). |
||
126 | * |
||
127 | * @return true |
||
128 | */ |
||
129 | public function clean_all( $cutoff_timestamp = PHP_INT_MAX, $time_limit = 20 ) { |
||
141 | |||
142 | /** |
||
143 | * Scheduled clean up of the expired nonces. |
||
144 | */ |
||
145 | public static function clean_scheduled() { |
||
157 | |||
158 | /** |
||
159 | * Delete the nonces. |
||
160 | * |
||
161 | * @param int $limit How many nonces to delete. |
||
162 | * @param null|int $cutoff_timestamp All nonces added before this timestamp will be removed. |
||
163 | * |
||
164 | * @return int|false Number of removed nonces, or `false` if nothing to remove (or in case of a database error). |
||
165 | */ |
||
166 | public function delete( $limit = 10, $cutoff_timestamp = null ) { |
||
190 | |||
191 | /** |
||
192 | * Clean the cached nonces valid during the current request, therefore making them invalid. |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | public static function invalidate_request_nonces() { |
||
201 | |||
202 | } |
||
203 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: