WP_REST_Attachments_Controller::edit_media_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 5.5.0, 6.9.0, 7.1.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:1086
Compatibility
- WordPress
- since 7.1.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, WP_Error object on failure.
Performance profile
How much work a call to WP_REST_Attachments_Controller::edit_media_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
- Scales with input
- Instructions
- 24–268
- Plugin surface
- 1 hook
- Called by
- 0
Reads or writes the filesystem via file_exists().
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 518.
Third-party callbacks on 'wp_edited_image_metadata' 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_post()called directly - hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
file_exists()called directly
Further down the call graph this can also reach cache, option, 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 · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Attachments_Controller::edit_media_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 24–25 | wp_get_original_image_path(), wp_get_attachment_metadata(), __() |
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists() && !$image_name && !$new_attachment_post | 175–180 | wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), ->prepare_item_for_database(), wp_slash(), wp_insert_attachment(), is_wp_error(), ->get_error_code(), ->add_data() |
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists() && !is_wp_error() && !$image_name && !$new_attachment_post && empty($image_alt) && !wp_is_serving_rest_request() | 237–255 | wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), ->prepare_item_for_database(), wp_slash(), wp_insert_attachment(), is_wp_error(), get_post_meta(), wp_is_serving_rest_request(), wp_generate_attachment_metadata(), _wp_relative_upload_path(), apply_filters(), wp_update_attachment_metadata(), get_post(), ->prepare_item_for_response(), ->set_status(), rest_url(), ->header() |
wp_image_file_matches_image_meta() && $mime_type && empty($request) && file_exists() && !is_wp_error() && !$image_name && !$new_attachment_post && empty($image_alt) && !wp_is_serving_rest_request() | 238–256 | wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), ->prepare_item_for_database(), wp_slash(), wp_insert_attachment(), is_wp_error(), sanitize_text_field(), wp_is_serving_rest_request(), wp_generate_attachment_metadata(), _wp_relative_upload_path(), apply_filters(), wp_update_attachment_metadata(), get_post(), ->prepare_item_for_response(), ->set_status(), rest_url(), ->header() |
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists() && !is_wp_error() && !$image_name && !$new_attachment_post && empty($image_alt) && wp_is_serving_rest_request() | 241–259 | wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), ->prepare_item_for_database(), wp_slash(), wp_insert_attachment(), is_wp_error(), get_post_meta(), wp_is_serving_rest_request(), header(), wp_generate_attachment_metadata(), _wp_relative_upload_path(), apply_filters(), wp_update_attachment_metadata(), get_post(), ->prepare_item_for_response(), ->set_status(), rest_url(), ->header() |
wp_image_file_matches_image_meta() && $mime_type && empty($request) && file_exists() && !is_wp_error() && !$image_name && !$new_attachment_post && empty($image_alt) && wp_is_serving_rest_request() | 242–260 | wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), ->prepare_item_for_database(), wp_slash(), wp_insert_attachment(), is_wp_error(), sanitize_text_field(), wp_is_serving_rest_request(), header(), wp_generate_attachment_metadata(), _wp_relative_upload_path(), apply_filters(), wp_update_attachment_metadata(), get_post(), ->prepare_item_for_response(), ->set_status(), rest_url(), ->header() |
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists() && !is_wp_error() && !$image_name && !$new_attachment_post && !empty($image_alt) && !wp_is_serving_rest_request() | 245–263 | wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), ->prepare_item_for_database(), wp_slash(), wp_insert_attachment(), is_wp_error(), get_post_meta(), wp_slash(), update_post_meta(), wp_is_serving_rest_request(), wp_generate_attachment_metadata(), _wp_relative_upload_path(), apply_filters(), wp_update_attachment_metadata(), get_post(), ->prepare_item_for_response(), ->set_status(), rest_url(), ->header() |
wp_image_file_matches_image_meta() && $mime_type && empty($request) && file_exists() && !is_wp_error() && !$image_name && !$new_attachment_post && !empty($image_alt) && !wp_is_serving_rest_request() | 246–264 | wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), ->prepare_item_for_database(), wp_slash(), wp_insert_attachment(), is_wp_error(), sanitize_text_field(), wp_slash(), update_post_meta(), wp_is_serving_rest_request(), wp_generate_attachment_metadata(), _wp_relative_upload_path(), apply_filters(), wp_update_attachment_metadata(), get_post(), ->prepare_item_for_response(), ->set_status(), rest_url(), ->header() |
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists() && !is_wp_error() && !$image_name && !$new_attachment_post && !empty($image_alt) && wp_is_serving_rest_request() | 249–267 | wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), ->prepare_item_for_database(), wp_slash(), wp_insert_attachment(), is_wp_error(), get_post_meta(), wp_slash(), update_post_meta(), wp_is_serving_rest_request(), header(), wp_generate_attachment_metadata(), _wp_relative_upload_path(), apply_filters(), wp_update_attachment_metadata(), get_post(), ->prepare_item_for_response(), ->set_status(), rest_url(), ->header() |
wp_image_file_matches_image_meta() && $mime_type && empty($request) && file_exists() && !is_wp_error() && !$image_name && !$new_attachment_post && !empty($image_alt) && wp_is_serving_rest_request() | 250–268 | wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), ->prepare_item_for_database(), wp_slash(), wp_insert_attachment(), is_wp_error(), sanitize_text_field(), wp_slash(), update_post_meta(), wp_is_serving_rest_request(), header(), wp_generate_attachment_metadata(), _wp_relative_upload_path(), apply_filters(), wp_update_attachment_metadata(), get_post(), ->prepare_item_for_response(), ->set_status(), rest_url(), ->header() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 516 | 24–267 | 51 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 518 | 24–268 | 51 | |
| 8.4 | 518 | 24–268 | 51 | 10 fewer instructions than PHP 8.3 |
| 8.3 | 528 | 24–275 | 51 | |
| 8.2 | 528 | 24–275 | 51 | 1 more instruction than PHP 8.1 |
| 8.1 | 527 | 24–275 | 51 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 529 | 24–275 | 51 |
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_Attachments_Controller::edit_media_item() runs, in this order:
- apply_filters( wp_edited_image_metadata )filterline 1368 (+282 into the body)
Filters the meta data for the new image created by editing an existing image.
Uses · 26
- wp_get_original_image_path()Retrieves the path to an uploaded image file.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- wp_image_file_matches_image_meta()Determines if the image meta data is for the image source file.
- __()Retrieves the translation of $text.
- get_post_mime_type()Retrieves the mime type of an attachment based on the ID.
- _load_image_to_edit_path()Retrieves the path or URL of an attachment's attached file.
- wp_get_image_editor()Returns a WP_Image_Editor instance and loads file into it.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_basename()i18n-friendly version of basename().
- wp_upload_dir()Returns an array containing the current upload directory's path and URL.
- wp_unique_filename()Gets a filename that is sanitized and unique for the given directory.
- get_post()Retrieves post data given a post ID or post object.
Show all 26
- wp_insert_attachment()Inserts an attachment.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- sanitize_text_field()Sanitizes a string from user input or from the database.
- get_post_meta()Retrieves a post meta field for the given post ID.
- update_post_meta()Updates a post meta field based on the given post ID.
- wp_is_serving_rest_request()Determines whether WordPress is currently serving a REST API request.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- _wp_relative_upload_path()Returns relative path to an uploaded file.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- rest_url()Retrieves the URL to a REST endpoint.
- WP_Error::__construct()Initializes the error.
- WP_REST_Attachments_Controller::prepare_item_for_database()Prepares a single attachment for create or update.
- WP_REST_Attachments_Controller::prepare_item_for_response()Prepares a single attachment output for response.
Source code
public function edit_media_item( $request ) { require_once ABSPATH . 'wp-admin/includes/image.php'; $attachment_id = $request['id']; // This also confirms the attachment is an image. $image_file = wp_get_original_image_path( $attachment_id ); $image_meta = wp_get_attachment_metadata( $attachment_id ); if ( ! $image_meta || ! $image_file || ! wp_image_file_matches_image_meta( $request['src'], $image_meta, $attachment_id ) ) { return new WP_Error( 'rest_unknown_attachment', __( 'Unable to get meta information for file.' ), array( 'status' => 404 ) ); } $supported_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/heic' ); $mime_type = get_post_mime_type( $attachment_id ); if ( ! in_array( $mime_type, $supported_types, true ) ) { return new WP_Error( 'rest_cannot_edit_file_type', __( 'This type of file cannot be edited.' ), array( 'status' => 400 ) ); } // The `modifiers` param takes precedence over the older format. if ( isset( $request['modifiers'] ) ) { $modifiers = $request['modifiers']; } else { $modifiers = array(); if ( isset( $request['flip']['horizontal'] ) || isset( $request['flip']['vertical'] ) ) { $flip_args = array( 'vertical' => isset( $request['flip']['vertical'] ) ? (bool) $request['flip']['vertical'] : false, 'horizontal' => isset( $request['flip']['horizontal'] ) ? (bool) $request['flip']['horizontal'] : false, ); $modifiers[] = array( 'type' => 'flip', 'args' => array( 'flip' => $flip_args, ), ); } if ( ! empty( $request['rotation'] ) ) { $modifiers[] = array( 'type' => 'rotate', 'args' => array( 'angle' => $request['rotation'], ), ); } if ( isset( $request['x'], $request['y'], $request['width'], $request['height'] ) ) { $modifiers[] = array( 'type' => 'crop', 'args' => array( 'left' => $request['x'], 'top' => $request['y'], 'width' => $request['width'], 'height' => $request['height'], ), ); } if ( 0 === count( $modifiers ) ) { return new WP_Error( 'rest_image_not_edited', __( 'The image was not edited. Edit the image before applying the changes.' ), array( 'status' => 400 ) ); } }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.1.0 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-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.