WC_Correios_Tracking_Email

Correios Tracking code email.

Defined (1)

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

/includes/emails/class-wc-correios-tracking-email.php  
  1. class WC_Correios_Tracking_Email extends WC_Email { 
  2.  
  3. /** 
  4. * Initialize tracking template. 
  5. */ 
  6. public function __construct() { 
  7. $this->id = 'correios_tracking'; 
  8. $this->title = __( 'Correios Tracking Code', 'woocommerce-correios' ); 
  9. $this->customer_email = true; 
  10. $this->description = __( 'This email is sent when configured a tracking code within an order.', 'woocommerce-correios' ); 
  11. $this->heading = __( 'Your order has been sent', 'woocommerce-correios' ); 
  12. $this->subject = __( '[{site_title}] Your order {order_number} has been sent by Correios', 'woocommerce-correios' ); 
  13. $this->message = __( 'Hi there. Your recent order on {site_title} has been sent by Correios.', 'woocommerce-correios' ) 
  14. . PHP_EOL . ' ' . PHP_EOL 
  15. . __( 'To track your delivery, use the following the tracking code(s): {tracking_code}', 'woocommerce-correios' ) 
  16. . PHP_EOL . ' ' . PHP_EOL 
  17. . __( 'The delivery service is the responsibility of the Correios, but if you have any questions, please contact us.', 'woocommerce-correios' ); 
  18. $this->tracking_message = $this->get_option( 'tracking_message', $this->message ); 
  19. $this->template_html = 'emails/correios-tracking-code.php'; 
  20. $this->template_plain = 'emails/plain/correios-tracking-code.php'; 
  21.  
  22. // Call parent constructor. 
  23. parent::__construct(); 
  24.  
  25. $this->template_base = WC_Correios::get_templates_path(); 
  26.  
  27. /** 
  28. * Initialise settings form fields. 
  29. */ 
  30. public function init_form_fields() { 
  31. $this->form_fields = array( 
  32. 'enabled' => array( 
  33. 'title' => __( 'Enable/Disable', 'woocommerce-correios' ),  
  34. 'type' => 'checkbox',  
  35. 'label' => __( 'Enable this email notification', 'woocommerce-correios' ),  
  36. 'default' => 'yes',  
  37. ),  
  38. 'subject' => array( 
  39. 'title' => __( 'Subject', 'woocommerce-correios' ),  
  40. 'type' => 'text',  
  41. 'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', 'woocommerce-correios' ), $this->subject ),  
  42. 'placeholder' => $this->subject,  
  43. 'default' => '',  
  44. 'desc_tip' => true,  
  45. ),  
  46. 'heading' => array( 
  47. 'title' => __( 'Email Heading', 'woocommerce-correios' ),  
  48. 'type' => 'text',  
  49. 'description' => sprintf( __( 'This controls the main heading contained within the email. Leave blank to use the default heading: <code>%s</code>.', 'woocommerce-correios' ), $this->heading ),  
  50. 'placeholder' => $this->heading,  
  51. 'default' => '',  
  52. 'desc_tip' => true,  
  53. ),  
  54. 'tracking_message' => array( 
  55. 'title' => __( 'Email Content', 'woocommerce-correios' ),  
  56. 'type' => 'textarea',  
  57. 'description' => sprintf( __( 'This controls the initial content of the email. Leave blank to use the default content: <code>%s</code>.', 'woocommerce-correios' ), $this->message ),  
  58. 'placeholder' => $this->message,  
  59. 'default' => '',  
  60. 'desc_tip' => true,  
  61. ),  
  62. 'email_type' => array( 
  63. 'title' => __( 'Email type', 'woocommerce-correios' ),  
  64. 'type' => 'select',  
  65. 'description' => __( 'Choose which format of email to send.', 'woocommerce-correios' ),  
  66. 'default' => 'html',  
  67. 'class' => 'email_type wc-enhanced-select',  
  68. 'options' => $this->get_custom_email_type_options(),  
  69. 'desc_tip' => true,  
  70. ),  
  71. ); 
  72.  
  73. /** 
  74. * Email type options. 
  75. * @return array 
  76. */ 
  77. protected function get_custom_email_type_options() { 
  78. if ( method_exists( $this, 'get_email_type_options' ) ) { 
  79. return $this->get_email_type_options(); 
  80.  
  81. $types = array( 'plain' => __( 'Plain text', 'woocommerce-correios' ) ); 
  82.  
  83. if ( class_exists( 'DOMDocument' ) ) { 
  84. $types['html'] = __( 'HTML', 'woocommerce-correios' ); 
  85. $types['multipart'] = __( 'Multipart', 'woocommerce-correios' ); 
  86.  
  87. return $types; 
  88.  
  89. /** 
  90. * Get email tracking message. 
  91. * @return string 
  92. */ 
  93. public function get_tracking_message() { 
  94. return apply_filters( 'woocommerce_correios_email_tracking_message', $this->format_string( $this->tracking_message ), $this->object ); 
  95.  
  96. /** 
  97. * Get tracking code url. 
  98. * @param string $tracking_code Tracking code. 
  99. * @return string 
  100. */ 
  101. public function get_tracking_code_url( $tracking_code ) { 
  102. $url = sprintf( '<a href="%s#wc-correios-tracking">%s</a>', $this->object->get_view_order_url(), $tracking_code ); 
  103.  
  104. return apply_filters( 'woocommerce_correios_email_tracking_core_url', $url, $tracking_code, $this->object ); 
  105.  
  106. /** 
  107. * Get tracking codes HTML. 
  108. * @param array $tracking_codes Tracking codes. 
  109. * @return string 
  110. */ 
  111. public function get_tracking_codes( $tracking_codes ) { 
  112. $html = '<ul>'; 
  113.  
  114. foreach ( $tracking_codes as $tracking_code ) { 
  115. $html .= '<li>' . $this->get_tracking_code_url( $tracking_code ) . '</li>'; 
  116.  
  117. $html .= '</ul>'; 
  118.  
  119. return $html; 
  120.  
  121. /** 
  122. * Trigger email. 
  123. * @param int $order_id Order ID. 
  124. * @param WC_Order $order Order data. 
  125. * @param string $tracking_code Tracking code. 
  126. */ 
  127. public function trigger( $order_id, $order = false, $tracking_code = '' ) { 
  128. // Get the order object while resending emails. 
  129. if ( $order_id && ! is_a( $order, 'WC_Order' ) ) { 
  130. $order = wc_get_order( $order_id ); 
  131.  
  132. if ( is_object( $order ) ) { 
  133. $this->object = $order; 
  134.  
  135. if ( method_exists( $order, 'get_billing_email' ) ) { 
  136. $this->recipient = $order->get_billing_email(); 
  137. } else { 
  138. $this->recipient = $order->billing_email; 
  139.  
  140. $this->find[] = '{order_number}'; 
  141. $this->replace[] = $order->get_order_number(); 
  142.  
  143. $this->find[] = '{date}'; 
  144. $this->replace[] = date_i18n( wc_date_format(), time() ); 
  145.  
  146. if ( empty( $tracking_code ) ) { 
  147. $tracking_codes = wc_correios_get_tracking_codes( $order ); 
  148. } else { 
  149. $tracking_codes = array( $tracking_code ); 
  150.  
  151. $this->find[] = '{tracking_code}'; 
  152. $this->replace[] = $this->get_tracking_codes( $tracking_codes ); 
  153.  
  154. if ( ! $this->get_recipient() ) { 
  155. return; 
  156.  
  157. $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); 
  158.  
  159. /** 
  160. * Get content HTML. 
  161. * @return string 
  162. */ 
  163. public function get_content_html() { 
  164. ob_start(); 
  165.  
  166. wc_get_template( $this->template_html, array( 
  167. 'order' => $this->object,  
  168. 'email_heading' => $this->get_heading(),  
  169. 'tracking_message' => $this->get_tracking_message(),  
  170. 'sent_to_admin' => false,  
  171. 'plain_text' => false,  
  172. 'email' => $this,  
  173. ), '', $this->template_base ); 
  174.  
  175. return ob_get_clean(); 
  176.  
  177. /** 
  178. * Get content plain text. 
  179. * @return string 
  180. */ 
  181. public function get_content_plain() { 
  182. ob_start(); 
  183.  
  184. // Format list. 
  185. $message = $this->get_tracking_message(); 
  186. $message = str_replace( '<ul>', "\n", $message ); 
  187. $message = str_replace( '<li>', "\n - ", $message ); 
  188. $message = str_replace( array( '</ul>', '</li>' ), '', $message ); 
  189.  
  190. wc_get_template( $this->template_plain, array( 
  191. 'order' => $this->object,  
  192. 'email_heading' => $this->get_heading(),  
  193. 'tracking_message' => $message,  
  194. 'sent_to_admin' => false,  
  195. 'plain_text' => true,  
  196. 'email' => $this,  
  197. ), '', $this->template_base ); 
  198.  
  199. return ob_get_clean();