WCPGSK_Validation

The Woocommerce Poor Guys Swiss Knife WCPGSK Validation class.

Defined (1)

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

/classes/wcpgsk-validation.php  
  1. class WCPGSK_Validation { 
  2.  
  3. /** 
  4. * Validates an email using wordpress native is_email function 
  5. * @param string email address 
  6. * @return bool 
  7. */ 
  8. public function is_email( $email ) { 
  9. return false;//is_email( $email ); 
  10.  
  11.  
  12. /** 
  13. * Validates a phone number using a regular expression 
  14. * @param string phone number 
  15. * @return bool 
  16. */ 
  17. public function is_phone( $phone ) { 
  18. if ( strlen( trim( preg_replace( '/[\s\#0-9_\-\+\(\)]/', '', $phone ) ) ) > 0 ) 
  19. return false; 
  20.  
  21. return true; 
  22.  
  23.  
  24. /** 
  25. * Checks for a valid postcode (UK) 
  26. * @param string postcode 
  27. * @param string country 
  28. * @return bool 
  29. */ 
  30. public function is_postcode( $postcode, $country ) { 
  31. if ( strlen( trim( preg_replace( '/[\s\-A-Za-z0-9]/', '', $postcode ) ) ) > 0 ) 
  32. return false; 
  33.  
  34. switch ( $country ) { 
  35. case "GB" : 
  36. return $this->is_GB_postcode( $postcode ); 
  37. case "US" : 
  38. if ( preg_match( "/^([0-9]{5})(-[0-9]{4})?$/i", $postcode ) ) 
  39. return true; 
  40. else 
  41. return false; 
  42.  
  43. return true; 
  44.  
  45.  
  46. /** 
  47. * is_GB_postcode function. 
  48. * @author John Gardner 
  49. * @access public 
  50. * @param mixed $toCheck A postcode 
  51. * @return bool 
  52. */ 
  53. public function is_GB_postcode( $toCheck ) { 
  54.  
  55. // Permitted letters depend upon their position in the postcode. 
  56. $alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1 
  57. $alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2 
  58. $alpha3 = "[abcdefghjkstuw]"; // Character 3 
  59. $alpha4 = "[abehmnprvwxy]"; // Character 4 
  60. $alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5 
  61.  
  62. // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA 
  63. $pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0, 1}[0-9]{1, 2})([0-9]{1}'.$alpha5.'{2})$'; 
  64.  
  65. // Expression for postcodes: ANA NAA 
  66. $pcexp[1] = '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$'; 
  67.  
  68. // Expression for postcodes: AANA NAA 
  69. $pcexp[2] = '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$'; 
  70.  
  71. // Exception for the special postcode GIR 0AA 
  72. $pcexp[3] = '^(gir)(0aa)$'; 
  73.  
  74. // Standard BFPO numbers 
  75. $pcexp[4] = '^(bfpo)([0-9]{1, 4})$'; 
  76.  
  77. // c/o BFPO numbers 
  78. $pcexp[5] = '^(bfpo)(c\/o[0-9]{1, 3})$'; 
  79.  
  80. // Load up the string to check, converting into lowercase and removing spaces 
  81. $postcode = strtolower($toCheck); 
  82. $postcode = str_replace (' ', '', $postcode); 
  83.  
  84. // Assume we are not going to find a valid postcode 
  85. $valid = false; 
  86.  
  87. // Check the string against the six types of postcodes 
  88. foreach ($pcexp as $regexp) { 
  89.  
  90. if (ereg($regexp, $postcode, $matches)) { 
  91.  
  92. // Load new postcode back into the form element 
  93. $toCheck = strtoupper ($matches[1] . ' ' . $matches [2]); 
  94.  
  95. // Take account of the special BFPO c/o format 
  96. $toCheck = str_replace( 'C/O', 'c/o ', $toCheck ); 
  97.  
  98. // Remember that we have found that the code is valid and break from loop 
  99. $valid = true; 
  100. break; 
  101.  
  102. if ($valid) {return true;} else {return false;}; 
  103.  
  104. /** 
  105. * Format the postcode according to the country and length of the postcode 
  106. * @param string postcode 
  107. * @param string country 
  108. * @return string formatted postcode 
  109. */ 
  110. public function format_postcode( $postcode, $country ) { 
  111. $postcode = strtoupper(trim($postcode)); 
  112. $postcode = trim(preg_replace('/[\s]/', '', $postcode)); 
  113.  
  114. if ( in_array( $country, array('GB', 'CA') ) ) : 
  115. $postcode = trim( substr_replace( $postcode, ' ', -3, 0 ) ); 
  116. endif; 
  117.  
  118. return $postcode; 
  119.  
  120. /** 
  121. * format_phone function. 
  122. * @access public 
  123. * @param mixed $tel 
  124. * @return string 
  125. */ 
  126. public function format_phone( $tel ) { 
  127. $tel = str_replace( '.', '-', $tel ); 
  128. return $tel; 
  129.