1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HM\BackUpWordPress; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Email notifications for backups |
7
|
|
|
* |
8
|
|
|
* @extends Service |
9
|
|
|
*/ |
10
|
|
|
class Email_Service extends Service { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Human readable name for this service |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
public $name = 'Email'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Output the email form field |
20
|
|
|
* |
21
|
|
|
* @access public |
22
|
|
|
*/ |
23
|
|
|
public function field() { |
24
|
|
|
|
25
|
|
|
?> |
26
|
|
|
|
27
|
|
|
<tr> |
28
|
|
|
|
29
|
|
|
<th scope="row"> |
30
|
|
|
<label for="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>"><?php esc_html_e( 'Email notification', 'backupwordpress' ); ?></label> |
31
|
|
|
</th> |
32
|
|
|
|
33
|
|
|
<td> |
34
|
|
|
<input type="text" id="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>" value="<?php echo esc_attr( $this->get_field_value( 'email' ) ); ?>" placeholder="[email protected]" /> |
35
|
|
|
|
36
|
|
|
<p class="description"><?php printf( esc_html__( 'Receive a notification email when a backup completes. If the backup is small enough (< %s), then it will be attached to the email. Separate multiple email addresses with a comma.', 'backupwordpress' ), '<code>' . size_format( get_max_attachment_size() ) . '</code>' ); ?></p> |
37
|
|
|
</td> |
38
|
|
|
|
39
|
|
|
</tr> |
40
|
|
|
|
41
|
|
|
<?php } |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Not used as we only need a field |
45
|
|
|
* |
46
|
|
|
* @see field |
47
|
|
|
* @return string Empty string |
48
|
|
|
*/ |
49
|
|
|
public function form() { |
50
|
|
|
return ''; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function constant() { |
54
|
|
|
|
55
|
|
|
?> |
56
|
|
|
|
57
|
|
|
<tr<?php if ( defined( 'HMBKP_ATTACHMENT_MAX_FILESIZE' ) ) { ?> class="hmbkp_active"<?php } ?>> |
58
|
|
|
|
59
|
|
|
<td><code>HMBKP_ATTACHMENT_MAX_FILESIZE</code></td> |
60
|
|
|
|
61
|
|
|
<td> |
62
|
|
|
|
63
|
|
View Code Duplication |
<?php if ( defined( 'HMBKP_ATTACHMENT_MAX_FILESIZE' ) ) { ?> |
64
|
|
|
<p><?php printf( |
65
|
|
|
/* translators: Constant value specified in wp-config.php */ |
66
|
|
|
esc_html__( 'You\'ve set it to: %s', 'backupwordpress' ), |
67
|
|
|
'<code>' . esc_html( HMBKP_ATTACHMENT_MAX_FILESIZE ) . '</code>' |
68
|
|
|
); ?></p> |
69
|
|
|
<?php } ?> |
70
|
|
|
|
71
|
|
|
<p><?php printf( |
72
|
|
|
/* translators: 1: Default value 2: Code example of how to specify the constant in wp-config.php */ |
73
|
|
|
esc_html__( 'The maximum file size of your backup that will be attached to your notification emails. Defaults to %1$s. e.g. %2$s', 'backupwordpress' ), |
74
|
|
|
'<code>10MB</code>', |
75
|
|
|
"<code>define( 'HMBKP_ATTACHMENT_MAX_FILESIZE', '25MB' );</code>" |
76
|
|
|
); ?></p> |
77
|
|
|
|
78
|
|
|
</td> |
79
|
|
|
|
80
|
|
|
</tr> |
81
|
|
|
|
82
|
|
|
<?php } |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The sentence fragment that is output as part of the schedule sentence |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function display() { |
90
|
|
|
|
91
|
|
|
if ( $emails = $this->get_email_address_array() ) { |
92
|
|
|
|
93
|
|
|
$email = '<code>' . implode( '</code>, <code>', array_map( 'esc_html', $emails ) ) . '</code>'; |
94
|
|
|
|
95
|
|
|
return sprintf( |
96
|
|
|
/* translators: List of email addresses */ |
97
|
|
|
esc_html__( 'Send an email notification to %s', 'backupwordpress' ), |
98
|
|
|
$email |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return ''; |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Used to determine if the service is in use or not |
109
|
|
|
*/ |
110
|
|
|
public function is_service_active() { |
111
|
|
|
return (bool) $this->get_email_address_array(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Validate the email and return an error if validation fails |
116
|
|
|
* |
117
|
|
|
* @param array &$new_data Array of new data, passed by reference |
118
|
|
|
* @param array $old_data The data we are replacing |
119
|
|
|
* @return null|array Null on success, array of errors if validation failed |
|
|
|
|
120
|
|
|
*/ |
121
|
|
|
public function update( &$new_data, $old_data ) { |
122
|
|
|
|
123
|
|
|
$errors = array(); |
124
|
|
|
|
125
|
|
|
if ( isset( $new_data['email'] ) ) { |
126
|
|
|
|
127
|
|
|
if ( ! empty( $new_data['email'] ) ) { |
128
|
|
|
|
129
|
|
|
foreach ( explode( ',', $new_data['email'] ) as $email ) { |
130
|
|
|
|
131
|
|
|
$email = trim( $email ); |
132
|
|
|
|
133
|
|
|
if ( ! is_email( $email ) ) { |
134
|
|
|
$errors['email'] = sprintf( esc_html__( '%s isn\'t a valid email', 'backupwordpress' ), $email ); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ( ! empty( $errors['email'] ) ) { |
140
|
|
|
$new_data['email'] = ''; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $errors; |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get an array or validated email address's |
150
|
|
|
* @return array An array of validated email address's |
151
|
|
|
*/ |
152
|
|
|
private function get_email_address_array() { |
153
|
|
|
$emails = array_map( 'trim', explode( ',', $this->get_field_value( 'email' ) ) ); |
154
|
|
|
return array_filter( array_unique( $emails ), 'is_email' ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Fire the email notification on the hmbkp_backup_complete |
159
|
|
|
* |
160
|
|
|
* @see Backup::do_action |
161
|
|
|
* @param string $action The action received from the backup |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function action( $action, Backup $backup ) { |
165
|
|
|
|
166
|
|
|
if ( 'hmbkp_backup_complete' === $action && $this->get_email_address_array() ) { |
167
|
|
|
|
168
|
|
|
$file = $backup->get_backup_filepath(); |
169
|
|
|
|
170
|
|
|
$sent = false; |
171
|
|
|
|
172
|
|
|
$download = add_query_arg( 'hmbkp_download', base64_encode( $file ), HMBKP_ADMIN_URL ); |
173
|
|
|
$domain = parse_url( home_url(), PHP_URL_HOST ) . parse_url( home_url(), PHP_URL_PATH ); |
174
|
|
|
|
175
|
|
|
$headers = 'From: BackUpWordPress <' . apply_filters( 'hmbkp_from_email', get_bloginfo( 'admin_email' ) ) . '>' . "\r\n"; |
176
|
|
|
|
177
|
|
|
// The backup failed, send a message saying as much |
178
|
|
|
if ( ! file_exists( $file ) && ( $errors = array_merge( $backup->get_errors(), $backup->get_warnings() ) ) ) { |
179
|
|
|
|
180
|
|
|
$error_message = ''; |
181
|
|
|
|
182
|
|
|
foreach ( $errors as $error_set ) { |
183
|
|
|
$error_message .= implode( "\n - ", $error_set ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if ( $error_message ) { |
187
|
|
|
$error_message = ' - ' . $error_message; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$subject = sprintf( |
191
|
|
|
/* translators: Domain URL */ |
192
|
|
|
esc_html__( 'Backup of %s Failed', 'backupwordpress' ), |
193
|
|
|
$domain |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
$message = sprintf( |
197
|
|
|
/* translators: 1: Site URL */ |
198
|
|
|
esc_html__( 'BackUpWordPress was unable to backup your site %1$s.', 'backupwordpress' ) . "\n\n" . |
199
|
|
|
esc_html__( 'Here are the errors that we\'ve encountered:', 'backupwordpress' ) . "\n\n" . '%2$s' . "\n\n" . |
200
|
|
|
esc_html__( 'If the errors above look like Martian, forward this email to %3$s and we\'ll take a look', 'backupwordpress' ) . "\n\n" . |
201
|
|
|
home_url(), |
202
|
|
|
$error_message, |
203
|
|
|
'[email protected]' |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
wp_mail( $this->get_email_address_array(), $subject, $message, $headers ); |
207
|
|
|
|
208
|
|
|
return; |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$subject = sprintf( |
213
|
|
|
/* translators: Domain URL */ |
214
|
|
|
esc_html__( 'Backup of %s', 'backupwordpress' ), |
215
|
|
|
$domain |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
// If it's larger than the max attachment size limit assume it's not going to be able to send the backup |
219
|
|
|
if ( @filesize( $file ) < get_max_attachment_size() ) { |
220
|
|
|
|
221
|
|
|
$message = sprintf( |
222
|
|
|
/* translators: 1: Site URL */ |
223
|
|
|
esc_html__( 'BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress' ) . "\n\n" . |
224
|
|
|
esc_html__( 'The backup file should be attached to this email.', 'backupwordpress' ) . "\n\n" . |
225
|
|
|
/* translators: 2: WordPress admin URL to BackupWordPress page */ |
226
|
|
|
esc_html__( 'You can download the backup file by clicking the link below:', 'backupwordpress' ) . "\n\n" . '%2$s' . "\n\n" . |
227
|
|
|
esc_html__( "Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress' ), |
228
|
|
|
home_url(), |
229
|
|
|
$download |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
$sent = wp_mail( $this->get_email_address_array(), $subject, $message, $headers, $file ); |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// If we didn't send the email above then send just the notification |
237
|
|
|
if ( ! $sent ) { |
238
|
|
|
|
239
|
|
|
$message = sprintf( |
240
|
|
|
/* translators: 1: Site URL */ |
241
|
|
|
esc_html__( 'BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress' ) . "\n\n" . |
242
|
|
|
esc_html__( 'Unfortunately, the backup file was too large to attach to this email.', 'backupwordpress' ) . "\n\n" . |
243
|
|
|
/* translators: 2: WordPress admin URL to BackupWordPress page */ |
244
|
|
|
esc_html__( 'You can download the backup file by clicking the link below:', 'backupwordpress' ) . "\n\n" . '%2$s' . "\n\n" . |
245
|
|
|
esc_html__( "Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress' ), |
246
|
|
|
home_url(), |
247
|
|
|
$download |
248
|
|
|
); |
249
|
|
|
wp_mail( $this->get_email_address_array(), $subject, $message, $headers ); |
250
|
|
|
|
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public static function intercom_data() { |
256
|
|
|
return array(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public static function intercom_data_html() {} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
// Register the service |
263
|
|
|
Services::register( __FILE__, 'HM\BackUpWordPress\Email_Service' ); |
264
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.