Last active
July 26, 2018 01:47
-
-
Save spivurno/92734d769e0ec8c0eaf1 to your computer and use it in GitHub Desktop.
Ounce of Talent // Gravity Forms // Export Entry to CSV and Email on Submission
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gravity Wiz // Gravity Forms // Export Entry to CSV and Email on Submission | |
* | |
* This snippet will export the entry to CSV format, email that CSV file to a specified email address, and then delete the entry. | |
* | |
* @version 1.1 | |
* @author David Smith <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/... | |
*/ | |
class BT_Email_CSV { | |
private static $_current_entry; | |
public function __construct( $args = array() ) { | |
// set our default arguments, parse against the provided arguments, and store for use throughout the class | |
$this->_args = wp_parse_args( $args, array( | |
'form_id' => false, | |
'email' => false | |
) ); | |
// do version check in the init to make sure if GF is going to be loaded, it is already loaded | |
add_action( 'init', array( $this, 'init' ) ); | |
} | |
public function init() { | |
// make sure we're running the required minimum version of Gravity Forms | |
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) { | |
return; | |
} | |
// carry on | |
add_action( 'gform_after_submission_' . $this->_args['form_id'], array( $this, 'email_csv' ), 20, 2 ); | |
} | |
public function email_csv( $entry, $form ) { | |
$csv_deets = $this->get_csv( $form, $entry ); | |
wp_mail( $this->_args['email']['to'], $this->_args['email']['subject'], $this->_args['email']['message'], '', array( $csv_deets['path'] ) ); | |
// delete CSV from file system once email is sent | |
unlink( $csv_deets['path'] ); | |
// delete entry once email is sent | |
GFAPI::delete_entry( $entry['id'] ); | |
} | |
public function get_csv( $form, $entry ) { | |
require_once( GFCommon::get_base_path() . '/export.php' ); | |
self::$_current_entry = $entry; | |
add_filter( 'gform_leads_before_export', array( $this, 'filter_export_entries' ) ); | |
ob_start(); | |
$_POST['export_field'] = array_keys( $entry ); | |
GFExport::start_export( $form, 0, $entry['id'] ); | |
$csv = ob_get_clean(); | |
self::$_current_entry = null; | |
remove_filter( 'gform_leads_before_export', array( $this, 'filter_export_entries' ) ); | |
//GFFormsModel::get_file_upload_path( $form['id'], sprintf( 'export-%d.csv', $entry['id'] ) ); | |
return $this->get_file_path( $entry['id'] ); | |
} | |
public function get_file_path( $entry_id ){ | |
$export_folder = trailingslashit( GFFormsModel::get_upload_root() . 'export' ); | |
$result = array( 'path' => $export_folder . sanitize_file_name( 'export-' . $entry_id .'.csv' ) ); | |
return $result; | |
} | |
public function filter_export_entries( $entries ) { | |
if( ! self::$_current_entry ) { | |
return $entries; | |
} | |
foreach( $entries as $entry ) { | |
if( $entry['id'] == self::$_current_entry['id'] ) { | |
return array( $entry ); | |
} | |
} | |
return $entries; | |
} | |
} | |
# Configuration | |
new BT_Email_CSV( array( | |
'form_id' => 414, | |
'email' => array( | |
'to' => '[email protected]', | |
'subject' => 'This is the subject', | |
'message' => 'This is the message' | |
) | |
) ); |
Thanks @tw-360vier. The snippet has been updated.
Hey David,
This seems to work fine. Couple of questions
- How can i use a specific file name taken from fields ? (i.e. {name}_{surname})
- How to exclude certain columns from the generated CSV?
Thanks in advance.
@spivurno
Hello David,
Would you have plans to update the script with the following:
- Limit the .csv to show only the most recent entry
- remove the extra fields such as the url and browser information
Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By now,
GFExport::start_export( $form )
doesn't echo the file contents any more, but writes a file to the uploads folder.This version deals with that issue: