PluginProbe
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar / 2.0
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar v2.0
2.1.21 2.1.20 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 trunk 1.1 2.0 2.0.1 2.0.10.2 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.18 2.0.2 2.0.20 2.0.21 2.0.22 All 55 releases
booking-manager / core / any / api-emails.php

api-emails.php in Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar 2.0, at core/any/api-emails.php

533 lines 22.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.1
4 * @package General Emails API
5 * @category General Emails API Fields for Settings page and Functions for Sending emails.
6 * @author wpdevelop
7 *
8 * @web-site http://oplugins.com/
9 * @email info@oplugins.com
10 * @modified 2016-05-12, 2015-10-06
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14
15 // Email Settings API - Saving different options
16 abstract class WPBM_Emails_API extends WPBM_Settings_API {
17
18 public $sending;
19 public $replace = array();
20
21
22
23 /** Email Settings API Constructor
24 * During creation, system try to load values from DB, if exist.
25 *
26 * @param type $id - "Pure Email Template name
27 */
28 public function __construct( $id, $init_fields_values = array()) {
29
30 $options = array(
31 'db_prefix_option' => 'wpbm_email_'
32 , 'db_saving_type' => 'togather' // { 'togather' (Default), 'separate', 'separate_prefix' }
33 );
34 // Email template saved as:
35 // ( "prefix_email_" . $id ) >>> "prefix_email_new_admin"
36
37 parent::__construct( $id, $options, $init_fields_values ); // Define ID of Setting page and options
38
39 add_filter( 'phpmailer_init', array( $this, 'process_multipart' ) ); // For multipart messages
40
41 add_action( 'wp_mail_failed', array($this, 'email_error_parse') ); // Parse any errors during sending emails.
42 }
43
44
45
46 /** This function must be overriden - Initialise Settings Form Fields
47
48 public function init_settings_fields() {
49
50 $this->fields = array();
51
52 $this->fields['enabled'] = array(
53 'type' => 'checkbox'
54 , 'default' => 'On'
55 , 'title' => __('Enable / Disable', 'booking-manager')
56 , 'label' => __('Enable this email notification', 'booking-manager')
57 , 'description' => ''
58 , 'group' => 'general'
59
60 );
61 // ...
62 }
63 /**/
64
65
66
67 // <editor-fold defaultstate="collapsed" desc=" Suport functions " >
68
69 /** List of preg* regular expression patterns to search for replace in plain emails.
70 * More: https://raw.github.com/ushahidi/wp-silcc/master/class.html2text.inc
71 */
72 private function get_plain_search_array() {
73 return array(
74 "/\r/", // Non-legal carriage return
75 '/&(nbsp|#160);/i', // Non-breaking space
76 '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i', // Double quotes
77 '/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes
78 '/&gt;/i', // Greater-than
79 '/&lt;/i', // Less-than
80 '/&#38;/i', // Ampersand
81 '/&#038;/i', // Ampersand
82 '/&amp;/i', // Ampersand
83 '/&(copy|#169);/i', // Copyright
84 '/&(trade|#8482|#153);/i', // Trademark
85 '/&(reg|#174);/i', // Registered
86 '/&(mdash|#151|#8212);/i', // mdash
87 '/&(ndash|minus|#8211|#8722);/i', // ndash
88 '/&(bull|#149|#8226);/i', // Bullet
89 '/&(pound|#163);/i', // Pound sign
90 '/&(euro|#8364);/i', // Euro sign
91 '/&#36;/', // Dollar sign
92 '/&[^&;]+;/i', // Unknown/unhandled entities
93 '/[ ]{2,}/' // Runs of spaces, post-handling
94 );
95
96 }
97
98
99 /** List of symbols "for Replace To" */
100 private function get_plain_replace_array() {
101
102 return array(
103 '', // Non-legal carriage return
104 ' ', // Non-breaking space
105 '"', // Double quotes
106 "'", // Single quotes
107 '>', // Greater-than
108 '<', // Less-than
109 '&', // Ampersand
110 '&', // Ampersand
111 '&', // Ampersand
112 '(c)', // Copyright
113 '(tm)', // Trademark
114 '(R)', // Registered
115 '--', // mdash
116 '-', // ndash
117 '*', // Bullet
118 '£', // Pound sign
119 'EUR', // Euro sign. € ?
120 '$', // Dollar sign
121 '', // Unknown/unhandled entities
122 ' ' // Runs of spaces, post-handling
123 );
124 }
125
126
127 /** Esacpe and replace any HTML entities
128 *
129 * @param type $string
130 * @return string
131 */
132 public function esc_to_plain_text( $string ) {
133
134 // //Replace <a href="http://server.com">Link</a> to Link( http://server.com )
135 // $pattern = "/<a(.*)+href=[\"|']+([^\"']+)(?=(\"|'))[^>]*>(.*)<\/a>/" ; //"/(?<=href=(\"|'))[^\"']+(?=(\"|'))/i";
136 // $newurl = "$4 ($2)";
137 // $string = preg_replace($pattern,$newurl,$string);
138
139 $newstring = preg_replace( $this->get_plain_search_array(), $this->get_plain_replace_array(), strip_tags( $string ) );
140 return $newstring;
141 }
142
143
144 /** Set array for replacing shortcodes in Email Content and Subject
145 *
146 * @param array $replace - if this parameter skipped, then array reseted to empty array
147 */
148 function set_replace( $replace = array() ) {
149 $this->replace = $replace;
150 }
151
152
153 /** Replace shortcodes in givven string. Usually its Email Content and Subject
154 *
155 * @param string $subject
156 */
157 public function replace_shortcodes( $subject ) {
158
159 $defaults = array(
160 'ip' => apply_wpbm_filter( 'wpbm_get_user_ip' )
161 , 'blogname' => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES )
162 , 'siteurl' => get_site_url()
163 );
164
165 $replace = wp_parse_args( $this->replace, $defaults );
166
167 foreach ( $replace as $replace_shortcode => $replace_value ) {
168 $replace_shortcode = (string) $replace_shortcode;
169 $replace_value = (string) $replace_value;
170 $subject = str_replace( array( '[' . $replace_shortcode . ']'
171 , '{' . $replace_shortcode . '}' )
172 , $replace_value
173 , $subject );
174 }
175
176 return $subject;
177 }
178
179 // </editor-fold>
180
181
182 // <editor-fold defaultstate="collapsed" desc=" Pure Email functions " >
183
184 /** For MultiPart email define plain text - AltBody
185 *
186 * Also additionaly fix Sender - its have to be same as From
187 *
188 * @param PHPMailer $mailer
189 * @return PHPMailer
190 */
191 public function process_multipart( $mailer ) {
192
193 //if ( $this->sending && $this->get_email_content_type() == 'multipart' ) {
194 if ( $this->sending ) {
195
196 if ( $this->get_email_content_type() == 'multipart' )
197 $mailer->AltBody = wordwrap( $this->esc_to_plain_text( $this->replace_shortcodes( $this->get_content_plain() ) ) );
198
199 $mailer->Sender = $mailer->From;
200
201 //$this->sending = false; // If we set it to false, then we can not trigger for any Mail errors in this CLASS
202 }
203
204 return $mailer;
205 }
206
207
208 /** Get type of Email: 'plain' | 'html' | 'multipart'
209 *
210 * @return string
211 */
212 public function get_email_content_type() {
213
214 return $this->fields_values['email_content_type'] && class_exists( 'DOMDocument' ) ? $this->fields_values['email_content_type'] : 'plain';
215 }
216
217
218 /** Get Email Content Type: 'text/plain' | 'text/html' | 'multipart/alternative'
219 *
220 * @return string
221 */
222 public function get_content_type() {
223
224 switch ( $this->get_email_content_type() ) {
225 case 'html' :
226 return 'text/html';
227 case 'multipart' :
228 return 'multipart/alternative';
229 default :
230 return 'text/plain';
231 }
232 }
233
234
235 /** Define Email Headers. For exmaple: "Conte type:" 'text/plain' | 'text/html' | 'multipart/alternative' or "From": Name <email@server.com>
236 *
237 * @return string
238 */
239 public function get_headers( $additional_params = array() ) {
240
241 $mail_headers = '';
242
243 $from_address = $this->get_from__email_address();
244
245 if ( ! empty( $from_address ) ) {
246 $mail_headers .= 'From: ' . $this->get_from__name() . ' <' . $from_address . '> ' . "\r\n" ;
247 } else {
248 /* If we don't have an email from the input headers default to wordpress@$sitename
249 * Some hosts will block outgoing mail from this address if it doesn't exist but
250 * there's no easy alternative. Defaulting to admin_email might appear to be another
251 * option but some hosts may refuse to relay mail from an unknown domain. See
252 * https://core.trac.wordpress.org/ticket/5007.
253 */
254 }
255
256 $mail_headers .= 'Content-Type: ' . $this->get_content_type() . "\r\n" ;
257
258 $mail_headers = apply_filters( 'wpbm_email_api_get_headers_after', $mail_headers, $this->id, $this->fields_values , $this->replace, $additional_params );
259
260 return $mail_headers;
261 }
262
263
264 /** Get Content of Email
265 *
266 * @return string
267 */
268 public function get_content() {
269
270 if ( $this->get_email_content_type() == 'plain' ) {
271 $email_content = $this->esc_to_plain_text( $this->replace_shortcodes( $this->get_content_plain() ) );
272 } else {
273 $email_content = $this->replace_shortcodes( $this->get_content_html() );
274 }
275
276 $email_content = apply_filters( 'wpbm_email_api_get_content_after' , $email_content, $this->id, $this->get_email_content_type() );
277
278 // Remove all shortcodes, which is not replaced early.
279 $email_content = preg_replace ('/[\[\{][a-zA-Z0-9.,_-]{0,}[\]\}]/', '', $email_content);
280
281 return wordwrap( $email_content, 100 );
282 }
283
284
285 /** Get Email Content as Text (from Plain Text template)
286 *
287 * @return string
288 */
289 function get_content_plain() {
290
291 //require_once( dirname(__FILE__) . '/emails_tpl/standard-text-tpl.php' );
292 //require_once( dirname(__FILE__) . '/emails_tpl/plain-text-tpl.php' );
293 if ( isset( $this->fields_values['template_file'] ) ) {
294 $email_tpl_name = $this->fields_values['template_file'];
295 } else {
296 $email_tpl_name = 'plain';
297 }
298
299 if ( file_exists( dirname(__FILE__) . '/emails_tpl/'. $email_tpl_name .'-text-tpl.php' ) )
300 require_once( dirname(__FILE__) . '/emails_tpl/'. $email_tpl_name .'-text-tpl.php' );
301 else
302 require_once( dirname(__FILE__) . '/emails_tpl/plain-text-tpl.php' );
303
304 $fields_values = $this->fields_values;
305
306 $fields_values = apply_filters( 'wpbm_email_api_get_content_before' , $fields_values, $this->id , 'plain' ); //Ability to parse 'content', 'header_content', 'footer_content' for different languges, etc ....
307
308 if ( function_exists( 'wpbm_email_template_' . $email_tpl_name . '_text' ) )
309 $plain_email = call_user_func_array( 'wpbm_email_template_' . $email_tpl_name . '_text' , array( $fields_values ) );
310 else
311 $plain_email = wpbm_email_template_text( $fields_values );
312
313 // Replace <p> | <br> to \n
314 $plain_email = preg_replace( '/<(br|p)[\t\s]*[\/]?>/i', "\n", $plain_email );
315 // $plain_email = str_replace( array('<br/>', '<br>'), "\n", $plain_email );
316
317 return $plain_email;
318 }
319
320
321 /** Get Email Content as HTML (from HTML template)
322 *
323 * @return type
324 */
325 function get_content_html() {
326
327 // Load specific Email Template: ///////////////////////////////////
328 if ( isset( $this->fields_values['template_file'] ) ) {
329 $email_tpl_name = $this->fields_values['template_file'];
330 } else {
331 $email_tpl_name = 'plain';
332 }
333 //require_once( dirname(__FILE__) . '/emails_tpl/standard-html-tpl.php' );
334 //require_once( dirname(__FILE__) . '/emails_tpl/plain-html-tpl.php' );
335
336 if ( file_exists( dirname(__FILE__) . '/emails_tpl/'. $email_tpl_name .'-html-tpl.php' ) )
337 require_once( dirname(__FILE__) . '/emails_tpl/'. $email_tpl_name .'-html-tpl.php' );
338 else
339 require_once( dirname(__FILE__) . '/emails_tpl/plain-html-tpl.php' );
340 ////////////////////////////////////////////////////////////////////
341
342 $fields_values = $this->fields_values;
343
344 $fields_values = apply_filters( 'wpbm_email_api_get_content_before' , $fields_values, $this->id , 'html' ); //Ability to parse 'content', 'header_content', 'footer_content' for different languges, etc ....
345
346 if ( function_exists( 'wpbm_email_template_' . $email_tpl_name . '_html' ) )
347 $html_email = call_user_func_array( 'wpbm_email_template_' . $email_tpl_name . '_html' , array( $fields_values ) );
348 else
349 $html_email = wpbm_email_template_html( $fields_values );
350
351 return $html_email;
352 }
353
354
355 /** Get escaped Email Subject
356 *
357 * @return string
358 */
359 public function get_subject() {
360
361 $subject = $this->fields_values['subject'];
362
363 $subject = apply_filters( 'wpbm_email_api_get_subject_before' , $subject, $this->id );
364
365 $subject = $this->esc_to_plain_text( $this->replace_shortcodes( $subject ) );
366
367 $subject = apply_filters( 'wpbm_email_api_get_subject_after' , $subject, $this->id );
368
369 return $subject;
370 }
371
372
373 /** Get escapeed Name from any not supporting characters
374 *
375 * @return string
376 */
377 public function get_from__name() {
378 return wp_specialchars_decode( esc_html( stripslashes( $this->fields_values['from_name'] ) ), ENT_QUOTES );
379 }
380
381
382 /** Get sanitized Email address
383 *
384 * @return type
385 */
386 public function get_from__email_address() {
387 return sanitize_email( $this->fields_values['from'] );
388 }
389
390
391 /** For future support, right now does not support
392 *
393 * @return empty string
394 */
395 public function get_attachments() {
396 return '';
397 }
398
399
400 /** Check email and format it
401 *
402 * @param string $emails
403 * @return string
404 */
405 public function validate_emails( $emails ) {
406
407 $emails = str_replace(';', ',', $emails);
408
409 if ( !is_array( $emails ) )
410 $emails = explode( ',', $emails );
411
412 $emails_list = array();
413 foreach ( (array) $emails as $recipient ) {
414
415 // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
416 $recipient_name = '';
417 if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
418 if ( count( $matches ) == 3 ) {
419 $recipient_name = $matches[1];
420 $recipient = $matches[2];
421 }
422 } else {
423 // Check about correct format of email
424 if( preg_match( '/([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}/im', $recipient, $matches ) ) {
425 $recipient = $matches[0];
426 }
427 }
428
429 //$recipient_name = str_replace('"', '', $recipient_name);
430 $recipient_name = trim( wp_specialchars_decode( esc_html( stripslashes( $recipient_name ) ), ENT_QUOTES ) );
431
432 $emails_list[] = ( empty( $recipient_name ) ? '' : $recipient_name . ' ' )
433 . '<' . sanitize_email( $recipient ) . '>';
434 }
435
436 $emails_list = implode( ',', $emails_list );
437
438 return $emails_list;
439 }
440
441
442 /**
443 * Make Email Sending by using wp_mail standard function.
444 *
445 * @param string $to - Email
446 * @param array $replace - accociated array of values to replace in email Body and Subject. Exmaple: array( 'name' => 'Jo', 'secondname' => 'Smith' )
447 * @return boolean Sent or Failed to send.
448 */
449 public function send( $to = '', $replace = array() ) {
450
451 $return = false;
452
453 // if ( empty( $to ) )
454 // return $return;
455
456 $this->sending = true;
457
458
459 $this->set_replace( $replace );
460
461 $to = $this->validate_emails( $to );
462
463 $subject = $this->get_subject();
464 $message = $this->get_content();
465 $headers = $this->get_headers();
466 $attachments = $this->get_attachments();
467 //debuge('In email', htmlentities($to), $subject, htmlentities($message), $headers, $attachments) ;
468 $is_send_email = true;
469
470 $is_send_email = apply_filters( 'wpbm_email_api_is_allow_send', $is_send_email, $this->id, $this->fields_values );
471
472 if ( $is_send_email )
473 $return = wp_mail( $to, $subject, $message, $headers, $attachments );
474
475 $this->sending = false;
476
477 // Send Copy to admin email
478 if (
479 ( isset( $this->fields_values['copy_to_admin'] ) )
480 && ( $this->fields_values['copy_to_admin'] == 'On' )
481 ){
482
483 $this->sending = true;
484
485 $subject = __('Email copy to', 'booking-manager') . ' ' . str_replace( array( '<', '>' ), '', $to) . ' - ' . $subject;
486
487 $headers = $this->get_headers( array( 'reply' => $to ) );
488
489 $to = $this->validate_emails( $this->get_from__name() . ' <' . $this->get_from__email_address() . '> ' );
490
491 $is_send_email = apply_filters( 'wpbm_email_api_is_allow_send_copy', $is_send_email, $this->id, $this->fields_values );
492
493 if ( $is_send_email )
494 $return_copy = wp_mail( $to, $subject, $message, $headers, $attachments );
495
496 $this->sending = false;
497 }
498
499 $this->set_replace(); // Reset replace parameter
500
501 return $return;
502 }
503 // </editor-fold>
504
505
506 /** Show possible erros during sending emails.
507 *
508 * @param type $wp_error_object - new WP_Error( $e->getCode(), $e->getMessage(), $mail_error_data )
509 */
510 public function email_error_parse( $wp_error_object ) {
511 //debuge( $wp_error_object );
512 if ( $this->sending ) { // Check if this error generated for email relative to this class.
513
514 $error_description_array = array();
515 if ( isset( $wp_error_object->errors ) )
516 foreach ( $wp_error_object->errors as $key_error => $error_description ) {
517 $error_description_array[] = implode(' ', $error_description );
518 }
519
520 if ( ! empty( $error_description_array ) ) {
521 $error_description = implode(' ', $error_description_array ) ;
522
523 do_action('wpbm_email_sending_error', $wp_error_object, $error_description );
524
525 } else {
526 do_action('wpbm_email_sending_error', $wp_error_object, '' );
527 }
528 }
529 }
530
531 }
532
533