Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakejackson1/079cc854dc68d1cdbc513fca929b08d5 to your computer and use it in GitHub Desktop.
Save jakejackson1/079cc854dc68d1cdbc513fca929b08d5 to your computer and use it in GitHub Desktop.
Support the current version of WP Offload Media (as of Oct 2021) for Gravity Forms Single and Multi File Upload field. If WP Offload Media is configured to delete local files after offloading, this will do it too.
<?php
add_action( 'init', function() {
/** @var $as3cf Amazon_S3_And_Cloudfront */
global $as3cf;
if ( ! $as3cf instanceof Amazon_S3_And_Cloudfront || ! $as3cf->is_plugin_setup( true ) ) {
return;
}
add_filter( 'gform_upload_path', 's3_gform_change_upload_path', 10, 2 );
add_action( 'gform_after_submission', 's3_gform_submit_to_s3', 10, 2 );
}, 20 );
/**
* Get the S3 URL
*
* @return string
*/
function s3_get_delivery_domain() {
/** @var $as3cf Amazon_S3_And_Cloudfront */
global $as3cf;
$scheme = $as3cf->get_url_scheme();
$enable_delivery_domain = $as3cf->get_delivery_provider()->delivery_domain_allowed() ? $as3cf->get_setting( 'enable-delivery-domain' ) : false;
$delivery_domain = $as3cf->get_setting( 'delivery-domain' );
if ( ! $enable_delivery_domain || empty( $delivery_domain ) ) {
$region = $as3cf->get_setting( 'region' );
if ( is_wp_error( $region ) ) {
return $region;
}
$delivery_domain = $as3cf->get_storage_provider()->get_url_domain( $as3cf->get_setting( 'bucket' ), $region );
} else {
$delivery_domain = AS3CF_Utils::sanitize_custom_domain( $delivery_domain );
}
return "$scheme://$delivery_domain";
}
/**
* Convert the
*
* @param array $path_info
* @param int $form_id
*
* @return string
*/
function s3_gform_change_upload_path( $path_info, $form_id ) {
$gform_link = str_replace( content_url(), '', $path_info['url'] );
$path_info['url'] = s3_get_delivery_domain() . $gform_link;
return $path_info;
}
/**
* Upload the files to S3
*
* @param array $entry
* @param array $form
*
* @throws Exception
*/
function s3_gform_submit_to_s3( $entry, $form ) {
/** @var $as3cf Amazon_S3_And_Cloudfront */
global $as3cf;
/* if $form is invalid, exit */
if ( empty( $form['fields'] ) ) {
return;
}
$bucket = $as3cf->get_setting( 'bucket' );
$region = $as3cf->get_setting( 'region' );
if ( is_wp_error( $region ) ) {
$region = '';
}
$provider_client = $as3cf->get_provider_client( $as3cf->get_setting( $region ) );
$uploads_dir = wp_upload_dir();
$upload_folder_name = wp_basename( $uploads_dir['basedir'] );
$file_name_strip_url = s3_get_delivery_domain() . '/' . $upload_folder_name . '/gravity_forms';
$remove_local_files = $as3cf->get_setting('remove-local-file');
/* Loop over the form fields and process any fileupload field that has a value */
foreach ( $form['fields'] as $field ) {
if ( $field->get_input_type() !== 'fileupload' || empty( $entry[ $field->id ] ) ) {
continue;
}
/* Handle both single and multi file upload fields */
if ( $field->multipleFiles ) {
$urls = (array) json_decode( $entry[ $field->id ], true );
} else {
$urls = [ $entry[ $field->id ] ];
}
/* Loop over the URLs and upload each one */
foreach ( array_filter( $urls ) as $url ) {
$file_name = str_replace( s3_get_delivery_domain() . '/', '', $url );
$file_path = untrailingslashit( GFFormsModel::get_upload_root() ) . str_replace( $file_name_strip_url, '', $url );
try {
$type = wp_check_filetype_and_ext( $file_path, wp_basename( $file_path ) )['type'];
if( ! is_string( $type ) ) {
$type = '';
}
$args = [
'Bucket' => $bucket,
'Key' => $file_name,
'SourceFile' => $file_path,
'ContentType' => $type,
'ACL' => $as3cf->get_storage_provider()->get_default_acl(),
];
$provider_client->upload_object( $args );
if ( $remove_local_files ) {
if ( ! @unlink( $file_path ) ) {
$message = 'Error removing local file ';
if ( ! file_exists( $file_path ) ) {
$message = "Error removing local file. Couldn't find the file at ";
} elseif ( ! is_writable( $file_path ) ) {
$message = 'Error removing local file. Ownership or permissions are mis-configured for ';
}
GFCommon::log_error( $message . $file_path );
}
}
} catch ( \Exception $e ) {
/* Log an error to the Gravity Forms log */
$error_msg = sprintf( __( 'Error offloading %s to provider: %s', 'amazon-s3-and-cloudfront' ), $file_path, $e->getMessage() );
GFCommon::log_error( $error_msg );
}
}
}
}
@jakejackson1
Copy link
Author

Thanks to https://gist.github.com/antonmaju/eccf799602e95e386670 for providing a starting point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment