wppaste
WordPress

WP_REST_Comments_Controller::check_target_post_permission( int $post_id ): true|WP_Error

Since
7.1.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:1945
Checks that a post can receive a comment from the current user.

Description

Used when changing the parent post of an existing comment, so that attaching a comment to a post is authorized the same way whichever path it arrives by.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_idint
Target post ID.

Return value

true|WP_Error
True if the post can receive the comment, error object otherwise.

Performance profile

How much work a call to WP_REST_Comments_Controller::check_target_post_permission() 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_post().

Scaling
Constant

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

Instructions
11–25

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • querycontent queryget_post()called directly

Further down the call graph this can also reach hook, option, cache, serialize 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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
always11__()
current_user_can()12current_user_can(), get_post()
always17current_user_can(), current_user_can(), get_post()
current_user_can()20current_user_can(), get_post(), __()
!current_user_can()23current_user_can(), current_user_can(), __(), rest_authorization_required_code(), status()
always25current_user_can(), current_user_can(), get_post(), __()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 47 instructions, 11–25 executed per call, 4 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.

Uses · 5

Used by · 1

Source code

	protected function check_target_post_permission( $post_id ) {		if ( ! $post_id ) {			return new WP_Error(				'rest_comment_invalid_post_id',				__( 'Sorry, you are not allowed to create this comment without a post.' ),				array( 'status' => 403 )			);		} 		/*		 * A comment needs either comment moderation rights or edit access to the		 * post, which is what check_edit_permission() grants on the post a comment		 * is moving away from. Requiring the same at the destination means both		 * ends of a move are authorized alike.		 */		if ( ! current_user_can( 'moderate_comments' ) && ! current_user_can( 'edit_post', $post_id ) ) {			return new WP_Error(				'rest_cannot_edit',				__( 'Sorry, you are not allowed to edit this comment.' ),				array( 'status' => rest_authorization_required_code() )			);		} 		$post = get_post( $post_id ); 		if ( ! $post ) {			return new WP_Error(				'rest_comment_invalid_post_id',				__( 'Sorry, you are not allowed to create this comment without a post.' ),				array( 'status' => 403 )			);		} 		/*		 * The create-time draft and comments-open rules are deliberately not applied		 * here, because moderators move comments onto posts whose discussion has		 * closed and onto drafts today. Enforcing them would break that without		 * blocking anything the capability check above already permits.		 */		return true;	}

Changelog

Introduced in 7.1.0. 2 changes between 6.7.10 and 7.1.3.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
Introduced.from the docblock
6.9.10
Parameter $request added.verified against source
6.9.10
Parameter $is_note added.verified against source

About this page

Parsed data
Generated from the wordpress-develop 6.7.10 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.