PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-notification.php

class-mainwp-notification.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies trunk, at class/class-mainwp-notification.php

423 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Notification
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Class MainWP_Notification
17 *
18 * @package MainWP\Dashboard
19 */
20 class MainWP_Notification { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
21
22 /**
23 * Method get_class_name()
24 *
25 * Get Class Name.
26 *
27 * @return object
28 */
29 public static function get_class_name() {
30 return __CLASS__;
31 }
32
33
34 /**
35 * Method send_notify_user()
36 *
37 * To send user a notification.
38 *
39 * @param int $userId User ID.
40 * @param string $subject Email Subject.
41 * @param string $content Email Content.
42 *
43 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_notification_email()
44 */
45 public static function send_notify_user( $userId, $subject, $content ) {
46 $content_type = 'content-type: text/html';
47 static::send_wp_mail(
48 MainWP_DB_Common::instance()->get_user_notification_email( $userId ),
49 $subject,
50 $content,
51 $content_type
52 );
53 }
54
55
56 /**
57 * Method send_http_check_notification().
58 *
59 * Send HTTP response email notification.
60 *
61 * @param mixed $email_settings Email settings.
62 * @param array $sites_status Websites http status.
63 * @param bool $plain_text Text format.
64 *
65 * @return bool False if failed.
66 *
67 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
68 * @uses \MainWP\Dashboard\MainWP_Notification_Template::get_template_html()
69 */
70 public static function send_http_check_notification( $email_settings, $sites_status, $plain_text ) {
71
72 if ( $plain_text ) {
73 $content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n";
74 } else {
75 $content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n";
76 }
77
78 $heading = $email_settings['heading'];
79 $subject = $email_settings['subject'];
80
81 $formated_content = MainWP_Notification_Template::instance()->get_template_html(
82 'emails/mainwp-after-update-http-check-email.php',
83 array(
84 'sites_statuses' => $sites_status,
85 'heading' => $heading,
86 )
87 );
88
89 $email = '';
90
91 if ( ! empty( $email_settings['recipients'] ) ) {
92 $email .= ',' . $email_settings['recipients']; // send to recipients.
93 }
94
95 $email = trim( $email, ',' );
96
97 if ( ! empty( $email ) ) {
98 MainWP_Logger::instance()->debug( 'http check :: send mail ::' );
99 static::send_wp_mail(
100 $email,
101 $subject,
102 $formated_content,
103 $content_type
104 );
105 return true;
106 }
107
108 return false;
109 }
110
111
112 /**
113 * Method send_license_deactivated_alert().
114 *
115 * Send extensions license deactivated email notification.
116 *
117 * @param mixed $email_settings Email settings.
118 * @param array $deactivated_license Websites http status.
119 * @param bool $plain_text Text format.
120 *
121 * @return bool False if failed.
122 */
123 public static function send_license_deactivated_alert( $email_settings, $deactivated_license, $plain_text ) {
124
125 if ( $plain_text ) {
126 $content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n";
127 } else {
128 $content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n";
129 }
130
131 $heading = $email_settings['heading'];
132 $subject = $email_settings['subject'];
133
134 $formated_content = MainWP_Notification_Template::instance()->get_template_html(
135 'emails/mainwp-licenses-deactivated-alert-email.php',
136 array(
137 'deactivated_licenses' => $deactivated_license,
138 'heading' => $heading,
139 )
140 );
141
142 $email = '';
143
144 if ( ! empty( $email_settings['recipients'] ) ) {
145 $email .= ',' . $email_settings['recipients']; // send to recipients.
146 }
147
148 $email = trim( $email, ',' );
149
150 if ( ! empty( $email ) ) {
151 MainWP_Logger::instance()->debug( 'license deactivated alert:: send mail ::' );
152 static::send_wp_mail(
153 $email,
154 $subject,
155 $formated_content,
156 $content_type
157 );
158 return true;
159 }
160
161 return false;
162 }
163
164
165 /**
166 * Method send_daily_digest_notification().
167 *
168 * Sent available updates notification email.
169 *
170 * @param array $email_settings Email settings.
171 * @param bool $available_updates Update avaiable.
172 * @param mixed $wp_updates WP updates.
173 * @param mixed $plugin_updates Plugin updates.
174 * @param mixed $theme_updates Theme updates.
175 * @param mixed $sites_disconnected Sites disconnected.
176 * @param array $params Other params.
177 *
178 * @return bool
179 *
180 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
181 * @uses \MainWP\Dashboard\MainWP_Notification_Template::get_template_html()
182 */
183 public static function send_daily_digest_notification( $email_settings, $available_updates, $wp_updates, $plugin_updates, $theme_updates, $sites_disconnected, $params = array() ) {
184
185 if ( ! is_array( $params ) ) {
186 $params = array();
187 }
188
189 $plain_text = isset( $params['plain_text'] ) ? $params['plain_text'] : '';
190 $sites_ids = isset( $params['sites_ids'] ) ? $params['sites_ids'] : false;
191 $email_site = isset( $params['email_site'] ) ? $params['email_site'] : false;
192
193 if ( $email_settings['disable'] ) {
194 return false; // disabled send daily digest notification.
195 }
196
197 $email = '';
198
199 if ( ! empty( $email_settings['recipients'] ) ) {
200 $email .= ',' . $email_settings['recipients']; // send to recipients, individual email settings or general email settings.
201 }
202
203 $email = trim( $email, ',' );
204
205 if ( empty( $email ) ) {
206 return false;
207 }
208
209 if ( $plain_text ) {
210 $content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n";
211 } else {
212 $content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n";
213 }
214
215 /**
216 * Filter: mainwp_daily_digest_content
217 *
218 * Filters the Daily Digest email content and adds support for enabling text/plain emails.
219 *
220 * @param array $sites_ids Array of sites IDs.
221 * @param bool $plain_text Wether plain text mode is enabled.
222 *
223 * @since 4.1
224 */
225 $other_digest = apply_filters( 'mainwp_daily_digest_content', false, $sites_ids, $plain_text );
226
227 $heading = $email_settings['heading'];
228
229 $formated_content = MainWP_Notification_Template::instance()->get_template_html(
230 'emails/mainwp-daily-digest-email.php',
231 array(
232 'available_updates' => $available_updates,
233 'wp_updates' => $wp_updates,
234 'plugin_updates' => $plugin_updates,
235 'theme_updates' => $theme_updates,
236 'sites_disconnected' => $sites_disconnected,
237 'other_digest' => $other_digest,
238 'heading' => $heading,
239 'current_email_site' => $email_site, // support tokens process.
240 )
241 );
242
243 $subject = $email_settings['subject'];
244 $sent = static::send_wp_mail(
245 $email,
246 $subject,
247 $formated_content,
248 $content_type
249 );
250 if ( $sent ) {
251 MainWP_Logger::instance()->log_update_check( 'daily digest :: send mail :: successful :: ' . $email );
252 } else {
253 MainWP_Logger::instance()->log_update_check( 'daily digest :: send mail :: failed :: ' . $email );
254 }
255 return true;
256 }
257
258
259 /**
260 * Method send_websites_uptime_monitoring().
261 *
262 * Send websites status email notification.
263 *
264 * @param string $emails notification emails.
265 * @param string $subject email subject.
266 * @param string $mail_content email content.
267 * @param bool $plain_text Text format.
268 * @param object $site_mo Monitor website.
269 *
270 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
271 */
272 public static function send_websites_uptime_monitoring( $emails, $subject, $mail_content, $plain_text, $site_mo = false ) {
273
274 if ( $plain_text ) {
275 $content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n";
276 } else {
277 $content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n";
278 }
279
280 if ( ! empty( $emails ) && ! empty( $mail_content ) ) {
281 MainWP_Logger::instance()->log_uptime_notice( 'Uptime monitoring status :: send mail :: ' . $subject );
282 if ( $site_mo ) {
283 $event_time = ! empty( $site_mo->hb_time_check ) ? gmdate( 'Y-m-d H:i:s', $site_mo->hb_time_check ) : '';
284 MainWP_Logger::instance()->log_uptime_notice( 'Sending notification :: [subject=' . esc_html( $subject ) . '] :: [event-time=' . $event_time . '] :: [monitorid=' . $site_mo->monitor_id . '] :: [monitor-url=' . ( empty( $site_mo->issub ) ? $site_mo->url : $site_mo->suburl ) . ']' );
285 }
286 static::send_wp_mail(
287 $emails,
288 $subject,
289 $mail_content,
290 $content_type,
291 $site_mo
292 );
293 }
294 }
295
296 /**
297 * Method send_websites_health_status_notification().
298 *
299 * Send websites status email notification.
300 *
301 * @param string $email notification email.
302 * @param string $subject subject.
303 * @param string $mail_content email content.
304 * @param bool $plain_text Text format.
305 *
306 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
307 */
308 public static function send_websites_health_status_notification( $email, $subject, $mail_content, $plain_text ) {
309
310 if ( $plain_text ) {
311 $content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n";
312 } else {
313 $content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n";
314 }
315
316 if ( ! empty( $email ) && ! empty( $mail_content ) ) {
317 MainWP_Logger::instance()->debug( 'sites health :: send mail ::' );
318 static::send_wp_mail(
319 $email,
320 $subject,
321 $mail_content,
322 $content_type
323 );
324 }
325 }
326
327
328 /**
329 * Start notification for auto update info.
330 *
331 * @param array $admin_email_settings Admin email settings.
332 * @param array $params Parameters for the notification.
333 *
334 * @return void
335 */
336 public static function start_notification_auto_updates_info( $admin_email_settings, $params = array() ) {
337
338 $email = isset( $admin_email_settings['recipients'] ) ? $admin_email_settings['recipients'] : '';
339 $subject = isset( $admin_email_settings['subject'] ) ? $admin_email_settings['subject'] : '';
340 $plain_text = isset( $params['plain_text'] ) ? $params['plain_text'] : false;
341 $mail_content = isset( $params['mail_content'] ) ? $params['mail_content'] : '';
342
343 if ( $plain_text ) {
344 $content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n";
345 } else {
346 $content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n";
347 }
348
349 if ( ! empty( $email ) && ! empty( $mail_content ) ) {
350 MainWP_Logger::instance()->log_events( 'auto-updates', 'Auto updates notification :: send mail' );
351 static::send_wp_mail(
352 $email,
353 $subject,
354 $mail_content,
355 $content_type
356 );
357 }
358 }
359
360
361 /**
362 * Method send_wp_mail().
363 *
364 * Send email via wp_mail().
365 *
366 * @param string $email send to email.
367 * @param string $subject email content.
368 * @param string $mail_content Text format.
369 * @param string $content_type email content.
370 * @param mixed $object_sending Optional object associated with the email being sent.
371 */
372 public static function send_wp_mail( $email, $subject, $mail_content, $content_type = '', $object_sending = null ) {
373 if ( empty( $content_type ) ) {
374 $content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n";
375 }
376
377 $from_header = array(
378 'from_name' => get_option( 'admin_email' ),
379 'from_email' => get_option( 'admin_email' ),
380 );
381
382 $custom_header = apply_filters( 'mainwp_send_mail_from_header', false, $email, $subject );
383 if ( is_array( $custom_header ) && isset( $custom_header['from_name'] ) && isset( $custom_header['from_email'] ) && ! empty( $custom_header['from_name'] ) && ! empty( $custom_header['from_email'] ) ) {
384 $from_header = $custom_header;
385 }
386
387 /**
388 * Fires before an email is sent via wp_mail().
389 *
390 * @since 6.1.6
391 *
392 * @param string $email Recipient email address.
393 * @param string $subject Email subject.
394 * @param string $mail_content Email message content.
395 * @param string $content_type Email content type.
396 * @param object|mixed $object_sending Object associated with the email being sent.
397 */
398 $email = apply_filters(
399 'mainwp_wp_mail_to',
400 $email,
401 $subject,
402 $mail_content,
403 $content_type,
404 $object_sending
405 );
406
407 if ( empty( $email ) ) {
408 // stop send.
409 return false;
410 }
411
412 return wp_mail(
413 $email,
414 $subject,
415 $mail_content,
416 array(
417 'From: "' . $from_header['from_name'] . '" <' . $from_header['from_email'] . '>',
418 $content_type,
419 )
420 );
421 }
422 }
423