wppaste
WordPress

_wp_batch_update_comment_type()

Since
5.5.0
Source
wp-includes/comment.php:4418
Updates the comment type for a batch of comments.

Compatibility

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

Performance profile

How much work a call to _wp_batch_update_comment_type() 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 ->query().

Scaling
Constant

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

Instructions
33–88

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

Plugin surface
1 hook

Third-party callbacks on 'wp_update_comment_type_batch_size' 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

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • sqldatabase query->query()called directly
  • cacheobject cachewp_cache_get()one call below _wp_batch_update_comment_type()
  • serializeserialisationmaybe_unserialize()one call below _wp_batch_update_comment_type()

Further down the call graph this can also reach query 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 · 8 distinct outcomes

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

WhenInstructionsCalls it makes
always33time(), ->prepare(), ->query(), get_option(), time(), wp_schedule_single_event()
$lock_result39time(), ->prepare(), ->query(), get_option(), time(), time(), wp_schedule_single_event()
always41time(), ->prepare(), ->query(), time(), update_option(), ->get_var(), update_option(), delete_option()
!$lock_result52time(), ->prepare(), ->query(), get_option(), time(), time(), update_option(), ->get_var(), update_option(), delete_option()
always64time(), ->prepare(), ->query(), time(), update_option(), ->get_var(), time(), wp_schedule_single_event(), apply_filters(), ->prepare(), ->get_col(), delete_option()
!$lock_result75time(), ->prepare(), ->query(), get_option(), time(), time(), update_option(), ->get_var(), time(), wp_schedule_single_event(), apply_filters(), ->prepare(), ->get_col(), delete_option()
always77time(), ->prepare(), ->query(), time(), update_option(), ->get_var(), time(), wp_schedule_single_event(), apply_filters(), ->prepare(), ->get_col(), ->query(), clean_comment_cache(), delete_option()
!$lock_result88time(), ->prepare(), ->query(), get_option(), time(), time(), update_option(), ->get_var(), time(), wp_schedule_single_event(), apply_filters(), ->prepare(), ->get_col(), ->query(), clean_comment_cache(), delete_option()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10633–885
8.510633–885
8.410633–8854 fewer instructions than PHP 8.3
8.311033–925
8.211033–925
8.111033–925
7.411033–925

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

One hook fires while _wp_batch_update_comment_type() runs, in this order:

  1. apply_filters( wp_update_comment_type_batch_size )filterline 4463 (+45 into the body)

    Filters the comment batch size for updating the comment type.

Uses · 6

Source code

function _wp_batch_update_comment_type() {	global $wpdb; 	$lock_name = 'update_comment_type.lock'; 	// Try to lock.	$lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) ); 	if ( ! $lock_result ) {		$lock_result = get_option( $lock_name ); 		// Bail if we were unable to create a lock, or if the existing lock is still valid.		if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) {			wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' );			return;		}	} 	// Update the lock, as by this point we've definitely got a lock, just need to fire the actions.	update_option( $lock_name, time() ); 	// Check if there's still an empty comment type.	$empty_comment_type = $wpdb->get_var(		"SELECT comment_ID FROM $wpdb->comments		WHERE comment_type = ''		LIMIT 1"	); 	// No empty comment type, we're done here.	if ( ! $empty_comment_type ) {		update_option( 'finished_updating_comment_type', true );		delete_option( $lock_name );		return;	} 	// Empty comment type found? We'll need to run this script again.	wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' ); 	/**	 * Filters the comment batch size for updating the comment type.	 *	 * @since 5.5.0	 *	 * @param int $comment_batch_size The comment batch size. Default 100.	 */	$comment_batch_size = (int) apply_filters( 'wp_update_comment_type_batch_size', 100 ); 	// Get the IDs of the comments to update.	$comment_ids = $wpdb->get_col(		$wpdb->prepare(			"SELECT comment_ID			FROM {$wpdb->comments}			WHERE comment_type = ''			ORDER BY comment_ID DESC			LIMIT %d",			$comment_batch_size		)	); 	if ( $comment_ids ) {		$comment_id_list = implode( ',', $comment_ids ); 		// Update the `comment_type` field value to be `comment` for the next batch of comments.		$wpdb->query(			"UPDATE {$wpdb->comments}			SET comment_type = 'comment'			WHERE comment_type = ''			AND comment_ID IN ({$comment_id_list})" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared		); 		// Make sure to clean the comment cache.		clean_comment_cache( $comment_ids );	} 	delete_option( $lock_name );}

Changelog

Introduced in 5.5.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.