Validates the sub_sizes file names against what this attachment produced.
Description
The WP_REST_Attachments_Controller::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 WP_REST_Attachments_Controller::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).
Parameters
$attachment_idintrequired- The attachment being finalized.
$sub_sizesarrayrequired- Sub-size metadata collected from sideloads.
Source
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
| Version | Description |
|---|---|
| 7.1.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.