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