decrypt( $stored ); return null === $plain ? '' : $plain; } /** * Whether a key is stored but cannot be decrypted (the site's salts changed). * * @return bool */ public static function is_unreadable() { $stored = get_option( self::OPTION, '' ); return '' !== $stored && null === SecretStore::for_site()->decrypt( $stored ); } /** * Stores a key, encrypted. * * @param string $api_key API key. * @return void */ public static function save( $api_key ) { update_option( self::OPTION, SecretStore::for_site()->encrypt( $api_key ), false ); } /** * Removes the stored key. * * @return void */ public static function delete() { delete_option( self::OPTION ); } /** * Cleans up user input: trims it and rejects anything containing whitespace. * * @param string $input Raw input. * @return string The key, or '' when the input is not a plausible key. */ public static function normalize( $input ) { $key = trim( (string) $input ); if ( '' === $key || strlen( $key ) > 200 || preg_match( '/\s/', $key ) ) { return ''; } return $key; } /** * Masked form for display, e.g. "abs_12••••••••ab34". * * @param string $api_key API key. * @return string */ public static function mask( $api_key ) { $api_key = (string) $api_key; if ( strlen( $api_key ) < 12 ) { return '••••••••'; } return substr( $api_key, 0, 6 ) . '••••••••' . substr( $api_key, -4 ); } }