wp_check_comment_flood

Checks whether comment flooding is occurring.

Description

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

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

Parameters (5)

0. $is_flood (bool)
Is a comment flooding occurring?
1. $ip (string)
Comment IP.
2. $email (string)
Comment author email address.
3. $date (string)
MySQL time string.
4. $avoid_die — Optional. (bool) => false
When true, a disallowed comment will result in the function returning a WP_Error object, rather than executing wp_die(). Default false.

Usage

  1. if ( !function_exists( 'wp_check_comment_flood' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Is a comment flooding occurring? 
  5. $is_flood = true; 
  6.  
  7. // Comment IP. 
  8. $ip = ''; 
  9.  
  10. // Comment author email address. 
  11. $email = ''; 
  12.  
  13. // MySQL time string. 
  14. $date = ''; 
  15. $avoid_die = false; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = wp_check_comment_flood($is_flood, $ip, $email, $date, $avoid_die); 
  19.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment.php  
  1. function wp_check_comment_flood( $is_flood, $ip, $email, $date, $avoid_die = false ) { 
  2.  
  3. global $wpdb; 
  4.  
  5. // Another callback has declared a flood. Trust it. 
  6. if ( true === $is_flood ) { 
  7. return $is_flood; 
  8.  
  9. // don't throttle admins or moderators 
  10. if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) { 
  11. return false; 
  12. $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ); 
  13.  
  14. if ( is_user_logged_in() ) { 
  15. $user = get_current_user_id(); 
  16. $check_column = '`user_id`'; 
  17. } else { 
  18. $user = $ip; 
  19. $check_column = '`comment_author_IP`'; 
  20.  
  21. $sql = $wpdb->prepare( 
  22. "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",  
  23. $hour_ago,  
  24. $user,  
  25. $email 
  26. ); 
  27. $lasttime = $wpdb->get_var( $sql ); 
  28. if ( $lasttime ) { 
  29. $time_lastcomment = mysql2date('U', $lasttime, false); 
  30. $time_newcomment = mysql2date('U', $date, false); 
  31. /** 
  32. * Filters the comment flood status. 
  33. * @since 2.1.0 
  34. * @param bool $bool Whether a comment flood is occurring. Default false. 
  35. * @param int $time_lastcomment Timestamp of when the last comment was posted. 
  36. * @param int $time_newcomment Timestamp of when the new comment was posted. 
  37. */ 
  38. $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment ); 
  39. if ( $flood_die ) { 
  40. /** 
  41. * Fires before the comment flood message is triggered. 
  42. * @since 1.5.0 
  43. * @param int $time_lastcomment Timestamp of when the last comment was posted. 
  44. * @param int $time_newcomment Timestamp of when the new comment was posted. 
  45. */ 
  46. do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment ); 
  47. if ( true === $avoid_die ) { 
  48. return true; 
  49. } else { 
  50. if ( wp_doing_ajax() ) { 
  51. die( __('You are posting comments too quickly. Slow down.') ); 
  52.  
  53. wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 ); 
  54.  
  55. return false;