WP_REST_Plugins_Controller::create_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 5.5.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php:273
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
$requestWP_REST_Request- Full details about the request.
Return value
WP_REST_Response|WP_Error- Response object on success, or WP_Error object on failure.
Performance profile
How much work a call to WP_REST_Plugins_Controller::create_item() 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
- Expensive
- Scaling
- Constant
- Instructions
- 6
- Plugin surface
- 1 hook
- Called by
- 0
Reaches an outbound HTTP request via wp_remote_get().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 245.
Third-party callbacks on 'plugins_update_check_locales' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - httpoutbound HTTP request
wp_remote_get()one call below WP_REST_Plugins_Controller::create_item()
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 · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Plugins_Controller::create_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 245 | 6 | 15 | |
| 8.5 | 245 | 6 | 15 | |
| 8.4 | 245 | 6 | 15 | 10 fewer instructions than PHP 8.3 |
| 8.3 | 255 | 9 | 15 | |
| 8.2 | 255 | 9 | 15 | |
| 8.1 | 255 | 9 | 15 | 12 more instructions than PHP 7.4 |
| 7.4 | 243 | 24–169 | 15 |
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.
Hooks and filters fired · 1
One hook fires while WP_REST_Plugins_Controller::create_item() runs, in this order:
- apply_filters( plugins_update_check_locales )filterline 381 (+108 into the body)
Filters the locales requested for plugin translations.
Uses · 16
- is_wp_error()Checks whether the given variable is a WordPress Error.
- plugins_api()Retrieves plugin installer pages from the WordPress.org Plugins API.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- __()Retrieves the translation of $text.
- get_available_languages()Gets all available languages based on the presence of *.mo and *.l10n.php files in a given directory.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_plugin_data()Parses the plugin contents to retrieve plugin's metadata.
- rest_url()Retrieves the URL to a REST endpoint.
- WP_REST_Plugins_Controller::is_filesystem_available()Determine if the endpoints are available.
- WP_Ajax_Upgrader_Skin::__construct()Constructor.
- Plugin_Upgrader::__construct()
- WP_Error::__construct()Initializes the error.
Show all 16
- WP_REST_Plugins_Controller::plugin_status_permission_check()Handle updating a plugin's status.
- WP_REST_Plugins_Controller::handle_plugin_status()Handle updating a plugin's status.
- Language_Pack_Upgrader::__construct()
- WP_REST_Plugins_Controller::prepare_item_for_response()Prepares the plugin for the REST response.
Source code
public function create_item( $request ) { global $wp_filesystem; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; $slug = $request['slug']; // Verify filesystem is accessible first. $filesystem_available = $this->is_filesystem_available(); if ( is_wp_error( $filesystem_available ) ) { return $filesystem_available; } $api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false, 'language_packs' => true, ), ) ); if ( is_wp_error( $api ) ) { if ( str_contains( $api->get_error_message(), 'Plugin not found.' ) ) { $api->add_data( array( 'status' => 404 ) ); } else { $api->add_data( array( 'status' => 500 ) ); } return $api; } $skin = new WP_Ajax_Upgrader_Skin(); $upgrader = new Plugin_Upgrader( $skin ); $result = $upgrader->install( $api->download_link ); if ( is_wp_error( $result ) ) { $result->add_data( array( 'status' => 500 ) ); return $result; } // This should be the same as $result above. if ( is_wp_error( $skin->result ) ) { $skin->result->add_data( array( 'status' => 500 ) ); return $skin->result; } if ( $skin->get_errors()->has_errors() ) { $error = $skin->get_errors(); $error->add_data( array( 'status' => 500 ) ); return $error; } if ( is_null( $result ) ) { // Pass through the error from WP_Filesystem if one was raised. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { return new WP_Error( 'unable_to_connect_to_filesystem', $wp_filesystem->errors->get_error_message(), array( 'status' => 500 ) ); } return new WP_Error( 'unable_to_connect_to_filesystem', __( 'Unable to connect to the filesystem. Please confirm your credentials.' ), array( 'status' => 500 ) ); }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 7.0.4 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.