WP_REST_Attachments_Controller::register_routes │ public │ WP 5.3.0
Registers the routes for attachments.
Method of the class: WP_REST_Attachments_Controller{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WP_REST_Attachments_Controller = new WP_REST_Attachments_Controller(); $WP_REST_Attachments_Controller->register_routes();
Notes
Changelog
| Since 5.3.0 | Introduced. |
WP_REST_Attachments_Controller::register_routes() WP REST Attachments Controller::register routes code WP 7.1
public function register_routes() {
parent::register_routes();
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/post-process',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'post_process_item' ),
'permission_callback' => array( $this, 'post_process_item_permissions_check' ),
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the attachment.' ),
'type' => 'integer',
),
'action' => array(
'type' => 'string',
'enum' => array( 'create-image-subsizes' ),
'required' => true,
),
),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/edit',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'edit_media_item' ),
'permission_callback' => array( $this, 'edit_media_item_permissions_check' ),
'args' => $this->get_edit_media_item_args(),
)
);
if ( wp_is_client_side_media_processing_enabled() ) {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/sideload',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'sideload_item' ),
'permission_callback' => array( $this, 'sideload_item_permissions_check' ),
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the attachment.' ),
'type' => 'integer',
),
'image_size' => array(
'description' => __( 'Image size. Can be a single size name or an array of size names to register the same file under multiple sizes.' ),
'type' => array( 'string', 'array' ),
'items' => array(
'type' => 'string',
'minLength' => 1,
),
'minItems' => 1,
'minLength' => 1,
'required' => true,
/*
* A custom callback is used instead of the default enum validation
* because rest_is_array() treats scalar strings as single-element
* lists (via wp_parse_list()), so a [ 'string', 'array' ] type alone
* cannot enforce the enum. The callback validates each item against
* the current list of registered sizes, which reflects sizes added
* after route registration (e.g. via add_image_size()).
*/
'validate_callback' => static function ( $value, WP_REST_Request $request, string $param ) {
/*
* Providing a custom callback replaces the default schema
* validation, so apply the declared schema (type, minLength,
* minItems) before the enum check below.
*/
$schema_validity = rest_validate_request_arg( $value, $request, $param );
if ( is_wp_error( $schema_validity ) ) {
return $schema_validity;
}
return self::validate_image_size_names( $value, $param );
},
),
'convert_format' => array(
'type' => 'boolean',
'default' => true,
'description' => __( 'Whether to convert image formats.' ),
),
),
),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/finalize',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'finalize_item' ),
'permission_callback' => array( $this, 'edit_media_item_permissions_check' ),
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the attachment.' ),
'type' => 'integer',
),
'sub_sizes' => array(
'description' => __( 'Array of sub-size metadata collected from sideload responses.' ),
'type' => 'array',
'default' => array(),
/*
* A finalize request sends one entry per sideloaded sub-size, so
* the ceiling only needs to clear the number of sizes a site can
* register. Bounding it keeps a request from repeating a name
* across an arbitrary number of entries.
*/
'maxItems' => 100,
/*
* As on the sideload endpoint, the size names are checked in a
* callback rather than an enum, so the set reflects the sizes
* registered when the request runs. The callback sits on
* sub_sizes because a nested property cannot carry one.
*/
'validate_callback' => static function ( $value, WP_REST_Request $request, string $param ) {
/*
* Providing a custom callback replaces the default schema
* validation, so apply the declared schema first. That is what
* guarantees each entry is an object carrying an image_size of
* the declared type.
*/
$schema_validity = rest_validate_request_arg( $value, $request, $param );
if ( is_wp_error( $schema_validity ) ) {
return $schema_validity;
}
foreach ( (array) $value as $index => $sub_size ) {
$sub_size = (array) $sub_size;
$validity = self::validate_image_size_names(
$sub_size['image_size'] ?? null,
sprintf( '%s[%s][image_size]', $param, $index )
);
if ( is_wp_error( $validity ) ) {
return $validity;
}
}
return true;
},
'items' => array(
'type' => 'object',
'properties' => array(
'image_size' => array(
'description' => __( 'Size name, or an array of size names when a single file is registered under multiple sizes with matching dimensions.' ),
'type' => array( 'string', 'array' ),
'items' => array(
'type' => 'string',
'minLength' => 1,
),
'minItems' => 1,
'minLength' => 1,
'required' => true,
),
'width' => array(
'type' => 'integer',
'minimum' => 1,
),
'height' => array(
'type' => 'integer',
'minimum' => 1,
),
'file' => array(
'type' => 'string',
'minLength' => 1,
),
'mime_type' => array(
'type' => 'string',
'pattern' => '^image/.*',
),
'filesize' => array(
'type' => 'integer',
'minimum' => 1,
),
'original_image' => array(
'type' => 'string',
'minLength' => 1,
),
),
),
),
),
),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
}