humanmade / S3-Uploads

The WordPress Plugin to Store Uploads on Amazon S3
1.92k stars 389 forks source link

Missing thumbnails for private attachments in Media library #631

Open mklasen opened 1 year ago

mklasen commented 1 year ago

Heya!

I noticed private attachment thumbnails are not showing when the Media library is shown in (default) grid view. I've solved this on my own environment with the following snippet;

add_filter( 'wp_prepare_attachment_for_js', array( $this, 'prepare_js_attachments' ) );

    public function prepare_js_attachments( $attachment ) {
        $s3_uploads = \S3_Uploads\Plugin::get_instance();
        if ( $s3_uploads->is_private_attachment( $attachment['id'] ) ) {
            if ( isset( $attachment['sizes'] ) && is_array( $attachment['sizes'] ) ) {
                foreach ( $attachment['sizes'] as &$size ) {
                    $size['url'] = $s3_uploads->add_s3_signed_params_to_attachment_url( $size['url'], $attachment['id'] );
                }
            }
        }
        return $attachment;
    }

Happy to provide a PR :)