wppaste
WordPress

wp_transition_comment_status( string $new_status, string $old_status, WP_Comment $comment )

Since
2.7.0
Source
wp-includes/comment.php:1929
Calls hooks for when a comment status transition occurs.

Description

Calls hooks for comment status transitions. If the new comment status is not the same as the previous comment status, then two hooks will be ran, the first is 'transition_commentstatus' with new status, old status, and comment data.
The next action called is 'comment
$old_statusto$new_status'. It has the comment data.

The final action will run whether or not the comment statuses are the same.
The action is named 'comment_$newstatus$comment->comment_type'.

Compatibility

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

$new_statusstring
New comment status.
$old_statusstring
Previous comment status.
$commentWP_Comment
Comment object.

Performance profile

How much work a call to wp_transition_comment_status() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
22–40

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

Plugin surface
3 hooks

Third-party callbacks on 'transition_comment_status', 'comment_{$old_status}_to_{$new_status}', 'comment_{$new_status}_{$comment->comment_type}' run inside this call, and their cost is not bounded by anything here.

Called by
3

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

What it touches

  • hookthird-party callbacksdo_action()called directly

What one call costs · 2 distinct outcomes

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

WhenInstructionsCalls it makes
$new_status === false22–26do_action()
$new_status !== false36–40do_action(), do_action(), do_action()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 40 instructions, 22–40 executed per call, 3 branches. The work does not change between versions.

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_transition_comment_status() runs, in this order:

  1. do_action( transition_comment_status )actionline 1959 (+30 into the body)

    Fires when the comment status is in transition.

  2. do_action( comment_{$old_status}_to_{$new_status} )actionline 1980 (+51 into the body)

    Fires when the comment status is in transition from one specific status to another.

  3. do_action( comment_{$new_status}_{$comment->comment_type} )actionline 2007 (+78 into the body)

    Fires when the status of a specific comment type is in transition.

Uses · 1

  • do_action()Calls the callback functions that have been added to an action hook.

Used by · 3

Source code

function wp_transition_comment_status( $new_status, $old_status, $comment ) {	/*	 * Translate raw statuses to human-readable formats for the hooks.	 * This is not a complete list of comment status, it's only the ones	 * that need to be renamed.	 */	$comment_statuses = array(		0         => 'unapproved',		'hold'    => 'unapproved', // wp_set_comment_status() uses "hold".		1         => 'approved',		'approve' => 'approved',   // wp_set_comment_status() uses "approve".	);	if ( isset( $comment_statuses[ $new_status ] ) ) {		$new_status = $comment_statuses[ $new_status ];	}	if ( isset( $comment_statuses[ $old_status ] ) ) {		$old_status = $comment_statuses[ $old_status ];	} 	// Call the hooks.	if ( $new_status !== $old_status ) {		/**		 * Fires when the comment status is in transition.		 *		 * @since 2.7.0		 *		 * @param string     $new_status The new comment status.		 * @param string     $old_status The old comment status.		 * @param WP_Comment $comment    Comment object.		 */		do_action( 'transition_comment_status', $new_status, $old_status, $comment ); 		/**		 * Fires when the comment status is in transition from one specific status to another.		 *		 * The dynamic portions of the hook name, `$old_status`, and `$new_status`,		 * refer to the old and new comment statuses, respectively.		 *		 * Possible hook names include:		 *		 *  - `comment_unapproved_to_approved`		 *  - `comment_spam_to_approved`		 *  - `comment_approved_to_unapproved`		 *  - `comment_spam_to_unapproved`		 *  - `comment_unapproved_to_spam`		 *  - `comment_approved_to_spam`		 *		 * @since 2.7.0		 *		 * @param WP_Comment $comment Comment object.		 */		do_action( "comment_{$old_status}_to_{$new_status}", $comment );	}	/**	 * Fires when the status of a specific comment type is in transition.	 *	 * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,	 * refer to the new comment status, and the type of comment, respectively.	 *	 * Typical comment types include 'comment', 'pingback', or 'trackback'.	 *	 * Possible hook names include:	 *	 *  - `comment_approved_comment`	 *  - `comment_approved_pingback`	 *  - `comment_approved_trackback`	 *  - `comment_unapproved_comment`	 *  - `comment_unapproved_pingback`	 *  - `comment_unapproved_trackback`	 *  - `comment_spam_comment`	 *  - `comment_spam_pingback`	 *  - `comment_spam_trackback`	 *	 * @since 2.7.0	 *	 * @param string     $comment_id The comment ID as a numeric string.	 * @param WP_Comment $comment    Comment object.	 */	do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );}

Changelog

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