WP_REST_Posts_Controller::get_item_permissions_check( WP_REST_Request $request ): bool|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:580
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
bool|WP_Error- True if the request has read access for the item, WP_Error object or false otherwise.
Performance profile
How much work a call to WP_REST_Posts_Controller::get_item_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
- Trivial
- Scaling
- Constant
- Instructions
- 12–50
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
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 73.
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
- hookthird-party callbacks
do_action()one call below WP_REST_Posts_Controller::get_item_permissions_check()
Further down the call graph this can also reach query, 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 · 15 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Posts_Controller::get_item_permissions_check() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12–29 | ->get_post(), is_wp_error() |
!is_wp_error() | 23–32 | ->get_post(), is_wp_error(), ->check_read_permission() |
!is_wp_error() && empty($value) | 24–33 | ->get_post(), is_wp_error(), ->get_query_params() |
!is_wp_error() && ->check_update_permission() | 25–34 | ->get_post(), is_wp_error(), ->check_update_permission() |
!is_wp_error() && empty($value) | 27–36 | ->get_post(), is_wp_error(), ->get_query_params(), ->check_read_permission() |
!is_wp_error() && ->check_update_permission() | 28–37 | ->get_post(), is_wp_error(), ->check_update_permission(), ->check_read_permission() |
!is_wp_error() && ->check_update_permission() && empty($value) | 29–38 | ->get_post(), is_wp_error(), ->check_update_permission(), ->get_query_params() |
!is_wp_error() && !->check_update_permission() | 31 | ->get_post(), is_wp_error(), ->check_update_permission(), __(), rest_authorization_required_code(), status() |
!is_wp_error() && ->check_update_permission() && empty($value) | 32–41 | ->get_post(), is_wp_error(), ->check_update_permission(), ->get_query_params(), ->check_read_permission() |
!is_wp_error() && !empty($value) && hash_equals() | 33–42 | ->get_post(), is_wp_error(), ->get_query_params(), ->get_query_params(), hash_equals() |
!is_wp_error() && !empty($value) && hash_equals() | 36–45 | ->get_post(), is_wp_error(), ->get_query_params(), ->get_query_params(), hash_equals(), ->check_read_permission() |
!is_wp_error() && !empty($value) && !hash_equals() | 37 | ->get_post(), is_wp_error(), ->get_query_params(), ->get_query_params(), hash_equals(), __() |
3 further outcomes, up to 42 instructions
!is_wp_error() && ->check_update_permission() && !empty($value) && hash_equals() | 38–47 | ->get_post(), is_wp_error(), ->check_update_permission(), ->get_query_params(), ->get_query_params(), hash_equals() |
!is_wp_error() && ->check_update_permission() && !empty($value) && hash_equals() | 41–50 | ->get_post(), is_wp_error(), ->check_update_permission(), ->get_query_params(), ->get_query_params(), hash_equals(), ->check_read_permission() |
!is_wp_error() && ->check_update_permission() && !empty($value) && !hash_equals() | 42 | ->get_post(), is_wp_error(), ->check_update_permission(), ->get_query_params(), ->get_query_params(), hash_equals(), __() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 73 instructions, 12–50 executed per call, 9 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 · 8
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- rest_authorization_required_code()Returns a contextual HTTP error code for authorization failure.
- add_filter()Adds a callback function to a filter hook.
- WP_REST_Posts_Controller::get_post()Gets the post, if the ID is valid.
- WP_REST_Posts_Controller::check_update_permission()Checks if a post can be edited.
- WP_Error::__construct()Initializes the error.
- WP_REST_Posts_Controller::check_read_permission()Checks if a post can be read.
Used by · 1
- WP_REST_Menu_Items_Controller::get_item_permissions_check()Checks if a given request has access to read a menu item if they have access to edit them.
Source code
public function get_item_permissions_check( $request ) { $post = $this->get_post( $request['id'] ); if ( is_wp_error( $post ) ) { return $post; } if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) { return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this post.' ), array( 'status' => rest_authorization_required_code() ) ); } if ( $post && ! empty( $request->get_query_params()['password'] ) ) { // Check post password, and return error if invalid. if ( ! hash_equals( $post->post_password, $request->get_query_params()['password'] ) ) { return new WP_Error( 'rest_post_incorrect_password', __( 'Incorrect post password.' ), array( 'status' => 403 ) ); } } // Allow access to all password protected posts if the context is edit. if ( 'edit' === $request['context'] ) { add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 ); } if ( $post ) { return $this->check_read_permission( $post ); } return true; }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-posts-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.