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

199 lines 5.6 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 array of campaigns with WC 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 /**
30 * Run campaign for then given product page.
31 *
32 * @param $productId
33 */
34 public function run($productId)
35 {
36
37 $compaigns = $this->findWCCampaigns();
38
39 foreach ($compaigns as $compaign) {
40
41 if ($this->hasActiveCampaign($compaign, $productId)) {
42 add_action(
43 'woocommerce_single_product_summary',
44 function () use ($compaign) { $this->renderShortcode($compaign); },
45 $compaign->getWcPosition(),
46 [$compaign->getId()]
47 );
48 }
49 }
50 }
51
52
53 /**
54 * Check if given product has an active campaign.
55 *
56 * @param Campaign $campaign
57 * @param $productId
58 *
59 * @return bool
60 */
61 public function hasActiveCampaign(
62 $campaign,
63 $productId
64 ) {
65 if ( ! $campaign->isWcEnabled()) {
66 return false;
67 }
68 $categories = $this->getProductsCategories($productId);
69
70 $type = $campaign->getWcProductsSelectionType();
71
72 switch ($type) {
73 case C::WC_PS_TYPE_ALL:
74 return true;
75
76 case C::WC_PS_TYPE_INCLUDE_PRODUCTS:
77 return in_array($productId, $campaign->getWcProductsSelection());
78
79 case C::WC_PS_TYPE_EXCLUDE_PRODUCTS:
80 return ! in_array($productId, $campaign->getWcProductsSelection());
81
82 case C::WC_PS_TYPE_INCLUDE_CATEGORIES:
83 $included_categories = $campaign->getWcProductsSelection();
84 $callback = function ($id)
85 use ($categories) {
86 return in_array($id, $categories);
87 };
88 $results = array_filter($included_categories, $callback);
89
90 return count($results) > 0;
91
92 case C::WC_PS_TYPE_EXCLUDE_CATEGORIES:
93 $excluded_categories = $campaign->getWcProductsSelection();
94 $results = array_filter($excluded_categories, function (
95 $id
96 ) use ($categories) {
97 return ! in_array($id, $categories);
98 });
99
100 return count($results) > 0;
101
102 default:
103 return false;
104 }
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 \Hurrytimer\Campaign
137 * @param $stockStatus
138 */
139 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 \Hurrytimer\Campaign $campaign
157 *
158 * @return \WC_Product[]
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