PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.74
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.74
1.2.74 1.2.73 1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 All 174 releases
← All changes | includes/class-emails.php +649 -323 1.0.111.2.74 View file →
@@ -8,389 +8,715 @@
8 8 * @author GeoDirectory Team <info@wpgeodirectory.com>
9 9 */
10 10 class UsersWP_Mails {
11 11
12 - /**
13 - * All UsersWP user mails happen via this method.
14 - *
15 - * @since 1.0.0
16 - * @package userswp
17 - * @param string $message_type Message type.
18 - * @param int $user_id User ID.
19 - * @return bool True when success. False when error.
20 - */
21 - public function send( $message_type, $user_id)
22 - {
23 - $user_data = get_userdata($user_id);
24 -
25 - $login_page_id = uwp_get_option('login_page', false);
26 - if ($login_page_id) {
27 - $login_page_url = get_permalink($login_page_id);
28 - } else {
29 - $login_page_url = wp_login_url();
30 - }
12 + /**
13 + * Constructor.
14 + */
15 + public function __construct() {
31 16
32 - $subject = "";
33 - $message = "";
34 -
35 - $extras = apply_filters('uwp_send_mail_extras', "", $message_type, $user_id);
36 - $subject = apply_filters('uwp_send_mail_subject', $subject, $message_type);
37 - $message = apply_filters('uwp_send_mail_message', $message, $message_type);
17 + add_action( 'uwp_email_header', array( $this, 'email_header' ), 10, 5 );
18 + add_action( 'uwp_email_footer', array( $this, 'email_footer' ), 10, 4 );
19 + add_action( 'wp_new_user_notification_email', array( $this, 'wp_new_user_notification_email' ), 10, 2 );
20 + add_action( 'wp_new_user_notification_email_admin', array( $this, 'wp_new_user_notification_email_admin' ), 10, 2 );
38 21
39 - $contains_login_details_tag = false;
40 - if (strpos($message, '[#login_details#]') !== false) {
41 - $contains_login_details_tag = true;
42 - }
43 -
44 - if (!empty($subject)) {
45 - $subject = __(stripslashes_deep($subject), 'userswp');
46 - }
22 + }
47 23
48 - if (!empty($message)) {
49 - $message = __(stripslashes_deep($message), 'userswp');
50 - }
24 + /**
25 + * Get the email logo.
26 + *
27 + * @since 1.2.1.2
28 + *
29 + * @return string Logo url.
30 + */
31 + public static function get_email_logo( $size = 'full' ) {
32 + $attachment_id = uwp_get_option( 'email_logo' );
51 33
52 - $sitefromEmail = get_option('admin_email');
53 - $sitefromEmailName = stripslashes(get_option('blogname'));
34 + $email_logo = '';
35 + if ( ! empty( $attachment_id ) ) {
36 + $email_logo = wp_get_attachment_image( $attachment_id, $size );
37 + }
54 38
39 + return apply_filters( 'uwp_get_email_logo', $email_logo, $attachment_id, $size );
40 + }
55 41
56 - $user_login = '';
57 - if ($user_id > 0 && $user_info = get_userdata($user_id)) {
58 - $user_login = $user_info->user_login;
59 - }
42 + /**
43 + * The default email footer text.
44 + *
45 + * @since 1.2.1.2
46 + *
47 + * @return string
48 + */
49 + public static function email_header_text(){
50 + $header_text = self::get_email_logo();
60 51
61 - $siteurl = home_url();
62 - $siteurl_link = '<a href="' . $siteurl . '">' . $siteurl . '</a>';
63 - $loginurl = $login_page_url;
64 - $loginurl_link = '<a href="' . $loginurl . '">login</a>';
52 + if ( empty( $header_text ) ) {
53 + $header_text = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
54 + }
65 55
66 - $current_date = date_i18n('Y-m-d H:i:s', current_time('timestamp'));
56 + return apply_filters( 'uwp_email_header_text', $header_text );
57 + }
67 58
68 - $site_email = get_option('admin_email');
59 + /**
60 + * Get the email header template.
61 + *
62 + * @since 1.2.1.2
63 + *
64 + * @param string $email_heading
65 + * @param string $email_name
66 + * @param array $email_vars
67 + * @param bool $plain_text
68 + * @param bool $sent_to_admin
69 + */
70 + public static function email_header( $email_heading = '', $email_name = '', $email_vars = array(), $plain_text = false, $sent_to_admin = false ) {
71 + if ( ! $plain_text ) {
72 + $header_text = self::email_header_text();
73 + $header_text = $header_text ? wpautop( wp_kses_post( wptexturize( $header_text ) ) ) : '';
69 74
70 - $site_name = stripslashes(get_option('blogname'));
75 + uwp_get_template( 'emails/uwp-email-header.php', array(
76 + 'email_heading' => $email_heading,
77 + 'email_name' => $email_name,
78 + 'email_vars' => $email_vars,
79 + 'plain_text' => $plain_text,
80 + 'header_text' => $header_text,
81 + 'sent_to_admin' => $sent_to_admin
82 + ) );
83 + }
84 + }
71 85
72 - //user
73 - $user_name = $user_data->display_name;
74 - $user_email = $user_data->user_email;
86 + /**
87 + * The default email footer text.
88 + *
89 + * @since 1.2.1.2
90 + * @return string
91 + */
92 + public static function email_footer_text(){
93 + $footer_text = uwp_get_option( 'email_footer_text' );
75 94
76 - $message_search_array = array(
77 - '[#site_name_url#]',
78 - '[#site_name#]',
79 - '[#to_name#]',
80 - '[#from_name#]',
81 - '[#login_url#]',
82 - '[#user_name#]',
83 - '[#from_email#]',
84 - '[#user_login#]',
85 - '[#username#]',
86 - '[#current_date#]',
87 - '[#login_details#]',
88 - );
89 - $message_replace_array = array(
90 - $siteurl_link,
91 - $sitefromEmailName,
92 - $user_name,
93 - $site_name,
94 - $loginurl_link,
95 - $user_name,
96 - $site_email,
97 - $user_login,
98 - $user_login,
99 - $current_date,
100 - $extras
101 - );
102 - $message = str_replace($message_search_array, $message_replace_array, $message);
95 + if ( empty( $footer_text ) ) {
96 + $footer_text = wp_sprintf( __( '%s - Powered by UsersWP', 'userswp' ), get_bloginfo( 'name', 'display' ) );
97 + }
103 98
99 + return apply_filters( 'uwp_email_footer_text', $footer_text );
100 + }
104 101
105 - // Applicable only for activate mails
106 - if ($message_type == 'activate' && !$contains_login_details_tag) {
107 - $user_data = get_userdata($user_id);
108 - global $wpdb;
109 - $key = wp_generate_password( 20, false );
110 - do_action( 'uwp_activation_key', $user_data->user_login, $key );
102 + /**
103 + * Get the email footer template.
104 + *
105 + * @since 1.2.1.2
106 + *
107 + * @param string $email_name
108 + * @param array $email_vars
109 + * @param bool $plain_text
110 + * @param bool $sent_to_admin
111 + */
112 + public static function email_footer( $email_name = '', $email_vars = array(), $plain_text = false, $sent_to_admin = false ) {
113 + if ( ! $plain_text ) {
114 + $footer_text = self::email_footer_text();
115 + $footer_text = $footer_text ? wpautop( wp_kses_post( wptexturize( $footer_text ) ) ) : '';
111 116
112 - global $wp_hasher;
113 - if ( empty( $wp_hasher ) ) {
114 - require_once ABSPATH . 'wp-includes/class-phpass.php';
115 - $wp_hasher = new PasswordHash( 8, true );
116 - }
117 - $hashed = $wp_hasher->HashPassword( $key );
118 - $wpdb->update( $wpdb->users, array( 'user_activation_key' => time().":".$hashed ), array( 'user_login' => $user_data->user_login ) );
119 - update_user_meta( $user_id, 'uwp_mod', 'email_unconfirmed' );
117 + uwp_get_template( 'emails/uwp-email-footer.php', array(
118 + 'email_name' => $email_name,
119 + 'email_vars' => $email_vars,
120 + 'email_heading' => '',
121 + 'plain_text' => $plain_text,
122 + 'footer_text' => $footer_text,
123 + 'sent_to_admin' => $sent_to_admin
124 + ) );
125 + }
126 + }
120 127
121 - $activation_link = add_query_arg(
122 - array(
123 - 'uwp_activate' => 'yes',
124 - 'key' => $key,
125 - 'login' => $user_data->user_login
126 - ),
127 - site_url()
128 - );
129 -
130 - $activate_message_search_array = array(
131 - '[#activation_link#]',
132 - );
133 - $activate_message_replace_array = array(
134 - $activation_link
135 - );
136 - $message = str_replace($activate_message_search_array, $activate_message_replace_array, $message);
137 - }
128 + /**
129 + * Get the email message wraped in the header and footer.
130 + *
131 + * @since 1.2.1.2
132 + *
133 + * @param string $message Message.
134 + * @param string $email_name Optional. Email name. Default null.
135 + * @param array $email_vars Optional. Email vars. Default array.
136 + * @param string $email_heading Optional. Email header. Default null.
137 + * @param bool $plain_text Optional. Plain text. Default false.
138 + * @param bool $sent_to_admin Optional. Send to admin. Default false.
139 + *
140 + * @return string $message.
141 + */
142 + public static function email_wrap_message( $message, $email_name = '', $email_vars = array(), $email_heading = '', $plain_text = false, $sent_to_admin = false ) {
143 + // Buffer
144 + ob_start();
138 145
139 - // Applicable only for forgot mails
140 - if ($message_type == 'forgot' && !$contains_login_details_tag) {
146 + if ( $plain_text ) {
147 + echo esc_attr( wp_strip_all_tags( $message ) );
148 + } else {
149 + do_action( 'uwp_email_header', $email_heading, $email_name, $email_vars, $plain_text, $sent_to_admin );
141 150
142 - $new_pass = "";
143 - $reset_link = "";
151 + echo wp_kses_post( wpautop( wptexturize( $message ) ) );
144 152
145 - global $wpdb, $wp_hasher;
153 + do_action( 'uwp_email_footer', $email_name, $email_vars, $plain_text, $sent_to_admin );
154 + }
146 155
147 - $allow = apply_filters('allow_password_reset', true, $user_data->ID);
148 - if ( ! $allow )
149 - return false;
150 - else if ( is_wp_error($allow) )
151 - return false;
156 + // Get contents
157 + $message = ob_get_clean();
152 158
153 - $as_password = apply_filters('uwp_forgot_message_as_password', false);
159 + return $message;
160 + }
154 161
155 - if ($as_password) {
156 - $new_pass = wp_generate_password(12, false);
157 - wp_set_password($new_pass, $user_data->ID);
158 - update_user_meta($user_data->ID, 'default_password_nag', true); //Set up the Password change nag.
162 + /**
163 + * Check if the email is enabled for the email type.
164 + *
165 + * @since 1.2.1.2
166 + *
167 + * @param string $email_name Email name.
168 + * @param string $default Optional. Default option value. Default null.
169 + * @param bool $is_admin Optional. Admin email. Default false.
170 + *
171 + * @return string
172 + */
173 + public static function is_email_enabled( $email_name, $default = '', $is_admin = false ) {
174 + if($is_admin){
175 + $key = $email_name . '_email_admin';
176 + } else {
177 + $key = $email_name . '_email';
178 + }
159 179
160 - } else {
161 - $key = wp_generate_password(20, false);
162 - do_action('retrieve_password_key', $user_data->user_login, $key);
180 + switch ( $email_name ) {
181 + // TODO add some cases
182 + default:
183 + $active = uwp_get_option( $key , $default);
184 + $active = $active === 'yes' || $active == '1' ? true : false;
185 + break;
186 + }
163 187
164 - if (empty($wp_hasher)) {
165 - require_once ABSPATH . 'wp-includes/class-phpass.php';
166 - $wp_hasher = new PasswordHash(8, true);
167 - }
168 - $hashed = $wp_hasher->HashPassword($key);
169 - $wpdb->update($wpdb->users, array('user_activation_key' => time() . ":" . $hashed), array('user_login' => $user_data->user_login));
170 - $reset_page = uwp_get_option('reset_page', false);
171 - if ($reset_page) {
172 - $reset_link = add_query_arg(array(
173 - 'key' => $key,
174 - 'login' => rawurlencode($user_data->user_login),
175 - ), get_permalink($reset_page));
176 - } else {
177 - $reset_link = site_url("reset?key=$key&login=" . rawurlencode($user_data->user_login), 'login');
178 - }
179 - }
188 + return apply_filters( 'uwp_email_is_enabled', $active, $email_name );
189 + }
180 190
181 - $reset_message_search_array = array(
182 - '[#new_password#]',
183 - '[#reset_link#]',
184 - );
185 - $reset_message_replace_array = array(
186 - $new_pass,
187 - $reset_link
188 - );
189 - $message = str_replace($reset_message_search_array, $reset_message_replace_array, $message);
190 - }
191 + /**
192 + * Get the email subject by type.
193 + *
194 + * @since 1.2.1.2
195 + *
196 + * @param string $email_name Optional. Email name. Default null.
197 + * @param array $email_vars Optional. Email vars. Default array.
198 + * @param bool $is_admin Optional. Admin email. Default false.
199 + *
200 + * @return string
201 + */
202 + public static function get_subject( $email_name = '', $email_vars = array(), $is_admin = false ) {
203 + if($is_admin){
204 + $key = $email_name . '_email_subject_admin';
205 + } else {
206 + $key = $email_name . '_email_subject';
207 + }
191 208
192 - $subject_search_array = array(
193 - '[#site_name_url#]',
194 - '[#site_name#]',
195 - '[#to_name#]',
196 - '[#from_name#]',
197 - '[#user_name#]',
198 - '[#from_email#]',
199 - '[#user_login#]',
200 - '[#username#]',
201 - '[#current_date#]'
202 - );
203 - $subject_replace_array = array(
204 - $siteurl_link,
205 - $sitefromEmailName,
206 - $user_name,
207 - $site_name,
208 - $user_name,
209 - $site_email,
210 - $user_login,
211 - $user_login,
212 - $current_date
213 - );
214 - $subject = str_replace($subject_search_array, $subject_replace_array, $subject);
209 + switch ( $email_name ) {
210 + // TODO some custom options
211 + default:
212 + $subject = uwp_get_option( $key );
213 + break;
215 214
216 - $headers = array();
217 - $headers[] = 'Content-type: text/html; charset=UTF-8';
218 - $headers[] = "Reply-To: " . $site_email;
219 - $headers[] = 'From: ' . $sitefromEmailName . ' <' . $sitefromEmail . '>';
215 + }
220 216
221 - $to = $user_email;
217 + // Get the default text is empty
218 + if(!$subject && method_exists('UsersWP_Defaults', $key)){
219 + $subject = UsersWP_Defaults::$key();
220 + }
222 221
223 - $to = apply_filters('uwp_send_email_to', $to, $message_type, $user_id);
222 + if ( !$subject ) {
223 + // Used to override subject for specific email type
224 + $subject = apply_filters( 'uwp_'.$key, $subject, $email_name, $email_vars );
225 + }
224 226
225 - $subject = apply_filters('uwp_send_email_subject', $subject, $message_type, $user_id);
227 + $subject = self::replace_variables( __( $subject, 'userswp' ), $email_name, $email_vars );
226 228
227 - $message = apply_filters('uwp_send_email_message', $message, $message_type, $user_id);
229 + return apply_filters( 'uwp_email_subject', $subject, $email_name, $email_vars );
230 + }
228 231
229 - $headers = apply_filters('uwp_send_email_headers', $headers, $message_type, $user_id);
232 + /**
233 + * Get the email content by type.
234 + *
235 + * @since 1.2.1.2
236 + *
237 + * @param string $email_name Optional. Email name. Default null.
238 + * @param array $email_vars Optional. Email vars. Default array.
239 + * @param bool $is_admin Optional. Admin email. Default false.
240 + *
241 + * @return string
242 + */
243 + public static function get_content( $email_name = '', $email_vars = array(), $is_admin = false ) {
244 + if($is_admin){
245 + $key = $email_name . '_email_content_admin';
246 + } else {
247 + $key = $email_name . '_email_content';
248 + }
230 249
231 - $sent = wp_mail($to, $subject, $message, $headers);
250 + switch ( $email_name ) {
251 + // TODO some custom options
252 + default:
253 + $content = uwp_get_option( $key );
254 + break;
255 + }
232 256
257 + // Get the default text is empty
258 + if(!$content && method_exists('UsersWP_Defaults', $key)){
259 + $content = UsersWP_Defaults::$key();
260 + }
233 261
234 - if (!$sent) {
235 - if (is_array($to)) {
236 - $to = implode(',', $to);
237 - }
238 - $err = $this->get_mail_errors();
239 - $log_message = sprintf(
240 - __("Email from UsersWP failed to send.\nMessage type: %s\nSend time: %s\nTo: %s\nSubject: %s\nError: %s\n\n", 'userswp'),
241 - $message_type,
242 - date_i18n('F j Y H:i:s', current_time('timestamp')),
243 - $to,
244 - $subject,
245 - $err
246 - );
247 - uwp_error_log($log_message);
248 - return false;
249 - } else {
250 - return true;
251 - }
252 - }
262 + if ( !$content ) {
263 + // Used to override content for specific email type
264 + $content = apply_filters( 'uwp_'.$key, $content, $email_name, $email_vars );
265 + }
253 266
254 - /**
255 - * All UsersWP admin mails happen via this method.
256 - *
257 - * @since 1.0.0
258 - * @package userswp
259 - * @param string $message_type Message type.
260 - * @param int $user_id User ID. Not admin user ID.
261 - * @return bool True when success. False when error.
262 - */
263 - public function send_admin_email( $message_type, $user_id)
264 - {
267 + $content = self::replace_variables( __( $content, 'userswp' ), $email_name, $email_vars );
265 268
266 - $login_page_id = uwp_get_option('login_page', false);
267 - if ($login_page_id) {
268 - $login_page_url = get_permalink($login_page_id);
269 - } else {
270 - $login_page_url = wp_login_url();
271 - }
269 + return apply_filters( 'uwp_email_content', $content, $email_name, $email_vars );
270 + }
272 271
273 - $extras = apply_filters('uwp_send_admin_mail_extras', "", $message_type, $user_id);
274 - $subject = apply_filters('uwp_send_admin_mail_subject', "", $message_type);
275 - $message = apply_filters('uwp_send_admin_mail_message', "", $message_type);
272 + /**
273 + * Replace variables in the email text.
274 + *
275 + * @since 1.2.1.2
276 + *
277 + * @param string $content Content.
278 + * @param string $email_name Optional. Email name. Default null.
279 + * @param array $email_vars Optional. Email vars. Default array.
280 + *
281 + * @return mixed
282 + */
283 + public static function replace_variables( $content, $email_name = '', $email_vars = array() ) {
284 + $site_url = home_url();
285 + $blogname = uwp_get_blogname();
286 + $login_url = wp_login_url( false ); // 'false' to prevent adding redirect_to to login url.
287 + $timestamp = current_time( 'timestamp' );
288 + $date = date_i18n( get_option( 'date_format' ), $timestamp );
289 + $time = date_i18n( get_option( 'time_format' ), $timestamp );
290 + $date_time = $date . ' ' . $time;
276 291
277 - if (!empty($subject)) {
278 - $subject = __(stripslashes_deep($subject), 'userswp');
279 - }
292 + $replace_array = array(
293 + '[#blogname#]' => $blogname,
294 + '[#site_url#]' => esc_url( $site_url ),
295 + '[#site_name_url#]' => '<a href="' . esc_url( $site_url ) . '">' . $site_url . '</a>',
296 + '[#site_link#]' => '<a href="' . esc_url( $site_url ) . '">' . $blogname . '</a>',
297 + '[#site_name#]' => esc_attr( $blogname ),
298 + '[#login_url#]' => esc_url( $login_url ),
299 + '[#login_link#]' => '<a href="' . esc_url( $login_url ) . '">' . __( 'Login', 'userswp' ) . '</a>',
300 + '[#current_date#]' => date_i18n( 'Y-m-d H:i:s', $timestamp ),
301 + '[#date#]' => $date,
302 + '[#time#]' => $time,
303 + '[#date_time#]' => $date_time,
304 + '[#from_name#]' => esc_attr( self::get_mail_from_name() ),
305 + '[#from_email#]' => sanitize_email( self::get_mail_from() ),
306 + );
280 307
281 - if (!empty($message)) {
282 - $message = __(stripslashes_deep($message), 'userswp');
283 - }
308 + $user_id = ! empty( $email_vars['user_id'] ) ? $email_vars['user_id'] : null;
309 + if ( !empty( $user_id ) && $user_id > 0 ) {
310 + $user_data = get_userdata($user_id);
311 + $profile_link = uwp_build_profile_tab_url($user_id);
312 + $replace_array = array_merge(
313 + $replace_array,
314 + array(
315 + '[#to_name#]' => esc_attr( $user_data->display_name ),
316 + '[#user_login#]' => esc_attr( $user_data->user_login ),
317 + '[#user_name#]' => esc_attr( $user_data->display_name ),
318 + '[#first_name#]' => esc_attr( $user_data->first_name ),
319 + '[#last_name#]' => esc_attr( $user_data->last_name ),
320 + '[#username#]' => esc_attr( $user_data->user_login ),
321 + '[#user_email#]' => sanitize_email( $user_data->user_email ),
322 + '[#profile_link#]' => esc_url( $profile_link ),
323 + )
324 + );
325 + }
284 326
285 - $sitefromEmail = apply_filters('uwp_send_mail_admin_email', get_option('admin_email'));
286 - $sitefromEmailName = apply_filters('uwp_send_mail_admin_blogname', stripslashes(get_option('blogname')));
327 + $replace_array = apply_filters( 'uwp_email_format_text', $replace_array, $content, $user_id );
287 328
288 - $siteurl = home_url();
289 - $siteurl_link = '<a href="' . $siteurl . '">' . $siteurl . '</a>';
290 - $loginurl = $login_page_url;
291 - $loginurl_link = '<a href="' . $loginurl . '">login</a>';
329 + foreach ( $email_vars as $key => $value ) {
330 + if ( is_scalar( $value ) ) {
331 + $replace_array[ '[#' . $key . '#]' ] = $value ;
332 + }
333 + }
292 334
293 - $current_date = date_i18n('Y-m-d H:i:s', current_time('timestamp'));
335 + $replace_array = apply_filters( 'uwp_email_wild_cards', $replace_array, $content, $email_name, $email_vars );
294 336
295 - $site_email = get_option('admin_email');
337 + foreach ( $replace_array as $key => $value ) {
338 + $content = str_replace( $key, $value, $content );
339 + }
296 340
297 - $site_name = stripslashes(get_option('blogname'));
341 + return apply_filters( 'uwp_email_content_replace', $content, $email_name, $email_vars );
342 + }
298 343
299 - $search_array = array(
300 - '[#site_name_url#]',
301 - '[#site_name#]',
302 - '[#from_name#]',
303 - '[#login_url#]',
304 - '[#from_email#]',
305 - '[#current_date#]',
306 - '[#extras#]',
307 - );
308 - $replace_array = array(
309 - $siteurl_link,
310 - $sitefromEmailName,
311 - $site_name,
312 - $loginurl_link,
313 - $site_email,
314 - $current_date,
315 - $extras
316 - );
317 - $message = str_replace($search_array, $replace_array, $message);
344 + /**
345 + * Returns email headers
346 + *
347 + * @since 1.2.1.2
348 + *
349 + * @param string $email_name Email name.
350 + * @param array $email_vars Optional. Email vars. Default array.
351 + * @param string $from_email Optional. From email. Default null.
352 + * @param string $from_name Optional. From name. Default null.
353 + *
354 + * @return string
355 + */
356 + public static function get_headers( $email_name, $email_vars = array(), $from_email = '', $from_name = '' ) {
357 + $from_email = ! empty( $from_email ) ? $from_email : self::get_mail_from();
358 + $from_name = ! empty( $from_name ) ? $from_name : self::get_mail_from_name();
359 + $reply_to = ! empty( $email_vars['reply_to'] ) ? $email_vars['reply_to'] : $from_email;
318 360
319 - $search_array = array(
320 - '[#site_name_url#]',
321 - '[#site_name#]',
322 - '[#from_name#]',
323 - '[#from_email#]',
324 - '[#current_date#]'
325 - );
326 - $replace_array = array(
327 - $siteurl_link,
328 - $sitefromEmailName,
329 - $site_name,
330 - $site_email,
331 - $current_date
332 - );
333 - $subject = str_replace($search_array, $replace_array, $subject);
361 + $headers = "From: " . stripslashes_deep( html_entity_decode( $from_name, ENT_COMPAT, 'UTF-8' ) ) . " <$from_email>\r\n";
362 + $headers .= "Reply-To: " . $reply_to . "\r\n";
363 + $headers .= "Content-Type: " . self::get_content_type() . "; charset=\"" . get_option( 'blog_charset' ) . "\"\r\n";
334 364
335 - $headers = array();
336 - $headers[] = 'Content-type: text/html; charset=UTF-8';
337 - $headers[] = "Reply-To: " . $site_email;
338 - $headers[] = 'From: ' . $sitefromEmailName . ' <' . $sitefromEmail . '>';
365 + return apply_filters( 'uwp_email_headers', $headers, $email_name, $email_vars, $from_email, $from_name );
366 + }
339 367
340 - $to = $site_email;
368 + /**
369 + * Get the from name for outgoing emails.
370 + *
371 + * @since 1.2.1.2
372 + *
373 + * @return string Site name.
374 + */
375 + public static function get_mail_from_name() {
376 + $mail_from_name = uwp_get_option( 'email_name' );
341 377
342 - $to = apply_filters('uwp_send_admin_email_to', $to, $message_type, $user_id);
378 + if ( ! $mail_from_name ) {
379 + $mail_from_name = get_bloginfo('name');
380 + }
343 381
344 - $subject = apply_filters('uwp_send_admin_email_subject', $subject, $message_type, $user_id);
382 + return apply_filters( 'uwp_get_mail_from_name', stripslashes( $mail_from_name ) );
383 + }
345 384
346 - $message = apply_filters('uwp_send_admin_email_message', $message, $message_type, $user_id);
385 + /**
386 + * Get the from address for outgoing emails.
387 + *
388 + * @since 1.2.1.2
389 + *
390 + * @return string The email ID.
391 + */
392 + public static function get_mail_from() {
393 + $mail_from = uwp_get_option( 'email_address' );
347 394
348 - $headers = apply_filters('uwp_send_admin_email_headers', $headers, $message_type, $user_id);
395 + if ( ! $mail_from ) {
396 + $sitename = strtolower( $_SERVER['SERVER_NAME'] );
397 + if ( substr( $sitename, 0, 4 ) == 'www.' ) {
398 + $sitename = substr( $sitename, 4 );
399 + }
349 400
350 - $sent = wp_mail($to, $subject, $message, $headers);
401 + $mail_from = 'wordpress@' . $sitename;
402 + }
351 403
352 - if (!$sent) {
353 - if (is_array($to)) {
354 - $to = implode(',', $to);
355 - }
356 - $err = $this->get_mail_errors();
357 - $log_message = sprintf(
358 - __("Email from UsersWP failed to send.\nMessage type: %s\nSend time: %s\nTo: %s\nSubject: %s\nError: %s\n\n", 'userswp'),
359 - $message_type,
360 - date_i18n('F j Y H:i:s', current_time('timestamp')),
361 - $to,
362 - $subject,
363 - $err
364 - );
365 - uwp_error_log($log_message);
366 - return false;
367 - } else {
368 - return true;
369 - }
370 - }
404 + return apply_filters( 'uwp_get_mail_from', $mail_from );
405 + }
371 406
407 + /**
408 + * Get the content type of the email html or plain.
409 + *
410 + * @since 1.2.1.2
411 + *
412 + * @param string $content_type Optional. Content type. Default text/html.
413 + * @param string $email_type Optional. Email type. Default null.
414 + *
415 + * @return string $content_type
416 + */
417 + public static function get_content_type( $content_type = 'text/html', $email_type = '' ) {
418 + if ( empty( $email_type ) ) {
419 + $email_type = self::get_email_type();
420 + }
372 421
373 - /**
374 - * Gets the original error message from phpmailer.
375 - *
376 - * @since 1.0.7
377 - * @package userswp
378 - * @return string Error messages.
379 - */
380 - public function get_mail_errors() {
381 - // wp mail debugging
382 - global $ts_mail_errors;
383 - global $phpmailer;
422 + switch ( $email_type ) {
423 + case 'plain' :
424 + $content_type = 'text/plain';
425 + break;
426 + case 'multipart' :
427 + $content_type = 'multipart/alternative';
428 + break;
429 + }
384 430
385 - if (!isset($ts_mail_errors)) $ts_mail_errors = array();
431 + return $content_type;
432 + }
386 433
387 - if (isset($phpmailer)) {
388 - $ts_mail_errors[] = $phpmailer->ErrorInfo;
389 - }
434 + /**
435 + * Get the email type from settings, html/plain.
436 + *
437 + * @since 1.2.1.2
438 + *
439 + * @return string
440 + */
441 + public static function get_email_type() {
442 + $email_type = uwp_get_option( 'email_type' );
390 443
391 - $out = json_encode($ts_mail_errors);
444 + if ( empty( $email_type ) ) {
445 + $email_type = 'html';
446 + }
392 447
393 - return $out;
394 - }
448 + return apply_filters( 'uwp_get_email_type', $email_type );
449 + }
395 450
396 -}
451 + /**
452 + * Get the email attachments per type.
453 + *
454 + * @since 1.2.1.2
455 + *
456 + * @param string $email_name Optional. Email name. Default null.
457 + * @param array $email_vars Optional. Email vars. Default array.
458 + * @param bool $is_admin Optional. Admin email. Default false.
459 + *
460 + * @return array $attachments
461 + */
462 + public static function get_attachments( $email_name = '', $email_vars = array(), $is_admin = false ) {
463 + $attachments = array();
464 +
465 + return apply_filters( 'uwp_email_attachments', $attachments, $email_name, $email_vars, $is_admin );
466 + }
467 +
468 + /**
469 + * Style the body of the email content.
470 + *
471 + * @since 1.2.1.2
472 + *
473 + * @param string $content Email content.
474 + * @param string $email_name Optional. Email name. Default null.
475 + * @param array $email_vars Optional. Email vars. Default array.
476 + *
477 + * @return string $content.
478 + */
479 + public static function style_body( $content, $email_name = '', $email_vars = array() ) {
480 + // make sure we only inline CSS for html emails
481 + if ( in_array( self::get_email_type(), array( 'html', 'multipart' ) ) && class_exists( 'DOMDocument' ) ) {
482 + // include css inliner
483 + if ( ! class_exists( 'Emogrifier' ) ) {
484 + include_once( USERSWP_PATH . 'includes/libraries/class-emogrifier.php' );
485 + }
486 +
487 + ob_start();
488 + uwp_get_template( 'emails/uwp-email-styles.php', array(
489 + 'email_name' => $email_name,
490 + 'email_vars' => $email_vars
491 + ) );
492 + $css = apply_filters( 'uwp_email_styles', ob_get_clean() );
493 +
494 + // apply CSS styles inline for picky email clients
495 + try {
496 + $emogrifier = new Emogrifier( $content, $css );
497 + $content = $emogrifier->emogrify();
498 + } catch ( Exception $e ) {
499 + uwp_error_log( $e->getMessage() );
500 + }
501 + }
502 +
503 + return $content;
504 + }
505 +
506 + /**
507 + * Function to send email.
508 + *
509 + * @since 1.2.1.3
510 + *
511 + * @param string $to To email address.
512 + * @param string $subject Email subject.
513 + * @param string $message Email message.
514 + * @param string $headers Email Headers.
515 + * @param array $attachments Optional. Email attachments. Default array.
516 + *
517 + * @return bool
518 + */
519 + public static function uwp_mail( $to, $subject, $message, $headers, $attachments = array() ) {
520 + add_filter( 'wp_mail_from', array( __CLASS__, 'get_mail_from' ) );
521 + add_filter( 'wp_mail_from_name', array( __CLASS__, 'get_mail_from_name' ) );
522 + add_filter( 'wp_mail_content_type', array( __CLASS__, 'get_content_type' ) );
523 +
524 + $message = self::style_body( $message );
525 + $message = apply_filters( 'uwp_mail_content', $message);
526 +
527 + $sent = wp_mail( $to, $subject, $message, $headers, $attachments );
528 +
529 + if ( ! $sent ) {
530 + $log_message = wp_sprintf( __( "Email from UsersWP failed to send.\nTime: %s\nTo: %s\nSubject: %s\nError: %s\n\n", 'userswp' ), date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ), ( is_array( $to ) ? implode( ', ', $to ) : $to ), $subject );
531 + uwp_error_log( $log_message);
532 + }
533 +
534 + remove_filter( 'wp_mail_from', array( __CLASS__, 'get_mail_from' ) );
535 + remove_filter( 'wp_mail_from_name', array( __CLASS__, 'get_mail_from_name' ) );
536 + remove_filter( 'wp_mail_content_type', array( __CLASS__, 'get_content_type' ) );
537 +
538 + return $sent;
539 + }
540 +
541 + /**
542 + * Function to send email for default UWP emails.
543 + *
544 + * @since 1.2.1.3
545 + *
546 + * @param string $to To email address.
547 + * @param string $email_name Email name.
548 + * @param array $email_vars Optional. Email vars. Default null.
549 + * @param bool $is_admin Optional. Admin email. Default false.
550 + *
551 + * @return bool
552 + */
553 + public static function send( $to, $email_name, $email_vars = array(), $is_admin = false ) {
554 +
555 + if ( !self::is_email_enabled( $email_name, '', $is_admin ) ) {
556 + return false;
557 + }
558 +
559 + $to = apply_filters( 'uwp_send_email_to', $to, $email_name, $email_vars, $is_admin );
560 +
561 + if(empty($to)){
562 + return false;
563 + }
564 +
565 + // Switch language to user language.
566 + do_action( 'wpml_switch_language_for_email', $to );
567 +
568 + $subject = self::get_subject( $email_name, $email_vars, $is_admin );
569 + $message_body = self::get_content( $email_name, $email_vars, $is_admin );
570 + $headers = self::get_headers( $email_name, $email_vars );
571 + $attachments = self::get_attachments( $email_name, $email_vars, $is_admin );
572 +
573 + $plain_text = self::get_email_type() != 'html' ? true : false;
574 + $template = $plain_text ? 'emails/plain/uwp-email-' . $email_name . '.php' : 'emails/uwp-email-' . $email_name . '.php';
575 +
576 + $content = uwp_get_template_html( $template, array(
577 + 'email_name' => $email_name,
578 + 'email_vars' => $email_vars,
579 + 'email_heading' => '',
580 + 'sent_to_admin' => $is_admin,
581 + 'plain_text' => $plain_text,
582 + 'message_body' => $message_body,
583 + ) );
584 +
585 + do_action( 'uwp_before_'.$email_name.'_email', $email_name, $email_vars );
586 +
587 + $sent = self::uwp_mail($to, $subject, $content, $headers, $attachments);
588 +
589 + do_action( 'uwp_after_'.$email_name.'_email', $email_name, $email_vars );
590 +
591 + // Switch language back.
592 + do_action( 'wpml_restore_language_from_email' );
593 +
594 + return $sent;
595 + }
596 +
597 + public function wp_new_user_notification_email($wp_new_user_notification_email, $user){
598 +
599 + $email_name = 'wp_new_user_notification';
600 + $is_admin = false;
601 + if ( !self::is_email_enabled( $email_name, '', $is_admin ) ) {
602 + return $wp_new_user_notification_email;
603 + }
604 +
605 + $key = get_password_reset_key( $user );
606 + if ( is_wp_error( $key ) ) {
607 + return $wp_new_user_notification_email;
608 + }
609 +
610 + $email_vars = array(
611 + 'user_id' => $user->ID
612 + );
613 +
614 + $to = apply_filters( 'uwp_send_email_to', $user->user_email, $email_name, $email_vars, $is_admin);
615 +
616 + // Switch language to user language.
617 + do_action( 'wpml_switch_language_for_email', $to );
618 +
619 + $subject = self::get_subject( $email_name, $email_vars, $is_admin );
620 + $subject = !empty($subject) ? $subject : UsersWP_Defaults::wp_new_user_notification_email_subject();
621 + $headers = self::get_headers( $email_name, $email_vars );
622 + $message = self::get_content( $email_name, $email_vars, $is_admin );
623 + $message = !empty($message) ? $message : UsersWP_Defaults::wp_new_user_notification_email_content();
624 +
625 + $reset_page = uwp_get_page_id( 'reset_page', false );
626 + if ( $reset_page ) {
627 + $reset_link = add_query_arg( array(
628 + 'key' => $key,
629 + 'login' => rawurlencode( $user->user_login ),
630 + ), get_permalink( $reset_page ) );
631 + $reset_link = "<a href='" . $reset_link . "' target='_blank'>" . $reset_link . "</a>";
632 + } else {
633 + $reset_link = home_url( "reset?key=$key&login=" . rawurlencode( $user->user_login ), 'login' );
634 + $reset_link = "<a href='" .$reset_link. "' target='_blank'>" . $reset_link . "</a>";
635 + }
636 +
637 + $message = str_replace( '[#reset_link#]', $reset_link, $message );
638 +
639 + $plain_text = self::get_email_type() != 'html' ? true : false;
640 + $template = $plain_text ? 'emails/plain/uwp-email-' . $email_name . '.php' : 'emails/uwp-email-' . $email_name . '.php';
641 +
642 + $content = uwp_get_template_html( $template, array(
643 + 'email_name' => $email_name,
644 + 'email_vars' => $email_vars,
645 + 'email_heading' => '',
646 + 'sent_to_admin' => $is_admin,
647 + 'plain_text' => $plain_text,
648 + 'message_body' => $message,
649 + ) );
650 +
651 + $content = self::style_body( $content );
652 + $content = apply_filters( 'uwp_mail_content', $content);
653 +
654 + // Switch language back.
655 + do_action( 'wpml_restore_language_from_email' );
656 +
657 + $wp_new_user_notification_email = array(
658 + 'to' => $to,
659 + 'subject' => $subject,
660 + 'message' => $content,
661 + 'headers' => $headers,
662 + );
663 +
664 + return $wp_new_user_notification_email;
665 +
666 + }
667 +
668 + public function wp_new_user_notification_email_admin($wp_new_user_notification_email_admin, $user){
669 +
670 + $email_name = 'wp_new_user_notification';
671 + $is_admin = true;
672 + if ( !self::is_email_enabled( $email_name, '', $is_admin ) ) {
673 + return $wp_new_user_notification_email_admin;
674 + }
675 +
676 + $email_vars = array(
677 + 'user_id' => $user->ID
678 + );
679 +
680 + $to = apply_filters( 'uwp_send_email_to', get_option( 'admin_email' ), $email_name, $email_vars, $is_admin);
681 +
682 + // Switch language to user language.
683 + do_action( 'wpml_switch_language_for_email', $to );
684 +
685 + $subject = self::get_subject( $email_name, $email_vars, $is_admin );
686 + $subject = !empty($subject) ? $subject : UsersWP_Defaults::wp_new_user_notification_email_subject_admin();
687 + $headers = self::get_headers( $email_name, $email_vars );
688 + $message = self::get_content( $email_name, $email_vars, $is_admin );
689 + $message = !empty($message) ? $message : UsersWP_Defaults::wp_new_user_notification_email_content_admin();
690 +
691 + $plain_text = self::get_email_type() != 'html' ? true : false;
692 + $template = $plain_text ? 'emails/plain/uwp-email-' . $email_name . '.php' : 'emails/uwp-email-' . $email_name . '.php';
693 +
694 + $content = uwp_get_template_html( $template, array(
695 + 'email_name' => $email_name,
696 + 'email_vars' => $email_vars,
697 + 'email_heading' => '',
698 + 'sent_to_admin' => $is_admin,
699 + 'plain_text' => $plain_text,
700 + 'message_body' => $message,
701 + ) );
702 +
703 + $content = self::style_body( $content );
704 + $content = apply_filters( 'uwp_mail_content', $content);
705 +
706 + // Switch language back.
707 + do_action( 'wpml_restore_language_from_email' );
708 +
709 + $wp_new_user_notification_email = array(
710 + 'to' => $to,
711 + 'subject' => $subject,
712 + 'message' => $content,
713 + 'headers' => $headers,
714 + );
715 +
716 + return $wp_new_user_notification_email;
717 +
718 + }
719 +
720 +}
721 +
722 +new UsersWP_Mails();