Create a gist now

Instantly share code, notes, and snippets.

Save Gravity Forms File Upload to Advanced Custom Fields Field in WordPress
<?php
/**
* Event Form.
*
* @since NEXT
*/
class WDSP_Event_Form {
/**
* Parent plugin class
*
* @var class
* @since NEXT
*/
protected $plugin = null;
/**
* Constructor
*
* @since NEXT
* @param object $plugin Main plugin object.
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
/**
* Initiate our hooks
*
* @since NEXT
*/
public function hooks() {
add_filter( 'gform_after_submission', array( $this, 'save_gravity_form_event_registration_file_to_media_library' ), 10, 2 );
}
/**
* Insert event registration file in the media library and attach it to the post.
* Delete the copy of it that was originally saved to the GF uploads directory.
*
* @since NEXT
* @author Kellen Mace
* @param array $entry The entry data.
* @param array $form The form data.
*/
public function save_gravity_form_event_registration_file_to_media_library( $entry, $form ) {
if ( 'Submit Event' !== $form['title'] ) {
return;
}
if ( ! isset( $entry['post_id'] ) ) {
return;
}
$gf_field_id = 38;
$acf_field_id = 'field_57bb39fca15a9b';
$post = get_post( $entry['post_id'] );
$file_url = $entry[ $gf_field_id ];
$relative_file_path = parse_url( $file_url, PHP_URL_PATH );
$absolute_file_path = untrailingslashit( ABSPATH ) . $relative_file_path;
$file_type = wp_check_filetype( $absolute_file_path )['type'];
$timeout_seconds = 5;
// Sideload media file.
$sideload_result = $this->sideload_media_file( $file_url, $file_type, $timeout_seconds );
// If an error occurred while trying to sideload the media file; continue to next blog.
if ( ! $sideload_result || ! empty( $sideload_result['error'] ) ) {
return;
}
$new_file_path = $sideload_result['file'];
$new_file_type = $sideload_result['type'];
// Insert file into the media library.
$attachment_id = $this->insert_media_file( $new_file_path, $new_file_type );
if ( $attachment_id ) {
// Update the ACF field to reference the newly uploaded file.
update_field( $acf_field_id, $attachment_id, $post->ID );
}
// Delete the original file from the Gravity Forms upload directory.
unlink( $absolute_file_path );
}
/**
* Sideload Media File
*
* @since 0.1.0
* @author Kellen Mace
* @param string $file_url The URL of the file to sideload.
* @param string $file_type The file type of the file to sideload.
* @param int $timeout_seconds The number of seconds to allow before sideloading times out.
* @return array On success, returns an associative array of file attributes. On failure, returns
* $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
*/
private function sideload_media_file( $file_url, $file_type, $timeout_seconds ) {
// Gives us access to the download_url() and wp_handle_sideload() functions.
require_once( ABSPATH . 'wp-admin/includes/file.php' );
// Download file to temp dir.
$temp_file = download_url( $file_url, $timeout_seconds );
if ( is_wp_error( $temp_file ) ) {
return false;
}
// Array based on $_FILE as seen in PHP file uploads.
$file = array(
'name' => basename( $file_url ),
'type' => $file_type,
'tmp_name' => $temp_file,
'error' => 0,
'size' => filesize( $temp_file ),
);
$overrides = array(
// Tells WordPress to not look for the POST form
// fields that would normally be present, default is true,
// we downloaded the file from a remote server, so there
// will be no form fields.
'test_form' => false,
// Setting this to false lets WordPress allow empty files – not recommended.
'test_size' => true,
// A properly uploaded file will pass this test.
// There should be no reason to override this one.
'test_upload' => true,
);
// Move the temporary file into the uploads directory.
return wp_handle_sideload( $file, $overrides );
}
/**
* Insert media file into the current site
*
* @since 0.1.0
* @author Kellen Mace
* @param string $file_path The path to the media file.
* @param string $file_type The mime type of the media file.
* @return int $inserted_attachment_id The inserted attachment ID, or 0 on failure.
*/
private function insert_media_file( $file_path = '', $file_type = '' ) {
if ( ! $file_path || ! $file_type ) {
return false;
}
// Get the path to the uploads directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment_data = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $file_path ),
'post_mime_type' => $file_type,
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_path ) ),
'post_content' => '',
'post_status' => 'inherit',
);
// Insert the attachment.
$inserted_attachment_id = wp_insert_attachment( $attachment_data, $file_path );
$inserted_attachment_path = get_attached_file( $inserted_attachment_id );
// Update the attachment metadata.
$this->update_inserted_attachment_metadata( $inserted_attachment_id, $inserted_attachment_path );
return $inserted_attachment_id;
}
/**
* Update inserted attachment metadata
*
* @since 0.1.0
* @author Kellen Mace
* @param int $inserted_attachment_id The inserted attachment ID.
* @param string $file_path The path to the media file.
*/
private function update_inserted_attachment_metadata( $inserted_attachment_id, $file_path ) {
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate metadata for the attachment and update the database record.
$attach_data = wp_generate_attachment_metadata( $inserted_attachment_id, $file_path );
wp_update_attachment_metadata( $inserted_attachment_id, $attach_data );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment