wp_get_unapproved_comment_author_email()
云策文档标注
概述
wp_get_unapproved_comment_author_email() 函数用于获取未批准评论的作者邮箱,主要用于允许评论者查看其待审评论。
关键要点
- 函数返回未批准评论作者的邮箱字符串(如果提供)。
- 通过 URL 参数 unapproved 和 moderation-hash 验证评论身份,确保安全性。
- 评论仅在发布后 10 分钟内可被作者查看,过期后无法获取邮箱。
- 如果无法从未批准评论获取邮箱,则回退到当前评论者的邮箱。
代码示例
function wp_get_unapproved_comment_author_email() {
$commenter_email = '';
if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
$comment_id = (int) $_GET['unapproved'];
$comment = get_comment( $comment_id );
if ( $comment && hash_equals( $_GET['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) {
// The comment will only be viewable by the comment author for 10 minutes.
$comment_preview_expires = strtotime( $comment->comment_date_gmt . '+10 minutes' );
if ( time() < $comment_preview_expires ) {
$commenter_email = $comment->comment_author_email;
}
}
}
if ( ! $commenter_email ) {
$commenter = wp_get_current_commenter();
$commenter_email = $commenter['comment_author_email'];
}
return $commenter_email;
}注意事项
- 确保 URL 参数正确传递以验证评论,防止未授权访问。
- 注意 10 分钟的时间窗口限制,过期后函数将返回当前评论者的邮箱。
- 函数依赖于 wp_hash() 和 wp_get_current_commenter() 等辅助函数。
原文内容
Gets unapproved comment author’s email.
Description
Used to allow the commenter to see their pending comment.
Source
function wp_get_unapproved_comment_author_email() {
$commenter_email = '';
if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
$comment_id = (int) $_GET['unapproved'];
$comment = get_comment( $comment_id );
if ( $comment && hash_equals( $_GET['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) {
// The comment will only be viewable by the comment author for 10 minutes.
$comment_preview_expires = strtotime( $comment->comment_date_gmt . '+10 minutes' );
if ( time() < $comment_preview_expires ) {
$commenter_email = $comment->comment_author_email;
}
}
}
if ( ! $commenter_email ) {
$commenter = wp_get_current_commenter();
$commenter_email = $commenter['comment_author_email'];
}
return $commenter_email;
}