$body JSON body. */ public function postUnsigned( string $path, array $body ): HubResponse { return $this->send( 'POST', $path, (string) wp_json_encode( $body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ), null, 0 ); } /** * Signed request. * * @param string $method HTTP method. * @param string $path Path under /api/ingest/v1/. * @param string $json Body, already encoded; empty for GET. * @param Credentials $credentials Store id and key. * @param int $clockOffset Seconds to add to the local clock. */ public function signed( string $method, string $path, string $json, Credentials $credentials, int $clockOffset ): HubResponse { return $this->send( $method, $path, $json, $credentials, $clockOffset ); } /** * Perform a request. * * @param string $method HTTP method. * @param string $path Path under /api/ingest/v1/. * @param string $json Body. * @param Credentials|null $credentials Sign with these, when given. * @param int $clockOffset Seconds to add to the local clock. */ private function send( string $method, string $path, string $json, ?Credentials $credentials, int $clockOffset ): HubResponse { if ( ! self::urlIsAllowed() ) { return HubResponse::transportError( __( 'آدرس هاب باید HTTPS باشد.', 'rahkar-toolkit' ) ); } $headers = [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-Agent' => 'RahkarToolkit/' . RAHKAR_WP_VERSION . '; ' . home_url(), ]; $body = $json; if ( strlen( $body ) >= self::GZIP_FROM && function_exists( 'gzencode' ) ) { $compressed = gzencode( $body, 6 ); if ( false !== $compressed ) { $body = $compressed; $headers['Content-Encoding'] = 'gzip'; } } if ( null !== $credentials ) { $timestamp = (string) ( time() + $clockOffset ); $headers['X-RF-Store'] = $credentials->storeId; $headers['X-RF-Timestamp'] = $timestamp; $headers['X-RF-Signature'] = 'v1=' . hash_hmac( 'sha256', $timestamp . '.' . $body, $credentials->secret ); } $args = [ 'method' => $method, 'timeout' => 15, 'redirection' => 0, 'headers' => $headers, ]; if ( 'GET' !== $method ) { $args['body'] = $body; } $response = wp_remote_request( self::baseUrl() . '/api/ingest/v1/' . ltrim( $path, '/' ), $args ); if ( is_wp_error( $response ) ) { return HubResponse::transportError( $response->get_error_message() ); } $decoded = json_decode( (string) wp_remote_retrieve_body( $response ), true ); return new HubResponse( (int) wp_remote_retrieve_response_code( $response ), is_array( $decoded ) ? $decoded : [], (int) wp_remote_retrieve_header( $response, 'retry-after' ) ); } }