23
23
*/
24
24
class Coupon
25
25
{
26
+ static $ enable_logging = false ;
27
+ static $ enable_wjecf = false ;
26
28
27
29
/**
28
30
* Construct object.
29
31
*/
30
32
public function __construct ()
31
33
{
34
+ //avoid excessive loading
35
+ if ( defined ( 'DOING_CRON ' ) ) {return ;}
32
36
if ('on ' === Settings::getOption ('coupons ' , Features::getID (), 'on ' )) {
37
+
33
38
add_action ('woocommerce_coupon_loaded ' , array ($ this , 'couponLoaded ' ));
34
39
35
40
add_action ('wp_loaded ' , array ($ this , 'adminRegisterCouponStrings ' ));
@@ -41,12 +46,20 @@ public function __construct()
41
46
array ($ this , 'translateDescription ' ), 10 , 2 );
42
47
43
48
/* additional fields for WooCommerce Extended Coupon Features */
44
- add_filter ('woocommerce_coupon_get__wjecf_enqueue_message ' ,
45
- array ($ this , 'translateMessage ' ), 10 , 2 );
46
- add_filter ('woocommerce_coupon_get__wjecf_select_free_product_message ' ,
47
- array ($ this , 'translateMessage ' ), 10 , 2 );
48
- add_filter ('woocommerce_coupon_get__wjecf_free_product_ids ' ,
49
- array ($ this , 'getFreeProductsInLanguage ' ), 10 , 2 );
49
+ $ enable_wjecf = function_exists ('WJECF ' );
50
+ if ($ enable_wjecf ){
51
+ if (self ::$ enable_logging ) {
52
+ error_log ('woopoly enabled wjecf translation: ' .
53
+ ' in request: ' . (isset ($ _SERVER ['REQUEST_URI ' ]) ? $ _SERVER ['REQUEST_URI ' ] :
54
+ ' no $_SERVER[REQUEST_URI] available ' ));
55
+ }
56
+ add_filter ('woocommerce_coupon_get__wjecf_enqueue_message ' ,
57
+ array ($ this , 'translateMessage ' ), 10 , 2 );
58
+ add_filter ('woocommerce_coupon_get__wjecf_select_free_product_message ' ,
59
+ array ($ this , 'translateMessage ' ), 10 , 2 );
60
+ add_filter ('woocommerce_coupon_get__wjecf_free_product_ids ' ,
61
+ array ($ this , 'getFreeProductsInLanguage ' ), 10 , 2 );
62
+ }
50
63
}
51
64
}
52
65
@@ -58,12 +71,17 @@ public function __construct()
58
71
*
59
72
* @return array filtered result
60
73
*/
61
- public function getFreeProductsInLanguage ($ productIds , $ coupon )
74
+ public function getFreeProductsInLanguage ($ productIds , \ WC_Coupon $ coupon )
62
75
{
63
76
if (is_admin ()) {
64
77
return $ productIds ;
65
78
}
66
79
$ productLang = pll_current_language ();
80
+ if (self ::$ enable_logging ) {
81
+ error_log ('woopoly getting translated ids for: ' . $ productIds . ' for coupon ' . $ coupon ->get_code () .
82
+ ' in request: ' . (isset ($ _SERVER ['REQUEST_URI ' ]) ? $ _SERVER ['REQUEST_URI ' ] :
83
+ ' no $_SERVER[REQUEST_URI] available ' ));
84
+ }
67
85
$ productIds = explode (', ' , $ productIds );
68
86
$ mappedIds = array ();
69
87
foreach ($ productIds as $ productId ) {
@@ -80,11 +98,15 @@ public function getFreeProductsInLanguage($productIds, $coupon)
80
98
*
81
99
* @return string
82
100
*/
83
- public function translateLabel ($ value , $ coupon )
101
+ public function translateLabel ($ value , \ WC_Coupon $ coupon )
84
102
{
85
103
$ this ->registerCouponStringsForTranslation ();
86
- return sprintf (esc_html__ ('Coupon: %s ' , 'woocommerce ' ),
87
- pll__ (\get_post ($ coupon ->get_id ())->post_title ));
104
+ $ code = $ coupon ->get_code ();
105
+ if ($ code ){
106
+ return sprintf (esc_html__ ('Coupon: %s ' , 'woocommerce ' ), pll__ ($ code ));
107
+ } else {
108
+ return $ value ;
109
+ }
88
110
}
89
111
/**
90
112
* translate coupon description.
@@ -94,7 +116,7 @@ public function translateLabel($value, $coupon)
94
116
*
95
117
* @return string
96
118
*/
97
- public function translateDescription ($ value , $ coupon )
119
+ public function translateDescription ($ value , \ WC_Coupon $ coupon )
98
120
{
99
121
$ this ->registerCouponStringsForTranslation ();
100
122
return pll__ ($ value );
@@ -125,30 +147,45 @@ public function adminRegisterCouponStrings()
125
147
public function registerCouponStringsForTranslation ()
126
148
{
127
149
static $ coupons_loaded ;
128
- if (! $ coupons_loaded ) {
150
+ static $ doingload ;
151
+ if ($ coupons_loaded || $ doingload ){return ;}
152
+ if (! $ coupons_loaded && ! $ doingload ) {
153
+ $ doingload = true ;
129
154
if (function_exists ('pll_register_string ' )) {
155
+ if (self ::$ enable_logging ) {
156
+ error_log ('woopoly registering coupons for translation: ' .
157
+ ' in request: ' . (isset ($ _SERVER ['REQUEST_URI ' ]) ? $ _SERVER ['REQUEST_URI ' ] :
158
+ ' no $_SERVER[REQUEST_URI] available ' ));
159
+ }
130
160
$ coupons = $ this ->getCoupons ();
131
161
132
- foreach ($ coupons as $ coupon ) {
133
- //$code = wc_format_coupon_code($coupon->post_title);
134
- pll_register_string ($ coupon ->post_name , $ coupon ->post_title ,
162
+ foreach ($ coupons as $ coupon_postid ) {
163
+ $ coupon = new \WC_Coupon ( $ coupon_postid );
164
+
165
+ $ coupon_code = $ coupon ->get_code ();
166
+ $ coupon_slug = sanitize_title_with_dashes ($ coupon_code );
167
+ pll_register_string ($ coupon_slug , $ coupon_code ,
135
168
__ ('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 );
138
-
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 ,
169
+ pll_register_string ($ coupon_slug . '_description ' , $ coupon ->get_description (),
147
170
__ ('Woocommerce Coupon Names ' , 'woo-poly-integration ' ), true );
171
+
172
+ if (self ::$ enable_wjecf ) {
173
+
174
+ $ coupon_message = $ coupon ->get_meta ('_wjecf_enqueue_message ' , true );
175
+ if ($ coupon_message ) {
176
+ pll_register_string ($ coupon_slug . '_message ' , $ coupon_message ,
177
+ __ ('Woocommerce Coupon Names ' , 'woo-poly-integration ' ), true );
178
+ }
179
+ $ freeproduct_message = $ coupon ->get_meta ('_wjecf_select_free_product_message ' , true );
180
+ if ($ freeproduct_message ) {
181
+ pll_register_string ($ coupon_slug . '_freeproductmessage ' , $ coupon_message ,
182
+ __ ('Woocommerce Coupon Names ' , 'woo-poly-integration ' ), true );
183
+ }
148
184
}
149
185
}
150
186
}
151
187
$ coupons_loaded = true ;
188
+ $ doingload = false ;
152
189
}
153
190
}
154
191
@@ -161,21 +198,39 @@ private function getCoupons()
161
198
{
162
199
global $ woocommerce ;
163
200
164
- $ args = array (
165
- 'posts_per_page ' => -1 ,
166
- 'orderby ' => 'title ' ,
167
- 'order ' => 'asc ' ,
168
- 'post_type ' => 'shop_coupon ' ,
169
- 'post_status ' => 'publish ' ,
170
- );
171
-
172
- $ coupons = get_posts ($ args );
173
- return $ coupons ;
201
+ $ locale = (function_exists ('pll_current_language ' )) ? pll_current_language ('locale ' ) : get_locale ();
202
+ $ tKey = 'coupons- ' . $ locale ;
203
+
204
+ $ coupon_ids = get_transient ($ tKey );
205
+ if ($ coupon_ids ) {
206
+ if (self ::$ enable_logging ) {
207
+ error_log ('woopoly found coupons in transient: ' .
208
+ ' in request: ' . (isset ($ _SERVER ['REQUEST_URI ' ]) ? $ _SERVER ['REQUEST_URI ' ] :
209
+ ' no $_SERVER[REQUEST_URI] available ' ));
210
+ }
211
+ } else {
212
+ if (self ::$ enable_logging ) {
213
+ error_log ('woopoly loading coupons to transient: ' .
214
+ ' in request: ' . (isset ($ _SERVER ['REQUEST_URI ' ]) ? $ _SERVER ['REQUEST_URI ' ] :
215
+ ' no $_SERVER[REQUEST_URI] available ' ));
216
+ }
217
+ $ args = array (
218
+ 'posts_per_page ' => -1 ,
219
+ 'orderby ' => 'title ' ,
220
+ 'order ' => 'asc ' ,
221
+ 'post_type ' => 'shop_coupon ' ,
222
+ 'post_status ' => 'publish ' ,
223
+ 'fields ' => 'ids ' ,
224
+ );
225
+ $ coupon_ids = get_posts ($ args );
226
+ set_transient ($ tKey , $ coupon_ids , 3600 );
227
+ }
228
+ return $ coupon_ids ;
174
229
}
175
230
176
231
177
232
/**
178
- * Extend the coupon to include porducts translations.
233
+ * Extend the coupon to include product translations.
179
234
*
180
235
* @param \WC_Coupon $coupon
181
236
*
@@ -199,7 +254,7 @@ public function couponLoaded(\WC_Coupon $coupon)
199
254
}
200
255
}
201
256
202
- foreach ($ coupon ->get_product_categories () as $ id ) {
257
+ foreach ($ coupon ->get_product_categories () as $ id ) {
203
258
foreach ($ this ->getProductTermTranslationIDS ($ id ) as $ _id ) {
204
259
$ productCategoriesIDS [] = $ _id ;
205
260
}
@@ -211,6 +266,16 @@ public function couponLoaded(\WC_Coupon $coupon)
211
266
}
212
267
}
213
268
269
+ if (self ::$ enable_logging ) {
270
+ error_log ('woopoly setting related for coupon : ' . $ coupon ->get_code () .
271
+ ' include-products: ' . implode (', ' , $ productIDS ) .
272
+ ' exclude-products: ' . implode (', ' , $ excludeProductIDS ) .
273
+ ' include-categories: ' . implode (', ' , $ productCategoriesIDS ) .
274
+ ' exclude-categories: ' . implode (', ' , $ excludeProductCategoriesIDS ) .
275
+ ' in request: ' . (isset ($ _SERVER ['REQUEST_URI ' ]) ? $ _SERVER ['REQUEST_URI ' ] :
276
+ ' no $_SERVER[REQUEST_URI] available ' ));
277
+ }
278
+
214
279
$ coupon ->set_product_ids ($ productIDS );
215
280
$ coupon ->set_excluded_product_ids ($ excludeProductIDS );
216
281
$ coupon ->set_product_categories ($ productCategoriesIDS );
0 commit comments