WP_REST_Terms_Controller::update_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php:669
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
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_Terms_Controller::update_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
- Heavy
- Scaling
- Constant
- Instructions
- 12–122
- Plugin surface
- 2 hooks
- Called by
- 0
Reaches the database via get_term().
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 144.
Third-party callbacks on 'rest_insert_{$this->taxonomy}', 'rest_after_insert_{$this->taxonomy}' 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
- querycontent query
get_term()called directly - hookthird-party callbacks
do_action()called directly
Further down the call graph this can also reach 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 · 25 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Terms_Controller::update_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 12 | ->get_term(), is_wp_error() |
!is_wp_error() && isset($request) && !is_taxonomy_hierarchical() | 28 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), __() |
!is_wp_error() && isset($request) && is_taxonomy_hierarchical() | 38 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), __() |
!isset($request) && !empty($prepared_term) | 38 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error() |
!isset($request) && empty($prepared_term) | 52–54 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error() |
isset($request) && is_taxonomy_hierarchical() && !empty($prepared_term) | 54 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error() |
empty($prepared_term) && !empty($value) | 59 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error() |
empty($prepared_term) && !empty($value) | 68 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error() |
isset($request) && is_taxonomy_hierarchical() && empty($prepared_term) | 68–70 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error() |
!isset($request) && !empty($prepared_term) | 70–72 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error() |
!is_wp_error() && !isset($request) && empty($prepared_term) | 72–74 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response() |
isset($request) && is_taxonomy_hierarchical() && empty($prepared_term) && !empty($value) | 75 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error() |
13 further outcomes, up to 122 instructions
!empty($prepared_term) && !empty($value) | 77 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error() |
isset($request) && is_taxonomy_hierarchical() && empty($prepared_term) && !empty($value) | 84 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error() |
!empty($prepared_term) && !empty($value) | 86 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error() |
isset($request) && is_taxonomy_hierarchical() && !empty($prepared_term) | 86–88 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error() |
!is_wp_error() && empty($prepared_term) && !empty($value) | 88 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response() |
!is_wp_error() && isset($request) && is_taxonomy_hierarchical() && empty($prepared_term) | 88–90 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response() |
!is_wp_error() && !isset($request) && !empty($prepared_term) | 90–92 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response() |
isset($request) && is_taxonomy_hierarchical() && !empty($prepared_term) && !empty($value) | 93 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error() |
isset($request) && is_taxonomy_hierarchical() && !empty($prepared_term) && !empty($value) | 102 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error() |
!is_wp_error() && isset($request) && is_taxonomy_hierarchical() && empty($prepared_term) && !empty($value) | 104 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response() |
!is_wp_error() && !empty($prepared_term) && !empty($value) | 106 | ->get_term(), is_wp_error(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response() |
!is_wp_error() && isset($request) && is_taxonomy_hierarchical() && !empty($prepared_term) | 106–108 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response() |
!is_wp_error() && isset($request) && is_taxonomy_hierarchical() && !empty($prepared_term) && !empty($value) | 122 | ->get_term(), is_wp_error(), is_taxonomy_hierarchical(), get_term(), ->prepare_item_for_database(), wp_slash(), wp_update_term(), is_wp_error(), get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 144 instructions, 12–122 executed per call, 10 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.
Hooks and filters fired · 2
2 hooks fire while WP_REST_Terms_Controller::update_item() runs, in this order:
- do_action( rest_insert_{$this->taxonomy} )actionline 709 (+40 into the body)
Fires after a single term is created or updated via the REST API.
- do_action( rest_after_insert_{$this->taxonomy} )actionline 729 (+60 into the body)
Fires after a single term is completely created or updated via the REST API.
Uses · 14
- is_wp_error()Checks whether the given variable is a WordPress Error.
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- __()Retrieves the translation of $text.
- get_term()Gets all term data from database by term ID.
- wp_update_term()Updates term based on arguments provided.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- do_action()Calls the callback functions that have been added to an action hook.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- WP_REST_Terms_Controller::get_term()Get the term, if the ID is valid.
- WP_Error::__construct()Initializes the error.
- WP_REST_Terms_Controller::prepare_item_for_database()Prepares a single term for create or update.
- WP_REST_Terms_Controller::get_item_schema()Retrieves the term's schema, conforming to JSON Schema.
Show all 14
- WP_REST_Terms_Controller::update_additional_fields_for_object()
- WP_REST_Terms_Controller::prepare_item_for_response()Prepares a single term output for response.
Source code
public function update_item( $request ) { $term = $this->get_term( $request['id'] ); if ( is_wp_error( $term ) ) { return $term; } if ( isset( $request['parent'] ) ) { if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) { return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Cannot set parent term, taxonomy is not hierarchical.' ), array( 'status' => 400 ) ); } $parent = get_term( (int) $request['parent'], $this->taxonomy ); if ( ! $parent ) { return new WP_Error( 'rest_term_invalid', __( 'Parent term does not exist.' ), array( 'status' => 400 ) ); } } $prepared_term = $this->prepare_item_for_database( $request ); // Only update the term if we have something to update. if ( ! empty( $prepared_term ) ) { $update = wp_update_term( $term->term_id, $term->taxonomy, wp_slash( (array) $prepared_term ) ); if ( is_wp_error( $update ) ) { return $update; } } $term = get_term( $term->term_id, $this->taxonomy ); /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */ do_action( "rest_insert_{$this->taxonomy}", $term, $request, false ); $schema = $this->get_item_schema(); if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { $meta_update = $this->meta->update_value( $request['meta'], $term->term_id ); if ( is_wp_error( $meta_update ) ) { return $meta_update; } } $fields_update = $this->update_additional_fields_for_object( $term, $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update; } $request->set_param( 'context', 'edit' ); /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */ do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, false ); $response = $this->prepare_item_for_response( $term, $request ); return rest_ensure_response( $response ); }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-terms-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.