| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\App; |
| 7 |
use FluentCart\App\Helpers\Helper; |
| 8 |
use FluentCart\Framework\Support\Str; |
| 9 |
|
| 10 |
class URL |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Append query parameters to a URI, ensuring proper formatting |
| 14 |
* |
| 15 |
* @param string $uri Base URI to append parameters to |
| 16 |
* @param array $params Query parameters to append |
| 17 |
* @return string URI with appended query parameters |
| 18 |
*/ |
| 19 |
public static function appendQueryParams(string $uri, $params = []): string |
| 20 |
{ |
| 21 |
// Ensure the URI ends with '/' if no query string and no trailing slash |
| 22 |
if (!Str::endsWith($uri, '/') && !Str::contains($uri, '?')) { |
| 23 |
$uri .= '/'; |
| 24 |
} |
| 25 |
|
| 26 |
return add_query_arg($params, $uri); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
public static function getFrontEndUrl($page = '', $params = []): string |
| 31 |
{ |
| 32 |
$baseUrl = site_url('/'); |
| 33 |
|
| 34 |
$baseParams = [ |
| 35 |
'fluent-cart' => $page |
| 36 |
]; |
| 37 |
|
| 38 |
// Merge base and custom params |
| 39 |
$allParams = is_array($params) |
| 40 |
? array_merge($baseParams, $params) |
| 41 |
: $baseParams; |
| 42 |
|
| 43 |
return self::appendQueryParams($baseUrl, $allParams); |
| 44 |
} |
| 45 |
|
| 46 |
public static function getApiUrl($path = '', $params = null): string |
| 47 |
{ |
| 48 |
$url = Str::of($path)->startsWith('/') ? |
| 49 |
Helper::getRestInfo()['url'] . $path : |
| 50 |
Helper::getRestInfo()['url'] . '/' . $path; |
| 51 |
|
| 52 |
if (is_array($params)) { |
| 53 |
return self::appendQueryParams($url, $params); |
| 54 |
} |
| 55 |
return $url; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
public static function getDashboardUrl(string $path, $params = null): string |
| 60 |
{ |
| 61 |
$url = admin_url('admin.php?page=fluent-cart#/'); |
| 62 |
|
| 63 |
if (is_array($params)) { |
| 64 |
return $url . self::appendQueryParams($path, $params); |
| 65 |
} |
| 66 |
return $url . $path; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
public static function getCustomerDashboardUrl($path): string |
| 71 |
{ |
| 72 |
$url = (new StoreSettings())->getCustomerProfilePage(); |
| 73 |
$url = rtrim($url, '/'); |
| 74 |
|
| 75 |
if ($path) { |
| 76 |
// add the extension with a leading slash |
| 77 |
$url .= '/' . rtrim($path, '/'); |
| 78 |
} |
| 79 |
|
| 80 |
return $url; |
| 81 |
} |
| 82 |
|
| 83 |
public static function getCustomerOrderUrl($uuid): string |
| 84 |
{ |
| 85 |
$storeSettings = new StoreSettings(); |
| 86 |
$customerPage = $storeSettings->getCustomerProfilePage(); |
| 87 |
if ($customerPage) { |
| 88 |
return site_url("/{$customerPage}/order/{$uuid}"); |
| 89 |
} else { |
| 90 |
return self::getFrontEndUrl(); |
| 91 |
} |
| 92 |
} |
| 93 |
} |