wppaste
WordPress

WP_REST_Attachments_Controller::edit_media_item( WP_REST_Request $request ): WP_REST_Response|WP_Error

Since
5.5.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:512
Applies edits to a media item and creates a new attachment record.

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, 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

Reads or writes the filesystem via file_exists().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
24–231

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 424.

Plugin surface
1 hook

Third-party callbacks on 'wp_edited_image_metadata' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_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 · 16 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.

WhenInstructionsCalls it makes
always24–25wp_get_original_image_path(), wp_get_attachment_metadata(), __()
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists() && is_wp_error()64wp_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(), __(), crop()
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists() && !$modifiers && $rotate !== 085–87wp_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(), ->rotate(), is_wp_error(), __(), angle()
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists()97–101wp_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(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error()
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && !file_exists() && !$image_name101wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error()
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists() && !$modifiers125–132wp_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(), ->get_size(), round(), round(), round(), round(), ->crop(), is_wp_error(), __(), rotate()
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && file_exists()134–147wp_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(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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()138–147wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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() && empty($image_alt) && !wp_is_serving_rest_request()193–219wp_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(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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 && !isset($request) && empty($request) && file_exists() && !is_wp_error() && empty($image_alt) && wp_is_serving_rest_request()197–223wp_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(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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 && !isset($request) && empty($request) && !file_exists() && !is_wp_error() && empty($image_alt) && !wp_is_serving_rest_request()197–222wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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 && !isset($request) && empty($request) && file_exists() && !is_wp_error() && !empty($image_alt) && !wp_is_serving_rest_request()201–227wp_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(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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()
4 further outcomes, up to 231 instructions
wp_image_file_matches_image_meta() && $mime_type && !isset($request) && empty($request) && !file_exists() && !is_wp_error() && empty($image_alt) && wp_is_serving_rest_request()201–226wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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 && !isset($request) && empty($request) && file_exists() && !is_wp_error() && !empty($image_alt) && wp_is_serving_rest_request()205–231wp_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(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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 && !isset($request) && empty($request) && !file_exists() && !is_wp_error() && !empty($image_alt) && !wp_is_serving_rest_request()205–227wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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 && !isset($request) && empty($request) && !file_exists() && !is_wp_error() && !empty($image_alt) && wp_is_serving_rest_request()209–231wp_get_original_image_path(), wp_get_attachment_metadata(), wp_image_file_matches_image_meta(), get_post_mime_type(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), pathinfo(), wp_basename(), wp_upload_dir(), wp_unique_filename(), ->save(), is_wp_error(), get_post(), 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()

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

PHPCompiledExecutedBranchesNotes
8.6-dev42424–23138
8.542424–23138
8.442424–2313810 fewer instructions than PHP 8.3
8.343424–24138
8.243424–241381 more instruction than PHP 8.1
8.143324–24138
7.443324–24138

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:

  1. apply_filters( wp_edited_image_metadata )filterline 755 (+243 into the body)

    Filters the meta data for the new image created by editing an existing image.

Uses · 24

Show all 24

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 ( ! 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 )				);			}		} 		/*		 * If the file doesn't exist, attempt a URL fopen on the src link.		 * This can occur with certain file replication plugins.		 * Keep the original file path to get a modified name later.		 */		$image_file_to_edit = $image_file;		if ( ! file_exists( $image_file_to_edit ) ) {			$image_file_to_edit = _load_image_to_edit_path( $attachment_id );		} 		$image_editor = wp_get_image_editor( $image_file_to_edit ); 		if ( is_wp_error( $image_editor ) ) {

Changelog

Introduced in 5.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.