wppaste
WordPress

WP_REST_Attachments_Controller::validate_image_size_names( mixed $value, string $param ): true|WP_Error

Since
7.1.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:2535
Validates an image size name, or an array of names sharing a single file.

Description

Shared by the sideload endpoint, which names the size a file is produced for, and the finalize endpoint, which names the size each submitted entry is stored under. Both need the same set, and finalize accepts a payload of its own rather than one this class produced, so leaving it unconstrained there would let a submission write an arbitrary key into the metadata 'sizes' array or route a file into a branch it was never produced for.

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

$valuemixed
The image size name, or an array of names.
$paramstring
Parameter name, used in the error messages.

Return value

true|WP_Error
True when every name is valid, WP_Error otherwise.

Performance profile

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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

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

Instructions
32–49

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

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 it touches

  • optionoption read or writeget_option()one call below WP_REST_Attachments_Controller::validate_image_size_names()

Further down the call graph this can also reach hook, cache, serialize, query 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::validate_image_size_names() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_string($value) && is_array($value)32–33::get_special_image_sizes(), wp_get_registered_image_subsizes(), array_keys(), array_merge(), array_diff(), array_values()
is_string($value)34–35::get_special_image_sizes(), wp_get_registered_image_subsizes(), array_keys(), array_merge(), array_diff(), array_values(), array_merge()
!is_string($value)38–47::get_special_image_sizes(), wp_get_registered_image_subsizes(), array_keys(), array_merge(), array_diff(), array_values(), __(), sprintf()
is_string($value) && !$value && !$items && !$item49::get_special_image_sizes(), wp_get_registered_image_subsizes(), array_keys(), array_merge(), array_diff(), array_values(), array_merge(), __(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev6832–495
8.56832–495
8.46832–4953 fewer instructions than PHP 8.3
8.37132–525
8.27132–525
8.17132–525
7.47132–525

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

Used by · 1

Source code

	private static function validate_image_size_names( $value, string $param ) {		$special_sizes = self::get_special_image_sizes();		$regular_sizes = array_values(			array_diff(				array_merge(					array_keys( wp_get_registered_image_subsizes() ),					// Not a registered sub-size, but stored as an ordinary					// entry in the metadata 'sizes' array (PDF thumbnails).					array( 'full' )				),				$special_sizes			)		); 		if ( is_string( $value ) ) {			$items       = array( $value );			$valid_sizes = array_merge( $regular_sizes, $special_sizes );		} elseif ( is_array( $value ) ) {			/**			 * An array registers one sideloaded file under several size names,			 * which only makes sense for regular sub-sizes: each special size			 * names a single file with its own handling in			 * {@see self::sideload_item()} and its own metadata key in			 * {@see self::finalize_item()}. Rejecting them here is what lets the			 * array branches in both methods treat an array as regular sizes.			 */			$items       = $value;			$valid_sizes = $regular_sizes;		} else {			return new WP_Error(				'rest_invalid_type',				/* translators: %s: Parameter name. */				sprintf( __( '%s must be a string or an array of strings.' ), $param )			);		} 		foreach ( $items as $item ) {			if ( ! in_array( $item, $valid_sizes, true ) ) {				return new WP_Error(					'rest_not_in_enum',					/* translators: %s: Parameter name. */					sprintf( __( '%s contains an invalid image size.' ), $param )				);			}		} 		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.