-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-table-csv-export.php
More file actions
128 lines (105 loc) · 3.2 KB
/
database-table-csv-export.php
File metadata and controls
128 lines (105 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
Plugin Name: Database Table CSV Export
Plugin URI: https://github.com/dhananjaya3107/Database-Table-CSV-Export.git
Description: Plugin to export any database table in to CSV
Version: 1.0
Author: Dananjaya Maha Malage
Author URI: http://www.whenalive.com/
*/
class CSVExport
{
/**
* Constructor
*/
public function __construct()
{
global $table_name;
if(isset($_POST['table-name']))
{
$table_name = $_POST['table-name'];
$csv = $this->generate_csv($table_name);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"Data-Table-Export.csv\";" );
header("Content-Transfer-Encoding: binary");
echo $csv;
exit;
}
// Add extra menu items for admins
add_action('admin_menu', array($this, 'admin_menu'));
// Create end-points
add_filter('query_vars', array($this, 'query_vars'));
add_action('parse_request', array($this, 'parse_request'));
}
/**
* Add extra menu items for admins
*/
public function admin_menu()
{
add_menu_page('CSV Export', 'CSV Export', 'manage_options', 'download_report', array($this, 'download_report'));
}
/**
* Allow for custom query variables
*/
public function query_vars($query_vars)
{
$query_vars[] = 'download_report';
return $query_vars;
}
/**
* Parse the request
*/
public function parse_request(&$wp)
{
if(array_key_exists('download_report', $wp->query_vars))
{
$this->download_report();
exit;
}
}
/**
* Download report
*/
public function download_report()
{
echo '<div class="wrap">';
echo '<div id="icon-tools" class="icon32"></div>';
echo '<h2>Export Data Tables</h2>';
echo '<p>Select the table from drop down you wish to export!</p>';
?>
<form action="" method="post">
<select name="table-name">
<option value="">Select a Table</option>
<option value="posts">Posts</option>
<option value="postmeta">Postmeta</option>
<option value="users">Users</option>
<option value="usermeta">Usersmeta</option>
</select>
<input type="submit">
</form>
<?php
}
/**
* Converting data to CSV
*/
public function generate_csv($table_name)
{
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix.$table_name.";",ARRAY_A);
if (empty($results)) {
return;
}
$csv_output = '"'.implode('","',array_keys($results[0])).'",'."\n";;
foreach ($results as $row) {
$csv_output .= '"'.implode('","',$row).'",'."\n";
}
$csv_output .= "\n";
return $csv_output;
}
}
// Instantiate a singleton of this plugin
$csvExport = new CSVExport();