WP_REST_Attachments_Controller::sideload_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:2752
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::sideload_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
- 5–25
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_post().
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 375.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below WP_REST_Attachments_Controller::sideload_item() - querycontent query
get_post()one call below WP_REST_Attachments_Controller::sideload_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 · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Attachments_Controller::sideload_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–25 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 374 | 5–25 | 41 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 375 | 5–25 | 41 | |
| 8.4 | 375 | 5–25 | 41 | |
| 8.3 | 375 | 5–25 | 41 | |
| 8.2 | 375 | 5–25 | 41 | |
| 8.1 | 375 | 5–25 | 41 | 35 more instructions than PHP 7.4 |
| 7.4 | 340 | 35–167 | 36 |
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.
Uses · 23
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_attachment_is_image()Determines whether an attachment is an image.
- wp_attachment_is()Verifies an attachment is of a given type.
- __()Retrieves the translation of $text.
- get_attached_file()Retrieves attached file path based on attachment ID.
- add_filter()Adds a callback function to a filter hook.
- wp_basename()i18n-friendly version of basename().
- remove_filter()Removes a callback function from a filter hook.
- wp_getimagesize()Allows PHP's getimagesize() to be debuggable when necessary.
- wp_delete_file()Deletes a file.
- wp_filesize()Wrapper for PHP filesize with filters and casting the result as an integer.
- update_attached_file()Updates attachment file path based on attachment ID.
Show all 23
- _wp_relative_upload_path()Returns relative path to an uploaded file.
- add_post_meta()Adds a meta field to the given post.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- WP_REST_Attachments_Controller::get_post()
- WP_Error::__construct()Initializes the error.
- WP_REST_Attachments_Controller::get_attachment_upload_subdir()Returns the uploads subdirectory an attachment is stored in.
- WP_REST_Attachments_Controller::filter_wp_unique_filename()Filters wp_unique_filename during sideloads.
- WP_REST_Attachments_Controller::upload_from_file()Handles an upload via multipart/form-data ($_FILES).
- WP_REST_Attachments_Controller::upload_from_data()Handles an upload via raw POST data.
- WP_REST_Attachments_Controller::validate_image_dimensions()Validates that uploaded image dimensions are appropriate for the specified image size.
Source code
public function sideload_item( WP_REST_Request $request ) { $attachment_id = (int) $request['id']; $post = $this->get_post( $attachment_id ); if ( is_wp_error( $post ) ) { return $post; } if ( ! wp_attachment_is_image( $post ) && ! wp_attachment_is( 'pdf', $post ) ) { return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID. Only images and PDFs can be sideloaded.' ), array( 'status' => 400 ) ); } /* * Sideloaded files are placed in the same directory as the attachment * they extend, because the file names produced here are later resolved * against that directory. An attachment stored outside the uploads * directory has no such directory to use, so there is nowhere the names * this would produce could resolve. */ $attached_file = get_attached_file( $attachment_id, true ); $subdir = is_string( $attached_file ) && '' !== $attached_file ? $this->get_attachment_upload_subdir( $attached_file ) : null; if ( ! is_string( $attached_file ) || '' === $attached_file || null === $subdir ) { return new WP_Error( 'rest_sideload_attachment_not_in_uploads', __( 'The attachment is not stored in the uploads directory, so a file cannot be sideloaded for it.' ), array( 'status' => 403 ) ); } if ( false === $request['convert_format'] ) { // Prevent image conversion as that is done client-side. add_filter( 'image_editor_output_format', '__return_empty_array', 100 ); } // Get the file via $_FILES or raw data. $files = $request->get_file_params(); $headers = $request->get_headers(); /* * wp_unique_filename() will always add numeric suffix if the name looks like a sub-size to avoid conflicts. * See /wp-includes/functions.php. * With the following filter we can work around this safeguard. */ $attachment_filename = wp_basename( $attached_file ); $filter_filename = static function ( $filename, $ext, $dir, $unique_filename_callback, $alt_filenames, $number ) use ( $attachment_filename ) { return self::filter_wp_unique_filename( $filename, $dir, $number, $attachment_filename ); }; add_filter( 'wp_unique_filename', $filter_filename, 10, 6 ); // Pin the upload to the attachment's own directory, rather than deriving // it from the parent post's date as media_handle_upload() does for a // brand new upload. See the note above where $subdir is resolved. $filter_upload_dir = static function ( $uploads ) use ( $subdir ) { if ( is_array( $uploads ) && isset( $uploads['basedir'], $uploads['baseurl'] ) && is_string( $uploads['basedir'] ) && is_string( $uploads['baseurl'] ) ) { $uploads['subdir'] = $subdir; $uploads['path'] = $uploads['basedir'] . $subdir; $uploads['url'] = $uploads['baseurl'] . $subdir; } return $uploads; }; add_filter( 'upload_dir', $filter_upload_dir, 100 );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.