7B2 主题的私信邮件提醒功能,意思就是当用户发送私信时,接收方会收到邮件通知。这项功能确保即使用户没有实时查看站点,也不会错过重要的私信消息。具体效果如下图所示:
修改教程

在进行修改之前,请务必备份原始文件。修改路径:b2/Modules/Common/RestApi.php,找到第 2721 行的代码,删除或注释掉该行代码及其注释部分
ppublic static function sendDirectmessage($request){if (!b2_check_repo())return new \WP_Error('user_error', __('操作频次过高', 'b2'), array('status' => 403));$res = Directmessage::send_directmessage($request['user_id'], $request['content']);if (isset($res['error'])) {return new \WP_Error('dmsg_error', $res['error'], array('status' => 403));} else {return new \WP_REST_Response($res, 200);}}
修改为以下代码:(注意不要弄错)
public static function sendDirectmessage($request){if (!b2_check_repo()) {return new \WP_Error('user_error', __('操作频次过高', 'b2'), array('status' => 403));}// 获取当前用户ID$user_id = $request['user_id'];// 假设您有一个函数来获取当前登录用户的ID和信息(这里需要您自己实现或调整)// 例如:global $current_user; get_currentuserinfo(); (这是旧的方法,WordPress新版本可能不同)// 在WordPress REST API中,您可能需要通过其他方式获取当前用户,比如通过认证令牌// 这里我们暂时假设$current_user_id是已经获取到的当前登录用户的ID// 注意:这个变量在您的原始代码中不存在,需要根据您的实际环境进行获取$current_user_id = b2_get_current_user_id(); // 这是一个假设的函数,您需要根据实际情况替换$current_user = get_userdata($current_user_id); // 获取当前用户信息// 获取当前时间戳的小时部分$current_hour = date('Hi');// 获取用户在当前时间窗口内的邮件发送次数$email_count = wp_cache_get("email_count_{$user_id}_{$current_hour}", 'direct_message');if ($email_count === false) {$email_count = 0;}// 检查是否已达到发送限制if ($email_count >= MAX_EMAILS_PER_HOUR) {return new \WP_Error('email_limit_error', __('您已达到每小时的私信或邮件发送限制。', 'b2'), array('status' => 429));}// 发送私信$res = Directmessage::send_directmessage($user_id, $request['content']);if (isset($res['error'])) {// 如果私信发送失败,返回错误return new \WP_Error('dmsg_error', $res['error'], array('status' => 403));} else {// 准备邮件通知(如果用户有邮箱)$user_info = get_userdata($user_id);if ($user_info && $user_info->user_email) {// 更新邮件发送次数$email_count++;wp_cache_set("email_count_{$user_id}_{$current_hour}", $email_count, 'direct_message', TIME_WINDOW_HOURS * 3600); // 设置缓存有效期为1小时// 准备邮件内容$subject = '您有一条新的私信';$message = sprintf('<div style="max-width: 700px; background-color: #fff; margin: 0 auto; border: 1px solid #e3e3e3; font-family: Arial, sans-serif; color: #333; border-radius: 8px;"><!-- Header Section with Text Logo and Gradient Background --><div style="height: 80px; line-height: 80px; border-bottom: 1px solid #e3e3e3; padding: 0 30px; background: linear-gradient(135deg, #2c3e50, #4e2a84); border-top-left-radius: 8px; border-top-right-radius: 8px; text-align: center;"><a href="https://js.vmccc.com" style="display: block; text-decoration: none; font-size: 34px; color: #fff; letter-spacing: 4px;" target="_blank" rel="noopener">技术巢</a></div><!-- Body Section --><div style="padding: 30px; line-height: 1.6; background-color: #fafafa;"><p style="font-size: 16px; color: #555; margin-bottom: 10px;"><strong>%1$s</strong>,您收到一条私信,发送人:<span style="color:#0066ff;"><a href="#" style="color:#0066ff; text-decoration:none;" target="_blank" rel="noopener">%2$s</a></span>,私信内容:</p><!-- Message Content with Icon --><div style="padding: 15px; background-color: #e3f7ff; border-left: 5px solid #66c2ff; border-radius: 5px; margin-top: 15px;"><img src="https://js.vmccc.com/wp-content/uploads/2025011104392289.png" alt="Message Icon" style="width: 32px; vertical-align: middle; margin-right: 10px;"><span style="font-size: 18px; color: #333;">%3$s</span></div><!-- Warning Section with Icon --><div style="margin-top: 25px; padding: 15px; background-color: #fff3cd; border-left: 5px solid #ffec3d; border-radius: 5px;"><a href="%4$s" style="color:#0066ff; text-decoration:none; font-weight: bold;">点击这里 · 进入技术巢 </a> 查看详情。</div><!-- Footer Section --><p style="font-size: 14px; color: #888; margin-top: 30px; text-align: center;">我们无法辨别私信内容的真实性,请仔细甄别!</p></div><!-- Footer Notice with Icon --><div style="font-size: 12px; color: #999; border-top: 1px dotted #E3E3E3; padding: 15px; text-align: center; background-color: #f5f5f5; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;"><img src="https://img.icons8.com/ios/50/000000/email.png" alt="Email Icon" style="width: 20px; vertical-align: middle; margin-right: 2px;">本邮件为系统邮件,请勿回复。</div></div>',esc_html($user_info->display_name),esc_html($current_user->display_name), // 发送者名称wp_kses_post($request['content']), // 私信内容esc_url(home_url('/')), // 网站URLesc_html(get_bloginfo('name')) // 网站名称);// 发送邮件$headers = array('Content-Type: text/html; charset=UTF-8');wp_mail($user_info->user_email, $subject, $message, $headers);}// 无论是否发送邮件,都返回私信发送成功的响应// 注意:这里可能需要根据您的业务逻辑进行调整,比如是否需要在响应中包含邮件发送状态return new \WP_REST_Response($res, 200);}}
同时,在主题的 functions.php 文件中添加以下代码:
define('MAX_EMAILS_PER_HOUR', 5); // 每小时最多发送的邮件数define('TIME_WINDOW_HOURS', 1); // 限制的时间窗口(小时)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
没有相关内容!
暂无评论...