PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.2.6
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.2.6
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / class-email.php

class-email.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.2.6, at includes/class-email.php

503 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WeDevs\ERP;
3
4 use \WeDevs\ERP\Framework\ERP_Settings_Page;
5
6 /**
7 * Email Class
8 */
9 class Email extends ERP_Settings_Page {
10
11 /**
12 * Email method ID.
13 *
14 * @var String
15 */
16 public $id;
17
18 /**
19 * Email method title.
20 *
21 * @var string
22 */
23 public $title;
24
25 /**
26 * Description for the email.
27 *
28 * @var string
29 */
30 public $description;
31
32 /**
33 * Plain text template path.
34 *
35 * @var string
36 */
37 public $template_plain;
38
39 /**
40 * HTML template path.
41 *
42 * @var string
43 */
44 public $template_html;
45
46 /**
47 * Recipients for the email.
48 *
49 * @var string
50 */
51 public $recipient;
52
53 /**
54 * Heading for the email content.
55 *
56 * @var string
57 */
58 public $heading;
59
60 /**
61 * Subject for the email.
62 *
63 * @var string
64 */
65 public $subject;
66
67 /**
68 * Object this email is for, for example a customer, product, or email.
69 *
70 * @var object
71 */
72 public $object;
73
74 /**
75 * Strings to find in subjects/headings.
76 *
77 * @var array
78 */
79 public $find;
80
81 /**
82 * Strings to replace in subjects/headings.
83 *
84 * @var array
85 */
86 public $replace;
87
88 /**
89 * Mime boundary (for multipart emails).
90 *
91 * @var string
92 */
93 public $mime_boundary;
94
95 /**
96 * Mime boundary header (for multipart emails).
97 *
98 * @var string
99 */
100 public $mime_boundary_header;
101
102 /**
103 * Form option fields.
104 *
105 * @var array
106 */
107 public $form_fields = array();
108
109 /**
110 * Email type
111 *
112 * @var string
113 */
114 public $email_type = 'html';
115
116 /**
117 * List of preg* regular expression patterns to search for,
118 * used in conjunction with $replace.
119 * https://raw.github.com/ushahidi/wp-silcc/master/class.html2text.inc
120 *
121 * @var array $search
122 * @see $replace
123 */
124 public $plain_search = array(
125 "/\r/", // Non-legal carriage return
126 '/&(nbsp|#160);/i', // Non-breaking space
127 '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i', // Double quotes
128 '/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes
129 '/&gt;/i', // Greater-than
130 '/&lt;/i', // Less-than
131 '/&#38;/i', // Ampersand
132 '/&#038;/i', // Ampersand
133 '/&amp;/i', // Ampersand
134 '/&(copy|#169);/i', // Copyright
135 '/&(trade|#8482|#153);/i', // Trademark
136 '/&(reg|#174);/i', // Registered
137 '/&(mdash|#151|#8212);/i', // mdash
138 '/&(ndash|minus|#8211|#8722);/i', // ndash
139 '/&(bull|#149|#8226);/i', // Bullet
140 '/&(pound|#163);/i', // Pound sign
141 '/&(euro|#8364);/i', // Euro sign
142 '/&#36;/', // Dollar sign
143 '/&[^&\s;]+;/i', // Unknown/unhandled entities
144 '/[ ]{2,}/' // Runs of spaces, post-handling
145 );
146
147 /**
148 * List of pattern replacements corresponding to patterns searched.
149 *
150 * @var array $replace
151 * @see $search
152 */
153 public $plain_replace = array(
154 '', // Non-legal carriage return
155 ' ', // Non-breaking space
156 '"', // Double quotes
157 "'", // Single quotes
158 '>', // Greater-than
159 '<', // Less-than
160 '&', // Ampersand
161 '&', // Ampersand
162 '&', // Ampersand
163 '(c)', // Copyright
164 '(tm)', // Trademark
165 '(R)', // Registered
166 '--', // mdash
167 '-', // ndash
168 '*', // Bullet
169 '£', // Pound sign
170 'EUR', // Euro sign. € ?
171 '$', // Dollar sign
172 '', // Unknown/unhandled entities
173 ' ' // Runs of spaces, post-handling
174 );
175
176 public function __construct() {
177 $this->init_form_fields();
178 }
179
180 public function get_title() {
181 return $this->title;
182 }
183
184 public function get_description() {
185 return $this->description;
186 }
187
188 /**
189 * Get saved option id
190 *
191 * @return string
192 */
193 public function get_option_id() {
194 return 'erp_email_settings_' . $this->id;
195 }
196
197 /**
198 * format_string function.
199 *
200 * @param mixed $string
201 * @return string
202 */
203 public function format_string( $string ) {
204 return str_replace( $this->find, $this->replace, $string );
205 }
206
207 /**
208 * get_subject function.
209 *
210 * @return string
211 */
212 public function get_subject() {
213 return apply_filters( 'erp_email_subject_' . $this->id, $this->format_string( $this->subject ), $this->object );
214 }
215
216 /**
217 * get_heading function.
218 *
219 * @return string
220 */
221 public function get_heading() {
222 return apply_filters( 'erp_email_heading_' . $this->id, $this->format_string( $this->heading ), $this->object );
223 }
224
225 /**
226 * get_recipient function.
227 *
228 * @return string
229 */
230 public function get_recipient() {
231 return apply_filters( 'erp_email_recipient_' . $this->id, $this->recipient, $this->object );
232 }
233
234 /**
235 * get_headers function.
236 *
237 * @return string
238 */
239 public function get_headers() {
240 return apply_filters( 'erp_email_headers', "Content-Type: " . $this->get_content_type() . "\r\n", $this->id, $this->object );
241 }
242
243 /**
244 * get_attachments function.
245 *
246 * @return string|array
247 */
248 public function get_attachments() {
249 return apply_filters( 'erp_email_attachments', array(), $this->id, $this->object );
250 }
251
252 /**
253 * get_type function.
254 *
255 * @return string
256 */
257 public function get_email_type() {
258 return $this->email_type ? $this->email_type : 'plain';
259 }
260
261 /**
262 * get_content_type function.
263 *
264 * @return string
265 */
266 public function get_content_type() {
267 switch ( $this->get_email_type() ) {
268 case 'html' :
269 return 'text/html';
270 case 'multipart' :
271 return 'multipart/alternative';
272 default :
273 return 'text/plain';
274 }
275 }
276
277 /**
278 * get_blogname function.
279 *
280 * @return string
281 */
282 public function get_blogname() {
283 return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
284 }
285
286 /**
287 * get_content function.
288 *
289 * @return string
290 */
291 public function get_content() {
292
293 $this->sending = true;
294
295 if ( $this->get_email_type() == 'plain' ) {
296 $email_content = preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) );
297 } else {
298 $email_content = $this->get_content_html();
299 }
300
301 return wordwrap( $email_content, 70 );
302 }
303
304 /**
305 * Apply inline styles to dynamic content.
306 *
307 * @param string|null $content
308 * @return string
309 */
310 public function style_inline( $content ) {
311 // make sure we only inline CSS for html emails
312 if ( in_array( $this->get_content_type(), array( 'text/html', 'multipart/alternative' ) ) && class_exists( 'DOMDocument' ) ) {
313
314 // get CSS styles
315 ob_start();
316 include WPERP_INCLUDES . '/email/email-css.php';
317 $css = apply_filters( 'erp_email_styles', ob_get_clean() );
318
319 try {
320
321 // apply CSS styles inline for picky email clients
322 $emogrifier = new \WeDevs\ERP\Lib\Emogrifier( $content, $css );
323 $content = $emogrifier->emogrify();
324
325 } catch ( Exception $e ) {
326
327 echo $e->getMessage();
328 }
329 }
330
331 return $content;
332 }
333
334 /**
335 * Get the form fields after they are initialized.
336 * @return array of options
337 */
338 public function get_form_fields() {
339 return apply_filters( 'erp_settings_email_form_fields_' . $this->id, $this->form_fields );
340 }
341
342 /**
343 * Send the email.
344 *
345 * @param string $to
346 * @param string $subject
347 * @param string $message
348 * @param string $headers
349 * @param string $attachments
350 * @return bool
351 */
352 public function send( $to, $subject, $message, $headers, $attachments ) {
353 $message = apply_filters( 'erp_mail_content', $this->style_inline( $message ) );
354 $return = erp_mail( $to, $subject, $message, $headers, $attachments );
355
356 return $return;
357 }
358
359 function generate_settings_html() {
360 $settings = $this->get_form_fields();
361 $this->output_fields( $settings );
362 }
363
364 public function get_template_content( $file_path, $args = [] ) {
365 extract( $args );
366
367 ob_start();
368 include $file_path;
369 return ob_get_clean();
370 }
371
372 /**
373 * Get email setting by key
374 *
375 * @param string $option
376 * @param string $default
377 *
378 * @return string
379 */
380 public function get_setting( $option, $default = '' ) {
381 $settings = get_option( 'erp_settings_erp-email_general', [] );
382
383 if ( array_key_exists( $option, $settings ) ) {
384 return $settings[ $option ];
385 }
386
387 return $default;
388 }
389
390 public function admin_options() {
391 ?>
392 <h3><?php echo esc_html( $this->get_title() ); ?></h3>
393 <?php echo wpautop( wp_kses_post( $this->get_description() ) ); ?>
394
395 <?php
396 /**
397 * erp_email_settings_before action hook.
398 *
399 * @param string $email The email object
400 */
401 do_action( 'erp_email_settings_before', $this );
402 ?>
403
404 <table class="form-table">
405 <?php $this->generate_settings_html(); ?>
406 </table>
407
408 <?php
409 /**
410 * erp_email_settings_after action hook.
411 *
412 * @param string $email The email object
413 */
414 do_action( 'erp_email_settings_after', $this );
415 ?>
416 <?php
417 }
418
419 /**
420 * get_content_html function.
421 *
422 * @access public
423 * @return string
424 */
425 function get_content_html() {
426 $message = $this->get_template_content( WPERP_INCLUDES . '/email/email-body.php', $this->get_args() );
427
428 return $this->format_string( $message );
429 }
430
431 /**
432 * get_content_plain function.
433 *
434 * @access public
435 * @return string
436 */
437 function get_content_plain() {
438 $message = $this->get_template_content( WPERP_INCLUDES . '/email/email-body.php', $this->get_args() );
439
440 return $message;
441 }
442
443 /**
444 * Initialise settings form fields.
445 */
446 public function init_form_fields() {
447 $this->form_fields = [
448 [
449 'title' => __( 'Subject', 'erp' ),
450 'id' => 'subject',
451 'type' => 'text',
452 'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', 'erp' ), $this->subject ),
453 'placeholder' => '',
454 'default' => $this->subject,
455 'desc_tip' => true
456 ],
457 [
458 'title' => __( 'Email Heading', 'erp' ),
459 'id' => 'heading',
460 'type' => 'text',
461 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.', 'erp' ), $this->heading ),
462 'placeholder' => '',
463 'default' => $this->heading,
464 'desc_tip' => true
465 ],
466 [
467 'title' => __( 'Email Body', 'erp' ),
468 'type' => 'wysiwyg',
469 'id' => 'body',
470 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.', 'erp' ), $this->heading ),
471 'placeholder' => '',
472 'default' => '',
473 'desc_tip' => true,
474 'custom_attributes' => [
475 'rows' => 5,
476 'cols' => 45
477 ]
478 ],
479 [
480 'type' => $this->id . '_help_texts'
481 ]
482 ];
483 }
484
485 /**
486 * Template tags
487 *
488 * @return void
489 */
490 function replace_keys() {
491 ?>
492 <tr valign="top" class="single_select_page">
493 <th scope="row" class="titledesc"><?php _e( 'Template Tags', 'erp' ); ?></th>
494 <td class="forminp">
495 <em><?php _e( 'You may use these template tags inside subject, heading, body and those will be replaced by original values', 'erp' ); ?></em>:
496 <?php echo '<code>' . implode( '</code>, <code>', $this->find ) . '</code>'; ?>
497 </td>
498 </tr>
499 <?php
500 }
501 }
502
503