wppaste
WordPress

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
Sideloads an external image from a URL into the media library.

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

Reaches an outbound HTTP request via wp_safe_remote_get().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6

Executed per call on PHP 8.5. The body compiles to 212.

Plugin surface
1 hook

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

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksdo_action()called directly
  • httpoutbound HTTP requestwp_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.

WhenInstructionsCalls it makes
always6none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2116161 fewer instruction than PHP 8.5
8.5212616
8.42126163 fewer instructions than PHP 8.3
8.3215616
8.2215616
8.12156164 more instructions than PHP 7.4
7.421117–13716

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:

  1. 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

Show all 22

Used by · 1

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.