Skip to content
This repository was archived by the owner on Mar 17, 2022. It is now read-only.

Commit d192347

Browse files
committed
fixes #268 setup coupon translations only when needed
fixes #268 setup coupon translations only when needed
1 parent e0c51ba commit d192347

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/Hyyan/WPI/Coupon.php

+31-18
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct()
3232
if ('on' === Settings::getOption('coupons', Features::getID(), 'on')) {
3333
add_action('woocommerce_coupon_loaded', array($this, 'couponLoaded'));
3434

35-
add_action('wp_loaded', array($this, 'registerCouponStringsForTranslation'));
35+
add_action('wp_loaded', array($this, 'adminRegisterCouponStrings'));
3636

3737
//apply label filter with higher priority than woocommerce-auto-added-coupons
3838
add_filter('woocommerce_cart_totals_coupon_label',
@@ -82,6 +82,7 @@ public function getFreeProductsInLanguage($productIds, $coupon)
8282
*/
8383
public function translateLabel($value, $coupon)
8484
{
85+
$this->registerCouponStringsForTranslation();
8586
return sprintf(esc_html__('Coupon: %s', 'woocommerce'),
8687
pll__(\get_post($coupon->get_id())->post_title));
8788
}
@@ -95,6 +96,7 @@ public function translateLabel($value, $coupon)
9596
*/
9697
public function translateDescription($value, $coupon)
9798
{
99+
$this->registerCouponStringsForTranslation();
98100
return pll__($value);
99101
}
100102
/**
@@ -107,35 +109,46 @@ public function translateDescription($value, $coupon)
107109
*/
108110
public function translateMessage($value, $coupon)
109111
{
112+
$this->registerCouponStringsForTranslation();
110113
return pll__($value);
111114
}
112115

116+
public function adminRegisterCouponStrings()
117+
{
118+
if (is_admin() && (!is_ajax())){
119+
$this->registerCouponStringsForTranslation();
120+
}
121+
}
113122
/**
114123
* Register coupon titles adn descriptions in Polylang's Strings translations table.
115124
*/
116125
public function registerCouponStringsForTranslation()
117126
{
118-
if (function_exists('pll_register_string')) {
119-
$coupons = $this->getCoupons();
127+
static $coupons_loaded;
128+
if (! $coupons_loaded){
129+
if (function_exists('pll_register_string')) {
130+
$coupons = $this->getCoupons();
120131

121-
foreach ($coupons as $coupon) {
122-
//$code = wc_format_coupon_code($coupon->post_title);
123-
pll_register_string($coupon->post_name, $coupon->post_title,
124-
__('Woocommerce Coupon Names', 'woo-poly-integration'));
125-
pll_register_string($coupon->post_name . '_description', $coupon->post_excerpt,
126-
__('Woocommerce Coupon Names', 'woo-poly-integration'), true);
132+
foreach ($coupons as $coupon) {
133+
//$code = wc_format_coupon_code($coupon->post_title);
134+
pll_register_string($coupon->post_name, $coupon->post_title,
135+
__('Woocommerce Coupon Names', 'woo-poly-integration'));
136+
pll_register_string($coupon->post_name . '_description', $coupon->post_excerpt,
137+
__('Woocommerce Coupon Names', 'woo-poly-integration'), true);
127138

128-
$coupon_message = get_post_meta($coupon->ID, '_wjecf_enqueue_message', true);
129-
if ($coupon_message) {
130-
pll_register_string($coupon->post_name . '_message', $coupon_message,
131-
__('Woocommerce Coupon Names', 'woo-poly-integration'), true);
132-
}
133-
$freeproduct_message = get_post_meta($coupon->ID, '_wjecf_select_free_product_message', true);
134-
if ($freeproduct_message) {
135-
pll_register_string($coupon->post_name . '_freeproductmessage', $coupon_message,
136-
__('Woocommerce Coupon Names', 'woo-poly-integration'), true);
139+
$coupon_message = get_post_meta($coupon->ID, '_wjecf_enqueue_message', true);
140+
if ($coupon_message) {
141+
pll_register_string($coupon->post_name . '_message', $coupon_message,
142+
__('Woocommerce Coupon Names', 'woo-poly-integration'), true);
143+
}
144+
$freeproduct_message = get_post_meta($coupon->ID, '_wjecf_select_free_product_message', true);
145+
if ($freeproduct_message) {
146+
pll_register_string($coupon->post_name . '_freeproductmessage', $coupon_message,
147+
__('Woocommerce Coupon Names', 'woo-poly-integration'), true);
148+
}
137149
}
138150
}
151+
$coupons_loaded = true;
139152
}
140153
}
141154

0 commit comments

Comments
 (0)