PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/shop quần áo starloveshop.com/wp-content/plugins/woocommerce/includes/wc-formatting-functions.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 607 lines | 313 code | 84 blank | 210 comment | 46 complexity | 66cb77e5260a6b4d43453d5869b20ebe MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Formatting
  4. *
  5. * Functions for formatting data.
  6. *
  7. * @author WooThemes
  8. * @category Core
  9. * @package WooCommerce/Functions
  10. * @version 2.1.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit; // Exit if accessed directly
  14. }
  15. /**
  16. * Sanitize taxonomy names. Slug format (no spaces, lowercase).
  17. *
  18. * urldecode is used to reverse munging of UTF8 characters.
  19. *
  20. * @param mixed $taxonomy
  21. * @return string
  22. */
  23. function wc_sanitize_taxonomy_name( $taxonomy ) {
  24. return apply_filters( 'sanitize_taxonomy_name', urldecode( sanitize_title( $taxonomy ) ), $taxonomy );
  25. }
  26. /**
  27. * Gets the filename part of a download URL
  28. *
  29. * @param string $file_url
  30. * @return string
  31. */
  32. function wc_get_filename_from_url( $file_url ) {
  33. $parts = parse_url( $file_url );
  34. if ( isset( $parts['path'] ) ) {
  35. return basename( $parts['path'] );
  36. }
  37. }
  38. /**
  39. * Normalise dimensions, unify to cm then convert to wanted unit value
  40. *
  41. * Usage: wc_get_dimension(55, 'in');
  42. *
  43. * @param mixed $dim
  44. * @param mixed $to_unit 'in', 'm', 'cm', 'm'
  45. * @return float
  46. */
  47. function wc_get_dimension( $dim, $to_unit ) {
  48. $from_unit = strtolower( get_option( 'woocommerce_dimension_unit' ) );
  49. $to_unit = strtolower( $to_unit );
  50. // Unify all units to cm first
  51. if ( $from_unit !== $to_unit ) {
  52. switch ( $from_unit ) {
  53. case 'in':
  54. $dim *= 2.54;
  55. break;
  56. case 'm':
  57. $dim *= 100;
  58. break;
  59. case 'mm':
  60. $dim *= 0.1;
  61. break;
  62. case 'yd':
  63. $dim *= 91.44;
  64. break;
  65. }
  66. // Output desired unit
  67. switch ( $to_unit ) {
  68. case 'in':
  69. $dim *= 0.3937;
  70. break;
  71. case 'm':
  72. $dim *= 0.01;
  73. break;
  74. case 'mm':
  75. $dim *= 10;
  76. break;
  77. case 'yd':
  78. $dim *= 0.010936133;
  79. break;
  80. }
  81. }
  82. return ( $dim < 0 ) ? 0 : $dim;
  83. }
  84. /**
  85. * Normalise weights, unify to kg then convert to wanted unit value
  86. *
  87. * Usage: wc_get_weight(55, 'kg');
  88. *
  89. * @param mixed $weight
  90. * @param mixed $to_unit 'g', 'kg', 'lbs'
  91. * @return float
  92. */
  93. function wc_get_weight( $weight, $to_unit ) {
  94. $from_unit = strtolower( get_option('woocommerce_weight_unit') );
  95. $to_unit = strtolower( $to_unit );
  96. //Unify all units to kg first
  97. if ( $from_unit !== $to_unit ) {
  98. switch ( $from_unit ) {
  99. case 'g':
  100. $weight *= 0.001;
  101. break;
  102. case 'lbs':
  103. $weight *= 0.4536;
  104. break;
  105. case 'oz':
  106. $weight *= 0.0283;
  107. break;
  108. }
  109. // Output desired unit
  110. switch ( $to_unit ) {
  111. case 'g':
  112. $weight *= 1000;
  113. break;
  114. case 'lbs':
  115. $weight *= 2.2046;
  116. break;
  117. case 'oz':
  118. $weight *= 35.274;
  119. break;
  120. }
  121. }
  122. return ( $weight < 0 ) ? 0 : $weight;
  123. }
  124. /**
  125. * Trim trailing zeros off prices.
  126. *
  127. * @param mixed $price
  128. * @return string
  129. */
  130. function wc_trim_zeros( $price ) {
  131. return preg_replace( '/' . preg_quote( get_option( 'woocommerce_price_decimal_sep' ), '/' ) . '0++$/', '', $price );
  132. }
  133. /**
  134. * Round a tax amount
  135. *
  136. * @param mixed $tax
  137. * @return double
  138. */
  139. function wc_round_tax_total( $tax ) {
  140. $dp = (int) get_option( 'woocommerce_price_num_decimals' );
  141. // @codeCoverageIgnoreStart
  142. if ( version_compare( phpversion(), '5.3', '<' ) ) {
  143. $tax = round( $tax, $dp );
  144. } else {
  145. // @codeCoverageIgnoreEnd
  146. $tax = round( $tax, $dp, WC_TAX_ROUNDING_MODE );
  147. }
  148. return $tax;
  149. }
  150. /**
  151. * Make a refund total negative
  152. * @return float
  153. */
  154. function wc_format_refund_total( $amount ) {
  155. return $amount * -1;
  156. }
  157. /**
  158. * Format decimal numbers ready for DB storage
  159. *
  160. * Sanitize, remove locale formatting, and optionally round + trim off zeros
  161. *
  162. * @param float|string $number Expects either a float or a string with a decimal separator only (no thousands)
  163. * @param mixed $dp number of decimal points to use, blank to use woocommerce_price_num_decimals, or false to avoid all rounding.
  164. * @param boolean $trim_zeros from end of string
  165. * @return string
  166. */
  167. function wc_format_decimal( $number, $dp = false, $trim_zeros = false ) {
  168. $locale = localeconv();
  169. $decimals = array( get_option( 'woocommerce_price_decimal_sep' ), $locale['decimal_point'], $locale['mon_decimal_point'] );
  170. // Remove locale from string
  171. if ( ! is_float( $number ) ) {
  172. $number = wc_clean( str_replace( $decimals, '.', $number ) );
  173. }
  174. if ( $dp !== false ) {
  175. $dp = intval( $dp == "" ? get_option( 'woocommerce_price_num_decimals' ) : $dp );
  176. $number = number_format( floatval( $number ), $dp, '.', '' );
  177. // DP is false - don't use number format, just return a string in our format
  178. } elseif ( is_float( $number ) ) {
  179. $number = wc_clean( str_replace( $decimals, '.', strval( $number ) ) );
  180. }
  181. if ( $trim_zeros && strstr( $number, '.' ) ) {
  182. $number = rtrim( rtrim( $number, '0' ), '.' );
  183. }
  184. return $number;
  185. }
  186. /**
  187. * Convert a float to a string without locale formatting which PHP adds when changing floats to strings
  188. * @param float $float
  189. * @return string
  190. */
  191. function wc_float_to_string( $float ) {
  192. if ( ! is_float( $float ) ) {
  193. return $float;
  194. }
  195. $locale = localeconv();
  196. $string = strval( $float );
  197. $string = str_replace( $locale['decimal_point'], '.', $string );
  198. return $string;
  199. }
  200. /**
  201. * Format a price with WC Currency Locale settings
  202. * @param string $value
  203. * @return string
  204. */
  205. function wc_format_localized_price( $value ) {
  206. return str_replace( '.', get_option( 'woocommerce_price_decimal_sep' ), strval( $value ) );
  207. }
  208. /**
  209. * Format a decimal with PHP Locale settings
  210. * @param string $value
  211. * @return string
  212. */
  213. function wc_format_localized_decimal( $value ) {
  214. $locale = localeconv();
  215. return str_replace( '.', $locale['decimal_point'], strval( $value ) );
  216. }
  217. /**
  218. * Clean variables
  219. *
  220. * @param string $var
  221. * @return string
  222. */
  223. function wc_clean( $var ) {
  224. return sanitize_text_field( $var );
  225. }
  226. /**
  227. * Merge two arrays
  228. *
  229. * @param array $a1
  230. * @param array $a2
  231. * @return array
  232. */
  233. function wc_array_overlay( $a1, $a2 ) {
  234. foreach ( $a1 as $k => $v ) {
  235. if ( ! array_key_exists( $k, $a2 ) ) {
  236. continue;
  237. }
  238. if ( is_array( $v ) && is_array( $a2[ $k ] ) ) {
  239. $a1[ $k ] = wc_array_overlay( $v, $a2[ $k ] );
  240. } else {
  241. $a1[ $k ] = $a2[ $k ];
  242. }
  243. }
  244. return $a1;
  245. }
  246. /**
  247. * Formats a stock amount by running it through a filter
  248. * @param int|float $amount
  249. * @return int|float
  250. */
  251. function wc_stock_amount( $amount ) {
  252. return apply_filters( 'woocommerce_stock_amount', $amount );
  253. }
  254. /**
  255. * Get the price format depending on the currency position
  256. *
  257. * @return string
  258. */
  259. function get_woocommerce_price_format() {
  260. $currency_pos = get_option( 'woocommerce_currency_pos' );
  261. switch ( $currency_pos ) {
  262. case 'left' :
  263. $format = '%1$s%2$s';
  264. break;
  265. case 'right' :
  266. $format = '%2$s%1$s';
  267. break;
  268. case 'left_space' :
  269. $format = '%1$s&nbsp;%2$s';
  270. break;
  271. case 'right_space' :
  272. $format = '%2$s&nbsp;%1$s';
  273. break;
  274. }
  275. return apply_filters( 'woocommerce_price_format', $format, $currency_pos );
  276. }
  277. /**
  278. * Format the price with a currency symbol.
  279. *
  280. * @param float $price
  281. * @param array $args (default: array())
  282. * @return string
  283. */
  284. function wc_price( $price, $args = array() ) {
  285. extract( shortcode_atts( array(
  286. 'ex_tax_label' => '0'
  287. ), $args ) );
  288. $return = '';
  289. $num_decimals = absint( get_option( 'woocommerce_price_num_decimals' ) );
  290. $currency = isset( $args['currency'] ) ? $args['currency'] : '';
  291. $currency_symbol = get_woocommerce_currency_symbol($currency);
  292. $decimal_sep = wp_specialchars_decode( stripslashes( get_option( 'woocommerce_price_decimal_sep' ) ), ENT_QUOTES );
  293. $thousands_sep = wp_specialchars_decode( stripslashes( get_option( 'woocommerce_price_thousand_sep' ) ), ENT_QUOTES );
  294. if ( $price < 0 ) {
  295. $price = $price * -1;
  296. $negative = true;
  297. } else {
  298. $negative = false;
  299. }
  300. $price = apply_filters( 'raw_woocommerce_price', floatval( $price ) );
  301. $price = apply_filters( 'formatted_woocommerce_price', number_format( $price, $num_decimals, $decimal_sep, $thousands_sep ), $price, $num_decimals, $decimal_sep, $thousands_sep );
  302. if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $num_decimals > 0 ) {
  303. $price = wc_trim_zeros( $price );
  304. }
  305. $formatted_price = ( $negative ? '-' : '' ) . sprintf( get_woocommerce_price_format(), $currency_symbol, $price );
  306. $return = '<span class="amount">' . $formatted_price . '</span>';
  307. if ( $ex_tax_label && get_option( 'woocommerce_calc_taxes' ) == 'yes' ) {
  308. $return .= ' <small>' . WC()->countries->ex_tax_or_vat() . '</small>';
  309. }
  310. return apply_filters( 'wc_price', $return, $price, $args );
  311. }
  312. /**
  313. * let_to_num function.
  314. *
  315. * This function transforms the php.ini notation for numbers (like '2M') to an integer.
  316. *
  317. * @param $size
  318. * @return int
  319. */
  320. function wc_let_to_num( $size ) {
  321. $l = substr( $size, -1 );
  322. $ret = substr( $size, 0, -1 );
  323. switch ( strtoupper( $l ) ) {
  324. case 'P':
  325. $ret *= 1024;
  326. case 'T':
  327. $ret *= 1024;
  328. case 'G':
  329. $ret *= 1024;
  330. case 'M':
  331. $ret *= 1024;
  332. case 'K':
  333. $ret *= 1024;
  334. }
  335. return $ret;
  336. }
  337. /**
  338. * WooCommerce Date Format - Allows to change date format for everything WooCommerce
  339. *
  340. * @return string
  341. */
  342. function wc_date_format() {
  343. return apply_filters( 'woocommerce_date_format', get_option( 'date_format' ) );
  344. }
  345. /**
  346. * WooCommerce Time Format - Allows to change time format for everything WooCommerce
  347. *
  348. * @return string
  349. */
  350. function wc_time_format() {
  351. return apply_filters( 'woocommerce_time_format', get_option( 'time_format' ) );
  352. }
  353. /**
  354. * WooCommerce Timezone - helper to retrieve the timezone string for a site until
  355. * a WP core method exists (see http://core.trac.wordpress.org/ticket/24730)
  356. *
  357. * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155
  358. *
  359. * @since 2.1
  360. * @return string a valid PHP timezone string for the site
  361. */
  362. function wc_timezone_string() {
  363. // if site timezone string exists, return it
  364. if ( $timezone = get_option( 'timezone_string' ) ) {
  365. return $timezone;
  366. }
  367. // get UTC offset, if it isn't set then return UTC
  368. if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) ) {
  369. return 'UTC';
  370. }
  371. // adjust UTC offset from hours to seconds
  372. $utc_offset *= 3600;
  373. // attempt to guess the timezone string from the UTC offset
  374. $timezone = timezone_name_from_abbr( '', $utc_offset );
  375. // last try, guess timezone string manually
  376. if ( false === $timezone ) {
  377. $is_dst = date( 'I' );
  378. foreach ( timezone_abbreviations_list() as $abbr ) {
  379. foreach ( $abbr as $city ) {
  380. if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset ) {
  381. return $city['timezone_id'];
  382. }
  383. }
  384. }
  385. }
  386. // fallback to UTC
  387. return 'UTC';
  388. }
  389. if ( ! function_exists( 'wc_rgb_from_hex' ) ) {
  390. /**
  391. * Hex darker/lighter/contrast functions for colours
  392. *
  393. * @param mixed $color
  394. * @return string
  395. */
  396. function wc_rgb_from_hex( $color ) {
  397. $color = str_replace( '#', '', $color );
  398. // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
  399. $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
  400. $rgb['R'] = hexdec( $color{0}.$color{1} );
  401. $rgb['G'] = hexdec( $color{2}.$color{3} );
  402. $rgb['B'] = hexdec( $color{4}.$color{5} );
  403. return $rgb;
  404. }
  405. }
  406. if ( ! function_exists( 'wc_hex_darker' ) ) {
  407. /**
  408. * Hex darker/lighter/contrast functions for colours
  409. *
  410. * @param mixed $color
  411. * @param int $factor (default: 30)
  412. * @return string
  413. */
  414. function wc_hex_darker( $color, $factor = 30 ) {
  415. $base = wc_rgb_from_hex( $color );
  416. $color = '#';
  417. foreach ( $base as $k => $v ) {
  418. $amount = $v / 100;
  419. $amount = round( $amount * $factor );
  420. $new_decimal = $v - $amount;
  421. $new_hex_component = dechex( $new_decimal );
  422. if ( strlen( $new_hex_component ) < 2 ) {
  423. $new_hex_component = "0" . $new_hex_component;
  424. }
  425. $color .= $new_hex_component;
  426. }
  427. return $color;
  428. }
  429. }
  430. if ( ! function_exists( 'wc_hex_lighter' ) ) {
  431. /**
  432. * Hex darker/lighter/contrast functions for colours
  433. *
  434. * @param mixed $color
  435. * @param int $factor (default: 30)
  436. * @return string
  437. */
  438. function wc_hex_lighter( $color, $factor = 30 ) {
  439. $base = wc_rgb_from_hex( $color );
  440. $color = '#';
  441. foreach ( $base as $k => $v ) {
  442. $amount = 255 - $v;
  443. $amount = $amount / 100;
  444. $amount = round( $amount * $factor );
  445. $new_decimal = $v + $amount;
  446. $new_hex_component = dechex( $new_decimal );
  447. if ( strlen( $new_hex_component ) < 2 ) {
  448. $new_hex_component = "0" . $new_hex_component;
  449. }
  450. $color .= $new_hex_component;
  451. }
  452. return $color;
  453. }
  454. }
  455. if ( ! function_exists( 'wc_light_or_dark' ) ) {
  456. /**
  457. * Detect if we should use a light or dark colour on a background colour
  458. *
  459. * @param mixed $color
  460. * @param string $dark (default: '#000000')
  461. * @param string $light (default: '#FFFFFF')
  462. * @return string
  463. */
  464. function wc_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
  465. $hex = str_replace( '#', '', $color );
  466. $c_r = hexdec( substr( $hex, 0, 2 ) );
  467. $c_g = hexdec( substr( $hex, 2, 2 ) );
  468. $c_b = hexdec( substr( $hex, 4, 2 ) );
  469. $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
  470. return $brightness > 155 ? $dark : $light;
  471. }
  472. }
  473. if ( ! function_exists( 'wc_format_hex' ) ) {
  474. /**
  475. * Format string as hex
  476. *
  477. * @param string $hex
  478. * @return string
  479. */
  480. function wc_format_hex( $hex ) {
  481. $hex = trim( str_replace( '#', '', $hex ) );
  482. if ( strlen( $hex ) == 3 ) {
  483. $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
  484. }
  485. return $hex ? '#' . $hex : null;
  486. }
  487. }
  488. /**
  489. * Format the postcode according to the country and length of the postcode
  490. *
  491. * @param string postcode
  492. * @param string country
  493. * @return string formatted postcode
  494. */
  495. function wc_format_postcode( $postcode, $country ) {
  496. $postcode = strtoupper( trim( $postcode ) );
  497. $postcode = trim( preg_replace( '/[\s]/', '', $postcode ) );
  498. if ( in_array( $country, array( 'GB', 'CA' ) ) ) {
  499. $postcode = trim( substr_replace( $postcode, ' ', -3, 0 ) );
  500. }
  501. return $postcode;
  502. }
  503. /**
  504. * format_phone function.
  505. *
  506. * @param mixed $tel
  507. * @return string
  508. */
  509. function wc_format_phone_number( $tel ) {
  510. $tel = str_replace( '.', '-', $tel );
  511. return $tel;
  512. }
  513. /**
  514. * Make a string lowercase.
  515. * Try to use mb_strtolower() when available.
  516. *
  517. * @since 2.3
  518. * @param string $string
  519. * @return string
  520. */
  521. function wc_strtolower( $string ) {
  522. return function_exists( 'mb_strtolower' ) ? mb_strtolower( $string ) : strtolower( $string );
  523. }