Brak opisu

class-wc-validation.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 'BA':
  50. $valid = (bool) preg_match( '/^([7-8]{1})([0-9]{4})$/', $postcode );
  51. break;
  52. case 'BE':
  53. $valid = (bool) preg_match( '/^([0-9]{4})$/i', $postcode );
  54. break;
  55. case 'BR':
  56. $valid = (bool) preg_match( '/^([0-9]{5})([-])?([0-9]{3})$/', $postcode );
  57. break;
  58. case 'CH':
  59. $valid = (bool) preg_match( '/^([0-9]{4})$/i', $postcode );
  60. break;
  61. case 'DE':
  62. $valid = (bool) preg_match( '/^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$/', $postcode );
  63. break;
  64. case 'ES':
  65. case 'FR':
  66. case 'IT':
  67. $valid = (bool) preg_match( '/^([0-9]{5})$/i', $postcode );
  68. break;
  69. case 'GB':
  70. $valid = self::is_gb_postcode( $postcode );
  71. break;
  72. case 'HU':
  73. $valid = (bool) preg_match( '/^([0-9]{4})$/i', $postcode );
  74. break;
  75. case 'IE':
  76. $valid = (bool) preg_match( '/([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}/', wc_normalize_postcode( $postcode ) );
  77. break;
  78. case 'IN':
  79. $valid = (bool) preg_match( '/^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$/', $postcode );
  80. break;
  81. case 'JP':
  82. $valid = (bool) preg_match( '/^([0-9]{3})([-]?)([0-9]{4})$/', $postcode );
  83. break;
  84. case 'PT':
  85. $valid = (bool) preg_match( '/^([0-9]{4})([-])([0-9]{3})$/', $postcode );
  86. break;
  87. case 'PR':
  88. case 'US':
  89. $valid = (bool) preg_match( '/^([0-9]{5})(-[0-9]{4})?$/i', $postcode );
  90. break;
  91. case 'CA':
  92. // 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.
  93. $valid = (bool) preg_match( '/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])([\ ])?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/i', $postcode );
  94. break;
  95. case 'PL':
  96. $valid = (bool) preg_match( '/^([0-9]{2})([-])([0-9]{3})$/', $postcode );
  97. break;
  98. case 'CZ':
  99. case 'SK':
  100. $valid = (bool) preg_match( '/^([0-9]{3})(\s?)([0-9]{2})$/', $postcode );
  101. break;
  102. case 'NL':
  103. $valid = (bool) preg_match( '/^([1-9][0-9]{3})(\s?)(?!SA|SD|SS)[A-Z]{2}$/i', $postcode );
  104. break;
  105. case 'SI':
  106. $valid = (bool) preg_match( '/^([1-9][0-9]{3})$/', $postcode );
  107. break;
  108. case 'LI':
  109. $valid = (bool) preg_match( '/^(94[8-9][0-9])$/', $postcode );
  110. break;
  111. default:
  112. $valid = true;
  113. break;
  114. }
  115. return apply_filters( 'woocommerce_validate_postcode', $valid, $postcode, $country );
  116. }
  117. /**
  118. * Check if is a GB postcode.
  119. *
  120. * @param string $to_check A postcode.
  121. * @return bool
  122. */
  123. public static function is_gb_postcode( $to_check ) {
  124. // Permitted letters depend upon their position in the postcode.
  125. // https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation.
  126. $alpha1 = '[abcdefghijklmnoprstuwyz]'; // Character 1.
  127. $alpha2 = '[abcdefghklmnopqrstuvwxy]'; // Character 2.
  128. $alpha3 = '[abcdefghjkpstuw]'; // Character 3 == ABCDEFGHJKPSTUW.
  129. $alpha4 = '[abehmnprvwxy]'; // Character 4 == ABEHMNPRVWXY.
  130. $alpha5 = '[abdefghjlnpqrstuwxyz]'; // Character 5 != CIKMOV.
  131. $pcexp = array();
  132. // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA.
  133. $pcexp[0] = '/^(' . $alpha1 . '{1}' . $alpha2 . '{0,1}[0-9]{1,2})([0-9]{1}' . $alpha5 . '{2})$/';
  134. // Expression for postcodes: ANA NAA.
  135. $pcexp[1] = '/^(' . $alpha1 . '{1}[0-9]{1}' . $alpha3 . '{1})([0-9]{1}' . $alpha5 . '{2})$/';
  136. // Expression for postcodes: AANA NAA.
  137. $pcexp[2] = '/^(' . $alpha1 . '{1}' . $alpha2 . '[0-9]{1}' . $alpha4 . ')([0-9]{1}' . $alpha5 . '{2})$/';
  138. // Exception for the special postcode GIR 0AA.
  139. $pcexp[3] = '/^(gir)(0aa)$/';
  140. // Standard BFPO numbers.
  141. $pcexp[4] = '/^(bfpo)([0-9]{1,4})$/';
  142. // c/o BFPO numbers.
  143. $pcexp[5] = '/^(bfpo)(c\/o[0-9]{1,3})$/';
  144. // Load up the string to check, converting into lowercase and removing spaces.
  145. $postcode = strtolower( $to_check );
  146. $postcode = str_replace( ' ', '', $postcode );
  147. // Assume we are not going to find a valid postcode.
  148. $valid = false;
  149. // Check the string against the six types of postcodes.
  150. foreach ( $pcexp as $regexp ) {
  151. if ( preg_match( $regexp, $postcode, $matches ) ) {
  152. // Remember that we have found that the code is valid and break from loop.
  153. $valid = true;
  154. break;
  155. }
  156. }
  157. return $valid;
  158. }
  159. /**
  160. * Format the postcode according to the country and length of the postcode.
  161. *
  162. * @param string $postcode Postcode to format.
  163. * @param string $country Country to format the postcode for.
  164. * @return string Formatted postcode.
  165. */
  166. public static function format_postcode( $postcode, $country ) {
  167. return wc_format_postcode( $postcode, $country );
  168. }
  169. /**
  170. * Format a given phone number.
  171. *
  172. * @param mixed $tel Phone number to format.
  173. * @return string
  174. */
  175. public static function format_phone( $tel ) {
  176. return wc_format_phone_number( $tel );
  177. }
  178. }