LineItem
1 week ago
Shipping
1 week ago
Hooks.php
1 week ago
Translator.php
1 week ago
TranslatorFactory.php
1 week ago
Hooks.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\OrderItems; |
| 4 | |
| 5 | use IWPML_Backend_Action; |
| 6 | use IWPML_DIC_Action; |
| 7 | use IWPML_Frontend_Action; |
| 8 | use SitePress; |
| 9 | use WCML\Rest\Functions; |
| 10 | use WCML\StandAlone\NullSitePress; |
| 11 | use WCML\Utilities\ActionScheduler; |
| 12 | use WPML\Core\ISitePress; |
| 13 | |
| 14 | class Hooks implements IWPML_Backend_Action, IWPML_Frontend_Action, IWPML_DIC_Action { |
| 15 | |
| 16 | private $sitepress; |
| 17 | |
| 18 | private $lineItemFactory; |
| 19 | |
| 20 | private $shippingFactory; |
| 21 | |
| 22 | public function __construct( |
| 23 | ISitePress $sitepress, |
| 24 | LineItem\Factory $lineItemFactory, |
| 25 | Shipping\Factory $shippingFactory |
| 26 | ) { |
| 27 | $this->sitepress = $sitepress; |
| 28 | $this->lineItemFactory = $lineItemFactory; |
| 29 | $this->shippingFactory = $shippingFactory; |
| 30 | } |
| 31 | |
| 32 | public function add_hooks() { |
| 33 | add_filter( 'woocommerce_order_get_items', [ $this, 'getOrderItems' ], 10, 2 ); |
| 34 | } |
| 35 | |
| 36 | public function getOrderItems( $items, $order ) { |
| 37 | if ( ! $items ) { |
| 38 | return $items; |
| 39 | } |
| 40 | |
| 41 | $shouldTranslateOrderItems = $this->shouldTranslateOrderItems(); |
| 42 | $shouldTranslateOrderItems = apply_filters( 'wcml_should_translate_order_items', $shouldTranslateOrderItems, $items, $order ); |
| 43 | if ( ! $shouldTranslateOrderItems ) { |
| 44 | return $items; |
| 45 | } |
| 46 | |
| 47 | $targetLanguage = $this->getTargetLanguage( $order ); |
| 48 | if ( ! $targetLanguage ) { |
| 49 | return $items; |
| 50 | } |
| 51 | |
| 52 | $this->translateOrderItems( $items, $targetLanguage ); |
| 53 | |
| 54 | return $items; |
| 55 | } |
| 56 | |
| 57 | private function shouldTranslateOrderItems() { |
| 58 | if ( ActionScheduler::isWcRunningFromAsyncActionScheduler() ) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | if ( |
| 63 | is_admin() || |
| 64 | is_view_order_page() || |
| 65 | is_order_received_page() || |
| 66 | Functions::isRestApiRequest() |
| 67 | ) { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | private function getTargetLanguage( $order ) { |
| 75 | if ( $this->isOrderEditPage() ) { |
| 76 | $language = $this->sitepress->get_user_admin_language( get_current_user_id(), true ); |
| 77 | } elseif ( $this->isOrderActionTriggeredForCustomer() ) { |
| 78 | $language = \WCML_Orders::getLanguage( $order->get_id() ) ?: $this->sitepress->get_default_language(); |
| 79 | } else { |
| 80 | $language = $this->sitepress->get_current_language(); |
| 81 | } |
| 82 | |
| 83 | return apply_filters( 'wcml_get_order_items_language', $language, $order ); |
| 84 | } |
| 85 | |
| 86 | private function isOrderEditPage() { |
| 87 | return ( \WCML\COT\Helper::isOrderEditAdminScreen() && empty( $_POST ) ) || ( isset( $_GET['post'] ) && 'shop_order' === get_post_type( $_GET['post'] ) ); |
| 88 | } |
| 89 | |
| 90 | private function isOrderActionTriggeredForCustomer() { |
| 91 | return isset( $_GET['action'] ) && wpml_collect( |
| 92 | [ 'woocommerce_mark_order_complete', 'woocommerce_mark_order_status', 'mark_processing' ] |
| 93 | )->contains( $_GET['action'] ); |
| 94 | } |
| 95 | |
| 96 | private function translateOrderItems( $items, $targetLanguage ) { |
| 97 | |
| 98 | if ( ! $targetLanguage ) { |
| 99 | $targetLanguage = $this->sitepress->get_current_language(); |
| 100 | } |
| 101 | |
| 102 | foreach ( $items as $item ) { |
| 103 | if ( $item instanceof \WC_Order_Item_Product ) { |
| 104 | $this->translateLineItem( $item, $targetLanguage ); |
| 105 | } elseif ( $item instanceof \WC_Order_Item_Shipping ) { |
| 106 | $this->translateShipping( $item, $targetLanguage ); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private function translateLineItem( $item, $targetLanguage ) { |
| 112 | $translator = $this->lineItemFactory->getTranslator( $item ); |
| 113 | if ( null === $translator ) { |
| 114 | return; |
| 115 | } |
| 116 | $translator->translateItem( $item, $targetLanguage ); |
| 117 | } |
| 118 | |
| 119 | private function translateShipping( $item, $targetLanguage ) { |
| 120 | $translator = $this->shippingFactory->getTranslator( $item ); |
| 121 | if ( null === $translator ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | $translator->translateItem( $item, $targetLanguage ); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 |