2013-08-09 16:11:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WooCommerce Formatting
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* Functions for formatting data.
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Core
|
|
|
|
* @package WooCommerce/Functions
|
|
|
|
* @version 2.1.0
|
|
|
|
*/
|
|
|
|
|
2014-09-20 19:09:16 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize taxonomy names. Slug format (no spaces, lowercase).
|
|
|
|
*
|
2014-05-07 14:02:00 +00:00
|
|
|
* urldecode is used to reverse munging of UTF8 characters.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param mixed $taxonomy
|
2013-11-23 17:59:21 +00:00
|
|
|
* @return string
|
2013-08-09 16:11:15 +00:00
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_sanitize_taxonomy_name( $taxonomy ) {
|
2014-05-07 14:02:00 +00:00
|
|
|
return apply_filters( 'sanitize_taxonomy_name', urldecode( sanitize_title( $taxonomy ) ), $taxonomy );
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Gets the filename part of a download URL.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param string $file_url
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_get_filename_from_url( $file_url ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
$parts = parse_url( $file_url );
|
2013-12-08 22:08:46 +00:00
|
|
|
if ( isset( $parts['path'] ) ) {
|
2013-10-25 19:02:21 +00:00
|
|
|
return basename( $parts['path'] );
|
2013-12-08 22:08:46 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Normalise dimensions, unify to cm then convert to wanted unit value.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
2013-11-25 13:34:21 +00:00
|
|
|
* Usage: wc_get_dimension(55, 'in');
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param mixed $dim
|
|
|
|
* @param mixed $to_unit 'in', 'm', 'cm', 'm'
|
|
|
|
* @return float
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_get_dimension( $dim, $to_unit ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
$from_unit = strtolower( get_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':
|
2013-11-18 12:31:51 +00:00
|
|
|
$dim *= 91.44;
|
2013-08-09 16:11:15 +00:00
|
|
|
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':
|
2013-11-18 12:31:51 +00:00
|
|
|
$dim *= 0.010936133;
|
2013-08-09 16:11:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ( $dim < 0 ) ? 0 : $dim;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Normalise weights, unify to kg then convert to wanted unit value.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
2013-11-25 13:34:21 +00:00
|
|
|
* Usage: wc_get_weight(55, 'kg');
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param mixed $weight
|
|
|
|
* @param mixed $to_unit 'g', 'kg', 'lbs'
|
|
|
|
* @return float
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_get_weight( $weight, $to_unit ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
$from_unit = strtolower( get_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':
|
2015-03-11 10:45:10 +00:00
|
|
|
$weight *= 0.453592;
|
2013-08-09 16:11:15 +00:00
|
|
|
break;
|
|
|
|
case 'oz':
|
2015-03-11 10:45:10 +00:00
|
|
|
$weight *= 0.0283495;
|
2013-08-09 16:11:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output desired unit
|
|
|
|
switch ( $to_unit ) {
|
|
|
|
case 'g':
|
|
|
|
$weight *= 1000;
|
|
|
|
break;
|
|
|
|
case 'lbs':
|
2015-03-11 10:45:10 +00:00
|
|
|
$weight *= 2.20462;
|
2013-08-09 16:11:15 +00:00
|
|
|
break;
|
|
|
|
case 'oz':
|
|
|
|
$weight *= 35.274;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ( $weight < 0 ) ? 0 : $weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trim trailing zeros off prices.
|
|
|
|
*
|
|
|
|
* @param mixed $price
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_trim_zeros( $price ) {
|
2015-01-23 11:50:32 +00:00
|
|
|
return preg_replace( '/' . preg_quote( wc_get_price_decimal_separator(), '/' ) . '0++$/', '', $price );
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
2013-10-24 12:15:42 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Round a tax amount.
|
2013-10-24 12:15:42 +00:00
|
|
|
*
|
2014-09-05 18:35:18 +00:00
|
|
|
* @param mixed $tax
|
2014-09-07 23:37:55 +00:00
|
|
|
* @return double
|
2013-10-24 12:15:42 +00:00
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_round_tax_total( $tax ) {
|
2015-01-23 11:50:32 +00:00
|
|
|
$dp = wc_get_price_decimals();
|
2013-10-24 12:41:42 +00:00
|
|
|
|
2014-09-05 18:35:18 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-10-24 12:41:42 +00:00
|
|
|
if ( version_compare( phpversion(), '5.3', '<' ) ) {
|
|
|
|
$tax = round( $tax, $dp );
|
|
|
|
} else {
|
2014-09-05 18:35:18 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2013-10-24 18:36:22 +00:00
|
|
|
$tax = round( $tax, $dp, WC_TAX_ROUNDING_MODE );
|
2013-10-24 12:41:42 +00:00
|
|
|
}
|
|
|
|
return $tax;
|
2013-10-24 12:15:42 +00:00
|
|
|
}
|
|
|
|
|
2014-07-24 14:34:14 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Make a refund total negative.
|
2014-07-24 14:34:14 +00:00
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
function wc_format_refund_total( $amount ) {
|
|
|
|
return $amount * -1;
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Format decimal numbers ready for DB storage.
|
2013-10-01 16:54:29 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* Sanitize, remove locale formatting, and optionally round + trim off zeros.
|
2013-10-01 16:54:29 +00:00
|
|
|
*
|
2013-11-12 17:43:30 +00:00
|
|
|
* @param float|string $number Expects either a float or a string with a decimal separator only (no thousands)
|
2013-10-01 16:54:29 +00:00
|
|
|
* @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
|
2013-08-09 16:11:15 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_format_decimal( $number, $dp = false, $trim_zeros = false ) {
|
2014-12-02 11:20:04 +00:00
|
|
|
$locale = localeconv();
|
2015-01-23 11:50:32 +00:00
|
|
|
$decimals = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] );
|
2014-12-02 11:20:04 +00:00
|
|
|
|
2013-11-12 17:43:30 +00:00
|
|
|
// Remove locale from string
|
|
|
|
if ( ! is_float( $number ) ) {
|
2014-12-02 11:20:04 +00:00
|
|
|
$number = wc_clean( str_replace( $decimals, '.', $number ) );
|
2013-11-12 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $dp !== false ) {
|
2015-01-23 11:50:32 +00:00
|
|
|
$dp = intval( $dp == "" ? wc_get_price_decimals() : $dp );
|
2013-11-12 17:43:30 +00:00
|
|
|
$number = number_format( floatval( $number ), $dp, '.', '' );
|
2014-12-02 11:20:04 +00:00
|
|
|
|
|
|
|
// DP is false - don't use number format, just return a string in our format
|
|
|
|
} elseif ( is_float( $number ) ) {
|
|
|
|
$number = wc_clean( str_replace( $decimals, '.', strval( $number ) ) );
|
2013-10-01 12:02:58 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 22:08:46 +00:00
|
|
|
if ( $trim_zeros && strstr( $number, '.' ) ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
$number = rtrim( rtrim( $number, '0' ), '.' );
|
2013-12-08 22:08:46 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
return $number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Convert a float to a string without locale formatting which PHP adds when changing floats to strings.
|
2013-11-12 17:43:30 +00:00
|
|
|
* @param float $float
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_float_to_string( $float ) {
|
2013-12-08 22:08:46 +00:00
|
|
|
if ( ! is_float( $float ) ) {
|
2013-11-12 17:43:30 +00:00
|
|
|
return $float;
|
2013-12-08 22:08:46 +00:00
|
|
|
}
|
2013-11-12 17:43:30 +00:00
|
|
|
|
|
|
|
$locale = localeconv();
|
|
|
|
$string = strval( $float );
|
|
|
|
$string = str_replace( $locale['decimal_point'], '.', $string );
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Format a price with WC Currency Locale settings.
|
2013-11-12 17:43:30 +00:00
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_format_localized_price( $value ) {
|
2015-01-23 11:50:32 +00:00
|
|
|
return str_replace( '.', wc_get_price_decimal_separator(), strval( $value ) );
|
2013-11-12 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Format a decimal with PHP Locale settings.
|
2013-11-12 17:43:30 +00:00
|
|
|
* @param string $value
|
2013-10-01 16:54:29 +00:00
|
|
|
* @return string
|
2013-08-09 16:11:15 +00:00
|
|
|
*/
|
2013-11-12 17:43:30 +00:00
|
|
|
function wc_format_localized_decimal( $value ) {
|
|
|
|
$locale = localeconv();
|
|
|
|
return str_replace( '.', $locale['decimal_point'], strval( $value ) );
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Clean variables using sanitize_text_field.
|
2015-10-09 09:41:48 +00:00
|
|
|
* @param string|array $var
|
|
|
|
* @return string|array
|
2013-08-09 16:11:15 +00:00
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_clean( $var ) {
|
2015-10-09 09:41:48 +00:00
|
|
|
return is_array( $var ) ? array_map( 'wc_clean', $var ) : sanitize_text_field( $var );
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 09:14:28 +00:00
|
|
|
/**
|
2015-05-21 23:03:57 +00:00
|
|
|
* Sanitize a string destined to be a tooltip.
|
|
|
|
*
|
|
|
|
* @since 2.3.10 Tooltips are encoded with htmlspecialchars to prevent XSS. Should not be used in conjunction with esc_attr()
|
2015-03-13 09:14:28 +00:00
|
|
|
* @param string $var
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_sanitize_tooltip( $var ) {
|
2015-05-21 23:03:57 +00:00
|
|
|
return htmlspecialchars( wp_kses( html_entity_decode( $var ), array(
|
2015-03-13 09:14:28 +00:00
|
|
|
'br' => array(),
|
|
|
|
'em' => array(),
|
|
|
|
'strong' => array(),
|
2015-05-21 23:03:57 +00:00
|
|
|
'small' => array(),
|
2015-03-13 09:14:28 +00:00
|
|
|
'span' => array(),
|
|
|
|
'ul' => array(),
|
|
|
|
'li' => array(),
|
|
|
|
'ol' => array(),
|
|
|
|
'p' => array(),
|
2015-05-21 23:03:57 +00:00
|
|
|
) ) );
|
2015-03-13 09:14:28 +00:00
|
|
|
}
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Merge two arrays.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param array $a1
|
|
|
|
* @param array $a2
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_array_overlay( $a1, $a2 ) {
|
2014-09-05 18:35:18 +00:00
|
|
|
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;
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 10:25:28 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Formats a stock amount by running it through a filter.
|
2014-06-25 10:25:28 +00:00
|
|
|
* @param int|float $amount
|
|
|
|
* @return int|float
|
|
|
|
*/
|
|
|
|
function wc_stock_amount( $amount ) {
|
|
|
|
return apply_filters( 'woocommerce_stock_amount', $amount );
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get the price format depending on the currency position.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_woocommerce_price_format() {
|
|
|
|
$currency_pos = get_option( 'woocommerce_currency_pos' );
|
2015-05-18 04:31:47 +00:00
|
|
|
$format = '%1$s%2$s';
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
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 %2$s';
|
|
|
|
break;
|
|
|
|
case 'right_space' :
|
|
|
|
$format = '%2$s %1$s';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_price_format', $format, $currency_pos );
|
|
|
|
}
|
|
|
|
|
2015-01-22 16:05:47 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Return the thousand separator for prices.
|
2015-01-22 16:05:47 +00:00
|
|
|
* @since 2.3
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-01-23 11:50:32 +00:00
|
|
|
function wc_get_price_thousand_separator() {
|
2015-05-18 07:47:59 +00:00
|
|
|
$separator = stripslashes( get_option( 'woocommerce_price_thousand_sep' ) );
|
2015-01-22 16:05:47 +00:00
|
|
|
return $separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Return the decimal separator for prices.
|
2015-01-22 16:05:47 +00:00
|
|
|
* @since 2.3
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-01-23 11:50:32 +00:00
|
|
|
function wc_get_price_decimal_separator() {
|
2015-05-18 07:47:59 +00:00
|
|
|
$separator = stripslashes( get_option( 'woocommerce_price_decimal_sep' ) );
|
2015-01-22 16:05:47 +00:00
|
|
|
return $separator ? $separator : '.';
|
|
|
|
}
|
|
|
|
|
2015-01-23 11:50:32 +00:00
|
|
|
/**
|
|
|
|
* Return the number of decimals after the decimal point.
|
|
|
|
* @since 2.3
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function wc_get_price_decimals() {
|
|
|
|
return absint( get_option( 'woocommerce_price_num_decimals', 2 ) );
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
|
|
|
* Format the price with a currency symbol.
|
|
|
|
*
|
|
|
|
* @param float $price
|
|
|
|
* @param array $args (default: array())
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_price( $price, $args = array() ) {
|
2015-01-23 11:50:32 +00:00
|
|
|
extract( apply_filters( 'wc_price_args', wp_parse_args( $args, array(
|
|
|
|
'ex_tax_label' => false,
|
|
|
|
'currency' => '',
|
|
|
|
'decimal_separator' => wc_get_price_decimal_separator(),
|
|
|
|
'thousand_separator' => wc_get_price_thousand_separator(),
|
|
|
|
'decimals' => wc_get_price_decimals(),
|
|
|
|
'price_format' => get_woocommerce_price_format()
|
|
|
|
) ) ) );
|
|
|
|
|
|
|
|
$negative = $price < 0;
|
|
|
|
$price = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
|
|
|
|
$price = apply_filters( 'formatted_woocommerce_price', number_format( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator );
|
|
|
|
|
|
|
|
if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) {
|
2013-11-25 13:34:21 +00:00
|
|
|
$price = wc_trim_zeros( $price );
|
2013-12-08 22:08:46 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2015-01-23 11:50:32 +00:00
|
|
|
$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format, get_woocommerce_currency_symbol( $currency ), $price );
|
2014-07-11 15:12:59 +00:00
|
|
|
$return = '<span class="amount">' . $formatted_price . '</span>';
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-11-18 16:45:29 +00:00
|
|
|
if ( $ex_tax_label && wc_tax_enabled() ) {
|
2015-05-27 15:17:33 +00:00
|
|
|
$return .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
|
2013-12-08 22:08:46 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-07-11 15:12:59 +00:00
|
|
|
return apply_filters( 'wc_price', $return, $price, $args );
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* let_to_num function.
|
|
|
|
*
|
|
|
|
* This function transforms the php.ini notation for numbers (like '2M') to an integer.
|
|
|
|
*
|
|
|
|
* @param $size
|
|
|
|
* @return int
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_let_to_num( $size ) {
|
2014-09-05 18:35:18 +00:00
|
|
|
$l = substr( $size, -1 );
|
|
|
|
$ret = substr( $size, 0, -1 );
|
|
|
|
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;
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* WooCommerce Date Format - Allows to change date format for everything WooCommerce.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_date_format() {
|
2013-08-09 16:11:15 +00:00
|
|
|
return apply_filters( 'woocommerce_date_format', get_option( 'date_format' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* WooCommerce Time Format - Allows to change time format for everything WooCommerce.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_time_format() {
|
2013-08-09 16:11:15 +00:00
|
|
|
return apply_filters( 'woocommerce_time_format', get_option( 'time_format' ) );
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:47:38 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* 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).
|
2013-11-18 21:47:38 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155.
|
2013-11-18 21:47:38 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @return string a valid PHP timezone string for the site
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_timezone_string() {
|
2013-11-18 21:47:38 +00:00
|
|
|
|
|
|
|
// if site timezone string exists, return it
|
2013-12-08 22:08:46 +00:00
|
|
|
if ( $timezone = get_option( 'timezone_string' ) ) {
|
2013-11-18 21:47:38 +00:00
|
|
|
return $timezone;
|
2013-12-08 22:08:46 +00:00
|
|
|
}
|
2013-11-18 21:47:38 +00:00
|
|
|
|
|
|
|
// get UTC offset, if it isn't set then return UTC
|
2013-12-08 22:08:46 +00:00
|
|
|
if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) ) {
|
2013-11-18 21:47:38 +00:00
|
|
|
return 'UTC';
|
2013-12-08 22:08:46 +00:00
|
|
|
}
|
2013-11-18 21:47:38 +00:00
|
|
|
|
|
|
|
// adjust UTC offset from hours to seconds
|
|
|
|
$utc_offset *= 3600;
|
|
|
|
|
|
|
|
// attempt to guess the timezone string from the UTC offset
|
2014-12-10 16:46:25 +00:00
|
|
|
$timezone = timezone_name_from_abbr( '', $utc_offset, 0 );
|
2013-11-18 21:47:38 +00:00
|
|
|
|
|
|
|
// last try, guess timezone string manually
|
|
|
|
if ( false === $timezone ) {
|
2014-12-05 14:21:02 +00:00
|
|
|
$is_dst = date( 'I' );
|
|
|
|
|
2013-11-18 21:47:38 +00:00
|
|
|
foreach ( timezone_abbreviations_list() as $abbr ) {
|
|
|
|
foreach ( $abbr as $city ) {
|
|
|
|
if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset ) {
|
|
|
|
return $city['timezone_id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 14:01:08 +00:00
|
|
|
|
|
|
|
// fallback to UTC
|
|
|
|
return 'UTC';
|
2013-11-18 21:47:38 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 14:01:08 +00:00
|
|
|
return $timezone;
|
2013-11-18 21:47:38 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 13:34:21 +00:00
|
|
|
if ( ! function_exists( 'wc_rgb_from_hex' ) ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Hex darker/lighter/contrast functions for colours.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param mixed $color
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_rgb_from_hex( $color ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
$color = str_replace( '#', '', $color );
|
|
|
|
// Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
|
|
|
|
$color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
|
|
|
|
|
2014-11-19 19:01:06 +00:00
|
|
|
$rgb = array();
|
2013-08-09 16:11:15 +00:00
|
|
|
$rgb['R'] = hexdec( $color{0}.$color{1} );
|
|
|
|
$rgb['G'] = hexdec( $color{2}.$color{3} );
|
|
|
|
$rgb['B'] = hexdec( $color{4}.$color{5} );
|
2014-11-19 19:01:06 +00:00
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
return $rgb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 13:34:21 +00:00
|
|
|
if ( ! function_exists( 'wc_hex_darker' ) ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Hex darker/lighter/contrast functions for colours.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param mixed $color
|
|
|
|
* @param int $factor (default: 30)
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_hex_darker( $color, $factor = 30 ) {
|
2014-09-05 18:35:18 +00:00
|
|
|
$base = wc_rgb_from_hex( $color );
|
2013-08-09 16:11:15 +00:00
|
|
|
$color = '#';
|
|
|
|
|
2014-09-05 18:35:18 +00:00
|
|
|
foreach ( $base as $k => $v ) {
|
|
|
|
$amount = $v / 100;
|
|
|
|
$amount = round( $amount * $factor );
|
|
|
|
$new_decimal = $v - $amount;
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-09-05 18:35:18 +00:00
|
|
|
$new_hex_component = dechex( $new_decimal );
|
|
|
|
if ( strlen( $new_hex_component ) < 2 ) {
|
|
|
|
$new_hex_component = "0" . $new_hex_component;
|
|
|
|
}
|
|
|
|
$color .= $new_hex_component;
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
return $color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 13:34:21 +00:00
|
|
|
if ( ! function_exists( 'wc_hex_lighter' ) ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Hex darker/lighter/contrast functions for colours.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param mixed $color
|
|
|
|
* @param int $factor (default: 30)
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_hex_lighter( $color, $factor = 30 ) {
|
2014-09-05 18:35:18 +00:00
|
|
|
$base = wc_rgb_from_hex( $color );
|
2013-08-09 16:11:15 +00:00
|
|
|
$color = '#';
|
|
|
|
|
2014-09-05 18:35:18 +00:00
|
|
|
foreach ( $base as $k => $v ) {
|
|
|
|
$amount = 255 - $v;
|
|
|
|
$amount = $amount / 100;
|
|
|
|
$amount = round( $amount * $factor );
|
|
|
|
$new_decimal = $v + $amount;
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-09-05 18:35:18 +00:00
|
|
|
$new_hex_component = dechex( $new_decimal );
|
|
|
|
if ( strlen( $new_hex_component ) < 2 ) {
|
|
|
|
$new_hex_component = "0" . $new_hex_component;
|
|
|
|
}
|
|
|
|
$color .= $new_hex_component;
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-09-05 18:35:18 +00:00
|
|
|
return $color;
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 13:34:21 +00:00
|
|
|
if ( ! function_exists( 'wc_light_or_dark' ) ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Detect if we should use a light or dark colour on a background colour.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param mixed $color
|
|
|
|
* @param string $dark (default: '#000000')
|
|
|
|
* @param string $light (default: '#FFFFFF')
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
|
2014-09-05 18:35:18 +00:00
|
|
|
|
|
|
|
$hex = str_replace( '#', '', $color );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
$c_r = hexdec( substr( $hex, 0, 2 ) );
|
|
|
|
$c_g = hexdec( substr( $hex, 2, 2 ) );
|
|
|
|
$c_b = hexdec( substr( $hex, 4, 2 ) );
|
2014-09-05 18:35:18 +00:00
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
$brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
|
|
|
|
|
|
|
|
return $brightness > 155 ? $dark : $light;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 13:34:21 +00:00
|
|
|
if ( ! function_exists( 'wc_format_hex' ) ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Format string as hex.
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @param string $hex
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-25 13:34:21 +00:00
|
|
|
function wc_format_hex( $hex ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-09-05 18:35:18 +00:00
|
|
|
$hex = trim( str_replace( '#', '', $hex ) );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-09-05 18:35:18 +00:00
|
|
|
if ( strlen( $hex ) == 3 ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
|
2014-09-05 18:35:18 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-09-05 18:35:18 +00:00
|
|
|
return $hex ? '#' . $hex : null;
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
2013-09-12 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Format the postcode according to the country and length of the postcode.
|
2013-09-12 13:41:02 +00:00
|
|
|
*
|
2014-09-05 18:35:18 +00:00
|
|
|
* @param string postcode
|
|
|
|
* @param string country
|
|
|
|
* @return string formatted postcode
|
2013-09-12 13:41:02 +00:00
|
|
|
*/
|
|
|
|
function wc_format_postcode( $postcode, $country ) {
|
2014-04-07 14:46:40 +00:00
|
|
|
$postcode = strtoupper( trim( $postcode ) );
|
|
|
|
$postcode = trim( preg_replace( '/[\s]/', '', $postcode ) );
|
2013-09-12 13:41:02 +00:00
|
|
|
|
2014-04-07 14:46:40 +00:00
|
|
|
if ( in_array( $country, array( 'GB', 'CA' ) ) ) {
|
2013-09-12 13:41:02 +00:00
|
|
|
$postcode = trim( substr_replace( $postcode, ' ', -3, 0 ) );
|
2013-12-08 22:08:46 +00:00
|
|
|
}
|
2013-09-12 13:41:02 +00:00
|
|
|
|
|
|
|
return $postcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* format_phone function.
|
|
|
|
*
|
|
|
|
* @param mixed $tel
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_format_phone_number( $tel ) {
|
|
|
|
$tel = str_replace( '.', '-', $tel );
|
|
|
|
return $tel;
|
2013-11-18 21:47:38 +00:00
|
|
|
}
|
2014-10-21 12:51:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
}
|
2014-11-19 14:34:34 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Trim a string and append a suffix.
|
2014-11-19 14:34:34 +00:00
|
|
|
* @param string $string
|
|
|
|
* @param integer $chars
|
|
|
|
* @param string $suffix
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_trim_string( $string, $chars = 200, $suffix = '...' ) {
|
|
|
|
if ( strlen( $string ) > $chars ) {
|
|
|
|
$string = substr( $string, 0, ( $chars - strlen( $suffix ) ) ) . $suffix;
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
2015-01-16 12:32:41 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Format content to display shortcodes.
|
2015-01-16 12:32:41 +00:00
|
|
|
*
|
|
|
|
* @since 2.3.0
|
2015-03-09 11:47:05 +00:00
|
|
|
* @param string $raw_string
|
2015-01-16 12:32:41 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-03-09 11:47:05 +00:00
|
|
|
function wc_format_content( $raw_string ) {
|
|
|
|
return apply_filters( 'woocommerce_format_content', do_shortcode( shortcode_unautop( wpautop( $raw_string ) ) ), $raw_string );
|
2015-01-16 12:32:41 +00:00
|
|
|
}
|
2015-07-06 14:31:43 +00:00
|
|
|
|
2015-07-12 00:35:47 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Format product short description.
|
|
|
|
* Adds support for Jetpack Markdown.
|
2015-07-12 00:35:47 +00:00
|
|
|
*
|
|
|
|
* @since 2.4.0
|
|
|
|
* @param string $content
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_format_product_short_description( $content ) {
|
|
|
|
// Add support for Jetpack Markdown
|
|
|
|
if ( class_exists( 'WPCom_Markdown' ) ) {
|
|
|
|
$markdown = WPCom_Markdown::get_instance();
|
|
|
|
|
2015-08-11 15:06:59 +00:00
|
|
|
return wpautop( $markdown->transform( $content, array( 'unslash' => false ) ) );
|
2015-07-12 00:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_filter( 'woocommerce_short_description', 'wc_format_product_short_description', 9999999 );
|
|
|
|
|
2015-07-06 14:31:43 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Formats curency symbols when saved in settings.
|
2015-07-06 14:31:43 +00:00
|
|
|
* @param string $value
|
|
|
|
* @param array $option
|
|
|
|
* @param string $raw_value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_format_option_price_separators( $value, $option, $raw_value ) {
|
|
|
|
return wp_kses_post( $raw_value );
|
|
|
|
}
|
|
|
|
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_price_decimal_sep', 'wc_format_option_price_separators', 10, 3 );
|
|
|
|
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_price_thousand_sep', 'wc_format_option_price_separators', 10, 3 );
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Formats decimals when saved in settings.
|
2015-07-06 14:31:43 +00:00
|
|
|
* @param string $value
|
|
|
|
* @param array $option
|
|
|
|
* @param string $raw_value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_format_option_price_num_decimals( $value, $option, $raw_value ) {
|
|
|
|
return is_null( $raw_value ) ? 2 : absint( $raw_value );
|
|
|
|
}
|
|
|
|
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_price_num_decimals', 'wc_format_option_price_num_decimals', 10, 3 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats hold stock option and sets cron event up.
|
|
|
|
* @param string $value
|
|
|
|
* @param array $option
|
|
|
|
* @param string $raw_value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_format_option_hold_stock_minutes( $value, $option, $raw_value ) {
|
|
|
|
$value = ! empty( $raw_value ) ? absint( $raw_value ) : ''; // Allow > 0 or set to ''
|
|
|
|
|
|
|
|
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
|
|
|
|
|
|
|
|
if ( '' !== $value ) {
|
|
|
|
wp_schedule_single_event( time() + ( absint( $value ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_hold_stock_minutes', 'wc_format_option_hold_stock_minutes', 10, 3 );
|
2015-08-14 18:22:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize terms from an attribute text based.
|
|
|
|
*
|
|
|
|
* @since 2.4.5
|
|
|
|
* @param string $term
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_sanitize_term_text_based( $term ) {
|
|
|
|
return trim( wp_unslash( strip_tags( $term ) ) );
|
|
|
|
}
|
2015-12-15 12:39:52 +00:00
|
|
|
|
2015-12-18 17:10:58 +00:00
|
|
|
if ( ! function_exists( 'wc_make_numeric_postcode' ) ) {
|
|
|
|
/**
|
|
|
|
* Make numeric postcode.
|
|
|
|
*
|
|
|
|
* Converts letters to numbers so we can do a simple range check on postcodes.
|
|
|
|
* E.g. PE30 becomes 16050300 (P = 16, E = 05, 3 = 03, 0 = 00)
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $postcode Regular postcode
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_make_numeric_postcode( $postcode ) {
|
|
|
|
$postcode_length = strlen( $postcode );
|
|
|
|
$letters_to_numbers = array_merge( array( 0 ), range( 'A', 'Z' ) );
|
|
|
|
$letters_to_numbers = array_flip( $letters_to_numbers );
|
|
|
|
$numeric_postcode = '';
|
|
|
|
|
|
|
|
for ( $i = 0; $i < $postcode_length; $i ++ ) {
|
|
|
|
if ( is_numeric( $postcode[ $i ] ) ) {
|
|
|
|
$numeric_postcode .= str_pad( $postcode[ $i ], 2, '0', STR_PAD_LEFT );
|
|
|
|
} elseif ( isset( $letters_to_numbers[ $postcode[ $i ] ] ) ) {
|
|
|
|
$numeric_postcode .= str_pad( $letters_to_numbers[ $postcode[ $i ] ], 2, '0', STR_PAD_LEFT );
|
|
|
|
} else {
|
|
|
|
$numeric_postcode .= '00';
|
|
|
|
}
|
2015-12-15 12:39:52 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 17:10:58 +00:00
|
|
|
return $numeric_postcode;
|
|
|
|
}
|
2015-12-15 12:39:52 +00:00
|
|
|
}
|