sanitize_text_field( (string) ( $config['api_key'] ?? '' ) ), ]; if ( $sanitized_config['api_key'] === '' ) { return null; } return $sanitized_config; } /** * Run the check. * * @since 4.10.0 * * @param array $config Mailer options. * * @return Findings */ protected function probe( $config ) { $findings = Findings::none(); $response = $this->request( self::API_ACCOUNT, [ 'headers' => [ 'api-key' => $config['api_key'] ] ] ); $body = $this->json( $response ); // The code is the envelope marker rather than the verdict: Brevo answers `unauthorized` for a // wrong key, a disabled key, an absent header and an unrecognised source IP alike. if ( $this->error_code( $body ) === 'unauthorized' && $this->refuses_credential( $this->message( $body ) ) ) { $findings->add( $this->error( Code::AUTH_FAILED, 'api_key' ) ); } return $findings; } /** * Read Brevo's own error prose, lowercased for matching. * * @since 4.10.0 * * @param array|null $body Decoded body. * * @return string Empty when the body carried no message. */ private function message( $body ) { $message = $body['message'] ?? ''; return is_string( $message ) ? strtolower( $message ) : ''; } /** * Whether the message refuses the credential. * * The unrecognised-IP message names the address it saw, so a fragment is what stays stable. * * @since 4.10.0 * * @param string $message Lowercased message. * * @return bool */ private function refuses_credential( $message ) { foreach ( self::MESSAGES_REJECTED as $fragment ) { if ( strpos( $message, $fragment ) !== false ) { return true; } } return false; } /** * Get Brevo's own error code, if the body carries one. * * @since 4.10.0 * * @param array|null $body Decoded body. * * @return string */ private function error_code( $body ) { $code = $body['code'] ?? ''; return is_string( $code ) ? $code : ''; } }