PluginProbe ʕ •ᴥ•ʔ
WPML Multilingual & Multicurrency for WooCommerce / 5.5.7
WPML Multilingual & Multicurrency for WooCommerce v5.5.7
5.5.7 5.3.8 5.3.9 5.4.3 5.4.4 5.4.5 5.5.1 5.5.1.1 5.5.2.2 5.5.2.3 5.5.3 5.5.3.1 5.5.4 5.5.5 5.5.6 trunk 0.9 1.0 1.1 1.2 1.3 1.4 1.5 2.0 2.2 2.3 2.3.1 2.3.2 3.0 3.0.1 3.1 3.2 3.2.1 3.2.2 3.3 3.3.1 3.3.2 3.3.3 3.3.4 3.4 3.4.1 3.4.2 3.4.3 3.5 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.6 3.6.1 3.6.10 3.6.11 3.6.2 3.6.3 3.6.4 3.6.5 3.6.5.1 3.6.6 3.6.7 3.6.8 3.6.9 3.7 3.7.1 3.7.10 3.7.11 3.7.12 3.7.13 3.7.14 3.7.15 3.7.16 3.7.2 3.7.3 3.7.4 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.9.0 3.9.1 3.9.1.1 3.9.2 3.9.3 3.9.4 3.9.5 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.10.4 4.11.2 4.11.3.2 4.11.5 4.11.6 4.12.1 4.12.4 4.12.5 4.12.6 4.2.0 4.2.0.1 4.2.1 4.2.1.1 4.2.10 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.7.1 4.2.8 4.2.8.1 4.2.9 4.3.0 4.3.1 4.3.2 4.3.2.1 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0 4.4.1 4.4.2 4.4.2.1 4.5.0 4.6.0 4.6.1 4.6.2 4.6.2.1 4.6.3 4.6.5 4.6.6 4.6.7 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / inc / class-wcml-emails.php
woocommerce-multilingual / inc Last commit date
admin-menus 2 weeks ago currencies 2 weeks ago template-classes 2 weeks ago translation-editor 2 weeks ago class-wcml-ajax-setup.php 2 weeks ago class-wcml-attributes.php 2 weeks ago class-wcml-capabilities.php 2 weeks ago class-wcml-cart-removed-items-widget.php 1 year ago class-wcml-cart-switch-lang-functions.php 2 weeks ago class-wcml-cart-sync-warnings.php 2 weeks ago class-wcml-cart.php 2 weeks ago class-wcml-comments.php 2 weeks ago class-wcml-compatibility.php 2 weeks ago class-wcml-coupons.php 2 weeks ago class-wcml-dependencies.php 2 weeks ago class-wcml-emails.php 2 weeks ago class-wcml-endpoints.php 2 weeks ago class-wcml-install.php 2 weeks ago class-wcml-languages-upgrader.php 2 weeks ago class-wcml-locale.php 2 weeks ago class-wcml-orders.php 2 weeks ago class-wcml-products-screen-options.php 2 weeks ago class-wcml-products.php 2 weeks ago class-wcml-reports.php 2 weeks ago class-wcml-requests.php 2 weeks ago class-wcml-resources.php 2 weeks ago class-wcml-store-pages.php 2 weeks ago class-wcml-terms.php 2 weeks ago class-wcml-tp-support.php 2 weeks ago class-wcml-troubleshooting.php 2 weeks ago class-wcml-upgrade.php 2 weeks ago class-wcml-url-translation.php 2 weeks ago class-wcml-wc-gateways.php 2 weeks ago class-wcml-wc-shipping.php 2 weeks ago class-wcml-wc-strings.php 2 weeks ago class-wcml-widgets.php 2 weeks ago constants.php 2 weeks ago functions-pure.php 2 weeks ago functions.php 2 weeks ago installer-loader.php 2 weeks ago missing-php-functions.php 2 weeks ago wcml-core-functions.php 2 weeks ago wcml-switch-lang-request.php 2 weeks ago
class-wcml-emails.php
715 lines
1 <?php
2
3 use WPML\FP\Obj;
4
5 class WCML_Emails {
6
7 const PRIORITY_AFTER_STATUS_CHANGE_EMAIL = 11;
8 const PRIORITY_BEFORE_EMAIL_SET_LANGUAGE = 9;
9
10 private $order_id = false;
11
12 private $locale = false;
13
14 private $admin_language = false;
15
16 private $rest_language;
17
18 private $wcmlStrings;
19
20 private $sitepress;
21
22 private $woocommerce;
23
24 public function __construct( WCML_WC_Strings $wcmlStrings, SitePress $sitepress, WooCommerce $woocommerce ) {
25 $this->wcmlStrings = $wcmlStrings;
26 $this->sitepress = $sitepress;
27 $this->woocommerce = $woocommerce;
28 }
29
30 public function add_hooks() {
31 if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
32 add_action(
33 'woocommerce_order_status_completed_notification',
34 [
35 $this,
36 'email_heading_completed',
37 ],
38 self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE
39 );
40 add_action( 'woocommerce_order_status_changed', [ $this, 'comments_language' ], 10 );
41
42 $this->add_hooks_to_restore_language_for_admin_notes();
43 }
44
45 add_action( 'woocommerce_new_customer_note_notification', [ $this, 'email_heading_note' ], self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE );
46
47 add_action( 'wp_ajax_woocommerce_mark_order_status', [ $this, 'email_refresh_in_ajax' ], self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE );
48
49 foreach ( [ 'pending', 'failed', 'cancelled', 'on-hold' ] as $state ) {
50 add_action(
51 'woocommerce_order_status_' . $state . '_to_processing_notification',
52 [
53 $this,
54 'email_heading_processing',
55 ],
56 self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE
57 );
58
59 add_action(
60 'woocommerce_order_status_' . $state . '_to_processing_notification',
61 [
62 $this,
63 'refresh_email_lang',
64 ],
65 self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE
66 );
67 add_action(
68 'woocommerce_order_status_failed',
69 [
70 $this,
71 'refresh_email_lang',
72 ],
73 self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE
74 );
75 }
76
77 foreach ( [ 'pending', 'failed', 'cancelled' ] as $state ) {
78 add_action(
79 'woocommerce_order_status_' . $state . '_to_on-hold_notification',
80 [
81 $this,
82 'email_heading_on_hold',
83 ],
84 self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE
85 );
86 }
87
88 add_action( 'woocommerce_before_resend_order_emails', [ $this, 'email_header' ] );
89 add_action( 'woocommerce_after_resend_order_email', [ $this, 'email_footer' ] );
90
91 add_filter( 'icl_current_string_language', [ $this, 'icl_current_string_language' ], 10, 2 );
92
93 add_action( 'woocommerce_order_status_completed', [ $this, 'refresh_email_lang_complete' ], self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE );
94
95 add_action(
96 'woocommerce_order_status_pending_to_on-hold_notification',
97 [
98 $this,
99 'refresh_email_lang',
100 ],
101 self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE
102 );
103 add_action( 'woocommerce_new_customer_note', [ $this, 'refresh_email_lang' ], self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE );
104
105 foreach ( [ 'pending', 'failed' ] as $from_state ) {
106 foreach ( [ 'processing', 'completed', 'on-hold' ] as $to_state ) {
107 add_action(
108 'woocommerce_order_status_' . $from_state . '_to_' . $to_state . '_notification',
109 [
110 $this,
111 'new_order_admin_email',
112 ],
113 self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE
114 );
115 }
116 }
117
118 add_action( 'woocommerce_before_resend_order_emails', [ $this, 'backend_new_order_admin_email' ], self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE );
119
120 add_filter( 'plugin_locale', [ $this, 'set_locale_for_emails' ], 10, 2 );
121 add_filter( 'woocommerce_countries', [ $this, 'translate_woocommerce_countries' ] );
122
123 add_filter(
124 'woocommerce_allow_send_queued_transactional_email',
125 [
126 $this,
127 'send_queued_transactional_email',
128 ],
129 10,
130 3
131 );
132
133 add_action( 'woocommerce_order_partially_refunded_notification', [ $this, 'refresh_email_lang' ], self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE );
134 add_action( 'woocommerce_order_fully_refunded_notification', [ $this, 'refresh_email_lang' ], self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE );
135 add_filter( 'woocommerce_email_get_option', [ $this, 'filter_emails_strings' ], 10, 4 );
136
137 add_filter( 'woocommerce_email_setup_locale', '__return_false' );
138 add_filter( 'woocommerce_email_restore_locale', '__return_false' );
139
140 add_filter( 'woocommerce_email_heading_new_order', [ $this, 'new_order_email_heading' ] );
141 add_filter( 'woocommerce_email_subject_new_order', [ $this, 'new_order_email_subject' ] );
142
143 add_filter(
144 'woocommerce_email_heading_customer_on_hold_order',
145 [
146 $this,
147 'customer_on_hold_order_heading',
148 ]
149 );
150 add_filter(
151 'woocommerce_email_subject_customer_on_hold_order',
152 [
153 $this,
154 'customer_on_hold_order_subject',
155 ]
156 );
157
158 add_filter(
159 'woocommerce_email_heading_customer_processing_order',
160 [
161 $this,
162 'customer_processing_order_heading',
163 ]
164 );
165 add_filter(
166 'woocommerce_email_subject_customer_processing_order',
167 [
168 $this,
169 'customer_processing_order_subject',
170 ]
171 );
172
173 add_action( 'woocommerce_low_stock_notification', [ $this, 'low_stock_admin_notification' ], self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE );
174 add_action( 'woocommerce_no_stock_notification', [ $this, 'no_stock_admin_notification' ], self::PRIORITY_BEFORE_EMAIL_SET_LANGUAGE );
175
176 add_filter( 'woocommerce_rest_pre_insert_shop_order_object', [ $this, 'set_rest_language' ], 10, 2 );
177 }
178
179 private function add_hooks_to_restore_language_for_admin_notes() {
180 wpml_collect( [
181 'pending',
182 'processing',
183 'on-hold',
184 'completed',
185 'cancelled',
186 'refunded',
187 'failed',
188 ] )->each( function ( $status ) {
189 add_action( 'woocommerce_order_status_' . $status, [ $this, 'comments_language' ], self::PRIORITY_AFTER_STATUS_CHANGE_EMAIL );
190 } );
191 }
192
193 public function email_refresh_in_ajax() {
194 if ( ! current_user_can( 'edit_shop_orders' ) ) {
195 return;
196 }
197 if ( ! check_admin_referer( 'woocommerce-mark-order-status' ) ) {
198 return;
199 }
200 if ( isset( $_GET['order_id'] ) ) {
201 $this->refresh_email_lang( (int) $_GET['order_id'] );
202
203 if ( isset( $_GET['status'] ) && 'completed' === $_GET['status'] ) {
204 $this->email_heading_completed( (int) $_GET['order_id'], true );
205 }
206
207 return true;
208 }
209 }
210
211 public function refresh_email_lang_complete( $order_id ) {
212
213 $this->order_id = $order_id;
214 $this->refresh_email_lang( $order_id );
215 $this->email_heading_completed( $order_id, true );
216 }
217
218 public function email_header( $order ) {
219
220 if ( is_array( $order ) ) {
221 $order = $order['order_id'];
222 } elseif ( is_object( $order ) ) {
223 $order = method_exists( 'WC_Order', 'get_id' ) ? $order->get_id() : $order->id;
224 }
225
226 $this->refresh_email_lang( $order );
227 }
228
229
230 public function refresh_email_lang( $order_id ) {
231
232 $language = $this->get_order_language( $order_id );
233
234 if ( ! empty( $language ) ) {
235 $this->change_email_language( $language );
236 }
237
238 $this->force_translating_admin_options_in_backend();
239 }
240
241 private function force_translating_admin_options_in_backend() {
242
243 do_action(
244 'wpml_st_force_translate_admin_options',
245 [
246 'woocommerce_checkout_privacy_policy_text',
247 'woocommerce_email_footer_text',
248 'woocommerce_email_from_address',
249 'woocommerce_email_from_name',
250 'woocommerce_price_decimal_sep',
251 'woocommerce_price_thousand_sep',
252 'woocommerce_registration_privacy_policy_text',
253 ]
254 );
255 }
256
257 public function get_order_language( $order_id ) {
258
259 if ( is_array( $order_id ) ) {
260 if ( isset( $order_id['order_id'] ) ) {
261 $order_id = $order_id['order_id'];
262 } else {
263 return null;
264 }
265 }
266
267 $language = WCML_Orders::getLanguage( $order_id );
268
269 if ( ! $language ) {
270 $language = $this->rest_language;
271 }
272
273 return $language;
274 }
275
276 public function email_footer() {
277 $this->sitepress->switch_lang( $this->sitepress->get_default_language() );
278 }
279
280 public function comments_language() {
281
282 if ( is_admin() && false !== $this->admin_language ) {
283 $this->change_email_language( $this->admin_language );
284 } else {
285 $this->change_email_language( $this->wcmlStrings->get_domain_language( 'woocommerce' ) );
286 }
287 }
288
289
290 public function email_heading_completed( $order_id, $no_checking = false ) {
291 $email = $this->getEmailObject( WC_Email_Customer_Completed_Order::class, $no_checking );
292
293 if ( $email instanceof WC_Email_Customer_Completed_Order ) {
294 $translate = $this->getTranslatorFor(
295 'admin_texts_woocommerce_customer_completed_order_settings',
296 '[woocommerce_customer_completed_order_settings]',
297 WCML_Orders::getLanguage( $order_id ),
298 $email
299 );
300
301 $email->heading = $translate( 'heading' );
302 $email->subject = $translate( 'subject' );
303 @$email->heading_downloadable = $translate( 'heading_downloadable' );
304 @$email->subject_downloadable = $translate( 'subject_downloadable' );
305 $original_enabled_state = $email->enabled;
306 $email->enabled = 'no';
307 $email->trigger( $order_id );
308 $email->enabled = $original_enabled_state;
309 }
310 }
311
312 public function email_heading_processing( $order_id ) {
313 $this->translate_email_headings( $order_id, 'WC_Email_Customer_Processing_Order', 'woocommerce_customer_processing_order_settings' );
314 }
315
316 public function customer_processing_order_heading( $heading ) {
317 return $this->get_translated_order_strings( 'heading', $heading, 'WC_Email_Customer_Processing_Order' );
318 }
319
320 public function customer_processing_order_subject( $subject ) {
321 return $this->get_translated_order_strings( 'subject', $subject, 'WC_Email_Customer_Processing_Order' );
322 }
323
324
325 public function email_heading_on_hold( $order_id ) {
326 $this->translate_email_headings( $order_id, 'WC_Email_Customer_On_Hold_Order', 'woocommerce_customer_on_hold_order_settings' );
327 }
328
329 private function translate_email_headings( $order_id, $class_name, $string_name ) {
330 $email = $this->getEmailObject( $class_name );
331
332 if ( $email ) {
333 $translate = $this->getTranslatorFor(
334 'admin_texts_' . $string_name,
335 '[' . $string_name . ']',
336 WCML_Orders::getLanguage( $order_id ),
337 $email
338 );
339
340 $email->heading = $translate( 'heading' );
341 $email->subject = $translate( 'subject' );
342 $original_enabled_state = $email->enabled;
343 $email->enabled = 'no';
344 if ( method_exists( $email, 'trigger' ) ) {
345 $email->trigger( $order_id );
346 }
347 $email->enabled = $original_enabled_state;
348 }
349 }
350
351 public function customer_on_hold_order_heading( $heading ) {
352 return $this->get_translated_order_strings( 'heading', $heading, 'WC_Email_Customer_On_Hold_Order' );
353 }
354
355 public function customer_on_hold_order_subject( $subject ) {
356 return $this->get_translated_order_strings( 'subject', $subject, 'WC_Email_Customer_On_Hold_Order' );
357 }
358
359 public function email_heading_note( $args ) {
360 $email = $this->getEmailObject( WC_Email_Customer_Note::class );
361
362 if ( $email instanceof WC_Email_Customer_Note ) {
363 $translate = $this->getTranslatorFor(
364 'admin_texts_woocommerce_customer_note_settings',
365 '[woocommerce_customer_note_settings]',
366 null,
367 $email
368 );
369
370 $email->heading = $translate( 'heading' );
371 $email->subject = $translate( 'subject' );
372 $original_enabled_state = $email->enabled;
373 $email->enabled = 'no';
374 $email->trigger( $args );
375 $email->enabled = $original_enabled_state;
376 }
377 }
378
379 public function filter_emails_strings( $value, WC_Email $email, $old_value, $key ) {
380
381 $translated_value = false;
382 $emailStrings = wpml_collect( [
383 'subject',
384 'subject_downloadable',
385 'subject_partial',
386 'subject_full',
387 'subject_paid',
388 'heading',
389 'heading_paid',
390 'heading_downloadable',
391 'heading_partial',
392 'heading_full',
393 'additional_content',
394 ] );
395
396 if (
397 isset( $email->object ) &&
398 $emailStrings->contains( $key )
399 ) {
400
401 $isAdminEmail = wpml_collect([
402 'new_order',
403 'cancelled_order',
404 'failed_order',
405 ])->contains( $email->id );
406
407 $translated_value = $this->get_email_translated_string( $key, $email, $isAdminEmail, $value );
408 }
409
410 return $translated_value ?: $value;
411 }
412
413 public function get_email_translated_string( $key, $email, $isAdminEmail, $originalValue = null, $originalDomain = 'woocommerce' ) {
414
415 list( $context, $name ) = $this->get_email_context_and_name( $email );
416 $orderId = $this->get_order_id_from_email_object( $email );
417
418 $language = $isAdminEmail
419 ? $this->get_admin_language_by_email( $email->recipient, $orderId )
420 : WCML_Orders::getLanguage( $orderId );
421
422 return $this->getStringTranslation( $context, $name . $key, $language, $originalValue, $originalDomain );
423 }
424
425 public function get_email_context_and_name( $emailObject ) {
426
427 $emailId = $emailObject->id;
428
429 if ( $emailObject instanceof WC_Email_Customer_Refunded_Order ) {
430 $emailId = 'customer_refunded_order';
431 }
432
433 $context = 'admin_texts_woocommerce_' . $emailId . '_settings';
434 $name = '[woocommerce_' . $emailId . '_settings]';
435
436 return [ $context, $name ];
437 }
438
439 private function get_order_id_from_email_object( $email ) {
440 if ( is_callable( [ $email->object, 'get_id' ] ) ) {
441 return $email->object->get_id();
442 }
443 if ( is_array( $email->object ) && isset( $email->object['ID'] ) ) {
444 return $email->object['ID'];
445 }
446
447 return false;
448 }
449
450 public function new_order_admin_email( $order_id ) {
451 $email = $this->getEmailObject( WC_Email_New_Order::class, true );
452
453 if ( $email instanceof WC_Email_New_Order ) {
454 $recipients = explode( ',', $email->get_recipient() );
455
456 $allowResendForAllRecipients = function() use ( $recipients ) {
457 static $numberOfAllowedResend;
458
459 if ( null === $numberOfAllowedResend ) {
460 $numberOfAllowedResend = count( $recipients );
461 } else {
462 $numberOfAllowedResend--;
463 }
464
465 return (bool) $numberOfAllowedResend;
466 };
467
468 add_filter( 'woocommerce_new_order_email_allows_resend', $allowResendForAllRecipients, 20 );
469
470 foreach ( $recipients as $recipient ) {
471 $admin_language = $this->get_admin_language_by_email( $recipient, $order_id );
472
473 $this->change_email_language( $admin_language );
474
475 $translate = $this->getTranslatorFor(
476 'admin_texts_woocommerce_new_order_settings',
477 '[woocommerce_new_order_settings]',
478 $admin_language ?: WCML_Orders::getLanguage( $order_id ),
479 $email
480 );
481
482 $email->heading = $translate( 'heading' );
483 $email->subject = $translate( 'subject' );
484 $email->recipient = $recipient;
485
486 $email->trigger( $order_id );
487 }
488
489 add_filter(
490 'woocommerce_email_enabled_new_order',
491 self::getPreventDuplicatedNewOrderEmail( $order_id ),
492 PHP_INT_MAX,
493 2
494 );
495
496 $this->refresh_email_lang( $order_id );
497 }
498 }
499
500 public static function getPreventDuplicatedNewOrderEmail( $processedOrderId ) {
501 return function( $isEmailEnabled, $order ) use ( $processedOrderId ) {
502 return $order->get_id() === $processedOrderId ? false : $isEmailEnabled;
503 };
504 }
505
506 private function get_admin_language_by_email( $recipient, $order_id = false ) {
507 $user = get_user_by( 'email', $recipient );
508 if ( $user ) {
509 $language = $this->sitepress->get_user_admin_language( $user->ID, true );
510 } else {
511 $language = $this->sitepress->get_default_language();
512 }
513
514 $language = apply_filters( 'wcml_new_order_admin_email_language', $language, $recipient, $order_id );
515
516 return apply_filters( 'wcml_get_admin_language_by_email', $language, $recipient, $order_id );
517 }
518
519 public function new_order_email_heading( $heading ) {
520 return $this->get_translated_order_strings( 'heading', $heading, 'WC_Email_New_Order' );
521 }
522
523 public function new_order_email_subject( $subject ) {
524 return $this->get_translated_order_strings( 'subject', $subject, 'WC_Email_New_Order' );
525 }
526
527 private function get_translated_order_strings( $type, $order_string, $class_name ) {
528 $email = $this->getEmailObject( $class_name );
529
530 if ( 'heading' === $type ) {
531 $translated_string = $email->heading;
532 } elseif ( 'subject' === $type ) {
533 $translated_string = $email->subject;
534 } else {
535 return $order_string;
536 }
537
538 return $translated_string ? $email->format_string( $translated_string ) : $order_string;
539 }
540
541 public function backend_new_order_admin_email( $order_id ) {
542 if ( isset( $_POST['wc_order_action'] ) && in_array(
543 $_POST['wc_order_action'],
544 [
545 'send_email_new_order',
546 'send_order_details_admin',
547 ]
548 ) ) {
549 $this->new_order_admin_email( $order_id );
550 }
551 }
552
553 public function change_email_language( $lang ) {
554 if ( ! $this->admin_language ) {
555 $this->admin_language = $this->sitepress->get_user_admin_language( get_current_user_id(), true );
556 }
557
558 $this->sitepress->switch_lang( $lang, true );
559 $this->locale = $this->sitepress->get_locale( $lang );
560 }
561
562 public function wcml_get_translated_email_string( $context, $name, $order_id = false, $language_code = null ) {
563
564 if ( $order_id && ! $language_code ) {
565 $order_language = WCML_Orders::getLanguage( $order_id );
566 if ( $order_language ) {
567 $language_code = $order_language;
568 }
569 }
570
571 return $this->wcmlStrings->get_translated_string_by_name_and_context( $context, $name, $language_code );
572 }
573
574 public function getStringTranslation( $domain, $name, $lang = null, $originalValue = null, $originalDomain = 'woocommerce' ) {
575 return $this->wcmlStrings->get_translated_string_by_name_and_context( $domain, $name, $lang ) ?: $this->getStringTranslationWithGettext( $originalValue, $originalDomain, $lang );
576 }
577
578 private function getStringTranslationWithGettext( $value, $domain, $lang ) {
579 if ( $value && $lang ) {
580 $switchLang = new WPML_Temporary_Switch_Language( $this->sitepress, $lang );
581 $translation = __( $value, $domain );
582 $switchLang->restore_lang();
583
584 return $translation;
585 }
586
587 return $value;
588 }
589
590 public function icl_current_string_language( $current_language, $name ) {
591 $order_id = false;
592
593 if ( isset( $_POST['action'] ) && 'editpost' === $_POST['action'] && isset( $_POST['post_type'] ) && 'shop_order' === $_POST['post_type'] && isset( $_POST['wc_order_action'] ) && 'send_email_new_order' !== $_POST['wc_order_action'] ) {
594 $order_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT );
595 } elseif ( isset( $_POST['action'] ) && 'woocommerce_add_order_note' === $_POST['action'] && isset( $_POST['note_type'] ) && 'customer' === $_POST['note_type'] ) {
596 $order_id = filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT );
597 } elseif ( isset( $_GET['action'] ) && isset( $_GET['order_id'] ) && ( 'woocommerce_mark_order_complete' === $_GET['action'] || 'woocommerce_mark_order_status' === $_GET['action'] ) ) {
598 $order_id = filter_input( INPUT_GET, 'order_id', FILTER_SANITIZE_NUMBER_INT );
599 } elseif ( WCML\COT\Helper::isOrderEditAdminScreen() ) {
600 $order_id = filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT );
601 } elseif ( isset( $_GET['action'] ) && 'mark_completed' === $_GET['action'] && $this->order_id ) {
602 $order_id = $this->order_id;
603 } elseif ( isset( $_POST['action'] ) && 'woocommerce_refund_line_items' === $_POST['action'] ) {
604 $order_id = filter_input( INPUT_POST, 'order_id', FILTER_SANITIZE_NUMBER_INT );
605 } elseif ( empty( $_POST ) && isset( $_GET['page'] ) && \WCML\Utilities\AdminUrl::PAGE_WOO_SETTINGS === $_GET['page'] && isset( $_GET['tab'] ) && 'email' === $_GET['tab'] && '[woocommerce' === substr( $name, 0, 12 ) ) {
606 $email_string = explode( ']', str_replace( '[', '', $name ) );
607 $email_option = get_option( $email_string[0], [] );
608 $context = 'admin_texts_' . $email_string[0];
609
610 $text = $email_option[ $email_string[1] ] ?? null;
611 if ( null !== $text ) {
612 $current_language = $this->wcmlStrings->get_string_language( $text, $context, $name );
613 }
614 } elseif ( $this->order_id ) {
615 $order_id = $this->order_id;
616 }
617
618 $order_id = apply_filters( 'wcml_send_email_order_id', $order_id );
619
620 if ( $order_id ) {
621 $order_language = WCML_Orders::getLanguage( $order_id );
622 if ( $order_language ) {
623 $current_language = $order_language;
624 } else {
625 $current_language = $this->sitepress->get_current_language();
626 }
627 }
628
629 return apply_filters( 'wcml_email_language', $current_language, $order_id );
630 }
631
632 public function set_locale_for_emails( $locale, $domain ) {
633
634 if ( 'woocommerce' === $domain && $this->locale ) {
635 $locale = $this->locale;
636 }
637
638 return $locale;
639 }
640
641 public function translate_woocommerce_countries( $countries ) {
642
643 if ( isset( $_POST['wc_order_action'] ) && 'send_email_new_order' !== $_POST['wc_order_action'] && isset( $_POST['post_ID'] ) ) {
644 $current_language = $this->sitepress->get_current_language();
645 $this->refresh_email_lang( (int) $_POST['post_ID'] );
646 $countries = include WC()->plugin_path() . '/i18n/countries.php';
647 $this->change_email_language( $current_language );
648 }
649
650 return $countries;
651 }
652
653
654 public function send_queued_transactional_email( $allow, $filter, $args ) {
655 $this->order_id = $args[0];
656
657 return $allow;
658 }
659
660 private function getEmailObject( $emailClass, $ignoreClassExists = false ) {
661
662 $wcEmails = $this->woocommerce->mailer();
663 if (
664 ( $ignoreClassExists || class_exists( $emailClass ) )
665 && isset( $wcEmails->emails[ $emailClass ] )
666 ) {
667 return $wcEmails->emails[ $emailClass ];
668 }
669
670 return null;
671 }
672
673 private function getTranslatorFor( $domain, $namePrefix, $languageCode, $email ) {
674 return function( $field ) use ( $domain, $namePrefix, $languageCode, $email ) {
675 return $this->getStringTranslation( $domain, $namePrefix . $field, $languageCode, Obj::prop( $field, $email ) );
676 };
677 }
678
679 public function low_stock_admin_notification( $product ) {
680 $this->admin_notification( $product, 'woocommerce_low_stock_notification', 'low_stock' );
681 }
682
683 public function no_stock_admin_notification( $product ) {
684 $this->admin_notification( $product, 'woocommerce_no_stock_notification', 'no_stock' );
685 }
686
687 private function admin_notification( $product, $action, $method ) {
688
689 $wcEmails = $this->woocommerce->mailer();
690
691 remove_action( $action, [ $wcEmails, $method ] );
692
693 if ( method_exists( $wcEmails, $method ) ) {
694 $admin_language = $this->get_admin_language_by_email( get_option( 'woocommerce_stock_email_recipient' ) );
695 $product_id_in_admin_language = wpml_object_id_filter(
696 $product->get_id(),
697 'product',
698 true,
699 $admin_language
700 );
701
702 $this->sitepress->switch_lang( $admin_language );
703 $wcEmails->$method( wc_get_product( $product_id_in_admin_language ) );
704 $this->sitepress->switch_lang();
705 }
706 }
707
708 public function set_rest_language( $order, $request ) {
709
710 $this->rest_language = isset( $request['lang'] ) ? $request['lang'] : null;
711
712 return $order;
713 }
714 }
715