PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.1.5
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.1.5
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.1.5, at includes/WCCampaign.php

196 lines 5.5 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
38 foreach ($compaigns as $compaign) {
39
40 if ($this->hasActiveCampaign($compaign, $productId)) {
41 add_action(
42 'woocommerce_single_product_summary',
43 function () use ($compaign) {$this->renderShortcode($compaign);},
44 $compaign->getWcPosition(),
45 [$compaign->getId()]
46 );
47 }
48 }
49 }
50
51 /**
52 * Check if given product has an active campaign.
53 *
54 * @param Campaign $campaign
55 * @param $productId
56 *
57 * @return bool
58 */
59 public function hasActiveCampaign(
60 $campaign,
61 $productId
62 ) {
63 if (!$campaign->isWcEnabled()) {
64 return false;
65 }
66 $categories = $this->getProductsCategories($productId);
67
68 $type = $campaign->getWcProductsSelectionType();
69
70 switch ($type) {
71 case C::WC_PS_TYPE_ALL:
72 return true;
73
74 case C::WC_PS_TYPE_INCLUDE_PRODUCTS:
75 return in_array($productId, $campaign->getWcProductsSelection());
76
77 case C::WC_PS_TYPE_EXCLUDE_PRODUCTS:
78 return !in_array($productId, $campaign->getWcProductsSelection());
79
80 case C::WC_PS_TYPE_INCLUDE_CATEGORIES:
81 $included_categories = $campaign->getWcProductsSelection();
82 $callback = function ($id)
83 use ($categories) {
84 return in_array($id, $categories);
85 };
86 $results = array_filter($included_categories, $callback);
87
88 return count($results) > 0;
89
90 case C::WC_PS_TYPE_EXCLUDE_CATEGORIES:
91 $excluded_categories = $campaign->getWcProductsSelection();
92 $results = array_filter($excluded_categories, function (
93 $id
94 ) use ($categories) {
95 return !in_array($id, $categories);
96 });
97
98 return count($results) > 0;
99
100 default:
101 return false;
102 }
103 }
104
105 /**
106 * Returns categories ids for the given product.
107 *
108 * @param $product_id
109 *
110 * @return array|\WP_Error
111 */
112 public function getProductsCategories($product_id)
113 {
114 return wp_get_object_terms($product_id, 'product_cat', [
115 'fields' => 'ids',
116 ]);
117 }
118
119 /**
120 * Render shortcode for the given campaign ID.
121 *
122 * @param Campaign $campaign
123 */
124 public function renderShortcode($campaign)
125 {
126 $shortcode = '[hurrytimer id=' . $campaign->getId() . ']';
127 echo do_shortcode($shortcode);
128 }
129
130 /**
131 * Change stock status.
132 *
133 * @param Campaign $campaign
134 * @param int $stockStatus
135 */
136 public function changeStockStatus($campaign, $stockStatus)
137 {
138
139 if (!$campaign->isWcEnabled()) {
140 return;
141 }
142
143 $products = $this->getProductsIdsByCampaign($campaign);
144 foreach ($products as $product) {
145 @$product->set_stock_status($stockStatus);
146 @$product->save();
147 }
148 }
149
150 /**
151 * Get campaign's products ID.
152 *
153 * @param Campaign $campaign
154 *
155 * @return array
156 */
157 private function getProductsIdsByCampaign($campaign)
158 {
159 $products = [];
160 $selection = $campaign->getWcProductsSelection();
161 $type = $campaign->getWcProductsSelectionType();
162 switch ($type) {
163 case C::WC_PS_TYPE_ALL:
164 $products = wc_get_products(['status' => 'publish']);
165 break;
166 case C::WC_PS_TYPE_EXCLUDE_PRODUCTS:
167 $products = wc_get_products([
168 'status' => 'publish',
169 'exclude' => $selection,
170 ]);
171 break;
172 case C::WC_PS_TYPE_INCLUDE_PRODUCTS:
173 $products = wc_get_products(['status' => 'publish', 'include' => $selection]);
174 break;
175 case C::WC_PS_TYPE_INCLUDE_CATEGORIES:
176 $categories = get_terms([
177 'taxonomy' => 'product_cat',
178 'include' => implode(',', $selection),
179 'fields' => 'slugs',
180 ]);
181 $products = wc_get_products(['status' => 'publish', 'category' => $categories]);
182 break;
183 case C::WC_PS_TYPE_EXCLUDE_CATEGORIES:
184 $categories = get_terms([
185 'taxonomy' => 'product_cat',
186 'exclude' => implode(',', $selection),
187 'fields' => 'slugs',
188 ]);
189 $products = wc_get_products(['status' => 'publish', 'category' => $categories]);
190 break;
191 }
192
193 return $products;
194 }
195 }
196