WP_REST_Attachments_Controller::create_item_from_url( 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:654
Description
Downloads the remote file on the server, avoiding a cross-origin browser fetch that fails under cross-origin isolation. Whether sub-sizes are generated is governed by the filters applied in create_item().
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::create_item_from_url() 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
- Expensive
- Scaling
- Constant
- Instructions
- 6
- Plugin surface
- 1 hook
- Called by
- 1
Reaches an outbound HTTP request via wp_safe_remote_get().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 212.
Third-party callbacks on 'rest_after_insert_attachment' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
do_action()called directly - httpoutbound HTTP request
wp_safe_remote_get()one call below WP_REST_Attachments_Controller::create_item_from_url()
Further down the call graph this can also reach option, cache, 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 · 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::create_item_from_url() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 211 | 6 | 16 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 212 | 6 | 16 | |
| 8.4 | 212 | 6 | 16 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 215 | 6 | 16 | |
| 8.2 | 215 | 6 | 16 | |
| 8.1 | 215 | 6 | 16 | 4 more instructions than PHP 7.4 |
| 7.4 | 211 | 17–137 | 16 |
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::create_item_from_url() runs, in this order:
- do_action( rest_after_insert_attachment )actionline 785 (+131 into the body)
Fires after a single attachment is completely created or updated via the REST API.
Uses · 22
- current_user_can()Returns whether the current user has the specified capability.
- __()Retrieves the translation of $text.
- rest_authorization_required_code()Returns a contextual HTTP error code for authorization failure.
- wp_parse_url()A wrapper for PHP's parse_url() function that handles consistency in the return values across PHP versions.
- wp_basename()i18n-friendly version of basename().
- wp_check_filetype()Retrieves the file type from the file name.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_max_upload_size()Determines the maximum upload size allowed in php.ini.
- add_filter()Adds a callback function to a filter hook.
- download_url()Downloads a URL to a local temporary file using the WordPress HTTP API.
- remove_filter()Removes a callback function from a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
Show all 22
- wp_delete_file()Deletes a file.
- wp_filesize()Wrapper for PHP filesize with filters and casting the result as an integer.
- media_handle_sideload()Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
- get_post()Retrieves post data given a post ID or post object.
- do_action()Calls the callback functions that have been added to an action hook.
- rest_url()Retrieves the URL to a REST endpoint.
- rest_get_route_for_post()Gets the REST API route for a post.
- WP_Error::__construct()Initializes the error.
- WP_REST_Attachments_Controller::check_upload_size()Determine if uploaded file exceeds space quota on multisite.
- WP_REST_Attachments_Controller::prepare_item_for_response()Prepares a single attachment output for response.
Used by · 1
- WP_REST_Attachments_Controller::create_item()Creates a single attachment.
Source code
protected function create_item_from_url( WP_REST_Request $request ) { // Sideloading downloads and stores a file, so require the upload capability. if ( ! current_user_can( 'upload_files' ) ) { return new WP_Error( 'rest_cannot_create', __( 'Sorry, you are not allowed to upload media on this site.' ), array( 'status' => rest_authorization_required_code() ) ); } require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $url = $request['url']; $post_id = ! empty( $request['post'] ) ? (int) $request['post'] : 0; // Derive the filename from the URL path before downloading anything. $url_path = wp_parse_url( $url, PHP_URL_PATH ); $filename = $url_path ? wp_basename( $url_path ) : ''; if ( '' === $filename ) { return new WP_Error( 'rest_invalid_url', __( 'Could not determine a filename from the provided URL.' ), array( 'status' => 400 ) ); } /* * Only download URLs whose extension maps to an allowed image MIME type. * The sideload handler would reject other types anyway (via * wp_check_filetype_and_ext()), but checking first avoids downloading * files that can never be accepted, such as PHP scripts. */ $filetype = wp_check_filetype( $filename ); if ( ! $filetype['type'] || ! str_starts_with( $filetype['type'], 'image/' ) ) { return new WP_Error( 'rest_invalid_url', __( 'The provided URL does not point to a supported image file.' ), array( 'status' => 400 ) ); } /* * Cap the download at the same size the site would accept as a direct * upload. check_upload_size() only applies on multisite, so without a * ceiling here a single site has no limit at all on this path: the * `upload_max_filesize` and `post_max_size` directives bound a request * body, not a fetch the server makes itself. * * When `wp_max_upload_size` returns 0, no ceiling is applied. */ $max_size = (int) wp_max_upload_size(); /* * Download the remote file with WordPress's HTTP API, which validates * the host and blocks requests to private or local addresses. This is * the same primitive core's media_sideload_image() relies on. * * `limit_response_size` stops the transfer once the limit is passed, * so an oversized remote file is never written to disk in full. One * byte over the ceiling is enough to fail the size check below. */ $limit_response_size = static function ( $args ) use ( $max_size ) { $args['limit_response_size'] = $max_size + 1; return $args; }; if ( $max_size > 0 ) { add_filter( 'http_request_args', $limit_response_size ); } $tmp_file = download_url( $url ); if ( $max_size > 0 ) { remove_filter( 'http_request_args', $limit_response_size ); } if ( is_wp_error( $tmp_file ) ) { return $tmp_file;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.