| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Notification |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class MainWP_Notification |
| 12 |
* |
| 13 |
* @package MainWP\Dashboard |
| 14 |
*/ |
| 15 |
class MainWP_Notification { |
| 16 |
|
| 17 |
/** |
| 18 |
* Method get_class_name() |
| 19 |
* |
| 20 |
* Get Class Name. |
| 21 |
* |
| 22 |
* @return object |
| 23 |
*/ |
| 24 |
public static function get_class_name() { |
| 25 |
return __CLASS__; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Method send_notify_user() |
| 31 |
* |
| 32 |
* To send user a notification. |
| 33 |
* |
| 34 |
* @param int $userId User ID. |
| 35 |
* @param string $subject Email Subject. |
| 36 |
* @param string $content Email Content. |
| 37 |
*/ |
| 38 |
public static function send_notify_user( $userId, $subject, $content ) { |
| 39 |
$content_type = 'content-type: text/html'; |
| 40 |
self::send_wp_mail( |
| 41 |
MainWP_DB_Common::instance()->get_user_notification_email( $userId ), |
| 42 |
$subject, |
| 43 |
$content, |
| 44 |
$content_type |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* Method send_http_check_notification(). |
| 51 |
* |
| 52 |
* Send HTTP response email notification. |
| 53 |
* |
| 54 |
* @param mixed $email_settings Email settings. |
| 55 |
* @param array $sites_status Websites http status. |
| 56 |
* @param bool $plain_text Text format. |
| 57 |
* @param bool $general Either general or individual notification. |
| 58 |
* |
| 59 |
* @return bool False if failed. |
| 60 |
*/ |
| 61 |
public static function send_http_check_notification( $email_settings, $sites_status, $plain_text, $general = true ) { |
| 62 |
|
| 63 |
if ( $plain_text ) { |
| 64 |
$content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n"; |
| 65 |
} else { |
| 66 |
$content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n"; |
| 67 |
} |
| 68 |
|
| 69 |
$heading = $email_settings['heading']; |
| 70 |
$subject = $email_settings['subject']; |
| 71 |
|
| 72 |
$formated_content = MainWP_Notification_Template::instance()->get_template_html( |
| 73 |
'emails/mainwp-after-update-http-check-email.php', |
| 74 |
array( |
| 75 |
'sites_statuses' => $sites_status, |
| 76 |
'heading' => $heading, |
| 77 |
) |
| 78 |
); |
| 79 |
|
| 80 |
$email = ''; |
| 81 |
|
| 82 |
// do not send individual data to admin. |
| 83 |
if ( $general ) { |
| 84 |
$email = get_option( 'mainwp_updatescheck_mail_email' ); |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! empty( $email_settings['recipients'] ) ) { |
| 88 |
$email .= ',' . $email_settings['recipients']; // send to recipients. |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! empty( $email ) ) { |
| 92 |
MainWP_Logger::instance()->debug( 'CRON :: http check :: send mail to :: ' . $email ); |
| 93 |
self::send_wp_mail( |
| 94 |
$email, |
| 95 |
$subject, |
| 96 |
$formated_content, |
| 97 |
$content_type |
| 98 |
); |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Method send_daily_digest_notification(). |
| 107 |
* |
| 108 |
* Sent available updates notification email. |
| 109 |
* |
| 110 |
* @param array $email_settings Email settings. |
| 111 |
* @param bool $available_updates Update avaiable. |
| 112 |
* @param mixed $wp_updates WP updates. |
| 113 |
* @param mixed $plugin_updates Plugins updates. |
| 114 |
* @param mixed $theme_updates Themes updates. |
| 115 |
* @param mixed $sites_disconnected Sites disconnected. |
| 116 |
* @param bool $plain_text Text format. |
| 117 |
* @param array $sites_ids Websites ids - default false (option). |
| 118 |
* @param bool $to_admin Send to admin or not - default false (option). |
| 119 |
* @param object $email_site current report site. |
| 120 |
*/ |
| 121 |
public static function send_daily_digest_notification( $email_settings, $available_updates, $wp_updates, $plugin_updates, $theme_updates, $sites_disconnected, $plain_text, $sites_ids = false, $to_admin = false, $email_site = false ) { |
| 122 |
|
| 123 |
if ( $email_settings['disable'] ) { |
| 124 |
return false; // disabled send daily digest notification. |
| 125 |
} |
| 126 |
|
| 127 |
$email = ''; |
| 128 |
|
| 129 |
if ( empty( $sites_ids ) || $to_admin ) { |
| 130 |
$email = get_option( 'mainwp_updatescheck_mail_email' ); // general notification, do not send individual notification to admin. |
| 131 |
} |
| 132 |
|
| 133 |
if ( ! empty( $email_settings['recipients'] ) ) { |
| 134 |
$email .= ',' . $email_settings['recipients']; // send to recipients. |
| 135 |
} |
| 136 |
|
| 137 |
if ( empty( $email ) ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
if ( $plain_text ) { |
| 142 |
$content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n"; |
| 143 |
} else { |
| 144 |
$content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n"; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Filter: mainwp_daily_digest_content |
| 149 |
* |
| 150 |
* Filters the Daily Digest email content and adds support for enabling text/plain emails. |
| 151 |
* |
| 152 |
* @param array $sites_ids Array of sites IDs. |
| 153 |
* @param bool $plain_text Wether plain text mode is enabled. |
| 154 |
* |
| 155 |
* @since 4.1 |
| 156 |
*/ |
| 157 |
$other_digest = apply_filters( 'mainwp_daily_digest_content', false, $sites_ids, $plain_text ); |
| 158 |
|
| 159 |
$heading = $email_settings['heading']; |
| 160 |
|
| 161 |
$formated_content = MainWP_Notification_Template::instance()->get_template_html( |
| 162 |
'emails/mainwp-daily-digest-email.php', |
| 163 |
array( |
| 164 |
'available_updates' => $available_updates, |
| 165 |
'wp_updates' => $wp_updates, |
| 166 |
'plugin_updates' => $plugin_updates, |
| 167 |
'theme_updates' => $theme_updates, |
| 168 |
'sites_disconnected' => $sites_disconnected, |
| 169 |
'other_digest' => $other_digest, |
| 170 |
'heading' => $heading, |
| 171 |
'current_email_site' => $email_site, // support tokens process. |
| 172 |
) |
| 173 |
); |
| 174 |
|
| 175 |
MainWP_Logger::instance()->debug( 'CRON :: updates check - daily digest :: send mail to [' . $email . ']' ); |
| 176 |
$subject = $email_settings['subject']; |
| 177 |
self::send_wp_mail( |
| 178 |
$email, |
| 179 |
$subject, |
| 180 |
$formated_content, |
| 181 |
$content_type |
| 182 |
); |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
/** |
| 188 |
* Method send_websites_uptime_monitoring(). |
| 189 |
* |
| 190 |
* Send websites status email notification. |
| 191 |
* |
| 192 |
* @param string $emails notification emails. |
| 193 |
* @param string $subject email subject. |
| 194 |
* @param string $mail_content email content. |
| 195 |
* @param bool $plain_text Text format. |
| 196 |
*/ |
| 197 |
public static function send_websites_uptime_monitoring( $emails, $subject, $mail_content, $plain_text ) { |
| 198 |
|
| 199 |
if ( $plain_text ) { |
| 200 |
$content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n"; |
| 201 |
} else { |
| 202 |
$content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n"; |
| 203 |
} |
| 204 |
|
| 205 |
if ( ! empty( $emails ) && '' != $mail_content ) { |
| 206 |
MainWP_Logger::instance()->debug( 'CRON :: sites status :: send mail to [' . $emails . ']' ); |
| 207 |
self::send_wp_mail( |
| 208 |
$emails, |
| 209 |
$subject, |
| 210 |
$mail_content, |
| 211 |
$content_type |
| 212 |
); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Method send_websites_health_status_notification(). |
| 218 |
* |
| 219 |
* Send websites status email notification. |
| 220 |
* |
| 221 |
* @param string $email notification email. |
| 222 |
* @param string $subject subject. |
| 223 |
* @param string $mail_content email content. |
| 224 |
* @param bool $plain_text Text format. |
| 225 |
*/ |
| 226 |
public static function send_websites_health_status_notification( $email, $subject, $mail_content, $plain_text ) { |
| 227 |
|
| 228 |
if ( $plain_text ) { |
| 229 |
$content_type = "Content-Type: text/plain; charset=\"utf-8\"\r\n"; |
| 230 |
} else { |
| 231 |
$content_type = "Content-Type: text/html; charset=\"utf-8\"\r\n"; |
| 232 |
} |
| 233 |
|
| 234 |
if ( ! empty( $email ) && '' != $mail_content ) { |
| 235 |
MainWP_Logger::instance()->debug( 'CRON :: sites health :: send mail to [' . $email . ']' ); |
| 236 |
self::send_wp_mail( |
| 237 |
$email, |
| 238 |
$subject, |
| 239 |
$mail_content, |
| 240 |
$content_type |
| 241 |
); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* Method send_wp_mail(). |
| 248 |
* |
| 249 |
* Send email via wp_mail(). |
| 250 |
* |
| 251 |
* @param string $email send to email. |
| 252 |
* @param string $subject email content. |
| 253 |
* @param bool $mail_content Text format. |
| 254 |
* @param string $content_type email content. |
| 255 |
*/ |
| 256 |
public static function send_wp_mail( $email, $subject, $mail_content, $content_type ) { |
| 257 |
wp_mail( |
| 258 |
$email, |
| 259 |
$subject, |
| 260 |
$mail_content, |
| 261 |
array( |
| 262 |
'From: "' . get_option( 'admin_email' ) . '" <' . get_option( 'admin_email' ) . '>', |
| 263 |
$content_type, |
| 264 |
) |
| 265 |
); |
| 266 |
} |
| 267 |
|
| 268 |
} |
| 269 |
|