返回介绍

wp_check_comment_flood()

发布于 2017-09-11 11:40:55 字数 4362 浏览 937 评论 0 收藏 0

wp_check_comment_flood( bool $is_flood,  string $ip,  string $email,  string $date,  bool $avoid_die = false )

Checks whether comment flooding is occurring.


description

Won’t run, if current user can manage options, so to not block administrators.


参数

$is_flood

(bool) (Required) Is a comment flooding occurring?

$ip

(string) (Required) Comment IP.

$email

(string) (Required) Comment author email address.

$date

(string) (Required) MySQL time string.

$avoid_die

(bool) (Optional) When true, a disallowed comment will result in the function returning a WP_Error object, rather than executing wp_die().

Default value: false


返回值

(bool) Whether comment flooding is occurring.


源代码

File: wp-includes/comment.php

function wp_check_comment_flood( $is_flood, $ip, $email, $date, $avoid_die = false ) {

	global $wpdb;

	// Another callback has declared a flood. Trust it.
	if ( true === $is_flood ) {
		return $is_flood;
	}

	// don't throttle admins or moderators
	if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) {
		return false;
	}
	$hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );

	if ( is_user_logged_in() ) {
		$user = get_current_user_id();
		$check_column = '`user_id`';
	} else {
		$user = $ip;
		$check_column = '`comment_author_IP`';
	}

	$sql = $wpdb->prepare(
		"SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( $check_column = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1",
		$hour_ago,
		$user,
		$email
	);
	$lasttime = $wpdb->get_var( $sql );
	if ( $lasttime ) {
		$time_lastcomment = mysql2date('U', $lasttime, false);
		$time_newcomment  = mysql2date('U', $date, false);
		/**
		 * Filters the comment flood status.
		 *
		 * @since 2.1.0
		 *
		 * @param bool $bool             Whether a comment flood is occurring. Default false.
		 * @param int  $time_lastcomment Timestamp of when the last comment was posted.
		 * @param int  $time_newcomment  Timestamp of when the new comment was posted.
		 */
		$flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
		if ( $flood_die ) {
			/**
			 * Fires before the comment flood message is triggered.
			 *
			 * @since 1.5.0
			 *
			 * @param int $time_lastcomment Timestamp of when the last comment was posted.
			 * @param int $time_newcomment  Timestamp of when the new comment was posted.
			 */
			do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
			if ( true === $avoid_die ) {
				return true;
			} else {
				if ( wp_doing_ajax() ) {
					die( __('You are posting comments too quickly. Slow down.') );
				}

				wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );
			}
		}
	}

	return false;
}

更新日志

Versiondescription
4.7.0Introduced.

相关函数

Uses

  • wp-includes/load.php: wp_doing_ajax()
  • wp-includes/capabilities.php: current_user_can()
  • wp-includes/l10n.php: __()
  • wp-includes/pluggable.php: is_user_logged_in()
  • wp-includes/functions.php: wp_die()
  • wp-includes/functions.php: mysql2date()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/plugin.php: do_action()
  • wp-includes/user.php: get_current_user_id()
  • wp-includes/wp-db.php: wpdb::get_var()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/comment.php: comment_flood_filter
  • wp-includes/comment.php: comment_flood_trigger
  • Show 8 more uses Hide more uses

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文