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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / wc-formatting-functions.php
woocommerce / includes Last commit date
abstracts 1 year ago admin 1 year ago blocks 1 year ago cli 1 year ago customizer 2 years ago data-stores 1 year ago emails 1 year ago export 1 year ago gateways 1 year ago import 1 year ago integrations 2 years ago interfaces 1 year ago legacy 1 year ago libraries 1 year ago log-handlers 1 year ago payment-tokens 5 years ago product-usage 1 year ago queue 4 years ago react-admin 1 year ago rest-api 1 year ago shipping 1 year ago shortcodes 1 year ago theme-support 2 years ago tracks 1 year ago traits 5 years ago walkers 5 years ago wccom-site 1 year ago widgets 1 year ago class-wc-ajax.php 1 year ago class-wc-auth.php 1 year ago class-wc-autoloader.php 1 year ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 1 year ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 1 year ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 1 year ago class-wc-cart-totals.php 1 year ago class-wc-cart.php 1 year ago class-wc-checkout.php 1 year ago class-wc-cli.php 1 year ago class-wc-comments.php 3 years ago class-wc-countries.php 1 year ago class-wc-coupon.php 1 year ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 1 year ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 years ago class-wc-discounts.php 1 year ago class-wc-download-handler.php 1 year ago class-wc-emails.php 1 year ago class-wc-embed.php 1 year ago class-wc-form-handler.php 1 year ago class-wc-frontend-scripts.php 1 year ago class-wc-geo-ip.php 1 year ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 1 year ago class-wc-https.php 2 years ago class-wc-install.php 1 year ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 1 year ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 2 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 1 year ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 1 year ago class-wc-order-item-shipping.php 1 year ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 1 year ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 year ago class-wc-payment-gateways.php 1 year ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 year ago class-wc-post-types.php 1 year ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 2 years ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 1 year ago class-wc-product-grouped.php 1 year ago class-wc-product-query.php 1 year ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 1 year ago class-wc-product-variation.php 1 year ago class-wc-query.php 1 year ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 1 year ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 1 year ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 2 years ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 1 year ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 1 year ago class-wc-tax.php 2 years ago class-wc-template-loader.php 2 years ago class-wc-tracker.php 1 year ago class-wc-validation.php 2 years ago class-wc-webhook.php 1 year ago class-woocommerce.php 1 year ago wc-account-functions.php 1 year ago wc-attribute-functions.php 1 year ago wc-brands-functions.php 1 year ago wc-cart-functions.php 1 year ago wc-conditional-functions.php 1 year ago wc-core-functions.php 1 year ago wc-coupon-functions.php 3 years ago wc-deprecated-functions.php 2 years ago wc-formatting-functions.php 1 year ago wc-notice-functions.php 2 years ago wc-order-functions.php 1 year ago wc-order-item-functions.php 3 years ago wc-page-functions.php 1 year ago wc-product-functions.php 1 year ago wc-rest-functions.php 1 year ago wc-stock-functions.php 1 year ago wc-template-functions.php 1 year ago wc-template-hooks.php 1 year ago wc-term-functions.php 1 year ago wc-update-functions.php 1 year ago wc-user-functions.php 1 year ago wc-webhook-functions.php 1 year ago wc-widget-functions.php 5 years ago
wc-formatting-functions.php
1584 lines
1 <?php
2 /**
3 * WooCommerce Formatting
4 *
5 * Functions for formatting data.
6 *
7 * @package WooCommerce\Functions
8 * @version 2.1.0
9 */
10
11 use Automattic\WooCommerce\Utilities\I18nUtil;
12 use Automattic\WooCommerce\Utilities\NumberUtil;
13
14 defined( 'ABSPATH' ) || exit;
15
16 // Once WooCommerce requires PHP 7.4, the "$x = $x ?? ''" constructs can be replaced with "$x ??= ''".
17
18 /**
19 * Converts a string (e.g. 'yes' or 'no') to a bool.
20 *
21 * @since 3.0.0
22 * @param string|bool $string String to convert. If a bool is passed it will be returned as-is.
23 * @return bool
24 */
25 function wc_string_to_bool( $string ) {
26 $string = $string ?? '';
27 return is_bool( $string ) ? $string : ( 'yes' === strtolower( $string ) || 1 === $string || 'true' === strtolower( $string ) || '1' === $string );
28 }
29
30 /**
31 * Converts a bool to a 'yes' or 'no'.
32 *
33 * @since 3.0.0
34 * @param bool|string $bool Bool to convert. If a string is passed it will first be converted to a bool.
35 * @return string
36 */
37 function wc_bool_to_string( $bool ) {
38 if ( ! is_bool( $bool ) ) {
39 $bool = wc_string_to_bool( $bool );
40 }
41 return true === $bool ? 'yes' : 'no';
42 }
43
44 /**
45 * Explode a string into an array by $delimiter and remove empty values.
46 *
47 * @since 3.0.0
48 * @param string $string String to convert.
49 * @param string $delimiter Delimiter, defaults to ','.
50 * @return array
51 */
52 function wc_string_to_array( $string, $delimiter = ',' ) {
53 $string = $string ?? '';
54 return is_array( $string ) ? $string : array_filter( explode( $delimiter, $string ) );
55 }
56
57 /**
58 * Sanitize taxonomy names. Slug format (no spaces, lowercase).
59 * Urldecode is used to reverse munging of UTF8 characters.
60 *
61 * @param string $taxonomy Taxonomy name.
62 * @return string
63 */
64 function wc_sanitize_taxonomy_name( $taxonomy ) {
65 return apply_filters( 'sanitize_taxonomy_name', urldecode( sanitize_title( urldecode( $taxonomy ?? '' ) ) ), $taxonomy );
66 }
67
68 /**
69 * Sanitize permalink values before insertion into DB.
70 *
71 * Cannot use wc_clean because it sometimes strips % chars and breaks the user's setting.
72 *
73 * @since 2.6.0
74 * @param string $value Permalink.
75 * @return string
76 */
77 function wc_sanitize_permalink( $value ) {
78 global $wpdb;
79
80 $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value ?? '' );
81
82 if ( is_wp_error( $value ) ) {
83 $value = '';
84 }
85
86 $value = esc_url_raw( trim( $value ) );
87 $value = str_replace( 'http://', '', $value );
88 return untrailingslashit( $value );
89 }
90
91 /**
92 * Gets the filename part of a download URL.
93 *
94 * @param string $file_url File URL.
95 * @return string
96 */
97 function wc_get_filename_from_url( $file_url ) {
98 $parts = wp_parse_url( $file_url );
99 if ( isset( $parts['path'] ) ) {
100 return basename( $parts['path'] );
101 }
102 }
103
104 /**
105 * Normalise dimensions, unify to cm then convert to wanted unit value.
106 *
107 * Usage:
108 * wc_get_dimension( 55, 'in' );
109 * wc_get_dimension( 55, 'in', 'm' );
110 *
111 * @param int|float $dimension Dimension.
112 * @param string $to_unit Unit to convert to.
113 * Options: 'in', 'mm', 'cm', 'm'.
114 * @param string $from_unit Unit to convert from.
115 * Defaults to ''.
116 * Options: 'in', 'mm', 'cm', 'm'.
117 * @return float
118 */
119 function wc_get_dimension( $dimension, $to_unit, $from_unit = '' ) {
120 $to_unit = strtolower( $to_unit );
121
122 if ( empty( $from_unit ) ) {
123 $from_unit = strtolower( get_option( 'woocommerce_dimension_unit' ) );
124 }
125
126 // Unify all units to cm first.
127 if ( $from_unit !== $to_unit ) {
128 switch ( $from_unit ) {
129 case 'in':
130 $dimension *= 2.54;
131 break;
132 case 'm':
133 $dimension *= 100;
134 break;
135 case 'mm':
136 $dimension *= 0.1;
137 break;
138 case 'yd':
139 $dimension *= 91.44;
140 break;
141 }
142
143 // Output desired unit.
144 switch ( $to_unit ) {
145 case 'in':
146 $dimension *= 0.3937;
147 break;
148 case 'm':
149 $dimension *= 0.01;
150 break;
151 case 'mm':
152 $dimension *= 10;
153 break;
154 case 'yd':
155 $dimension *= 0.010936133;
156 break;
157 }
158 }
159
160 return ( $dimension < 0 ) ? 0 : $dimension;
161 }
162
163 /**
164 * Normalise weights, unify to kg then convert to wanted unit value.
165 *
166 * Usage:
167 * wc_get_weight(55, 'kg');
168 * wc_get_weight(55, 'kg', 'lbs');
169 *
170 * @param int|float $weight Weight.
171 * @param string $to_unit Unit to convert to.
172 * Options: 'g', 'kg', 'lbs', 'oz'.
173 * @param string $from_unit Unit to convert from.
174 * Defaults to ''.
175 * Options: 'g', 'kg', 'lbs', 'oz'.
176 * @return float
177 */
178 function wc_get_weight( $weight, $to_unit, $from_unit = '' ) {
179 $weight = (float) $weight;
180 $to_unit = strtolower( $to_unit );
181
182 if ( empty( $from_unit ) ) {
183 $from_unit = strtolower( get_option( 'woocommerce_weight_unit' ) );
184 }
185
186 // Unify all units to kg first.
187 if ( $from_unit !== $to_unit ) {
188 switch ( $from_unit ) {
189 case 'g':
190 $weight *= 0.001;
191 break;
192 case 'lbs':
193 $weight *= 0.453592;
194 break;
195 case 'oz':
196 $weight *= 0.0283495;
197 break;
198 }
199
200 // Output desired unit.
201 switch ( $to_unit ) {
202 case 'g':
203 $weight *= 1000;
204 break;
205 case 'lbs':
206 $weight *= 2.20462;
207 break;
208 case 'oz':
209 $weight *= 35.274;
210 break;
211 }
212 }
213
214 return ( $weight < 0 ) ? 0 : $weight;
215 }
216
217 /**
218 * Trim trailing zeros off prices.
219 *
220 * @param string|float|int $price Price.
221 * @return string
222 */
223 function wc_trim_zeros( $price ) {
224 return preg_replace( '/' . preg_quote( wc_get_price_decimal_separator(), '/' ) . '0++$/', '', $price ?? '' );
225 }
226
227 /**
228 * Round a tax amount.
229 *
230 * @param double $value Amount to round.
231 * @param int $precision DP to round. Defaults to wc_get_price_decimals.
232 * @return float
233 */
234 function wc_round_tax_total( $value, $precision = null ) {
235 $precision = is_null( $precision ) ? wc_get_price_decimals() : intval( $precision );
236 $rounded_tax = NumberUtil::round( $value, $precision, wc_get_tax_rounding_mode() ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctionParameters.round_modeFound
237
238 return apply_filters( 'wc_round_tax_total', $rounded_tax, $value, $precision, WC_TAX_ROUNDING_MODE );
239 }
240
241 /**
242 * Round half down in PHP 5.2.
243 *
244 * @since 3.2.6
245 * @param float $value Value to round.
246 * @param int $precision Precision to round down to.
247 * @return float
248 */
249 function wc_legacy_round_half_down( $value, $precision ) {
250 $value = wc_float_to_string( $value ) ?? '';
251
252 if ( false !== strstr( $value, '.' ) ) {
253 $value = explode( '.', $value );
254
255 if ( strlen( $value[1] ) > $precision && substr( $value[1], -1 ) === '5' ) {
256 $value[1] = substr( $value[1], 0, -1 ) . '4';
257 }
258
259 $value = implode( '.', $value );
260 }
261
262 return NumberUtil::round( floatval( $value ), $precision );
263 }
264
265 /**
266 * Make a refund total negative.
267 *
268 * @param float $amount Refunded amount.
269 *
270 * @return float
271 */
272 function wc_format_refund_total( $amount ) {
273 return $amount * -1;
274 }
275
276 /**
277 * Format decimal numbers ready for DB storage.
278 *
279 * Sanitize, optionally remove decimals, and optionally round + trim off zeros.
280 *
281 * This function does not remove thousands - this should be done before passing a value to the function.
282 *
283 * @param float|string $number Expects either a float or a string with a decimal separator only (no thousands).
284 * @param mixed $dp number Number of decimal points to use, blank to use woocommerce_price_num_decimals, or false to avoid all rounding.
285 * @param bool $trim_zeros From end of string.
286 * @return string
287 */
288 function wc_format_decimal( $number, $dp = false, $trim_zeros = false ) {
289 $number = $number ?? '';
290
291 $locale = localeconv();
292 $decimals = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] );
293
294 // Remove locale from string.
295 if ( ! is_float( $number ) ) {
296 $number = str_replace( $decimals, '.', $number );
297
298 // Convert multiple dots to just one.
299 $number = preg_replace( '/\.(?![^.]+$)|[^0-9.-]/', '', wc_clean( $number ) );
300 }
301
302 if ( false !== $dp ) {
303 $dp = intval( '' === $dp ? wc_get_price_decimals() : $dp );
304 $number = number_format( floatval( $number ), $dp, '.', '' );
305 } elseif ( is_float( $number ) ) {
306 // DP is false - don't use number format, just return a string using whatever is given. Remove scientific notation using sprintf.
307 $number = str_replace( $decimals, '.', sprintf( '%.' . wc_get_rounding_precision() . 'f', $number ) );
308 // We already had a float, so trailing zeros are not needed.
309 $trim_zeros = true;
310 }
311
312 if ( $trim_zeros && strstr( $number, '.' ) ) {
313 $number = rtrim( rtrim( $number, '0' ), '.' );
314 }
315
316 return $number;
317 }
318
319 /**
320 * Convert a float to a string without locale formatting which PHP adds when changing floats to strings.
321 *
322 * @param float $float Float value to format.
323 * @return string
324 */
325 function wc_float_to_string( $float ) {
326 if ( ! is_float( $float ) ) {
327 return $float;
328 }
329
330 $locale = localeconv();
331 $string = strval( $float );
332 $string = str_replace( $locale['decimal_point'], '.', $string );
333
334 return $string;
335 }
336
337 /**
338 * Format a price with WC Currency Locale settings.
339 *
340 * @param string $value Price to localize.
341 * @return string
342 */
343 function wc_format_localized_price( $value ) {
344 return apply_filters( 'woocommerce_format_localized_price', str_replace( '.', wc_get_price_decimal_separator(), strval( $value ) ), $value );
345 }
346
347 /**
348 * Format a decimal with the decimal separator for prices or PHP Locale settings.
349 *
350 * @param string $value Decimal to localize.
351 * @return string
352 */
353 function wc_format_localized_decimal( $value ) {
354 $locale = localeconv();
355 $decimal_point = isset( $locale['decimal_point'] ) ? $locale['decimal_point'] : '.';
356 $decimal = ( ! empty( wc_get_price_decimal_separator() ) ) ? wc_get_price_decimal_separator() : $decimal_point;
357 return apply_filters( 'woocommerce_format_localized_decimal', str_replace( '.', $decimal, strval( $value ) ), $value );
358 }
359
360 /**
361 * Format a coupon code.
362 *
363 * @since 3.0.0
364 * @param string $value Coupon code to format.
365 * @return string
366 */
367 function wc_format_coupon_code( $value ) {
368 return apply_filters( 'woocommerce_coupon_code', $value );
369 }
370
371 /**
372 * Sanitize a coupon code.
373 *
374 * Uses sanitize_post_field since coupon codes are stored as post_titles - the sanitization and escaping must match.
375 *
376 * Due to the unfiltered_html captability that some (admin) users have, we need to account for slashes.
377 *
378 * @since 3.6.0
379 * @param string $value Coupon code to format.
380 * @return string
381 */
382 function wc_sanitize_coupon_code( $value ) {
383 $value = wp_kses( sanitize_post_field( 'post_title', $value ?? '', 0, 'db' ), 'entities' );
384 return current_user_can( 'unfiltered_html' ) ? $value : stripslashes( $value );
385 }
386
387 /**
388 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
389 * Non-scalar values are ignored.
390 *
391 * @param string|array $var Data to sanitize.
392 * @return string|array
393 */
394 function wc_clean( $var ) {
395 if ( is_array( $var ) ) {
396 return array_map( 'wc_clean', $var );
397 } else {
398 return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
399 }
400 }
401
402 /**
403 * Function wp_check_invalid_utf8 with recursive array support.
404 *
405 * @param string|array $var Data to sanitize.
406 * @return string|array
407 */
408 function wc_check_invalid_utf8( $var ) {
409 if ( is_array( $var ) ) {
410 return array_map( 'wc_check_invalid_utf8', $var );
411 } else {
412 return wp_check_invalid_utf8( $var );
413 }
414 }
415
416 /**
417 * Run wc_clean over posted textarea but maintain line breaks.
418 *
419 * @since 3.0.0
420 * @param string $var Data to sanitize.
421 * @return string
422 */
423 function wc_sanitize_textarea( $var ) {
424 return implode( "\n", array_map( 'wc_clean', explode( "\n", $var ?? '' ) ) );
425 }
426
427 /**
428 * Sanitize a string destined to be a tooltip.
429 *
430 * @since 2.3.10 Tooltips are encoded with htmlspecialchars to prevent XSS. Should not be used in conjunction with esc_attr()
431 * @param string $var Data to sanitize.
432 * @return string
433 */
434 function wc_sanitize_tooltip( $var ) {
435 return htmlspecialchars(
436 wp_kses(
437 html_entity_decode( $var ?? '' ),
438 array(
439 'br' => array(),
440 'em' => array(),
441 'strong' => array(),
442 'small' => array(),
443 'span' => array(),
444 'ul' => array(),
445 'li' => array(),
446 'ol' => array(),
447 'p' => array(),
448 )
449 )
450 );
451 }
452
453 /**
454 * Merge two arrays.
455 *
456 * @param array $a1 First array to merge.
457 * @param array $a2 Second array to merge.
458 * @return array
459 */
460 function wc_array_overlay( $a1, $a2 ) {
461 foreach ( $a1 as $k => $v ) {
462 if ( ! array_key_exists( $k, $a2 ) ) {
463 continue;
464 }
465 if ( is_array( $v ) && is_array( $a2[ $k ] ) ) {
466 $a1[ $k ] = wc_array_overlay( $v, $a2[ $k ] );
467 } else {
468 $a1[ $k ] = $a2[ $k ];
469 }
470 }
471 return $a1;
472 }
473
474 /**
475 * Formats a stock amount by running it through a filter.
476 *
477 * @param int|float $amount Stock amount.
478 * @return int|float
479 */
480 function wc_stock_amount( $amount ) {
481 return apply_filters( 'woocommerce_stock_amount', $amount );
482 }
483
484 /**
485 * Get the price format depending on the currency position.
486 *
487 * @return string
488 */
489 function get_woocommerce_price_format() {
490 $currency_pos = get_option( 'woocommerce_currency_pos' );
491 $format = '%1$s%2$s';
492
493 switch ( $currency_pos ) {
494 case 'left':
495 $format = '%1$s%2$s';
496 break;
497 case 'right':
498 $format = '%2$s%1$s';
499 break;
500 case 'left_space':
501 $format = '%1$s&nbsp;%2$s';
502 break;
503 case 'right_space':
504 $format = '%2$s&nbsp;%1$s';
505 break;
506 }
507
508 return apply_filters( 'woocommerce_price_format', $format, $currency_pos );
509 }
510
511 /**
512 * Return the thousand separator for prices.
513 *
514 * @since 2.3
515 * @return string
516 */
517 function wc_get_price_thousand_separator() {
518 return stripslashes( apply_filters( 'wc_get_price_thousand_separator', get_option( 'woocommerce_price_thousand_sep' ) ) );
519 }
520
521 /**
522 * Return the decimal separator for prices.
523 *
524 * @since 2.3
525 * @return string
526 */
527 function wc_get_price_decimal_separator() {
528 $separator = apply_filters( 'wc_get_price_decimal_separator', get_option( 'woocommerce_price_decimal_sep' ) );
529 return $separator ? stripslashes( $separator ) : '.';
530 }
531
532 /**
533 * Return the number of decimals after the decimal point.
534 *
535 * @since 2.3
536 * @return int
537 */
538 function wc_get_price_decimals() {
539 return absint( apply_filters( 'wc_get_price_decimals', get_option( 'woocommerce_price_num_decimals', 2 ) ) );
540 }
541
542 /**
543 * Format the price with a currency symbol.
544 *
545 * @param float $price Raw price.
546 * @param array $args Arguments to format a price {
547 * Array of arguments.
548 * Defaults to empty array.
549 *
550 * @type bool $ex_tax_label Adds exclude tax label.
551 * Defaults to false.
552 * @type string $currency Currency code.
553 * Defaults to empty string (Use the result from get_woocommerce_currency()).
554 * @type string $decimal_separator Decimal separator.
555 * Defaults the result of wc_get_price_decimal_separator().
556 * @type string $thousand_separator Thousand separator.
557 * Defaults the result of wc_get_price_thousand_separator().
558 * @type string $decimals Number of decimals.
559 * Defaults the result of wc_get_price_decimals().
560 * @type string $price_format Price format depending on the currency position.
561 * Defaults the result of get_woocommerce_price_format().
562 * }
563 * @return string
564 */
565 function wc_price( $price, $args = array() ) {
566 $args = apply_filters(
567 'wc_price_args',
568 wp_parse_args(
569 $args,
570 array(
571 'ex_tax_label' => false,
572 'currency' => '',
573 'decimal_separator' => wc_get_price_decimal_separator(),
574 'thousand_separator' => wc_get_price_thousand_separator(),
575 'decimals' => wc_get_price_decimals(),
576 'price_format' => get_woocommerce_price_format(),
577 )
578 )
579 );
580
581 $original_price = $price;
582
583 // Convert to float to avoid issues on PHP 8.
584 $price = (float) $price;
585
586 $unformatted_price = $price;
587 $negative = $price < 0;
588
589 /**
590 * Filter raw price.
591 *
592 * @param float $raw_price Raw price.
593 * @param float|string $original_price Original price as float, or empty string. Since 5.0.0.
594 */
595 $price = apply_filters( 'raw_woocommerce_price', $negative ? $price * -1 : $price, $original_price );
596
597 /**
598 * Filter formatted price.
599 *
600 * @param float $formatted_price Formatted price.
601 * @param float $price Unformatted price.
602 * @param int $decimals Number of decimals.
603 * @param string $decimal_separator Decimal separator.
604 * @param string $thousand_separator Thousand separator.
605 * @param float|string $original_price Original price as float, or empty string. Since 5.0.0.
606 */
607 $price = apply_filters( 'formatted_woocommerce_price', number_format( $price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] ), $price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'], $original_price );
608
609 if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $args['decimals'] > 0 ) {
610 $price = wc_trim_zeros( $price );
611 }
612
613 $formatted_price = ( $negative ? '-' : '' ) . sprintf( $args['price_format'], '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $args['currency'] ) . '</span>', $price );
614 $return = '<span class="woocommerce-Price-amount amount"><bdi>' . $formatted_price . '</bdi></span>';
615
616 if ( $args['ex_tax_label'] && wc_tax_enabled() ) {
617 $return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
618 }
619
620 /**
621 * Filters the string of price markup.
622 *
623 * @param string $return Price HTML markup.
624 * @param string $price Formatted price.
625 * @param array $args Pass on the args.
626 * @param float $unformatted_price Price as float to allow plugins custom formatting. Since 3.2.0.
627 * @param float|string $original_price Original price as float, or empty string. Since 5.0.0.
628 */
629 return apply_filters( 'wc_price', $return, $price, $args, $unformatted_price, $original_price );
630 }
631
632 /**
633 * Notation to numbers.
634 *
635 * This function transforms the php.ini notation for numbers (like '2M') to an integer.
636 *
637 * @param string $size Size value.
638 * @return int
639 */
640 function wc_let_to_num( $size ) {
641 $size = $size ?? '';
642
643 $l = substr( $size, -1 );
644 $ret = (int) substr( $size, 0, -1 );
645 switch ( strtoupper( $l ) ) {
646 case 'P':
647 $ret *= 1024;
648 // No break.
649 case 'T':
650 $ret *= 1024;
651 // No break.
652 case 'G':
653 $ret *= 1024;
654 // No break.
655 case 'M':
656 $ret *= 1024;
657 // No break.
658 case 'K':
659 $ret *= 1024;
660 // No break.
661 }
662 return $ret;
663 }
664
665 /**
666 * WooCommerce Date Format - Allows to change date format for everything WooCommerce.
667 *
668 * @return string
669 */
670 function wc_date_format() {
671 $date_format = get_option( 'date_format' );
672 if ( empty( $date_format ) ) {
673 // Return default date format if the option is empty.
674 $date_format = 'F j, Y';
675 }
676 return apply_filters( 'woocommerce_date_format', $date_format );
677 }
678
679 /**
680 * WooCommerce Time Format - Allows to change time format for everything WooCommerce.
681 *
682 * @return string
683 */
684 function wc_time_format() {
685 $time_format = get_option( 'time_format' );
686 if ( empty( $time_format ) ) {
687 // Return default time format if the option is empty.
688 $time_format = 'g:i a';
689 }
690 return apply_filters( 'woocommerce_time_format', $time_format );
691 }
692
693 /**
694 * Convert mysql datetime to PHP timestamp, forcing UTC. Wrapper for strtotime.
695 *
696 * Based on wcs_strtotime_dark_knight() from WC Subscriptions by Prospress.
697 *
698 * @since 3.0.0
699 * @param string $time_string Time string.
700 * @param int|null $from_timestamp Timestamp to convert from.
701 * @return int
702 */
703 function wc_string_to_timestamp( $time_string, $from_timestamp = null ) {
704 $time_string = $time_string ?? '';
705
706 $original_timezone = date_default_timezone_get();
707
708 // @codingStandardsIgnoreStart
709 date_default_timezone_set( 'UTC' );
710
711 if ( null === $from_timestamp ) {
712 $next_timestamp = strtotime( $time_string );
713 } else {
714 $next_timestamp = strtotime( $time_string, $from_timestamp );
715 }
716
717 date_default_timezone_set( $original_timezone );
718 // @codingStandardsIgnoreEnd
719
720 return $next_timestamp;
721 }
722
723 /**
724 * Convert a date string to a WC_DateTime.
725 *
726 * @since 3.1.0
727 * @param string $time_string Time string.
728 * @return WC_DateTime
729 */
730 function wc_string_to_datetime( $time_string ) {
731 $time_string = $time_string ?? '';
732
733 // Strings are defined in local WP timezone. Convert to UTC.
734 if ( 1 === preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|((-|\+)\d{2}:\d{2}))$/', $time_string, $date_bits ) ) {
735 $offset = ! empty( $date_bits[7] ) ? iso8601_timezone_to_offset( $date_bits[7] ) : wc_timezone_offset();
736 $timestamp = gmmktime( $date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1] ) - $offset;
737 } else {
738 $timestamp = wc_string_to_timestamp( get_gmt_from_date( gmdate( 'Y-m-d H:i:s', wc_string_to_timestamp( $time_string ) ) ) );
739 }
740 $datetime = new WC_DateTime( "@{$timestamp}", new DateTimeZone( 'UTC' ) );
741
742 // Set local timezone or offset.
743 if ( get_option( 'timezone_string' ) ) {
744 $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) );
745 } else {
746 $datetime->set_utc_offset( wc_timezone_offset() );
747 }
748
749 return $datetime;
750 }
751
752 /**
753 * WooCommerce Timezone - helper to retrieve the timezone string for a site until.
754 * a WP core method exists (see https://core.trac.wordpress.org/ticket/24730).
755 *
756 * Adapted from https://secure.php.net/manual/en/function.timezone-name-from-abbr.php#89155.
757 *
758 * @since 2.1
759 * @return string PHP timezone string for the site
760 */
761 function wc_timezone_string() {
762 // Added in WordPress 5.3 Ref https://developer.wordpress.org/reference/functions/wp_timezone_string/.
763 if ( function_exists( 'wp_timezone_string' ) ) {
764 return wp_timezone_string();
765 }
766
767 // If site timezone string exists, return it.
768 $timezone = get_option( 'timezone_string' );
769 if ( $timezone ) {
770 return $timezone;
771 }
772
773 // Get UTC offset, if it isn't set then return UTC.
774 $utc_offset = floatval( get_option( 'gmt_offset', 0 ) );
775 if ( ! is_numeric( $utc_offset ) || 0.0 === $utc_offset ) {
776 return 'UTC';
777 }
778
779 // Adjust UTC offset from hours to seconds.
780 $utc_offset = (int) ( $utc_offset * 3600 );
781
782 // Attempt to guess the timezone string from the UTC offset.
783 $timezone = timezone_name_from_abbr( '', $utc_offset );
784 if ( $timezone ) {
785 return $timezone;
786 }
787
788 // Last try, guess timezone string manually.
789 foreach ( timezone_abbreviations_list() as $abbr ) {
790 foreach ( $abbr as $city ) {
791 // WordPress restrict the use of date(), since it's affected by timezone settings, but in this case is just what we need to guess the correct timezone.
792 if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) { // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
793 return $city['timezone_id'];
794 }
795 }
796 }
797
798 // Fallback to UTC.
799 return 'UTC';
800 }
801
802 /**
803 * Get timezone offset in seconds.
804 *
805 * @since 3.0.0
806 * @return float
807 */
808 function wc_timezone_offset() {
809 $timezone = get_option( 'timezone_string' );
810
811 if ( $timezone ) {
812 $timezone_object = new DateTimeZone( $timezone );
813 return $timezone_object->getOffset( new DateTime( 'now' ) );
814 } else {
815 return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS;
816 }
817 }
818
819 /**
820 * Callback which can flatten post meta (gets the first value if it's an array).
821 *
822 * @since 3.0.0
823 * @param array $value Value to flatten.
824 * @return mixed
825 */
826 function wc_flatten_meta_callback( $value ) {
827 return is_array( $value ) ? current( $value ) : $value;
828 }
829
830 if ( ! function_exists( 'wc_rgb_from_hex' ) ) {
831
832 /**
833 * Convert RGB to HEX.
834 *
835 * @param mixed $color Color.
836 *
837 * @return array
838 */
839 function wc_rgb_from_hex( $color ) {
840 $color = str_replace( '#', '', $color ?? '000' );
841 // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF".
842 $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
843
844 $rgb = array();
845 $rgb['R'] = hexdec( $color[0] . $color[1] );
846 $rgb['G'] = hexdec( $color[2] . $color[3] );
847 $rgb['B'] = hexdec( $color[4] . $color[5] );
848
849 return $rgb;
850 }
851 }
852
853 if ( ! function_exists( 'wc_hex_darker' ) ) {
854
855 /**
856 * Make HEX color darker.
857 *
858 * @param mixed $color Color.
859 * @param int $factor Darker factor.
860 * Defaults to 30.
861 * @return string
862 */
863 function wc_hex_darker( $color, $factor = 30 ) {
864 $base = wc_rgb_from_hex( $color );
865 $color = '#';
866
867 foreach ( $base as $k => $v ) {
868 $amount = $v / 100;
869 $amount = NumberUtil::round( $amount * $factor );
870 $new_decimal = $v - $amount;
871
872 $new_hex_component = dechex( $new_decimal );
873 if ( strlen( $new_hex_component ) < 2 ) {
874 $new_hex_component = '0' . $new_hex_component;
875 }
876 $color .= $new_hex_component;
877 }
878
879 return $color;
880 }
881 }
882
883 if ( ! function_exists( 'wc_hex_lighter' ) ) {
884
885 /**
886 * Make HEX color lighter.
887 *
888 * @param mixed $color Color.
889 * @param int $factor Lighter factor.
890 * Defaults to 30.
891 * @return string
892 */
893 function wc_hex_lighter( $color, $factor = 30 ) {
894 $base = wc_rgb_from_hex( $color );
895 $color = '#';
896
897 foreach ( $base as $k => $v ) {
898 $amount = 255 - $v;
899 $amount = $amount / 100;
900 $amount = NumberUtil::round( $amount * $factor );
901 $new_decimal = $v + $amount;
902
903 $new_hex_component = dechex( $new_decimal );
904 if ( strlen( $new_hex_component ) < 2 ) {
905 $new_hex_component = '0' . $new_hex_component;
906 }
907 $color .= $new_hex_component;
908 }
909
910 return $color;
911 }
912 }
913
914 if ( ! function_exists( 'wc_hex_is_light' ) ) {
915
916 /**
917 * Determine whether a hex color is light.
918 *
919 * @param mixed $color Color.
920 * @return bool True if a light color.
921 */
922 function wc_hex_is_light( $color ) {
923 $hex = str_replace( '#', '', $color ?? '' );
924
925 $c_r = hexdec( substr( $hex, 0, 2 ) );
926 $c_g = hexdec( substr( $hex, 2, 2 ) );
927 $c_b = hexdec( substr( $hex, 4, 2 ) );
928
929 $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
930
931 return $brightness > 155;
932 }
933 }
934
935 if ( ! function_exists( 'wc_light_or_dark' ) ) {
936
937 /**
938 * Detect if we should use a light or dark color on a background color.
939 *
940 * @param mixed $color Color.
941 * @param string $dark Darkest reference.
942 * Defaults to '#000000'.
943 * @param string $light Lightest reference.
944 * Defaults to '#FFFFFF'.
945 * @return string
946 */
947 function wc_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
948 return wc_hex_is_light( $color ) ? $dark : $light;
949 }
950 }
951
952 if ( ! function_exists( 'wc_format_hex' ) ) {
953
954 /**
955 * Format string as hex.
956 *
957 * @param string $hex HEX color.
958 * @return string|null
959 */
960 function wc_format_hex( $hex ) {
961 $hex = trim( str_replace( '#', '', $hex ?? '' ) );
962
963 if ( strlen( $hex ) === 3 ) {
964 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
965 }
966
967 return $hex ? '#' . $hex : null;
968 }
969 }
970
971 /**
972 * Format the postcode according to the country and length of the postcode.
973 *
974 * @param string $postcode Unformatted postcode.
975 * @param string $country Base country.
976 * @return string
977 */
978 function wc_format_postcode( $postcode, $country ) {
979 $postcode = wc_normalize_postcode( $postcode ?? '' );
980
981 switch ( $country ) {
982 case 'SE':
983 $postcode = substr_replace( $postcode, ' ', -2, 0 );
984 break;
985 case 'CA':
986 case 'GB':
987 $postcode = substr_replace( $postcode, ' ', -3, 0 );
988 break;
989 case 'IE':
990 $postcode = substr_replace( $postcode, ' ', 3, 0 );
991 break;
992 case 'BR':
993 case 'PL':
994 $postcode = substr_replace( $postcode, '-', -3, 0 );
995 break;
996 case 'JP':
997 $postcode = substr_replace( $postcode, '-', 3, 0 );
998 break;
999 case 'PT':
1000 $postcode = substr_replace( $postcode, '-', 4, 0 );
1001 break;
1002 case 'PR':
1003 case 'US':
1004 case 'MN':
1005 $postcode = rtrim( substr_replace( $postcode, '-', 5, 0 ), '-' );
1006 break;
1007 case 'NL':
1008 $postcode = substr_replace( $postcode, ' ', 4, 0 );
1009 break;
1010 case 'LV':
1011 $postcode = preg_replace( '/^(LV)?-?(\d+)$/', 'LV-${2}', $postcode );
1012 break;
1013 case 'CZ':
1014 case 'SK':
1015 $postcode = preg_replace( "/^({$country})-?(\d+)$/", '${1}-${2}', $postcode );
1016 $postcode = substr_replace( $postcode, ' ', -2, 0 );
1017 break;
1018 case 'DK':
1019 $postcode = preg_replace( '/^(DK)(.+)$/', '${1}-${2}', $postcode );
1020 break;
1021 }
1022
1023 return apply_filters( 'woocommerce_format_postcode', trim( $postcode ), $country );
1024 }
1025
1026 /**
1027 * Normalize postcodes.
1028 *
1029 * Remove spaces and convert characters to uppercase.
1030 *
1031 * @since 2.6.0
1032 * @param string $postcode Postcode.
1033 * @return string
1034 */
1035 function wc_normalize_postcode( $postcode ) {
1036 return preg_replace( '/[\s\-]/', '', trim( wc_strtoupper( $postcode ?? '' ) ) );
1037 }
1038
1039 /**
1040 * Format phone numbers.
1041 *
1042 * @param string $phone Phone number.
1043 * @return string
1044 */
1045 function wc_format_phone_number( $phone ) {
1046 $phone = $phone ?? '';
1047
1048 if ( ! WC_Validation::is_phone( $phone ) ) {
1049 return '';
1050 }
1051 return preg_replace( '/[^0-9\+\-\(\)\s]/', '-', preg_replace( '/[\x00-\x1F\x7F-\xFF]/', '', $phone ) );
1052 }
1053
1054 /**
1055 * Sanitize phone number.
1056 * Allows only numbers and "+" (plus sign).
1057 *
1058 * @since 3.6.0
1059 * @param string $phone Phone number.
1060 * @return string
1061 */
1062 function wc_sanitize_phone_number( $phone ) {
1063 return preg_replace( '/[^\d+]/', '', $phone ?? '' );
1064 }
1065
1066 /**
1067 * Wrapper for mb_strtoupper which see's if supported first.
1068 *
1069 * @since 3.1.0
1070 * @param string $string String to format.
1071 * @return string
1072 */
1073 function wc_strtoupper( $string ) {
1074 $string = $string ?? '';
1075 return function_exists( 'mb_strtoupper' ) ? mb_strtoupper( $string ) : strtoupper( $string );
1076 }
1077
1078 /**
1079 * Make a string lowercase.
1080 * Try to use mb_strtolower() when available.
1081 *
1082 * @since 2.3
1083 * @param string $string String to format.
1084 * @return string
1085 */
1086 function wc_strtolower( $string ) {
1087 $string = $string ?? '';
1088 return function_exists( 'mb_strtolower' ) ? mb_strtolower( $string ) : strtolower( $string );
1089 }
1090
1091 /**
1092 * Trim a string and append a suffix.
1093 *
1094 * @param string $string String to trim.
1095 * @param integer $chars Amount of characters.
1096 * Defaults to 200.
1097 * @param string $suffix Suffix.
1098 * Defaults to '...'.
1099 * @return string
1100 */
1101 function wc_trim_string( $string, $chars = 200, $suffix = '...' ) {
1102 $string = $string ?? '';
1103
1104 if ( strlen( $string ) > $chars ) {
1105 if ( function_exists( 'mb_substr' ) ) {
1106 $string = mb_substr( $string, 0, ( $chars - mb_strlen( $suffix ) ) ) . $suffix;
1107 } else {
1108 $string = substr( $string, 0, ( $chars - strlen( $suffix ) ) ) . $suffix;
1109 }
1110 }
1111 return $string;
1112 }
1113
1114 /**
1115 * Format content to display shortcodes.
1116 *
1117 * @since 2.3.0
1118 * @param string $raw_string Raw string.
1119 * @return string
1120 */
1121 function wc_format_content( $raw_string ) {
1122 $raw_string = $raw_string ?? '';
1123 return apply_filters( 'woocommerce_format_content', apply_filters( 'woocommerce_short_description', $raw_string ), $raw_string );
1124 }
1125
1126 /**
1127 * Format product short description.
1128 * Adds support for Jetpack Markdown.
1129 *
1130 * @codeCoverageIgnore
1131 * @since 2.4.0
1132 * @param string $content Product short description.
1133 * @return string
1134 */
1135 function wc_format_product_short_description( $content ) {
1136 // Add support for Jetpack Markdown.
1137 if ( class_exists( 'WPCom_Markdown' ) ) {
1138 $markdown = WPCom_Markdown::get_instance();
1139
1140 return wpautop(
1141 $markdown->transform(
1142 $content,
1143 array(
1144 'unslash' => false,
1145 )
1146 )
1147 );
1148 }
1149
1150 return $content;
1151 }
1152
1153 /**
1154 * Formats curency symbols when saved in settings.
1155 *
1156 * @codeCoverageIgnore
1157 * @param string $value Option value.
1158 * @param array $option Option name.
1159 * @param string $raw_value Raw value.
1160 * @return string
1161 */
1162 function wc_format_option_price_separators( $value, $option, $raw_value ) {
1163 return wp_kses_post( $raw_value ?? '' );
1164 }
1165 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_price_decimal_sep', 'wc_format_option_price_separators', 10, 3 );
1166 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_price_thousand_sep', 'wc_format_option_price_separators', 10, 3 );
1167
1168 /**
1169 * Formats decimals when saved in settings.
1170 *
1171 * @codeCoverageIgnore
1172 * @param string $value Option value.
1173 * @param array $option Option name.
1174 * @param string $raw_value Raw value.
1175 * @return string
1176 */
1177 function wc_format_option_price_num_decimals( $value, $option, $raw_value ) {
1178 return is_null( $raw_value ) ? 2 : absint( $raw_value );
1179 }
1180 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_price_num_decimals', 'wc_format_option_price_num_decimals', 10, 3 );
1181
1182 /**
1183 * Formats hold stock option and sets cron event up.
1184 *
1185 * @codeCoverageIgnore
1186 * @param string $value Option value.
1187 * @param array $option Option name.
1188 * @param string $raw_value Raw value.
1189 * @return string
1190 */
1191 function wc_format_option_hold_stock_minutes( $value, $option, $raw_value ) {
1192 $value = ! empty( $raw_value ) ? absint( $raw_value ) : ''; // Allow > 0 or set to ''.
1193
1194 wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
1195
1196 if ( '' !== $value ) {
1197 $cancel_unpaid_interval = apply_filters( 'woocommerce_cancel_unpaid_orders_interval_minutes', absint( $value ) );
1198 wp_schedule_single_event( time() + ( absint( $cancel_unpaid_interval ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
1199 }
1200
1201 return $value;
1202 }
1203 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_hold_stock_minutes', 'wc_format_option_hold_stock_minutes', 10, 3 );
1204
1205 /**
1206 * Sanitize terms from an attribute text based.
1207 *
1208 * @since 2.4.5
1209 * @param string $term Term value.
1210 * @return string
1211 */
1212 function wc_sanitize_term_text_based( $term ) {
1213 return trim( wp_strip_all_tags( wp_unslash( $term ?? '' ) ) );
1214 }
1215
1216 if ( ! function_exists( 'wc_make_numeric_postcode' ) ) {
1217 /**
1218 * Make numeric postcode.
1219 *
1220 * Converts letters to numbers so we can do a simple range check on postcodes.
1221 * E.g. PE30 becomes 16050300 (P = 16, E = 05, 3 = 03, 0 = 00)
1222 *
1223 * @since 2.6.0
1224 * @param string $postcode Regular postcode.
1225 * @return string
1226 */
1227 function wc_make_numeric_postcode( $postcode ) {
1228 $postcode = str_replace( array( ' ', '-' ), '', $postcode ?? '' );
1229 $postcode_length = strlen( $postcode );
1230 $letters_to_numbers = array_merge( array( 0 ), range( 'A', 'Z' ) );
1231 $letters_to_numbers = array_flip( $letters_to_numbers );
1232 $numeric_postcode = '';
1233
1234 for ( $i = 0; $i < $postcode_length; $i++ ) {
1235 if ( is_numeric( $postcode[ $i ] ) ) {
1236 $numeric_postcode .= str_pad( $postcode[ $i ], 2, '0', STR_PAD_LEFT );
1237 } elseif ( isset( $letters_to_numbers[ $postcode[ $i ] ] ) ) {
1238 $numeric_postcode .= str_pad( $letters_to_numbers[ $postcode[ $i ] ], 2, '0', STR_PAD_LEFT );
1239 } else {
1240 $numeric_postcode .= '00';
1241 }
1242 }
1243
1244 return $numeric_postcode;
1245 }
1246 }
1247
1248 /**
1249 * Format the stock amount ready for display based on settings.
1250 *
1251 * @since 3.0.0
1252 * @param WC_Product $product Product object for which the stock you need to format.
1253 * @return string
1254 */
1255 function wc_format_stock_for_display( $product ) {
1256 $display = __( 'In stock', 'woocommerce' );
1257 $stock_amount = $product->get_stock_quantity();
1258
1259 switch ( get_option( 'woocommerce_stock_format' ) ) {
1260 case 'low_amount':
1261 if ( $stock_amount <= wc_get_low_stock_amount( $product ) ) {
1262 /* translators: %s: stock amount */
1263 $display = sprintf( __( 'Only %s left in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
1264 }
1265 break;
1266 case '':
1267 /* translators: %s: stock amount */
1268 $display = sprintf( __( '%s in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
1269 break;
1270 }
1271
1272 if ( $product->backorders_allowed() && $product->backorders_require_notification() ) {
1273 $display .= ' ' . __( '(can be backordered)', 'woocommerce' );
1274 }
1275
1276 return $display;
1277 }
1278
1279 /**
1280 * Format the stock quantity ready for display.
1281 *
1282 * @since 3.0.0
1283 * @param int $stock_quantity Stock quantity.
1284 * @param WC_Product $product Product instance so that we can pass through the filters.
1285 * @return string
1286 */
1287 function wc_format_stock_quantity_for_display( $stock_quantity, $product ) {
1288 return apply_filters( 'woocommerce_format_stock_quantity', $stock_quantity, $product );
1289 }
1290
1291 /**
1292 * Format a sale price for display.
1293 *
1294 * @since 3.0.0
1295 * @param string $regular_price Regular price.
1296 * @param string $sale_price Sale price.
1297 * @return string
1298 */
1299 function wc_format_sale_price( $regular_price, $sale_price ) {
1300 // Format the prices.
1301 $formatted_regular_price = is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price;
1302 $formatted_sale_price = is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price;
1303
1304 // Strikethrough pricing.
1305 $price = '<del aria-hidden="true">' . $formatted_regular_price . '</del> ';
1306
1307 // For accessibility (a11y) we'll also display that information to screen readers.
1308 $price .= '<span class="screen-reader-text">';
1309 // translators: %s is a product's regular price.
1310 $price .= esc_html( sprintf( __( 'Original price was: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_regular_price ) ) );
1311 $price .= '</span>';
1312
1313 // Add the sale price.
1314 $price .= '<ins aria-hidden="true">' . $formatted_sale_price . '</ins>';
1315
1316 // For accessibility (a11y) we'll also display that information to screen readers.
1317 $price .= '<span class="screen-reader-text">';
1318 // translators: %s is a product's current (sale) price.
1319 $price .= esc_html( sprintf( __( 'Current price is: %s.', 'woocommerce' ), wp_strip_all_tags( $formatted_sale_price ) ) );
1320 $price .= '</span>';
1321
1322 return apply_filters( 'woocommerce_format_sale_price', $price, $regular_price, $sale_price );
1323 }
1324
1325 /**
1326 * Format a price range for display.
1327 *
1328 * @param string $from Price from.
1329 * @param string $to Price to.
1330 * @return string
1331 */
1332 function wc_format_price_range( $from, $to ) {
1333 /* translators: 1: price from 2: price to */
1334 $price = sprintf( _x( '%1$s &ndash; %2$s', 'Price range: from-to', 'woocommerce' ), is_numeric( $from ) ? wc_price( $from ) : $from, is_numeric( $to ) ? wc_price( $to ) : $to );
1335 return apply_filters( 'woocommerce_format_price_range', $price, $from, $to );
1336 }
1337
1338 /**
1339 * Format a weight for display.
1340 *
1341 * @since 3.0.0
1342 * @param float $weight Weight.
1343 * @return string
1344 */
1345 function wc_format_weight( $weight ) {
1346 $weight_string = wc_format_localized_decimal( $weight );
1347
1348 if ( ! empty( $weight_string ) ) {
1349 $weight_label = I18nUtil::get_weight_unit_label( get_option( 'woocommerce_weight_unit' ) );
1350
1351 $weight_string = sprintf(
1352 // translators: 1. A formatted number; 2. A label for a weight unit of measure. E.g. 2.72 kg.
1353 _x( '%1$s %2$s', 'formatted weight', 'woocommerce' ),
1354 $weight_string,
1355 $weight_label
1356 );
1357 } else {
1358 $weight_string = __( 'N/A', 'woocommerce' );
1359 }
1360
1361 return apply_filters( 'woocommerce_format_weight', $weight_string, $weight );
1362 }
1363
1364 /**
1365 * Format dimensions for display.
1366 *
1367 * @since 3.0.0
1368 * @param array $dimensions Array of dimensions.
1369 * @return string
1370 */
1371 function wc_format_dimensions( $dimensions ) {
1372 $dimension_string = implode( ' &times; ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );
1373
1374 if ( ! empty( $dimension_string ) ) {
1375 $dimension_label = I18nUtil::get_dimensions_unit_label( get_option( 'woocommerce_dimension_unit' ) );
1376
1377 $dimension_string = sprintf(
1378 // translators: 1. A formatted number; 2. A label for a dimensions unit of measure. E.g. 3.14 cm.
1379 _x( '%1$s %2$s', 'formatted dimensions', 'woocommerce' ),
1380 $dimension_string,
1381 $dimension_label
1382 );
1383 } else {
1384 $dimension_string = __( 'N/A', 'woocommerce' );
1385 }
1386
1387 return apply_filters( 'woocommerce_format_dimensions', $dimension_string, $dimensions );
1388 }
1389
1390 /**
1391 * Format a date for output.
1392 *
1393 * @since 3.0.0
1394 * @param WC_DateTime $date Instance of WC_DateTime.
1395 * @param string $format Data format.
1396 * Defaults to the wc_date_format function if not set.
1397 * @return string
1398 */
1399 function wc_format_datetime( $date, $format = '' ) {
1400 if ( ! $format ) {
1401 $format = wc_date_format();
1402 }
1403 if ( ! is_a( $date, 'WC_DateTime' ) ) {
1404 return '';
1405 }
1406 return $date->date_i18n( $format );
1407 }
1408
1409 /**
1410 * Process oEmbeds.
1411 *
1412 * @since 3.1.0
1413 * @param string $content Content.
1414 * @return string
1415 */
1416 function wc_do_oembeds( $content ) {
1417 global $wp_embed;
1418
1419 $content = $wp_embed->autoembed( $content ?? '' );
1420
1421 return $content;
1422 }
1423
1424 /**
1425 * Get part of a string before :.
1426 *
1427 * Used for example in shipping methods ids where they take the format
1428 * method_id:instance_id
1429 *
1430 * @since 3.2.0
1431 * @param string $string String to extract.
1432 * @return string
1433 */
1434 function wc_get_string_before_colon( $string ) {
1435 return trim( current( explode( ':', (string) $string ) ) );
1436 }
1437
1438 /**
1439 * Array merge and sum function.
1440 *
1441 * Source: https://gist.github.com/Nickology/f700e319cbafab5eaedc
1442 *
1443 * @since 3.2.0
1444 * @return array
1445 */
1446 function wc_array_merge_recursive_numeric() {
1447 $arrays = func_get_args();
1448
1449 // If there's only one array, it's already merged.
1450 if ( 1 === count( $arrays ) ) {
1451 return $arrays[0];
1452 }
1453
1454 // Remove any items in $arrays that are NOT arrays.
1455 foreach ( $arrays as $key => $array ) {
1456 if ( ! is_array( $array ) ) {
1457 unset( $arrays[ $key ] );
1458 }
1459 }
1460
1461 // We start by setting the first array as our final array.
1462 // We will merge all other arrays with this one.
1463 $final = array_shift( $arrays );
1464
1465 foreach ( $arrays as $b ) {
1466 foreach ( $final as $key => $value ) {
1467 // If $key does not exist in $b, then it is unique and can be safely merged.
1468 if ( ! isset( $b[ $key ] ) ) {
1469 $final[ $key ] = $value;
1470 } else {
1471 // If $key is present in $b, then we need to merge and sum numeric values in both.
1472 if ( is_numeric( $value ) && is_numeric( $b[ $key ] ) ) {
1473 // If both values for these keys are numeric, we sum them.
1474 $final[ $key ] = $value + $b[ $key ];
1475 } elseif ( is_array( $value ) && is_array( $b[ $key ] ) ) {
1476 // If both values are arrays, we recursively call ourself.
1477 $final[ $key ] = wc_array_merge_recursive_numeric( $value, $b[ $key ] );
1478 } else {
1479 // If both keys exist but differ in type, then we cannot merge them.
1480 // In this scenario, we will $b's value for $key is used.
1481 $final[ $key ] = $b[ $key ];
1482 }
1483 }
1484 }
1485
1486 // Finally, we need to merge any keys that exist only in $b.
1487 foreach ( $b as $key => $value ) {
1488 if ( ! isset( $final[ $key ] ) ) {
1489 $final[ $key ] = $value;
1490 }
1491 }
1492 }
1493
1494 return $final;
1495 }
1496
1497 /**
1498 * Implode and escape HTML attributes for output.
1499 *
1500 * @since 3.3.0
1501 * @param array $raw_attributes Attribute name value pairs.
1502 * @return string
1503 */
1504 function wc_implode_html_attributes( $raw_attributes ) {
1505 $attributes = array();
1506 foreach ( $raw_attributes as $name => $value ) {
1507 $attributes[] = esc_attr( $name ) . '="' . esc_attr( $value ) . '"';
1508 }
1509 return implode( ' ', $attributes );
1510 }
1511
1512 /**
1513 * Escape JSON for use on HTML or attribute text nodes.
1514 *
1515 * @since 3.5.5
1516 * @param string $json JSON to escape.
1517 * @param bool $html True if escaping for HTML text node, false for attributes. Determines how quotes are handled.
1518 * @return string Escaped JSON.
1519 */
1520 function wc_esc_json( $json, $html = false ) {
1521 return _wp_specialchars(
1522 $json,
1523 $html ? ENT_NOQUOTES : ENT_QUOTES, // Escape quotes in attribute nodes only.
1524 'UTF-8', // json_encode() outputs UTF-8 (really just ASCII), not the blog's charset.
1525 true // Double escape entities: `&amp;` -> `&amp;amp;`.
1526 );
1527 }
1528
1529 /**
1530 * Parse a relative date option from the settings API into a standard format.
1531 *
1532 * @since 3.4.0
1533 * @param mixed $raw_value Value stored in DB.
1534 * @return array Nicely formatted array with number and unit values.
1535 */
1536 function wc_parse_relative_date_option( $raw_value ) {
1537 $periods = array(
1538 'days' => __( 'Day(s)', 'woocommerce' ),
1539 'weeks' => __( 'Week(s)', 'woocommerce' ),
1540 'months' => __( 'Month(s)', 'woocommerce' ),
1541 'years' => __( 'Year(s)', 'woocommerce' ),
1542 );
1543
1544 $value = wp_parse_args(
1545 (array) $raw_value,
1546 array(
1547 'number' => '',
1548 'unit' => 'days',
1549 )
1550 );
1551
1552 $value['number'] = ! empty( $value['number'] ) ? absint( $value['number'] ) : '';
1553
1554 if ( ! in_array( $value['unit'], array_keys( $periods ), true ) ) {
1555 $value['unit'] = 'days';
1556 }
1557
1558 return $value;
1559 }
1560
1561 /**
1562 * Format the endpoint slug, strip out anything not allowed in a url.
1563 *
1564 * @since 3.5.0
1565 * @param string $raw_value The raw value.
1566 * @return string
1567 */
1568 function wc_sanitize_endpoint_slug( $raw_value ) {
1569 return sanitize_title( $raw_value ?? '' );
1570 }
1571 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_checkout_pay_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1572 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_checkout_order_received_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1573 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_add_payment_method_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1574 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_delete_payment_method_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1575 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_set_default_payment_method_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1576 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_orders_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1577 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_view_order_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1578 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_downloads_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1579 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_edit_account_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1580 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_edit_address_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1581 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_payment_methods_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1582 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_lost_password_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1583 add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_logout_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
1584