Last active
October 17, 2024 11:34
-
-
Save bhowe/c91ecdf47a3fa7125be4b0c0beb4e07d to your computer and use it in GitHub Desktop.
Export gravity form entries every month
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
function export_gf_entries(){ | |
$send_to = $_REQUEST['sendto'] ?? 'youremailhere'; | |
$form_id = $_REQUEST['fid'] ?? 1; | |
$form = RGFormsModel::get_form_meta($form_id); | |
$fields = array(); | |
if(is_array($form["fields"])){ | |
foreach($form["fields"] as $field){ | |
if(isset($field["inputs"]) && is_array($field["inputs"])){ | |
foreach($field["inputs"] as $input) | |
$fields[] = array($input["id"], GFCommon::get_label($field, $input["id"])); | |
} | |
else if(!rgar($field, 'displayOnly')){ | |
$fields[] = array($field["id"], GFCommon::get_label($field)); | |
} | |
} | |
} | |
$_grp_data = []; | |
$line_data = []; | |
foreach($fields as $fld){ | |
$line_data[] = $fld[1] ?? ''; | |
} | |
//adjust headers | |
$line_data[] = 'Created By (User Id)'; | |
$line_data[] = 'Entry Id'; | |
$line_data[] = 'Entry Date'; | |
$line_data[] = 'Date Updated'; | |
$line_data[] = 'Source Url'; | |
$line_data[] = 'Transaction Id'; | |
$line_data[] = 'Payment Amount'; | |
$line_data[] = 'Payment Date'; | |
$line_data[] = 'Payment Status'; | |
$line_data[] = 'Post Id'; | |
$line_data[] = 'User Agent'; | |
$line_data[] = 'User IP'; | |
$_grp_data[] = '"' . implode('","', $line_data) . '"'; | |
$search_criteria = array(); | |
$start_date = date( 'Y-m-d', strtotime('-31 days') ); | |
$end_date = date( 'Y-m-d', time() ); | |
$search_criteria['start_date'] = $start_date; | |
$search_criteria['end_date'] = $end_date; | |
$results = GFAPI::get_entries($form_id, $search_criteria ); | |
foreach($results as $rs){ | |
$line_data = []; | |
foreach($fields as $fld){ | |
$line_data[] = $rs[$fld[0]] ?? ''; | |
} | |
//data for you form | |
$line_data[] = $rs['created_by'] ?? ''; | |
$line_data[] = $rs['id'] ?? ''; | |
$line_data[] = $rs['date_created'] ?? ''; | |
$line_data[] = $rs['date_updated'] ?? ''; | |
$line_data[] = $rs['source_url'] ?? ''; | |
$line_data[] = $rs['transaction_id'] ?? ''; | |
$line_data[] = $rs['payment_amount'] ?? ''; | |
$line_data[] = $rs['payment_date'] ?? ''; | |
$line_data[] = $rs['payment_status'] ?? ''; | |
$line_data[] = $rs['post_id'] ?? ''; | |
$line_data[] = $rs['user_agent'] ?? ''; | |
$line_data[] = $rs['ip'] ?? ''; | |
$_grp_data[] = '"' . implode('","', $line_data) . '"'; | |
} | |
$uploads = wp_upload_dir(); | |
$_loc_file = $uploads['basedir'] . '/gf-'. $form_id .'-entries-' . strtotime('now') . '.csv'; | |
file_put_contents($_loc_file, implode(PHP_EOL, $_grp_data)); | |
//$attachments = array(ABSPATH . '/uploads/abc.png'); | |
$attachments = array($_loc_file); | |
wp_mail($send_to, 'Gravity From #' . $form_id . ' Entries Last 30 days!', 'See attached for the exported entries from gravity form # ' . $form_id, [], $attachments); | |
@unlink($_loc_file); | |
echo 'Sent!'; | |
wp_die(); | |
} | |
//dangerous use for tsting | |
add_action( 'wp_ajax_nopriv_gfexport', 'export_gf_entries'); | |
add_action( 'wp_ajax_gfexport', 'export_gf_entries'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment