|
| 1 | +<?php |
| 2 | +// If ABSPATH is defined, we assume WP is calling us. |
| 3 | +// Otherwise, this could be an illicit direct request. |
| 4 | +if (!defined('ABSPATH')) exit(); |
| 5 | + |
| 6 | + |
| 7 | +/** |
| 8 | + * @package wp-sparkpost |
| 9 | + */ |
| 10 | +class SparkPostAdmin |
| 11 | +{ |
| 12 | + private $options; |
| 13 | + |
| 14 | + public function __construct() |
| 15 | + { |
| 16 | + $this->options = SparkPost::get_options(); |
| 17 | + add_action('admin_menu', array($this, 'add_plugin_page')); |
| 18 | + add_action('admin_init', array($this, 'admin_page_init')); |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + public function add_plugin_page() |
| 23 | + { |
| 24 | + add_options_page( |
| 25 | + 'SparkPost Settings', |
| 26 | + 'SparkPost', |
| 27 | + 'manage_options', |
| 28 | + 'wpsp-setting-admin', |
| 29 | + array($this, 'wpsp_admin_page') |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + protected function render_message($msg, $msg_type = 'error') |
| 34 | + { |
| 35 | + printf('<div class="%s notice is-dismissible"><p>%s</p></div>', $msg_type, $msg); |
| 36 | + } |
| 37 | + |
| 38 | + public function phpmailer_enable_debugging($phpmailer) |
| 39 | + { |
| 40 | + $phpmailer->SMTPDebug = 3; |
| 41 | + $phpmailer->Debugoutput = 'html'; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public function set_html_content_type() |
| 46 | + { |
| 47 | + return 'text/html'; |
| 48 | + } |
| 49 | + |
| 50 | + private function send_email($recipient) |
| 51 | + { |
| 52 | + add_filter('wp_mail_content_type', array($this, 'set_html_content_type')); |
| 53 | + $result = wp_mail($recipient, |
| 54 | + 'SparkPost email test', |
| 55 | + '<h3>Hurray!!</h3><p>You\'ve got mail! <br/><br> Regards,<br/><a href="https://www.sparkpost.com">SparkPost</a> WordPress plugin</p>', |
| 56 | + '' |
| 57 | + ); |
| 58 | + remove_filter('wp_mail_content_type', array($this, 'set_html_content_type')); |
| 59 | + return $result; |
| 60 | + } |
| 61 | + |
| 62 | + public function test_email_sending($recipient, $debug = false) |
| 63 | + { |
| 64 | + if (empty($recipient)) { |
| 65 | + return $this->render_message('Please enter a valid email address in the recipient field below.'); |
| 66 | + } |
| 67 | + |
| 68 | + if (!is_email($recipient)) { |
| 69 | + return $this->render_message('Recipient is not a valid email address.'); |
| 70 | + } |
| 71 | + |
| 72 | + if ($debug) { |
| 73 | + add_action('phpmailer_init', array($this, 'phpmailer_enable_debugging')); |
| 74 | + echo '<div class="notice is-dismissible">'; |
| 75 | + echo '<h4>Debug Messages</h4>'; |
| 76 | + $result = $this->send_email($recipient); |
| 77 | + echo '</div>'; |
| 78 | + } else { |
| 79 | + $result = $this->send_email($recipient); |
| 80 | + } |
| 81 | + |
| 82 | + if ($result) { |
| 83 | + if (!$this->options['enable_sparkpost']) { |
| 84 | + $this->render_message('Test email sent successfully but not through SparkPost.<br/>Note: the SparkPost plugin is not enabled. To enable it, check "Send email using SparkPost" on the SparkPost settings page.', 'updated'); |
| 85 | + } else { |
| 86 | + $this->render_message('Test email sent successfully', 'updated'); |
| 87 | + } |
| 88 | + } else { |
| 89 | + $this->render_message('Test email could not be sent. Please check your plugin settings and refer to <a href="https://support.sparkpost.com/customer/en/portal/topics/770787-getting-started/articles" target="_blank">Getting Started</a> in the <a href="https://support.sparkpost.com/" target="_blank">SparkPost Support Center</a>.'); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public function wpsp_admin_page() |
| 94 | + { |
| 95 | + ?> |
| 96 | + <div class="wrap"> |
| 97 | + <form method="post" action="options.php"> |
| 98 | + <?php |
| 99 | + settings_fields('sp_settings_group'); |
| 100 | + do_settings_sections('sp-options'); |
| 101 | + do_settings_sections('sp-overrides'); |
| 102 | + submit_button(); |
| 103 | + ?> |
| 104 | + </form> |
| 105 | + <hr/> |
| 106 | + <div> |
| 107 | + <h2>Test Email</h2> |
| 108 | + <?php |
| 109 | + if (isset($_POST['sp_test_email'])) { |
| 110 | + $this->test_email_sending($_POST['to_email'], !empty($_POST['enable_debugging'])); |
| 111 | + } |
| 112 | + ?> |
| 113 | + |
| 114 | + <form method="post" action=""> |
| 115 | + <input type="hidden" name="sp_test_email" value=""/> |
| 116 | + <?php |
| 117 | + do_settings_sections('sp-test-email'); |
| 118 | + submit_button('Send Test Email', 'secondary'); |
| 119 | + ?> |
| 120 | + </form> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + <?php |
| 124 | + } |
| 125 | + |
| 126 | + public function admin_page_init() |
| 127 | + { |
| 128 | + register_setting('sp_settings_group', 'sp_settings', array($this, 'sanitize')); |
| 129 | + add_settings_section('general', 'SparkPost Settings', null, 'sp-options'); |
| 130 | + add_settings_field('enable_sparkpost', '', array($this, 'render_enable_sparkpost_field'), 'sp-options', 'general'); |
| 131 | + add_settings_field('sending_method', 'Method*', array($this, 'render_sending_method_field'), 'sp-options', 'general'); |
| 132 | + add_settings_field('password', 'API Key*', array($this, 'render_password_field'), 'sp-options', 'general'); |
| 133 | + add_settings_field('enable_tracking', 'Enable tracking*', array($this, 'render_enable_tracking_field'), 'sp-options', 'general'); |
| 134 | + |
| 135 | + add_settings_section('overrides', 'Overrides', null, 'sp-overrides'); |
| 136 | + add_settings_field('from_name', 'From name', array($this, 'render_from_name_field'), 'sp-overrides', 'overrides'); |
| 137 | + add_settings_field('from_email', 'From email', array($this, 'render_from_email_field'), 'sp-overrides', 'overrides'); |
| 138 | + |
| 139 | + add_settings_section('test_email', '', null, 'sp-test-email'); |
| 140 | + add_settings_field('to_email', 'Recipient*', array($this, 'render_to_email_field'), 'sp-test-email', 'test_email'); |
| 141 | + add_settings_field('debug_messages', 'Debug', array($this, 'render_enable_debugging_field'), 'sp-test-email', 'test_email'); |
| 142 | + } |
| 143 | + |
| 144 | + public function sanitize($input) |
| 145 | + { |
| 146 | + |
| 147 | + $new_input = array(); |
| 148 | + |
| 149 | + if (!empty($input['from_email'])) { |
| 150 | + $new_input['from_email'] = trim($input['from_email']); |
| 151 | + } |
| 152 | + |
| 153 | + if (!empty($input['from_name'])) { |
| 154 | + $new_input['from_name'] = trim($input['from_name']); |
| 155 | + } |
| 156 | + |
| 157 | + if (empty($input['password'])) { |
| 158 | + add_settings_error('Password', esc_attr('password'), 'API Key is required', 'error'); |
| 159 | + } else { |
| 160 | + if(SparkPost::is_key_obfuscated(esc_attr($input['password']))) { //do not change password |
| 161 | + $new_input['password'] = $this->options['password']; |
| 162 | + } else { |
| 163 | + $new_input['password'] = trim(esc_attr($input['password'])); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if (isset($input['enable_sparkpost'])) { |
| 168 | + $new_input['enable_sparkpost'] = true; |
| 169 | + } else { |
| 170 | + $new_input['enable_sparkpost'] = false; |
| 171 | + } |
| 172 | + |
| 173 | + if ((empty($input['password'])) && $new_input['enable_sparkpost']) { |
| 174 | + add_settings_error('Enable', esc_attr('enable_sparkpost'), 'You must enter API key to enable sending via SparkPost', 'error'); |
| 175 | + $new_input['enable_sparkpost'] = false; |
| 176 | + } |
| 177 | + |
| 178 | + switch (esc_attr($input['sending_method'])) { |
| 179 | + case 'smtp587': |
| 180 | + $new_input['port'] = 587; |
| 181 | + $new_input['sending_method'] = 'smtp'; |
| 182 | + break; |
| 183 | + case 'smtp2525': |
| 184 | + $new_input['port'] = 2525; |
| 185 | + $new_input['sending_method'] = 'smtp'; |
| 186 | + break; |
| 187 | + default: |
| 188 | + unset($new_input['port']); |
| 189 | + $new_input['sending_method'] = 'api'; |
| 190 | + break; |
| 191 | + } |
| 192 | + |
| 193 | + if(!empty($input['enable_tracking'])) { |
| 194 | + $new_input['enable_tracking'] = true; |
| 195 | + } else { |
| 196 | + $new_input['enable_tracking'] = false; |
| 197 | + |
| 198 | + } |
| 199 | + return $new_input; |
| 200 | + } |
| 201 | + |
| 202 | + public function render_enable_sparkpost_field() |
| 203 | + { |
| 204 | + printf( |
| 205 | + '<label><input type="checkbox" id="enable_sparkpost" name="sp_settings[enable_sparkpost]" value="1" %s />Send email using SparkPost</label>', $this->options['enable_sparkpost'] ? 'checked' : '' |
| 206 | + ); |
| 207 | + } |
| 208 | + |
| 209 | + public function render_username_field() |
| 210 | + { |
| 211 | + echo '<input type="text" id="username" name="sp_settings[username]" class="regular-text" value="SMTP_Injection" readonly />'; |
| 212 | + } |
| 213 | + |
| 214 | + public function render_password_field() |
| 215 | + { |
| 216 | + $api_key = SparkPost::obfuscate_api_key($this->options['password']); |
| 217 | + |
| 218 | + printf( |
| 219 | + '<input type="text" id="password" name="sp_settings[password]" class="regular-text" value="%s" /><br/> |
| 220 | + <small><ul><li>For SMTP, use a SparkPost API key with <strong>Send via SMTP</strong> permission</li> <li>For HTTP API, use API Key with <strong>Transmissions: Read/Write</strong> permission</li><a href="https://support.sparkpost.com/customer/portal/articles/1933377-create-api-keys" target="_blank">Need help creating a SparkPost API key?</a></small>', |
| 221 | + isset($api_key) ? $api_key : '' |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | + public function render_from_email_field() |
| 226 | + { |
| 227 | + $hint = 'Important: Domain must match with one of your verified sending domains.'; |
| 228 | + if(empty($this->options['from_email'])){ |
| 229 | + $hostname = parse_url(get_bloginfo('url'), PHP_URL_HOST); |
| 230 | + $hint .= sprintf(' When left blank, <strong>%s</strong> will be used as email domain', $hostname); |
| 231 | + } |
| 232 | + |
| 233 | + $hint = sprintf('<small>%s</small>', $hint); |
| 234 | + printf( |
| 235 | + '<input type="email" id="from_email" name="sp_settings[from_email]" class="regular-text" value="%s" /><br/>%s', |
| 236 | + isset($this->options['from_email']) ? esc_attr($this->options['from_email']) : '', $hint |
| 237 | + ); |
| 238 | + } |
| 239 | + |
| 240 | + public function render_from_name_field() |
| 241 | + { |
| 242 | + printf( |
| 243 | + '<input type="text" id="from_name" name="sp_settings[from_name]" class="regular-text" value="%s" />', |
| 244 | + isset($this->options['from_name']) ? esc_attr($this->options['from_name']) : '' |
| 245 | + ); |
| 246 | + } |
| 247 | + |
| 248 | + public function render_sending_method_field() |
| 249 | + { |
| 250 | + $method = esc_attr($this->options['sending_method']); |
| 251 | + $port = esc_attr($this->options['port']); |
| 252 | + |
| 253 | + $selected_method = !empty($method) ? $method : 'api'; |
| 254 | + $selected_port = !empty($port) ? $port : 587; |
| 255 | + |
| 256 | + echo '<select name="sp_settings[sending_method]"> |
| 257 | + <option value="api" ' . (($selected_method == 'api') ? 'selected' : '') . '>HTTP API (Default)</option> |
| 258 | + <option value="smtp587" ' . (($selected_method == 'smtp' && $selected_port == 587) ? 'selected' : '') . '>SMTP (Port 587)</option> |
| 259 | + <option value="smtp2525" ' . (($selected_method == 'smtp' && $selected_port == 2525) ? 'selected' : '') . '>SMTP (Port 2525)</option> |
| 260 | + </select>'; |
| 261 | + } |
| 262 | + |
| 263 | + public function render_enable_tracking_field() |
| 264 | + { |
| 265 | + printf( |
| 266 | + '<label><input type="checkbox" id="enable_tracking" name="sp_settings[enable_tracking]" value="1" %s />Track clicks/opens in SparkPost</label>', $this->options['enable_tracking'] ? 'checked' : '' |
| 267 | + ); |
| 268 | + } |
| 269 | + |
| 270 | + public function render_to_email_field() |
| 271 | + { |
| 272 | + echo '<input type="email" id="to_email" name="to_email" class="regular-text" value="" />'; |
| 273 | + } |
| 274 | + |
| 275 | + public function render_enable_debugging_field() |
| 276 | + { |
| 277 | + echo '<label><input type="checkbox" id="enable_debugging" name="enable_debugging" value="1" checked />Show email debugging messages</label>'; |
| 278 | + } |
| 279 | +} |
0 commit comments