wppaste
WordPress

WP_REST_Attachments_Controller::validate_sub_size_provenance( int $attachment_id, array $sub_sizes ): true|WP_Error

Since
7.1.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:3087
Validates the sub_sizes file names against what this attachment produced.

Description

The self::finalize_item() method stores the client-supplied file and original_image values in the attachment metadata, where they are later resolved within the attachment's upload directory and read or deleted (for example by wp_get_original_image_path(), wp_getimagesize(), and wp_delete_attachment_files()).

Every file the sideload endpoint creates is recorded under self::META_KEY_SIDELOAD_FILE_NAME as it is produced, using server-generated names. finalize accepts a file or original_image value only when it matches one of those recorded names (or the attachment's own attached file, which it definitionally owns).

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

$attachment_idint
The attachment being finalized.
$sub_sizesarray
Sub-size metadata collected from sideloads.

Return value

true|WP_Error
True if every file name was produced here, WP_Error otherwise.

Performance profile

How much work a call to WP_REST_Attachments_Controller::validate_sub_size_provenance() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
9–30

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What one call costs · 2 distinct outcomes

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

WhenInstructionsCalls it makes
always9–10->get_sideloaded_file_names()
!$sub_sizes && isset($sub_size[$key])26–30->get_sideloaded_file_names(), __()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev349–307
8.5349–307
8.4349–3073 fewer instructions than PHP 8.3
8.3379–337
8.2379–337
8.1379–337
7.4379–337

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 · 3

Used by · 1

Source code

	protected function validate_sub_size_provenance( int $attachment_id, array $sub_sizes ) {		$allowed = $this->get_sideloaded_file_names( $attachment_id ); 		foreach ( $sub_sizes as $sub_size ) {			foreach ( array( 'file', 'original_image' ) as $key ) {				/*				 * Every value that was sent is checked, no matter how unlikely				 * a name it looks. A loose emptiness test would wave through				 * '0', which is a valid one-character name as far as the schema				 * is concerned and is stored like any other. A value the schema				 * types as a string but which arrives as something else is				 * rejected rather than skipped, so a subclass which widens the				 * schema cannot pass an unchecked value on to the metadata.				 */				if ( ! isset( $sub_size[ $key ] ) ) {					continue;				} 				if ( ! is_string( $sub_size[ $key ] ) || ! in_array( $sub_size[ $key ], $allowed, true ) ) {					return new WP_Error(						'rest_invalid_sub_size_file',						__( 'Invalid sub-size file name. File names must have been produced by a prior sideload for this attachment.' ),						array( 'status' => 400 )					);				}			}		} 		return true;	}

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.