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

Commit 74f8620

Browse files
committed
#325 Coupon optimization
removed posts queries from Coupon class, now queries for ids of coupons only and uses WooCommerce native api [and therefore caching transients] rather than any separate post meta queries. Lighter for sites with many coupons
1 parent c7f3db8 commit 74f8620

File tree

2 files changed

+104
-39
lines changed

2 files changed

+104
-39
lines changed

__init__.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* License: MIT License
1313
* Version: 1.0.4
1414
* Requires At Least: 4.7
15-
* Tested Up To: 4.9
15+
* Tested Up To: 4.9.5
1616
* WC requires at least: 3.0.0
1717
* WC tested up to: 3.2.6
1818
* Requires PHP: 5.3

src/Hyyan/WPI/Coupon.php

+103-38
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323
*/
2424
class Coupon
2525
{
26+
static $enable_logging = false;
27+
static $enable_wjecf = false;
2628

2729
/**
2830
* Construct object.
2931
*/
3032
public function __construct()
3133
{
34+
//avoid excessive loading
35+
if ( defined( 'DOING_CRON') ) {return;}
3236
if ('on' === Settings::getOption('coupons', Features::getID(), 'on')) {
37+
3338
add_action('woocommerce_coupon_loaded', array($this, 'couponLoaded'));
3439

3540
add_action('wp_loaded', array($this, 'adminRegisterCouponStrings'));
@@ -41,12 +46,20 @@ public function __construct()
4146
array($this, 'translateDescription'), 10, 2);
4247

4348
/* 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+
}
5063
}
5164
}
5265

@@ -58,12 +71,17 @@ public function __construct()
5871
*
5972
* @return array filtered result
6073
*/
61-
public function getFreeProductsInLanguage($productIds, $coupon)
74+
public function getFreeProductsInLanguage($productIds, \WC_Coupon $coupon)
6275
{
6376
if (is_admin()) {
6477
return $productIds;
6578
}
6679
$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+
}
6785
$productIds = explode(',', $productIds);
6886
$mappedIds = array();
6987
foreach ($productIds as $productId) {
@@ -80,11 +98,15 @@ public function getFreeProductsInLanguage($productIds, $coupon)
8098
*
8199
* @return string
82100
*/
83-
public function translateLabel($value, $coupon)
101+
public function translateLabel($value, \WC_Coupon $coupon)
84102
{
85103
$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+
}
88110
}
89111
/**
90112
* translate coupon description.
@@ -94,7 +116,7 @@ public function translateLabel($value, $coupon)
94116
*
95117
* @return string
96118
*/
97-
public function translateDescription($value, $coupon)
119+
public function translateDescription($value, \WC_Coupon $coupon)
98120
{
99121
$this->registerCouponStringsForTranslation();
100122
return pll__($value);
@@ -125,30 +147,45 @@ public function adminRegisterCouponStrings()
125147
public function registerCouponStringsForTranslation()
126148
{
127149
static $coupons_loaded;
128-
if (! $coupons_loaded) {
150+
static $doingload;
151+
if ($coupons_loaded || $doingload){return;}
152+
if (! $coupons_loaded && ! $doingload) {
153+
$doingload = true;
129154
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+
}
130160
$coupons = $this->getCoupons();
131161

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,
135168
__('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(),
147170
__('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+
}
148184
}
149185
}
150186
}
151187
$coupons_loaded = true;
188+
$doingload = false;
152189
}
153190
}
154191

@@ -161,21 +198,39 @@ private function getCoupons()
161198
{
162199
global $woocommerce;
163200

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;
174229
}
175230

176231

177232
/**
178-
* Extend the coupon to include porducts translations.
233+
* Extend the coupon to include product translations.
179234
*
180235
* @param \WC_Coupon $coupon
181236
*
@@ -199,7 +254,7 @@ public function couponLoaded(\WC_Coupon $coupon)
199254
}
200255
}
201256

202-
foreach ($coupon->get_product_categories() as $id) {
257+
foreach ($coupon->get_product_categories() as $id) {
203258
foreach ($this->getProductTermTranslationIDS($id) as $_id) {
204259
$productCategoriesIDS[] = $_id;
205260
}
@@ -211,6 +266,16 @@ public function couponLoaded(\WC_Coupon $coupon)
211266
}
212267
}
213268

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+
214279
$coupon->set_product_ids($productIDS);
215280
$coupon->set_excluded_product_ids($excludeProductIDS);
216281
$coupon->set_product_categories($productCategoriesIDS);

0 commit comments

Comments
 (0)