wppaste
WordPress

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

Since
4.7.0
Source
wp-includes/comment.php:909
Checks whether comment flooding is occurring.

Description

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

Compatibility

WordPress
since 4.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$is_floodbool
Is a comment flooding occurring?
$ipstring
Comment author's IP address.
$emailstring
Comment author's email address.
$datestring
MySQL time string.
$avoid_diebooloptional
When true, a disallowed comment will result in the function returning without executing wp_die() or die(). Default false.Default: false

Return value

bool
Whether comment flooding is occurring.

Performance profile

How much work a call to wp_check_comment_flood() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_var().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–96

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 101.

Plugin surface
3 hooks

Third-party callbacks on 'comment_flood_filter', 'comment_flood_trigger', 'comment_flood_message' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • sqldatabase query->get_var()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 13 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_check_comment_flood() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$is_flood === true9none
$is_flood !== true && current_user_can()13current_user_can()
$is_flood !== true17current_user_can(), current_user_can()
$is_flood !== true && !current_user_can() && !is_user_logged_in()49current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), ->prepare(), ->get_var()
$is_flood !== true && !current_user_can() && is_user_logged_in()52current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), get_current_user_id(), ->prepare(), ->get_var()
$is_flood !== true && !current_user_can() && !is_user_logged_in()69current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), ->prepare(), ->get_var(), mysql2date(), mysql2date(), apply_filters()
$is_flood !== true && !current_user_can() && is_user_logged_in()72current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), get_current_user_id(), ->prepare(), ->get_var(), mysql2date(), mysql2date(), apply_filters()
$is_flood !== true && !current_user_can() && !is_user_logged_in()75current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), ->prepare(), ->get_var(), mysql2date(), mysql2date(), apply_filters(), do_action()
$is_flood !== true && !current_user_can() && is_user_logged_in()78current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), get_current_user_id(), ->prepare(), ->get_var(), mysql2date(), mysql2date(), apply_filters(), do_action()
$is_flood !== true && !current_user_can() && !is_user_logged_in() && !wp_doing_ajax()90current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), ->prepare(), ->get_var(), mysql2date(), mysql2date(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), wp_die()
$is_flood !== true && !current_user_can() && !is_user_logged_in() && wp_doing_ajax()93current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), ->prepare(), ->get_var(), mysql2date(), mysql2date(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), exit(), wp_die()
$is_flood !== true && !current_user_can() && is_user_logged_in() && !wp_doing_ajax()93current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), get_current_user_id(), ->prepare(), ->get_var(), mysql2date(), mysql2date(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), wp_die()
1 further outcome, up to 96 instructions
$is_flood !== true && !current_user_can() && is_user_logged_in() && wp_doing_ajax()96current_user_can(), current_user_can(), time(), gmdate(), is_user_logged_in(), get_current_user_id(), ->prepare(), ->get_var(), mysql2date(), mysql2date(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), exit(), wp_die()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1019–968
8.51019–968
8.41019–9682 more instructions than PHP 8.3
8.3999–938
8.2999–938
8.1999–938
7.4999–938

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while wp_check_comment_flood() runs, in this order:

  1. apply_filters( comment_flood_filter )filterline 954 (+45 into the body)

    Filters the comment flood status.

  2. do_action( comment_flood_trigger )actionline 965 (+56 into the body)

    Fires before the comment flood message is triggered.

  3. apply_filters( comment_flood_message )filterline 977 (+68 into the body)

    Filters the comment flood error message.

Uses · 9

  • current_user_can()Returns whether the current user has the specified capability.
  • is_user_logged_in()Determines whether the current visitor is a logged in user.
  • get_current_user_id()Gets the current user's ID.
  • mysql2date()Converts given MySQL date string into a different format.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • do_action()Calls the callback functions that have been added to an action hook.
  • __()Retrieves the translation of $text.
  • wp_doing_ajax()Determines whether the current request is a WordPress Ajax request.
  • wp_die()Kills WordPress execution and displays HTML page with an error message.

Source code

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 ( $avoid_die ) {				return true;			} else {				/**				 * Filters the comment flood error message.				 *				 * @since 5.2.0				 *				 * @param string $comment_flood_message Comment flood error message.				 */				$comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) ); 				if ( wp_doing_ajax() ) {					die( $comment_flood_message );				} 				wp_die( $comment_flood_message, 429 );			}		}	} 	return false;

Changelog

Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/comment.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.