Introducing Ad Injector - Insert Custom Ads in Your WordPress Blog

SyncS3 for Gravity Forms allows you to send file uploads to any Amazon S3 bucket when your forms are submitted. Each time an entry is created, those files are sent to S3, and a reference to the file is saved in the entry on your website.

If you regularly find yourself deleting entries from your website, you may want to also delete the files that were sent to Amazon S3. If you don’t have a specific reason to keep the files, why pay for the space they take up?

By default, SyncS3 does not have a feature to automatically remove the files when an entry is deleted. This is by design, because we’d rather be safe and not remove files. Instead, you can elect to remove the files by using the following code snippet.

add_action( 'gform_delete_entry', 'syncs3_remove_s3_urls_on_delete_entry' );
/**
 * Delete an entry's files from Amazon S3 when an entry is deleted.
 * This fires before the entry is fully deleted, so the entry meta is still available.
 *
 * @param  int		$entry_id 		Entry ID to be deleted
 *
 * @return void
 */
function syncs3_remove_s3_urls_on_delete_entry( $entry_id ) {
	$s3_urls = gform_get_meta( $entry_id, 's3_urls' );

	if ( ! empty( $s3_urls ) ) {
		SyncS3::autoload();
		foreach ( $s3_urls as $field_id => $urls ) {
			if ( ! empty( $urls ) ) {
				foreach ( $urls as $url ) {
					if ( is_array( $url ) && isset( $url['file_url'] ) ) {
						$s3 = new \Aws\S3\S3Client( array(
							'version' => 'latest',
							'region' => $url['region'],
							'credentials' => array(
								'key' => $url['access_key'],
								'secret' => $url['secret_key'],
							),
						) );
						$s3->deleteObject([
						    'Bucket' => $url['bucket'],
						    'Key'    => $url['key']
						]);
					}
				}
			}
		}
	}
}

This code runs when an entry is deleted from your website’s database. Note that this is not the same as moving an entry to the trash. For this code to do its job – delete the files from S3 – you need to trash the entry, and then empty your trash.

Also, for this code to work, you need to be running SyncS3 Pro 1.2.0. Prior to 1.2.0, not enough information was saved about each file to remove them. Therefore, this will only work for deleting entries that were created on SyncS3 1.2.0 or later.

0 Comments

Leave a Reply

Your Cart
Your cart is currently empty.
Open Cart

How do you build WordPress websites?

Are you into using page builders to create websites and earn income? That's what we love doing, too! That's why we build extensions for the most popular WordPress page builders - to help you build awesome websites and make more money.

If you enjoy the same things, we'd love to have you as part of the Elegant Modules community. Let's get to know each other!

Interests

Welcome! We're so excited to get to meet you, and look forward to chatting with you soon.