add export to csv
This commit is contained in:
@@ -132,7 +132,6 @@ function go_form_handle_submission()
|
||||
exit;
|
||||
}
|
||||
add_action('admin_post_go_form_submit', 'go_form_handle_submission');
|
||||
add_action('admin_post_nopriv_go_form_submit', 'go_form_handle_submission');
|
||||
|
||||
// ========== Shortcode ==========
|
||||
function go_form_shortcode($atts)
|
||||
@@ -147,6 +146,7 @@ function go_form_shortcode($atts)
|
||||
add_shortcode('go_form', 'go_form_shortcode');
|
||||
|
||||
// ========== Admin Actions ==========
|
||||
|
||||
function go_form_admin_action($type)
|
||||
{
|
||||
if (!isset($_POST["go_form_{$type}_nonce"]) || !wp_verify_nonce($_POST["go_form_{$type}_nonce"], "go_form_{$type}_action"))
|
||||
@@ -205,7 +205,46 @@ function go_form_delete_form()
|
||||
}
|
||||
add_action('admin_post_go_form_delete_form', 'go_form_delete_form');
|
||||
|
||||
// ========== Admin Page ==========
|
||||
function go_form_export()
|
||||
{
|
||||
go_form_admin_action('export');
|
||||
if (!isset($_POST['form_id'])) {
|
||||
wp_redirect(admin_url('admin.php?page=go-form-settings'));
|
||||
exit;
|
||||
}
|
||||
|
||||
$form_id = intval($_POST['form_id']);
|
||||
$form_name = go_form_get_form_name($form_id);
|
||||
$entries = go_form_get_entries($form_id);
|
||||
|
||||
header('Content-Type: text/csv; charset=utf-8');
|
||||
header('Content-Disposition: attachment; filename="go-form-' . sanitize_title($form_name) . '-export.csv"');
|
||||
|
||||
$output = fopen('php://output', 'w');
|
||||
fputcsv($output, ['ID', 'First Name', 'Last Name', 'Country', 'Club', 'Rank', 'Email', 'EGD Number', 'Comment', 'Date Added']);
|
||||
|
||||
foreach ($entries as $e) {
|
||||
global $ranks;
|
||||
fputcsv($output, [
|
||||
$e->id,
|
||||
$e->first_name,
|
||||
$e->last_name,
|
||||
$e->country,
|
||||
$e->club,
|
||||
$ranks[$e->rank] ?? '',
|
||||
$e->email,
|
||||
$e->egd_number,
|
||||
$e->comment,
|
||||
$e->created_at
|
||||
]);
|
||||
}
|
||||
|
||||
fclose($output);
|
||||
exit;
|
||||
}
|
||||
add_action('admin_post_go_form_export', 'go_form_export');
|
||||
|
||||
// ========== Admin Page ====================
|
||||
function go_form_admin_menu()
|
||||
{
|
||||
add_menu_page('Go Form Settings', 'Go Form', 'manage_options', 'go-form-settings', 'go_form_settings_page', 'dashicons-admin-generic');
|
||||
@@ -241,12 +280,20 @@ function go_form_settings_page()
|
||||
|
||||
$form_name = go_form_get_form_name($selected_form_id);
|
||||
echo "<h3> $form_name Entries</h3>";
|
||||
echo '<form method="post" action="' . admin_url('admin-post.php') . '" style="margin-bottom:15px">
|
||||
echo '<div style="margin-bottom:15px">';
|
||||
echo '<form method="post" action="' . admin_url('admin-post.php') . '" style="display:inline-block;margin-right:10px">
|
||||
' . wp_nonce_field('go_form_delete_form_action', 'go_form_delete_form_nonce', true, false) . '
|
||||
<input type="hidden" name="action" value="go_form_delete_form">
|
||||
<input type="hidden" name="form_id" value="' . esc_attr($selected_form_id) . '">
|
||||
<input type="submit" value="Delete Form" class="button delete" onclick="return confirm(\'Delete this form and ALL entries?\')">
|
||||
</form>';
|
||||
echo '<form method="post" action="' . admin_url('admin-post.php') . '" style="display:inline-block">
|
||||
' . wp_nonce_field('go_form_export_action', 'go_form_export_nonce', true, false) . '
|
||||
<input type="hidden" name="action" value="go_form_export">
|
||||
<input type="hidden" name="form_id" value="' . esc_attr($selected_form_id) . '">
|
||||
<input type="submit" value="Export to CSV" class="button button-primary">
|
||||
</form>';
|
||||
echo '</div>';
|
||||
$entries = go_form_get_entries($selected_form_id);
|
||||
go_form_render_entries_table($entries, true);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user