-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
TimothyBJacobs
merged 1 commit into
WordPress:master
from
TimothyBJacobs:fix/edit-image-return
Jun 22, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]+)'; | ||
} | ||
|
||
/** | ||
|
@@ -44,22 +44,22 @@ public function register_routes() { | |
'permission_callback' => array( $this, 'permission_callback' ), | ||
'args' => array( | ||
'x' => array( | ||
'type' => 'float', | ||
'type' => 'number', | ||
'minimum' => 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it also possible to specify a maximum? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep we can use the |
||
'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, | ||
), | ||
|
@@ -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'; | ||
|
@@ -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 ) ); | ||
|
@@ -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(); | ||
|
@@ -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. | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 anumber
in JSON Schema, https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1.There was a problem hiding this comment.
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