/var/www/(Del)hsihk.com/wp-content/plugins/woocommerce/includes/wc-formatting-functions.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<?php
/**
 * WooCommerce Formatting
 *
 * Functions for formatting data.
 *
 * @author         WooThemes
 * @category     Core
 * @package     WooCommerce/Functions
 * @version     2.1.0
 */

if ( ! defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly
}

/**
 * Sanitize taxonomy names. Slug format (no spaces, lowercase).
 *
 * urldecode is used to reverse munging of UTF8 characters.
 *
 * @param mixed $taxonomy
 * @return string
 */
function wc_sanitize_taxonomy_name$taxonomy ) {
    return 
apply_filters'sanitize_taxonomy_name'urldecodesanitize_title$taxonomy ) ), $taxonomy );
}

/**
 * Gets the filename part of a download URL
 *
 * @param string $file_url
 * @return string
 */
function wc_get_filename_from_url$file_url ) {
    
$parts parse_url$file_url );
    if ( isset( 
$parts['path'] ) ) {
        return 
basename$parts['path'] );
    }
}

/**
 * Normalise dimensions, unify to cm then convert to wanted unit value
 *
 * Usage: wc_get_dimension(55, 'in');
 *
 * @param mixed $dim
 * @param mixed $to_unit 'in', 'm', 'cm', 'm'
 * @return float
 */
function wc_get_dimension$dim$to_unit ) {

    
$from_unit     strtolowerget_option'woocommerce_dimension_unit' ) );
    
$to_unit    strtolower$to_unit );

    
// Unify all units to cm first
    
if ( $from_unit !== $to_unit ) {

        switch ( 
$from_unit ) {
            case 
'in':
                
$dim *= 2.54;
            break;
            case 
'm':
                
$dim *= 100;
            break;
            case 
'mm':
                
$dim *= 0.1;
            break;
            case 
'yd':
                
$dim *= 91.44;
            break;
        }

        
// Output desired unit
        
switch ( $to_unit ) {
            case 
'in':
                
$dim *= 0.3937;
            break;
            case 
'm':
                
$dim *= 0.01;
            break;
            case 
'mm':
                
$dim *= 10;
            break;
            case 
'yd':
                
$dim *= 0.010936133;
            break;
        }
    }
    return ( 
$dim ) ? $dim;
}

/**
 * Normalise weights, unify to kg then convert to wanted unit value
 *
 * Usage: wc_get_weight(55, 'kg');
 *
 * @param mixed $weight
 * @param mixed $to_unit 'g', 'kg', 'lbs'
 * @return float
 */
function wc_get_weight$weight$to_unit ) {

    
$from_unit     strtolowerget_option('woocommerce_weight_unit') );
    
$to_unit    strtolower$to_unit );

    
//Unify all units to kg first
    
if ( $from_unit !== $to_unit ) {

        switch ( 
$from_unit ) {
            case 
'g':
                
$weight *= 0.001;
            break;
            case 
'lbs':
                
$weight *= 0.4536;
            break;
            case 
'oz':
                
$weight *= 0.0283;
            break;
        }

        
// Output desired unit
        
switch ( $to_unit ) {
            case 
'g':
                
$weight *= 1000;
            break;
            case 
'lbs':
                
$weight *= 2.2046;
            break;
            case 
'oz':
                
$weight *= 35.274;
            break;
        }
    }
    return ( 
$weight ) ? $weight;
}

/**
 * Trim trailing zeros off prices.
 *
 * @param mixed $price
 * @return string
 */
function wc_trim_zeros$price ) {
    return 
preg_replace'/' preg_quoteget_option'woocommerce_price_decimal_sep' ), '/' ) . '0++$/'''$price );
}

/**
 * Round a tax amount
 *
 * @param mixed $tax
 * @return double
 */
function wc_round_tax_total$tax ) {
    
$dp = (int) get_option'woocommerce_price_num_decimals' );

    
// @codeCoverageIgnoreStart
    
if ( version_comparephpversion(), '5.3''<' ) ) {
        
$tax round$tax$dp );
    } else {
        
// @codeCoverageIgnoreEnd
        
$tax round$tax$dpWC_TAX_ROUNDING_MODE );
    }
    return 
$tax;
}

/**
 * Make a refund total negative
 * @return float
 */
function wc_format_refund_total$amount ) {
    return 
$amount * -1;
}

/**
 * Format decimal numbers ready for DB storage
 *
 * Sanitize, remove locale formatting, and optionally round + trim off zeros
 *
 * @param  float|string $number Expects either a float or a string with a decimal separator only (no thousands)
 * @param  mixed $dp number of decimal points to use, blank to use woocommerce_price_num_decimals, or false to avoid all rounding.
 * @param  boolean $trim_zeros from end of string
 * @return string
 */
function wc_format_decimal$number$dp false$trim_zeros false ) {
    
// Remove locale from string
    
if ( ! is_float$number ) ) {
        
$locale   localeconv();
        
$decimals = array( get_option'woocommerce_price_decimal_sep' ), $locale['decimal_point'], $locale['mon_decimal_point'] );
        
$number   wc_cleanstr_replace$decimals'.'$number ) );
    }

    
// DP is false - don't use number format, just return a string in our format
    
if ( $dp !== false ) {
        
$dp     intval$dp == "" get_option'woocommerce_price_num_decimals' ) : $dp );
        
$number number_formatfloatval$number ), $dp'.''' );
    }

    if ( 
$trim_zeros && strstr$number'.' ) ) {
        
$number rtrimrtrim$number'0' ), '.' );
    }

    return 
$number;
}

/**
 * Convert a float to a string without locale formatting which PHP adds when changing floats to strings
 * @param  float $float
 * @return string
 */
function wc_float_to_string$float ) {
    if ( ! 
is_float$float ) ) {
        return 
$float;
    }

    
$locale localeconv();
    
$string strval$float );
    
$string str_replace$locale['decimal_point'], '.'$string );

    return 
$string;
}

/**
 * Format a price with WC Currency Locale settings
 * @param  string $value
 * @return string
 */
function wc_format_localized_price$value ) {
    return 
str_replace'.'get_option'woocommerce_price_decimal_sep' ), strval$value ) );
}

/**
 * Format a decimal with PHP Locale settings
 * @param  string $value
 * @return string
 */
function wc_format_localized_decimal$value ) {
    
$locale localeconv();
    return 
str_replace'.'$locale['decimal_point'], strval$value ) );
}

/**
 * Clean variables
 *
 * @param string $var
 * @return string
 */
function wc_clean$var ) {
    return 
sanitize_text_field$var );
}

/**
 * Merge two arrays
 *
 * @param array $a1
 * @param array $a2
 * @return array
 */
function wc_array_overlay$a1$a2 ) {
    foreach ( 
$a1 as $k => $v ) {
        if ( ! 
array_key_exists$k$a2 ) ) {
            continue;
        }
        if ( 
is_array$v ) && is_array$a2$k ] ) ) {
            
$a1$k ] = wc_array_overlay$v$a2$k ] );
        } else {
            
$a1$k ] = $a2$k ];
        }
    }
    return 
$a1;
}

/**
 * Formats a stock amount by running it through a filter
 * @param  int|float $amount
 * @return int|float
 */
function wc_stock_amount$amount ) {
    return 
apply_filters'woocommerce_stock_amount'$amount );
}

/**
 * Get the price format depending on the currency position
 *
 * @return string
 */
function get_woocommerce_price_format() {
    
$currency_pos get_option'woocommerce_currency_pos' );

    switch ( 
$currency_pos ) {
        case 
'left' :
            
$format '%1$s%2$s';
        break;
        case 
'right' :
            
$format '%2$s%1$s';
        break;
        case 
'left_space' :
            
$format '%1$s&nbsp;%2$s';
        break;
        case 
'right_space' :
            
$format '%2$s&nbsp;%1$s';
        break;
    }

    return 
apply_filters'woocommerce_price_format'$format$currency_pos );
}

/**
 * Format the price with a currency symbol.
 *
 * @param float $price
 * @param array $args (default: array())
 * @return string
 */
function wc_price$price$args = array() ) {
    
extractshortcode_atts( array(
        
'ex_tax_label'     => '0'
    
), $args ) );

    
$return          '';
    
$num_decimals    absintget_option'woocommerce_price_num_decimals' ) );
    
$currency        = isset( $args['currency'] ) ? $args['currency'] : '';
    
$currency_symbol get_woocommerce_currency_symbol($currency);
    
$decimal_sep     wp_specialchars_decodestripslashesget_option'woocommerce_price_decimal_sep' ) ), ENT_QUOTES );
    
$thousands_sep   wp_specialchars_decodestripslashesget_option'woocommerce_price_thousand_sep' ) ), ENT_QUOTES );

    if ( 
$price ) {
        
$price    $price * -1;
        
$negative true;
    } else {
        
$negative false;
    }

    
$price           apply_filters'raw_woocommerce_price'floatval$price ) );
    
$price           apply_filters'formatted_woocommerce_price'number_format$price$num_decimals$decimal_sep$thousands_sep ), $price$num_decimals$decimal_sep$thousands_sep );

    if ( 
apply_filters'woocommerce_price_trim_zeros'false ) && $num_decimals ) {
        
$price wc_trim_zeros$price );
    }

    
$formatted_price = ( $negative '-' '' ) . sprintfget_woocommerce_price_format(), $currency_symbol$price );
    
$return          '<span class="amount">' $formatted_price '</span>';

    if ( 
$ex_tax_label && get_option'woocommerce_calc_taxes' ) == 'yes' ) {
        
$return .= ' <small>' WC()->countries->ex_tax_or_vat() . '</small>';
    }

    return 
apply_filters'wc_price'$return$price$args );
}

/**
 * let_to_num function.
 *
 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
 *
 * @param $size
 * @return int
 */
function wc_let_to_num$size ) {
    
$l   substr$size, -);
    
$ret substr$size0, -);
    switch ( 
strtoupper$l ) ) {
        case 
'P':
            
$ret *= 1024;
        case 
'T':
            
$ret *= 1024;
        case 
'G':
            
$ret *= 1024;
        case 
'M':
            
$ret *= 1024;
        case 
'K':
            
$ret *= 1024;
    }
    return 
$ret;
}

/**
 * WooCommerce Date Format - Allows to change date format for everything WooCommerce
 *
 * @return string
 */
function wc_date_format() {
    return 
apply_filters'woocommerce_date_format'get_option'date_format' ) );
}

/**
 * WooCommerce Time Format - Allows to change time format for everything WooCommerce
 *
 * @return string
 */
function wc_time_format() {
    return 
apply_filters'woocommerce_time_format'get_option'time_format' ) );
}

/**
 * WooCommerce Timezone - helper to retrieve the timezone string for a site until
 * a WP core method exists (see http://core.trac.wordpress.org/ticket/24730)
 *
 * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155
 *
 * @since 2.1
 * @return string a valid PHP timezone string for the site
 */
function wc_timezone_string() {

    
// if site timezone string exists, return it
    
if ( $timezone get_option'timezone_string' ) ) {
        return 
$timezone;
    }

    
// get UTC offset, if it isn't set then return UTC
    
if ( === ( $utc_offset get_option'gmt_offset') ) ) {
        return 
'UTC';
    }

    
// adjust UTC offset from hours to seconds
    
$utc_offset *= 3600;

    
// attempt to guess the timezone string from the UTC offset
    
$timezone timezone_name_from_abbr''$utc_offset );

    
// last try, guess timezone string manually
    
if ( false === $timezone ) {

        
$is_dst date'I' );

        foreach ( 
timezone_abbreviations_list() as $abbr ) {
            foreach ( 
$abbr as $city ) {

                if ( 
$city['dst'] == $is_dst && $city['offset'] == $utc_offset ) {
                    return 
$city['timezone_id'];
                }
            }
        }
    }

    
// fallback to UTC
    
return 'UTC';
}

if ( ! 
function_exists'wc_rgb_from_hex' ) ) {

    
/**
     * Hex darker/lighter/contrast functions for colours
     *
     * @param mixed $color
     * @return string
     */
    
function wc_rgb_from_hex$color ) {
        
$color str_replace'#'''$color );
        
// Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
        
$color preg_replace'~^(.)(.)(.)$~''$1$1$2$2$3$3'$color );

        
$rgb['R'] = hexdec$color{0}.$color{1} );
        
$rgb['G'] = hexdec$color{2}.$color{3} );
        
$rgb['B'] = hexdec$color{4}.$color{5} );
        return 
$rgb;
    }
}

if ( ! 
function_exists'wc_hex_darker' ) ) {

    
/**
     * Hex darker/lighter/contrast functions for colours
     *
     * @param mixed $color
     * @param int $factor (default: 30)
     * @return string
     */
    
function wc_hex_darker$color$factor 30 ) {
        
$base  wc_rgb_from_hex$color );
        
$color '#';

        foreach ( 
$base as $k => $v ) {
            
$amount      $v 100;
            
$amount      round$amount $factor );
            
$new_decimal $v $amount;

            
$new_hex_component dechex$new_decimal );
            if ( 
strlen$new_hex_component ) < ) {
                
$new_hex_component "0" $new_hex_component;
            }
            
$color .= $new_hex_component;
        }

        return 
$color;
    }
}

if ( ! 
function_exists'wc_hex_lighter' ) ) {

    
/**
     * Hex darker/lighter/contrast functions for colours
     *
     * @param mixed $color
     * @param int $factor (default: 30)
     * @return string
     */
    
function wc_hex_lighter$color$factor 30 ) {
        
$base  wc_rgb_from_hex$color );
        
$color '#';

        foreach ( 
$base as $k => $v ) {
            
$amount      255 $v;
            
$amount      $amount 100;
            
$amount      round$amount $factor );
            
$new_decimal $v $amount;

            
$new_hex_component dechex$new_decimal );
            if ( 
strlen$new_hex_component ) < ) {
                
$new_hex_component "0" $new_hex_component;
            }
            
$color .= $new_hex_component;
        }

        return 
$color;
    }
}

if ( ! 
function_exists'wc_light_or_dark' ) ) {

    
/**
     * Detect if we should use a light or dark colour on a background colour
     *
     * @param mixed $color
     * @param string $dark (default: '#000000')
     * @param string $light (default: '#FFFFFF')
     * @return string
     */
    
function wc_light_or_dark$color$dark '#000000'$light '#FFFFFF' ) {

        
$hex str_replace'#'''$color );

        
$c_r hexdecsubstr$hex0) );
        
$c_g hexdecsubstr$hex2) );
        
$c_b hexdecsubstr$hex4) );

        
$brightness = ( ( $c_r 299 ) + ( $c_g 587 ) + ( $c_b 114 ) ) / 1000;

        return 
$brightness 155 $dark $light;
    }
}

if ( ! 
function_exists'wc_format_hex' ) ) {

    
/**
     * Format string as hex
     *
     * @param string $hex
     * @return string
     */
    
function wc_format_hex$hex ) {

        
$hex trimstr_replace'#'''$hex ) );

        if ( 
strlen$hex ) == ) {
            
$hex $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
        }

        return 
$hex '#' $hex null;
    }
}

/**
 * Format the postcode according to the country and length of the postcode
 *
 * @param string postcode
 * @param string country
 * @return string formatted postcode
 */
function wc_format_postcode$postcode$country ) {
    
$postcode strtouppertrim$postcode ) );
    
$postcode trimpreg_replace'/[\s]/'''$postcode ) );

    if ( 
in_array$country, array( 'GB''CA' ) ) ) {
        
$postcode trimsubstr_replace$postcode' ', -3) );
    }

    return 
$postcode;
}

/**
 * format_phone function.
 *
 * @param mixed $tel
 * @return string
 */
function wc_format_phone_number$tel ) {
    
$tel str_replace'.''-'$tel );
    return 
$tel;
}

/**
 * Make a string lowercase.
 * Try to use mb_strtolower() when available.
 *
 * @since  2.3
 * @param  string $string
 * @return string
 */
function wc_strtolower$string ) {
    return 
function_exists'mb_strtolower' ) ? mb_strtolower$string ) : strtolower$string );
}