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