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(); $body = $this->json( $this->post( self::API_PING, $config ) ); if ( $this->error_name( $body ) === self::ERROR_INVALID_KEY && strpos( $this->message( $body ), self::MESSAGE_REJECTED ) !== false ) { $findings->add( $this->error( Code::AUTH_FAILED, 'api_key' ) ); } return $findings; } /** * Get Mandrill's own error name, if the body carries one. * * @since 4.10.0 * * @param array|null $body Decoded body. * * @return string */ private function error_name( $body ) { return isset( $body['name'] ) && is_string( $body['name'] ) ? $body['name'] : ''; } /** * Read Mandrill'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 = isset( $body['message'] ) ? $body['message'] : ''; return is_string( $message ) ? strtolower( $message ) : ''; } /** * Issue one bounded read. * * @since 4.10.0 * * @param string $url Absolute URL. * @param array $config Mailer options. * * @return array */ private function post( $url, $config ) { return $this->request( $url, [ 'method' => 'POST', 'headers' => [ 'Content-Type' => 'application/json' ], 'body' => wp_json_encode( [ 'key' => $config['api_key'] ] ), ] ); } }