'woocommerce', 'siteUrl' => home_url(), 'email' => $email ), false ); if ( is_wp_error( $res ) ) { return $res; } update_option( self::OPTION_KEY, $res['apiKey'], false ); return $res; } /** * @return array|WP_Error Decoded JSON, or an error with the API's message. */ public static function request( $method, $path, $body = null, $auth = true ) { $headers = array( 'Accept' => 'application/json' ); if ( $auth ) { if ( ! self::connected() ) { return new WP_Error( 'peppolkit_not_connected', __( 'PeppolKit is not connected yet.', 'peppolkit' ) ); } $headers['Authorization'] = 'Bearer ' . self::key(); } $args = array( 'method' => $method, 'timeout' => 45, 'headers' => $headers, ); if ( null !== $body ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode( $body ); } $res = wp_remote_request( PEPPOLKIT_API_URL . $path, $args ); if ( is_wp_error( $res ) ) { return $res; } $code = wp_remote_retrieve_response_code( $res ); $data = json_decode( wp_remote_retrieve_body( $res ), true ); if ( $code >= 400 ) { $msg = is_array( $data ) ? ( isset( $data['errors'] ) ? implode( '; ', (array) $data['errors'] ) : ( $data['error'] ?? '' ) ) : ''; return new WP_Error( 'peppolkit_http_' . $code, $msg ? $msg : sprintf( 'HTTP %d', $code ), $data ); } return is_array( $data ) ? $data : array(); } }