wppaste
WordPress

check_comment( string $author, string $email, string $url, string $comment, string $user_ip, string $user_agent, string $comment_type ): bool

Since
1.2.0
Source
wp-includes/comment.php:39
Checks whether a comment passes internal checks to be allowed to add.

Description

If manual comment moderation is set in the administration, then all checks, regardless of their type and substance, will fail and the function will return false.

If the number of links exceeds the amount in the administration, then the check fails. If any of the parameter contents contain any disallowed words, then the check fails.

If the comment author was approved before, then the comment is automatically approved.

If all checks pass, the function will return true.

Compatibility

WordPress
since 1.2.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

$authorstring
Comment author name.
$emailstring
Comment author email.
$urlstring
Comment author URL.
$commentstring
Content of the comment.
$user_ipstring
Comment author IP address.
$user_agentstring
Comment author User-Agent.
$comment_typestring
Comment type, either user-submitted comment, trackback, or pingback.

Return value

bool
If all checks pass, true, otherwise false.

Performance profile

How much work a call to check_comment() 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
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
14–99

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

Plugin surface
2 hooks

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

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_user_by()called directly
  • regexregular expression over the whole inputpreg_match_all()called directly
  • sqldatabase query->get_var()called directly
  • cacheobject cachewp_cache_get()one call below check_comment()
  • serializeserialisationmaybe_unserialize()one call below check_comment()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

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

WhenInstructionsCalls it makes
always14get_option()
empty($mod_keys)37–45get_option(), apply_filters(), get_option(), get_option(), get_option()
$max_links40get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters()
!empty($mod_keys)45–54get_option(), apply_filters(), get_option(), get_option(), explode(), get_option()
!$max_links && empty($mod_keys)51–59get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), get_option()
!empty($mod_keys) && !$words && !empty($word) && $pattern56–66get_option(), apply_filters(), get_option(), get_option(), explode(), preg_quote()
!$max_links && !empty($mod_keys)59–68get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), explode(), get_option()
!$max_links && !empty($mod_keys) && !$words && !empty($word) && $pattern70–80get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), explode(), preg_quote()
empty($mod_keys) && $comment_type !== "trackback" && $comment_type !== "pingback" && $author !== "" && $email !== ""70–76get_option(), apply_filters(), get_option(), get_option(), get_option(), wp_unslash(), get_user_by(), ->prepare(), ->get_var()
!empty($mod_keys) && $comment_type !== "trackback" && $comment_type !== "pingback" && $author !== "" && $email !== ""78–85get_option(), apply_filters(), get_option(), get_option(), explode(), get_option(), wp_unslash(), get_user_by(), ->prepare(), ->get_var()
!$max_links && empty($mod_keys) && $comment_type !== "trackback" && $comment_type !== "pingback" && $author !== "" && $email !== ""84–90get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), get_option(), wp_unslash(), get_user_by(), ->prepare(), ->get_var()
!$max_links && !empty($mod_keys) && $comment_type !== "trackback" && $comment_type !== "pingback" && $author !== "" && $email !== ""92–99get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), explode(), get_option(), wp_unslash(), get_user_by(), ->prepare(), ->get_var()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15414–9922
8.515414–9922
8.415414–992226 fewer instructions than PHP 8.3
8.318014–10522
8.218014–10522
8.118014–10522
7.418014–10522

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 · 2

2 hooks fire while check_comment() runs, in this order:

  1. apply_filters( comment_text )filterline 48 (+9 into the body)

    Filters the text of a comment to be displayed.

  2. apply_filters( comment_max_links_url )filterline 65 (+26 into the body)

    Filters the number of links found in a comment.

Uses · 5

  • get_option()Retrieves an option value based on an option name.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • get_user_by()Retrieves user info by a given field.
  • wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
  • str_contains()Polyfill for `str_contains()` function added in PHP 8.0.

Used by · 1

Source code

function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) {	global $wpdb; 	// If manual moderation is enabled, skip all checks and return false.	if ( '1' === get_option( 'comment_moderation' ) ) {		return false;	} 	/** This filter is documented in wp-includes/comment-template.php */	$comment = apply_filters( 'comment_text', $comment, null, array() ); 	// Check for the number of external links if a max allowed number is set.	$max_links = get_option( 'comment_max_links' );	if ( $max_links ) {		$num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out ); 		/**		 * Filters the number of links found in a comment.		 *		 * @since 3.0.0		 * @since 4.7.0 Added the `$comment` parameter.		 *		 * @param int    $num_links The number of links found.		 * @param string $url       Comment author's URL. Included in allowed links total.		 * @param string $comment   Content of the comment.		 */		$num_links = apply_filters( 'comment_max_links_url', $num_links, $url, $comment ); 		/*		 * If the number of links in the comment exceeds the allowed amount,		 * fail the check by returning false.		 */		if ( $num_links >= $max_links ) {			return false;		}	} 	$mod_keys = trim( get_option( 'moderation_keys' ) ); 	// If moderation 'keys' (keywords) are set, process them.	if ( ! empty( $mod_keys ) ) {		$words = explode( "\n", $mod_keys ); 		foreach ( (array) $words as $word ) {			$word = trim( $word ); 			// Skip empty lines.			if ( empty( $word ) ) {				continue;			} 			/*			 * Do some escaping magic so that '#' (number of) characters in the spam			 * words don't break things:			 */			$word = preg_quote( $word, '#' ); 			/*			 * Check the comment fields for moderation keywords. If any are found,			 * fail the check for the given field by returning false.			 */			$pattern = "#$word#iu";			if ( preg_match( $pattern, $author ) ) {				return false;			}			if ( preg_match( $pattern, $email ) ) {				return false;			}			if ( preg_match( $pattern, $url ) ) {				return false;			}			if ( preg_match( $pattern, $comment ) ) {				return false;			}			if ( preg_match( $pattern, $user_ip ) ) {				return false;			}			if ( preg_match( $pattern, $user_agent ) ) {				return false;			}

Changelog

Introduced in 1.2.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 6.9.7 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.