HelperService.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Infrastructure\WP\HelperService; |
| 9 | |
| 10 | /** |
| 11 | * Class HelperService |
| 12 | * |
| 13 | * @package AmeliaBooking\Infrastructure\WP\HelperService |
| 14 | */ |
| 15 | class HelperService |
| 16 | { |
| 17 | public static $jsVars = []; |
| 18 | |
| 19 | /** |
| 20 | * Determine whether the site is using SSL. |
| 21 | * |
| 22 | * Supports setups behind reverse proxies where SSL is terminated |
| 23 | * before traffic reaches WordPress/PHP. |
| 24 | */ |
| 25 | public static function isSSL(): bool |
| 26 | { |
| 27 | if (function_exists('is_ssl') && is_ssl()) { |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { |
| 32 | $forwardedProtos = explode(',', (string)$_SERVER['HTTP_X_FORWARDED_PROTO']); |
| 33 | if (in_array('https', $forwardedProtos)) { |
| 34 | return true; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if ( |
| 39 | function_exists('get_option') && |
| 40 | 'https' === parse_url((string)get_option('siteurl'), PHP_URL_SCHEME) |
| 41 | ) { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Helper method to add PHP vars to JS vars |
| 50 | * |
| 51 | * @param $varName |
| 52 | * @param $phpVar |
| 53 | */ |
| 54 | public static function exportJSVar($varName, $phpVar) |
| 55 | { |
| 56 | self::$jsVars[$varName] = $phpVar; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Helper method to print PHP vars to JS vars |
| 61 | */ |
| 62 | public static function printJSVars() |
| 63 | { |
| 64 | if (!empty(self::$jsVars)) { |
| 65 | $jsBlock = '<script type="text/javascript">'; |
| 66 | foreach (self::$jsVars as $varName => $jsVar) { |
| 67 | $jsBlock .= "var {$varName} = " . json_encode($jsVar) . ';'; |
| 68 | } |
| 69 | $jsBlock .= '</script>'; |
| 70 | echo $jsBlock; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param int $orderId |
| 76 | * |
| 77 | * @return string|null |
| 78 | */ |
| 79 | public static function getWooCommerceOrderUrl($orderId) |
| 80 | { |
| 81 | return get_edit_post_link($orderId, ''); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param int $orderId |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | public static function getWooCommerceOrderItemAmountValues($orderId) |
| 90 | { |
| 91 | $order = wc_get_order($orderId); |
| 92 | |
| 93 | $prices = []; |
| 94 | |
| 95 | if ($order) { |
| 96 | foreach ($order->get_items() as $itemId => $orderItem) { |
| 97 | $data = wc_get_order_item_meta($itemId, 'ameliabooking'); |
| 98 | |
| 99 | if ($data && is_array($data)) { |
| 100 | $prices[$itemId] = [ |
| 101 | 'coupon' => (float)round($orderItem->get_subtotal() - $orderItem->get_total(), 2), |
| 102 | 'tax' => !isset($data['taxIncluded']) || !$data['taxIncluded'] ? |
| 103 | (float)$orderItem->get_total_tax() : 0, |
| 104 | ]; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $prices; |
| 110 | } |
| 111 | } |
| 112 |