-
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
(2P) Revert "Revert "FSE: Add API endpoint to side-load images"" #35344
Merged
obenland
merged 2 commits into
master
from
revert-35328-revert-34823-add/fse-image-endpoint
Aug 16, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
286 changes: 286 additions & 0 deletions
286
...ll-site-editing-plugin/starter-page-templates/class-wp-rest-sideload-image-controller.php
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 |
---|---|---|
@@ -0,0 +1,286 @@ | ||
<?php | ||
/** | ||
* WP_REST_Sideload_Image_Controller file. | ||
* | ||
* @package A8C\FSE | ||
*/ | ||
|
||
namespace A8C\FSE; | ||
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. So here is where you've moved the class into the namespace? 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. Correct |
||
|
||
/** | ||
* Class WP_REST_Sideload_Image_Controller. | ||
*/ | ||
class WP_REST_Sideload_Image_Controller extends \WP_REST_Attachments_Controller { | ||
|
||
/** | ||
* WP_REST_Sideload_Image_Controller constructor. | ||
*/ | ||
public function __construct() { | ||
parent::__construct( 'attachment' ); | ||
|
||
$this->namespace = 'fse/v1'; | ||
$this->rest_base = 'sideload/image'; | ||
} | ||
|
||
/** | ||
* Register available routes. | ||
*/ | ||
public function register_routes() { | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base, | ||
[ | ||
[ | ||
'methods' => \WP_REST_Server::CREATABLE, | ||
'callback' => [ $this, 'create_item' ], | ||
'permission_callback' => [ $this, 'create_item_permissions_check' ], | ||
'show_in_index' => false, | ||
'args' => $this->get_collection_params(), | ||
], | ||
'schema' => [ $this, 'get_item_schema' ], | ||
] | ||
); | ||
|
||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base . '/batch', | ||
[ | ||
[ | ||
'methods' => \WP_REST_Server::CREATABLE, | ||
'callback' => [ $this, 'create_items' ], | ||
'show_in_index' => false, | ||
'args' => [ | ||
'resources' => [ | ||
'description' => 'URL to the image to be side-loaded.', | ||
'type' => 'array', | ||
'required' => true, | ||
'items' => [ | ||
'type' => 'object', | ||
'properties' => $this->get_collection_params(), | ||
], | ||
], | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Creates a single attachment. | ||
* | ||
* @param \WP_REST_Request $request Full details about the request. | ||
* @return \WP_Error|\WP_REST_Response Response object on success, WP_Error object on failure. | ||
*/ | ||
public function create_item( $request ) { | ||
if ( ! empty( $request['post_id'] ) && in_array( get_post_type( $request['post_id'] ), [ 'revision', 'attachment' ], true ) ) { | ||
return new \WP_Error( 'rest_invalid_param', __( 'Invalid parent type.' ), [ 'status' => 400 ] ); | ||
} | ||
|
||
$inserted = false; | ||
$attachment = $this->get_attachment( $request->get_param( 'url' ) ); | ||
if ( ! $attachment ) { | ||
// Include image functions to get access to wp_read_image_metadata(). | ||
require_once ABSPATH . 'wp-admin/includes/file.php'; | ||
require_once ABSPATH . 'wp-admin/includes/image.php'; | ||
require_once ABSPATH . 'wp-admin/includes/media.php'; | ||
|
||
// The post ID on success, WP_Error on failure. | ||
$id = media_sideload_image( | ||
$request->get_param( 'url' ), | ||
$request->get_param( 'post_id' ), | ||
null, | ||
'id' | ||
); | ||
|
||
if ( is_wp_error( $id ) ) { | ||
if ( 'db_update_error' === $id->get_error_code() ) { | ||
$id->add_data( [ 'status' => 500 ] ); | ||
} else { | ||
$id->add_data( [ 'status' => 400 ] ); | ||
} | ||
|
||
return rest_ensure_response( $id ); // Return error. | ||
} | ||
|
||
$attachment = get_post( $id ); | ||
|
||
/** | ||
* Fires after a single attachment is created or updated via the REST API. | ||
* | ||
* @param WP_Post $attachment Inserted or updated attachment object. | ||
* @param WP_REST_Request $request The request sent to the API. | ||
* @param bool $creating True when creating an attachment, false when updating. | ||
*/ | ||
do_action( 'rest_insert_attachment', $attachment, $request, true ); | ||
|
||
if ( isset( $request['alt_text'] ) ) { | ||
update_post_meta( $id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) ); | ||
} | ||
|
||
update_post_meta( $id, '_sideloaded_url', $request->get_param( 'url' ) ); | ||
|
||
$fields_update = $this->update_additional_fields_for_object( $attachment, $request ); | ||
|
||
if ( is_wp_error( $fields_update ) ) { | ||
return $fields_update; | ||
} | ||
|
||
$inserted = true; | ||
$request->set_param( 'context', 'edit' ); | ||
|
||
/** | ||
* Fires after a single attachment is completely created or updated via the REST API. | ||
* | ||
* @param WP_Post $attachment Inserted or updated attachment object. | ||
* @param WP_REST_Request $request Request object. | ||
* @param bool $creating True when creating an attachment, false when updating. | ||
*/ | ||
do_action( 'rest_after_insert_attachment', $attachment, $request, true ); | ||
} | ||
|
||
$response = $this->prepare_item_for_response( $attachment, $request ); | ||
$response = rest_ensure_response( $response ); | ||
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', 'wp/v2', 'media', $attachment->ID ) ) ); | ||
|
||
if ( $inserted ) { | ||
$response->set_status( 201 ); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Creates a batch of attachments. | ||
* | ||
* @param \WP_REST_Request $request Full details about the request. | ||
* @return \WP_Error|\WP_REST_Response Response object on success, WP_Error object on failure. | ||
*/ | ||
public function create_items( $request ) { | ||
$data = []; | ||
|
||
// Foreach request specified in the requests param, run the endpoint. | ||
foreach ( $request['resources'] as $resource ) { | ||
$request = new \WP_REST_Request( 'POST', "/{$this->namespace}/{$this->rest_base}" ); | ||
|
||
// Add specified request parameters into the request. | ||
foreach ( $resource as $param_name => $param_value ) { | ||
$request->set_param( $param_name, $param_value ); | ||
} | ||
|
||
$response = rest_do_request( $request ); | ||
$data[] = $this->prepare_for_collection( $response ); | ||
} | ||
|
||
return rest_ensure_response( $data ); | ||
} | ||
|
||
/** | ||
* Prepare a response for inserting into a collection of responses. | ||
* | ||
* @param \WP_REST_Response $response Response object. | ||
* @return array|\WP_REST_Response Response data, ready for insertion into collection data. | ||
*/ | ||
public function prepare_for_collection( $response ) { | ||
if ( ! ( $response instanceof \WP_REST_Response ) ) { | ||
return $response; | ||
} | ||
|
||
$data = (array) $response->get_data(); | ||
$server = rest_get_server(); | ||
|
||
if ( method_exists( $server, 'get_compact_response_links' ) ) { | ||
$links = call_user_func( [ $server, 'get_compact_response_links' ], $response ); | ||
} else { | ||
$links = call_user_func( [ $server, 'get_response_links' ], $response ); | ||
} | ||
|
||
if ( ! empty( $links ) ) { | ||
$data['_links'] = $links; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Prepares a single attachment output for response. | ||
* | ||
* @param \WP_Post $post Attachment object. | ||
* @param \WP_REST_Request $request Request object. | ||
* @return \WP_REST_Response Response object. | ||
*/ | ||
public function prepare_item_for_response( $post, $request ) { | ||
$response = parent::prepare_item_for_response( $post, $request ); | ||
$base = 'wp/v2/media'; | ||
|
||
foreach ( [ 'self', 'collection', 'about' ] as $link ) { | ||
$response->remove_link( $link ); | ||
|
||
} | ||
|
||
$response->add_link( 'self', rest_url( trailingslashit( $base ) . $post->ID ) ); | ||
$response->add_link( 'collection', rest_url( $base ) ); | ||
$response->add_link( 'about', rest_url( 'wp/v2/types/' . $post->post_type ) ); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Gets the attachment if an image has been sideloaded previously. | ||
* | ||
* @param string $url URL of the image to sideload. | ||
* @return object|bool Attachment object on success, false on failure. | ||
*/ | ||
public function get_attachment( $url ) { | ||
$cache_key = 'fse_sideloaded_image_' . md5( $url ); | ||
$attachment = get_transient( $cache_key ); | ||
|
||
if ( false === $attachment ) { | ||
$attachments = new \WP_Query( | ||
[ | ||
'no_found_rows' => true, | ||
'posts_per_page' => 1, | ||
'post_status' => 'inherit', | ||
'post_type' => 'attachment', | ||
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query | ||
'meta_query' => [ | ||
[ | ||
'key' => '_sideloaded_url', | ||
'value' => $url, | ||
], | ||
], | ||
] | ||
); | ||
|
||
if ( $attachments->have_posts() ) { | ||
$attachment = $attachments->post; | ||
set_transient( $cache_key, $attachment ); | ||
} | ||
} | ||
|
||
return $attachment; | ||
} | ||
|
||
/** | ||
* Returns the endpoints request parameters. | ||
* | ||
* @return array Request parameters. | ||
*/ | ||
public function get_collection_params() { | ||
return [ | ||
'url' => [ | ||
'description' => 'URL to the image to be side-loaded.', | ||
'type' => 'string', | ||
'required' => true, | ||
'format' => 'uri', | ||
'sanitize_callback' => function( $url ) { | ||
return esc_url_raw( strtok( $url, '?' ) ); | ||
}, | ||
], | ||
'post_id' => [ | ||
'description' => 'ID of the post to associate the image with', | ||
'type' => 'integer', | ||
'default' => 0, | ||
], | ||
]; | ||
} | ||
} |
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.
Just to confirm there is no global namespace qualifier here because the
WP_REST_Sideload_Image_Controller
class is now in theA8C\FSE
namespace?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.
Right, no qualifier needed because the current namespace also applies to
WP_REST_Sideload_Image_Controller
.