sanitize_text_field( (string) ( $config['api_key'] ?? '' ) ), 'channel' => sanitize_text_field( (string) ( $config['channel'] ?? '' ) ), ]; 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(); $channel = $config['channel']; // An empty name leaves the trailing slash that reaches the channel list, which answers 200. if ( $channel === '' ) { $findings->add( $this->error( self::CHANNEL_NOT_FOUND, 'channel' ) ); return $findings; } $body = $this->json( $this->fetch_channel( $config, $channel ) ); if ( $this->fault( $body, 'api_key' ) === self::MESSAGE_KEY_INVALID ) { $findings->add( $this->error( Code::AUTH_FAILED, 'api_key' ) ); return $findings; } // A path we built wrong answers the same 404, but under `url`, so only this one is the channel. if ( $this->fault( $body, 'error' ) === self::MESSAGE_CHANNEL_MISSING ) { $findings->add( $this->error( self::CHANNEL_NOT_FOUND, 'channel' ) ); } return $findings; } /** * Read one member of SMTP.com's `data` object. * * There is no error code anywhere in the envelope, so the member name says which fault it is and * the value says whether it is that fault. * * @since 4.10.0 * * @param array|null $body Decoded body. * @param string $name Member to read. * * @return string Empty when the body carries no such member. */ private function fault( $body, $name ) { $value = isset( $body['data'][ $name ] ) ? $body['data'][ $name ] : ''; return is_string( $value ) ? $value : ''; } /** * Read the channel details. * * @since 4.10.0 * * @param array $config Mailer options. * @param string $channel Trimmed channel name. * * @return array */ private function fetch_channel( $config, $channel ) { return $this->request( self::API_CHANNEL . rawurlencode( $channel ), [ 'headers' => [ 'Authorization' => 'Bearer ' . $config['api_key'] ] ] ); } }