2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Contains Validation functions
|
|
|
|
*
|
2012-01-27 16:38:39 +00:00
|
|
|
* @class WC_Validation
|
2013-09-12 13:41:02 +00:00
|
|
|
* @version 2.1.0
|
2012-08-14 22:43:48 +00:00
|
|
|
* @package WooCommerce/Classes
|
2013-02-20 17:14:46 +00:00
|
|
|
* @category Class
|
2012-08-14 19:42:38 +00:00
|
|
|
* @author WooThemes
|
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
|
|
|
/**
|
|
|
|
* Validates an email using wordpress native is_email function
|
|
|
|
*
|
|
|
|
* @param string email address
|
2012-08-14 19:42:38 +00:00
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Validates a phone number using a regular expression
|
|
|
|
*
|
|
|
|
* @param string phone number
|
2012-08-14 19:42:38 +00:00
|
|
|
* @return bool
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2013-09-12 13:41:02 +00:00
|
|
|
public static function is_phone( $phone ) {
|
2012-08-14 19:42:38 +00:00
|
|
|
if ( strlen( trim( preg_replace( '/[\s\#0-9_\-\+\(\)]/', '', $phone ) ) ) > 0 )
|
2012-07-20 11:40:13 +00:00
|
|
|
return false;
|
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
|
|
|
/**
|
2013-10-18 18:58:51 +00:00
|
|
|
* Checks for a valid postcode
|
2011-08-09 15:16:18 +00:00
|
|
|
*
|
|
|
|
* @param string postcode
|
|
|
|
* @param string country
|
2012-08-14 19:42:38 +00:00
|
|
|
* @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 ) {
|
2012-08-14 19:42:38 +00:00
|
|
|
if ( strlen( trim( preg_replace( '/[\s\-A-Za-z0-9]/', '', $postcode ) ) ) > 0 )
|
2012-07-20 11:40:13 +00:00
|
|
|
return false;
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2012-07-20 11:40:13 +00:00
|
|
|
switch ( $country ) {
|
|
|
|
case "GB" :
|
2013-09-12 13:41:02 +00:00
|
|
|
return self::is_GB_postcode( $postcode );
|
2012-07-20 11:40:13 +00:00
|
|
|
case "US" :
|
|
|
|
if ( preg_match( "/^([0-9]{5})(-[0-9]{4})?$/i", $postcode ) )
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2013-10-18 18:19:24 +00:00
|
|
|
case "CH" :
|
|
|
|
if ( preg_match( "/^([0-9]{4})$/i", $postcode ) )
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2013-10-18 18:56:36 +00:00
|
|
|
case "BR" :
|
2013-10-18 19:06:27 +00:00
|
|
|
if ( preg_match( "/^([0-9]{5,5})([-])?([0-9]{3,3})$/", $postcode ) )
|
2013-10-18 18:56:36 +00:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2012-07-20 11:40:13 +00:00
|
|
|
}
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* is_GB_postcode function.
|
|
|
|
*
|
|
|
|
* @author John Gardner
|
|
|
|
* @access public
|
|
|
|
* @param mixed $toCheck A postcode
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-09-12 13:41:02 +00:00
|
|
|
public static function is_GB_postcode( $toCheck ) {
|
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.
|
2014-05-13 23:55:29 +00:00
|
|
|
// http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation
|
2011-08-09 15:16:18 +00:00
|
|
|
$alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
|
|
|
|
$alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
|
2014-05-13 23:55:29 +00:00
|
|
|
$alpha3 = "[abcdefghjkpstuw]"; // Character 3 == ABCDEFGHJKPSTUW
|
|
|
|
$alpha4 = "[abehmnprvwxy]"; // Character 4 == ABEHMNPRVWXY
|
|
|
|
$alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5 != CIKMOV
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
|
2013-09-04 10:26:19 +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
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
// Expression for postcodes: ANA NAA
|
2013-09-04 10:26:19 +00:00
|
|
|
$pcexp[1] = '/^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$/';
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
// Expression for postcodes: AANA NAA
|
2013-09-04 10:26:19 +00:00
|
|
|
$pcexp[2] = '/^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$/';
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
// Exception for the special postcode GIR 0AA
|
2013-09-04 10:26:19 +00:00
|
|
|
$pcexp[3] = '/^(gir)(0aa)$/';
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-08-09 15:16:18 +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
|
|
|
|
2011-08-09 15:16:18 +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
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
// Load up the string to check, converting into lowercase and removing spaces
|
2013-09-04 10:26:19 +00:00
|
|
|
$postcode = strtolower( $toCheck );
|
2011-08-09 15:16:18 +00:00
|
|
|
$postcode = str_replace (' ', '', $postcode);
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
// Assume we are not going to find a valid postcode
|
|
|
|
$valid = false;
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
// Check the string against the six types of postcodes
|
2013-09-04 10:26:19 +00:00
|
|
|
foreach ( $pcexp as $regexp ) {
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2013-09-04 10:26:19 +00:00
|
|
|
if ( preg_match( $regexp, $postcode, $matches ) ) {
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
// Load new postcode back into the form element
|
2011-08-09 15:16:18 +00:00
|
|
|
$toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
// Take account of the special BFPO c/o format
|
2012-06-15 17:30:00 +00:00
|
|
|
$toCheck = str_replace( 'C/O', 'c/o ', $toCheck );
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
// Remember that we have found that the code is valid and break from loop
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* Format the postcode according to the country and length of the postcode
|
|
|
|
*
|
|
|
|
* @param string postcode
|
|
|
|
* @param string country
|
|
|
|
* @return string formatted postcode
|
|
|
|
*/
|
2013-09-12 13:41:02 +00:00
|
|
|
public static function format_postcode( $postcode, $country ) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
* format_phone function.
|
2012-08-14 19:42:38 +00:00
|
|
|
*
|
2012-06-10 09:44:13 +00:00
|
|
|
* @access public
|
|
|
|
* @param mixed $tel
|
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 ) {
|
|
|
|
wc_format_phone_number( $tel );
|
2012-06-10 09:44:13 +00:00
|
|
|
}
|
2013-10-18 18:56:36 +00:00
|
|
|
}
|