PluginProbe
WooReer / 2.2.3
WooReer v2.2.3
trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 3.0.0 3.0.1 All 34 releases
wcsdm / includes / helpers.php

helpers.php in WooReer 2.2.3, at includes/helpers.php

410 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helpers file
4 *
5 * @link https://github.com/sofyansitorus
6 * @since 1.5.0
7 *
8 * @package Wcsdm
9 * @subpackage Wcsdm/includes
10 */
11
12 // If this file is called directly, abort.
13 if ( ! defined( 'ABSPATH' ) ) {
14 die;
15 }
16
17 /**
18 * Check if plugin is active
19 *
20 * @param string $plugin_file Plugin file name.
21 */
22 function wcsdm_is_plugin_active( $plugin_file ) {
23 $active_plugins = (array) apply_filters( 'active_plugins', get_option( 'active_plugins', array() ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
24
25 if ( is_multisite() ) {
26 $active_plugins = array_merge( $active_plugins, (array) get_site_option( 'active_sitewide_plugins', array() ) );
27 }
28
29 return in_array( $plugin_file, $active_plugins, true ) || array_key_exists( $plugin_file, $active_plugins );
30 }
31
32 /**
33 * Get i18n strings
34 *
35 * @param string $key Strings key.
36 * @param string $default Default value.
37 * @return mixed
38 */
39 function wcsdm_i18n( $key = '', $default = '' ) {
40 $i18n = array(
41 'Add New Rate' => __( 'Add New Rate', 'wcsdm' ),
42 'Address' => __( 'Address', 'wcsdm' ),
43 'Apply Changes' => __( 'Apply Changes', 'wcsdm' ),
44 'Cancel' => __( 'Cancel', 'wcsdm' ),
45 'Confirm Delete' => __( 'Confirm Delete', 'wcsdm' ),
46 'Delete Selected Rates' => __( 'Delete Selected Rates', 'wcsdm' ),
47 'Please drag this marker or enter your address in the input field above.' => __( 'Please drag this marker or enter your address in the input field above.', 'wcsdm' ),
48 'Latitude' => __( 'Latitude', 'wcsdm' ),
49 'Longitude' => __( 'Longitude', 'wcsdm' ),
50 'Save Changes' => __( 'Save Changes', 'wcsdm' ),
51 'Store Location Picker' => __( 'Store Location Picker', 'wcsdm' ),
52 // translators: %s = Field name.
53 '%s field is required.' => __( '%s field is required.', 'wcsdm' ),
54 // translators: %s = Field name.
55 '%s field value must be numeric.' => __( '%s field value must be numeric.', 'wcsdm' ),
56 // translators: %1$s = Field name, %2$d = Minimum field value rule.
57 '%1$s field value cannot be lower than %2$.d' => __( '%1$s field value cannot be lower than %2$.d', 'wcsdm' ),
58 // translators: %1$s = Field name, %2$d = Maximum field value rule.
59 '%1$s field value cannot be greater than %2$d' => __( '%1$s field value cannot be greater than %2$d', 'wcsdm' ),
60 // translators: %1$d = row number, %2$s = error message.
61 'Shipping rules combination duplicate with rate row #%1$d: %2$s.' => __( 'Shipping rules combination duplicate with rate row #%1$d: %2$s.', 'wcsdm' ),
62 // translators: %1$d = row number, %2$s = error message.
63 'Table rate row #%1$d: %2$s.' => __( 'Table rate row #%1$d: %2$s.', 'wcsdm' ),
64 'Table rates data is incomplete or invalid!' => __( 'Table rates data is incomplete or invalid!', 'wcsdm' ),
65 );
66
67 if ( ! empty( $key ) && is_string( $key ) ) {
68 if ( isset( $i18n[ $key ] ) ) {
69 return $i18n[ $key ];
70 }
71
72 if ( '' !== $default ) {
73 return $default;
74 }
75
76 return $key;
77 }
78
79 return $i18n;
80 }
81
82 /**
83 * Get shipping method instances
84 *
85 * @since 2.0
86 *
87 * @param bool $enabled_only Filter to includes only enabled instances.
88 * @return array
89 */
90 function wcsdm_instances( $enabled_only = true ) {
91 $instances = array();
92
93 $zone_data_store = new WC_Shipping_Zone_Data_Store();
94
95 $shipping_methods = $zone_data_store->get_methods( '0', $enabled_only );
96
97 if ( $shipping_methods ) {
98 foreach ( $shipping_methods as $shipping_method ) {
99 if ( WCSDM_METHOD_ID !== $shipping_method->method_id ) {
100 continue;
101 }
102
103 $instances[] = array(
104 'zone_id' => 0,
105 'method_id' => $shipping_method->method_id,
106 'instance_id' => $shipping_method->instance_id,
107 );
108 }
109 }
110
111 $zones = WC_Shipping_Zones::get_zones();
112
113 if ( ! empty( $zones ) ) {
114 foreach ( $zones as $zone ) {
115 $shipping_methods = $zone_data_store->get_methods( $zone['id'], $enabled_only );
116 if ( $shipping_methods ) {
117 foreach ( $shipping_methods as $shipping_method ) {
118 if ( WCSDM_METHOD_ID !== $shipping_method->method_id ) {
119 continue;
120 }
121
122 $instances[] = array(
123 'zone_id' => 0,
124 'method_id' => $shipping_method->method_id,
125 'instance_id' => $shipping_method->instance_id,
126 );
127 }
128 }
129 }
130 }
131
132 return apply_filters( 'wcsdm_instances', $instances );
133 }
134
135 /**
136 * Inserts a new key/value before the key in the array.
137 *
138 * @since 2.0.7
139 *
140 * @param string $before_key The key to insert before.
141 * @param array $array An array to insert in to.
142 * @param string $new_key The new key to insert.
143 * @param mixed $new_value The new value to insert.
144 *
145 * @return array
146 */
147 function wcsdm_array_insert_before( $before_key, $array, $new_key, $new_value ) {
148 if ( ! array_key_exists( $before_key, $array ) ) {
149 return $array;
150 }
151
152 $new = array();
153
154 foreach ( $array as $k => $value ) {
155 if ( $k === $before_key ) {
156 $new[ $new_key ] = $new_value;
157 }
158
159 $new[ $k ] = $value;
160 }
161
162 return $new;
163 }
164
165 /**
166 * Inserts a new key/value after the key in the array.
167 *
168 * @since 2.0.7
169 *
170 * @param string $after_key The key to insert after.
171 * @param array $array An array to insert in to.
172 * @param string $new_key The new key to insert.
173 * @param mixed $new_value The new value to insert.
174 *
175 * @return array
176 */
177 function wcsdm_array_insert_after( $after_key, $array, $new_key, $new_value ) {
178 if ( ! array_key_exists( $after_key, $array ) ) {
179 return $array;
180 }
181
182 $new = array();
183
184 foreach ( $array as $k => $value ) {
185 $new[ $k ] = $value;
186
187 if ( $k === $after_key ) {
188 $new[ $new_key ] = $new_value;
189 }
190 }
191
192 return $new;
193 }
194
195 /**
196 * Check is in development environment.
197 *
198 * @since 1.0.0
199 *
200 * @return bool
201 */
202 function wcsdm_is_dev_env() {
203 if ( defined( 'WCSDM_DEV' ) && WCSDM_DEV ) {
204 return true;
205 }
206
207 if ( function_exists( 'getenv' ) && getenv( 'WCSDM_DEV' ) ) {
208 return true;
209 }
210
211 return false;
212 }
213
214 if ( ! function_exists( 'wcsdm_autoload' ) ) :
215 /**
216 * Class autoload
217 *
218 * @since 1.0.0
219 *
220 * @param string $class Class name.
221 *
222 * @return void
223 */
224 function wcsdm_autoload( $class ) {
225 $class = strtolower( $class );
226
227 if ( strpos( $class, 'wcsdm' ) !== 0 ) {
228 return;
229 }
230
231 if ( strpos( $class, 'wcsdm_migration_' ) === 0 ) {
232 require_once WCSDM_PATH . 'includes/migrations/class-' . str_replace( '_', '-', $class ) . '.php';
233 } else {
234 require_once WCSDM_PATH . 'includes/classes/class-' . str_replace( '_', '-', $class ) . '.php';
235 }
236 }
237 endif;
238
239 if ( ! function_exists( 'wcsdm_is_calc_shipping' ) ) :
240 /**
241 * Check if current request is shipping calculator form.
242 *
243 * @since 1.0.0
244 *
245 * @return bool
246 */
247 function wcsdm_is_calc_shipping() {
248 $field = 'woocommerce-shipping-calculator-nonce';
249 $action = 'woocommerce-shipping-calculator';
250
251 if ( isset( $_POST['calc_shipping'], $_POST[ $field ] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ $field ] ) ), $action ) ) {
252 return true;
253 }
254
255 return false;
256 }
257 endif;
258
259 if ( ! function_exists( 'wcsdm_calc_shipping_field_value' ) ) :
260 /**
261 * Get calculated shipping for fields value.
262 *
263 * @since 2.1.3
264 *
265 * @param string $input_name Input name.
266 *
267 * @return mixed|bool False on failure
268 */
269 function wcsdm_calc_shipping_field_value( $input_name ) {
270 $nonce_field = 'woocommerce-shipping-calculator-nonce';
271 $nonce_action = 'woocommerce-shipping-calculator';
272
273 if ( isset( $_POST['calc_shipping'], $_POST[ $input_name ], $_POST[ $nonce_field ] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ $nonce_field ] ) ), $nonce_action ) ) {
274 return sanitize_text_field( wp_unslash( $_POST[ $input_name ] ) );
275 }
276
277 return false;
278 }
279 endif;
280
281 if ( ! function_exists( 'wcsdm_sort_address_fields' ) ) :
282 /**
283 * Get address fields.
284 *
285 * @since 2.1.6
286 *
287 * @param array $a Array 1 that will be sorted.
288 * @param array $b Array 2 that will be compared.
289 * @return int
290 */
291 function wcsdm_sort_address_fields( $a, $b ) {
292 if ( $a['priority'] === $b['priority'] ) {
293 return 0;
294 }
295
296 return ( $a['priority'] > $b['priority'] ) ? -1 : 1;
297 }
298 endif;
299
300 if ( ! function_exists( 'wcsdm_include_address_fields' ) ) :
301 /**
302 * Get included address fields keys.
303 *
304 * @since 2.1.6
305 *
306 * @return array
307 */
308 function wcsdm_include_address_fields() {
309 return array(
310 'address_1',
311 'address_2',
312 'city',
313 'state',
314 'country',
315 'postcode',
316 );
317 }
318 endif;
319
320 if ( ! function_exists( 'wcsdm_address_fields' ) ) :
321 /**
322 * Get address fields.
323 *
324 * @since 2.1.6
325 *
326 * @param string $address_type Address type: billing or shipping.
327 * @return array|bool Will return false on failure.
328 */
329 function wcsdm_address_fields( $address_type ) {
330 if ( 'shipping' === $address_type && wc_ship_to_billing_address_only() ) {
331 $address_type = 'billing';
332 }
333
334 $includes = wcsdm_include_address_fields();
335
336 $fields = array();
337
338 foreach ( WC()->checkout->get_checkout_fields( $address_type ) as $key => $field ) {
339 $keys = explode( '_', $key );
340
341 if ( isset( $keys[0] ) && $address_type === $keys[0] ) {
342 unset( $keys[0] );
343 }
344
345 $key = implode( '_', $keys );
346
347 if ( ! in_array( $key, $includes, true ) ) {
348 continue;
349 }
350
351 $fields[ $key ] = $field;
352 }
353
354 if ( $fields ) {
355 uasort( $fields, 'wcsdm_sort_address_fields' );
356 }
357
358 return $fields;
359 }
360 endif;
361
362 if ( ! function_exists( 'wcsdm_shipping_fields' ) ) :
363 /**
364 * Get shipping fields.
365 *
366 * @since 2.1.5
367 *
368 * @return array
369 */
370 function wcsdm_shipping_fields() {
371 return wcsdm_address_fields( 'shipping' );
372 }
373 endif;
374
375 if ( ! function_exists( 'wcsdm_billing_fields' ) ) :
376 /**
377 * Get billing fields.
378 *
379 * @since 2.1.6
380 *
381 * @return array
382 */
383 function wcsdm_billing_fields() {
384 return wcsdm_address_fields( 'billing' );
385 }
386 endif;
387
388 if ( ! function_exists( 'wcsdm_calc_shipping_fields' ) ) :
389 /**
390 * Get shipping calculator fields.
391 *
392 * @since 2.1.6
393 *
394 * @return array
395 */
396 function wcsdm_calc_shipping_fields() {
397 $fields = array();
398
399 foreach ( wcsdm_address_fields( 'shipping' ) as $key => $field ) {
400 if ( ! apply_filters( 'woocommerce_shipping_calculator_enable_' . $key, true ) ) { // phpcs:ignore WordPress.NamingConventions
401 continue;
402 }
403
404 $fields[ $key ] = $field;
405 }
406
407 return $fields;
408 }
409 endif;
410