PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.6.2
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.6.2
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 / functions.php

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

319 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Hurrytimer\Campaign;
4
5 if (!function_exists('hurryt_wc_stock_statuses')) {
6 /**
7 * Returns WooCommerce stock status as array.
8 *
9 * @return array
10 */
11 function hurryt_wc_stock_statuses()
12 {
13 return [
14 [
15 'name' => __('In stock', 'hurrytimer'),
16 'id' => \Hurrytimer\C::WC_IN_STOCK,
17 ],
18 [
19 'name' => __('Out of stock', 'hurrytimer'),
20 'id' => \Hurrytimer\C::WC_OUT_OF_STOCK,
21 ],
22 [
23 'name' => __('On backorder', 'hurrytimer'),
24 'id' => \Hurrytimer\C::WC_ON_BACKORDER,
25 ],
26 ];
27 }
28 }
29
30 if (!function_exists('hurryt_wc_conditions')) {
31
32 /**
33 * Returns supported conditions.
34 *
35 * @return array
36 */
37 function hurryt_wc_conditions()
38 {
39 return (new \Hurrytimer\ConditionalLogic())->addRule([
40 'key' => 'stock_status',
41 'name' => __('Stock status', 'hurrytimer'),
42 'operators' => ['==', '!='],
43 'values' => hurryt_wc_stock_statuses(),
44 'type' => 'string',
45 ])->addRule([
46 'key' => 'stock_quantity',
47 'name' => __('Stock quantity', 'hurrytimer'),
48 'operators' => ['==', '!=', '<', '>'],
49 'values' => [],
50 'type' => 'number',
51 ])->addRule([
52 'key' => 'shipping_class',
53 'name' => __('Shipping class', 'hurrytimer'),
54 'operators' => ['==', '!='],
55 'values' => array_map(function ($class) {
56 return [
57 'name' => $class->name,
58 'id' => $class->term_id,
59 ];
60 }, \WC_Shipping::instance()->get_shipping_classes()),
61 ])
62 ->addRule([
63 'key' => 'on_sale',
64 'name' => __('On sale', 'hurrytimer'),
65 'operators' => ['=='],
66 'values' => [
67 ['name' => 'Yes', 'id' => 'yes'],
68 ['name' => 'No', 'id' => 'no']
69 ],
70 ])->get();
71 }
72 }
73
74 if (!function_exists('hurryt_tz')) {
75 /**
76 * Return WP timezone string/offset.
77 *
78 * @return mixed|string|void
79 */
80 function hurryt_tz()
81 {
82 $tz_string = get_option('timezone_string');
83 if (!empty($tz_string)) {
84 return $tz_string;
85 }
86 $offset = get_option('gmt_offset');
87 $hours = (int)$offset;
88 $minutes = abs(($offset - (int)$offset) * 60);
89 $offset = sprintf('%+03d:%02d', $hours, $minutes);
90
91 return $offset;
92 }
93 }
94
95 if (!function_exists('hurryt_current_page_id')) {
96
97 /**
98 * Get current page ID.
99 *
100 * @return int|mixed
101 */
102 function hurryt_current_page_id()
103 {
104 $object_id = get_queried_object_id();
105 if (!hurryt_is_woocommerce_activated()) {
106 return $object_id;
107 }
108 $wc_ids = [
109 'shop' => get_option('woocommerce_shop_page_id'),
110 'cart' => get_option('woocommerce_cart_page_id'),
111 'checkout' => get_option('woocommerce_checkout_page_id'),
112 'checkout_pay' => get_option('woocommerce_pay_page_id'),
113 'thanks' => get_option('woocommerce_thanks_page_id'),
114 'myaccount' => get_option('woocommerce_myaccount_page_id'),
115 'edit_address' => get_option('woocommerce_edit_address_page_id'),
116 'view_order' => get_option('woocommerce_view_order_page_id'),
117 'terms' => get_option('woocommerce_terms_page_id'),
118 ];
119 if (is_shop()) {
120 $object_id = $wc_ids['shop'];
121 } elseif (is_account_page()) {
122 $object_id = $wc_ids['myaccount'];
123 } elseif (is_checkout_pay_page()) {
124 $object_id = $wc_ids['checkout_pay'];
125 } elseif (is_checkout()) {
126 $object_id = $wc_ids['checkout'];
127 } elseif (is_cart()) {
128 $object_id = $wc_ids['cart'];
129 } elseif (is_view_order_page()) {
130 $object_id = $wc_ids['view_order'];
131 } elseif (is_view_order_page()) {
132 $object_id = $wc_ids['view_order'];
133 } elseif (is_view_order_page()) {
134 $object_id = $wc_ids['view_order'];
135 } elseif (is_view_order_page()) {
136 $object_id = $wc_ids['view_order'];
137 }
138
139 return $object_id;
140 }
141 }
142
143 if (!function_exists('hurryt_is_woocommerce_activated')) {
144 /**
145 * Returns true if WC is active.
146 *
147 * @return bool
148 */
149 function hurryt_is_woocommerce_activated()
150 {
151 return class_exists('woocommerce');
152 }
153 }
154
155 if (!function_exists('hurryt_settings')) {
156 /**
157 * Returns plugin settings array.
158 *
159 * @return array
160 */
161 function hurryt_settings()
162 {
163 $defaults = ['disable_actions' => 0];
164
165 return wp_parse_args(get_option('hurryt_settings', []), $defaults);
166 }
167 }
168
169 if (!function_exists('hurryt_get_campaign')) {
170 /**
171 * Create a campaign instance with all props.
172 * @param $id
173 *
174 * @return Campaign
175 */
176 function hurryt_get_campaign($id)
177 {
178 $campaign = new Campaign($id);
179 $campaign->loadSettings();
180
181 return $campaign;
182 }
183 }
184
185 if (!function_exists('hurryt_is_admin_area')) {
186 function hurryt_is_admin_area()
187 {
188 global $wp;
189 wp_parse_str($wp->query_string, $params);
190
191 return is_admin()
192 || is_preview()
193 || is_customize_preview()
194 || hurrytimer_is_elementor_edit_mode()
195 || hurrytimer_is_elementor_preview_mode()
196 || !empty($params['preview'])
197 || !empty($params['fl_builder']);
198 }
199 }
200
201 if (!function_exists('hurryt_parse_campaigns')) {
202
203 /**
204 * Parse campaigns shortcodes in the given string content.
205 */
206 function hurryt_parse_campaigns($content)
207 {
208 $pattern = get_shortcode_regex();
209 $tag = 'hurrytimer';
210 $ids = [];
211 if (
212 preg_match_all('/' . $pattern . '/s', $content, $matches)
213 && array_key_exists(2, $matches)
214 && in_array($tag, $matches[2])
215 ) {
216 foreach ((array)$matches[2] as $key => $value) {
217 if ($tag === $value) {
218 $ids[] = shortcode_parse_atts($matches[3][$key]);
219 }
220 }
221 $ids = array_map(function ($id) {
222 return isset($id['id']) ? absint($id['id']) : null;
223 }, $ids);
224
225 return array_filter($ids);
226 }
227
228 return [];
229 }
230 }
231
232 if (!function_exists('hurryt_count_active_campaigns')) {
233 /**
234 * Returns published campaigns.
235 */
236 function hurryt_count_active_campaigns()
237 {
238
239 /** @var object $count */
240 $count = wp_count_posts(HURRYT_POST_TYPE);
241
242 return absint($count->publish);
243 }
244 }
245
246
247 /**
248 * Return all WooCommerce coupons.
249 * @param string $status
250 * @return array
251 */
252 function hurryt_get_wc_coupons($status = 'publish')
253 {
254
255 return get_posts([
256 'post_type' => 'shop_coupon',
257 'post_status' => $status,
258 'posts_per_page' => -1
259 ]);
260 }
261
262
263 function hurryt_wc_coupon_name($coupon_id)
264 {
265 if (!function_exists('wc_get_coupon_types')) {
266 return '';
267 }
268
269 $type_names = wc_get_coupon_types();
270
271 $coupon_type = get_post_meta($coupon_id, 'discount_type', true);
272
273 return isset($type_names[$coupon_type]) ? $type_names[$coupon_type] : '';
274 }
275
276 /**
277 * Return the current timezone string.
278 * @return string
279 */
280 function hurryt_current_timezone_string()
281 {
282 $current_offset = get_option('gmt_offset');
283 $tzstring = get_option('timezone_string');
284
285 if (false !== strpos($tzstring, 'Etc/GMT')) {
286 $tzstring = '';
287 }
288
289 if (empty($tzstring)) {
290 if (0 == $current_offset) {
291 $tzstring = 'UTC+0';
292 } elseif ($current_offset < 0) {
293 $tzstring = 'UTC' . $current_offset;
294 } else {
295 $tzstring = 'UTC+' . $current_offset;
296 }
297 }
298 return $tzstring;
299 }
300
301
302 function hurrytimer_is_elementor_edit_mode()
303 {
304 try {
305 return class_exists('\Elementor\Plugin') && \Elementor\Plugin::$instance->editor->is_edit_mode();
306 } catch (\Exception $e) {
307 return false;
308 }
309 }
310
311 function hurrytimer_is_elementor_preview_mode()
312 {
313 try {
314 return class_exists('\Elementor\Plugin') && \Elementor\Plugin::$instance->preview->is_preview_mode();
315 } catch (\Exception $e) {
316 return false;
317 }
318 }
319