wppaste
WordPress

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

Since
7.1.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:3247
Finalizes an attachment after client-side media processing.

Description

Applies the sub-size metadata collected from sideload responses in a single metadata update, then triggers the 'wp_generate_attachment_metadata' filter so that server-side plugins can process the attachment after all client-side operations (upload, thumbnail generation, sideloads) are complete.

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 1 of the 5 tracked releases, added in 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::finalize_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

Reaches the database via get_post().

Scaling
Scales with input

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

Instructions
12–81

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

Plugin surface
1 hook

Third-party callbacks on 'wp_generate_attachment_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

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()one call below WP_REST_Attachments_Controller::finalize_item()

Further down the call graph this can also reach serialize, cache, option 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 · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Attachments_Controller::finalize_item() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_wp_error()12->get_post(), is_wp_error()
always24–25->get_post(), is_wp_error(), ->validate_sub_size_provenance(), is_wp_error()
!is_wp_error() && !$sub_sizes && is_array($image_size) && array_intersect()52–54->get_post(), is_wp_error(), ->validate_sub_size_provenance(), is_wp_error(), wp_get_attachment_metadata(), ::get_special_image_sizes(), array_intersect(), __()
!is_wp_error()73–81->get_post(), is_wp_error(), ->validate_sub_size_provenance(), is_wp_error(), wp_get_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), ->get_sideloaded_file_names(), array_unique(), rest_get_route_for_post(), width(), ->prepare_item_for_response()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev25512–8146
8.525512–8146
8.425512–81463 fewer instructions than PHP 8.3
8.325812–8146
8.225812–8146
8.125812–8146
7.425812–8146

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::finalize_item() runs, in this order:

  1. apply_filters( wp_generate_attachment_metadata )filterline 3400 (+153 into the body)

    Filters the generated attachment meta data.

Uses · 15

Show all 15

Source code

	public function finalize_item( WP_REST_Request $request ) {		$attachment_id = (int) $request['id']; 		$post = $this->get_post( $attachment_id );		if ( is_wp_error( $post ) ) {			return $post;		} 		/**		 * Sub-size metadata collected from sideload responses. Confirm every		 * file name was produced by a prior sideload for this attachment before		 * storing it, so a client cannot make finalize record (and later read or		 * delete) another attachment's files.		 *		 * @var list<Image_Sub_Size> $sub_sizes		 */		$sub_sizes  = $request['sub_sizes'] ?? array();		$provenance = $this->validate_sub_size_provenance( $attachment_id, $sub_sizes );		if ( is_wp_error( $provenance ) ) {			return $provenance;		} 		$metadata = wp_get_attachment_metadata( $attachment_id );		if ( ! is_array( $metadata ) ) {			$metadata = array();		} 		// Apply all sub-size metadata collected from sideload responses.		foreach ( $sub_sizes as $sub_size ) {			$image_size = $sub_size['image_size']; 			// When multiple size names share identical dimensions the client			// sends a single sub-size entry with an array of names. Register the			// same file under each name.			if ( is_array( $image_size ) ) {				/*				 * Arrays carry regular sizes only, as the sideload endpoint				 * enforces. Each special size names a single file handled by one				 * of the branches below, so grouping one under a shared file				 * would write it to the wrong place; reject rather than guess.				 */				if ( array_intersect( $image_size, self::get_special_image_sizes() ) ) {					return new WP_Error(						'rest_invalid_sub_size_name',						__( 'A grouped sub-size entry may only name regular image sizes.' ),						array( 'status' => 400 )					);				} 				// As below: `file` is not required by the schema, and a size				// entry that names no file is not worth recording.				if ( empty( $sub_size['file'] ) ) {					continue;				} 				$metadata['sizes'] = $metadata['sizes'] ?? array(); 				foreach ( $image_size as $name ) {					$metadata['sizes'][ $name ] = array(						'width'     => $sub_size['width'] ?? 0,						'height'    => $sub_size['height'] ?? 0,						'file'      => $sub_size['file'],						'mime-type' => $sub_size['mime_type'] ?? '',						'filesize'  => $sub_size['filesize'] ?? 0,					);				}				continue;			} 			if ( 'original' === $image_size || 'scaled' === $image_size ) {				// Skip malformed entries so a bad payload cannot blank out the				// main file metadata.				if ( empty( $sub_size['file'] ) ) {					continue;				} 				/*				 * Record the supplied full-size image (from sideload_item()) as				 * the main file, keeping the current attached file as				 * `original_image`. A 'scaled' image is downsized and an

Changelog

Introduced in 7.1.0.

Signature, return type and hooks compared across 1 parsed release.

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.