2011-08-09 15:16:18 +00:00
< ? php
2018-03-13 11:37:48 +00:00
/**
2018-03-21 03:21:41 +00:00
* General user data validation methods
2018-03-13 11:37:48 +00:00
*
* @ package WooCommerce\Classes
2018-03-21 03:21:41 +00:00
* @ version 2.4 . 0
2018-03-13 11:37:48 +00:00
*/
2015-11-06 09:22:19 +00:00
2018-03-21 03:21:41 +00:00
defined ( 'ABSPATH' ) || exit ;
2015-11-06 09:22:19 +00:00
2011-08-09 15:16:18 +00:00
/**
2018-03-21 03:21:41 +00:00
* Validation class .
2011-08-09 15:16:18 +00:00
*/
2012-01-27 16:38:39 +00:00
class WC_Validation {
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
/**
2017-03-01 17:44:11 +00:00
* Validates an email using WordPress native is_email function .
2011-08-09 15:16:18 +00:00
*
2018-03-21 03:21:41 +00:00
* @ param string $email Email address to validate .
* @ return bool
2011-08-09 15:16:18 +00:00
*/
2013-09-12 13:41:02 +00:00
public static function is_email ( $email ) {
2011-08-09 15:16:18 +00:00
return is_email ( $email );
}
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
/**
2015-11-03 13:31:20 +00:00
* Validates a phone number using a regular expression .
2011-08-09 15:16:18 +00:00
*
2018-03-21 03:21:41 +00:00
* @ param string $phone Phone number to validate .
* @ return bool
2011-08-09 15:16:18 +00:00
*/
2013-09-12 13:41:02 +00:00
public static function is_phone ( $phone ) {
2018-08-24 13:43:46 +00:00
if ( 0 < strlen ( trim ( preg_replace ( '/[\s\#0-9_\-\+\/\(\)\.]/' , '' , $phone ) ) ) ) {
2012-07-20 11:40:13 +00:00
return false ;
2015-06-08 14:50:21 +00:00
}
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
return true ;
}
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
/**
2015-11-03 13:31:20 +00:00
* Checks for a valid postcode .
2011-08-09 15:16:18 +00:00
*
2018-03-21 03:21:41 +00:00
* @ param string $postcode Postcode to validate .
* @ param string $country Country to validate the postcode for .
* @ return bool
2011-08-09 15:16:18 +00:00
*/
2013-09-12 13:41:02 +00:00
public static function is_postcode ( $postcode , $country ) {
2015-06-08 14:50:21 +00:00
if ( strlen ( trim ( preg_replace ( '/[\s\-A-Za-z0-9]/' , '' , $postcode ) ) ) > 0 ) {
2012-07-20 11:40:13 +00:00
return false ;
2015-06-08 14:50:21 +00:00
}
2012-08-14 19:42:38 +00:00
2012-07-20 11:40:13 +00:00
switch ( $country ) {
2018-03-13 11:37:48 +00:00
case 'AT' :
2016-05-23 11:32:16 +00:00
$valid = ( bool ) preg_match ( '/^([0-9]{4})$/' , $postcode );
break ;
2020-07-16 11:52:01 +00:00
case 'BA' :
$valid = ( bool ) preg_match ( '/^([7-8]{1})([0-9]{4})$/' , $postcode );
break ;
2018-03-13 11:37:48 +00:00
case 'BR' :
2015-06-08 14:50:21 +00:00
$valid = ( bool ) preg_match ( '/^([0-9]{5})([-])?([0-9]{3})$/' , $postcode );
break ;
2018-03-13 11:37:48 +00:00
case 'CH' :
2015-06-08 14:50:21 +00:00
$valid = ( bool ) preg_match ( '/^([0-9]{4})$/i' , $postcode );
break ;
2018-03-13 11:37:48 +00:00
case 'DE' :
2015-10-08 10:20:57 +00:00
$valid = ( bool ) preg_match ( '/^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$/' , $postcode );
break ;
2018-03-13 11:37:48 +00:00
case 'ES' :
case 'FR' :
2019-04-10 21:44:50 +00:00
case 'IT' :
2016-03-10 12:17:49 +00:00
$valid = ( bool ) preg_match ( '/^([0-9]{5})$/i' , $postcode );
break ;
2018-03-13 11:37:48 +00:00
case 'GB' :
2018-03-13 11:42:53 +00:00
$valid = self :: is_gb_postcode ( $postcode );
2015-06-08 14:50:21 +00:00
break ;
2020-06-04 17:21:41 +00:00
case 'HU' :
$valid = ( bool ) preg_match ( '/^([0-9]{4})$/i' , $postcode );
break ;
2018-05-03 10:26:13 +00:00
case 'IE' :
$valid = ( bool ) preg_match ( '/([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}/' , wc_normalize_postcode ( $postcode ) );
break ;
2018-03-13 11:37:48 +00:00
case 'JP' :
2016-05-10 01:05:09 +00:00
$valid = ( bool ) preg_match ( '/^([0-9]{3})([-])([0-9]{4})$/' , $postcode );
2016-05-09 22:33:47 +00:00
break ;
2018-03-13 11:37:48 +00:00
case 'PT' :
2015-06-08 14:50:21 +00:00
$valid = ( bool ) preg_match ( '/^([0-9]{4})([-])([0-9]{3})$/' , $postcode );
break ;
2018-03-13 11:37:48 +00:00
case 'US' :
2015-06-08 14:50:21 +00:00
$valid = ( bool ) preg_match ( '/^([0-9]{5})(-[0-9]{4})?$/i' , $postcode );
break ;
2018-03-13 11:37:48 +00:00
case 'CA' :
// CA Postal codes cannot contain D,F,I,O,Q,U and cannot start with W or Z. https://en.wikipedia.org/wiki/Postal_codes_in_Canada#Number_of_possible_postal_codes.
2016-02-09 17:39:09 +00:00
$valid = ( bool ) preg_match ( '/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])([\ ])?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/i' , $postcode );
break ;
2016-10-19 13:48:13 +00:00
case 'PL' :
$valid = ( bool ) preg_match ( '/^([0-9]{2})([-])([0-9]{3})$/' , $postcode );
break ;
2017-11-29 17:48:15 +00:00
case 'CZ' :
2017-11-29 17:55:35 +00:00
case 'SK' :
2017-11-30 18:33:16 +00:00
$valid = ( bool ) preg_match ( '/^([0-9]{3})(\s?)([0-9]{2})$/' , $postcode );
2017-11-29 17:48:15 +00:00
break ;
2018-12-30 19:02:59 +00:00
case 'NL' :
2019-06-01 14:52:15 +00:00
$valid = ( bool ) preg_match ( '/^([1-9][0-9]{3})(\s?)(?!SA|SD|SS)[A-Z]{2}$/i' , $postcode );
2018-12-30 19:02:59 +00:00
break ;
2019-12-09 13:09:45 +00:00
case 'SI' :
$valid = ( bool ) preg_match ( '/^([1-9][0-9]{3})$/' , $postcode );
break ;
2018-03-13 11:37:48 +00:00
default :
2015-06-08 14:50:21 +00:00
$valid = true ;
break ;
2012-07-20 11:40:13 +00:00
}
2015-06-08 14:50:21 +00:00
return apply_filters ( 'woocommerce_validate_postcode' , $valid , $postcode , $country );
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
2015-06-08 14:50:21 +00:00
* Check if is a GB postcode .
2012-08-14 19:42:38 +00:00
*
2018-03-13 11:37:48 +00:00
* @ param string $to_check A postcode .
2012-08-14 19:42:38 +00:00
* @ return bool
*/
2018-03-13 11:42:53 +00:00
public static function is_gb_postcode ( $to_check ) {
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
// Permitted letters depend upon their position in the postcode.
2018-03-13 11:37:48 +00:00
// https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation.
$alpha1 = '[abcdefghijklmnoprstuwyz]' ; // Character 1.
$alpha2 = '[abcdefghklmnopqrstuvwxy]' ; // Character 2.
$alpha3 = '[abcdefghjkpstuw]' ; // Character 3 == ABCDEFGHJKPSTUW.
$alpha4 = '[abehmnprvwxy]' ; // Character 4 == ABEHMNPRVWXY.
$alpha5 = '[abdefghjlnpqrstuwxyz]' ; // Character 5 != CIKMOV.
2012-08-14 19:42:38 +00:00
2015-06-08 14:50:21 +00:00
$pcexp = array ();
2014-10-07 10:00:41 +00:00
2018-03-13 11:37:48 +00:00
// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA.
2016-09-01 20:50:14 +00:00
$pcexp [ 0 ] = '/^(' . $alpha1 . '{1}' . $alpha2 . '{0,1}[0-9]{1,2})([0-9]{1}' . $alpha5 . '{2})$/' ;
2012-08-14 19:42:38 +00:00
2018-03-13 11:37:48 +00:00
// Expression for postcodes: ANA NAA.
2016-09-01 20:50:14 +00:00
$pcexp [ 1 ] = '/^(' . $alpha1 . '{1}[0-9]{1}' . $alpha3 . '{1})([0-9]{1}' . $alpha5 . '{2})$/' ;
2012-08-14 19:42:38 +00:00
2018-03-13 11:37:48 +00:00
// Expression for postcodes: AANA NAA.
2016-09-01 20:50:14 +00:00
$pcexp [ 2 ] = '/^(' . $alpha1 . '{1}' . $alpha2 . '[0-9]{1}' . $alpha4 . ')([0-9]{1}' . $alpha5 . '{2})$/' ;
2012-08-14 19:42:38 +00:00
2018-03-13 11:37:48 +00:00
// Exception for the special postcode GIR 0AA.
2016-08-27 06:14:06 +00:00
$pcexp [ 3 ] = '/^(gir)(0aa)$/' ;
2012-08-14 19:42:38 +00:00
2018-03-13 11:37:48 +00:00
// Standard BFPO numbers.
2013-09-04 10:26:19 +00:00
$pcexp [ 4 ] = '/^(bfpo)([0-9]{1,4})$/' ;
2012-08-14 19:42:38 +00:00
2018-03-13 11:37:48 +00:00
// c/o BFPO numbers.
2013-09-04 10:26:19 +00:00
$pcexp [ 5 ] = '/^(bfpo)(c\/o[0-9]{1,3})$/' ;
2012-08-14 19:42:38 +00:00
2018-03-13 11:37:48 +00:00
// Load up the string to check, converting into lowercase and removing spaces.
2015-02-03 16:24:01 +00:00
$postcode = strtolower ( $to_check );
$postcode = str_replace ( ' ' , '' , $postcode );
2012-08-14 19:42:38 +00:00
2018-03-13 11:37:48 +00:00
// Assume we are not going to find a valid postcode.
2011-08-09 15:16:18 +00:00
$valid = false ;
2012-08-14 19:42:38 +00:00
2018-03-13 11:37:48 +00:00
// Check the string against the six types of postcodes.
2013-09-04 10:26:19 +00:00
foreach ( $pcexp as $regexp ) {
if ( preg_match ( $regexp , $postcode , $matches ) ) {
2018-03-13 11:37:48 +00:00
// Remember that we have found that the code is valid and break from loop.
2011-08-09 15:16:18 +00:00
$valid = true ;
break ;
}
}
2013-09-12 13:41:02 +00:00
return $valid ;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
/**
2015-11-03 13:31:20 +00:00
* Format the postcode according to the country and length of the postcode .
2011-08-09 15:16:18 +00:00
*
2018-03-21 03:21:41 +00:00
* @ param string $postcode Postcode to format .
* @ param string $country Country to format the postcode for .
* @ return string Formatted postcode .
2011-08-09 15:16:18 +00:00
*/
2013-09-12 13:41:02 +00:00
public static function format_postcode ( $postcode , $country ) {
2015-04-08 14:28:06 +00:00
return wc_format_postcode ( $postcode , $country );
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
2012-06-10 09:44:13 +00:00
/**
2018-03-13 11:37:48 +00:00
* Format a given phone number .
2012-08-14 19:42:38 +00:00
*
2018-03-21 03:21:41 +00:00
* @ param mixed $tel Phone number to format .
2012-08-14 19:42:38 +00:00
* @ return string
2012-06-10 09:44:13 +00:00
*/
2013-09-12 13:41:02 +00:00
public static function format_phone ( $tel ) {
2015-04-08 14:28:06 +00:00
return wc_format_phone_number ( $tel );
2012-06-10 09:44:13 +00:00
}
2013-10-18 18:56:36 +00:00
}