/includes/class-wc-emails.php

  1. <?php 
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) { 
  4. exit; // Exit if accessed directly 
  5.  
  6. /** 
  7. * Transactional Emails Controller 
  8. * 
  9. * WooCommerce Emails Class which handles the sending on transactional emails and email templates. This class loads in available emails. 
  10. * 
  11. * @class WC_Emails 
  12. * @version 2.3.0 
  13. * @package WooCommerce/Classes/Emails 
  14. * @category Class 
  15. * @author WooThemes 
  16. */ 
  17. class WC_Emails { 
  18.  
  19. /** @var array Array of email notification classes */ 
  20. public $emails; 
  21.  
  22. /** @var WC_Emails The single instance of the class */ 
  23. protected static $_instance = null; 
  24.  
  25. /** 
  26. * Background emailer class. 
  27. */ 
  28. protected static $background_emailer; 
  29.  
  30. /** 
  31. * Main WC_Emails Instance. 
  32. * 
  33. * Ensures only one instance of WC_Emails is loaded or can be loaded. 
  34. * 
  35. * @since 2.1 
  36. * @static 
  37. * @return WC_Emails Main instance 
  38. */ 
  39. public static function instance() { 
  40. if ( is_null( self::$_instance ) ) { 
  41. self::$_instance = new self(); 
  42. return self::$_instance; 
  43.  
  44. /** 
  45. * Cloning is forbidden. 
  46. * 
  47. * @since 2.1 
  48. */ 
  49. public function __clone() { 
  50. wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); 
  51.  
  52. /** 
  53. * Unserializing instances of this class is forbidden. 
  54. * 
  55. * @since 2.1 
  56. */ 
  57. public function __wakeup() { 
  58. wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); 
  59.  
  60. /** 
  61. * Hook in all transactional emails. 
  62. */ 
  63. public static function init_transactional_emails() { 
  64. $email_actions = apply_filters( 'woocommerce_email_actions', array( 
  65. 'woocommerce_low_stock',  
  66. 'woocommerce_no_stock',  
  67. 'woocommerce_product_on_backorder',  
  68. 'woocommerce_order_status_pending_to_processing',  
  69. 'woocommerce_order_status_pending_to_completed',  
  70. 'woocommerce_order_status_pending_to_cancelled',  
  71. 'woocommerce_order_status_pending_to_failed',  
  72. 'woocommerce_order_status_pending_to_on-hold',  
  73. 'woocommerce_order_status_failed_to_processing',  
  74. 'woocommerce_order_status_failed_to_completed',  
  75. 'woocommerce_order_status_failed_to_on-hold',  
  76. 'woocommerce_order_status_on-hold_to_processing',  
  77. 'woocommerce_order_status_on-hold_to_cancelled',  
  78. 'woocommerce_order_status_on-hold_to_failed',  
  79. 'woocommerce_order_status_completed',  
  80. 'woocommerce_order_fully_refunded',  
  81. 'woocommerce_order_partially_refunded',  
  82. 'woocommerce_new_customer_note',  
  83. 'woocommerce_created_customer',  
  84. ) ); 
  85.  
  86. if ( apply_filters( 'woocommerce_defer_transactional_emails', false ) ) { 
  87. self::$background_emailer = new WC_Background_Emailer(); 
  88.  
  89. foreach ( $email_actions as $action ) { 
  90. add_action( $action, array( __CLASS__, 'queue_transactional_email' ), 10, 10 ); 
  91. } else { 
  92. foreach ( $email_actions as $action ) { 
  93. add_action( $action, array( __CLASS__, 'send_transactional_email' ), 10, 10 ); 
  94.  
  95. /** 
  96. * Queues transactional email so it's not sent in current request if enabled,  
  97. * otherwise falls back to send now. 
  98. */ 
  99. public static function queue_transactional_email() { 
  100. if ( is_a( self::$background_emailer, 'WC_Background_Emailer' ) ) { 
  101. self::$background_emailer->push_to_queue( array( 
  102. 'filter' => current_filter(),  
  103. 'args' => func_get_args(),  
  104. ) ); 
  105. } else { 
  106. call_user_func_array( array( __CLASS__, 'send_transactional_email' ), func_get_args() ); 
  107.  
  108. /** 
  109. * Init the mailer instance and call the notifications for the current filter. 
  110. * 
  111. * @internal 
  112. * 
  113. * @param string $filter Filter name. 
  114. * @param array $args Email args (default: []). 
  115. */ 
  116. public static function send_queued_transactional_email( $filter = '', $args = array() ) { 
  117. if ( apply_filters( 'woocommerce_allow_send_queued_transactional_email', true, $filter, $args ) ) { 
  118. self::instance(); // Init self so emails exist. 
  119.  
  120. // Ensure gateways are loaded in case they need to insert data into the emails. 
  121. WC()->payment_gateways(); 
  122. WC()->shipping(); 
  123.  
  124. do_action_ref_array( $filter . '_notification', $args ); 
  125.  
  126. /** 
  127. * Init the mailer instance and call the notifications for the current filter. 
  128. * 
  129. * @internal 
  130. * 
  131. * @param array $args Email args (default: []). 
  132. */ 
  133. public static function send_transactional_email( $args = array() ) { 
  134. try { 
  135. $args = func_get_args(); 
  136. self::instance(); // Init self so emails exist. 
  137. do_action_ref_array( current_filter() . '_notification', $args ); 
  138. } catch ( Exception $e ) { 
  139. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  140. trigger_error( 'Transactional email triggered fatal error for callback ' . current_filter(), E_USER_WARNING ); 
  141.  
  142. /** 
  143. * Constructor for the email class hooks in all emails that can be sent. 
  144. * 
  145. */ 
  146. public function __construct() { 
  147. $this->init(); 
  148.  
  149. // Email Header, Footer and content hooks 
  150. add_action( 'woocommerce_email_header', array( $this, 'email_header' ) ); 
  151. add_action( 'woocommerce_email_footer', array( $this, 'email_footer' ) ); 
  152. add_action( 'woocommerce_email_order_details', array( $this, 'order_details' ), 10, 4 ); 
  153. add_action( 'woocommerce_email_order_meta', array( $this, 'order_meta' ), 10, 3 ); 
  154. add_action( 'woocommerce_email_customer_details', array( $this, 'customer_details' ), 10, 3 ); 
  155. add_action( 'woocommerce_email_customer_details', array( $this, 'email_addresses' ), 20, 3 ); 
  156.  
  157. // Hooks for sending emails during store events 
  158. add_action( 'woocommerce_low_stock_notification', array( $this, 'low_stock' ) ); 
  159. add_action( 'woocommerce_no_stock_notification', array( $this, 'no_stock' ) ); 
  160. add_action( 'woocommerce_product_on_backorder_notification', array( $this, 'backorder' ) ); 
  161. add_action( 'woocommerce_created_customer_notification', array( $this, 'customer_new_account' ), 10, 3 ); 
  162.  
  163. // Let 3rd parties unhook the above via this hook 
  164. do_action( 'woocommerce_email', $this ); 
  165.  
  166. /** 
  167. * Init email classes. 
  168. */ 
  169. public function init() { 
  170. // Include email classes 
  171. include_once( dirname( __FILE__ ) . '/emails/class-wc-email.php' ); 
  172.  
  173. $this->emails['WC_Email_New_Order'] = include( 'emails/class-wc-email-new-order.php' ); 
  174. $this->emails['WC_Email_Cancelled_Order'] = include( 'emails/class-wc-email-cancelled-order.php' ); 
  175. $this->emails['WC_Email_Failed_Order'] = include( 'emails/class-wc-email-failed-order.php' ); 
  176. $this->emails['WC_Email_Customer_On_Hold_Order'] = include( 'emails/class-wc-email-customer-on-hold-order.php' ); 
  177. $this->emails['WC_Email_Customer_Processing_Order'] = include( 'emails/class-wc-email-customer-processing-order.php' ); 
  178. $this->emails['WC_Email_Customer_Completed_Order'] = include( 'emails/class-wc-email-customer-completed-order.php' ); 
  179. $this->emails['WC_Email_Customer_Refunded_Order'] = include( 'emails/class-wc-email-customer-refunded-order.php' ); 
  180. $this->emails['WC_Email_Customer_Invoice'] = include( 'emails/class-wc-email-customer-invoice.php' ); 
  181. $this->emails['WC_Email_Customer_Note'] = include( 'emails/class-wc-email-customer-note.php' ); 
  182. $this->emails['WC_Email_Customer_Reset_Password'] = include( 'emails/class-wc-email-customer-reset-password.php' ); 
  183. $this->emails['WC_Email_Customer_New_Account'] = include( 'emails/class-wc-email-customer-new-account.php' ); 
  184.  
  185. $this->emails = apply_filters( 'woocommerce_email_classes', $this->emails ); 
  186.  
  187. // include css inliner 
  188. if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) { 
  189. include_once( dirname( __FILE__ ) . '/libraries/class-emogrifier.php' ); 
  190.  
  191. /** 
  192. * Return the email classes - used in admin to load settings. 
  193. * 
  194. * @return array 
  195. */ 
  196. public function get_emails() { 
  197. return $this->emails; 
  198.  
  199. /** 
  200. * Get from name for email. 
  201. * 
  202. * @return string 
  203. */ 
  204. public function get_from_name() { 
  205. return wp_specialchars_decode( get_option( 'woocommerce_email_from_name' ), ENT_QUOTES ); 
  206.  
  207. /** 
  208. * Get from email address. 
  209. * 
  210. * @return string 
  211. */ 
  212. public function get_from_address() { 
  213. return sanitize_email( get_option( 'woocommerce_email_from_address' ) ); 
  214.  
  215. /** 
  216. * Get the email header. 
  217. * 
  218. * @param mixed $email_heading heading for the email 
  219. */ 
  220. public function email_header( $email_heading ) { 
  221. wc_get_template( 'emails/email-header.php', array( 'email_heading' => $email_heading ) ); 
  222.  
  223. /** 
  224. * Get the email footer. 
  225. */ 
  226. public function email_footer() { 
  227. wc_get_template( 'emails/email-footer.php' ); 
  228.  
  229. /** 
  230. * Wraps a message in the woocommerce mail template. 
  231. * 
  232. * @param mixed $email_heading 
  233. * @param string $message 
  234. * @return string 
  235. */ 
  236. public function wrap_message( $email_heading, $message, $plain_text = false ) { 
  237. // Buffer 
  238. ob_start(); 
  239.  
  240. do_action( 'woocommerce_email_header', $email_heading ); 
  241.  
  242. echo wpautop( wptexturize( $message ) ); 
  243.  
  244. do_action( 'woocommerce_email_footer' ); 
  245.  
  246. // Get contents 
  247. $message = ob_get_clean(); 
  248.  
  249. return $message; 
  250.  
  251. /** 
  252. * Send the email. 
  253. * 
  254. * @param mixed $to 
  255. * @param mixed $subject 
  256. * @param mixed $message 
  257. * @param string $headers (default: "Content-Type: text/html\r\n") 
  258. * @param string $attachments (default: "") 
  259. * @return bool 
  260. */ 
  261. public function send( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) { 
  262. // Send 
  263. $email = new WC_Email(); 
  264. return $email->send( $to, $subject, $message, $headers, $attachments ); 
  265.  
  266. /** 
  267. * Prepare and send the customer invoice email on demand. 
  268. */ 
  269. public function customer_invoice( $order ) { 
  270. $email = $this->emails['WC_Email_Customer_Invoice']; 
  271.  
  272. if ( ! is_object( $order ) ) { 
  273. $order = wc_get_order( absint( $order ) ); 
  274.  
  275. $email->trigger( $order->get_id(), $order ); 
  276.  
  277. /** 
  278. * Customer new account welcome email. 
  279. * 
  280. * @param int $customer_id 
  281. * @param array $new_customer_data 
  282. */ 
  283. public function customer_new_account( $customer_id, $new_customer_data = array(), $password_generated = false ) { 
  284. if ( ! $customer_id ) { 
  285. return; 
  286.  
  287. $user_pass = ! empty( $new_customer_data['user_pass'] ) ? $new_customer_data['user_pass'] : ''; 
  288.  
  289. $email = $this->emails['WC_Email_Customer_New_Account']; 
  290. $email->trigger( $customer_id, $user_pass, $password_generated ); 
  291.  
  292. /** 
  293. * Show the order details table 
  294. */ 
  295. public function order_details( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) { 
  296. if ( $plain_text ) { 
  297. wc_get_template( 'emails/plain/email-order-details.php', array( 'order' => $order, 'sent_to_admin' => $sent_to_admin, 'plain_text' => $plain_text, 'email' => $email ) ); 
  298. } else { 
  299. wc_get_template( 'emails/email-order-details.php', array( 'order' => $order, 'sent_to_admin' => $sent_to_admin, 'plain_text' => $plain_text, 'email' => $email ) ); 
  300.  
  301. /** 
  302. * Add order meta to email templates. 
  303. * 
  304. * @param mixed $order 
  305. * @param bool $sent_to_admin (default: false) 
  306. * @param bool $plain_text (default: false) 
  307. * @return string 
  308. */ 
  309. public function order_meta( $order, $sent_to_admin = false, $plain_text = false ) { 
  310. $fields = apply_filters( 'woocommerce_email_order_meta_fields', array(), $sent_to_admin, $order ); 
  311.  
  312. /** 
  313. * Deprecated woocommerce_email_order_meta_keys filter. 
  314. * 
  315. * @since 2.3.0 
  316. */ 
  317. $_fields = apply_filters( 'woocommerce_email_order_meta_keys', array(), $sent_to_admin ); 
  318.  
  319. if ( $_fields ) { 
  320. foreach ( $_fields as $key => $field ) { 
  321. if ( is_numeric( $key ) ) { 
  322. $key = $field; 
  323.  
  324. $fields[ $key ] = array( 
  325. 'label' => wptexturize( $key ),  
  326. 'value' => wptexturize( get_post_meta( $order->get_id(), $field, true ) ),  
  327. ); 
  328.  
  329. if ( $fields ) { 
  330.  
  331. if ( $plain_text ) { 
  332.  
  333. foreach ( $fields as $field ) { 
  334. if ( isset( $field['label'] ) && isset( $field['value'] ) && $field['value'] ) { 
  335. echo $field['label'] . ': ' . $field['value'] . "\n"; 
  336. } else { 
  337.  
  338. foreach ( $fields as $field ) { 
  339. if ( isset( $field['label'] ) && isset( $field['value'] ) && $field['value'] ) { 
  340. echo '<p><strong>' . $field['label'] . ':</strong> ' . $field['value'] . '</p>'; 
  341.  
  342. /** 
  343. * Is customer detail field valid? 
  344. * @param array $field 
  345. * @return boolean 
  346. */ 
  347. public function customer_detail_field_is_valid( $field ) { 
  348. return isset( $field['label'] ) && ! empty( $field['value'] ); 
  349.  
  350. /** 
  351. * Add customer details to email templates. 
  352. * 
  353. * @param WC_Order $order 
  354. * @param bool $sent_to_admin (default: false) 
  355. * @param bool $plain_text (default: false) 
  356. * @return string 
  357. */ 
  358. public function customer_details( $order, $sent_to_admin = false, $plain_text = false ) { 
  359. if ( ! is_a( $order, 'WC_Order' ) ) { 
  360. return; 
  361. $fields = array(); 
  362.  
  363. if ( $order->get_customer_note() ) { 
  364. $fields['customer_note'] = array( 
  365. 'label' => __( 'Note', 'woocommerce' ),  
  366. 'value' => wptexturize( $order->get_customer_note() ),  
  367. ); 
  368.  
  369. if ( $order->get_billing_email() ) { 
  370. $fields['billing_email'] = array( 
  371. 'label' => __( 'Email address', 'woocommerce' ),  
  372. 'value' => wptexturize( $order->get_billing_email() ),  
  373. ); 
  374.  
  375. if ( $order->get_billing_phone() ) { 
  376. $fields['billing_phone'] = array( 
  377. 'label' => __( 'Phone', 'woocommerce' ),  
  378. 'value' => wptexturize( $order->get_billing_phone() ),  
  379. ); 
  380.  
  381. $fields = array_filter( apply_filters( 'woocommerce_email_customer_details_fields', $fields, $sent_to_admin, $order ), array( $this, 'customer_detail_field_is_valid' ) ); 
  382.  
  383. if ( $plain_text ) { 
  384. wc_get_template( 'emails/plain/email-customer-details.php', array( 'fields' => $fields ) ); 
  385. } else { 
  386. wc_get_template( 'emails/email-customer-details.php', array( 'fields' => $fields ) ); 
  387.  
  388. /** 
  389. * Get the email addresses. 
  390. */ 
  391. public function email_addresses( $order, $sent_to_admin = false, $plain_text = false ) { 
  392. if ( ! is_a( $order, 'WC_Order' ) ) { 
  393. return; 
  394. if ( $plain_text ) { 
  395. wc_get_template( 'emails/plain/email-addresses.php', array( 'order' => $order ) ); 
  396. } else { 
  397. wc_get_template( 'emails/email-addresses.php', array( 'order' => $order ) ); 
  398.  
  399. /** 
  400. * Get blog name formatted for emails. 
  401. * @return string 
  402. */ 
  403. private function get_blogname() { 
  404. return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 
  405.  
  406. /** 
  407. * Low stock notification email. 
  408. * 
  409. * @param WC_Product $product 
  410. */ 
  411. public function low_stock( $product ) { 
  412. if ( 'no' === get_option( 'woocommerce_notify_low_stock', 'yes' ) ) { 
  413. return; 
  414.  
  415. $subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product low in stock', 'woocommerce' ) ); 
  416. /** translators: 1: product name 2: items in stock */ 
  417. $message = sprintf( 
  418. __( '%1$s is low in stock. There are %2$d left.', 'woocommerce' ),  
  419. html_entity_decode( strip_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ),  
  420. html_entity_decode( strip_tags( $product->get_stock_quantity() ) ) 
  421. ); 
  422.  
  423. wp_mail( 
  424. apply_filters( 'woocommerce_email_recipient_low_stock', get_option( 'woocommerce_stock_email_recipient' ), $product ),  
  425. apply_filters( 'woocommerce_email_subject_low_stock', $subject, $product ),  
  426. apply_filters( 'woocommerce_email_content_low_stock', $message, $product ),  
  427. apply_filters( 'woocommerce_email_headers', '', 'low_stock', $product ),  
  428. apply_filters( 'woocommerce_email_attachments', array(), 'low_stock', $product ) 
  429. ); 
  430.  
  431. /** 
  432. * No stock notification email. 
  433. * 
  434. * @param WC_Product $product 
  435. */ 
  436. public function no_stock( $product ) { 
  437. if ( 'no' === get_option( 'woocommerce_notify_no_stock', 'yes' ) ) { 
  438. return; 
  439.  
  440. $subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product out of stock', 'woocommerce' ) ); 
  441. /** translators: %s: product name */ 
  442. $message = sprintf( __( '%s is out of stock.', 'woocommerce' ), html_entity_decode( strip_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ) ); 
  443.  
  444. wp_mail( 
  445. apply_filters( 'woocommerce_email_recipient_no_stock', get_option( 'woocommerce_stock_email_recipient' ), $product ),  
  446. apply_filters( 'woocommerce_email_subject_no_stock', $subject, $product ),  
  447. apply_filters( 'woocommerce_email_content_no_stock', $message, $product ),  
  448. apply_filters( 'woocommerce_email_headers', '', 'no_stock', $product ),  
  449. apply_filters( 'woocommerce_email_attachments', array(), 'no_stock', $product ) 
  450. ); 
  451.  
  452. /** 
  453. * Backorder notification email. 
  454. * 
  455. * @param array $args 
  456. */ 
  457. public function backorder( $args ) { 
  458. $args = wp_parse_args( $args, array( 
  459. 'product' => '',  
  460. 'quantity' => '',  
  461. 'order_id' => '',  
  462. ) ); 
  463.  
  464. extract( $args ); 
  465.  
  466. if ( ! $product || ! $quantity || ! ( $order = wc_get_order( $order_id ) ) ) { 
  467. return; 
  468.  
  469. $subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product backorder', 'woocommerce' ) ); 
  470. $message = sprintf( __( '%1$s units of %2$s have been backordered in order #%3$s.', 'woocommerce' ), $quantity, html_entity_decode( strip_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ), $order->get_order_number() ); 
  471.  
  472. wp_mail( 
  473. apply_filters( 'woocommerce_email_recipient_backorder', get_option( 'woocommerce_stock_email_recipient' ), $args ),  
  474. apply_filters( 'woocommerce_email_subject_backorder', $subject, $args ),  
  475. apply_filters( 'woocommerce_email_content_backorder', $message, $args ),  
  476. apply_filters( 'woocommerce_email_headers', '', 'backorder', $args ),  
  477. apply_filters( 'woocommerce_email_attachments', array(), 'backorder', $args ) 
  478. ); 
  479.  
  480. /** 
  481. * Adds Schema.org markup for order in JSON-LD format. 
  482. * 
  483. * @deprecated 3.0.0 
  484. * @see WC_Structured_Data::generate_order_data() 
  485. * 
  486. * @since 2.6.0 
  487. * @param mixed $order 
  488. * @param bool $sent_to_admin (default: false) 
  489. * @param bool $plain_text (default: false) 
  490. */ 
  491. public function order_schema_markup( $order, $sent_to_admin = false, $plain_text = false ) { 
  492. wc_deprecated_function( 'WC_Emails::order_schema_markup', '3.0', 'WC_Structured_Data::generate_order_data' ); 
  493.  
  494. WC()->structured_data->generate_order_data( $order, $sent_to_admin, $plain_text ); 
  495. WC()->structured_data->output_structured_data(); 
.