Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up image editor REST route. #23368

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions lib/class-wp-rest-image-editor-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class WP_REST_Image_Editor_Controller extends WP_REST_Controller {
*/
public function __construct() {
$this->namespace = '__experimental';
$this->rest_base = '/richimage/(?P<media_id>[\d]+)';
$this->rest_base = '/image-editor/(?P<media_id>[\d]+)';
}

/**
Expand All @@ -44,22 +44,22 @@ public function register_routes() {
'permission_callback' => array( $this, 'permission_callback' ),
'args' => array(
'x' => array(
'type' => 'float',
'type' => 'number',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange, using number failed for me before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, it seems to work, but what happens then? Is it rounded? Ideally we accept float values here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A float is referred to as a number in JSON Schema, https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More accurate link ( its the v4 spec which we use ) https://tools.ietf.org/html/draft-zyp-json-schema-04#section-3.5

'minimum' => 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it also possible to specify a maximum?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep we can use the maximum property.

'required' => true,
),
'y' => array(
'type' => 'float',
'type' => 'number',
'minimum' => 0,
'required' => true,
),
'width' => array(
'type' => 'float',
'type' => 'number',
'minimum' => 1,
'required' => true,
),
'height' => array(
'type' => 'float',
'type' => 'number',
'minimum' => 1,
'required' => true,
),
Expand Down Expand Up @@ -96,7 +96,7 @@ public function permission_callback( $request ) {
* @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return array|WP_Error If successful image JSON for the modified image, otherwise a WP_Error.
* @return WP_REST_Response|WP_Error If successful image JSON for the modified image, otherwise a WP_Error.
*/
public function apply_edits( $request ) {
require_once ABSPATH . 'wp-admin/includes/image.php';
Expand All @@ -110,7 +110,7 @@ public function apply_edits( $request ) {
$media_url = wp_get_attachment_image_url( $media_id, 'original' );

if ( ! $attachment_info || ! $media_url ) {
return new WP_Error( 'unknown', 'Unable to get meta information for file' );
return new WP_Error( 'rest_unknown_attachment', __( 'Unable to get meta information for file.', 'gutenberg' ), array( 'status' => 404 ) );
}

$meta = array( 'original_name' => basename( $media_url ) );
Expand All @@ -122,12 +122,12 @@ public function apply_edits( $request ) {
// Try and load the image itself.
$image_path = get_attached_file( $media_id );
if ( empty( $image_path ) ) {
return new WP_Error( 'fileunknown', 'Unable to find original media file' );
return new WP_Error( 'rest_cannot_find_attached_file', __( 'Unable to find original media file.', 'gutenberg' ), array( 'status' => 500 ) );
}

$image_editor = wp_get_image_editor( $image_path );
if ( ! $image_editor->load() ) {
return new WP_Error( 'fileload', 'Unable to load original media file' );
return new WP_Error( 'rest_cannot_load_editor', __( 'Unable to load original media file.', 'gutenberg' ), array( 'status' => 500 ) );
}

$size = $image_editor->get_size();
Expand Down Expand Up @@ -165,9 +165,16 @@ public function apply_edits( $request ) {
);

// Add this as an attachment.
$attachment_id = wp_insert_attachment( $attachment_post, $saved['path'], 0 );
if ( 0 === $attachment_id ) {
return new WP_Error( 'attachment', 'Unable to add image as attachment' );
$attachment_id = wp_insert_attachment( $attachment_post, $saved['path'], 0, true );

if ( is_wp_error( $attachment_id ) ) {
if ( 'db_update_error' === $attachment_id->get_error_code() ) {
$attachment_id->add_data( array( 'status' => 500 ) );
} else {
$attachment_id->add_data( array( 'status' => 400 ) );
}

return $attachment_id;
}

// Generate thumbnails.
Expand All @@ -177,9 +184,14 @@ public function apply_edits( $request ) {

wp_update_attachment_metadata( $attachment_id, $metadata );

return array(
'media_id' => $attachment_id,
'url' => wp_get_attachment_image_url( $attachment_id, 'original' ),
);
$path = '/wp/v2/media/' . $attachment_id;
$response = rest_do_request( $path );

if ( ! $response->is_error() ) {
$response->set_status( 201 );
$response->header( 'Location', rest_url( $path ) );
}

return $response;
}
}
11 changes: 4 additions & 7 deletions packages/block-library/src/image/image-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,14 @@ export default function ImageEditor( {
}

apiFetch( {
path: `__experimental/richimage/${ id }/apply`,
headers: {
'Content-type': 'application/json',
},
path: `__experimental/image-editor/${ id }/apply`,
method: 'POST',
body: JSON.stringify( attrs ),
data: attrs,
} )
.then( ( response ) => {
setAttributes( {
id: response.media_id,
url: response.url,
id: response.id,
url: response.source_url,
height: height && width ? width / aspect : undefined,
} );
} )
Expand Down