Files
ultimatemember/core/um-mail.php
T

113 lines
2.6 KiB
PHP
Raw Normal View History

2014-12-15 22:38:07 +02:00
<?php
class UM_Mail {
function __construct() {
2015-01-28 17:16:04 +02:00
add_filter('mandrill_nl2br', array(&$this, 'mandrill_nl2br') );
2016-01-31 11:52:44 -08:00
2015-04-07 20:10:23 +02:00
$this->force_plain_text = '';
2016-01-31 11:52:44 -08:00
2015-01-28 17:16:04 +02:00
}
2016-01-31 11:52:44 -08:00
2015-01-28 17:16:04 +02:00
/***
*** @mandrill compatibility
***/
2015-02-10 02:05:27 +02:00
function mandrill_nl2br($nl2br, $message = '') {
2016-01-31 11:52:44 -08:00
2015-01-28 17:16:04 +02:00
// text emails
2015-03-01 00:44:04 +02:00
if ( !um_get_option('email_html') ) {
$nl2br = true;
}
2016-01-31 11:52:44 -08:00
2015-01-28 17:16:04 +02:00
return $nl2br;
2014-12-15 22:38:07 +02:00
}
2016-01-31 11:52:44 -08:00
2015-03-01 00:44:04 +02:00
/***
*** @check If template exists
***/
2015-04-07 20:10:23 +02:00
function email_template( $template, $args = array() ) {
2015-11-05 19:51:31 +08:00
$lang = '';
2016-01-31 11:52:44 -08:00
2015-11-05 19:51:31 +08:00
if ( function_exists('icl_get_current_language') ) {
if ( icl_get_current_language() != 'en' ) {
$lang = icl_get_current_language() . '/';
}
2016-01-31 11:52:44 -08:00
} else {
$lang = get_locale();
2015-11-05 19:51:31 +08:00
}
2016-01-31 11:52:44 -08:00
2015-11-05 19:51:31 +08:00
if ( file_exists( get_stylesheet_directory() . '/ultimate-member/templates/email/' . $lang . $template . '.html' ) ) {
return get_stylesheet_directory() . '/ultimate-member/templates/email/' . $lang . $template . '.html';
2015-03-01 00:44:04 +02:00
}
2015-04-07 20:10:23 +02:00
if ( isset( $args['path'] ) ) {
2015-11-05 19:51:31 +08:00
$path = $args['path'] . $lang;
2015-04-07 20:10:23 +02:00
} else {
2015-11-05 19:51:31 +08:00
$path = um_path . 'templates/email/' . $lang;
2015-04-07 20:10:23 +02:00
}
2016-01-31 11:52:44 -08:00
2015-04-07 20:10:23 +02:00
if ( file_exists( $path . $template . '.html' ) ) {
return $path . $template . '.html';
2015-03-01 00:44:04 +02:00
}
2016-01-31 11:52:44 -08:00
2015-03-01 00:44:04 +02:00
return false;
2016-01-31 11:52:44 -08:00
2015-03-01 00:44:04 +02:00
}
2016-01-31 11:52:44 -08:00
2014-12-15 22:38:07 +02:00
/***
*** @sends an email to any user
***/
function send( $email, $template=null, $args = array() ) {
2016-01-31 11:52:44 -08:00
2014-12-15 22:38:07 +02:00
if ( !$template ) return;
if ( um_get_option( $template . '_on' ) != 1 ) return;
if ( !is_email( $email ) ) return;
2016-01-31 11:52:44 -08:00
2014-12-15 22:38:07 +02:00
$this->attachments = null;
$this->headers = 'From: '. um_get_option('mail_from') .' <'. um_get_option('mail_from_addr') .'>' . "\r\n";
$this->subject = um_get_option( $template . '_sub' );
2015-11-16 21:55:43 +08:00
$this->subject = um_convert_tags( $this->subject, $args );
2016-01-31 11:52:44 -08:00
2015-04-07 20:10:23 +02:00
if ( isset( $args['admin'] ) || isset( $args['plain_text'] ) ) {
$this->force_plain_text = 'forced';
}
2015-03-01 00:44:04 +02:00
2015-04-07 20:10:23 +02:00
// HTML e-mail or text
if ( um_get_option('email_html') && $this->email_template( $template, $args ) ) {
add_filter( 'wp_mail_content_type', array(&$this, 'set_content_type') );
$this->message = file_get_contents( $this->email_template( $template, $args ) );
2015-03-01 00:44:04 +02:00
} else {
$this->message = um_get_option( $template );
}
2016-01-31 11:52:44 -08:00
2015-03-01 00:44:04 +02:00
// Convert tags in body
2015-11-16 21:55:43 +08:00
$this->message = um_convert_tags( $this->message, $args );
2014-12-15 22:38:07 +02:00
2015-03-01 00:44:04 +02:00
// Send mail
2014-12-15 22:38:07 +02:00
wp_mail( $email, $this->subject, $this->message, $this->headers, $this->attachments );
2015-03-01 00:44:04 +02:00
remove_filter( 'wp_mail_content_type', array(&$this, 'set_content_type') );
2016-01-31 11:52:44 -08:00
2015-04-07 20:10:23 +02:00
// reset globals
$this->force_plain_text = '';
2016-01-31 11:52:44 -08:00
2015-03-01 00:44:04 +02:00
}
2016-01-31 11:52:44 -08:00
2015-03-01 00:44:04 +02:00
/***
*** @maybe sending HTML emails
***/
function set_content_type( $content_type ) {
2015-11-05 19:51:31 +08:00
if ( $this->force_plain_text == 'forced' )
return 'text/plain';
2016-01-31 11:52:44 -08:00
2015-11-05 19:51:31 +08:00
if ( um_get_option('email_html') )
return 'text/html';
2016-01-31 11:52:44 -08:00
2015-03-01 00:44:04 +02:00
return 'text/plain';
2014-12-15 22:38:07 +02:00
}
2016-01-31 11:52:44 -08:00
}