Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phillipwilhelm/653f49423d6e8d15215052401d140014 to your computer and use it in GitHub Desktop.
Save phillipwilhelm/653f49423d6e8d15215052401d140014 to your computer and use it in GitHub Desktop.
Ounce of Talent // Gravity Forms // Export Entry to CSV and Email on Submission
<?php
/**
* Ounce of Talent // 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.0
* @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, 4 );
}
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 );
$csv = ob_get_clean();
self::$_current_entry = null;
remove_filter( 'gform_leads_before_export', array( $this, 'filter_export_entries' ) );
$file_deets = GFFormsModel::get_file_upload_path( $form['id'], 'my-test-file.csv' );
$file = fopen( $file_deets['path'], 'w' );
fwrite( $file, $csv );
fclose( $file );
return $file_deets;
}
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'
)
) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment