← All changes
|
includes/widgets/Login_Register_Form/Email_Handler.php
+143
-0
51.1.2
→
51.1.83
View file →
| @@ -1,0 +1,143 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace King_Addons\Widgets\Login_Register_Form; | |
| 4 | + | |
| 5 | +if (!defined('ABSPATH')) { | |
| 6 | + exit; // Exit if accessed directly. | |
| 7 | +} | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Email Handler for Login Register Form widget | |
| 11 | + */ | |
| 12 | +class Email_Handler | |
| 13 | +{ | |
| 14 | + /** | |
| 15 | + * Send registration emails | |
| 16 | + */ | |
| 17 | + public static function send_registration_emails($user_id, $settings) | |
| 18 | + { | |
| 19 | + $user = get_userdata($user_id); | |
| 20 | + if (!$user) { | |
| 21 | + return; | |
| 22 | + } | |
| 23 | + | |
| 24 | + // Prepare placeholders | |
| 25 | + $placeholders = self::get_email_placeholders($user); | |
| 26 | + | |
| 27 | + // Send user email | |
| 28 | + if ($settings['enable_user_email'] === 'yes') { | |
| 29 | + self::send_user_email($user, $settings, $placeholders); | |
| 30 | + } | |
| 31 | + | |
| 32 | + // Send admin email | |
| 33 | + if ($settings['enable_admin_email'] === 'yes') { | |
| 34 | + self::send_admin_email($user, $settings, $placeholders); | |
| 35 | + } | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Send welcome email to user | |
| 40 | + */ | |
| 41 | + private static function send_user_email($user, $settings, $placeholders) | |
| 42 | + { | |
| 43 | + $subject = self::replace_placeholders($settings['user_email_subject'], $placeholders); | |
| 44 | + $content = self::replace_placeholders($settings['user_email_content'], $placeholders); | |
| 45 | + | |
| 46 | + // Convert line breaks to HTML | |
| 47 | + $content = nl2br($content); | |
| 48 | + | |
| 49 | + // Set content type to HTML | |
| 50 | + add_filter('wp_mail_content_type', [__CLASS__, 'set_html_content_type']); | |
| 51 | + | |
| 52 | + $headers = array( | |
| 53 | + 'From: ' . get_bloginfo('name') . ' <' . get_option('admin_email') . '>', | |
| 54 | + ); | |
| 55 | + | |
| 56 | + $sent = wp_mail($user->user_email, $subject, $content, $headers); | |
| 57 | + | |
| 58 | + // Reset content type | |
| 59 | + remove_filter('wp_mail_content_type', [__CLASS__, 'set_html_content_type']); | |
| 60 | + | |
| 61 | + return $sent; | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Send notification email to admin | |
| 66 | + */ | |
| 67 | + private static function send_admin_email($user, $settings, $placeholders) | |
| 68 | + { | |
| 69 | + $admin_email = !empty($settings['admin_email_address']) ? $settings['admin_email_address'] : get_option('admin_email'); | |
| 70 | + | |
| 71 | + if (empty($admin_email)) { | |
| 72 | + return false; | |
| 73 | + } | |
| 74 | + | |
| 75 | + $subject = self::replace_placeholders($settings['admin_email_subject'], $placeholders); | |
| 76 | + $content = self::replace_placeholders($settings['admin_email_content'], $placeholders); | |
| 77 | + | |
| 78 | + // Convert line breaks to HTML | |
| 79 | + $content = nl2br($content); | |
| 80 | + | |
| 81 | + // Set content type to HTML | |
| 82 | + add_filter('wp_mail_content_type', [__CLASS__, 'set_html_content_type']); | |
| 83 | + | |
| 84 | + $headers = array( | |
| 85 | + 'From: ' . get_bloginfo('name') . ' <' . get_option('admin_email') . '>', | |
| 86 | + ); | |
| 87 | + | |
| 88 | + $sent = wp_mail($admin_email, $subject, $content, $headers); | |
| 89 | + | |
| 90 | + // Reset content type | |
| 91 | + remove_filter('wp_mail_content_type', [__CLASS__, 'set_html_content_type']); | |
| 92 | + | |
| 93 | + return $sent; | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Get email placeholders | |
| 98 | + */ | |
| 99 | + private static function get_email_placeholders($user) | |
| 100 | + { | |
| 101 | + $user_roles = $user->roles; | |
| 102 | + $user_role_name = ''; | |
| 103 | + | |
| 104 | + if (!empty($user_roles)) { | |
| 105 | + global $wp_roles; | |
| 106 | + if (!isset($wp_roles)) { | |
| 107 | + $wp_roles = new \WP_Roles(); | |
| 108 | + } | |
| 109 | + $role_key = $user_roles[0]; | |
| 110 | + $user_role_name = isset($wp_roles->roles[$role_key]) ? $wp_roles->roles[$role_key]['name'] : $role_key; | |
| 111 | + } | |
| 112 | + | |
| 113 | + $user_name = trim($user->first_name . ' ' . $user->last_name); | |
| 114 | + if (empty($user_name)) { | |
| 115 | + $user_name = $user->display_name; | |
| 116 | + } | |
| 117 | + | |
| 118 | + return [ | |
| 119 | + '{site_name}' => get_bloginfo('name'), | |
| 120 | + '{user_name}' => $user_name, | |
| 121 | + '{username}' => $user->user_login, | |
| 122 | + '{user_email}' => $user->user_email, | |
| 123 | + '{user_role}' => $user_role_name, | |
| 124 | + '{registration_date}' => date_i18n(get_option('date_format') . ' ' . get_option('time_format')), | |
| 125 | + ]; | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Replace placeholders in text | |
| 130 | + */ | |
| 131 | + private static function replace_placeholders($text, $placeholders) | |
| 132 | + { | |
| 133 | + return str_replace(array_keys($placeholders), array_values($placeholders), $text); | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * Set HTML content type for emails | |
| 138 | + */ | |
| 139 | + public static function set_html_content_type() | |
| 140 | + { | |
| 141 | + return 'text/html'; | |
| 142 | + } | |
| 143 | +} | |