CustomizerTrait.php
5 years ago
DefaultTemplateCustomizer.php
5 years ago
EmailSettingsPage.php
4 years ago
WPListTable.php
4 years ago
email-template-preview.php
5 years ago
EmailSettingsPage.php
355 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\EmailSettings; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\SendEmail; |
| 6 | use ProfilePress\Custom_Settings_Page_Api; |
| 7 | |
| 8 | class EmailSettingsPage |
| 9 | { |
| 10 | public $email_notification_list_table; |
| 11 | |
| 12 | public $settingsPageInstance; |
| 13 | |
| 14 | public function __construct() |
| 15 | { |
| 16 | add_action('ppress_admin_settings_page_email', [$this, 'admin_page']); |
| 17 | add_action('ppress_settings_page_screen_option', [$this, 'screen_option']); |
| 18 | add_action('admin_init', [$this, 'handle_email_preview']); |
| 19 | |
| 20 | add_action('admin_enqueue_scripts', function ($hook_suffix) { |
| 21 | if ($hook_suffix == 'toplevel_page_pp-config') { |
| 22 | wp_enqueue_script('customize-loader'); |
| 23 | } |
| 24 | }); |
| 25 | |
| 26 | add_filter('ppress_general_settings_admin_page_title', function ($title) { |
| 27 | if (isset($_GET['view']) && $_GET['view'] == 'email') { |
| 28 | $title = esc_html__('Emails', 'wp-user-avatar'); |
| 29 | } |
| 30 | |
| 31 | return $title; |
| 32 | }); |
| 33 | |
| 34 | add_action('ppress_register_menu_page', function () { |
| 35 | |
| 36 | add_filter('wp_cspa_sanitize_skip', function ($return, $fieldkey, $value) { |
| 37 | |
| 38 | if (isset($_GET['type']) && $fieldkey == sanitize_text_field($_GET['type']) . '_email_content') { |
| 39 | return stripslashes($value); |
| 40 | } |
| 41 | |
| 42 | return $return; |
| 43 | |
| 44 | }, 10, 3); |
| 45 | |
| 46 | $this->settingsPageInstance = Custom_Settings_Page_Api::instance('', PPRESS_SETTINGS_DB_OPTION_NAME); |
| 47 | |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | public function screen_option() |
| 52 | { |
| 53 | if (isset($_GET['view']) && $_GET['view'] == 'email') { |
| 54 | add_filter('screen_options_show_screen', '__return_false'); |
| 55 | |
| 56 | $this->email_notification_list_table = new WPListTable($this->email_notifications()); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @return mixed|void |
| 62 | */ |
| 63 | public function email_notifications() |
| 64 | { |
| 65 | $site_title = ppress_site_title(); |
| 66 | |
| 67 | return apply_filters('ppress_email_notifications', [ |
| 68 | [ |
| 69 | 'key' => 'welcome_message', |
| 70 | 'title' => esc_html__('Account Welcome Email', 'wp-user-avatar'), |
| 71 | 'subject' => sprintf(esc_html__('Welcome To %s', 'wp-user-avatar'), $site_title), |
| 72 | 'message' => ppress_welcome_msg_content_default(), |
| 73 | 'description' => esc_html__('Email that is sent to the user upon successful registration.', 'wp-user-avatar'), |
| 74 | 'recipient' => esc_html__('Users', 'wp-user-avatar'), |
| 75 | 'placeholders' => [ |
| 76 | '{{username}}' => esc_html__('Username of the registered user.', 'wp-user-avatar'), |
| 77 | '{{email}}' => esc_html__('Email address of the registered user.', 'wp-user-avatar'), |
| 78 | '{{password}}' => esc_html__('Password of the registered user.', 'wp-user-avatar'), |
| 79 | '{{site_title}}' => esc_html__('Website title or name.', 'wp-user-avatar'), |
| 80 | '{{first_name}}' => esc_html__('First Name entered by user on registration.', 'wp-user-avatar'), |
| 81 | '{{last_name}}' => esc_html__('Last Name entered by user on registration.', 'wp-user-avatar'), |
| 82 | '{{password_reset_link}}' => esc_html__('URL to reset password.', 'wp-user-avatar'), |
| 83 | '{{login_link}}' => esc_html__('URL to login..', 'wp-user-avatar'), |
| 84 | ] |
| 85 | ], |
| 86 | [ |
| 87 | 'key' => 'password_reset', |
| 88 | 'title' => esc_html__('Password Reset Email', 'wp-user-avatar'), |
| 89 | 'subject' => sprintf(esc_html__('[%s] Password Reset', 'wp-user-avatar'), $site_title), |
| 90 | 'message' => ppress_password_reset_content_default(), |
| 91 | 'description' => esc_html__('Email that is sent to the user upon password reset request.', 'wp-user-avatar'), |
| 92 | 'recipient' => esc_html__('Users', 'wp-user-avatar'), |
| 93 | 'placeholders' => [ |
| 94 | '{{username}}' => esc_html__('Username of user.', 'wp-user-avatar'), |
| 95 | '{{email}}' => esc_html__('Email address of the registered user.', 'wp-user-avatar'), |
| 96 | '{{site_title}}' => esc_html__('Website title or name.', 'wp-user-avatar'), |
| 97 | '{{password_reset_link}}' => esc_html__('URL to reset password.', 'wp-user-avatar'), |
| 98 | ] |
| 99 | ], |
| 100 | [ |
| 101 | 'key' => 'new_user_admin_email', |
| 102 | 'title' => esc_html__('New User Admin Notification', 'wp-user-avatar'), |
| 103 | 'subject' => sprintf(esc_html__('[%s] New User Registration', 'wp-user-avatar'), $site_title), |
| 104 | 'message' => ppress_new_user_admin_notification_message_default(), |
| 105 | 'description' => esc_html__('Email that is sent to admins when there is a new user registration', 'wp-user-avatar'), |
| 106 | 'recipient' => esc_html__('Administrators', 'wp-user-avatar'), |
| 107 | 'placeholders' => [ |
| 108 | '{{username}}' => esc_html__('Username of the newly registered user.', 'wp-user-avatar'), |
| 109 | '{{email}}' => esc_html__('Email address of the newly registered user.', 'wp-user-avatar'), |
| 110 | '{{first_name}}' => esc_html__('First name of the newly registered user.', 'wp-user-avatar'), |
| 111 | '{{last_name}}' => esc_html__('Last name of the newly registered user.', 'wp-user-avatar'), |
| 112 | '{{site_title}}' => esc_html__('Website name or name.', 'wp-user-avatar'), |
| 113 | '{{field_key}}' => sprintf( |
| 114 | esc_html__('Replace "field_key" with the %scustom field key%s or usermeta key.', 'wp-user-avatar'), |
| 115 | '<a href="' . PPRESS_CUSTOM_FIELDS_SETTINGS_PAGE . '" target="_blank">', '</a>' |
| 116 | ) |
| 117 | ] |
| 118 | ] |
| 119 | ]); |
| 120 | } |
| 121 | |
| 122 | public function admin_page() |
| 123 | { |
| 124 | if ( ! isset($_GET['type'])) { |
| 125 | add_filter('wp_cspa_main_content_area', function ($content_area) { |
| 126 | ob_start(); |
| 127 | $this->email_notification_list_table->prepare_items(); |
| 128 | $this->email_notification_list_table->display(); |
| 129 | |
| 130 | return ob_get_clean() . $content_area; |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | $page_header = esc_html__('Emails', 'wp-user-avatar'); |
| 135 | |
| 136 | $email_settings = [ |
| 137 | [ |
| 138 | 'admin_email_addresses' => [ |
| 139 | 'label' => esc_html__('Admin Email Address(es)', 'wp-user-avatar'), |
| 140 | 'description' => esc_html__('The Email address to receive admin notifications. Use comma to separate multiple email addresses.', 'wp-user-avatar'), |
| 141 | 'type' => 'text', |
| 142 | 'value' => ppress_admin_email() |
| 143 | ], |
| 144 | 'email_sender_name' => [ |
| 145 | 'label' => esc_html__('Sender Name', 'wp-user-avatar'), |
| 146 | 'description' => esc_html__('The name to use as the sender of all ProfilePress emails. Preferably your website name.', 'wp-user-avatar'), |
| 147 | 'type' => 'text', |
| 148 | 'value' => ppress_site_title() |
| 149 | ], |
| 150 | 'email_sender_email' => [ |
| 151 | 'value' => ppress_admin_email(), |
| 152 | 'label' => esc_html__('Sender Email Address', 'wp-user-avatar'), |
| 153 | 'description' => esc_html__('The email address to use as the sender of all ProfilePress emails.', 'wp-user-avatar'), |
| 154 | 'type' => 'text' |
| 155 | ], |
| 156 | 'email_content_type' => [ |
| 157 | 'type' => 'select', |
| 158 | 'options' => [ |
| 159 | 'text/html' => esc_html__('HTML', 'wp-user-avatar'), |
| 160 | 'text/plain' => esc_html__('Plain Text', 'wp-user-avatar') |
| 161 | ], |
| 162 | 'value' => 'text/html', |
| 163 | 'label' => esc_html__('Content Type', 'wp-user-avatar'), |
| 164 | 'description' => esc_html__('Choose whether to send ProfilePress emails in HTML or plain text. HTML is recommended.', 'wp-user-avatar') |
| 165 | ], |
| 166 | 'email_template_type' => [ |
| 167 | 'type' => 'select', |
| 168 | 'options' => [ |
| 169 | 'default' => esc_html__('Default Template', 'wp-user-avatar'), |
| 170 | 'custom' => esc_html__('Custom Email Template', 'wp-user-avatar') |
| 171 | ], |
| 172 | 'value' => 'default', |
| 173 | 'label' => esc_html__('Email Template', 'wp-user-avatar'), |
| 174 | 'description' => esc_html__('Choose "Custom Email Template" if you want to code your own email template from scratch.', 'wp-user-avatar') |
| 175 | ], |
| 176 | 'customize_default_template' => [ |
| 177 | 'type' => 'custom_field_block', |
| 178 | 'data' => sprintf( |
| 179 | '<a href="%s" target="_blank" class="button load-customize"><span style="margin-top: 3px;margin-right: 3px;" class="dashicons dashicons-edit"></span> <span>%s</span></a>', |
| 180 | add_query_arg(['ppress-preview-template' => 'true'], admin_url('customize.php')), |
| 181 | esc_html__('Customize Default Template', 'wp-user-avatar') |
| 182 | ), |
| 183 | ] |
| 184 | ], |
| 185 | ]; |
| 186 | |
| 187 | if (isset($_GET['type'])) { |
| 188 | |
| 189 | $key = sanitize_text_field($_GET['type']); |
| 190 | $data = wp_list_filter($this->email_notifications(), ['key' => $key]); |
| 191 | |
| 192 | $data = array_shift($data); |
| 193 | |
| 194 | if (empty($data)) { |
| 195 | wp_safe_redirect(PPRESS_SETTINGS_SETTING_PAGE); |
| 196 | exit; |
| 197 | } |
| 198 | |
| 199 | $page_header = $data['title']; |
| 200 | |
| 201 | $email_content_field_type = 'email_editor'; |
| 202 | $content_type = ppress_get_setting('email_content_type', 'text/html'); |
| 203 | |
| 204 | if ($content_type == 'text/html' && ppress_get_setting('email_template_type', 'default') == 'default') { |
| 205 | $email_content_field_type = 'wp_editor'; |
| 206 | } |
| 207 | |
| 208 | if ($content_type == 'text/plain') { |
| 209 | $email_content_field_type = 'textarea'; |
| 210 | } |
| 211 | |
| 212 | add_action('wp_cspa_media_button', function () use ($key) { |
| 213 | add_action('media_buttons', function () use ($key) { |
| 214 | $url = add_query_arg([ |
| 215 | 'pp_email_preview' => $key, |
| 216 | '_wpnonce' => ppress_create_nonce() |
| 217 | ], |
| 218 | admin_url() |
| 219 | ); |
| 220 | |
| 221 | printf( |
| 222 | '<a target="_blank" href="%s" class="button"><span class="wp-media-buttons-icon dashicons dashicons-visibility"></span> %s</a>', |
| 223 | $url, |
| 224 | esc_html__('Preview Email', 'wp-user-avatar') |
| 225 | ); |
| 226 | }); |
| 227 | }); |
| 228 | |
| 229 | add_action('wp_cspa_after_wp_editor_field', function () use ($data) { |
| 230 | if (isset($data['placeholders'])) { |
| 231 | $this->placeholder_tags_table($data['placeholders']); |
| 232 | } |
| 233 | }); |
| 234 | |
| 235 | add_action('wp_cspa_after_email_editor_field', function () use ($data) { |
| 236 | if (isset($data['placeholders'])) { |
| 237 | $this->placeholder_tags_table($data['placeholders']); |
| 238 | } |
| 239 | }); |
| 240 | |
| 241 | $email_settings = [ |
| 242 | [ |
| 243 | $key . '_email_enabled' => [ |
| 244 | 'type' => 'checkbox', |
| 245 | 'label' => esc_html__('Enable Notification', 'wp-user-avatar'), |
| 246 | 'checkbox_label' => esc_html__('Enable', 'wp-user-avatar'), |
| 247 | 'value' => 'on', |
| 248 | 'default_value' => 'on', |
| 249 | 'description' => esc_html__('Check to enable this email notification.', 'wp-user-avatar') |
| 250 | ], |
| 251 | $key . '_email_subject' => [ |
| 252 | 'type' => 'text', |
| 253 | 'value' => $data['subject'], |
| 254 | 'label' => esc_html__('Subject Line', 'wp-user-avatar'), |
| 255 | 'description' => esc_html__('Enter the subject or title for the welcome message email.', 'wp-user-avatar') |
| 256 | ], |
| 257 | $key . '_email_content' => [ |
| 258 | 'type' => $email_content_field_type, |
| 259 | 'value' => $data['message'], |
| 260 | 'label' => esc_html__('Message Body', 'wp-user-avatar'), |
| 261 | ], |
| 262 | ] |
| 263 | ]; |
| 264 | } |
| 265 | |
| 266 | $this->settingsPageInstance->page_header($page_header); |
| 267 | $this->settingsPageInstance->main_content($email_settings); |
| 268 | $this->settingsPageInstance->remove_white_design(); |
| 269 | $this->settingsPageInstance->header_without_frills(); |
| 270 | $this->settingsPageInstance->build(true); |
| 271 | |
| 272 | $this->toggle_field_js_script(); |
| 273 | } |
| 274 | |
| 275 | public function handle_email_preview() |
| 276 | { |
| 277 | if ( ! isset($_GET['pp_email_preview']) || empty($_GET['pp_email_preview'])) return; |
| 278 | |
| 279 | if ( ! current_user_can('manage_options')) return; |
| 280 | |
| 281 | ppress_verify_nonce(); |
| 282 | |
| 283 | $key = sanitize_text_field($_GET['pp_email_preview']); |
| 284 | |
| 285 | |
| 286 | $data = wp_list_filter($this->email_notifications(), ['key' => $key]); |
| 287 | |
| 288 | $data = array_shift($data); |
| 289 | |
| 290 | $subject = ppress_get_setting($key . '_email_subject', $data['subject'], true); |
| 291 | $content = ppress_get_setting($key . '_email_content', $data['message'], true); |
| 292 | echo (new SendEmail('', $subject, $content))->templatified_email(); |
| 293 | exit; |
| 294 | } |
| 295 | |
| 296 | protected function placeholder_tags_table($placeholders) |
| 297 | { |
| 298 | ?> |
| 299 | <div class="ppress-placeholder-tags"> |
| 300 | <table class="widefat striped"> |
| 301 | <tbody> |
| 302 | <tr> |
| 303 | <th colspan="2"><?= esc_html__('Available placeholders for subject and message body', 'wp-user-avatar'); ?></th> |
| 304 | </tr> |
| 305 | <?php foreach ($placeholders as $tag => $description) : ?> |
| 306 | <tr> |
| 307 | <td><?= $tag ?></td> |
| 308 | <td><?= $description ?></td> |
| 309 | </tr> |
| 310 | <?php endforeach; ?> |
| 311 | </tbody> |
| 312 | </table> |
| 313 | </div> |
| 314 | <?php |
| 315 | } |
| 316 | |
| 317 | public function toggle_field_js_script() |
| 318 | { |
| 319 | ?> |
| 320 | <script type="text/javascript"> |
| 321 | (function ($) { |
| 322 | |
| 323 | $('#email_template_type').change(function () { |
| 324 | var cache = $('#customize_default_template_row'); |
| 325 | cache.hide(); |
| 326 | if (this.value === 'default') { |
| 327 | cache.show(); |
| 328 | } |
| 329 | }).change(); |
| 330 | |
| 331 | $('#email_content_type').change(function () { |
| 332 | var cache = $('#email_template_type_row, #customize_default_template_row'); |
| 333 | cache.hide(); |
| 334 | if (this.value === 'text/html') { |
| 335 | cache.show(); |
| 336 | $('#email_template_type').change(); |
| 337 | } |
| 338 | |
| 339 | }).change(); |
| 340 | })(jQuery); |
| 341 | </script> |
| 342 | <?php |
| 343 | } |
| 344 | |
| 345 | public static function get_instance() |
| 346 | { |
| 347 | static $instance = null; |
| 348 | |
| 349 | if (is_null($instance)) { |
| 350 | $instance = new self(); |
| 351 | } |
| 352 | |
| 353 | return $instance; |
| 354 | } |
| 355 | } |