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