$m[1], 'expires' => (int) $m[2], ); } public static function build_value( $token, $expires ) { return $token . '|' . (int) $expires; } /** Unparseable values count as expired (stale garbage must not block scans forever). */ public static function is_expired_value( $value, $now ) { $parsed = self::parse_value( $value ); return ! $parsed || $parsed['expires'] <= $now; } private static function new_token() { return function_exists( 'wp_generate_password' ) ? wp_generate_password( 24, false ) : bin2hex( random_bytes( 12 ) ); } /** * Atomic insert: succeeds only when no row with this option_name exists. * * @param string $key Option name. * @param string $value Lock value. * @return bool */ private static function insert( $key, $value ) { global $wpdb; $rows = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no')", $key, $value ) ); self::flush( $key ); return 1 === (int) $rows; } /** * Read the lock value straight from the database (never from a possibly stale object cache). * * @param string $key Option name. * @return string */ private static function current( $key ) { global $wpdb; return (string) $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", $key ) ); } /** * Delete the lock row only if it still has this exact value. * * @param string $key Option name. * @param string $value Expected value. * @return bool */ private static function delete_exact( $key, $value ) { global $wpdb; $rows = $wpdb->delete( $wpdb->options, array( 'option_name' => $key, 'option_value' => $value, ) ); self::flush( $key ); return (int) $rows > 0; } /** * Clear option caches for the lock row. * * @param string $key Option name. */ private static function flush( $key ) { wp_cache_delete( $key, 'options' ); wp_cache_delete( 'notoptions', 'options' ); wp_cache_delete( 'alloptions', 'options' ); } }