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

(2P) Revert "Revert "FSE: Add API endpoint to side-load images"" #35344

Merged
merged 2 commits into from
Aug 16, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class Starter_Page_Templates {
private function __construct() {
add_action( 'init', [ $this, 'register_scripts' ] );
add_action( 'init', [ $this, 'register_meta_field' ] );
add_action( 'rest_api_init', [ $this, 'register_rest_api' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_assets' ] );
add_action( 'delete_attachment', [ $this, 'clear_sideloaded_image_cache' ] );
}

/**
Expand Down Expand Up @@ -71,6 +73,15 @@ public function register_meta_field() {
register_meta( 'post', '_starter_page_template', $args );
}

/**
* Register rest api endpoint for side-loading images.
*/
public function register_rest_api() {
require_once __DIR__ . '/class-wp-rest-sideload-image-controller.php';

( new WP_REST_Sideload_Image_Controller() )->register_routes();
Copy link
Contributor

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 the A8C\FSE namespace?

Copy link
Member Author

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.

}

/**
* Pass error message to frontend JavaScript console.
*
Expand Down Expand Up @@ -193,6 +204,18 @@ public function fetch_vertical_data() {
return $vertical_templates;
}

/**
* Deletes cached attachment data when attachment gets deleted.
*
* @param int $id Attachment ID of the attachment to be deleted.
*/
public function clear_sideloaded_image_cache( $id ) {
$url = get_post_meta( $id, '_sideloaded_url', true );
if ( ! empty( $url ) ) {
delete_transient( 'fse_sideloaded_image_' . md5( $url ) );
}
}

/**
* Returns ISO 639 conforming locale string.
*
Expand Down
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;
Copy link
Contributor

@getdave getdave Aug 13, 2019

Choose a reason for hiding this comment

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

So here is where you've moved the class into the namespace?

Copy link
Member Author

Choose a reason for hiding this comment

The 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,
],
];
}
}