Advertisement
Guest User

export user data modified to include update time

a guest
Nov 6th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.83 KB | None | 0 0
  1. <?php
  2. /**
  3. * @package Export_User_Data
  4. * @version 0.7.3
  5. */
  6. /*
  7. Plugin Name: Export User Data
  8. Plugin URI: http://qstudio.us/plugins/
  9. Description: Export User data, metadata and BuddyPressX Profile data.
  10. Version: 0.7.3
  11. Author: Q Studio
  12. Author URI: http://qstudio.us/
  13. License: GPL2
  14. Text Domain: export-user-data
  15. */
  16.  
  17. /*
  18. * Based on: Export User to CSV by PubPoet ( http://pubpoet.com/ )- Thanks!
  19. */
  20.  
  21. load_plugin_textdomain( 'export-user-data', false, basename( dirname( __FILE__ ) ) . '/languages' );
  22.  
  23. /**
  24. * Main plugin class
  25. *
  26. * @since 0.1
  27. **/
  28. class Q_EUD_Export_Users {
  29.  
  30. /**
  31. * Class contructor
  32. *
  33. * @since 0.1
  34. **/
  35. public function __construct() {
  36. add_action( 'admin_menu', array( $this, 'add_admin_pages' ) );
  37. add_action( 'init', array( $this, 'generate_data' ) );
  38. add_filter( 'q_eud_exclude_data', array( $this, 'exclude_data' ) );
  39. add_action( 'admin_init', array( $this, 'add_css_and_js' ) );
  40. add_action( 'admin_footer', array( $this, 'multiselect' ), 100000 );
  41.  
  42. }
  43.  
  44.  
  45. /**
  46. * Add administration menus
  47. *
  48. * @since 0.1
  49. **/
  50. public function add_admin_pages() {
  51. add_users_page( __( 'Export User Data', 'export-user-data' ), __( 'Export User Data', 'export-user-data' ), 'list_users', 'export-user-data', array( $this, 'users_page' ) );
  52. #add_action( 'admin_footer-'. $plugin_page, 'multiselect' );
  53. }
  54.  
  55.  
  56. /* style and interaction */
  57. function add_css_and_js() {
  58. wp_register_style('q_eud_multi_select_css', plugins_url('css/multi-select.css',__FILE__ ));
  59. wp_enqueue_style('q_eud_multi_select_css');
  60. wp_enqueue_script('q_eud_multi_select_js', plugins_url('js/jquery.multi-select.js',__FILE__ ), array('jquery'), '0.9.8', false );
  61. }
  62.  
  63.  
  64. /* clean that stuff up ## */
  65. public function sanitize($value) {
  66. $value = str_replace("\r", '', $value);
  67. $value = str_replace("\n", '', $value);
  68. $value = str_replace("\t", '', $value);
  69. return $value;
  70. }
  71.  
  72.  
  73. /* activate multiselects */
  74. function multiselect() {
  75. ?>
  76. <script>
  77. // build super multiselect ##
  78. jQuery('#usermeta, #bp_fields, #bp_fields_update_time').multiSelect();
  79.  
  80. // show only common ##
  81. jQuery('.usermeta-common').click(function(e){
  82. e.preventDefault();
  83. jQuery('#ms-usermeta .ms-selectable li.system').hide();
  84. });
  85.  
  86. // show all ##
  87. jQuery('.usermeta-all').click(function(e){
  88. e.preventDefault();
  89. jQuery('#ms-usermeta .ms-selectable li').show();
  90. });
  91.  
  92. </script>
  93. <?php
  94. }
  95.  
  96. /**
  97. * Process content of CSV file
  98. *
  99. * @since 0.1
  100. **/
  101. public function generate_data() {
  102. if ( isset( $_POST['_wpnonce-q-eud-export-user-page_export'] ) ) {
  103. check_admin_referer( 'q-eud-export-user-page_export', '_wpnonce-q-eud-export-user-page_export' );
  104. // build argument array ##
  105. $args = array(
  106. 'fields' => 'all_with_meta',
  107. 'role' => stripslashes( $_POST['role'] )
  108. );
  109.  
  110. // did the user request a specific program ? ##
  111. if ( isset( $_POST['program'] ) && $_POST['program'] != '' ) {
  112.  
  113. $args['meta_key'] = 'member_of_club';
  114. $args['meta_value'] = (int)$_POST['program'];
  115. $args['meta_compare'] = '=';
  116.  
  117. }
  118.  
  119. add_action( 'pre_user_query', array( $this, 'pre_user_query' ) );
  120. $users = get_users( $args );
  121. remove_action( 'pre_user_query', array( $this, 'pre_user_query' ) );
  122.  
  123. if ( ! $users ) {
  124. $referer = add_query_arg( 'error', 'empty', wp_get_referer() );
  125. wp_redirect( $referer );
  126. exit;
  127. }
  128.  
  129. $sitename = sanitize_key( get_bloginfo( 'name' ) );
  130. if ( ! empty( $sitename ) )
  131. $sitename .= '.';
  132.  
  133. // export method ? ##
  134. $export_method = 'excel'; // default to Excel export ##
  135. if ( isset( $_POST['format'] ) && $_POST['format'] != '' ) {
  136.  
  137. $export_method = $_POST['format'];
  138.  
  139. }
  140.  
  141. // set export filename structure ##
  142. $filename = $sitename . 'users.' . date( 'Y-m-d-H-i-s' );
  143.  
  144. switch ( $export_method ) {
  145.  
  146. case "csv":
  147.  
  148. // to csv ##
  149. header( 'Content-Description: File Transfer' );
  150. header( 'Content-Disposition: attachment; filename='.$filename.'.csv' );
  151. header( 'Content-Type: text/csv; charset=' . get_option( 'blog_charset' ), true );
  152.  
  153. // how to seperate data ##
  154. $seperator = ','; // comma for csv ##
  155.  
  156. break;
  157.  
  158. case ('excel'):
  159.  
  160. // to xls ##
  161. header( 'Content-Description: File Transfer' );
  162. header("Content-Type: application/vnd.ms-excel");
  163. header("Content-Disposition: attachment; filename=$filename.xls");
  164. header("Pragma: no-cache");
  165. header("Expires: 0");
  166.  
  167. // how to seperate data ##
  168. $seperator = "\t"; //tabbed character
  169.  
  170. break;
  171.  
  172. }
  173.  
  174. // line break ##
  175. $breaker = "\n";
  176.  
  177. // function to exclude data ##
  178. $exclude_data = apply_filters( 'q_eud_exclude_data', array() );
  179.  
  180. // check for selected usermeta fields ##
  181. $usermeta = isset( $_POST['usermeta'] ) ? $_POST['usermeta']: '';
  182. $usermeta_fields = array();
  183. if ( $usermeta && is_array($usermeta) ) {
  184. foreach( $usermeta as $field ) {
  185. $usermeta_fields[] = $field;
  186. }
  187. }
  188.  
  189. // check for selected x profile fields ##
  190. $bp_fields = isset( $_POST['bp_fields'] ) ? $_POST['bp_fields'] : '';
  191. $bp_fields_passed = array();
  192. if ( $bp_fields && is_array($bp_fields) ) {
  193. foreach( $bp_fields as $field ) {
  194.  
  195. // reverse tidy ##
  196. $field = str_replace( '__', ' ', $field );
  197.  
  198. // add to array ##
  199. $bp_fields_passed[] = $field;
  200.  
  201. }
  202. }
  203. // cwjordan: check for x profile fields we want update time for
  204. $bp_fields_update = isset( $_POST['bp_fields_update_time'] ) ? $_POST['bp_fields_update_time'] : '';
  205. $bp_fields_update_passed = array();
  206. if ( $bp_fields_update && is_array($bp_fields_update ) ) {
  207. foreach( $bp_fields_update as $field ) {
  208. // reverse tidy ##
  209. $field = str_replace( '__', ' ', $field );
  210. // add to array ##
  211. $bp_fields_update_passed[] = $field . " Update Date";
  212. }
  213. }
  214. // global wpdb object ##
  215. global $wpdb;
  216.  
  217. // exportable user data ##
  218. $data_keys = array(
  219. 'ID', 'user_login', 'user_pass',
  220. 'user_nicename', 'user_email', 'user_url',
  221. 'user_registered', /*'user_activation_key',*/ /*'user_status',*/
  222. 'display_name'
  223. );
  224.  
  225. // compile final fields list ##
  226. $fields = array_merge( $data_keys, $usermeta_fields, $bp_fields_passed, $bp_fields_update_passed );
  227.  
  228. // build the document headers ##
  229. $headers = array();
  230. foreach ( $fields as $key => $field ) {
  231.  
  232. // rename programs field ##
  233. if ( $field == 'member_of_club' ){
  234. $field = 'Program';
  235. }
  236.  
  237. if ( in_array( $field, $exclude_data ) )
  238. unset( $fields[$key] );
  239. else
  240. $headers[] = '"' . $field . '"';
  241.  
  242. }
  243.  
  244. // echo headers ##
  245. echo implode( $seperator, $headers ) . $breaker;
  246.  
  247. // build row values for each user ##
  248. foreach ( $users as $user ) {
  249.  
  250. $data = array();
  251. foreach ( $fields as $field ) {
  252.  
  253. // BP loaded ? ##
  254. if ( function_exists ('bp_is_active') ) {
  255. $bp_data = BP_XProfile_ProfileData::get_all_for_user($user->ID);
  256. }
  257.  
  258. // check if this is a BP field ##
  259. if ( in_array( $field, $bp_fields_passed ) ) {
  260.  
  261. $value = $bp_data[$field];
  262.  
  263. if (is_array($value)) {
  264. $value = $value['field_data'];
  265. }
  266. $value = $this->sanitize($value);
  267. // check if this is a BP field we want update date for ##
  268. } elseif ( in_array( $field, $bp_fields_update_passed ) ) {
  269. global $bp;
  270. $real_field = str_replace(" Update Date", "", $field);
  271. $field_id = xprofile_get_field_id_from_name( $real_field );
  272. $value = $wpdb->get_var ($wpdb->prepare( "SELECT last_updated FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = %d", $user->ID, $field_id ) );
  273. // user data or usermeta ##
  274. } else {
  275.  
  276. $value = isset( $user->{$field} ) ? $user->{$field} : '';
  277. $value = is_array( $value ) ? serialize( $value ) : $value;
  278.  
  279. }
  280.  
  281. // correct program value to Program Name ##
  282. if ( $field == 'member_of_club' ){
  283. $value = get_the_title($value);
  284. }
  285.  
  286. $data[] = '"' . str_replace( '"', '""', $value ) . '"';
  287.  
  288. }
  289.  
  290. // echo row data ##
  291. echo implode( $seperator, $data ) . $breaker;
  292. }
  293.  
  294. // stop PHP, so file can export correctly ##
  295. exit;
  296. }
  297. }
  298.  
  299. /**
  300. * Content of the settings page
  301. *
  302. * @since 0.1
  303. **/
  304. public function users_page() {
  305. if ( ! current_user_can( 'list_users' ) ) {
  306. wp_die( __( 'You do not have sufficient permissions to access this page.', 'export-user-data' ) );
  307. }
  308. ?>
  309.  
  310. <div class="wrap">
  311. <h2><?php _e( 'Export User Data', 'export-user-data' ); ?></h2>
  312. <?php
  313.  
  314. // nothing happening? ##
  315. if ( isset( $_GET['error'] ) ) {
  316. echo '<div class="updated"><p><strong>' . __( 'No users found.', 'export-user-data' ) . '</strong></p></div>';
  317. }
  318.  
  319. ?>
  320. <form method="post" action="" enctype="multipart/form-data">
  321. <?php wp_nonce_field( 'q-eud-export-user-page_export', '_wpnonce-q-eud-export-user-page_export' ); ?>
  322. <table class="form-table">
  323. <?php
  324.  
  325. // allow admin to select user meta fields to export ##
  326. global $wpdb;
  327. $meta_keys = $wpdb->get_results( "SELECT distinct(meta_key) FROM $wpdb->usermeta" );
  328.  
  329. // get meta_key value from object ##
  330. $meta_keys = wp_list_pluck( $meta_keys, 'meta_key' );
  331.  
  332. // let's note some of them odd keys ##
  333. $meta_keys_system = array(
  334. 'metaboxhidden',
  335. 'activation',
  336. 'bp_',
  337. 'nav_',
  338. 'wp_',
  339. 'admin_color',
  340. 'wpmudev',
  341. 'screen_',
  342. 'show_',
  343. 'rich_',
  344. 'reward_',
  345. 'meta-box',
  346. 'manageedit',
  347. 'edit_',
  348. 'closedpostboxes_',
  349. 'dismissed_',
  350. 'manage',
  351. 'comment',
  352. 'current',
  353. 'incentive_',
  354. '_wdp',
  355. 'ssl',
  356. 'wdfb',
  357. 'users_per_page',
  358. );
  359.  
  360. // allow array to be filtered ##
  361. $meta_keys_system = apply_filters( 'export_user_data_meta_keys_system', $meta_keys_system );
  362.  
  363. /*
  364. foreach ( $meta_keys as $key ) {
  365.  
  366. foreach ( $meta_keys_system as $drop ) {
  367.  
  368. if ( strpos( $key, $drop ) !== false ) {
  369.  
  370. #echo 'FOUND -- '.$drop.' in '.$key.'<br />';
  371.  
  372. if(($key = array_search($key, $meta_keys)) !== false) {
  373. unset($meta_keys[$key]);
  374. }
  375.  
  376. }
  377.  
  378. }
  379.  
  380. }
  381. */
  382.  
  383. // test array ##
  384. #echo '<pre>'; var_dump($meta_keys); echo '</pre>';
  385.  
  386. // check if we got anything ? ##
  387. if ( $meta_keys ) {
  388.  
  389. ?>
  390. <tr valign="top">
  391. <th scope="row">
  392. <label for="q_eud_usermeta"><?php _e( 'User Meta Fields', 'export-user-data' ); ?></label>
  393. <p class="filter" style="margin: 10px 0 0;">
  394. <?php _e('Filter', 'export-user-data'); ?>: <a href="#" class="usermeta-all"><?php _e('All', 'export-user-data'); ?></a> | <a href="#" class="usermeta-common"><?php _e('Common', 'export-user-data'); ?></a>
  395. </p>
  396. </th>
  397. <td>
  398. <select multiple="multiple" id="usermeta" name="usermeta[]">
  399. <?php
  400.  
  401. foreach ( $meta_keys as $key ) {
  402.  
  403. #echo "\n\t<option value='" . esc_attr( $role ) . "'>$name</option>";
  404.  
  405. // display $key ##
  406. $display_key = $key;
  407.  
  408. // rename programs field ##
  409. if ( $display_key == 'member_of_club' ){
  410. $display_key = 'program';
  411. }
  412.  
  413. // tidy ##
  414. $display_key = str_replace( "_", " ", ucwords($display_key) );
  415.  
  416. #echo "<label for='".esc_attr( $key )."' title='".esc_attr( $key )."'><input id='".esc_attr( $key )."' type='checkbox' name='usermeta[]' value='".esc_attr( $key )."'/> $display_key</label><br />";
  417.  
  418. // class ##
  419. $usermeta_class = 'normal';
  420.  
  421. foreach ( $meta_keys_system as $drop ) {
  422.  
  423. if ( strpos( $key, $drop ) !== false ) {
  424.  
  425. #echo 'FOUND -- '.$drop.' in '.$key.'<br />';
  426.  
  427. if(($key = array_search($key, $meta_keys)) !== false) {
  428.  
  429. $usermeta_class = 'system';
  430.  
  431. }
  432.  
  433. }
  434.  
  435. }
  436.  
  437. // print key ##
  438. echo "<option value='".esc_attr( $key )."' title='".esc_attr( $key )."' class='".$usermeta_class."'>$display_key</option>";
  439.  
  440. }
  441. ?>
  442. </select>
  443. </td>
  444. </tr>
  445. <?php
  446.  
  447. } // meta_keys found ##
  448.  
  449. ?>
  450. <?php
  451.  
  452. // buddypress x profile data ##
  453. if ( function_exists ('bp_is_active') ) {
  454.  
  455. // grab all buddypress x profile fields ##
  456. $bp_fields = $wpdb->get_results( "SELECT distinct(name) FROM ".$wpdb->base_prefix."bp_xprofile_fields WHERE parent_id = 0" );
  457.  
  458. // get name value from object ##
  459. $bp_fields = wp_list_pluck( $bp_fields, 'name' );
  460.  
  461. // test array ##
  462. #echo '<pre>'; var_dump($bp_fields); echo '</pre>';
  463.  
  464. // allow array to be filtered ##
  465. $bp_fields = apply_filters( 'export_user_data_bp_fields', $bp_fields );
  466.  
  467. ?>
  468. <tr valign="top">
  469. <th scope="row"><label for="q_eud_xprofile"><?php _e( 'BP xProfile Fields', 'export-user-data' ); ?></label></th>
  470. <td>
  471. <select multiple="multiple" id="bp_fields" name="bp_fields[]">
  472. <?php
  473.  
  474. foreach ( $bp_fields as $key ) {
  475.  
  476. // tidy up key ##
  477. $key_tidy = str_replace( ' ', '__', ($key));
  478.  
  479. #echo "<label for='".esc_attr( $key_tidy )."'><input id='".esc_attr( $key_tidy )."' type='checkbox' name='bp_fields[]' value='".esc_attr( $key_tidy )."'/> $key</label><br />";
  480.  
  481. // print key ##
  482. echo "<option value='".esc_attr( $key )."' title='".esc_attr( $key )."'>$key</option>";
  483.  
  484. }
  485. ?>
  486. </select>
  487. </td>
  488. </tr>
  489. <?php
  490. //cwjordan: allow export of update times
  491. ?>
  492. <tr valign="top">
  493. <th scope="row"><label for="q_eud_xprofile"><?php _e( 'BP xProfile Fields Update Time', 'export-user-data' ); ?></label</th>
  494. <td>
  495. <select multiple="multiple" id="bp_fields_update_time" name="bp_fields_update_time[]">
  496. <?php
  497. foreach ( $bp_fields as $key ) {
  498. echo "<option value='".esc_attr( $key )."' title='".esc_attr( $key )."'>$key</option>";
  499.  
  500. }
  501. ?>
  502. </select>
  503. </td>
  504. </tr>
  505.  
  506. <?php
  507.  
  508. } // BP installed and active ##
  509.  
  510. ?>
  511. <tr valign="top">
  512. <th scope="row"><label for="q_eud_users_role"><?php _e( 'Role', 'export-user-data' ); ?></label></th>
  513. <td>
  514. <select name="role" id="q_eud_users_role">
  515. <?php
  516.  
  517. echo '<option value="">' . __( 'All Roles', 'export-user-data' ) . '</option>';
  518. global $wp_roles;
  519. foreach ( $wp_roles->role_names as $role => $name ) {
  520. echo "\n\t<option value='" . esc_attr( $role ) . "'>$name</option>";
  521. }
  522.  
  523. ?>
  524. </select>
  525. </td>
  526. </tr>
  527. <?php
  528.  
  529. // clubs ? ##
  530. if ( post_type_exists( 'club' ) ) {
  531.  
  532. ?>
  533. <tr valign="top">
  534. <th scope="row"><label for="q_eud_users_program"><?php _e( 'Programs', 'export-user-data' ); ?></label></th>
  535. <td>
  536. <select name="program" id="q_eud_users_program">
  537. <?php
  538.  
  539. echo '<option value="">' . __( 'All Programs', 'export-user-data' ) . '</option>';
  540.  
  541. $clubs_array = get_posts(array( 'post_type'=> 'club', 'posts_per_page' => -1 )); // grab all posts of type "club" ##
  542.  
  543. foreach ( $clubs_array as $c ) { // loop over all clubs ##
  544.  
  545. #$clubs[$c->ID] = $c; // grab club ID ##
  546. echo "\n\t<option value='" . esc_attr( $c->ID ) . "'>$c->post_title</option>";
  547.  
  548. }
  549.  
  550. ?>
  551. </select>
  552. </td>
  553. </tr>
  554. <?php
  555.  
  556. } // clubs ##
  557.  
  558. ?>
  559. <tr valign="top">
  560. <th scope="row"><label><?php _e( 'Registered', 'export-user-data' ); ?></label></th>
  561. <td>
  562. <select name="start_date" id="q_eud_users_start_date">
  563. <option value="0"><?php _e( 'Start Date', 'export-user-data' ); ?></option>
  564. <?php $this->export_date_options(); ?>
  565. </select>
  566. <select name="end_date" id="q_eud_users_end_date">
  567. <option value="0"><?php _e( 'End Date', 'export-user-data' ); ?></option>
  568. <?php $this->export_date_options(); ?>
  569. </select>
  570. </td>
  571. </tr>
  572.  
  573. <tr valign="top">
  574. <th scope="row"><label for="q_eud_users_format"><?php _e( 'Format', 'export-user-data' ); ?></label></th>
  575. <td>
  576. <select name="format" id="q_eud_users_format">
  577. <?php
  578.  
  579. echo '<option value="excel">' . __( 'Excel', 'export-user-data' ) . '</option>';
  580. echo '<option value="csv">' . __( 'CSV', 'export-user-data' ) . '</option>';
  581.  
  582. ?>
  583. </select>
  584. </td>
  585. </tr>
  586. </table>
  587. <p class="submit">
  588. <input type="hidden" name="_wp_http_referer" value="<?php echo $_SERVER['REQUEST_URI'] ?>" />
  589. <input type="submit" class="button-primary" value="<?php _e( 'Export', 'export-user-data' ); ?>" />
  590. </p>
  591. </form>
  592. <?php
  593. }
  594.  
  595. // data to exclude from export ##
  596. public function exclude_data() {
  597. $exclude = array( 'user_pass', 'user_activation_key' );
  598. return $exclude;
  599. }
  600.  
  601. public function pre_user_query( $user_search ) {
  602. global $wpdb;
  603.  
  604. $where = '';
  605.  
  606. if ( ! empty( $_POST['start_date'] ) )
  607. $where .= $wpdb->prepare( " AND $wpdb->users.user_registered >= %s", date( 'Y-m-d', strtotime( $_POST['start_date'] ) ) );
  608.  
  609. if ( ! empty( $_POST['end_date'] ) )
  610. $where .= $wpdb->prepare( " AND $wpdb->users.user_registered < %s", date( 'Y-m-d', strtotime( '+1 month', strtotime( $_POST['end_date'] ) ) ) );
  611.  
  612. if ( ! empty( $where ) )
  613. $user_search->query_where = str_replace( 'WHERE 1=1', "WHERE 1=1 $where", $user_search->query_where );
  614.  
  615. return $user_search;
  616. }
  617.  
  618. private function export_date_options() {
  619. global $wpdb, $wp_locale;
  620.  
  621. $months = $wpdb->get_results( "
  622. SELECT DISTINCT YEAR( user_registered ) AS year, MONTH( user_registered ) AS month
  623. FROM $wpdb->users
  624. ORDER BY user_registered DESC
  625. " );
  626.  
  627. $month_count = count( $months );
  628. if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
  629. return;
  630.  
  631. foreach ( $months as $date ) {
  632. if ( 0 == $date->year )
  633. continue;
  634.  
  635. $month = zeroise( $date->month, 2 );
  636. echo '<option value="' . $date->year . '-' . $month . '">' . $wp_locale->get_month( $month ) . ' ' . $date->year . '</option>';
  637. }
  638. }
  639. }
  640.  
  641. new Q_EUD_Export_Users;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement