request_arguments( $method ) ); if ( is_wp_error( $response ) ) { return new HttpAnswer( null, $first_status_code, $response->get_error_message(), $current_url ); } $status_code = (int) wp_remote_retrieve_response_code( $response ); $first_status_code ??= $status_code; $location = $this->location_of( $response ); if ( ! $this->is_redirect( $status_code, $location ) ) { return new HttpAnswer( $status_code, $first_status_code, '', $current_url ); } $current_url = $this->location_resolver->resolve( $current_url, $location ); } return new HttpAnswer( null, $first_status_code, self::TOO_MANY_REDIRECTS, $current_url ); } /** * @return array{ * method: string, * timeout: int, * redirection: int, * user-agent: string * } */ private function request_arguments( string $method ): array { return array( 'method' => $method, 'timeout' => self::TIMEOUT_SECONDS, 'redirection' => 0, 'user-agent' => PluginUserAgent::NAME, ); } /** @param array $response */ private function location_of( array $response ): string { $header = wp_remote_retrieve_header( $response, self::LOCATION_HEADER ); return implode( self::HEADER_SEPARATOR, array_filter( (array) $header, 'is_string' ) ); } private function is_redirect( int $status_code, string $location ): bool { return in_array( $status_code, self::REDIRECT_CODES, true ) && '' !== $location; } }