wppaste
WordPress

WP_REST_Comments_Controller::check_target_post_permission( int $post_id, WP_REST_Request $request, bool $is_note = false ): true|WP_Error

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

Description

Used when creating a note and when changing the parent post of an existing comment or note, so that attaching content 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.
$requestWP_REST_Request
Full details about the request.
$is_notebooloptional
Whether the comment is a note. Default false.Default: false

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
13–48

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places 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 · 13 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
always13__()
always18–19current_user_can(), get_post()
!current_user_can()23current_user_can(), current_user_can(), get_post()
always24–25current_user_can(), __(), rest_authorization_required_code(), status()
always25–26current_user_can(), get_post(), __()
!current_user_can()29current_user_can(), current_user_can(), __(), rest_authorization_required_code(), status()
!current_user_can()30current_user_can(), current_user_can(), get_post(), __()
->check_post_type_supports_notes() && ->check_read_post_permission()32–33current_user_can(), get_post(), ->check_post_type_supports_notes(), ->check_read_post_permission()
always32–36current_user_can(), get_post(), ->check_post_type_supports_notes(), __()
!current_user_can() && ->check_post_type_supports_notes() && ->check_read_post_permission()37current_user_can(), current_user_can(), get_post(), ->check_post_type_supports_notes(), ->check_read_post_permission()
!current_user_can()37–40current_user_can(), current_user_can(), get_post(), ->check_post_type_supports_notes(), __()
->check_post_type_supports_notes() && !->check_read_post_permission()43–44current_user_can(), get_post(), ->check_post_type_supports_notes(), ->check_read_post_permission(), __(), rest_authorization_required_code(), status()
1 further outcome, up to 48 instructions
!current_user_can() && ->check_post_type_supports_notes() && !->check_read_post_permission()48current_user_can(), current_user_can(), get_post(), ->check_post_type_supports_notes(), ->check_read_post_permission(), __(), rest_authorization_required_code(), status()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 117 instructions, 13–48 executed per call, 10 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 · 7

Used by · 2

Source code

	protected function check_target_post_permission( int $post_id, WP_REST_Request $request, bool $is_note = false ) {		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 )			);		} 		/*		 * Notes are editorial content, so they may only be attached to a post the		 * user can edit. Any other 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 ( $is_note ) {			$can_target_post = current_user_can( 'edit_post', $post_id );		} else {			$can_target_post = current_user_can( 'moderate_comments' ) || current_user_can( 'edit_post', $post_id );		} 		if ( ! $can_target_post ) {			if ( $is_note ) {				return new WP_Error(					'rest_cannot_create_note',					__( 'Sorry, you are not allowed to create notes for this post.' ),					array( 'status' => rest_authorization_required_code() )				);			} 			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 remaining rules mirror the create-time checks for notes only. They are		 * deliberately not applied to other comments, because moderators move comments		 * onto posts whose discussion has closed and onto drafts today. Enforcing the		 * create-time rules there would break that without blocking anything the		 * capability check above already permits.		 */		if ( ! $is_note ) {			return true;		} 		if ( ! $this->check_post_type_supports_notes( $post->post_type ) ) {			return new WP_Error(				'rest_comment_not_supported_post_type',				__( 'Sorry, this post type does not support notes.' ),				array( 'status' => 403 )			);		} 		if ( 'trash' === $post->post_status ) {			return new WP_Error(				'rest_comment_trash_post',				__( 'Sorry, you are not allowed to create a comment on this post.' ),				array( 'status' => 403 )			);		} 		if ( ! $this->check_read_post_permission( $post, $request ) ) {			return new WP_Error(				'rest_cannot_read_post',				__( 'Sorry, you are not allowed to read the post for this comment.' ),				array( 'status' => rest_authorization_required_code() )

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.9.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.