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_PERMISSIONS, $config ) ); // Every refusal this endpoint issues is JSON carrying `data.error_code`. $code = isset( $body['data']['error_code'] ) ? (string) $body['data']['error_code'] : ''; if ( in_array( $code, self::CODES_REJECTED, true ) ) { $findings->add( $this->error( Code::AUTH_FAILED, 'api_key' ) ); } return $findings; } /** * Issue one bounded read. * * @since 4.10.0 * * @param string $url Absolute URL. * @param array $config Mailer options. * @param array $payload Request body. * * @return array */ private function post( $url, $config, $payload = [] ) { return $this->request( $url, [ 'method' => 'POST', 'headers' => [ 'X-Smtp2go-Api-Key' => $config['api_key'], 'Content-Type' => 'application/json', ], 'body' => wp_json_encode( (object) $payload ), ] ); } }