/
export-customer-email.php
executable file
·250 lines (210 loc) · 8.84 KB
/
export-customer-email.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* Plugin Name: WooCommerce Export Customer Email
* Plugin URI: https://github.com/mhmithu/woocommerce-export-customer-email
* Description: Allows you to export all customer emails into a CSV file.
* Version: 1.3
* Author: MH Mithu
* Author URI: http://mithu.me/
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* ----------------------------------------------------------------------
* Copyright (C) 2015 MH Mithu (email: mail@mithu.me)
* ----------------------------------------------------------------------
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ----------------------------------------------------------------------
*/
// Including WP core file
if ( ! function_exists( 'get_plugins' ) )
require_once ABSPATH . 'wp-admin/includes/plugin.php';
// Whether plugin active or not
if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
class WC_Export_Customer_Email {
/**
* Class contructor
*
* @access public
* @return void
* */
public function __construct() {
add_action( 'init', array( $this, 'csv_generate' ) );
}
/**
* Preparing data
*
* @access private
* @return array
*/
private function csv_data() {
global $wpdb;
$ids = $wpdb->get_col( "SELECT DISTINCT `order_id` FROM `{$wpdb->prefix}woocommerce_order_items`" );
foreach ( $ids as $id ) {
$fname = get_post_meta( $id, '_billing_first_name' );
$lname = get_post_meta( $id, '_billing_last_name' );
$email[] = get_post_meta( $id, '_billing_email' );
$name[] = $fname[0].' '.$lname[0];
}
$email = wp_list_pluck( $email, 0 );
for ( $i = 0; $i < count( $name ); $i++ ) {
$body[$i+1] = array( $name[$i], $email[$i] );
}
$headers = array( 'Customer_Name', 'Customer_Email' );
$body = array( $headers ) + $body;
return $body;
}
/**
* Process content of CSV file
*
* @access public
* @return void
* */
public function csv_generate() {
if ( isset( $_POST['_wpnonce-mhm-export-customer-email'] ) ) {
check_admin_referer( 'mhm-export-customer-email', '_wpnonce-mhm-export-customer-email' );
$sitename = sanitize_key( get_bloginfo( 'name' ) );
if ( ! empty( $sitename ) )
$sitename .= '.';
$filename = $sitename . date( 'ymdHis', current_time( 'timestamp' ) ) . '.csv';
$data = $this->csv_data();
if ( $_POST['cname'] == 'no' ) {
for ( $i = 0; $i < count( $data ); $i++ ) {
unset( $data[$i][0] );
}
}
if ( $_POST['duplicate'] == 'yes' ) {
$data = array_map( 'unserialize', array_unique( array_map( 'serialize', $data ) ) );
}
$this->csv_header( $filename );
ob_start();
$file = @fopen( 'php://output', 'w' );
foreach ( $data as $list ) {
@fputcsv( $file, $list, ',' );
}
@fclose( $file );
ob_end_flush();
exit();
}
}
/**
* File headers
*
* @access private
* @param string $filename
* @return void
*/
private function csv_header( $filename ) {
send_nosniff_header();
nocache_headers();
@header( 'Content-Type: application/csv; charset=' . get_option( 'blog_charset' ), true );
@header( 'Content-Type: application/force-download' );
@header( 'Content-Description: File Transfer' );
@header( 'Content-Disposition: attachment; filename=' . $filename );
}
/**
* Get current WooCommerce version
*
* @access public
* @return void
*/
public function get_wc_version() {
global $woocommerce;
$plugin_folder = get_plugins( '/' . 'woocommerce' );
$plugin_file = 'woocommerce.php';
$wc_version = $plugin_folder[$plugin_file]['Version'];
return isset( $wc_version ) ? $wc_version : $woocommerce->version;
}
}
// The object
$export = new WC_Export_Customer_Email;
/**
* Export form
*
* @return string
*/
function export_action() {
if ( isset( $_GET['error'] ) ) {
echo '<div class="updated"><p><strong>No email found.</strong></p></div>';
}
?>
<form method="post" action="" enctype="multipart/form-data">
<?php wp_nonce_field( 'mhm-export-customer-email', '_wpnonce-mhm-export-customer-email' ); ?>
<table class="form-table">
<tbody>
<tr>
<th scope="row" class="titledesc"><label for="cname">Export customer name</label></th>
<td>
<label><input name="cname" value="yes" type="radio" checked="checked"> Yes</label>
<label><input name="cname" value="no" type="radio"> No</label>
</td>
</tr>
<tr>
<th scope="row" class="titledesc"><label for="duplicate">Remove duplicate emails</label></th>
<td>
<label><input name="duplicate" value="yes" type="radio"> Yes</label>
<label><input name="duplicate" value="no" type="radio" checked="checked"> No</label>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="hidden" name="_wp_http_referer" value="<?php echo $_SERVER['REQUEST_URI'] ?>" />
<input type="submit" class="button-primary" value="Export CSV" />
</p>
</form>
<?php
}
/**
* Hooking into WooCommerce by checking current version
* */
if ( version_compare( $export->get_wc_version(), '2.1', 'lt' ) ) {
function export_to_csv( $charts ) {
$charts['export'] = array(
'title' => 'Export',
'charts' => array(
"overview" => array(
'title' => 'Export customer email',
'description' => 'Click on <strong>Export</strong> button to generate and download customer\'s billing email data into a CSV file.',
'hide_title' => false,
'function' => 'export_action'
)
)
);
return $charts;
}
add_filter( 'woocommerce_reports_charts', 'export_to_csv' );
} else {
function export_to_csv( $reports ) {
$reports['customers']['reports']['export'] = array(
'title' => 'Export Customer Email',
'description' => 'Click on <strong>Export</strong> button to generate and download customer\'s billing email data into a CSV file.',
'hide_title' => true,
'callback' => 'export_action'
);
return $reports;
}
add_filter( 'woocommerce_admin_reports', 'export_to_csv' );
}
/**
* Fallback admin notice
*/
} else {
function wc_export_error_notice() {
global $current_screen;
if ( $current_screen->parent_base == 'plugins' ) {
echo '<div class="error"><p>The <strong>WooCommerce Export Customer Email</strong> plugin requires the <a href="http://wordpress.org/plugins/woocommerce" target="_blank">WooCommerce</a> plugin to be activated in order to work. Please <a href="'.admin_url( 'plugin-install.php?tab=search&type=term&s=WooCommerce' ).'" target="_blank">install WooCommerce</a> or <a href="'.admin_url( 'plugins.php' ).'">activate</a> first.</p></div>';
}
}
add_action( 'admin_notices', 'wc_export_error_notice' );
}