Last active
February 16, 2024 14:29
-
-
Save asugrad/5eb0b4348ce8ac7b5439e70dd5cee40c to your computer and use it in GitHub Desktop.
WordPress Ninja Forms Export Submissions to CSV
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
/** | |
* WP CLI custom command to export a form submsissions from Ninja Forms | |
* The to_csv function will create a 'reports' directory | |
* if it does not exist and create a datetime stamped csv | |
* | |
* ## OPTIONS | |
* | |
* <id> | |
* : The form id. | |
* <send_to_sftp> | |
* : Send the submissions to an SFTP server. | |
* | |
* ## EXAMPLES | |
* | |
* wp ninja-forms-export to_csv 1 | |
* | |
* @synopsis <id> | |
*/ | |
if (defined('WP_CLI') && WP_CLI) { | |
class Export_Submissions extends WP_CLI_Command | |
{ | |
public function to_csv($args, $assoc_args) | |
{ | |
list($form_id) = $args; | |
$sub_ids = []; | |
if (empty($form_id)) { | |
WP_CLI::error('Please pass the form id as an argument like this; wp ninja-forms-export to_csv 1'); | |
} | |
$form = Ninja_Forms()->form($form_id[0])->get(); | |
$form_title = str_replace(' ', '_', strtolower($form->get_setting('title'))); | |
$date_format = Ninja_Forms()->get_setting('date_format'); | |
$field_labels = array( | |
'_seq_num' => '#id', | |
'_date_submitted' => __('Date Submitted', 'ninja-forms') | |
); | |
// Legacy Filter from 2.9.* | |
$field_labels = apply_filters('nf_subs_csv_label_array_before_fields', $field_labels, $sub_ids); | |
$fields = Ninja_Forms()->form($form_id[0])->get_fields(); | |
if (! has_filter('ninja_forms_get_fields_sorted')) { | |
uasort($fields, array( 'NF_Database_Models_Submission', 'sort_fields' )); | |
} | |
$hidden_field_types = apply_filters('nf_sub_hidden_field_types', array()); | |
$subs = Ninja_Forms()->form($form_id[0])->get_subs(array(), false, $sub_ids); | |
foreach ($subs as $sub) { | |
$value[ '_seq_num' ] = $sub->get_seq_num(); | |
$value[ '_date_submitted' ] = $sub->get_sub_date($date_format); | |
foreach ($fields as $field_id => $field) { | |
if (!is_int($field_id)) { | |
continue; | |
} | |
if (in_array($field->get_setting('type'), $hidden_field_types)) { | |
continue; | |
} | |
if ($field->get_setting('admin_label')) { | |
$field_labels[ $field->get_id() ] = $field->get_setting('admin_label'); | |
} else { | |
$field_labels[ $field->get_id() ] = $field->get_setting('label'); | |
} | |
$field_value = maybe_unserialize($sub->get_field_value($field_id)); | |
$field_value = apply_filters('nf_subs_export_pre_value', $field_value, $field_id); | |
$field_value = apply_filters('ninja_forms_subs_export_pre_value', $field_value, $field_id, $form_id); | |
$field_value = apply_filters('ninja_forms_subs_export_field_value_' . $field->get_setting('type'), $field_value); | |
if (is_array($field_value)) { | |
$field_value = implode(',', $field_value); | |
} | |
$value[ $field_id ] = $field_value; | |
} | |
$value_array[] = $value; | |
} | |
if (empty($value_array)) { | |
WP_CLI::error('The form id ' . $form_id[0] . ' does not exist.'); | |
} | |
$value_array = WPN_Helper::stripslashes($value_array); | |
// Legacy Filter from 2.9.* | |
$value_array = apply_filters('nf_subs_csv_value_array', $value_array, $sub_ids); | |
$csv_array[ 0 ][] = $field_labels; | |
$csv_array[ 1 ][] = $value_array; | |
// Get any extra data from our other plugins... | |
$csv_array = apply_filters('nf_subs_csv_extra_values', $csv_array, $subs, $form_id); | |
$date = new DateTime('now'); | |
$directory_name = 'reports'; | |
$file_name = $form_title . '_form_id_'.$form_id[0].'_'.$date->format('Y-m-d_H_i_s') . '.csv'; | |
if (!is_dir($directory_name)) { | |
mkdir($directory_name, 0755); | |
} | |
header('Content-type: application/csv'); | |
header('Content-Disposition: attachment; filename="'.$file_name .'"'); | |
header('Pragma: no-cache'); | |
header('Expires: 0'); | |
$output = apply_filters('nf_sub_csv_bom', "\xEF\xBB\xBF") ; // Byte Order Mark | |
$output .= WPN_Helper::str_putcsv($csv_array, | |
apply_filters('nf_sub_csv_delimiter', ','), | |
apply_filters('nf_sub_csv_enclosure', '"'), | |
apply_filters('nf_sub_csv_terminator', "\n") | |
); | |
file_put_contents($directory_name.'/'.$file_name, $output); | |
// if (WP_CLI\Utils\get_flag_value($assoc_args, 'send_to_sftp')) { | |
// $host = env('SFTP_SERVER'); | |
// $user_name = env('SFTP_USER_NAME'); | |
// $pass = env('SFTP_PASSWORD'); | |
// | |
// $remote = "sftp://$user_name:$pass@$host/$file_name"; | |
// | |
// $sftp = new SFTP($host); | |
// if (!$sftp->login($user_name, $pass)) { | |
// exit('User name and/or password not accepted.'); | |
// } | |
// | |
// $sftp->put($file_name, $output); | |
// WP_CLI::line('File uploaded to SFTP.'); | |
// } | |
WP_CLI::success('Ninja forms submissions have been exported!'); | |
die(); | |
} | |
} | |
WP_CLI::add_command('ninja-forms-export', 'Export_Submissions'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script had some errors / problems, I fixed some of them. Here is my version: