Aucune description

class-woo-custom-emails-output.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * Woo_Custom_Emails_Output is a class used to output the custom email message.
  4. */
  5. class Woo_Custom_Emails_Output {
  6. // Define vars.
  7. /**
  8. * Assigns the current version of this plugin.
  9. *
  10. * @since 1.0.0
  11. * @var string $version The current version of this plugin.
  12. */
  13. protected $version;
  14. /**
  15. * Assigns the text domain of this plugin.
  16. *
  17. * @since 1.0.0
  18. * @var string $textdomain The text domain of this plugin.
  19. */
  20. protected $textdomain;
  21. /**
  22. * Assigns an array of messages which have already been added to the email.
  23. *
  24. * @since 1.0.0
  25. * @var array $shown_messages An array of messages which have already been added to the email.
  26. */
  27. public $shown_messages;
  28. /**
  29. * Class constructor.
  30. */
  31. public function __construct() {
  32. $this->version = WCE_PLUGIN_VERSION;
  33. $shown_messages = array();
  34. // ON-HOLD STATUS.
  35. add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'status_action_onhold' ), 10, 2 );
  36. add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'woo_custom_emails_insert_content' ), 10, 2 );
  37. add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'status_action_onhold' ), 10, 2 );
  38. add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'woo_custom_emails_insert_content' ), 10, 2 );
  39. add_action( 'woocommerce_order_status_cancelled_to_on-hold_notification', array( $this, 'status_action_onhold' ), 10, 2 );
  40. add_action( 'woocommerce_order_status_cancelled_to_on-hold_notification', array( $this, 'woo_custom_emails_insert_content' ), 10, 2 );
  41. // PROCESSING STATUS.
  42. add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'status_action_processing' ), 10, 2 );
  43. add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'woo_custom_emails_insert_content' ), 10, 2 );
  44. add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'status_action_processing' ), 10, 2 );
  45. add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'woo_custom_emails_insert_content' ), 10, 2 );
  46. add_action( 'woocommerce_order_status_on-hold_to_processing_notification', array( $this, 'status_action_processing' ), 10, 2 );
  47. add_action( 'woocommerce_order_status_on-hold_to_processing_notification', array( $this, 'woo_custom_emails_insert_content' ), 10, 2 );
  48. add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'status_action_processing' ), 10, 2 );
  49. add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'woo_custom_emails_insert_content' ), 10, 2 );
  50. // COMPLETED STATUS.
  51. add_action( 'woocommerce_order_status_completed_notification', array( $this, 'status_action_completed' ), 10, 2 );
  52. add_action( 'woocommerce_order_status_completed_notification', array( $this, 'woo_custom_emails_insert_content' ), 10, 2 );
  53. /*
  54. // FAILED STATUS
  55. // woocommerce_order_status_pending_to_failed_notification
  56. // woocommerce_order_status_on-hold_to_failed_notification
  57. // REFUNDED
  58. // woocommerce_order_fully_refunded_notification
  59. // woocommerce_order_partially_refunded_notification
  60. // CANCELLED
  61. // woocommerce_order_status_processing_to_cancelled_notification
  62. // woocommerce_order_status_on-hold_to_cancelled_notification
  63. */
  64. }
  65. /**
  66. * Adds a flag for when the current status is set to 'on-hold'.
  67. */
  68. public function status_action_onhold() {
  69. global $this_order_status_action;
  70. $this_order_status_action = 'woocommerce_order_status_on-hold';
  71. }
  72. /**
  73. * Adds a flag for when the current status is set to 'processing'.
  74. */
  75. public function status_action_processing() {
  76. global $this_order_status_action;
  77. $this_order_status_action = 'woocommerce_order_status_processing';
  78. }
  79. /**
  80. * Adds a flag for when the current status is set to 'completed'.
  81. */
  82. public function status_action_completed() {
  83. global $this_order_status_action;
  84. $this_order_status_action = 'woocommerce_order_status_completed';
  85. }
  86. /**
  87. * Insert the custom content into the email template at the chosen location.
  88. *
  89. * @param array $shown_messages An array of which messages have already been added to the email.
  90. * @param object $order An object containing all the order info.
  91. */
  92. public function woo_custom_emails_insert_content( $shown_messages, $order ) {
  93. global $woocommerce;
  94. if ( ! is_array( $shown_messages ) ) {
  95. $shown_messages = array();
  96. }
  97. global $this_order_status_action;
  98. if ( ! function_exists( 'log_message' ) ) {
  99. /**
  100. * Debug logging.
  101. *
  102. * @param string $message The message to be logged.
  103. * @param string $log A string to determine the log type.
  104. */
  105. function log_message( $message, $log = 'default' ) {
  106. if ( WP_DEBUG_LOG ) {
  107. $wc_logger = wc_get_logger();
  108. $context = array( 'source' => $log );
  109. if ( is_array( $message ) || is_object( $message ) ) {
  110. $wc_logger->debug( print_r( $message, true ), $context );
  111. } else {
  112. $wc_logger->debug( $message, $context );
  113. }
  114. }
  115. }
  116. }
  117. // log_message( $recipient ); // log the recipient.
  118. // Function to output the custom message.
  119. if ( ! function_exists( 'woo_custom_emails_output_message' ) ) {
  120. /**
  121. * Inserts the custom content into the email template at the chosen location.
  122. *
  123. * @param object $order An object containing all the Order information.
  124. * @param array $shown_messages An array of which messages have already been added to the email.
  125. * @param boolean $sent_to_admin A boolean value if this email is sent to admin or not.
  126. */
  127. function woo_custom_emails_output_message( $order, $sent_to_admin, $shown_messages ) {
  128. // Show content on Admin emails if setting is enabled.
  129. $options = get_option( 'woocustomemails_settings_name' );
  130. $show_in_admin_email_setting = isset( $options['show_in_admin_email'] ) ? $options['show_in_admin_email'] : false;
  131. if ( false !== $show_in_admin_email_setting ) {
  132. // Setting is enabled - show the message in the Admin email.
  133. // The parent function will return the message content.
  134. } else {
  135. // Setting is disabled - do not show the message in the Admin email.
  136. // Exit out of the parent function and return nothing.
  137. if ( $sent_to_admin ) {
  138. return;
  139. }
  140. }
  141. if ( ! is_array( $shown_messages ) ) {
  142. $shown_messages = array();
  143. }
  144. global $this_order_status_action;
  145. // Get items in this order.
  146. $items = (array) $order->get_items();
  147. // Loop through all items in this order.
  148. foreach ( $items as $item ) {
  149. // Get this product ID.
  150. $this_product_id = $item['product_id'];
  151. // Get this meta.
  152. $orderstatus_meta = (string) get_post_meta( $this_product_id, 'order_status', true );
  153. $wcemessage_id = get_post_meta( $this_product_id, 'wcemessage_id', true );
  154. $templatelocation_meta = get_post_meta( $this_product_id, 'location', true );
  155. /**
  156. * New data as of 2.2.0+
  157. *
  158. * @since 2.2.0
  159. */
  160. $wcemessage_id_onhold = (int) get_post_meta( $this_product_id, 'wcemessage_id_onhold', true );
  161. $wcemessage_location_onhold = get_post_meta( $this_product_id, 'location_onhold', true );
  162. $wcemessage_id_processing = (int) get_post_meta( $this_product_id, 'wcemessage_id_processing', true );
  163. $wcemessage_location_processing = get_post_meta( $this_product_id, 'location_processing', true );
  164. $wcemessage_id_completed = (int) get_post_meta( $this_product_id, 'wcemessage_id_completed', true );
  165. $wcemessage_location_completed = get_post_meta( $this_product_id, 'location_completed', true );
  166. // Set a var to track the current email template location.
  167. $this_email_template_location = (string) current_action();
  168. // If there is NEW data assigned...
  169. if ( ! empty( $wcemessage_id_onhold ) || ! empty( $wcemessage_id_processing ) || ! empty( $wcemessage_id_completed ) ) {
  170. // ON-HOLD Status.
  171. if ( 'woocommerce_order_status_on-hold' === $this_order_status_action ) {
  172. // If there is an email assigned for 'On hold' status...
  173. if ( ! empty( $wcemessage_id_onhold ) ) {
  174. // If this message has not already been shown in this email...
  175. if ( ! in_array( $wcemessage_id_onhold, $shown_messages, true ) ) {
  176. if ( ! is_array( $shown_messages ) ) {
  177. $shown_messages = array();
  178. }
  179. // If message location setting is equal to the current email template location...
  180. if ( $wcemessage_location_onhold === $this_email_template_location ) {
  181. // Show the message!
  182. // Define output var.
  183. $output = '';
  184. if ( 'woocommerce_email_order_meta' === $this_email_template_location || 'woocommerce_email_customer_details' === $this_email_template_location ) {
  185. // Extra line breaks at the beginning to separate Message content from Email content.
  186. $output .= '<br><br>';
  187. }
  188. // Output the_content of the saved WCE Message ID.
  189. $output .= nl2br( get_post_field( 'post_content', $wcemessage_id_onhold ) );
  190. // Extra line breaks at the end to separate Message content from Email content.
  191. $output .= '<br><br>';
  192. // Output everything!
  193. echo $output;
  194. // Update 'shown_emails' var.
  195. $shown_messages[] = $wcemessage_id_onhold;
  196. }
  197. }
  198. }
  199. }
  200. // PROCESSING Status.
  201. if ( 'woocommerce_order_status_processing' === $this_order_status_action ) {
  202. // If there is an email assigned for 'Processing' status...
  203. if ( ! empty( $wcemessage_id_processing ) ) {
  204. // If this message has not already been shown in this email...
  205. if ( ! in_array( $wcemessage_id_processing, $shown_messages ) ) {
  206. if ( ! is_array( $shown_messages ) ) {
  207. $shown_messages = array();
  208. }
  209. // If message location setting is equal to the current email template location...
  210. if ( $wcemessage_location_processing === $this_email_template_location ) {
  211. // Show the message!
  212. // Define output var.
  213. $output = '';
  214. if ( 'woocommerce_email_order_meta' === $this_email_template_location || 'woocommerce_email_customer_details' === $this_email_template_location ) {
  215. // Extra line breaks at the beginning to separate Message content from Email content.
  216. $output .= '<br><br>';
  217. }
  218. // Output the_content of the saved WCE Message ID.
  219. $output .= nl2br( get_post_field( 'post_content', $wcemessage_id_processing ) );
  220. // Extra line breaks at the end to separate Message content from Email content.
  221. $output .= '<br><br>';
  222. // Output everything!
  223. echo $output;
  224. // Update 'shown_emails' var.
  225. $shown_messages[] = $wcemessage_id_processing;
  226. }
  227. }
  228. }
  229. }
  230. // COMPLETED Status.
  231. if ( 'woocommerce_order_status_completed' === $this_order_status_action ) {
  232. // If there is an email assigned for 'Completed' status...
  233. if ( ! empty( $wcemessage_id_completed ) ) {
  234. // If this message has not already been shown in this email...
  235. if ( ! in_array( $wcemessage_id_completed, $shown_messages ) ) {
  236. if ( ! is_array( $shown_messages ) ) {
  237. $shown_messages = array();
  238. }
  239. // If message location setting is equal to the current email template location...
  240. if ( $wcemessage_location_completed === $this_email_template_location ) {
  241. // Show the message!
  242. // Define output var.
  243. $output = '';
  244. if ( 'woocommerce_email_order_meta' === $this_email_template_location || 'woocommerce_email_customer_details' === $this_email_template_location ) {
  245. // Extra line breaks at the beginning to separate Message content from Email content.
  246. $output .= '<br><br>';
  247. }
  248. // Output the_content of the saved WCE Message ID.
  249. $output .= nl2br( get_post_field( 'post_content', $wcemessage_id_completed ) );
  250. // Extra line breaks at the end to separate Message content from Email content.
  251. $output .= '<br><br>';
  252. // Output everything!
  253. echo $output;
  254. // Update 'shown_emails' var.
  255. $shown_messages[] = $wcemessage_id_completed;
  256. }
  257. }
  258. }
  259. }
  260. } else {
  261. // If there is a legacy WCE Message assigned...
  262. if ( $wcemessage_id ) {
  263. // If order status setting is equal to the current order status action...
  264. if ( $orderstatus_meta == $this_order_status_action ) {
  265. // If this message has not already been shown in this email...
  266. if ( ! in_array( $wcemessage_id, $shown_messages ) ) {
  267. // If template location setting is equal to the current email template location...
  268. if ( $templatelocation_meta == $this_email_template_location ) {
  269. // Show the message!
  270. // Define output var.
  271. $output = '';
  272. if ( 'woocommerce_email_order_meta' === $this_email_template_location || 'woocommerce_email_customer_details' === $this_email_template_location ) {
  273. // Extra line breaks at the beginning to separate Message content from Email content.
  274. $output .= '<br><br>';
  275. }
  276. // Output the_content of the saved WCE Message ID.
  277. $output .= nl2br( get_post_field( 'post_content', $wcemessage_id ) );
  278. // Extra line breaks at the end to separate Message content from Email content.
  279. $output .= '<br><br>';
  280. // Output everything!
  281. echo $output;
  282. // Update 'shown_emails' var.
  283. $shown_messages[] = $wcemessage_id;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. } // woo_custom_emails_output_message()
  291. }
  292. // Add an action for each email template location to insert our custom message.
  293. add_action( 'woocommerce_email_before_order_table', 'woo_custom_emails_output_message', 10, 3 );
  294. add_action( 'woocommerce_email_after_order_table', 'woo_custom_emails_output_message', 10, 3 );
  295. add_action( 'woocommerce_email_order_meta', 'woo_custom_emails_output_message', 10, 3 );
  296. add_action( 'woocommerce_email_customer_details', 'woo_custom_emails_output_message', 10, 3 );
  297. } // woo_custom_emails_insert_content()
  298. }