PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.2.16
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.2.16
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / WCCampaign.php

WCCampaign.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.2.16, at includes/WCCampaign.php

253 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 class WCCampaign
6 {
7 /**
8 * Returns campaigns with WooCommerce enabled.
9 *
10 * @return array
11 */
12 public function findWCCampaigns()
13 {
14 $posts = get_posts([
15 'post_type' => HURRYT_POST_TYPE,
16 'numberposts' => -1,
17 'post_status' => 'publish',
18 'meta_key' => 'wc_enable',
19 'meta_value' => C::YES,
20 'suppress_filters' => false,
21 ]);
22
23 return array_map(function ($post) {
24 return new Campaign($post->ID);
25 }, $posts);
26 }
27
28 /**
29 * Run campaign for then given product page.
30 *
31 * @param $productId
32 */
33 public function run($productId)
34 {
35
36 $compaigns = $this->findWCCampaigns();
37 foreach ($compaigns as $compaign) {
38 $compaign->loadSettings();
39 if($compaign->enableSticky === C::YES) {
40 continue;
41 };
42
43 if ($this->hasActiveCampaign($compaign, $productId) && $this->canApply($compaign,$productId)) {
44 add_action(
45 'woocommerce_single_product_summary',
46 function () use ($compaign) {$this->renderShortcode($compaign);},
47 $compaign->getWcPosition(),
48 [$compaign->getId()]
49 );
50 }
51 }
52 }
53
54 /**
55 * Check if given product has an active campaign.
56 *
57 * @param Campaign $campaign
58 * @param $productId
59 *
60 * @return bool
61 */
62 public function hasActiveCampaign(
63 $campaign,
64 $productId
65 ) {
66 if (!$campaign->isWcEnabled()) {
67 return false;
68 }
69 $categories = $this->getProductsCategories($productId);
70
71 $type = $campaign->getWcProductsSelectionType();
72
73 switch ($type) {
74 case C::WC_PS_TYPE_ALL:
75 return true;
76
77 case C::WC_PS_TYPE_INCLUDE_PRODUCTS:
78 return in_array($productId, $campaign->getWcProductsSelection());
79
80 case C::WC_PS_TYPE_EXCLUDE_PRODUCTS:
81 return !in_array($productId, $campaign->getWcProductsSelection());
82
83 case C::WC_PS_TYPE_INCLUDE_CATEGORIES:
84 $included_categories = $campaign->getWcProductsSelection();
85 $callback = function ($id)
86 use ($categories) {
87 return in_array($id, $categories);
88 };
89 $results = array_filter($included_categories, $callback);
90
91 return count($results) > 0;
92
93 case C::WC_PS_TYPE_EXCLUDE_CATEGORIES:
94 $excluded_categories = $campaign->getWcProductsSelection();
95 $results = array_filter($excluded_categories, function (
96 $id
97 ) use ($categories) {
98 return !in_array($id, $categories);
99 });
100
101 return count($results) > 0;
102
103 default:
104 return false;
105 }
106 }
107
108 /**
109 * Returns categories ids for the given product.
110 *
111 * @param $product_id
112 *
113 * @return array|\WP_Error
114 */
115 public function getProductsCategories($product_id)
116 {
117 return wp_get_object_terms($product_id, 'product_cat', [
118 'fields' => 'ids',
119 ]);
120 }
121
122 /**
123 * Render shortcode for the given campaign ID.
124 *
125 * @param Campaign $campaign
126 */
127 public function renderShortcode($campaign)
128 {
129 $shortcode = '[hurrytimer id=' . $campaign->getId() . ']';
130 echo do_shortcode($shortcode);
131 }
132
133 /**
134 * Change stock status.
135 *
136 * @param Campaign $campaign
137 * @param int $stockStatus
138 */
139 public function changeStockStatus($campaign, $stockStatus)
140 {
141
142 if (!$campaign->isWcEnabled()) {
143 return;
144 }
145
146 $products = $this->getProductsIdsByCampaign($campaign);
147 foreach ($products as $product) {
148 @$product->set_stock_status($stockStatus);
149 @$product->save();
150 }
151 }
152
153 /**
154 * Get campaign's products ID.
155 *
156 * @param Campaign $campaign
157 *
158 * @return array
159 */
160 private function getProductsIdsByCampaign($campaign)
161 {
162 $products = [];
163 $selection = $campaign->getWcProductsSelection();
164 $type = $campaign->getWcProductsSelectionType();
165 switch ($type) {
166 case C::WC_PS_TYPE_ALL:
167 $products = wc_get_products(['status' => 'publish']);
168 break;
169 case C::WC_PS_TYPE_EXCLUDE_PRODUCTS:
170 $products = wc_get_products([
171 'status' => 'publish',
172 'exclude' => $selection,
173 ]);
174 break;
175 case C::WC_PS_TYPE_INCLUDE_PRODUCTS:
176 $products = wc_get_products(['status' => 'publish', 'include' => $selection]);
177 break;
178 case C::WC_PS_TYPE_INCLUDE_CATEGORIES:
179 $categories = get_terms([
180 'taxonomy' => 'product_cat',
181 'include' => implode(',', $selection),
182 'fields' => 'slugs',
183 ]);
184 $products = wc_get_products(['status' => 'publish', 'category' => $categories]);
185 break;
186 case C::WC_PS_TYPE_EXCLUDE_CATEGORIES:
187 $categories = get_terms([
188 'taxonomy' => 'product_cat',
189 'exclude' => implode(',', $selection),
190 'fields' => 'slugs',
191 ]);
192 $products = wc_get_products(['status' => 'publish', 'category' => $categories]);
193 break;
194 }
195
196 return $products;
197 }
198
199 public function canApply($campaign, $objectId)
200 {
201 $groups = $campaign->getWcConditions();
202 if(empty($groups)) return true;
203
204 $product = wc_get_product($objectId);
205 $trueConditions = [];
206 $trueGroups = [];
207 foreach ($groups as $conditions) {
208 foreach ($conditions as $condition) {
209 switch ($condition['key']) {
210 case 'stock_status':
211 if ($condition['operator'] === '==') {
212 $trueConditions[] = $product->get_stock_status() == $condition['value'];
213 }
214 if ($condition['operator'] === '!=') {
215 $trueConditions[] = $product->get_stock_status() != $condition['value'];
216 }
217 break;
218 case 'stock_quantity':
219 if ($condition['operator'] === '==') {
220 $trueConditions[] = $product->get_stock_quantity() == $condition['value'];
221 }
222 if ($condition['operator'] === '!=') {
223 $trueConditions[] = $product->get_stock_quantity() != $condition['value'];
224 }
225 if ($condition['operator'] === '>') {
226 $trueConditions[] = $product->get_stock_quantity() > $condition['value'];
227 }
228 if ($condition['operator'] === '<') {
229 $trueConditions[] = $product->get_stock_quantity() < $condition['value'];
230 }
231 break;
232 case 'shipping_class':
233 if ($condition['operator'] === '==') {
234 $trueConditions[] = $product->get_shipping_class_id() == $condition['value'];
235 }
236 if ($condition['operator'] === '!=') {
237 $trueConditions[] = $product->get_shipping_class_id() != $condition['value'];
238 }
239 break;
240 }
241 }
242 $_true = array_filter($trueConditions, function ($bool) {
243 return $bool === true;
244 });
245
246 $trueGroups[] = count($_true) === count($conditions);
247 }
248 return !empty(array_filter($trueGroups, function ($bool) {
249 return $bool === true;
250 }));
251 }
252 }
253