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
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
- Scaling
- Constant
- Instructions
- 13–48
- Plugin surface
- None
- Called by
- 2
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 117.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13 | __() |
| always | 18–19 | current_user_can(), get_post() |
!current_user_can() | 23 | current_user_can(), current_user_can(), get_post() |
| always | 24–25 | current_user_can(), __(), rest_authorization_required_code(), status() |
| always | 25–26 | current_user_can(), get_post(), __() |
!current_user_can() | 29 | current_user_can(), current_user_can(), __(), rest_authorization_required_code(), status() |
!current_user_can() | 30 | current_user_can(), current_user_can(), get_post(), __() |
->check_post_type_supports_notes() && ->check_read_post_permission() | 32–33 | current_user_can(), get_post(), ->check_post_type_supports_notes(), ->check_read_post_permission() |
| always | 32–36 | current_user_can(), get_post(), ->check_post_type_supports_notes(), __() |
!current_user_can() && ->check_post_type_supports_notes() && ->check_read_post_permission() | 37 | current_user_can(), current_user_can(), get_post(), ->check_post_type_supports_notes(), ->check_read_post_permission() |
!current_user_can() | 37–40 | current_user_can(), current_user_can(), get_post(), ->check_post_type_supports_notes(), __() |
->check_post_type_supports_notes() && !->check_read_post_permission() | 43–44 | current_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() | 48 | current_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
- __()Retrieves the translation of $text.
- current_user_can()Returns whether the current user has the specified capability.
- rest_authorization_required_code()Returns a contextual HTTP error code for authorization failure.
- get_post()Retrieves post data given a post ID or post object.
- WP_Error::__construct()Initializes the error.
- WP_REST_Comments_Controller::check_post_type_supports_notes()Check if post type supports notes.
- WP_REST_Comments_Controller::check_read_post_permission()Checks if the post can be read.
Used by · 2
- WP_REST_Comments_Controller::create_item_permissions_check()Checks if a given request has access to create a comment.
- WP_REST_Comments_Controller::update_item_permissions_check()Checks if a given REST request has access to update a comment.
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.
Signature, return type and hooks compared across 5 parsed releases.
$request added.verified against source$is_note added.verified against sourceAbout 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.