class-mail.php
4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Mail.
*
* @package Smush\Core
*/
namespace Smush\Core\Modules\Helpers;
defined( 'ABSPATH' ) || exit;
/**
* Class Mail
*/
abstract class Mail {
/**
* Identifier
*
* @var mixed
* @access protected
*/
protected $identifier;
/**
* Whitelabel class.
*
* @var Whitelabel
*/
protected $whitelabel;
/**
* Constructor.
*
* @param string $identifier Identifier.
*/
public function __construct( $identifier ) {
$this->identifier = $identifier;
$this->whitelabel = new WhiteLabel();
}
/**
* Get first blog admin.
*
* @return null|WP_User.
*/
private function get_blog_admin() {
$admins = get_users(
array(
'role' => 'administrator',
'orderby' => 'ID',
'order' => 'ASC',
'limit' => 1,
)
);
if ( ! empty( $admins ) ) {
return $admins[0];
}
}
/**
* Get user meta data of recipient.
*
* @return false|WP_User
*/
protected function get_recipient_meta() {
$admin_id = (int) apply_filters( $this->identifier . '_mail_admin_id', 0 );
if ( $admin_id > 0 ) {
$user_data = get_user_by( 'id', $admin_id );
if ( ! empty( $user_data ) ) {
return $user_data;
}
}
$blog_admin = get_user_by( 'email', get_option( 'admin_email' ) );
if ( empty( $blog_admin ) ) {
$blog_admin = $this->get_blog_admin();
}
return $blog_admin;
}
/**
* Get first name of recipient.
*/
public function get_recipient_name() {
$user_data = $this->get_recipient_meta();
if ( empty( $user_data ) ) {
return 'Sir';
}
return ! empty( $user_data->first_name ) ? $user_data->first_name : $user_data->display_name;
}
/**
* Get mail recipients.
*
* @return array
*/
public function get_mail_recipients() {
$recipients = (array) apply_filters( $this->identifier . '_get_mail_recipients', array() );
if ( ! empty( $recipients ) ) {
return $recipients;
}
$user_data = $this->get_recipient_meta();
if ( ! empty( $user_data->user_email ) ) {
$recipients[] = $user_data->user_email;
}
if ( empty( $recipients ) ) {
$recipients[] = get_option( 'admin_email' );
}
return $recipients;
}
/**
* Send an email.
*/
public function send_email() {
$mail_attributes = array(
'to' => $this->get_mail_recipients(),
'subject' => $this->get_mail_subject(),
'message' => $this->get_mail_message(),
'headers' => $this->get_mail_headers(),
);
$modified_attributes = apply_filters( $this->identifier . '_mail_attributes', $mail_attributes );
if ( is_array( $modified_attributes ) && $modified_attributes !== $mail_attributes ) {
$mail_attributes = wp_parse_args( $modified_attributes, $mail_attributes );
}
$sender_email_callback = array( $this, 'custom_sender_email' );
$sender_name_callback = array( $this, 'custom_sender_name' );
$priority = - 10; // Let other plugins take over
add_filter( 'wp_mail_from', $sender_email_callback, $priority );
add_filter( 'wp_mail_from_name', $sender_name_callback, $priority );
// Send email.
$sent = wp_mail( $mail_attributes['to'], $mail_attributes['subject'], $mail_attributes['message'], $mail_attributes['headers'] );
remove_filter( 'wp_mail_from', $sender_email_callback, $priority );
remove_filter( 'wp_mail_from_name', $sender_name_callback, $priority );
return $sent;
}
/**
* Get email header.
*
* @return array
*/
protected function get_mail_headers() {
return array( 'Content-Type: text/html; charset=UTF-8' );
}
/**
* Retrieve noreply email.
*
* @return string
*/
public function get_noreply_email() {
$noreply_email = apply_filters( $this->identifier . '_noreply_email', null );
if ( $noreply_email && filter_var( $noreply_email, FILTER_VALIDATE_EMAIL ) ) {
return $noreply_email;
}
// Get the site domain and get rid of www.
$sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
$noreply_email = 'noreply@';
if ( null !== $sitename ) {
if ( 'www.' === substr( $sitename, 0, 4 ) ) {
$sitename = substr( $sitename, 4 );
}
$noreply_email .= $sitename;
}
return $noreply_email;
}
/**
* Custom sender email.
*
* @return string
*/
public function custom_sender_email() {
return $this->get_noreply_email();
}
/**
* Custom sender name.
*
* @return string
*/
public function custom_sender_name() {
return $this->get_sender_name();
}
/**
* Get mail subject.
*
* @return string
*/
abstract protected function get_mail_message();
/**
* Get mail subject.
*
* @return string
*/
abstract protected function get_mail_subject();
/**
* Get sender name.
*
* @return string
*/
abstract protected function get_sender_name();
}