WC_Correios_Integration

Correios integration class.

Defined (1)

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

/includes/integrations/class-wc-correios-integration.php  
  1. class WC_Correios_Integration extends WC_Integration { 
  2.  
  3. /** 
  4. * Initialize integration actions. 
  5. */ 
  6. public function __construct() { 
  7. $this->id = 'correios-integration'; 
  8. $this->method_title = __( 'Correios', 'woocommerce-correios' ); 
  9.  
  10. // Load the form fields. 
  11. $this->init_form_fields(); 
  12.  
  13. // Load the settings. 
  14. $this->init_settings(); 
  15.  
  16. // Define user set variables. 
  17. $this->tracking_enable = $this->get_option( 'tracking_enable' ); 
  18. $this->tracking_debug = $this->get_option( 'tracking_debug' ); 
  19. $this->autofill_enable = $this->get_option( 'autofill_enable' ); 
  20. $this->autofill_validity = $this->get_option( 'autofill_validity' ); 
  21. $this->autofill_force = $this->get_option( 'autofill_force' ); 
  22. $this->autofill_empty_database = $this->get_option( 'autofill_empty_database' ); 
  23. $this->autofill_debug = $this->get_option( 'autofill_debug' ); 
  24.  
  25. // Actions. 
  26. add_action( 'woocommerce_update_options_integration_' . $this->id, array( $this, 'process_admin_options' ) ); 
  27.  
  28. // Tracking history actions. 
  29. add_filter( 'woocommerce_correios_enable_tracking_history', array( $this, 'setup_tracking_history' ), 10 ); 
  30. add_filter( 'woocommerce_correios_enable_tracking_debug', array( $this, 'setup_tracking_debug' ), 10 ); 
  31.  
  32. // Autofill address actions. 
  33. add_filter( 'woocommerce_correios_enable_autofill_addresses', array( $this, 'setup_autofill_addresses' ), 10 ); 
  34. add_filter( 'woocommerce_correios_enable_autofill_addresses_debug', array( $this, 'setup_autofill_addresses_debug' ), 10 ); 
  35. add_filter( 'woocommerce_correios_autofill_addresses_validity_time', array( $this, 'setup_autofill_addresses_validity_time' ), 10 ); 
  36. add_filter( 'woocommerce_correios_autofill_addresses_force_autofill', array( $this, 'setup_autofill_addresses_force_autofill' ), 10 ); 
  37. add_action( 'wp_ajax_correios_autofill_addresses_empty_database', array( $this, 'ajax_empty_database' ) ); 
  38.  
  39. /** 
  40. * Get tracking log url. 
  41. * @return string 
  42. */ 
  43. protected function get_tracking_log_link() { 
  44. return ' <a href="' . esc_url( admin_url( 'admin.php?page=wc-status&tab=logs&log_file=correios-tracking-history-' . sanitize_file_name( wp_hash( 'correios-tracking-history' ) ) . '.log' ) ) . '">' . __( 'View logs.', 'woocommerce-correios' ) . '</a>'; 
  45.  
  46. /** 
  47. * Initialize integration settings fields. 
  48. */ 
  49. public function init_form_fields() { 
  50. $this->form_fields = array( 
  51. 'tracking' => array( 
  52. 'title' => __( 'Tracking History Table', 'woocommerce-correios' ),  
  53. 'type' => 'title',  
  54. 'description' => __( 'Displays a table with informations about the shipping in My Account > View Order page.', 'woocommerce-correios' ),  
  55. ),  
  56. 'tracking_enable' => array( 
  57. 'title' => __( 'Enable/Disable', 'woocommerce-correios' ),  
  58. 'type' => 'checkbox',  
  59. 'label' => __( 'Enable Tracking History Table', 'woocommerce-correios' ),  
  60. 'default' => 'no',  
  61. ),  
  62. 'tracking_debug' => array( 
  63. 'title' => __( 'Debug Log', 'woocommerce-correios' ),  
  64. 'type' => 'checkbox',  
  65. 'label' => __( 'Enable logging for Tracking History', 'woocommerce-correios' ),  
  66. 'default' => 'no',  
  67. 'description' => sprintf( __( 'Log %s events, such as WebServices requests.', 'woocommerce-correios' ), __( 'Tracking History Table', 'woocommerce-correios' ) ) . $this->get_tracking_log_link(),  
  68. ),  
  69. 'autofill_addresses' => array( 
  70. 'title' => __( 'Autofill Addresses', 'woocommerce-correios' ),  
  71. 'type' => 'title',  
  72. 'description' => __( 'Displays a table with informations about the shipping in My Account > View Order page.', 'woocommerce-correios' ),  
  73. ),  
  74. 'autofill_enable' => array( 
  75. 'title' => __( 'Enable/Disable', 'woocommerce-correios' ),  
  76. 'type' => 'checkbox',  
  77. 'label' => __( 'Enable Autofill Addresses', 'woocommerce-correios' ),  
  78. 'default' => 'no',  
  79. ),  
  80. 'autofill_validity' => array( 
  81. 'title' => __( 'Postcodes Validity', 'woocommerce-correios' ),  
  82. 'type' => 'select',  
  83. 'default' => 'forever',  
  84. 'class' => 'wc-enhanced-select',  
  85. 'description' => __( 'Defines how long a postcode will stay saved in the database before a new query.', 'woocommerce-correios' ),  
  86. 'options' => array( 
  87. '1' => __( '1 month', 'woocommerce-correios' ),  
  88. '2' => sprintf( __( '%d month', 'woocommerce-correios' ), 2 ),  
  89. '3' => sprintf( __( '%d month', 'woocommerce-correios' ), 3 ),  
  90. '4' => sprintf( __( '%d month', 'woocommerce-correios' ), 4 ),  
  91. '5' => sprintf( __( '%d month', 'woocommerce-correios' ), 5 ),  
  92. '6' => sprintf( __( '%d month', 'woocommerce-correios' ), 6 ),  
  93. '7' => sprintf( __( '%d month', 'woocommerce-correios' ), 7 ),  
  94. '8' => sprintf( __( '%d month', 'woocommerce-correios' ), 8 ),  
  95. '9' => sprintf( __( '%d month', 'woocommerce-correios' ), 9 ),  
  96. '10' => sprintf( __( '%d month', 'woocommerce-correios' ), 10 ),  
  97. '11' => sprintf( __( '%d month', 'woocommerce-correios' ), 11 ),  
  98. '12' => sprintf( __( '%d month', 'woocommerce-correios' ), 12 ),  
  99. 'forever' => __( 'Forever', 'woocommerce-correios' ),  
  100. ),  
  101. ),  
  102. 'autofill_force' => array( 
  103. 'title' => __( 'Force Autofill', 'woocommerce-correios' ),  
  104. 'type' => 'checkbox',  
  105. 'label' => __( 'Enable Force Autofill', 'woocommerce-correios' ),  
  106. 'description' => __( 'When enabled will autofill all addresses after the user finish to fill the postcode, even if the addresses are already filled.', 'woocommerce-correios' ),  
  107. 'default' => 'no',  
  108. ),  
  109. 'autofill_empty_database' => array( 
  110. 'title' => __( 'Empty Database', 'woocommerce-correios' ),  
  111. 'type' => 'button',  
  112. 'label' => __( 'Empty Database', 'woocommerce-correios' ),  
  113. 'description' => __( 'Delete all the saved postcodes in the database, use this option if you have issues with outdated postcodes.', 'woocommerce-correios' ),  
  114. ),  
  115. 'autofill_debug' => array( 
  116. 'title' => __( 'Debug Log', 'woocommerce-correios' ),  
  117. 'type' => 'checkbox',  
  118. 'label' => __( 'Enable logging for Autofill Addresses', 'woocommerce-correios' ),  
  119. 'default' => 'no',  
  120. 'description' => sprintf( __( 'Log %s events, such as WebServices requests.', 'woocommerce-correios' ), __( 'Autofill Addresses', 'woocommerce-correios' ) ) . $this->get_tracking_log_link(),  
  121. ),  
  122. ); 
  123.  
  124. /** 
  125. * Correios options page. 
  126. */ 
  127. public function admin_options() { 
  128. echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>'; 
  129. echo wp_kses_post( wpautop( $this->get_method_description() ) ); 
  130.  
  131. include WC_Correios::get_plugin_path() . 'includes/admin/views/html-admin-help-message.php'; 
  132.  
  133. if ( class_exists( 'SoapClient' ) ) { 
  134. echo '<div><input type="hidden" name="section" value="' . esc_attr( $this->id ) . '" /></div>'; 
  135. echo '<table class="form-table">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>'; 
  136. } else { 
  137. $GLOBALS['hide_save_button'] = true; // Hide save button. 
  138. echo '<div class="notice notice-error inline"><p>' . sprintf( __( 'It\'s required have installed the %s on your server in order to integrate with the services of the Correios!', 'woocommerce-correios' ), '<a href="https://secure.php.net/manual/book.soap.php" target="_blank">' . __( 'SOAP module', 'woocommerce-correios' ) . '</a>' ) . '</p></div>'; 
  139.  
  140. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  141. wp_enqueue_script( $this->id . '-admin', plugins_url( 'assets/js/admin/integration' . $suffix . '.js', WC_Correios::get_main_file() ), array( 'jquery', 'jquery-blockui' ), WC_Correios::VERSION, true ); 
  142. wp_localize_script( 
  143. $this->id . '-admin',  
  144. 'WCCorreiosIntegrationAdminParams',  
  145. array( 
  146. 'i18n_confirm_message' => __( 'Are you sure you want to delete all postcodes from the database?', 'woocommerce-correios' ),  
  147. 'empty_database_nonce' => wp_create_nonce( 'woocommerce_correios_autofill_addresses_nonce' ),  
  148. ); 
  149.  
  150. /** 
  151. * Generate Button Input HTML. 
  152. * @param string $key 
  153. * @param array $data 
  154. * @return string 
  155. */ 
  156. public function generate_button_html( $key, $data ) { 
  157. $field_key = $this->get_field_key( $key ); 
  158. $defaults = array( 
  159. 'title' => '',  
  160. 'label' => '',  
  161. 'desc_tip' => false,  
  162. 'description' => '',  
  163. ); 
  164.  
  165. $data = wp_parse_args( $data, $defaults ); 
  166.  
  167. ob_start(); 
  168. ?> 
  169. <tr valign="top"> 
  170. <th scope="row" class="titledesc"> 
  171. <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  172. <?php echo $this->get_tooltip_html( $data ); ?> 
  173. </th> 
  174. <td class="forminp"> 
  175. <fieldset> 
  176. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  177. <button class="button-secondary" type="button" id="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['label'] ); ?></button> 
  178. <?php echo $this->get_description_html( $data ); ?> 
  179. </fieldset> 
  180. </td> 
  181. </tr> 
  182. <?php 
  183.  
  184. return ob_get_clean(); 
  185.  
  186. /** 
  187. * Enable tracking history. 
  188. * @return bool 
  189. */ 
  190. public function setup_tracking_history() { 
  191. return 'yes' === $this->tracking_enable && class_exists( 'SoapClient' ); 
  192.  
  193. /** 
  194. * Set up tracking debug. 
  195. * @return bool 
  196. */ 
  197. public function setup_tracking_debug() { 
  198. return 'yes' === $this->tracking_debug; 
  199.  
  200. /** 
  201. * Enable autofill addresses. 
  202. * @return bool 
  203. */ 
  204. public function setup_autofill_addresses() { 
  205. return 'yes' === $this->autofill_enable && class_exists( 'SoapClient' ); 
  206.  
  207. /** 
  208. * Set up autofill addresses debug. 
  209. * @return bool 
  210. */ 
  211. public function setup_autofill_addresses_debug() { 
  212. return 'yes' === $this->autofill_debug; 
  213.  
  214. /** 
  215. * Set up autofill addresses validity time. 
  216. * @return string 
  217. */ 
  218. public function setup_autofill_addresses_validity_time() { 
  219. return $this->autofill_validity; 
  220.  
  221. /** 
  222. * Set up autofill addresses force autofill. 
  223. * @return string 
  224. */ 
  225. public function setup_autofill_addresses_force_autofill() { 
  226. return $this->autofill_force; 
  227.  
  228. /** 
  229. * Ajax empty database. 
  230. */ 
  231. public function ajax_empty_database() { 
  232. global $wpdb; 
  233.  
  234. if ( ! isset( $_POST['nonce'] ) ) { 
  235. wp_send_json_error( array( 'message' => __( 'Missing parameters!', 'woocommerce-correios' ) ) ); 
  236. exit; 
  237.  
  238. if ( ! wp_verify_nonce( $_POST['nonce'], 'woocommerce_correios_autofill_addresses_nonce' ) ) { 
  239. wp_send_json_error( array( 'message' => __( 'Invalid nonce!', 'woocommerce-correios' ) ) ); 
  240. exit; 
  241.  
  242. $table_name = $wpdb->prefix . WC_Correios_Autofill_Addresses::$table; 
  243. $wpdb->query( "DROP TABLE IF EXISTS $table_name;" ); 
  244.  
  245. WC_Correios_Autofill_Addresses::create_database(); 
  246.  
  247. wp_send_json_success( array( 'message' => __( 'Postcode database emptied successfully!', 'woocommerce-correios' ) ) );