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
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
- Scaling
- Scales with input
- Instructions
- 12–81
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via get_post().
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 255.
Third-party callbacks on 'wp_generate_attachment_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
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 12 | ->get_post(), is_wp_error() |
| always | 24–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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 255 | 12–81 | 46 | |
| 8.5 | 255 | 12–81 | 46 | |
| 8.4 | 255 | 12–81 | 46 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 258 | 12–81 | 46 | |
| 8.2 | 258 | 12–81 | 46 | |
| 8.1 | 258 | 12–81 | 46 | |
| 7.4 | 258 | 12–81 | 46 |
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:
- apply_filters( wp_generate_attachment_metadata )filterline 3400 (+153 into the body)
Filters the generated attachment meta data.
Uses · 15
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- delete_post_meta()Deletes a post meta field for the given post ID.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- rest_get_route_for_post()Gets the REST API route for a post.
- WP_REST_Attachments_Controller::get_post()
- WP_REST_Attachments_Controller::validate_sub_size_provenance()Validates the `sub_sizes` file names against what this attachment produced.
- WP_REST_Attachments_Controller::get_special_image_sizes()Returns the image size names which name a single file rather than a sub-size.
- WP_Error::__construct()Initializes the error.
Show all 15
- WP_REST_Attachments_Controller::get_sideloaded_file_names()Returns the file names which a finalize request may store for an attachment.
- WP_REST_Request::__construct()Constructor.
- WP_REST_Attachments_Controller::prepare_item_for_response()Prepares a single attachment output for response.
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 anChangelog
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.