WC_Boleto_Admin

Boleto Admin.

Defined (1)

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

/includes/class-wc-boleto-admin.php  
  1. class WC_Boleto_Admin { 
  2.  
  3. /** 
  4. * Initialize the admin. 
  5. */ 
  6. public function __construct() { 
  7. // Add metabox. 
  8. add_action( 'add_meta_boxes', array( $this, 'register_metabox' ) ); 
  9.  
  10. // Save Metabox. 
  11. add_action( 'save_post', array( $this, 'save' ) ); 
  12.  
  13. // Update. 
  14. add_action( 'admin_init', array( $this, 'update' ), 5 ); 
  15.  
  16. // Load scripts in gateway settings page. 
  17. add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) ); 
  18.  
  19. /** 
  20. * Register boleto metabox. 
  21. */ 
  22. public function register_metabox() { 
  23. add_meta_box( 
  24. 'woocommerce-boleto',  
  25. __( 'Banking Ticket', 'woocommerce-boleto' ),  
  26. array( $this, 'metabox_content' ),  
  27. 'shop_order',  
  28. 'side',  
  29. 'default' 
  30. ); 
  31.  
  32. /** 
  33. * Banking Ticket metabox content. 
  34. * @param object $post order_shop data. 
  35. * @return string Metabox HTML. 
  36. */ 
  37. public function metabox_content( $post ) { 
  38. // Get order data. 
  39. $order = new WC_Order( $post->ID ); 
  40.  
  41. // Use nonce for verification. 
  42. wp_nonce_field( basename( __FILE__ ), 'wcboleto_metabox_nonce' ); 
  43.  
  44. if ( 'boleto' == $order->payment_method ) { 
  45. $boleto_data = get_post_meta( $post->ID, 'wc_boleto_data', true ); 
  46.  
  47. // Save the ticket data if don't have. 
  48. if ( ! isset( $boleto_data['data_vencimento'] ) ) { 
  49. $settings = get_option( 'woocommerce_boleto_settings', array() ); 
  50. $boleto_time = isset( $settings['boleto_time'] ) ? absint( $settings['boleto_time'] ) : 5; 
  51. $data = array(); 
  52. $data['nosso_numero'] = apply_filters( 'wcboleto_our_number', $order->id ); 
  53. $data['numero_documento'] = apply_filters( 'wcboleto_document_number', $order->id ); 
  54. $data['data_vencimento'] = date( 'd/m/Y', time() + ( $boleto_time * 86400 ) ); 
  55. $data['data_documento'] = date( 'd/m/Y' ); 
  56. $data['data_processamento'] = date( 'd/m/Y' ); 
  57.  
  58. update_post_meta( $post->ID, 'wc_boleto_data', $data ); 
  59.  
  60. $boleto_data['data_vencimento'] = $data['data_vencimento']; 
  61.  
  62. $html = '<p><strong>' . __( 'Expiration date:', 'woocommerce-boleto' ) . '</strong> ' . $boleto_data['data_vencimento'] . '</p>'; 
  63. $html .= '<p><strong>' . __( 'URL:', 'woocommerce-boleto' ) . '</strong> <a target="_blank" href="' . esc_url( wc_boleto_get_boleto_url( $order->order_key ) ) . '">' . __( 'View boleto', 'woocommerce-boleto' ) . '</a></p>'; 
  64.  
  65. $html .= '<p style="border-top: 1px solid #ccc;"></p>'; 
  66.  
  67. $html .= '<label for="wcboleto_expiration_date">' . __( 'Set new expiration data:', 'woocommerce-boleto' ) . '</label><br />'; 
  68. $html .= '<input type="text" id="wcboleto_expiration_date" name="wcboleto_expiration_date" style="width: 100%;" />'; 
  69. $html .= '<span class="description">' . __( 'Configuring a new expiration date the boleto is resent to the client.', 'woocommerce-boleto' ) . '</span>'; 
  70.  
  71. } else { 
  72. $html = '<p>' . __( 'This purchase was not paid with Ticket.', 'woocommerce-boleto' ) . '</p>'; 
  73. $html .= '<style>#woocommerce-boleto.postbox {display: none;}</style>'; 
  74.  
  75. echo $html; 
  76.  
  77. /** 
  78. * Save metabox data. 
  79. * @param int $post_id Current post type ID. 
  80. */ 
  81. public function save( $post_id ) { 
  82. // Verify nonce. 
  83. if ( ! isset( $_POST['wcboleto_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['wcboleto_metabox_nonce'], basename( __FILE__ ) ) ) { 
  84. return $post_id; 
  85.  
  86. // Verify if this is an auto save routine. 
  87. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 
  88. return $post_id; 
  89.  
  90. // Check permissions. 
  91. if ( 'shop_order' == $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) ) { 
  92. return $post_id; 
  93.  
  94. if ( isset( $_POST['wcboleto_expiration_date'] ) && ! empty( $_POST['wcboleto_expiration_date'] ) ) { 
  95. // Gets ticket data. 
  96. $boleto_data = get_post_meta( $post_id, 'wc_boleto_data', true ); 
  97. $boleto_data['data_vencimento'] = sanitize_text_field( $_POST['wcboleto_expiration_date'] ); 
  98.  
  99. // Update ticket data. 
  100. update_post_meta( $post_id, 'wc_boleto_data', $boleto_data ); 
  101.  
  102. // Gets order data. 
  103. $order = new WC_Order( $post_id ); 
  104.  
  105. // Add order note. 
  106. $order->add_order_note( sprintf( __( 'Expiration date updated to: %s', 'woocommerce-boleto' ), $boleto_data['data_vencimento'] ) ); 
  107.  
  108. // Send email notification. 
  109. $this->email_notification( $order, $boleto_data['data_vencimento'] ); 
  110.  
  111. /** 
  112. * New expiration date email notification. 
  113. * @param object $order Order data. 
  114. * @param string $expiration_date Ticket expiration date. 
  115. */ 
  116. protected function email_notification( $order, $expiration_date ) { 
  117. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.1', '>=' ) ) { 
  118. $mailer = WC()->mailer(); 
  119. } else { 
  120. global $woocommerce; 
  121. $mailer = $woocommerce->mailer(); 
  122.  
  123. $subject = sprintf( __( 'New expiration date for the boleto your order %s', 'woocommerce-boleto' ), $order->get_order_number() ); 
  124.  
  125. // Mail headers. 
  126. $headers = array(); 
  127. $headers[] = "Content-Type: text/html\r\n"; 
  128.  
  129. // Body message. 
  130. $main_message = '<p>' . sprintf( __( 'The expiration date of your boleto was updated to: %s', 'woocommerce-boleto' ), '<code>' . $expiration_date . '</code>' ) . '</p>'; 
  131. $main_message .= '<p>' . sprintf( '<a class="button" href="%s" target="_blank">%s</a>', esc_url( wc_boleto_get_boleto_url( $order->order_key ) ), __( 'Pay the Ticket →', 'woocommerce-boleto' ) ) . '</p>'; 
  132.  
  133. // Sets message template. 
  134. $message = $mailer->wrap_message( __( 'New expiration date for your boleto', 'woocommerce-boleto' ), $main_message ); 
  135.  
  136. // Send email. 
  137. $mailer->send( $order->billing_email, $subject, $message, $headers, '' ); 
  138.  
  139. /** 
  140. * Admin scripts. 
  141. * @param string $hook Page slug. 
  142. */ 
  143. public function scripts( $hook ) { 
  144. if ( in_array( $hook, array( 'woocommerce_page_wc-settings', 'woocommerce_page_woocommerce_settings' ) ) && ( isset( $_GET['section'] ) && 'wc_boleto_gateway' == strtolower( $_GET['section'] ) ) ) { 
  145. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  146.  
  147. wp_enqueue_script( 'wc-boleto-admin', plugins_url( 'assets/js/admin' . $suffix . '.js', plugin_dir_path( __FILE__ ) ), array( 'jquery' ), WC_Boleto::VERSION, true ); 
  148.  
  149. /** 
  150. * Performance an update to all options. 
  151. */ 
  152. public function update() { 
  153. $db_version = get_option( 'woocommerce_boleto_db_version' ); 
  154. $version = WC_Boleto::VERSION; 
  155.  
  156. // Update to 1.2.2. 
  157. if ( version_compare( $db_version, '1.2.2', '<' ) ) { 
  158. // Delete boleto page. 
  159. $boleto_post = get_page_by_path( 'boleto' ); 
  160. if ( $boleto_post ) { 
  161. wp_delete_post( $boleto_post->ID, true ); 
  162.  
  163. // Flush urls. 
  164. WC_Boleto::activate(); 
  165.  
  166. // Update the db version. 
  167. if ( $db_version != $version ) { 
  168. update_option( 'woocommerce_boleto_db_version', $version );