PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/class-wc-emails.php

https://gitlab.com/haque.mdmanzurul/barongbarong
PHP | 429 lines | 167 code | 81 blank | 181 comment | 17 complexity | 7fe387a548cb9854f16bcdbea1c2351c MD5 | raw file
  1. <?php
  2. /**
  3. * Transactional Emails Controller
  4. *
  5. * WooCommerce Emails Class which handles the sending on transactional emails and email templates. This class loads in available emails.
  6. *
  7. * @class WC_Emails
  8. * @version 2.0.0
  9. * @package WooCommerce/Classes/Emails
  10. * @category Class
  11. * @author WooThemes
  12. */
  13. class WC_Emails {
  14. /**
  15. * @var array Array of email notification classes.
  16. * @access public
  17. */
  18. public $emails;
  19. /**
  20. * @var string Stores the emailer's address.
  21. * @access private
  22. */
  23. private $_from_address;
  24. /**
  25. * @var string Stores the emailer's name.
  26. * @access private
  27. */
  28. private $_from_name;
  29. /**
  30. * @var mixed Content type for sent emails
  31. * @access private
  32. */
  33. private $_content_type;
  34. /**
  35. * @var WooCommerce The single instance of the class
  36. * @since 2.1
  37. */
  38. protected static $_instance = null;
  39. /**
  40. * Main WooCommerce Instance
  41. *
  42. * Ensures only one instance of WooCommerce is loaded or can be loaded.
  43. *
  44. * @since 2.1
  45. * @static
  46. * @see WC()
  47. * @return Main WooCommerce instance
  48. */
  49. public static function instance() {
  50. if ( is_null( self::$_instance ) )
  51. self::$_instance = new self();
  52. return self::$_instance;
  53. }
  54. /**
  55. * Cloning is forbidden.
  56. *
  57. * @since 2.1
  58. */
  59. public function __clone() {
  60. _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
  61. }
  62. /**
  63. * Unserializing instances of this class is forbidden.
  64. *
  65. * @since 2.1
  66. */
  67. public function __wakeup() {
  68. _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
  69. }
  70. /**
  71. * Constructor for the email class hooks in all emails that can be sent.
  72. *
  73. * @access public
  74. * @return void
  75. */
  76. function __construct() {
  77. $this->init();
  78. // Email Header, Footer and content hooks
  79. add_action( 'woocommerce_email_header', array( $this, 'email_header' ) );
  80. add_action( 'woocommerce_email_footer', array( $this, 'email_footer' ) );
  81. add_action( 'woocommerce_email_order_meta', array( $this, 'order_meta' ), 10, 3 );
  82. // Hooks for sending emails during store events
  83. add_action( 'woocommerce_low_stock_notification', array( $this, 'low_stock' ) );
  84. add_action( 'woocommerce_no_stock_notification', array( $this, 'no_stock' ) );
  85. add_action( 'woocommerce_product_on_backorder_notification', array( $this, 'backorder' ) );
  86. add_action( 'woocommerce_created_customer_notification', array( $this, 'customer_new_account' ), 10, 3 );
  87. // Let 3rd parties unhook the above via this hook
  88. do_action( 'woocommerce_email', $this );
  89. }
  90. /**
  91. * Init email classes
  92. */
  93. function init() {
  94. // Include email classes
  95. include_once( 'abstracts/abstract-wc-email.php' );
  96. $this->emails['WC_Email_New_Order'] = include( 'emails/class-wc-email-new-order.php' );
  97. $this->emails['WC_Email_Customer_Processing_Order'] = include( 'emails/class-wc-email-customer-processing-order.php' );
  98. $this->emails['WC_Email_Customer_Completed_Order'] = include( 'emails/class-wc-email-customer-completed-order.php' );
  99. $this->emails['WC_Email_Customer_Invoice'] = include( 'emails/class-wc-email-customer-invoice.php' );
  100. $this->emails['WC_Email_Customer_Note'] = include( 'emails/class-wc-email-customer-note.php' );
  101. $this->emails['WC_Email_Customer_Reset_Password'] = include( 'emails/class-wc-email-customer-reset-password.php' );
  102. $this->emails['WC_Email_Customer_New_Account'] = include( 'emails/class-wc-email-customer-new-account.php' );
  103. $this->emails = apply_filters( 'woocommerce_email_classes', $this->emails );
  104. }
  105. /**
  106. * Return the email classes - used in admin to load settings.
  107. *
  108. * @access public
  109. * @return array
  110. */
  111. function get_emails() {
  112. return $this->emails;
  113. }
  114. /**
  115. * Get from name for email.
  116. *
  117. * @access public
  118. * @return string
  119. */
  120. function get_from_name() {
  121. if ( ! $this->_from_name )
  122. $this->_from_name = get_option( 'woocommerce_email_from_name' );
  123. return wp_specialchars_decode( $this->_from_name );
  124. }
  125. /**
  126. * Get from email address.
  127. *
  128. * @access public
  129. * @return string
  130. */
  131. function get_from_address() {
  132. if ( ! $this->_from_address )
  133. $this->_from_address = get_option( 'woocommerce_email_from_address' );
  134. return $this->_from_address;
  135. }
  136. /**
  137. * Get the content type for the email.
  138. *
  139. * @access public
  140. * @return string
  141. */
  142. function get_content_type() {
  143. return $this->_content_type;
  144. }
  145. /**
  146. * Get the email header.
  147. *
  148. * @access public
  149. * @param mixed $email_heading heading for the email
  150. * @return void
  151. */
  152. function email_header( $email_heading ) {
  153. wc_get_template( 'emails/email-header.php', array( 'email_heading' => $email_heading ) );
  154. }
  155. /**
  156. * Get the email footer.
  157. *
  158. * @access public
  159. * @return void
  160. */
  161. function email_footer() {
  162. wc_get_template( 'emails/email-footer.php' );
  163. }
  164. /**
  165. * Wraps a message in the woocommerce mail template.
  166. *
  167. * @access public
  168. * @param mixed $email_heading
  169. * @param mixed $message
  170. * @return string
  171. */
  172. function wrap_message( $email_heading, $message, $plain_text = false ) {
  173. // Buffer
  174. ob_start();
  175. do_action( 'woocommerce_email_header', $email_heading );
  176. echo wpautop( wptexturize( $message ) );
  177. do_action( 'woocommerce_email_footer' );
  178. // Get contents
  179. $message = ob_get_clean();
  180. return $message;
  181. }
  182. /**
  183. * Send the email.
  184. *
  185. * @access public
  186. * @param mixed $to
  187. * @param mixed $subject
  188. * @param mixed $message
  189. * @param string $headers (default: "Content-Type: text/html\r\n")
  190. * @param string $attachments (default: "")
  191. * @param string $content_type (default: "text/html")
  192. * @return void
  193. */
  194. function send( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "", $content_type = 'text/html' ) {
  195. // Set content type
  196. $this->_content_type = $content_type;
  197. // Filters for the email
  198. add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  199. add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  200. add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
  201. // Send
  202. wp_mail( $to, $subject, $message, $headers, $attachments );
  203. // Unhook filters
  204. remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  205. remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  206. remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
  207. }
  208. /**
  209. * Prepare and send the customer invoice email on demand.
  210. *
  211. * @access public
  212. * @param mixed $pay_for_order
  213. * @return void
  214. */
  215. function customer_invoice( $order ) {
  216. $email = $this->emails['WC_Email_Customer_Invoice'];
  217. $email->trigger( $order );
  218. }
  219. /**
  220. * Customer new account welcome email.
  221. *
  222. * @access public
  223. * @param int $customer_id
  224. * @param array $new_customer_data
  225. * @return void
  226. */
  227. function customer_new_account( $customer_id, $new_customer_data = array(), $password_generated = false ) {
  228. if ( ! $customer_id )
  229. return;
  230. $user_pass = ! empty( $new_customer_data['user_pass'] ) ? $new_customer_data['user_pass'] : '';
  231. $email = $this->emails['WC_Email_Customer_New_Account'];
  232. $email->trigger( $customer_id, $user_pass, $password_generated );
  233. }
  234. /**
  235. * Add order meta to email templates.
  236. *
  237. * @access public
  238. * @param mixed $order
  239. * @param bool $sent_to_admin (default: false)
  240. * @param bool $plain_text (default: false)
  241. * @return void
  242. */
  243. function order_meta( $order, $sent_to_admin = false, $plain_text = false ) {
  244. $meta = array();
  245. $show_fields = apply_filters( 'woocommerce_email_order_meta_keys', array(), $sent_to_admin );
  246. if ( $order->customer_note )
  247. $meta[ __( 'Note', 'woocommerce' ) ] = wptexturize( $order->customer_note );
  248. if ( $show_fields )
  249. foreach ( $show_fields as $key => $field ) {
  250. if ( is_numeric( $key ) )
  251. $key = $field;
  252. $meta[ wptexturize( $key ) ] = wptexturize( get_post_meta( $order->id, $field, true ) );
  253. }
  254. if ( sizeof( $meta ) > 0 ) {
  255. if ( $plain_text ) {
  256. foreach ( $meta as $key => $value ) {
  257. if ( $value ) {
  258. echo $key . ': ' . $value . "\n";
  259. }
  260. }
  261. } else {
  262. foreach ( $meta as $key => $value ) {
  263. if ( $value ) {
  264. echo '<p><strong>' . $key . ':</strong> ' . $value . '</p>';
  265. }
  266. }
  267. }
  268. }
  269. }
  270. /**
  271. * Low stock notification email.
  272. *
  273. * @access public
  274. * @param mixed $product
  275. * @return void
  276. */
  277. function low_stock( $product ) {
  278. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  279. $subject = apply_filters( 'woocommerce_email_subject_low_stock', sprintf( '[%s] %s', $blogname, __( 'Product low in stock', 'woocommerce' ) ), $product );
  280. $sku = ($product->sku) ? '(' . $product->sku . ') ' : '';
  281. if ( ! empty( $product->variation_id ) )
  282. $title = sprintf(__( 'Variation #%s of %s', 'woocommerce' ), $product->variation_id, get_the_title($product->id)) . ' ' . $sku;
  283. else
  284. $title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . ' ' . $sku;
  285. $message = $title . __( 'is low in stock.', 'woocommerce' );
  286. // CC, BCC, additional headers
  287. $headers = apply_filters('woocommerce_email_headers', '', 'low_stock', $product);
  288. // Attachments
  289. $attachments = apply_filters('woocommerce_email_attachments', array(), 'low_stock', $product);
  290. // Send the mail
  291. wp_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message, $headers, $attachments );
  292. }
  293. /**
  294. * No stock notification email.
  295. *
  296. * @access public
  297. * @param mixed $product
  298. * @return void
  299. */
  300. function no_stock( $product ) {
  301. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  302. $subject = apply_filters( 'woocommerce_email_subject_no_stock', sprintf( '[%s] %s', $blogname, __( 'Product out of stock', 'woocommerce' ) ), $product );
  303. $sku = ($product->sku) ? '(' . $product->sku . ') ' : '';
  304. if ( ! empty( $product->variation_id ) )
  305. $title = sprintf(__( 'Variation #%s of %s', 'woocommerce' ), $product->variation_id, get_the_title($product->id)) . ' ' . $sku;
  306. else
  307. $title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . ' ' . $sku;
  308. $message = $title . __( 'is out of stock.', 'woocommerce' );
  309. // CC, BCC, additional headers
  310. $headers = apply_filters('woocommerce_email_headers', '', 'no_stock', $product);
  311. // Attachments
  312. $attachments = apply_filters('woocommerce_email_attachments', array(), 'no_stock', $product);
  313. // Send the mail
  314. wp_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message, $headers, $attachments );
  315. }
  316. /**
  317. * Backorder notification email.
  318. *
  319. * @access public
  320. * @param mixed $args
  321. * @return void
  322. */
  323. function backorder( $args ) {
  324. $defaults = array(
  325. 'product' => '',
  326. 'quantity' => '',
  327. 'order_id' => ''
  328. );
  329. $args = wp_parse_args( $args, $defaults );
  330. extract( $args );
  331. if (!$product || !$quantity) return;
  332. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  333. $subject = apply_filters( 'woocommerce_email_subject_backorder', sprintf( '[%s] %s', $blogname, __( 'Product Backorder', 'woocommerce' ) ), $product );
  334. $sku = ($product->sku) ? ' (' . $product->sku . ')' : '';
  335. if ( ! empty( $product->variation_id ) )
  336. $title = sprintf(__( 'Variation #%s of %s', 'woocommerce' ), $product->variation_id, get_the_title($product->id)) . $sku;
  337. else
  338. $title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . $sku;
  339. $order = new WC_Order( $order_id );
  340. $message = sprintf(__( '%s units of %s have been backordered in order %s.', 'woocommerce' ), $quantity, $title, $order->get_order_number() );
  341. // CC, BCC, additional headers
  342. $headers = apply_filters('woocommerce_email_headers', '', 'backorder', $args);
  343. // Attachments
  344. $attachments = apply_filters('woocommerce_email_attachments', array(), 'backorder', $args);
  345. // Send the mail
  346. wp_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message, $headers, $attachments );
  347. }
  348. }