Export Users to CSV - Version 0.2

Version Description

  • First public release.
  • Improved memory usage.
  • Added date range selection.
  • Added readme.txt.
Download this release

Release Info

Developer sorich87
Plugin Icon 128x128 Export Users to CSV
Version 0.2
Comparing to
See all releases

Version 0.2

Files changed (3) hide show
  1. export-users-to-csv.php +231 -0
  2. readme.txt +79 -0
  3. screenshot-1.png +0 -0
export-users-to-csv.php ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package Export_Users_to_CSV
4
+ * @version 0.2
5
+ */
6
+ /*
7
+ Plugin Name: Export Users to CSV
8
+ Plugin URI: http://pubpoet.com/plugins/
9
+ Description: Export Users data and metadata to a csv file.
10
+ Version: 0.2
11
+ Author: PubPoet
12
+ Author URI: http://pubpoet.com/
13
+ License: GPL2
14
+ */
15
+ /* Copyright 2011 Ulrich Sossou (http://github.com/sorich87)
16
+
17
+ This program is free software; you can redistribute it and/or modify
18
+ it under the terms of the GNU General Public License, version 2, as
19
+ published by the Free Software Foundation.
20
+
21
+ This program is distributed in the hope that it will be useful,
22
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ GNU General Public License for more details.
25
+
26
+ You should have received a copy of the GNU General Public License
27
+ along with this program; if not, write to the Free Software
28
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29
+ */
30
+
31
+ /**
32
+ * Main plugin class
33
+ *
34
+ * @since 0.1
35
+ **/
36
+ class PP_EU_Export_Users {
37
+
38
+ /**
39
+ * Class contructor
40
+ *
41
+ * @since 0.1
42
+ **/
43
+ public function __construct() {
44
+ add_action( 'admin_menu', array( $this, 'add_admin_pages' ) );
45
+ add_action( 'init', array( $this, 'generate_csv' ) );
46
+ add_filter( 'pp_eu_exclude_data', array( $this, 'exclude_data' ) );
47
+ }
48
+
49
+ /**
50
+ * Add administration menus
51
+ *
52
+ * @since 0.1
53
+ **/
54
+ public function add_admin_pages() {
55
+ add_users_page( __( 'Export to CSV' ), __( 'Export to CSV' ), 'list_users', 'export-users-to-csv', array( $this, 'users_page' ) );
56
+ }
57
+
58
+ /**
59
+ * Process content of CSV file
60
+ *
61
+ * @since 0.1
62
+ **/
63
+ public function generate_csv() {
64
+ if ( isset( $_POST['_wpnonce-pp-eu-export-users-users-page_export'] ) ) {
65
+ check_admin_referer( 'pp-eu-export-users-users-page_export', '_wpnonce-pp-eu-export-users-users-page_export' );
66
+
67
+ $args = array(
68
+ 'fields' => 'all_with_meta',
69
+ 'role' => stripslashes( $_POST['role'] )
70
+ );
71
+
72
+ if ( ! empty( $args['role'] ) ) {
73
+ global $wp_roles;
74
+ $roles = array_keys( $wp_roles->role_names );
75
+
76
+ if ( ! in_array( $_POST['role'], $roles ) )
77
+ $args['role'] = '';
78
+ }
79
+
80
+ add_action( 'pre_user_query', array( $this, 'pre_user_query' ) );
81
+ $users = get_users( $args );
82
+ remove_action( 'pre_user_query', array( $this, 'pre_user_query' ) );
83
+
84
+ if ( ! $users ) {
85
+ $referer = add_query_arg( 'error', 'empty', wp_get_referer() );
86
+ wp_redirect( $referer );
87
+ exit;
88
+ }
89
+
90
+ $sitename = sanitize_key( get_bloginfo( 'name' ) );
91
+ if ( ! empty( $sitename ) )
92
+ $sitename .= '.';
93
+ $filename = $sitename . 'users.' . date( 'Y-m-d-H-i-s' ) . '.csv';
94
+
95
+ header( 'Content-Description: File Transfer' );
96
+ header( 'Content-Disposition: attachment; filename=' . $filename );
97
+ header( 'Content-Type: text/csv; charset=' . get_option( 'blog_charset' ), true );
98
+
99
+ $exclude_data = apply_filters( 'pp_eu_exclude_data', array() );
100
+
101
+ $first_user = reset( $users );
102
+ $fields = array_keys( (array) $first_user->data );
103
+ $headers = array();
104
+ foreach ( $fields as $key => $field ) {
105
+ if ( in_array( $field, $exclude_data ) )
106
+ unset( $fields[$key] );
107
+ else
108
+ $headers[] = '"' . $field . '"';
109
+ }
110
+ echo implode( ',', $headers ) . "\n";
111
+
112
+ foreach ( $users as $user ) {
113
+ $data = array();
114
+ foreach ( $fields as $field ) {
115
+ $user_data = (array) $user->data;
116
+ $value = $user_data[$field];
117
+ $data[] = is_array( $value ) ? '"' . serialize( $value ) . '"' : '"' . $value . '"';
118
+ }
119
+ echo implode( ',', $data ) . "\n";
120
+ }
121
+
122
+ exit;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Content of the settings page
128
+ *
129
+ * @since 0.1
130
+ **/
131
+ public function users_page() {
132
+ if ( ! current_user_can( 'list_users' ) )
133
+ wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
134
+ ?>
135
+
136
+ <div class="wrap">
137
+ <h2><?php _e( 'Export users to a CSV file' ); ?></h2>
138
+ <?php
139
+ if ( isset( $_GET['error'] ) ) {
140
+ echo '<div class="updated"><p><strong>' . __( 'No user found.' ) . '</strong></p></div>';
141
+ }
142
+ ?>
143
+ <form method="post" action="" enctype="multipart/form-data">
144
+ <?php wp_nonce_field( 'pp-eu-export-users-users-page_export', '_wpnonce-pp-eu-export-users-users-page_export' ); ?>
145
+ <table class="form-table">
146
+ <tr valign="top">
147
+ <th scope="row"><label for"pp_eu_users_role"><?php _e( 'Role' ); ?></label></th>
148
+ <td>
149
+ <select name="role" id="pp_eu_users_role">
150
+ <?php
151
+ echo '<option value="">' . __( 'Every Role' ) . '</option>';
152
+ global $wp_roles;
153
+ foreach ( $wp_roles->role_names as $role => $name ) {
154
+ echo "\n\t<option value='" . esc_attr( $role ) . "'>$name</option>";
155
+ }
156
+ ?>
157
+ </select>
158
+ </td>
159
+ </tr>
160
+ <tr valign="top">
161
+ <th scope="row"><label><?php _e( 'Date range' ); ?></label></th>
162
+ <td>
163
+ <select name="start_date" id="pp_eu_users_start_date">
164
+ <option value="0"><?php _e( 'Start Date' ); ?></option>
165
+ <?php $this->export_date_options(); ?>
166
+ </select>
167
+ <select name="end_date" id="pp_eu_users_end_date">
168
+ <option value="0"><?php _e( 'End Date' ); ?></option>
169
+ <?php $this->export_date_options(); ?>
170
+ </select>
171
+ </td>
172
+ </tr>
173
+ </table>
174
+ <p class="submit">
175
+ <input type="hidden" name="_wp_http_referer" value="<?php echo $_SERVER['REQUEST_URI'] ?>" />
176
+ <input type="submit" class="button-primary" value="<?php _e( 'Export' ); ?>" />
177
+ </p>
178
+ </form>
179
+ <?php
180
+ }
181
+
182
+ public function exclude_data() {
183
+ $exclude = array( 'user_pass', 'user_activation_key', 'user_status', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'show_admin_bar_admin', 'wp_capabilities', 'wp_user_level', 'wp_usersettings', 'wp_usersettingstime', 'wp_dashboard_quick_press_last_post_id', 'metaboxorder_dashboard', 'screen_layout_dashboard', 'closedpostboxes_dashboard', 'metaboxhidden_dashboard', 'metaboxhidden_forum', 'closedpostboxes_forum', 'plugins_last_view', 'metaboxhidden_program', 'closedpostboxes_program', 'metaboxhidden_navmenus', 'managenavmenuscolumnshidden', 'user_level', 'user_firstname', 'user_lastname', 'user_description' );
184
+
185
+ if ( ! is_multisite() )
186
+ $exclude = array_merge( $exclude, array( 'spam', 'deleted', 'primary_blog', 'source_domain' ) );
187
+
188
+ return $exclude;
189
+ }
190
+
191
+ public function pre_user_query( $user_search ) {
192
+ global $wpdb;
193
+
194
+ $where = '';
195
+
196
+ if ( ! empty( $_POST['start_date'] ) )
197
+ $where .= $wpdb->prepare( " AND $wpdb->users.user_registered >= %s", date( 'Y-m-d', strtotime($_POST['start_date']) ) );
198
+
199
+ if ( ! empty( $_POST['end_date'] ) )
200
+ $where .= $wpdb->prepare( " AND $wpdb->users.user_registered < %s", date( 'Y-m-d', strtotime( '+1 month', strtotime( $_POST['end_date'] ) ) ) );
201
+
202
+ if ( ! empty( $where ) )
203
+ $user_search->query_where = str_replace( 'WHERE 1=1', "WHERE 1=1$where", $user_search->query_where );
204
+
205
+ return $user_search;
206
+ }
207
+
208
+ private function export_date_options() {
209
+ global $wpdb, $wp_locale;
210
+
211
+ $months = $wpdb->get_results( "
212
+ SELECT DISTINCT YEAR( user_registered ) AS year, MONTH( user_registered ) AS month
213
+ FROM $wpdb->users
214
+ ORDER BY user_registered DESC
215
+ " );
216
+
217
+ $month_count = count( $months );
218
+ if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
219
+ return;
220
+
221
+ foreach ( $months as $date ) {
222
+ if ( 0 == $date->year )
223
+ continue;
224
+
225
+ $month = zeroise( $date->month, 2 );
226
+ echo '<option value="' . $date->year . '-' . $month . '">' . $wp_locale->get_month( $month ) . ' ' . $date->year . '</option>';
227
+ }
228
+ }
229
+ }
230
+
231
+ new PP_EU_Export_Users;
readme.txt ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: sorich87
3
+ Tags: user, users, csv, batch, export, exporter, admin
4
+ Requires at least: 3.2
5
+ Tested up to: 3.3
6
+ Stable tag: 0.2
7
+
8
+ Export users data and metadata to a csv file
9
+
10
+ == Description ==
11
+
12
+ A plugin that export ALL user data and meta data, and that works!
13
+
14
+ You can even export the users by role and registration date range.
15
+
16
+ = Features =
17
+
18
+ * Exports all users fields
19
+ * Exports users meta
20
+ * Exports users by role
21
+ * Exports users by date range
22
+
23
+ For feature request and bug reports, [please use the forums](http://wordpress.org/tags/export-users-to-csv?forum_id=10#postform).
24
+
25
+ == Installation ==
26
+
27
+ For an automatic installation through WordPress:
28
+
29
+ 1. Go to the 'Add New' plugins screen in your WordPress admin area
30
+ 1. Search for 'Export Users to CSV'
31
+ 1. Click 'Install Now' and activate the plugin
32
+ 1. Go the 'Users' menu, under 'Export to CSV'
33
+
34
+
35
+ Or use a nifty tool by WordPress lead developer Mark Jaquith:
36
+
37
+ 1. Visit [this link](http://coveredwebservices.com/wp-plugin-install/?plugin=export-users-to-csv) and follow the instructions.
38
+
39
+
40
+ For a manual installation via FTP:
41
+
42
+ 1. Upload the `export-users-to-csv` directory to the `/wp-content/plugins/` directory
43
+ 1. Activate the plugin through the 'Plugins' screen in your WordPress admin area
44
+ 1. Go the 'Users' menu, under 'Export to CSV'
45
+
46
+
47
+ To upload the plugin through WordPress, instead of FTP:
48
+
49
+ 1. Upload the downloaded zip file on the 'Add New' plugins screen (see the 'Upload' tab) in your WordPress admin area and activate.
50
+ 1. Go the 'Users' menu, under 'Export to CSV'
51
+
52
+ == Frequently Asked Questions ==
53
+
54
+ = How to use? =
55
+
56
+ Click on the 'Export to CSV' link in the 'Users' menu, choose the role and the date range or don't select anything if you want to export all users, then click 'Export'. That's all!
57
+
58
+ == Screenshots ==
59
+
60
+ 1. User export screen
61
+
62
+ == Changelog ==
63
+
64
+ = 0.2 =
65
+ * First public release.
66
+ * Improved memory usage.
67
+ * Added date range selection.
68
+ * Added readme.txt.
69
+
70
+ = 0.1 =
71
+ * First release.
72
+
73
+ == Upgrade Notice ==
74
+
75
+ = 0.2 =
76
+ Improved memory usage. Added date range selection. Added readme.txt.
77
+
78
+ = 0.1 =
79
+ First release.
screenshot-1.png ADDED
Binary file