wppaste
WordPress

WP_REST_Comments_Controller::create_item( WP_REST_Request $request ): WP_REST_Response|WP_Error

Since
4.7.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:568
Creates a comment.

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

$requestWP_REST_Request
Full details about the request.

Return value

WP_REST_Response|WP_Error
Response object on success, or error object on failure.

Performance profile

How much work a call to WP_REST_Comments_Controller::create_item() 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
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

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

Instructions
44–203

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

Plugin surface
3 hooks

Third-party callbacks on 'rest_pre_insert_comment', 'rest_insert_comment', 'rest_after_insert_comment' 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
  • cacheobject cachewp_cache_get()one call below WP_REST_Comments_Controller::create_item()
  • serializeserialisationmaybe_unserialize()one call below WP_REST_Comments_Controller::create_item()

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 · 30 distinct outcomes

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

WhenInstructionsCalls it makes
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && get_option() && empty($prepared_comment)44–51->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), __()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed()59–74->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), ->get_error_code(), __()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && is_user_logged_in() && get_option()60–66->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), __()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed()70–85->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed()73–92->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), ->get_error_code(), ->get_error_message()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in()75–89->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), ->get_error_code(), __()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in()86–100->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in()89–107->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), ->get_error_code(), ->get_error_message()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed()90–105->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), __()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in()106–120->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), __()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !isset($request)109–126->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($value)114–129->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error()
18 further outcomes, up to 203 instructions
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && isset($request)115–132->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && isset($request) && !empty($value)120–135->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($value)123–138->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && !isset($request)125–141->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && isset($request) && !empty($value)129–144->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && !empty($value)130–144->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && isset($request)131–147->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && isset($request) && !empty($value)136–150->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && !empty($value)139–153->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error()
empty($request) && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && isset($request) && !empty($value)145–159->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && !isset($request)152–170->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error(), current_user_can(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && isset($request)158–176->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error(), current_user_can(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($value)166–182->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error(), current_user_can(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && !isset($request)168–185->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error(), current_user_can(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && isset($request) && !empty($value)172–188->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error(), current_user_can(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && isset($request)174–191->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error(), current_user_can(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && !empty($value)182–197->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error(), current_user_can(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
empty($request) && !is_wp_error() && isset($prepared_comment) && ->check_is_comment_content_allowed() && !empty($prepared_comment) && is_user_logged_in() && isset($request) && !empty($value)188–203->prepare_item_for_database(), is_wp_error(), ->check_is_comment_content_allowed(), is_user_logged_in(), wp_get_current_user(), get_option(), wp_check_comment_data_max_lengths(), is_wp_error(), wp_allow_comment(), is_wp_error(), apply_filters(), is_wp_error(), wp_slash(), wp_filter_comment(), wp_insert_comment(), ->handle_status_param(), get_comment(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error(), current_user_can(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 303 instructions, 44–203 executed per call, 30 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_REST_Comments_Controller::create_item() runs, in this order:

  1. apply_filters( rest_pre_insert_comment )filterline 697 (+129 into the body)

    Filters a comment before it is inserted via the REST API.

  2. do_action( rest_insert_comment )actionline 728 (+160 into the body)

    Fires after a comment is created or updated via the REST API.

  3. do_action( rest_after_insert_comment )actionline 759 (+191 into the body)

    Fires completely after a comment is created or updated via the REST API.

Uses · 24

Show all 24

Source code

	public function create_item( $request ) {		if ( ! empty( $request['id'] ) ) {			return new WP_Error(				'rest_comment_exists',				__( 'Cannot create existing comment.' ),				array( 'status' => 400 )			);		} 		// Do not allow comments to be created with a non-default type.		if ( ! empty( $request['type'] ) && 'comment' !== $request['type'] ) {			return new WP_Error(				'rest_invalid_comment_type',				__( 'Cannot create a comment with that type.' ),				array( 'status' => 400 )			);		} 		$prepared_comment = $this->prepare_item_for_database( $request );		if ( is_wp_error( $prepared_comment ) ) {			return $prepared_comment;		} 		$prepared_comment['comment_type'] = 'comment'; 		if ( ! isset( $prepared_comment['comment_content'] ) ) {			$prepared_comment['comment_content'] = '';		} 		if ( ! $this->check_is_comment_content_allowed( $prepared_comment ) ) {			return new WP_Error(				'rest_comment_content_invalid',				__( 'Invalid comment content.' ),				array( 'status' => 400 )			);		} 		// Setting remaining values before wp_insert_comment so we can use wp_allow_comment().		if ( ! isset( $prepared_comment['comment_date_gmt'] ) ) {			$prepared_comment['comment_date_gmt'] = current_time( 'mysql', true );		} 		// Set author data if the user's logged in.		$missing_author = empty( $prepared_comment['user_id'] )			&& empty( $prepared_comment['comment_author'] )			&& empty( $prepared_comment['comment_author_email'] )			&& empty( $prepared_comment['comment_author_url'] ); 		if ( is_user_logged_in() && $missing_author ) {			$user = wp_get_current_user(); 			$prepared_comment['user_id']              = $user->ID;			$prepared_comment['comment_author']       = $user->display_name;			$prepared_comment['comment_author_email'] = $user->user_email;			$prepared_comment['comment_author_url']   = $user->user_url;		} 		// Honor the discussion setting that requires a name and email address of the comment author.		if ( get_option( 'require_name_email' ) ) {			if ( empty( $prepared_comment['comment_author'] ) || empty( $prepared_comment['comment_author_email'] ) ) {				return new WP_Error(					'rest_comment_author_data_required',					__( 'Creating a comment requires valid author name and email values.' ),					array( 'status' => 400 )				);			}		} 		if ( ! isset( $prepared_comment['comment_author_email'] ) ) {			$prepared_comment['comment_author_email'] = '';		} 		if ( ! isset( $prepared_comment['comment_author_url'] ) ) {			$prepared_comment['comment_author_url'] = '';		} 		if ( ! isset( $prepared_comment['comment_agent'] ) ) {			$prepared_comment['comment_agent'] = '';		}

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 6.7.7 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.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.