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

277 lines 7.4 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
75 if ( !function_exists( 'hurryt_tz' ) ) {
76 /**
77 * Return WP timezone string/offset.
78 *
79 * @return mixed|string|void
80 */
81 function hurryt_tz()
82 {
83 $tz_string = get_option( 'timezone_string' );
84 if ( !empty( $tz_string ) ) {
85 return $tz_string;
86 }
87 $offset = get_option( 'gmt_offset' );
88 $hours = (int)$offset;
89 $minutes = abs( ( $offset - (int)$offset ) * 60 );
90 $offset = sprintf( '%+03d:%02d', $hours, $minutes );
91
92 return $offset;
93 }
94 }
95
96 if ( !function_exists( 'hurryt_current_page_id' ) ) {
97
98 /**
99 * Get current page ID.
100 *
101 * @return int|mixed
102 */
103 function hurryt_current_page_id()
104 {
105 $object_id = get_queried_object_id();
106 if ( !hurryt_is_woocommerce_activated() ) {
107 return $object_id;
108 }
109 $wc_ids = [
110 'shop' => get_option( 'woocommerce_shop_page_id' ),
111 'cart' => get_option( 'woocommerce_cart_page_id' ),
112 'checkout' => get_option( 'woocommerce_checkout_page_id' ),
113 'checkout_pay' => get_option( 'woocommerce_pay_page_id' ),
114 'thanks' => get_option( 'woocommerce_thanks_page_id' ),
115 'myaccount' => get_option( 'woocommerce_myaccount_page_id' ),
116 'edit_address' => get_option( 'woocommerce_edit_address_page_id' ),
117 'view_order' => get_option( 'woocommerce_view_order_page_id' ),
118 'terms' => get_option( 'woocommerce_terms_page_id' ),
119 ];
120 if ( is_shop() ) {
121 $object_id = $wc_ids[ 'shop' ];
122 } elseif ( is_account_page() ) {
123 $object_id = $wc_ids[ 'myaccount' ];
124 } elseif ( is_checkout_pay_page() ) {
125 $object_id = $wc_ids[ 'checkout_pay' ];
126 } elseif ( is_checkout() ) {
127 $object_id = $wc_ids[ 'checkout' ];
128 } elseif ( is_cart() ) {
129 $object_id = $wc_ids[ 'cart' ];
130 } elseif ( is_view_order_page() ) {
131 $object_id = $wc_ids[ 'view_order' ];
132 } elseif ( is_view_order_page() ) {
133 $object_id = $wc_ids[ 'view_order' ];
134 } elseif ( is_view_order_page() ) {
135 $object_id = $wc_ids[ 'view_order' ];
136 } elseif ( is_view_order_page() ) {
137 $object_id = $wc_ids[ 'view_order' ];
138 }
139
140 return $object_id;
141 }
142 }
143
144 if ( !function_exists( 'hurryt_is_woocommerce_activated' ) ) {
145 /**
146 * Returns true if WC is active.
147 *
148 * @return bool
149 */
150 function hurryt_is_woocommerce_activated()
151 {
152 return class_exists( 'woocommerce' );
153 }
154 }
155
156 if ( !function_exists( 'hurryt_settings' ) ) {
157 /**
158 * Returns plugin settings array.
159 *
160 * @return array
161 */
162 function hurryt_settings()
163 {
164 $defaults = [ 'disable_actions' => 0 ];
165
166 return wp_parse_args( get_option( 'hurryt_settings', [] ), $defaults );
167 }
168 }
169
170 if ( !function_exists( 'hurryt_get_campaign' ) ) {
171 /**
172 * Create a campaign instance with all props.
173 * @param $id
174 *
175 * @return Campaign
176 */
177 function hurryt_get_campaign( $id )
178 {
179 $campaign = new Campaign( $id );
180 $campaign->loadSettings();
181
182 return $campaign;
183 }
184 }
185
186 if ( !function_exists( 'hurryt_is_admin_area' ) ) {
187 function hurryt_is_admin_area()
188 {
189 global $wp;
190 wp_parse_str( $wp->query_string, $params );
191
192 return is_admin()
193 || is_preview()
194 || is_customize_preview()
195 || !empty( $params[ 'preview' ] )
196 || !empty( $params[ 'fl_builder' ] );
197 }
198 }
199
200 if ( !function_exists( 'hurryt_parse_campaigns' ) ) {
201
202 /**
203 * Parse campaigns shortcodes in the given string content.
204 */
205 function hurryt_parse_campaigns( $content )
206 {
207 $pattern = get_shortcode_regex();
208 $tag = 'hurrytimer';
209 $ids = [];
210 if ( preg_match_all( '/' . $pattern . '/s', $content, $matches )
211 && array_key_exists( 2, $matches )
212 && in_array( $tag, $matches[ 2 ] )
213 ) {
214 foreach ( (array)$matches[ 2 ] as $key => $value ) {
215 if ( $tag === $value ) {
216 $ids[] = shortcode_parse_atts( $matches[ 3 ][ $key ] );
217 }
218 }
219 $ids = array_map( function ( $id ) {
220 return isset( $id[ 'id' ] ) ? absint( $id[ 'id' ] ) : null;
221 }, $ids );
222
223 return array_filter( $ids );
224 }
225
226 return [];
227 }
228 }
229
230 if ( !function_exists( 'hurryt_count_active_campaigns' ) ) {
231 /**
232 * Returns published campaigns.
233 */
234 function hurryt_count_active_campaigns()
235 {
236
237 /** @var object $count */
238 $count = wp_count_posts( HURRYT_POST_TYPE );
239
240 return absint( $count->publish );
241 }
242 }
243
244
245 /**
246 * Return all WooCommerce coupons.
247 * @param string $status
248 * @return array
249 */
250 function hurryt_get_wc_coupons( $status = 'publish' )
251 {
252
253 return get_posts( [
254 'post_type' => 'shop_coupon',
255 'post_status' => $status,
256 'posts_per_page' => -1
257 ] );
258 }
259
260
261 function hurryt_wc_coupon_name( $coupon_id )
262 {
263 if ( !function_exists( 'wc_get_coupon_types' ) ) {
264 return '';
265 }
266
267 $type_names = wc_get_coupon_types();
268
269 $coupon_type = get_post_meta( $coupon_id, 'discount_type', true );
270
271 return isset( $type_names[ $coupon_type ] ) ? $type_names[ $coupon_type ] : '';
272
273 }
274
275
276
277