$method, 'timeout' => 20, 'headers' => array_merge( array( 'Authorization' => 'Bearer ' . self::option( 'key_id' ) . ':' . self::option( 'secret' ), 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-Agent' => 'MarkitMall-WooCommerce/' . MARKITMALL_DS_VERSION . '; ' . home_url(), ), $headers ), ); if ( null !== $body ) { $args['body'] = wp_json_encode( $body ); } $response = wp_remote_request( self::base_url() . $path, $args ); if ( is_wp_error( $response ) ) { return new WP_Error( 'network_error', sprintf( /* translators: %s: error from WordPress HTTP API */ __( 'Could not reach MarkitMall (%s). If your host blocks outgoing connections, ask them to allow backend.markitmall.com.', 'markitmall-dropship' ), $response->get_error_message() ) ); } $status = (int) wp_remote_retrieve_response_code( $response ); $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $status >= 200 && $status < 300 ) { return is_array( $data ) ? $data : array(); } $code = isset( $data['error']['code'] ) ? $data['error']['code'] : 'http_' . $status; $message = isset( $data['error']['message'] ) ? $data['error']['message'] : sprintf( 'HTTP %d', $status ); $error = new WP_Error( $code, $message, array( 'status' => $status, 'details' => isset( $data['error']['details'] ) ? $data['error']['details'] : null ) ); if ( 429 === $status ) { $error->add_data( array( 'retry_after' => (int) wp_remote_retrieve_header( $response, 'retry-after' ) ) ); } return $error; } public static function get( $path ) { return self::request( 'GET', $path ); } public static function post( $path, $body, $headers = array() ) { return self::request( 'POST', $path, $body, $headers ); } public static function put( $path, $body ) { return self::request( 'PUT', $path, $body ); } public static function delete( $path ) { return self::request( 'DELETE', $path ); } }