$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 %2$s';
break;
case 'right_space' :
$format = '%2$s %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() ) {
extract( shortcode_atts( array(
'ex_tax_label' => '0'
), $args ) );
$num_decimals = absint( get_option( 'woocommerce_price_num_decimals' ) );
$currency = isset( $args['currency'] ) ? $args['currency'] : '';
$currency_symbol = get_woocommerce_currency_symbol($currency);
$decimal_sep = wp_specialchars_decode( stripslashes( get_option( 'woocommerce_price_decimal_sep' ) ), ENT_QUOTES );
$thousands_sep = wp_specialchars_decode( stripslashes( get_option( 'woocommerce_price_thousand_sep' ) ), ENT_QUOTES );
if ( $price < 0 ) {
$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 > 0 ) {
$price = wc_trim_zeros( $price );
}
$formatted_price = ( $negative ? '-' : '' ) . sprintf( get_woocommerce_price_format(), $currency_symbol, $price );
$return = '' . $formatted_price . '';
if ( $ex_tax_label && wc_tax_enabled() ) {
$return .= ' ' . WC()->countries->ex_tax_or_vat() . '';
}
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, -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;
}
/**
* 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 ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) ) {
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, 0 );
// 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';
}
return $timezone;
}
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 = array();
$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 ) < 2 ) {
$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 ) < 2 ) {
$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 = hexdec( substr( $hex, 0, 2 ) );
$c_g = hexdec( substr( $hex, 2, 2 ) );
$c_b = hexdec( substr( $hex, 4, 2 ) );
$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 = trim( str_replace( '#', '', $hex ) );
if ( strlen( $hex ) == 3 ) {
$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 = strtoupper( trim( $postcode ) );
$postcode = trim( preg_replace( '/[\s]/', '', $postcode ) );
if ( in_array( $country, array( 'GB', 'CA' ) ) ) {
$postcode = trim( substr_replace( $postcode, ' ', -3, 0 ) );
}
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 );
}
/**
* Trim a string and append a suffix
* @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;
}
/**
* Format content to display shortcodes
*
* @since 2.3.0
* @param string $string
* @return string
*/
function wc_format_content( $string ) {
return do_shortcode( shortcode_unautop( wpautop( $string ) ) );
}