Files
ultimatemember/core/um-mail.php
T

160 lines
3.8 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') );
2015-04-07 20:10:23 +02:00
$this->force_plain_text = '';
2015-01-28 17:16:04 +02:00
}
2014-12-15 22:38:07 +02:00
2015-01-28 17:16:04 +02:00
/***
*** @mandrill compatibility
***/
2015-02-10 02:05:27 +02:00
function mandrill_nl2br($nl2br, $message = '') {
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;
}
2015-01-28 17:16:04 +02:00
return $nl2br;
2014-12-15 22:38:07 +02: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-03-01 00:44:04 +02:00
if ( file_exists( get_stylesheet_directory() . '/ultimate-member/templates/email/' . $template . '.html' ) ) {
return get_stylesheet_directory() . '/ultimate-member/templates/email/' . $template . '.html';
}
2015-04-07 20:10:23 +02:00
if ( isset( $args['path'] ) ) {
$path = $args['path'];
} else {
$path = um_path . 'templates/email/';
}
if ( file_exists( $path . $template . '.html' ) ) {
return $path . $template . '.html';
2015-03-01 00:44:04 +02:00
}
2015-04-07 20:10:23 +02:00
2015-03-01 00:44:04 +02:00
return false;
2015-04-07 20:10:23 +02:00
2015-03-01 00:44:04 +02:00
}
2014-12-15 22:38:07 +02:00
/***
*** @sends an email to any user
***/
function send( $email, $template=null, $args = array() ) {
if ( !$template ) return;
if ( um_get_option( $template . '_on' ) != 1 ) return;
if ( !is_email( $email ) ) return;
$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-04-07 20:10:23 +02:00
$this->subject = $this->convert_tags( $this->subject, $args );
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 );
}
2014-12-15 22:38:07 +02:00
2015-03-01 00:44:04 +02:00
// Convert tags in body
2015-04-07 20:10:23 +02:00
$this->message = $this->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') );
2015-04-07 20:10:23 +02:00
// reset globals
$this->force_plain_text = '';
2015-03-01 00:44:04 +02:00
}
/***
*** @maybe sending HTML emails
***/
function set_content_type( $content_type ) {
2015-04-07 20:10:23 +02:00
if ( $this->force_plain_text == 'forced' ) return 'text/plain';
if ( um_get_option('email_html') ) return 'text/html';
2015-03-01 00:44:04 +02:00
return 'text/plain';
2015-04-07 20:10:23 +02:00
2014-12-15 22:38:07 +02:00
}
/***
*** @convert template tags in email template
***/
2015-04-07 20:10:23 +02:00
function convert_tags( $content, $args = array() ) {
2014-12-15 22:38:07 +02:00
$search = array(
'{display_name}',
2015-02-01 01:30:04 +02:00
'{first_name}',
'{last_name}',
'{gender}',
2015-01-22 02:10:26 +02:00
'{username}',
'{email}',
'{password}',
'{login_url}',
2014-12-15 22:38:07 +02:00
'{site_name}',
2015-03-01 00:44:04 +02:00
'{site_url}',
2014-12-15 22:38:07 +02:00
'{account_activation_link}',
2014-12-22 01:45:24 +02:00
'{password_reset_link}',
2014-12-15 22:38:07 +02:00
'{admin_email}',
'{user_profile_link}',
2015-03-01 00:44:04 +02:00
'{user_account_link}',
2015-01-22 02:10:26 +02:00
'{submitted_registration}',
2015-03-01 00:44:04 +02:00
'{user_avatar_url}',
2014-12-15 22:38:07 +02:00
);
$search = apply_filters('um_template_tags_patterns_hook', $search);
$replace = array(
um_user('display_name'),
2015-02-01 01:30:04 +02:00
um_user('first_name'),
um_user('last_name'),
um_user('gender'),
2015-01-22 02:10:26 +02:00
um_user('user_login'),
um_user('user_email'),
um_user('_um_cool_but_hard_to_guess_plain_pw'),
um_get_core_page('login'),
2014-12-15 22:38:07 +02:00
um_get_option('site_name'),
2015-03-01 00:44:04 +02:00
get_bloginfo('url'),
2014-12-15 22:38:07 +02:00
um_user('account_activation_link'),
2014-12-22 01:45:24 +02:00
um_user('password_reset_link'),
2014-12-15 22:38:07 +02:00
um_admin_email(),
um_user_profile_url(),
2015-03-01 00:44:04 +02:00
um_get_core_page('account'),
2015-01-22 02:10:26 +02:00
um_user_submitted_registration(),
2015-03-01 00:44:04 +02:00
um_get_user_avatar_url(),
2014-12-15 22:38:07 +02:00
);
$replace = apply_filters('um_template_tags_replaces_hook', $replace);
$content = str_replace($search, $replace, $content);
2015-04-07 20:10:23 +02:00
if ( isset( $args['tags'] ) && isset( $args['tags_replace'] ) ) {
$content = str_replace($args['tags'], $args['tags_replace'], $content);
}
2014-12-15 22:38:07 +02:00
return $content;
}
}