Last active
August 29, 2015 14:17
-
-
Save vodanh1213/7f07c6a0426420b2a769 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
/* | |
Plugin Name: Membership Users Exporter | |
Author: Hoang Ngo | |
Version: 1.0 | |
*/ | |
add_action('admin_menu', 'me_main_menu'); | |
add_action('wp_loaded', 'me_export_users'); | |
function me_main_menu() | |
{ | |
add_menu_page(__("Membership Users Exporter", "me"), __("Membership Users Exporter", "me"), 'manage_options', 'me_main_menu', 'membership_users_exporter'); | |
} | |
function me_export_users() | |
{ | |
if (!current_user_can('manage_options')) { | |
return ''; | |
} | |
if (isset($_POST['me_export'])) { | |
$users = get_users(); | |
$data = array(); | |
global $wpdb; | |
foreach ($users as $user) { | |
$street_address = $wpdb->get_var($wpdb->prepare( | |
"SELECT `value` FROM wp_bp_xprofile_data WHERE user_id = %d AND field_id = (SELECT id FROM wp_bp_xprofile_fields WHERE `name` = %s);", $user->ID, "Street Address" | |
)); | |
$apt = $wpdb->get_var($wpdb->prepare( | |
"SELECT `value` FROM wp_bp_xprofile_data WHERE user_id = %d AND field_id = (SELECT id FROM wp_bp_xprofile_fields WHERE `name` = %s);", $user->ID, "Apt / Unit No." | |
));; | |
$city = $wpdb->get_var($wpdb->prepare( | |
"SELECT `value` FROM wp_bp_xprofile_data WHERE user_id = %d AND field_id = (SELECT id FROM wp_bp_xprofile_fields WHERE `name` = %s);", $user->ID, "City" | |
));; | |
$provide = $wpdb->get_var($wpdb->prepare( | |
"SELECT `value` FROM wp_bp_xprofile_data WHERE user_id = %d AND field_id = (SELECT id FROM wp_bp_xprofile_fields WHERE `name` = %s);", $user->ID, "Province" | |
));; | |
$postal = $wpdb->get_var($wpdb->prepare( | |
"SELECT `value` FROM wp_bp_xprofile_data WHERE user_id = %d AND field_id = (SELECT id FROM wp_bp_xprofile_fields WHERE `name` = %s);", $user->ID, "Postal Code" | |
)); | |
$country = $wpdb->get_var($wpdb->prepare( | |
"SELECT `value` FROM wp_bp_xprofile_data WHERE user_id = %d AND field_id = (SELECT id FROM wp_bp_xprofile_fields WHERE `name` = %s);", $user->ID, "Country" | |
)); | |
$subscription = array( | |
'name' => '', | |
'start' => '', | |
'end' => '', | |
'id' => '' | |
); | |
$member = new Membership_Model_Member($user->ID); | |
$rels = $member->get_relationships(); | |
if (is_array($rels)) { | |
foreach ($rels as $id) { | |
$sub = new Membership_Model_Subscription($id->sub_id); | |
$subscription['name'] = $sub->sub_name(); | |
$subscription['start'] = $id->startdate; | |
$subscription['end'] = $id->expirydate; | |
$subscription['id'] = $id->sub_id; | |
break; | |
} | |
} | |
$tmp = array( | |
'user_login' => $user->user_login, | |
'user_nicename' => $user->user_nicename, | |
'user_email' => $user->user_email, | |
'First Name' => $user->first_name, | |
'Last Name' => $user->last_name, | |
'Street Address' => $street_address, | |
'Apt / Unit No.' => $apt, | |
'City' => $city, | |
'Provide' => $provide, | |
'Postal Code' => $postal, | |
'Country' => $country, | |
'Subscription ID' => $subscription['id'], | |
'Subscription' => $subscription['name'], | |
'Start Date' => $subscription['start'], | |
'Expiry Date' => $subscription['end'] | |
); | |
$data[] = $tmp; | |
} | |
//build header | |
if (!empty($data)) { | |
$headers = array_keys($data[0]); | |
$data = array_merge(array($headers), $data); | |
me_download_send_headers("data_export_" . date("Y-m-d") . ".csv"); | |
echo me_array2csv($data); | |
die; | |
} | |
} | |
} | |
function membership_users_exporter() | |
{ | |
?> | |
<div class="wrap"> | |
<form method="post"> | |
<h2><?php _e("Export Users", "me") ?></h2> | |
<button name="me_export" type="submit"><?php _e("Download CSV", "me") ?></button> | |
</form> | |
</div> | |
<?php | |
} | |
function me_array2csv(array &$array) | |
{ | |
if (count($array) == 0) { | |
return null; | |
} | |
ob_start(); | |
$df = fopen("php://output", 'w'); | |
fputcsv($df, array_keys(reset($array))); | |
foreach ($array as $row) { | |
fputcsv($df, $row); | |
} | |
fclose($df); | |
return ob_get_clean(); | |
} | |
function me_download_send_headers($filename) | |
{ | |
// disable caching | |
$now = gmdate("D, d M Y H:i:s"); | |
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT"); | |
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate"); | |
header("Last-Modified: {$now} GMT"); | |
// force download | |
header("Content-Type: application/force-download"); | |
header("Content-Type: application/octet-stream"); | |
header("Content-Type: application/download"); | |
// disposition / encoding on response body | |
header("Content-Disposition: attachment;filename={$filename}"); | |
header("Content-Transfer-Encoding: binary"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment