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

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

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