wp_check_comment_data()
云策文档标注
概述
wp_check_comment_data() 函数用于检查评论数据是否通过内部验证或包含不允许的内容,返回评论的批准状态或 WP_Error。
关键要点
- 参数:$comment_data(数组,必需),包含插入评论的参数。
- 返回值:成功时返回批准状态(0、1、'spam' 或 'trash'),否则返回 WP_Error。
- 功能:基于用户身份(如作者或管理员)和内容检查(如 check_comment() 和 wp_check_comment_disallowed_list())确定评论批准状态。
- Hook:通过 pre_comment_approved 过滤器允许在设置批准状态前进行自定义修改。
代码示例
function wp_check_comment_data( $comment_data ) {
global $wpdb;
if ( ! empty( $comment_data['user_id'] ) ) {
$user = get_userdata( $comment_data['user_id'] );
$post_author = (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
$comment_data['comment_post_ID']
)
);
}
if ( isset( $user ) && ( $comment_data['user_id'] === $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
$approved = 1;
} else {
if ( check_comment(
$comment_data['comment_author'],
$comment_data['comment_author_email'],
$comment_data['comment_author_url'],
$comment_data['comment_content'],
$comment_data['comment_author_IP'],
$comment_data['comment_agent'],
$comment_data['comment_type']
) ) {
$approved = 1;
} else {
$approved = 0;
}
if ( wp_check_comment_disallowed_list(
$comment_data['comment_author'],
$comment_data['comment_author_email'],
$comment_data['comment_author_url'],
$comment_data['comment_content'],
$comment_data['comment_author_IP'],
$comment_data['comment_agent']
) ) {
$approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam';
}
}
return apply_filters( 'pre_comment_approved', $approved, $comment_data );
}注意事项
- 函数在 WordPress 6.7.0 版本中引入。
- 返回的批准状态可通过 pre_comment_approved 过滤器进行修改,返回 WP_Error 可跳过进一步处理。
- 相关函数包括 wp_check_comment_disallowed_list()、check_comment()、get_userdata() 等。
原文内容
Checks whether comment data passes internal checks or has disallowed content.
Parameters
$comment_dataarrayrequired-
Array of arguments for inserting a comment.
Source
function wp_check_comment_data( $comment_data ) {
global $wpdb;
if ( ! empty( $comment_data['user_id'] ) ) {
$user = get_userdata( $comment_data['user_id'] );
$post_author = (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
$comment_data['comment_post_ID']
)
);
}
if ( isset( $user ) && ( $comment_data['user_id'] === $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
// The author and the admins get respect.
$approved = 1;
} else {
// Everyone else's comments will be checked.
if ( check_comment(
$comment_data['comment_author'],
$comment_data['comment_author_email'],
$comment_data['comment_author_url'],
$comment_data['comment_content'],
$comment_data['comment_author_IP'],
$comment_data['comment_agent'],
$comment_data['comment_type']
) ) {
$approved = 1;
} else {
$approved = 0;
}
if ( wp_check_comment_disallowed_list(
$comment_data['comment_author'],
$comment_data['comment_author_email'],
$comment_data['comment_author_url'],
$comment_data['comment_content'],
$comment_data['comment_author_IP'],
$comment_data['comment_agent']
) ) {
$approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam';
}
}
/**
* Filters a comment's approval status before it is set.
*
* @since 2.1.0
* @since 4.9.0 Returning a WP_Error value from the filter will short-circuit comment insertion
* and allow skipping further processing.
*
* @param int|string|WP_Error $approved The approval status. Accepts 1, 0, 'spam', 'trash',
* or WP_Error.
* @param array $commentdata Comment data.
*/
return apply_filters( 'pre_comment_approved', $approved, $comment_data );
}
Hooks
- apply_filters( ‘pre_comment_approved’, int|string|WP_Error $approved, array $commentdata )
-
Filters a comment’s approval status before it is set.
Changelog
| Version | Description |
|---|---|
| 6.7.0 | Introduced. |