templates
4 weeks ago
class-mail-admin.php
4 weeks ago
class-mail.php
4 weeks ago
index.php
4 weeks ago
class-mail.php
229 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Class to send an e-mail |
| 8 | */ |
| 9 | |
| 10 | if ( ! class_exists( 'rsssl_mailer' ) ) { |
| 11 | class rsssl_mailer { |
| 12 | |
| 13 | public $to; |
| 14 | public $title; |
| 15 | public $headers; |
| 16 | public $message; |
| 17 | public $branded = true; |
| 18 | public $subject; |
| 19 | public $button_text; |
| 20 | public $change_text; |
| 21 | public $sent_to_text; |
| 22 | public $what_now_text; |
| 23 | public $sent_by_text; |
| 24 | public $warning_blocks; |
| 25 | public $error = ''; |
| 26 | public $template_filename; |
| 27 | public $block_template_filename; |
| 28 | |
| 29 | public function __construct() { |
| 30 | |
| 31 | $this->sent_by_text = __( "This email is part of the Really Simple Security Notification System", "really-simple-ssl" ); |
| 32 | $this->subject = __( "Notification by Really Simple Security", "really-simple-ssl" ); |
| 33 | $this->button_text = __( "Learn more", "really-simple-ssl" ); |
| 34 | $this->to = rsssl_get_option( 'notifications_email_address', get_bloginfo( 'admin_email' ) ); |
| 35 | $this->title = __( "Learn more about our features!", "really-simple-ssl" ); |
| 36 | $this->sent_to_text = __( "This email was sent to", "really-simple-ssl" ); |
| 37 | $this->what_now_text = __( "Learn more", "really-simple-ssl" ); |
| 38 | $this->change_text = __( "Why did I receive this email?", "really-simple-ssl" ); |
| 39 | |
| 40 | $domain = '<a href="' . site_url() . '">' . site_url() . '</a>'; |
| 41 | $this->message = sprintf( __( "You have enabled a feature on %s. We think it's important to let you know a little bit more about this feature so you can use it without worries.", "really-simple-ssl" ), $domain ); |
| 42 | |
| 43 | add_action( 'wp_mail_failed', array( $this, 'log_mailer_errors' ), 10, 1 ); |
| 44 | |
| 45 | } |
| 46 | |
| 47 | public function set_to( $email ) |
| 48 | { |
| 49 | $this->to = sanitize_email( $email ); |
| 50 | rsssl_update_option('notifications_email_address', $email); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Send a test email |
| 55 | * @return array |
| 56 | */ |
| 57 | public function send_test_mail() { |
| 58 | if ( ! rsssl_user_can_manage() ) { |
| 59 | return [ 'success' => false, 'message' => 'Not allowed' ]; |
| 60 | } |
| 61 | |
| 62 | if ( ! is_email( $this->to ) ) { |
| 63 | return [ |
| 64 | 'success' => false, |
| 65 | 'title' => __( "Test notification email error", 'really-simple-ssl' ), |
| 66 | 'message' => __( 'Email address not valid', "really-simple-ssl" ), |
| 67 | ]; |
| 68 | } |
| 69 | $this->title = __( "Really Simple Security - Notification Test", "really-simple-ssl" ); |
| 70 | $this->message = __( "This email is confirmation that any security notices are likely to reach your inbox.", "really-simple-ssl" ); |
| 71 | $this->warning_blocks = [ |
| 72 | [ |
| 73 | 'title' => __( "About notifications", "really-simple-ssl" ), |
| 74 | 'message' => __( "Email notifications are only sent for important updates, security notices or when certain features are enabled.", "really-simple-ssl" ), |
| 75 | 'url' => rsssl_link('email-notifications/'), |
| 76 | ] |
| 77 | ]; |
| 78 | |
| 79 | return $this->send_mail( true ); |
| 80 | } |
| 81 | |
| 82 | public function send_verification_mail() { |
| 83 | if ( ! rsssl_user_can_manage() ) { |
| 84 | return [ |
| 85 | 'success' => false, |
| 86 | 'message' => 'Not allowed', |
| 87 | 'title' => __( "Email verification error", 'really-simple-ssl' ), |
| 88 | ]; |
| 89 | } |
| 90 | |
| 91 | $verification_code = str_pad( rand( 0, 999999 ), 6, '0', STR_PAD_LEFT ); |
| 92 | $verification_expiration = strtotime( "+15 minutes" ); |
| 93 | |
| 94 | // Delete existing option |
| 95 | delete_option( 'rsssl_email_verification_code' ); |
| 96 | |
| 97 | update_option( 'rsssl_email_verification_code', $verification_code, false ); |
| 98 | update_option( 'rsssl_email_verification_code_expiration', $verification_expiration, false ); |
| 99 | update_option( 'rsssl_email_verification_status', 'started', false ); |
| 100 | |
| 101 | if ( ! is_email( $this->to ) ) { |
| 102 | return [ |
| 103 | 'success' => false, |
| 104 | 'title' => __( "Email verification error", 'really-simple-ssl' ), |
| 105 | 'message' => __( 'Email address not valid', "really-simple-ssl" ) |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | $user_id = get_current_user_id(); |
| 110 | |
| 111 | $verification_url = add_query_arg( |
| 112 | array( |
| 113 | 'page' => 'really-simple-security', |
| 114 | 'rsssl_nonce' => wp_create_nonce( 'rsssl_email_verification_' . $user_id ), |
| 115 | 'rsssl_verification_code' => $verification_code, |
| 116 | 'verified_email' => '1', |
| 117 | ), |
| 118 | rsssl_admin_url([], '#settings/general') |
| 119 | ); |
| 120 | |
| 121 | $this->subject = __( "Really Simple Security - Verify your email address", "really-simple-ssl" ); |
| 122 | $this->title = __( "Please verify your email", "really-simple-ssl" ); |
| 123 | $this->message = __('To use certain features in Really Simple Security we need to confirm emails are delivered without issues.', 'really-simple-ssl'); |
| 124 | $this->button_text = __( "Verify email", "really-simple-ssl" ); |
| 125 | $this->warning_blocks[] = [ |
| 126 | 'title' => '', |
| 127 | 'message' => sprintf( __( "Click the button below to confirm your email address, or copy the following URL: %s", "really-simple-ssl" ), '{url}' ), |
| 128 | 'url' => $verification_url, |
| 129 | ]; |
| 130 | |
| 131 | return $this->send_mail(); |
| 132 | } |
| 133 | |
| 134 | public function log_mailer_errors( $wp_error ) { |
| 135 | if ( is_wp_error( $wp_error ) ) { |
| 136 | $this->error = $wp_error->get_error_message(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Send an e-mail with the correct login URL |
| 142 | * |
| 143 | * @return array |
| 144 | */ |
| 145 | public function send_mail(): array { |
| 146 | if ( empty( $this->message ) || empty( $this->subject ) ) { |
| 147 | $this->error = __( "Email could not be sent. No message or subject set.", "really-simple-ssl" ); |
| 148 | } |
| 149 | |
| 150 | if ( ! is_email( $this->to ) ) { |
| 151 | $this->error = __( "Email address not valid", "really-simple-ssl" ); |
| 152 | } |
| 153 | $block_template = $this->branded ? rsssl_path . '/mailer/templates/block.html' : rsssl_path . '/mailer/templates/block-unbranded.html'; |
| 154 | $email_template = $this->branded ? rsssl_path . '/mailer/templates/email.html' : rsssl_path . '/mailer/templates/email-unbranded.html'; |
| 155 | $this->block_template_filename = apply_filters( 'rsssl_email_block_template', $block_template ); |
| 156 | $this->template_filename = apply_filters( 'rsssl_email_template', $email_template ); |
| 157 | |
| 158 | $template = file_get_contents( $this->template_filename ); |
| 159 | $block_html = ''; |
| 160 | if ( is_array( $this->warning_blocks ) && count( $this->warning_blocks ) > 0 ) { |
| 161 | $block_template = file_get_contents( $this->block_template_filename ); |
| 162 | foreach ( $this->warning_blocks as $warning_block ) { |
| 163 | $block_html .= str_replace( |
| 164 | [ '{title}', '{message}', '{url}' ], |
| 165 | [ |
| 166 | sanitize_text_field( $warning_block['title'] ), |
| 167 | wp_kses_post( $warning_block['message'] ), |
| 168 | esc_url_raw( $warning_block['url'] ) |
| 169 | ], |
| 170 | $block_template ); |
| 171 | } |
| 172 | } |
| 173 | $username = rsssl_get_option( 'new_admin_user_login' ); |
| 174 | $login_url = ! empty( rsssl_get_option( 'change_login_url' ) ) |
| 175 | ? trailingslashit( site_url() ) . rsssl_get_option( 'change_login_url' ) |
| 176 | : wp_login_url(); |
| 177 | $body = str_replace( |
| 178 | [ |
| 179 | '{title}', |
| 180 | '{message}', |
| 181 | '{warnings}', |
| 182 | '{email-address}', |
| 183 | '{learn-more}', |
| 184 | '{site_url}', |
| 185 | '{login_url}', |
| 186 | '{username}', |
| 187 | '{change_text}', |
| 188 | '{what_now}', |
| 189 | '{sent_to_text}', |
| 190 | '{sent_by_text}', |
| 191 | ], |
| 192 | [ |
| 193 | sanitize_text_field( $this->title ), |
| 194 | wp_kses_post( $this->message ), |
| 195 | $block_html, |
| 196 | $this->to, |
| 197 | $this->button_text, |
| 198 | site_url(), |
| 199 | $login_url, |
| 200 | $username, |
| 201 | $this->change_text, |
| 202 | $this->what_now_text, |
| 203 | $this->sent_to_text, |
| 204 | $this->sent_by_text, |
| 205 | ], $template ); |
| 206 | $success = wp_mail( $this->to, sanitize_text_field( $this->subject ), $body, array( 'Content-Type: text/html; charset=UTF-8' ) ); |
| 207 | if ( $success ) { |
| 208 | return [ |
| 209 | 'success' => true, |
| 210 | 'title' => __( "Email validation", 'really-simple-ssl' ), |
| 211 | 'message' => __( 'Email sent! Please check your mail', "really-simple-ssl" ) |
| 212 | ]; |
| 213 | } |
| 214 | |
| 215 | if ( empty( $this->error ) ) { |
| 216 | $this->error = __( 'Email could not be sent.', "really-simple-ssl" ); |
| 217 | } else { |
| 218 | $this->error = __( 'An error occurred:', "really-simple-ssl" ) . '<br>' . $this->error; |
| 219 | } |
| 220 | |
| 221 | return [ |
| 222 | 'success' => false, |
| 223 | 'title' => __( "Email notification error", 'really-simple-ssl' ), |
| 224 | 'message' => $this->error |
| 225 | ]; |
| 226 | } |
| 227 | |
| 228 | } |
| 229 | } |