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
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
- Scaling
- Constant
- Instructions
- 11–25
- Plugin surface
- None
- Called by
- 1
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 47.
Nothing here hands control to plugin code.
1 place 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 · 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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11 | __() |
current_user_can() | 12 | current_user_can(), get_post() |
| always | 17 | current_user_can(), current_user_can(), get_post() |
current_user_can() | 20 | current_user_can(), get_post(), __() |
!current_user_can() | 23 | current_user_can(), current_user_can(), __(), rest_authorization_required_code(), status() |
| always | 25 | current_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
- __()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.
Used by · 1
- 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( $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.
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.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.