Skip to content

Instantly share code, notes, and snippets.

@spivurno
Last active July 26, 2018 01:47
Show Gist options
  • Save spivurno/92734d769e0ec8c0eaf1 to your computer and use it in GitHub Desktop.
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
<?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'
)
) );
@bl4ckcoff33
Copy link

bl4ckcoff33 commented Nov 20, 2017

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.

@oneblackcrayon
Copy link

@spivurno
Hello David,
Would you have plans to update the script with the following:

  1. Limit the .csv to show only the most recent entry
  2. 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