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