class-wc-validation.php 5.0 KB

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