PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
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.3.13, at includes/class-email.php

508 lines 14.2 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 $recipients = apply_filters( 'erp_mail_recipients', $to );
355
356 $return = false;
357 if ( ! empty( $recipients ) ) {
358 $return = erp_mail( $recipients, $subject, $message, $headers, $attachments );
359 }
360
361 return $return;
362 }
363
364 function generate_settings_html() {
365 $settings = $this->get_form_fields();
366 $this->output_fields( $settings );
367 }
368
369 public function get_template_content( $file_path, $args = [] ) {
370 extract( $args );
371
372 ob_start();
373 include $file_path;
374 return ob_get_clean();
375 }
376
377 /**
378 * Get email setting by key
379 *
380 * @param string $option
381 * @param string $default
382 *
383 * @return string
384 */
385 public function get_setting( $option, $default = '' ) {
386 $settings = get_option( 'erp_settings_erp-email_general', [] );
387
388 if ( array_key_exists( $option, $settings ) ) {
389 return $settings[ $option ];
390 }
391
392 return $default;
393 }
394
395 public function admin_options() {
396 ?>
397 <h3><?php echo esc_html( $this->get_title() ); ?></h3>
398 <?php echo wpautop( wp_kses_post( $this->get_description() ) ); ?>
399
400 <?php
401 /**
402 * erp_email_settings_before action hook.
403 *
404 * @param string $email The email object
405 */
406 do_action( 'erp_email_settings_before', $this );
407 ?>
408
409 <table class="form-table">
410 <?php $this->generate_settings_html(); ?>
411 </table>
412
413 <?php
414 /**
415 * erp_email_settings_after action hook.
416 *
417 * @param string $email The email object
418 */
419 do_action( 'erp_email_settings_after', $this );
420 ?>
421 <?php
422 }
423
424 /**
425 * get_content_html function.
426 *
427 * @access public
428 * @return string
429 */
430 function get_content_html() {
431 $message = $this->get_template_content( WPERP_INCLUDES . '/email/email-body.php', $this->get_args() );
432
433 return $this->format_string( $message );
434 }
435
436 /**
437 * get_content_plain function.
438 *
439 * @access public
440 * @return string
441 */
442 function get_content_plain() {
443 $message = $this->get_template_content( WPERP_INCLUDES . '/email/email-body.php', $this->get_args() );
444
445 return $message;
446 }
447
448 /**
449 * Initialise settings form fields.
450 */
451 public function init_form_fields() {
452 $this->form_fields = [
453 [
454 'title' => __( 'Subject', 'erp' ),
455 'id' => 'subject',
456 'type' => 'text',
457 'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', 'erp' ), $this->subject ),
458 'placeholder' => '',
459 'default' => $this->subject,
460 'desc_tip' => true
461 ],
462 [
463 'title' => __( 'Email Heading', 'erp' ),
464 'id' => 'heading',
465 'type' => 'text',
466 '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 ),
467 'placeholder' => '',
468 'default' => $this->heading,
469 'desc_tip' => true
470 ],
471 [
472 'title' => __( 'Email Body', 'erp' ),
473 'type' => 'wysiwyg',
474 'id' => 'body',
475 '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 ),
476 'placeholder' => '',
477 'default' => '',
478 'desc_tip' => true,
479 'custom_attributes' => [
480 'rows' => 5,
481 'cols' => 45
482 ]
483 ],
484 [
485 'type' => $this->id . '_help_texts'
486 ]
487 ];
488 }
489
490 /**
491 * Template tags
492 *
493 * @return void
494 */
495 function replace_keys() {
496 ?>
497 <tr valign="top" class="single_select_page">
498 <th scope="row" class="titledesc"><?php _e( 'Template Tags', 'erp' ); ?></th>
499 <td class="forminp">
500 <em><?php _e( 'You may use these template tags inside subject, heading, body and those will be replaced by original values', 'erp' ); ?></em>:
501 <?php echo '<code>' . implode( '</code>, <code>', $this->find ) . '</code>'; ?>
502 </td>
503 </tr>
504 <?php
505 }
506 }
507
508