Skip to content

Commit

Permalink
Merge pull request #11 from Automattic/add/upload-image-edits-to-s3
Browse files Browse the repository at this point in the history
Add/upload image edits to s3
  • Loading branch information
brettshumaker authored May 14, 2021
2 parents 714395c + b93b59b commit 5278ed9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ wp vip migration validate-attachments invalid-attachments.csv --url=example-site
The log is then available at `invalid-attachments.csv`. The full command can be found here:

https://github.com/Automattic/vip-go-mu-plugins/blob/master/wp-cli/vip-migrations.php#L165-L187


## Changelog

### 1.1.0
- Fix: Upload images edited within WordPress to the bucket.
22 changes: 22 additions & 0 deletions inc/class-s3-media-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function setup() {
// Perform on-the-fly media syncs by hooking into these actions
add_filter( 'wp_handle_upload', [ $this, 'add_attachment_to_s3' ], 10, 2 );
add_action( 'delete_attachment', [ $this, 'delete_attachment_from_s3' ], 10, 1 );
add_filter( 'wp_save_image_editor_file', [ $this, 'add_updated_attachment_to_s3' ], 10, 5 );
}
}

Expand Down Expand Up @@ -84,6 +85,27 @@ public function delete_attachment_from_s3( $post_id ) {
$this->s3()->deleteMatchingObjects( $bucket, $path );
}

/**
* When an image is about to be saved from the image editor, save the image first, and then try
* and copy it to S3.
*
* This filter is documented here https://developer.wordpress.org/reference/hooks/wp_save_image_editor_file/
*/
public function add_updated_attachment_to_s3( $override, $filename, $image, $mime_type, $post_ID ) {
// Go ahead and save the image
$override = $image->save( $filename, $mime_type );

// If there wasn't an error while saving the image, copy to S3.
if ( ! is_wp_error( $override ) ) {
// The image saved, try and send it to S3.
$source_path = $filename;
$destination_path = trailingslashit( $this->get_s3_bucket_url() ) . str_replace( 'vip://', '', $source_path );
copy( $source_path, $destination_path );
}

return $override;
}

/**
* Props S3 Uploads and HM: https://github.com/humanmade/S3-Uploads/
*
Expand Down
2 changes: 1 addition & 1 deletion s3-media-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Author: Alexis Kulash, WordPress VIP
* Text Domain: s3-media-sync
* Domain Path: /languages/
* Version: 1.0.0
* Version: 1.1.0
*/

/**
Expand Down

0 comments on commit 5278ed9

Please sign in to comment.