WC_Validation

Contains Validation functions.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-validation.php  
  1. class WC_Validation { 
  2.  
  3. /** 
  4. * Validates an email using WordPress native is_email function. 
  5. * @param string $email Email address to validate. 
  6. * @return bool 
  7. */ 
  8. public static function is_email( $email ) { 
  9. return is_email( $email ); 
  10.  
  11. /** 
  12. * Validates a phone number using a regular expression. 
  13. * @param string $phone Phone number to validate. 
  14. * @return bool 
  15. */ 
  16. public static function is_phone( $phone ) { 
  17. if ( 0 < strlen( trim( preg_replace( '/[\s\#0-9_\-\+\/\(\)]/', '', $phone ) ) ) ) { 
  18. return false; 
  19.  
  20. return true; 
  21.  
  22. /** 
  23. * Checks for a valid postcode. 
  24. * @param string $postcode Postcode to validate. 
  25. * @param string $country Country to validate the postcode for. 
  26. * @return bool 
  27. */ 
  28. public static function is_postcode( $postcode, $country ) { 
  29. if ( strlen( trim( preg_replace( '/[\s\-A-Za-z0-9]/', '', $postcode ) ) ) > 0 ) { 
  30. return false; 
  31.  
  32. switch ( $country ) { 
  33. case 'AT' : 
  34. $valid = (bool) preg_match( '/^([0-9]{4})$/', $postcode ); 
  35. break; 
  36. case 'BR' : 
  37. $valid = (bool) preg_match( '/^([0-9]{5})([-])?([0-9]{3})$/', $postcode ); 
  38. break; 
  39. case 'CH' : 
  40. $valid = (bool) preg_match( '/^([0-9]{4})$/i', $postcode ); 
  41. break; 
  42. case 'DE' : 
  43. $valid = (bool) preg_match( '/^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$/', $postcode ); 
  44. break; 
  45. case 'ES' : 
  46. case 'FR' : 
  47. $valid = (bool) preg_match( '/^([0-9]{5})$/i', $postcode ); 
  48. break; 
  49. case 'GB' : 
  50. $valid = self::is_GB_postcode( $postcode ); 
  51. break; 
  52. case 'JP' : 
  53. $valid = (bool) preg_match( '/^([0-9]{3})([-])([0-9]{4})$/', $postcode ); 
  54. break; 
  55. case 'PT' : 
  56. $valid = (bool) preg_match( '/^([0-9]{4})([-])([0-9]{3})$/', $postcode ); 
  57. break; 
  58. case 'US' : 
  59. $valid = (bool) preg_match( '/^([0-9]{5})(-[0-9]{4})?$/i', $postcode ); 
  60. break; 
  61. case 'CA' : 
  62. // 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 
  63. $valid = (bool) preg_match( '/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])([\ ])?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/i', $postcode ); 
  64. break; 
  65. case 'PL': 
  66. $valid = (bool) preg_match( '/^([0-9]{2})([-])([0-9]{3})$/', $postcode ); 
  67. break; 
  68.  
  69. default : 
  70. $valid = true; 
  71. break; 
  72.  
  73. return apply_filters( 'woocommerce_validate_postcode', $valid, $postcode, $country ); 
  74.  
  75. /** 
  76. * Check if is a GB postcode. 
  77. * @author John Gardner 
  78. * @param string $to_check A postcode 
  79. * @return bool 
  80. */ 
  81. public static function is_GB_postcode( $to_check ) { 
  82.  
  83. // Permitted letters depend upon their position in the postcode. 
  84. // https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation 
  85. $alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1 
  86. $alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2 
  87. $alpha3 = "[abcdefghjkpstuw]"; // Character 3 == ABCDEFGHJKPSTUW 
  88. $alpha4 = "[abehmnprvwxy]"; // Character 4 == ABEHMNPRVWXY 
  89. $alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5 != CIKMOV 
  90.  
  91. $pcexp = array(); 
  92.  
  93. // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA 
  94. $pcexp[0] = '/^(' . $alpha1 . '{1}' . $alpha2 . '{0, 1}[0-9]{1, 2})([0-9]{1}' . $alpha5 . '{2})$/'; 
  95.  
  96. // Expression for postcodes: ANA NAA 
  97. $pcexp[1] = '/^(' . $alpha1 . '{1}[0-9]{1}' . $alpha3 . '{1})([0-9]{1}' . $alpha5 . '{2})$/'; 
  98.  
  99. // Expression for postcodes: AANA NAA 
  100. $pcexp[2] = '/^(' . $alpha1 . '{1}' . $alpha2 . '[0-9]{1}' . $alpha4 . ')([0-9]{1}' . $alpha5 . '{2})$/'; 
  101.  
  102. // Exception for the special postcode GIR 0AA 
  103. $pcexp[3] = '/^(gir)(0aa)$/'; 
  104.  
  105. // Standard BFPO numbers 
  106. $pcexp[4] = '/^(bfpo)([0-9]{1, 4})$/'; 
  107.  
  108. // c/o BFPO numbers 
  109. $pcexp[5] = '/^(bfpo)(c\/o[0-9]{1, 3})$/'; 
  110.  
  111. // Load up the string to check, converting into lowercase and removing spaces 
  112. $postcode = strtolower( $to_check ); 
  113. $postcode = str_replace( ' ', '', $postcode ); 
  114.  
  115. // Assume we are not going to find a valid postcode 
  116. $valid = false; 
  117.  
  118. // Check the string against the six types of postcodes 
  119. foreach ( $pcexp as $regexp ) { 
  120. if ( preg_match( $regexp, $postcode, $matches ) ) { 
  121. // Remember that we have found that the code is valid and break from loop 
  122. $valid = true; 
  123. break; 
  124.  
  125. return $valid; 
  126.  
  127. /** 
  128. * Format the postcode according to the country and length of the postcode. 
  129. * @param string $postcode Postcode to format. 
  130. * @param string $country Country to format the postcode for. 
  131. * @return string Formatted postcode. 
  132. */ 
  133. public static function format_postcode( $postcode, $country ) { 
  134. return wc_format_postcode( $postcode, $country ); 
  135.  
  136. /** 
  137. * format_phone function. 
  138. * @param mixed $tel Phone number to format. 
  139. * @return string 
  140. */ 
  141. public static function format_phone( $tel ) { 
  142. return wc_format_phone_number( $tel );