WC_Correios_Autofill_Addresses

Correios autofill addresses class.

Defined (1)

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

/includes/class-wc-correios-autofill-addresses.php  
  1. class WC_Correios_Autofill_Addresses { 
  2.  
  3. /** 
  4. * Table name. 
  5. * @var string 
  6. */ 
  7. public static $table = 'correios_postcodes'; 
  8.  
  9. /** 
  10. * Ajax endpoint. 
  11. * @var string 
  12. */ 
  13. protected $ajax_endpoint = 'correios_autofill_address'; 
  14.  
  15. /** 
  16. * Addresses webservice URL. 
  17. * @var string 
  18. */ 
  19. private static $_webservice_url = 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl'; 
  20.  
  21. /** 
  22. * Initialize actions. 
  23. */ 
  24. public function __construct() { 
  25. add_action( 'init', array( $this, 'init' ) ); 
  26.  
  27. /** 
  28. * Init autofill. 
  29. */ 
  30. public function init() { 
  31. if ( apply_filters( 'woocommerce_correios_enable_autofill_addresses', false ) ) { 
  32. $this->maybe_install(); 
  33.  
  34. add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); 
  35. add_action( 'wc_ajax_' . $this->ajax_endpoint, array( $this, 'ajax_autofill' ) ); 
  36.  
  37. /** 
  38. * Get the addresses webservice URL. 
  39. * @return string 
  40. */ 
  41. protected static function get_tracking_addresses_webservice_url() { 
  42. return apply_filters( 'woocommerce_correios_addresses_webservice_url', self::$_webservice_url ); 
  43.  
  44. /** 
  45. * Logger. 
  46. * @param string $data Data to log. 
  47. */ 
  48. protected static function logger( $data ) { 
  49. if ( apply_filters( 'woocommerce_correios_enable_autofill_addresses_debug', false ) ) { 
  50. $logger = new WC_Logger(); 
  51. $logger->add( 'correios-autofill-addresses', $data ); 
  52.  
  53. /** 
  54. * Get validity. 
  55. * @return string 
  56. */ 
  57. protected static function get_validity() { 
  58. return apply_filters( 'woocommerce_correios_autofill_addresses_validity', 'forever' ); 
  59.  
  60. /** 
  61. * Get address by postcode. 
  62. * @param string $postcode Address postcode. 
  63. * @return stdClass 
  64. */ 
  65. public static function get_address( $postcode ) { 
  66. global $wpdb; 
  67.  
  68. $postcode = wc_correios_sanitize_postcode( $postcode ); 
  69.  
  70. if ( empty( $postcode ) || 8 !== strlen( $postcode ) ) { 
  71. return null; 
  72.  
  73. $table = $wpdb->prefix . self::$table; 
  74. $address = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE postcode = %s;", $postcode ) ); 
  75.  
  76. if ( is_wp_error( $address ) || is_null( $address ) ) { 
  77. $address = self::fetch_address( $postcode ); 
  78.  
  79. if ( ! is_null( $address ) ) { 
  80. self::save_address( (array) $address ); 
  81. } elseif ( self::check_if_expired( $address->last_query ) ) { 
  82. $_address = self::fetch_address( $postcode ); 
  83.  
  84. if ( ! is_null( $_address ) ) { 
  85. $address = $_address; 
  86. self::update_address( (array) $address ); 
  87.  
  88. return $address; 
  89.  
  90. /** 
  91. * Check if postcode is expired. 
  92. * @param string $last_query Date of the last query. 
  93. * @return bool 
  94. */ 
  95. protected static function check_if_expired( $last_query ) { 
  96. $validity = self::get_validity(); 
  97.  
  98. if ( 'forever' !== $validity && strtotime( '+' . $validity . ' months', strtotime( $last_query ) ) < current_time( 'timestamp' ) ) { 
  99. return true; 
  100.  
  101. return false; 
  102.  
  103. /** 
  104. * Insert an address. 
  105. * @param array $address Address data to save. 
  106. * @return bool 
  107. */ 
  108. protected static function save_address( $address ) { 
  109. global $wpdb; 
  110.  
  111. $default = array( 
  112. 'postcode' => '',  
  113. 'address' => '',  
  114. 'city' => '',  
  115. 'neighborhood' => '',  
  116. 'state' => '',  
  117. 'last_query' => current_time( 'mysql' ),  
  118. ); 
  119.  
  120. $address = wp_parse_args( $address, $default ); 
  121.  
  122. $result = $wpdb->insert( 
  123. $wpdb->prefix . self::$table,  
  124. $address,  
  125. array( '%s', '%s', '%s', '%s', '%s', '%s' ) 
  126. ); 
  127.  
  128. return false !== $result; 
  129.  
  130. /** 
  131. * Delete an address from database. 
  132. * @param string $postcode Address postcode. 
  133. */ 
  134. protected static function delete_address( $postcode ) { 
  135. global $wpdb; 
  136.  
  137. $wpdb->delete( $wpdb->prefix . self::$table, array( 'postcode' => $postcode ), array( '%s' ) ); 
  138.  
  139. /** 
  140. * Update an address. 
  141. * @param array $address Address data. 
  142. * @return bool 
  143. */ 
  144. protected static function update_address( $address ) { 
  145. self::delete_address( $address['postcode'] ); 
  146.  
  147. return self::save_address( $address ); 
  148.  
  149. /** 
  150. * Fetch an address from Correios Webservices. 
  151. * @param string $postcode Address postcode. 
  152. * @return stdClass 
  153. */ 
  154. protected static function fetch_address( $postcode ) { 
  155. include_once dirname( __FILE__ ) . '/class-wc-correios-soap-client.php'; 
  156.  
  157. self::logger( sprintf( 'Fetching address for "%s" on Correios Webservices...', $postcode ) ); 
  158.  
  159. $address = null; 
  160.  
  161. try { 
  162. $soap = new WC_Correios_Soap_Client( self::get_tracking_addresses_webservice_url() ); 
  163. $response = $soap->consultaCEP( array( 'cep' => $postcode ) ); 
  164. $data = $response->return; 
  165. $address = new stdClass; 
  166.  
  167. $address->postcode = $data->cep; 
  168. $address->address = $data->end; 
  169. $address->city = $data->cidade; 
  170. $address->neighborhood = $data->bairro; 
  171. $address->state = $data->uf; 
  172. $address->last_query = current_time( 'mysql' ); 
  173. } catch ( Exception $e ) { 
  174. self::logger( sprintf( 'An error occurred while trying to fetch address for "%s": %s', $postcode, $e->getMessage() ) ); 
  175.  
  176. if ( ! is_null( $address ) ) { 
  177. self::logger( sprintf( 'Address for "%s" found successfully: %s', $postcode, print_r( $address, true ) ) ); 
  178.  
  179. return $address; 
  180.  
  181. /** 
  182. * Frontend scripts. 
  183. */ 
  184. public function frontend_scripts() { 
  185. if ( is_checkout() || is_account_page() ) { 
  186. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  187.  
  188. wp_enqueue_script( 'woocommerce-correios-autofill-addresses', plugins_url( 'assets/js/frontend/autofill-address' . $suffix . '.js', WC_Correios::get_main_file() ), array( 'jquery', 'jquery-blockui' ), WC_Correios::VERSION, true ); 
  189.  
  190. wp_localize_script( 
  191. 'woocommerce-correios-autofill-addresses',  
  192. 'WCCorreiosAutofillAddressParams',  
  193. array( 
  194. 'url' => WC_AJAX::get_endpoint( $this->ajax_endpoint ),  
  195. 'force' => apply_filters( 'woocommerce_correios_autofill_addresses_force_autofill', 'no' ),  
  196. ); 
  197.  
  198. /** 
  199. * Ajax autofill endpoint. 
  200. */ 
  201. public function ajax_autofill() { 
  202. if ( empty( $_GET['postcode'] ) ) { 
  203. wp_send_json_error( array( 'message' => __( 'Missing postcode paramater.', 'woocommerce-correios' ) ) ); 
  204. exit; 
  205.  
  206. $postcode = wc_correios_sanitize_postcode( wp_unslash( $_GET['postcode'] ) ); 
  207.  
  208. if ( empty( $postcode ) || 8 !== strlen( $postcode ) ) { 
  209. wp_send_json_error( array( 'message' => __( 'Invalid postcode.', 'woocommerce-correios' ) ) ); 
  210. exit; 
  211.  
  212. $address = self::get_address( $postcode ); 
  213.  
  214. if ( is_null( $address ) ) { 
  215. wp_send_json_error( array( 'message' => __( 'Invalid postcode.', 'woocommerce-correios' ) ) ); 
  216. exit; 
  217.  
  218. // Unset ID and last_query. 
  219. unset( $address->ID ); 
  220. unset( $address->last_query ); 
  221.  
  222. wp_send_json_success( $address ); 
  223.  
  224. /** 
  225. * Maybe install database. 
  226. */ 
  227. public function maybe_install() { 
  228. $version = get_option( 'woocommerce_correios_autofill_addresses_db_version' ); 
  229.  
  230. if ( empty( $version ) ) { 
  231. self::create_database(); 
  232.  
  233. update_option( 'woocommerce_correios_autofill_addresses_db_version', '1.0.0' ); 
  234.  
  235. /** 
  236. * Create database. 
  237. */ 
  238. public static function create_database() { 
  239. global $wpdb; 
  240.  
  241. $charset_collate = $wpdb->get_charset_collate(); 
  242. $table_name = $wpdb->prefix . self::$table; 
  243.  
  244. $sql = "CREATE TABLE $table_name ( 
  245. ID bigint(20) NOT NULL auto_increment,  
  246. postcode char(8) NOT NULL,  
  247. address longtext NULL,  
  248. city longtext NULL,  
  249. neighborhood longtext NULL,  
  250. state char(2) NULL,  
  251. last_query datetime NULL,  
  252. PRIMARY KEY (ID),  
  253. KEY postcode (postcode) 
  254. ) $charset_collate;"; 
  255.  
  256. require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 
  257.  
  258. dbDelta( $sql );