1 | <?php |
||
13 | class Nonce_Handler { |
||
14 | |||
15 | /** |
||
16 | * How many nonces should be removed during each run of the runtime cleanup. |
||
17 | * Can be modified using the filter `jetpack_connection_nonce_cleanup_runtime_limit`. |
||
18 | */ |
||
19 | const CLEANUP_RUNTIME_LIMIT = 10; |
||
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 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private static $nonces_used_this_request = array(); |
||
37 | |||
38 | /** |
||
39 | * Adds a used nonce to a list of known nonces. |
||
40 | * |
||
41 | * @param int $timestamp the current request timestamp. |
||
42 | * @param string $nonce the nonce value. |
||
43 | * @param bool $run_cleanup Whether to run the `cleanup_runtime()`. |
||
44 | * |
||
45 | * @return bool whether the nonce is unique or not. |
||
46 | */ |
||
47 | public static function add( $timestamp, $nonce, $run_cleanup = true ) { |
||
98 | |||
99 | /** |
||
100 | * Removing [almost] all the nonces. |
||
101 | * Capped at 20 seconds to avoid breaking the site. |
||
102 | * |
||
103 | * @param int $cutoff_timestamp All nonces added before this timestamp will be removed. |
||
104 | * |
||
105 | * @return true |
||
106 | */ |
||
107 | public static function clean_all( $cutoff_timestamp = PHP_INT_MAX ) { |
||
108 | // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed |
||
109 | for ( $end_time = time() + 20; time() < $end_time; ) { |
||
110 | $result = static::delete( static::CLEAN_ALL_LIMIT_PER_BATCH, $cutoff_timestamp ); |
||
111 | |||
112 | if ( ! $result ) { |
||
113 | break; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | return true; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Clean up the expired nonces on shutdown. |
||
122 | * |
||
123 | * @return bool True if the cleanup query has been run, false if the table is locked. |
||
124 | */ |
||
125 | public static function clean_runtime() { |
||
146 | |||
147 | |||
148 | /** |
||
149 | * Delete the nonces. |
||
150 | * |
||
151 | * @param int $limit How many nonces to delete. |
||
152 | * @param null|int $cutoff_timestamp All nonces added before this timestamp will be removed. |
||
153 | * |
||
154 | * @return int|false Number of removed nonces, or `false` if nothing to remove (or in case of a database error). |
||
155 | */ |
||
156 | public static function delete( $limit = 10, $cutoff_timestamp = null ) { |
||
180 | |||
181 | /** |
||
182 | * Clean the cached nonces valid during the current request, therefore making them invalid. |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | public static function invalidate_request_nonces() { |
||
191 | |||
192 | /** |
||
193 | * Check if the options table is locked. |
||
194 | * Subject to race condition, the table may appear locked when a fast database query is performing. |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | protected static function is_table_locked() { |
||
205 | |||
206 | } |
||
207 |
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: