WP_REST_Plugins_Controller::check_read_permission( string $plugin ): true|WP_Error
- Since
- 5.5.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php:211
Description
On multisite, this hides non-active network only plugins if the user does not have permission to manage network plugins.
Compatibility
- WordPress
- since 5.5.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
$pluginstring- The plugin file to check.
Return value
true|WP_Error- True if can read, a WP_Error instance otherwise.
Performance profile
How much work a call to WP_REST_Plugins_Controller::check_read_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
- Moderate
- Scaling
- Constant
- Instructions
- 12–35
- Plugin surface
- None
- Called by
- 4
Reads stored settings via get_option(), cached per request but not free on a cold cache.
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 46.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()one call below WP_REST_Plugins_Controller::check_read_permission()
Further down the call graph this can also reach hook, cache, serialize, query 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_Plugins_Controller::check_read_permission() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
->is_plugin_installed() && !is_multisite() | 12 | ->is_plugin_installed(), is_multisite() |
->is_plugin_installed() && is_multisite() && !is_network_only_plugin() | 16 | ->is_plugin_installed(), is_multisite(), is_network_only_plugin() |
!->is_plugin_installed() | 17 | ->is_plugin_installed(), __() |
->is_plugin_installed() && is_multisite() && is_network_only_plugin() && is_plugin_active() | 20 | ->is_plugin_installed(), is_multisite(), is_network_only_plugin(), is_plugin_active() |
->is_plugin_installed() && is_multisite() && is_network_only_plugin() && !is_plugin_active() && current_user_can() | 24 | ->is_plugin_installed(), is_multisite(), is_network_only_plugin(), is_plugin_active(), current_user_can() |
->is_plugin_installed() && is_multisite() && is_network_only_plugin() && !is_plugin_active() && !current_user_can() | 35 | ->is_plugin_installed(), is_multisite(), is_network_only_plugin(), is_plugin_active(), current_user_can(), __(), 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: 46 instructions, 12–35 executed per call, 5 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
- __()Retrieves the translation of $text.
- is_multisite()Determines whether Multisite is enabled.
- is_network_only_plugin()Checks for "Network: true" in the plugin header to see if this should be activated only as a network wide plugin. The plugin would also work when Multisite is not enabled.
- is_plugin_active()Determines whether a plugin is active.
- 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.
- WP_REST_Plugins_Controller::is_plugin_installed()Checks if the plugin is installed.
- WP_Error::__construct()Initializes the error.
Used by · 4
- WP_REST_Plugins_Controller::delete_item_permissions_check()Checks if a given request has access to delete a specific plugin.
- WP_REST_Plugins_Controller::get_item_permissions_check()Checks if a given request has access to get a specific plugin.
- WP_REST_Plugins_Controller::get_items()Retrieves a collection of plugins.
- WP_REST_Plugins_Controller::update_item_permissions_check()Checks if a given request has access to update a specific plugin.
Source code
protected function check_read_permission( $plugin ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; if ( ! $this->is_plugin_installed( $plugin ) ) { return new WP_Error( 'rest_plugin_not_found', __( 'Plugin not found.' ), array( 'status' => 404 ) ); } if ( ! is_multisite() ) { return true; } if ( ! is_network_only_plugin( $plugin ) || is_plugin_active( $plugin ) || current_user_can( 'manage_network_plugins' ) ) { return true; } return new WP_Error( 'rest_cannot_view_plugin', __( 'Sorry, you are not allowed to manage this plugin.' ), array( 'status' => rest_authorization_required_code() ) ); }Changelog
Introduced in 5.5.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 6.8.8 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-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.