-
Notifications
You must be signed in to change notification settings - Fork 2k
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
FSE: Add API endpoint to side-load images #34823
Conversation
Currently the endpoint will only side-load one image at a time, and won't assign it to a post. Before implementing that I wanted to discuss if that's really needed, and if so how that request format should look like. |
This PR does not affect the size of JS and CSS bundles shipped to the user's browser. Generated by performance advisor bot at iscalypsofastyet.com. |
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 few minor comments.
.../full-site-editing-plugin/starter-page-templates/class-wp-rest-image-sideload-controller.php
Outdated
Show resolved
Hide resolved
.../full-site-editing-plugin/starter-page-templates/class-wp-rest-image-sideload-controller.php
Outdated
Show resolved
Hide resolved
.../full-site-editing-plugin/starter-page-templates/class-wp-rest-image-sideload-controller.php
Outdated
Show resolved
Hide resolved
.../full-site-editing-plugin/starter-page-templates/class-wp-rest-image-sideload-controller.php
Outdated
Show resolved
Hide resolved
.../full-site-editing-plugin/starter-page-templates/class-wp-rest-image-sideload-controller.php
Outdated
Show resolved
Hide resolved
Any chance to make also a batch version of the endpoint as mentioned here? Not having to take care of multiple concurrent requests on the frontend would be nice. https://developer.wordpress.org/rest-api/requests/#internal-requests If the endpoint is called several times with the same URL would it make multiple attachments or only one per URL? If multiple, can we prevent this by for example adding post meta to the attachment with the source URL and checking for it first before side loading the image? What is the response format here? I'm not sure I can deduct it from the code. Can we make it an object with |
Valid requests:Batch sideload with post_id 1
Structure: {
requests: [
{
method: 'POST',
route: 'fse/v1/sideload/image',
params: {
url: 'https://a8ctm1.files.wordpress.com/2019/06/phone-table.jpg',
post_id: 1 // optional
}
}
]
} Single sideload, no post_id
Return object single:{
"id": 139,
"date": "2019-07-23T22:51:50",
"date_gmt": "2019-07-23T22:51:50",
"guid": {
"rendered": "http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13.jpg",
"raw": "http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13.jpg"
},
"modified": "2019-07-23T22:51:50",
"modified_gmt": "2019-07-23T22:51:50",
"slug": "phone-table-13",
"status": "inherit",
"type": "attachment",
"link": "http://localhost/trunk/phone-table-13/",
"template": "",
"description": {
"raw": "",
"rendered": "<p class=\"attachment\"><a href='http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13.jpg'><img width=\"300\" height=\"200\" src=\"http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13-300x200.jpg\" class=\"attachment-medium size-medium\" alt=\"\" srcset=\"http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13-300x200.jpg 300w, http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13-768x512.jpg 768w, http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13-1024x683.jpg 1024w, http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13.jpg 1600w\" sizes=\"100vw\" /></a></p>\n"
},
"caption": {
"raw": "",
"rendered": ""
},
"alt_text": "",
"media_type": "image",
"mime_type": "image/jpeg",
"media_details": {
"width": 1600,
"height": 1067,
"file": "2019/07/phone-table-13.jpg",
"sizes": {
"thumbnail": {
"file": "phone-table-13-150x150.jpg",
"width": 150,
"height": 150,
"mime_type": "image/jpeg",
"source_url": "http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13-150x150.jpg"
},
"medium": {
"file": "phone-table-13-300x200.jpg",
"width": 300,
"height": 200,
"mime_type": "image/jpeg",
"source_url": "http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13-300x200.jpg"
},
"medium_large": {
"file": "phone-table-13-768x512.jpg",
"width": 768,
"height": 512,
"mime_type": "image/jpeg",
"source_url": "http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13-768x512.jpg"
},
"large": {
"file": "phone-table-13-1024x683.jpg",
"width": 1024,
"height": 683,
"mime_type": "image/jpeg",
"source_url": "http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13-1024x683.jpg"
},
"full": {
"file": "phone-table-13.jpg",
"width": 1600,
"height": 1067,
"mime_type": "image/jpeg",
"source_url": "http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13.jpg"
}
},
"image_meta": {
"aperture": "1.8",
"credit": "",
"camera": "Canon EOS 1000D",
"caption": "",
"created_timestamp": "0",
"copyright": "",
"focal_length": "50",
"iso": "200",
"shutter_speed": "0.004",
"title": "",
"orientation": "0",
"keywords": []
}
},
"post": 1,
"source_url": "http://localhost/trunk/wp-content/uploads/2019/07/phone-table-13.jpg",
"_links": {
"replies": [
{
"embeddable": true,
"href": "http://localhost/trunk/wp-json/wp/v2/comments?post=139"
}
],
"self": [
{
"href": "http://localhost/trunk/wp-json/wp/v2/media/139"
}
],
"collection": [
{
"href": "http://localhost/trunk/wp-json/wp/v2/media"
}
],
"about": [
{
"href": "http://localhost/trunk/wp-json/wp/v2/types/attachment"
}
]
}
} |
$id = media_sideload_image( | ||
$request->get_param( 'url' ), | ||
$request->get_param( 'post_id' ), | ||
null, |
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.
Maybe it worths adding the possibility to set up the image title, through of a title
parameter for instanance:x
$id = media_sideload_image(
$request->get_param( 'url' ),
$request->get_param( 'post_id' ),
$request->get_param( 'title' ),
'id'
);
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.
I don't think we currently have that information in our templates. Not sure how generic we want to make this endpoint?
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.
Yeah, in fact, if we want to make it more generic we should consider changing the namespace.
Anyway, there's anything wrong to allow the title
attr? In the worse of cases, we just ignore it.
...e-editing/full-site-editing-plugin/starter-page-templates/class-wp-rest-batch-controller.php
Outdated
Show resolved
Hide resolved
Testing
Despite this, it works and uploads an image only if we add only one route. {
"POST \/fse\/v1\/sideload\/image": {
"code": "rest_invalid_param",
"message": "Invalid parent type.",
"data": {
"status": 400
}
}
} This is the request body requests
Array
(
[0] => Array
(
[method] => POST
[route] => /fse/v1/sideload/image
[params] => Array
(
[url] => https://a8ctm1.files.wordpress.com/2019/06/phone-table.jpg
)
)
[1] => Array
(
[method] => POST
[route] => /fse/v1/sideload/image
[params] => Array
(
[url] => https://a8ctm1.files.wordpress.com/2019/06/phone-table.jpg
)
)
) |
@retrofox Thanks so much for your review! All errors should be fixed now, would you mind testing them again? |
Tested again and it works as expected. Created two media entries and a post entry hitting the |
|
@marekhrabe The API now sanitizes image URLs and reuses previously sideloaded images. |
Valid requests:Batch sideload with post_id 1
Structure: {
resources: [
{
url: 'https://a8ctm1.files.wordpress.com/2019/06/phone-table.jpg',
post_id: 1 // optional
}
]
} Single sideload, no post_id
|
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.
🚢
I'm seeing the following fatal error when trying to create a new page or edit an existing one:
Maybe smoke testing the plugin should be added to testing instructions, since editing a page is quite a common flow to test both for SPT and FSE. I'm going to revert this in order to unblock our FSE plugin sync on Dotcom. |
@vindl Why was this blocking the plugin sync? I thought there was already a diff for that? |
* FSE: Add API endpoint to side-load images * Address Dave's feedback * Rename class to match rest base * Add batch endpoint See #34823 (comment) * Add post_id parameter * Make sure we link to the appropriate URLs * Remove empty line * FSE: Add API endpoint to side-load images * Address Dave's feedback * Rename class to match rest base * Add batch endpoint See #34823 (comment) * Add post_id parameter * Make sure we link to the appropriate URLs * Remove empty line * Fix errors from review Props @retrofox. * Look for previously sideloaded images before saving it * Sanitize image url /cc @marekhrabe * Merge `/batch` endpoint into sideload endpoint
Changes proposed in this Pull Request
Testing instructions
Send a POST request to the
/wp-json/wp/v2/media/image
endpoint on your test site with an image url in the body.See #34603