PluginProbe
Whols – Wholesale Prices and B2B Store Solution for WooCommerce / trunk
Whols – Wholesale Prices and B2B Store Solution for WooCommerce vtrunk
2.4.12 2.4.11 trunk 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 55 releases
whols / includes / Email_Notifications.php

Email_Notifications.php in Whols – Wholesale Prices and B2B Store Solution for WooCommerce trunk, at includes/Email_Notifications.php

238 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Whols;
3
4 /**
5 * Email Notifications
6 */
7 class Email_Notifications {
8
9 /**
10 * Constructor.
11 */
12 public function __construct() {
13 add_action( 'whols_user_registration_success', array( $this, 'user_registration_email_for_admin' ) );
14 add_action( 'whols_user_registration_success', array( $this, 'user_registration_email_for_user' ) );
15
16 // Send email notification for either quote request or conversation
17 add_action( 'whols_after_raq_form_submit', array( $this, 'send_raq_email_notification' ) );
18 }
19
20 /**
21 * Notification for admin
22 */
23 public function user_registration_email_for_admin( $user_id ){
24 $enable_email_notification = whols_get_option('enable_registration_notification_for_admin');
25 $subject = whols_get_option('registration_notification_subject_for_admin');
26 $body = whols_get_option('registration_notification_message_for_admin');
27 $user = get_user_by( 'ID', $user_id );
28 $custom_emails = whols_get_option('registration_notification_recipients'); // Comma separated emails
29
30 if( $enable_email_notification && $user ){
31 $posted_data = [
32 'name' => $user->first_name,
33 'email' => $user->user_email,
34 'date' => gmdate( 'Y-m-d', strtotime( $user->user_registered ) ),
35 'time' => gmdate( 'H:i:s', strtotime( $user->user_registered ) )
36 ];
37
38 // subject
39 $subject = stripslashes( html_entity_decode($subject, ENT_QUOTES, 'UTF-8' ) );
40 $subject = $this->replace_placeholders($subject, $posted_data);
41
42 // body
43 $body = $this->replace_placeholders($body, $posted_data);
44 $body = wpautop($body);
45
46 // send the mail
47 $to = get_option('admin_email');
48 if( $custom_emails ){
49 $to = explode(',', $custom_emails);
50 }
51
52 $headers[] = 'Content-Type: text/html; charset=UTF-8';
53
54 wp_mail( $to, $subject, $body, $headers );
55 }
56 }
57
58 /**
59 * Notification for user
60 */
61 public function user_registration_email_for_user( $user_id ){
62 $enable_email_notification = whols_get_option('enable_registration_notification_for_user');
63 $subject = whols_get_option('registration_notification_subject_for_user');
64 $body = whols_get_option('registration_notification_message_for_user');
65 $user = get_user_by( 'ID', $user_id );
66
67 if( $enable_email_notification && $user ){
68 $posted_data = [
69 'name' => $user->first_name,
70 'email' => $user->user_email,
71 'date' => gmdate( 'Y-m-d', strtotime( $user->user_registered ) ),
72 'time' => gmdate( 'H:i:s', strtotime( $user->user_registered ) )
73 ];
74
75 // subject
76 $subject = $this->replace_placeholders($subject, $posted_data);
77
78 // body
79 $body = $this->replace_placeholders($body, $posted_data);
80 $body = wpautop($body);
81
82 // send the mail
83 $to = $user->user_email;
84 $headers[] = 'Content-Type: text/html; charset=UTF-8';
85
86 wp_mail( $to, $subject, $body, $headers );
87 }
88 }
89
90 public function send_raq_email_notification($posted_data) {
91 $defaults = array('location' => '');
92 $posted_data = wp_parse_args($posted_data, $defaults);
93
94 $email_type = $posted_data['location'] === 'cart' ? 'raq' : 'conversation';
95
96 if ($this->should_send_raq_email( $posted_data )) {
97 $email_data = $this->prepare_email_data($email_type, $posted_data);
98 $this->send_email($email_data);
99 }
100 }
101
102 public function should_send_raq_email( $posted_data ) {
103 $return_value = false;
104
105 $enable_raq_email_notification = whols_get_option('enable_raq_email_notification');
106 $enable_conversation_email_notification = whols_get_option('enable_conversation_email_notification');
107
108 // Location is cart, raq email notification is enabled
109 if( $posted_data['location'] == 'cart' && $enable_raq_email_notification ){
110 $return_value = true;
111 }
112
113 // Location is conversation, conversation email notification is enabled
114 if( $posted_data['location'] == 'conversation' && $enable_conversation_email_notification ){
115 $return_value = true;
116 }
117
118 return $return_value;
119 }
120
121 public function prepare_email_data($email_type, $posted_data) {
122 $defaults = array(
123 'raq_new_request_email_subject' => __('[{site_title}] New Quote Request from {name}', 'whols'),
124 'raq_new_request_email_body' => __('
125 Name: {name}
126 Email: {email}
127 Subject: {subject}
128 Message: {message}
129 Products: {products}', 'whols'),
130
131 'conversation_start_email_subject' => __('[{site_title}] New Conversation from {name}', 'whols'),
132 'conversation_start_email_body' => __('Name: {name}
133 Email: {email}
134 Subject: {subject}
135 Message: {message}
136 ', 'whols'),
137 );
138
139 if(whols_get_option('raq_new_request_email_subject')){
140 $defaults['raq_new_request_email_subject'] = whols_get_option('raq_new_request_email_subject');
141 }
142
143 if(whols_get_option('raq_new_request_email_body')){
144 $defaults['raq_new_request_email_body'] = whols_get_option('raq_new_request_email_body');
145 }
146
147 $subject_key = $email_type === 'raq' ? 'raq_new_request_email_subject' : 'conversation_start_email_subject';
148 $body_key = $email_type === 'raq' ? 'raq_new_request_email_body' : 'conversation_start_email_body';
149
150 $subject = $this->replace_placeholders($defaults[$subject_key], $posted_data);
151 $body = $this->replace_placeholders($defaults[$body_key], $posted_data);
152
153
154
155 if ($email_type === 'raq') {
156 $body = $this->add_products_data($body, $posted_data);
157 $body = $this->get_raq_prepared_body($body); // html, head, body
158 } else {
159 $body = wpautop($body);
160 }
161
162 return [
163 'to' => get_option('admin_email'),
164 'subject' => $subject,
165 'body' => $body,
166 'headers' => ['Content-Type: text/html; charset=UTF-8'],
167 ];
168 }
169
170 public function get_raq_prepared_body( $body ) {
171 ob_start();
172 ?>
173 <html>
174 <head>
175 <style>
176 table {
177 border-collapse: collapse;
178 margin: 20px 0;
179 font-size: 16px;
180 font-family: Arial, sans-serif;
181 text-align: left;
182 }
183 table, th, td {
184 border: 1px solid #dddddd;
185 padding: 8px;
186 }
187 th {
188 background-color: #f2f2f2;
189 }
190 </style>
191 </head>
192 <body>
193 <?php echo wpautop($body); // @phpcs:ignore ?>
194 </body>
195 </html>
196 <?php
197 return ob_get_clean();
198 }
199
200 public function replace_placeholders($content, $posted_data) {
201 $placeholders = [
202 '{name}' => $posted_data['name'] ?? '',
203 '{email}' => $posted_data['email'] ?? '',
204 '{message}' => $posted_data['message'] ?? '', // For Raq
205 '{date}' => date_i18n( get_option( 'date_format' ) ),
206 '{time}' => date_i18n( get_option( 'time_format' ) ),
207 '{subject}' => !empty($posted_data['subject']) ? $posted_data['subject'] : '', // For conversation
208 '{site_title}' => get_bloginfo('name'),
209 '{shop_url}' => get_permalink( wc_get_page_id( 'shop' ) )
210 ];
211
212 return str_replace(array_keys($placeholders), array_values($placeholders), $content);
213 }
214
215 public function add_products_data($raq_body_html, $posted_data) {
216 $products_data = json_decode(wp_unslash($posted_data['products_data']), true);
217 $products_data_html = '';
218
219 if ($products_data) {
220 ob_start();
221 include WHOLS_PATH . '/includes/request-a-quote/html-product-data.php';
222 $products_data_html = ob_get_clean();
223 }
224
225 return str_replace('{products}', $products_data_html, $raq_body_html);
226 }
227
228 public function send_email($email_data) {
229 $to = $email_data['to'];
230 $subject = $email_data['subject'];
231 $body = $email_data['body'];
232
233 wp_mail($to, $subject, $body, $email_data['headers']);
234 }
235 }
236
237 // New instance
238 new Email_Notifications();