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

342 lines 9.6 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 function hurryt_tz( $tz = null ) {
74
75 // If no saved timezone, fallback to WordPress setting.
76 if (empty($tz)) {
77 $current_offset = get_option('gmt_offset');
78 $tzstring = get_option('timezone_string');
79
80 if (str_contains($tzstring, 'Etc/GMT')) {
81 $tzstring = '';
82 }
83
84 if (empty($tzstring)) {
85 if (0 === (int) $current_offset) {
86 $tzstring = 'UTC+0';
87 } elseif ($current_offset < 0) {
88 $tzstring = 'UTC' . $current_offset;
89 } else {
90 $tzstring = 'UTC+' . $current_offset;
91 }
92 }
93
94 $tz = $tzstring;
95 }
96
97 // Convert UTC offsets like "UTC+2" into a valid timezone for PHP.
98 if (preg_match('/^UTC[+-]/', $tz)) {
99 $offset = (float)preg_replace( '/UTC\+?/', '', $tz );
100 $hours = (int) $offset;
101 $minutes = ( $offset - $hours );
102 $sign = ( $offset < 0 ) ? '-' : '+';
103 $abs_hour = abs( $hours );
104 $abs_mins = abs( $minutes * 60 );
105 $tz = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
106 }
107
108 return $tz;
109 }
110 }
111 if (!function_exists('hurryt_current_page_id')) {
112
113 /**
114 * Get current page ID.
115 *
116 * @return int|mixed
117 */
118 function hurryt_current_page_id() {
119 $object_id = get_queried_object_id();
120 if (!hurryt_is_woocommerce_activated()) {
121 return $object_id;
122 }
123 $wc_ids = [
124 'shop' => get_option('woocommerce_shop_page_id'),
125 'cart' => get_option('woocommerce_cart_page_id'),
126 'checkout' => get_option('woocommerce_checkout_page_id'),
127 'checkout_pay' => get_option('woocommerce_pay_page_id'),
128 'thanks' => get_option('woocommerce_thanks_page_id'),
129 'myaccount' => get_option('woocommerce_myaccount_page_id'),
130 'edit_address' => get_option('woocommerce_edit_address_page_id'),
131 'view_order' => get_option('woocommerce_view_order_page_id'),
132 'terms' => get_option('woocommerce_terms_page_id'),
133 ];
134 if (is_shop()) {
135 $object_id = $wc_ids['shop'];
136 } elseif (is_account_page()) {
137 $object_id = $wc_ids['myaccount'];
138 } elseif (is_checkout_pay_page()) {
139 $object_id = $wc_ids['checkout_pay'];
140 } elseif (is_checkout()) {
141 $object_id = $wc_ids['checkout'];
142 } elseif (is_cart()) {
143 $object_id = $wc_ids['cart'];
144 } elseif (is_view_order_page()) {
145 $object_id = $wc_ids['view_order'];
146 } elseif (is_view_order_page()) {
147 $object_id = $wc_ids['view_order'];
148 } elseif (is_view_order_page()) {
149 $object_id = $wc_ids['view_order'];
150 } elseif (is_view_order_page()) {
151 $object_id = $wc_ids['view_order'];
152 }
153
154 return $object_id;
155 }
156 }
157
158 if (!function_exists('hurryt_is_woocommerce_activated')) {
159 /**
160 * Returns true if WC is active.
161 *
162 * @return bool
163 */
164 function hurryt_is_woocommerce_activated() {
165 return class_exists('woocommerce');
166 }
167 }
168
169 if (!function_exists('hurryt_settings')) {
170 /**
171 * Returns plugin settings array.
172 *
173 * @return array
174 */
175 function hurryt_settings() {
176 $defaults = ['disable_actions' => 0];
177
178 return wp_parse_args(get_option('hurryt_settings', []), $defaults);
179 }
180 }
181
182 if (!function_exists('hurryt_get_campaign')) {
183 /**
184 * Create a campaign instance with all props.
185 * @param $id
186 *
187 * @return Campaign
188 */
189 function hurryt_get_campaign($id) {
190 $campaign = new Campaign($id);
191 $campaign->loadSettings();
192
193 return $campaign;
194 }
195 }
196
197 if (!function_exists('hurryt_is_admin_area')) {
198 function hurryt_is_admin_area() {
199 global $wp;
200 wp_parse_str($wp->query_string, $params);
201
202 return is_admin()
203 || is_preview()
204 || is_customize_preview()
205 || hurrytimer_is_elementor_edit_mode()
206 || hurrytimer_is_elementor_preview_mode()
207 || !empty($params['preview'])
208 || !empty($params['fl_builder']);
209 }
210 }
211
212 if (!function_exists('hurryt_parse_campaigns')) {
213
214 /**
215 * Parse campaigns shortcodes in the given string content.
216 */
217 function hurryt_parse_campaigns($content) {
218 $pattern = get_shortcode_regex();
219 $tag = 'hurrytimer';
220 $ids = [];
221 if (
222 preg_match_all('/' . $pattern . '/s', $content, $matches)
223 && array_key_exists(2, $matches)
224 && in_array($tag, $matches[2])
225 ) {
226 foreach ((array)$matches[2] as $key => $value) {
227 if ($tag === $value) {
228 $ids[] = shortcode_parse_atts($matches[3][$key]);
229 }
230 }
231 $ids = array_map(function ($id) {
232 return isset($id['id']) ? absint($id['id']) : null;
233 }, $ids);
234
235 return array_filter($ids);
236 }
237
238 return [];
239 }
240 }
241
242 if (!function_exists('hurryt_count_active_campaigns')) {
243 /**
244 * Returns published campaigns.
245 */
246 function hurryt_count_active_campaigns() {
247
248 /** @var object $count */
249 $count = wp_count_posts(HURRYT_POST_TYPE);
250
251 return absint($count->publish);
252 }
253 }
254
255
256 /**
257 * Return all WooCommerce coupons.
258 * @param string $status
259 * @access private
260 * @internal
261 * @return array
262 */
263 function hurryt_get_wc_coupons($status = 'any') {
264
265 return get_posts([
266 'post_type' => 'shop_coupon',
267 'post_status' => $status,
268 'no_found_rows' => true,
269 'numberposts' => 30
270 ]);
271 }
272
273
274 function hurryt_wc_coupon_name($coupon_id) {
275 if (!function_exists('wc_get_coupon_types')) {
276 return '';
277 }
278
279 $type_names = wc_get_coupon_types();
280
281 $coupon_type = get_post_meta($coupon_id, 'discount_type', true);
282
283 return isset($type_names[$coupon_type]) ? $type_names[$coupon_type] : '';
284 }
285
286 /**
287 * Return the current timezone string.
288 * @return string
289 */
290 function hurryt_current_timezone_string() {
291 $current_offset = get_option('gmt_offset');
292 $tzstring = get_option('timezone_string');
293
294 if (false !== strpos($tzstring, 'Etc/GMT')) {
295 $tzstring = '';
296 }
297
298 if (empty($tzstring)) {
299 if (0 == $current_offset) {
300 $tzstring = 'UTC+0';
301 } elseif ($current_offset < 0) {
302 $tzstring = 'UTC' . $current_offset;
303 } else {
304 $tzstring = 'UTC+' . $current_offset;
305 }
306 }
307 return $tzstring;
308 }
309
310
311 function hurrytimer_is_elementor_edit_mode() {
312 try {
313 return class_exists('\Elementor\Plugin') && \Elementor\Plugin::$instance->editor->is_edit_mode();
314 } catch (\Exception $e) {
315 return false;
316 }
317 }
318
319 function hurrytimer_is_elementor_preview_mode() {
320 try {
321 return class_exists('\Elementor\Plugin') && \Elementor\Plugin::$instance->preview->is_preview_mode();
322 } catch (\Exception $e) {
323 return false;
324 }
325 }
326
327
328 function hurrytimer_allow_unfiltered_html() {
329
330 $allow_unfiltered_html = current_user_can('unfiltered_html');
331
332 /**
333 * Filters whether the current user is allowed to save unfiltered HTML.
334 *
335 * @param bool allow_unfiltered_html The result.
336 */
337 return apply_filters( 'hurrytimer/allow_unfiltered_html', $allow_unfiltered_html );
338 }
339
340 function hurrytimer_is_pro(){
341 return defined( 'HURRYT_IS_PRO' ) && HURRYT_IS_PRO;
342 }