WC_GZD_Emails

Attaches legal relevant Pages to WooCommerce Emails if has been set by WooCommerce Germanized Options.

Defined (1)

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

/includes/class-wc-gzd-emails.php  
  1. class WC_GZD_Emails { 
  2.  
  3. /** 
  4. * contains options and page ids 
  5. * @var array 
  6. */ 
  7. private $footer_attachments = array(); 
  8.  
  9. /** 
  10. * Adds legal page ids to different options and adds a hook to the email footer 
  11. */ 
  12. public function __construct() { 
  13.  
  14. // Order attachments 
  15. $attachment_order = wc_gzd_get_email_attachment_order(); 
  16. $this->footer_attachments = array(); 
  17.  
  18. foreach ( $attachment_order as $key => $order ) 
  19. $this->footer_attachments[ 'woocommerce_gzd_mail_attach_' . $key ] = $key; 
  20.  
  21. add_action( 'woocommerce_email', array( $this, 'email_hooks' ), 0, 1 ); 
  22.  
  23. // Change email template path if is germanized email template 
  24. add_filter( 'woocommerce_template_directory', array( $this, 'set_woocommerce_template_dir' ), 10, 2 ); 
  25.  
  26. $this->admin_hooks(); 
  27.  
  28.  
  29. public function admin_hooks() { 
  30. add_filter( 'woocommerce_resend_order_emails_available', array( $this, 'resend_order_emails' ), 0 ); 
  31.  
  32. public function resend_order_emails( $emails ) { 
  33. global $theorder; 
  34.  
  35. if ( is_null( $theorder ) ) 
  36. return $emails; 
  37.  
  38. array_push( $emails, 'customer_paid_for_order' ); 
  39.  
  40. return $emails; 
  41.  
  42. public function set_woocommerce_template_dir( $dir, $template ) { 
  43. if ( file_exists( WC_germanized()->plugin_path() . '/templates/' . $template ) ) 
  44. return 'woocommerce-germanized'; 
  45. return $dir; 
  46.  
  47. public function email_hooks( $mailer ) { 
  48. // Hook before WooCommerce Footer is applied 
  49. remove_action( 'woocommerce_email_footer', array( $mailer, 'email_footer' ) ); 
  50. add_action( 'woocommerce_email_footer', array( $this, 'add_template_footers' ), 0 ); 
  51. add_action( 'woocommerce_email_footer', array( $mailer, 'email_footer' ), 1 ); 
  52.  
  53. add_filter( 'woocommerce_email_footer_text', array( $this, 'email_footer_plain' ), 0 ); 
  54.  
  55. add_filter( 'woocommerce_email_styles', array( $this, 'styles' ) ); 
  56.  
  57. $mails = $mailer->get_emails(); 
  58.  
  59. if ( ! empty( $mails ) ) { 
  60.  
  61. foreach ( $mails as $mail ) 
  62. add_action( 'woocommerce_germanized_email_footer_' . $mail->id, array( $this, 'hook_mail_footer' ), 10, 1 ); 
  63.  
  64. // Set email filters 
  65. add_action( 'woocommerce_email_before_order_table', array( $this, 'set_order_email_filters' ), 10 ); 
  66. // Remove them after total has been displayed 
  67. add_action( 'woocommerce_email_after_order_table', array( $this, 'remove_order_email_filters' ), 10 ); 
  68.  
  69. // Pay now button 
  70. add_action( 'woocommerce_email_before_order_table', array( $this, 'email_pay_now_button' ), 0, 1 ); 
  71.  
  72. add_action( 'woocommerce_email_after_order_table', array( $this, 'email_digital_revocation_notice' ), 0, 3 ); 
  73.  
  74.  
  75. public function email_digital_revocation_notice( $order, $sent_to_admin, $plain_text ) { 
  76.  
  77. if ( get_option( 'woocommerce_gzd_checkout_legal_digital_checkbox' ) !== 'yes' ) 
  78. return; 
  79.  
  80. $type = $this->get_current_email_object(); 
  81.  
  82. if ( $type && $type->id == 'customer_processing_order' ) { 
  83.  
  84. // Check if order contains digital products 
  85. $items = $order->get_items(); 
  86. $is_downloadable = false; 
  87. $is_service = false; 
  88.  
  89. if ( ! empty( $items ) ) { 
  90.  
  91. foreach ( $items as $item ) { 
  92.  
  93. $_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item ); 
  94.  
  95. if ( wc_gzd_is_revocation_exempt( $_product ) || apply_filters( 'woocommerce_gzd_product_is_revocation_exception', false, $_product, 'digital' ) ) 
  96. $is_downloadable = true; 
  97.  
  98. if ( wc_gzd_is_revocation_exempt( $_product, 'service' ) || apply_filters( 'woocommerce_gzd_product_is_revocation_exception', false, $_product, 'service' ) ) 
  99. $is_service = true; 
  100.  
  101. if ( $is_downloadable && $text = wc_gzd_get_legal_text_digital_email_notice() ) 
  102. echo wpautop( apply_filters( 'woocommerce_gzd_order_confirmation_digital_notice', '<div class="gzd-digital-notice-text">' . $text . '</div>', $order ) ); 
  103.  
  104. if ( $is_service && $text = wc_gzd_get_legal_text_service_email_notice() ) 
  105. echo wpautop( apply_filters( 'woocommerce_gzd_order_confirmation_service_notice', '<div class="gzd-service-notice-text">' . $text . '</div>', $order ) ); 
  106.  
  107.  
  108. public function email_pay_now_button( $order ) { 
  109.  
  110. $type = $this->get_current_email_object(); 
  111.  
  112. if ( $type && $type->id == 'customer_processing_order' ) 
  113. WC_GZD_Checkout::instance()->add_payment_link( $order->id ); 
  114.  
  115. public function email_footer_plain( $text ) { 
  116.  
  117. $type = $this->get_current_email_object(); 
  118.  
  119. if ( $type && $type->get_email_type() == 'plain' ) 
  120. $this->add_template_footers(); 
  121.  
  122. return $text; 
  123.  
  124.  
  125. public function get_email_instance_by_id( $id ) { 
  126.  
  127. $mailer = WC()->mailer(); 
  128. $mails = $mailer->get_emails(); 
  129.  
  130. foreach ( $mails as $mail ) { 
  131. if ( $id === $mail->id ) 
  132. return $mail; 
  133.  
  134. return false; 
  135.  
  136. public function set_order_email_filters() { 
  137.  
  138. $current = $this->get_current_email_object(); 
  139.  
  140. if ( ! $current || empty( $current ) ) 
  141. return; 
  142.  
  143. // Add order item name actions 
  144. add_action( 'woocommerce_order_item_name', 'wc_gzd_cart_product_units', wc_gzd_get_hook_priority( 'email_product_units' ), 2 ); 
  145. add_action( 'woocommerce_order_item_name', 'wc_gzd_cart_product_delivery_time', wc_gzd_get_hook_priority( 'email_product_delivery_time' ), 2 ); 
  146. add_action( 'woocommerce_order_item_name', 'wc_gzd_cart_product_item_desc', wc_gzd_get_hook_priority( 'email_product_item_desc' ), 2 ); 
  147. add_filter( 'woocommerce_order_formatted_line_subtotal', 'wc_gzd_cart_product_unit_price', wc_gzd_get_hook_priority( 'email_product_unit_price' ), 2 ); 
  148.  
  149.  
  150. public function remove_order_email_filters() { 
  151.  
  152. $current = $this->get_current_email_object(); 
  153.  
  154. if ( ! $current || empty( $current ) ) 
  155. return; 
  156.  
  157. // Add order item name actions 
  158. remove_action( 'woocommerce_order_item_name', 'wc_gzd_cart_product_units', wc_gzd_get_hook_priority( 'email_product_units' ) ); 
  159. remove_action( 'woocommerce_order_item_name', 'wc_gzd_cart_product_delivery_time', wc_gzd_get_hook_priority( 'email_product_delivery_time' ) ); 
  160. remove_action( 'woocommerce_order_item_name', 'wc_gzd_cart_product_item_desc', wc_gzd_get_hook_priority( 'email_product_item_desc' ) ); 
  161. remove_filter( 'woocommerce_order_formatted_line_subtotal', 'wc_gzd_cart_product_unit_price', wc_gzd_get_hook_priority( 'email_product_unit_price' ) ); 
  162.  
  163.  
  164. /** 
  165. * Add email styles 
  166. *  
  167. * @param string $css  
  168. * @return string  
  169. */ 
  170. public function styles( $css ) { 
  171. return $css .= ' 
  172. .unit-price-cart { 
  173. display: block; 
  174. font-size: 0.9em; 
  175. .gzd-digital-notice-text { 
  176. margin-top: 16px; 
  177. '; 
  178.  
  179. /** 
  180. * Hook into Email Footer and attach legal page content if necessary 
  181. *  
  182. * @param object $mail 
  183. */ 
  184. public function hook_mail_footer( $mail ) { 
  185. if ( ! empty( $this->footer_attachments ) ) { 
  186. foreach ( $this->footer_attachments as $option_key => $page_option ) { 
  187. $option = woocommerce_get_page_id ( $page_option ); 
  188. if ( $option == -1 || ! get_option( $option_key ) ) 
  189. continue; 
  190. if ( in_array( $mail->id, get_option( $option_key ) ) && apply_filters( 'woocommerce_gzd_attach_email_footer', true, $mail, $page_option ) ) { 
  191. $this->attach_page_content( $option, $mail->get_email_type() ); 
  192.  
  193. /** 
  194. * Add global footer Hooks to Email templates 
  195. */ 
  196. public function add_template_footers() { 
  197. $type = $this->get_current_email_object(); 
  198. if ( $type ) 
  199. do_action( 'woocommerce_germanized_email_footer_' . $type->id, $type ); 
  200.  
  201. public function get_current_email_object() { 
  202.  
  203. if ( isset( $GLOBALS[ 'wc_gzd_template_name' ] ) && ! empty( $GLOBALS[ 'wc_gzd_template_name' ] ) ) { 
  204.  
  205. $object = $this->get_email_instance_by_tpl( $GLOBALS[ 'wc_gzd_template_name' ] ); 
  206. if ( is_object( $object ) ) 
  207. return $object; 
  208.  
  209. return false; 
  210.  
  211. /** 
  212. * Returns Email Object by examining the template file 
  213. *  
  214. * @param string $tpl  
  215. * @return mixed  
  216. */ 
  217. private function get_email_instance_by_tpl( $tpls = array() ) { 
  218. $found_mails = array(); 
  219. foreach ( $tpls as $tpl ) { 
  220. $tpl = apply_filters( 'woocommerce_germanized_email_template_name', str_replace( array( 'admin-', '-' ), array( '', '_' ), basename( $tpl, '.php' ) ), $tpl ); 
  221. $mails = WC()->mailer()->get_emails(); 
  222. if ( ! empty( $mails ) ) { 
  223. foreach ( $mails as $mail ) { 
  224. if ( is_object( $mail ) && $mail->id == $tpl ) 
  225. array_push( $found_mails, $mail ); 
  226. if ( ! empty( $found_mails ) ) 
  227. return $found_mails[ sizeof( $found_mails ) - 1 ]; 
  228. return null; 
  229.  
  230. /** 
  231. * Attach page content by ID. Removes revocation_form shortcut to not show the form within the Email footer. 
  232. *  
  233. * @param integer $page_id  
  234. */ 
  235. public function attach_page_content( $page_id, $email_type = 'html' ) { 
  236.  
  237. remove_shortcode( 'revocation_form' ); 
  238. add_shortcode( 'revocation_form', array( $this, 'revocation_form_replacement' ) ); 
  239.  
  240. $template = 'emails/email-footer-attachment.php'; 
  241. if ( $email_type == 'plain' ) 
  242. $template = 'emails/plain/email-footer-attachment.php'; 
  243.  
  244. wc_get_template( $template, array( 
  245. 'post_attach' => get_post( $page_id ),  
  246. ) ); 
  247.  
  248. add_shortcode( 'revocation_form', 'WC_GZD_Shortcodes::revocation_form' ); 
  249.  
  250. /** 
  251. * Replaces revocation_form shortcut with a link to the revocation form 
  252. *  
  253. * @param array $atts  
  254. * @return string  
  255. */ 
  256. public function revocation_form_replacement( $atts ) { 
  257. return '<a href="' . esc_url( get_permalink( wc_get_page_id( 'revocation' ) ) ) . '">' . _x( 'Forward your Revocation online', 'revocation-form', 'woocommerce-germanized' ) . '</a>'; 
  258.