WP_REST_Comments_Controller::get_items_permissions_check( WP_REST_Request $request ): true|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:125
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
true|WP_Error- True if the request has read access, error object otherwise.
Performance profile
How much work a call to WP_REST_Comments_Controller::get_items_permissions_check() 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
- Scales with input
- Instructions
- 14–66
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 217.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
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 · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Comments_Controller::get_items_permissions_check() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14–33 | current_user_can() |
current_user_can() | 20–32 | current_user_can(), current_user_can() |
!current_user_can() | 27–39 | current_user_can(), __(), rest_authorization_required_code(), status() |
!current_user_can() && !empty($forbidden_params) | 34–49 | current_user_can(), __(), sprintf(), rest_authorization_required_code(), status() |
!empty($request) && $post_id === 0 && !current_user_can() | 37–38 | get_post(), current_user_can(), __(), rest_authorization_required_code(), status() |
!empty($request) && !empty($post_id) && !->check_read_post_permission() | 37 | get_post(), ->check_read_post_permission(), __(), rest_authorization_required_code(), status() |
!empty($forbidden_params) | 40–48 | current_user_can(), current_user_can(), __(), sprintf(), rest_authorization_required_code(), status() |
!empty($request) && !empty($post_id) && ->check_read_post_permission() && $post_id === 0 && !current_user_can() | 43 | get_post(), ->check_read_post_permission(), current_user_can(), __(), rest_authorization_required_code(), status() |
!empty($request) && $post_id !== 0 && !->check_post_type_supports_notes() && current_user_can() | 45–46 | get_post(), ->check_post_type_supports_notes(), current_user_can(), __() |
!empty($request) && $post_id === 0 && current_user_can() && !->check_post_type_supports_notes() | 49–50 | get_post(), current_user_can(), ->check_post_type_supports_notes(), current_user_can(), __() |
!empty($request) && !empty($post_id) && ->check_read_post_permission() && $post_id !== 0 && !->check_post_type_supports_notes() && current_user_can() | 51 | get_post(), ->check_read_post_permission(), ->check_post_type_supports_notes(), current_user_can(), __() |
!empty($request) && !empty($post_id) && ->check_read_post_permission() && $post_id === 0 && current_user_can() && !->check_post_type_supports_notes() | 55 | get_post(), ->check_read_post_permission(), current_user_can(), ->check_post_type_supports_notes(), current_user_can(), __() |
4 further outcomes, up to 66 instructions
!empty($request) && $post_id !== 0 && !->check_post_type_supports_notes() && !current_user_can() | 55–57 | get_post(), ->check_post_type_supports_notes(), current_user_can(), __(), sprintf(), rest_authorization_required_code(), status() |
!empty($request) && $post_id === 0 && !->check_post_type_supports_notes() | 59–61 | get_post(), current_user_can(), ->check_post_type_supports_notes(), current_user_can(), __(), sprintf(), rest_authorization_required_code(), status() |
!empty($request) && !empty($post_id) && ->check_read_post_permission() && $post_id !== 0 && !->check_post_type_supports_notes() && !current_user_can() | 61–62 | get_post(), ->check_read_post_permission(), ->check_post_type_supports_notes(), current_user_can(), __(), sprintf(), rest_authorization_required_code(), status() |
!empty($request) && !empty($post_id) && ->check_read_post_permission() && $post_id === 0 && !->check_post_type_supports_notes() | 65–66 | get_post(), ->check_read_post_permission(), current_user_can(), ->check_post_type_supports_notes(), current_user_can(), __(), sprintf(), rest_authorization_required_code(), status() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 217 | 14–66 | 36 | |
| 8.5 | 217 | 14–66 | 36 | |
| 8.4 | 217 | 14–66 | 36 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 223 | 14–69 | 36 | |
| 8.2 | 223 | 14–69 | 36 | |
| 8.1 | 223 | 14–69 | 36 | |
| 7.4 | 223 | 14–69 | 36 |
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
- get_post()Retrieves post data given a post ID or post object.
- __()Retrieves the translation of $text.
- rest_authorization_required_code()Returns a contextual HTTP error code for authorization failure.
- current_user_can()Returns whether the current user has the specified capability.
- WP_REST_Comments_Controller::check_read_post_permission()Checks if the post can be read.
- WP_Error::__construct()Initializes the error.
- WP_REST_Comments_Controller::check_post_type_supports_notes()Check if post type supports notes.
Source code
public function get_items_permissions_check( $request ) { $is_note = 'note' === $request['type']; $is_edit_context = 'edit' === $request['context']; $protected_params = array( 'author', 'author_exclude', 'author_email', 'type', 'status' ); $forbidden_params = array(); if ( ! empty( $request['post'] ) ) { foreach ( (array) $request['post'] as $post_id ) { $post = get_post( $post_id ); if ( ! empty( $post_id ) && $post && ! $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() ) ); } elseif ( 0 === $post_id && ! current_user_can( 'moderate_comments' ) ) { return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to read comments without a post.' ), array( 'status' => rest_authorization_required_code() ) ); } if ( $post && $is_note && ! $this->check_post_type_supports_notes( $post->post_type ) ) { if ( current_user_can( 'edit_post', $post->ID ) ) { return new WP_Error( 'rest_comment_not_supported_post_type', __( 'Sorry, this post type does not support notes.' ), array( 'status' => 403 ) ); } foreach ( $protected_params as $param ) { if ( 'status' === $param ) { if ( 'approve' !== $request[ $param ] ) { $forbidden_params[] = $param; } } elseif ( 'type' === $param ) { if ( 'comment' !== $request[ $param ] ) { $forbidden_params[] = $param; } } elseif ( ! empty( $request[ $param ] ) ) { $forbidden_params[] = $param; } } return new WP_Error( 'rest_forbidden_param', /* translators: %s: List of forbidden parameters. */ sprintf( __( 'Query parameter not permitted: %s' ), implode( ', ', $forbidden_params ) ), array( 'status' => rest_authorization_required_code() ) ); } } } // Re-map edit context capabilities when requesting `note` for a post. if ( $is_edit_context && $is_note && ! empty( $request['post'] ) ) { foreach ( (array) $request['post'] as $post_id ) { if ( ! current_user_can( 'edit_post', $post_id ) ) { return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit comments.' ), array( 'status' => rest_authorization_required_code() ) ); } } } elseif ( $is_edit_context && ! current_user_can( 'moderate_comments' ) ) { return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit comments.' ), array( 'status' => rest_authorization_required_code() ) ); } if ( ! current_user_can( 'edit_posts' ) ) { foreach ( $protected_params as $param ) { if ( 'status' === $param ) { if ( 'approve' !== $request[ $param ] ) { $forbidden_params[] = $param;Changelog
Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.