| 1 |
<?php |
| 2 |
/** |
| 3 |
* The WP_Members Export Class. |
| 4 |
* |
| 5 |
* @package WP-Members |
| 6 |
* @subpackage WP_Members Export Object Class |
| 7 |
* @since 3.3.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit(); |
| 13 |
} |
| 14 |
|
| 15 |
class WP_Members_User_Export { |
| 16 |
|
| 17 |
/** |
| 18 |
* Used instead of getting global. |
| 19 |
* |
| 20 |
* @since 3.4.1 |
| 21 |
* @todo May change how this is used. (Currently just replaces minor use of $wpmem global object for this one thing.) |
| 22 |
*/ |
| 23 |
public static $membership_product_stem = "_wpmem_products_"; |
| 24 |
|
| 25 |
/** |
| 26 |
* New export function to export all or selected users |
| 27 |
* |
| 28 |
* @since 2.9.7 |
| 29 |
* @since 3.2.0 Updated to use fputcsv. |
| 30 |
* @since 3.2.1 Added user data filters. |
| 31 |
* @since 3.3.0 Moved to new object class as static method. |
| 32 |
* @since 3.4.0 Added $tag to identify what export process is being run. |
| 33 |
* |
| 34 |
* @param array $args array { |
| 35 |
* Array of defaults for export. |
| 36 |
* |
| 37 |
* @type string $export The type of export (all|selected) |
| 38 |
* @type string $filename |
| 39 |
* @type array $fields { |
| 40 |
* The array of export fields is keyed as 'meta_key' => 'heading value'. |
| 41 |
* The array can include fields in the Fields tab, plus the following: |
| 42 |
* |
| 43 |
* @type int $ID ID from wp_users |
| 44 |
* @type string $username user_login from wp_users |
| 45 |
* @type string $user_nicename user_nicename |
| 46 |
* @type string $user_url user_url |
| 47 |
* @type string $display_name display_name |
| 48 |
* @type int $active Whether the user is active/deactivated. |
| 49 |
* @type string $exp_type If the PayPal extension is installed pending|subscrption (optional) |
| 50 |
* @type string $expires If the PayPal extension is installed MM/DD/YYYY (optional) |
| 51 |
* @type string $user_registered user_registered |
| 52 |
* @type string $user_ip The IP of the user when they registered. |
| 53 |
* @type string $role The user's role (or roles, if multiple). |
| 54 |
* } |
| 55 |
* @type array $exclude_fields @deprecated 3.4.0 |
| 56 |
* @type boolean $entity_decode Whether HTML entities should be decoded (default: false) |
| 57 |
* @type string $date_format A PHP readable date format (default: Y-m-d which results in YYYY-MM-DD) |
| 58 |
* @type string $required_caps The user capability required to export. |
| 59 |
* } |
| 60 |
* @param array $users Array of user IDs to export. |
| 61 |
* @param string $tag |
| 62 |
*/ |
| 63 |
public static function export_users( $args = array(), $users = array(), $tag = 'default' ) { |
| 64 |
|
| 65 |
$export_fields = ( ! isset( $args['fields'] ) ) ? self::get_export_fields() : $args['fields']; |
| 66 |
|
| 67 |
/** |
| 68 |
* Filter the export fields. |
| 69 |
* |
| 70 |
* @since 3.2.5 |
| 71 |
* @since 3.4.0 Added $tag. |
| 72 |
* |
| 73 |
* @param array $export_fields { |
| 74 |
* The array of export fields is keyed as 'meta_key' => 'heading value'. |
| 75 |
* The array will include all fields in the Fields tab, plus the following: |
| 76 |
* |
| 77 |
* @type int $ID ID from wp_users |
| 78 |
* @type string $username user_login from wp_users |
| 79 |
* @type string $user_nicename user_nicename |
| 80 |
* @type string $user_url user_url |
| 81 |
* @type string $display_name display_name |
| 82 |
* @type int $active Whether the user is active/deactivated. |
| 83 |
* @type string $exp_type If the PayPal extension is installed pending|subscrption (optional) |
| 84 |
* @type string $expires If the PayPal extension is installed MM/DD/YYYY (optional) |
| 85 |
* @type string $user_registered user_registered |
| 86 |
* @type string $user_ip The IP of the user when they registered. |
| 87 |
* @type string $role The user's role (or roles, if multiple). |
| 88 |
* } |
| 89 |
* @param string $tag |
| 90 |
*/ |
| 91 |
$export_fields = apply_filters( 'wpmem_export_fields', $export_fields, $tag ); |
| 92 |
|
| 93 |
$today = date( "Y-m-d" ); |
| 94 |
|
| 95 |
// Setup defaults. |
| 96 |
$defaults = array( |
| 97 |
'export' => 'all', |
| 98 |
'filename' => 'wp-members-user-export-' . $today . '.csv', |
| 99 |
'fields' => $export_fields, |
| 100 |
'entity_decode' => false, |
| 101 |
'date_format' => 'Y-m-d', |
| 102 |
'required_caps' => 'list_users', |
| 103 |
'separator' => ',', |
| 104 |
'enclosure' => '"', |
| 105 |
); |
| 106 |
// Merge args with default (in case any were missing). |
| 107 |
$args = wp_parse_args( $args, $defaults ); |
| 108 |
|
| 109 |
/** |
| 110 |
* Filter the default export arguments. |
| 111 |
* |
| 112 |
* @since 2.9.7 |
| 113 |
* @since 3.4.0 Filter all defaults (like other _args changes), then wp_parse_args() in case any are missing. |
| 114 |
* @since 3.4.0 Added $tag. |
| 115 |
* @since 3.4.0 Deprecated 'exclude_fields' (unset using wpmem_export_fields instead). |
| 116 |
* @since 3.4.0 Deprecated 'export_fields' use "fields" instead. |
| 117 |
* @since 3.5.0 Added $separator and $enclosure |
| 118 |
* |
| 119 |
* @param array $args { |
| 120 |
* Array of defaults for export. |
| 121 |
* |
| 122 |
* @type string $export |
| 123 |
* @type string $filename |
| 124 |
* @type array $fields |
| 125 |
* @type array $exclude_fields @deprecated 3.4.0 |
| 126 |
* @type boolean $entity_decode |
| 127 |
* @type string $date_format |
| 128 |
* @type string $required_caps |
| 129 |
* @type string $separator default: , |
| 130 |
* @type string $enclosure default: " |
| 131 |
* } |
| 132 |
* @param string $tag |
| 133 |
*/ |
| 134 |
$args = apply_filters( 'wpmem_export_args', $args, $tag ); |
| 135 |
|
| 136 |
// Merge args with default (in case any were missing). |
| 137 |
$args = wp_parse_args( $args, $defaults ); |
| 138 |
|
| 139 |
if ( current_user_can( $args['required_caps'] ) ) { |
| 140 |
// Output needs to be buffered, start the buffer. |
| 141 |
ob_start(); |
| 142 |
|
| 143 |
// If exporting all, get all of the users. |
| 144 |
$export_users = ( 'all' == $args['export'] ) ? get_users( array( 'fields' => 'ID' ) ) : $users; |
| 145 |
|
| 146 |
// Generate headers and a filename based on date of export. |
| 147 |
header( "Content-Description: File Transfer" ); |
| 148 |
header( "Content-type: application/octet-stream" ); |
| 149 |
header( "Content-Disposition: attachment; filename=" . $args['filename'] ); |
| 150 |
header( "Content-Type: text/csv; charset=" . get_option( 'blog_charset' ), true ); |
| 151 |
|
| 152 |
$handle = fopen( 'php://output', 'w' ); |
| 153 |
fputs( $handle, "\xEF\xBB\xBF" ); // UTF-8 BOM |
| 154 |
|
| 155 |
// Remove excluded fields from $export_fields while setting up $header array. |
| 156 |
$header = array(); |
| 157 |
foreach ( $args['fields'] as $meta => $field ) { |
| 158 |
$header[ $meta ] = $field; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Filters user export header row before assembly. |
| 163 |
* |
| 164 |
* As of 3.4.0, this really isn't a necessary filter. You can specify the header |
| 165 |
* value in wpmem_export_fields instead and just use one filter. |
| 166 |
* |
| 167 |
* @since 3.2.1 |
| 168 |
* @since 3.4.0 Added $tag. |
| 169 |
* |
| 170 |
* @param array $header The header column values |
| 171 |
* @param string $tag |
| 172 |
*/ |
| 173 |
$header = apply_filters( 'wpmem_user_export_header', $header, $tag ); |
| 174 |
|
| 175 |
fputcsv( $handle, $header, $args['separator'], $args['enclosure'] ); |
| 176 |
|
| 177 |
// Loop through the array of users, assemble csv. |
| 178 |
// $fields only includes fields to be exported at this point. |
| 179 |
foreach ( $export_users as $user ) { |
| 180 |
|
| 181 |
$user_info = get_userdata( $user ); |
| 182 |
|
| 183 |
$wp_user_fields = array( 'ID', 'user_login', 'user_pass', 'user_nicename', 'user_email', 'user_url', 'user_registered', 'user_activation_key', 'user_status', 'display_name' ); |
| 184 |
foreach ( $args['fields'] as $meta => $field ) { |
| 185 |
|
| 186 |
switch ( $meta ) { |
| 187 |
case 'ID': |
| 188 |
case 'user_login': |
| 189 |
case 'user_pass': |
| 190 |
case 'user_nicename': |
| 191 |
case 'user_email': |
| 192 |
case 'user_url': |
| 193 |
case 'user_registered': |
| 194 |
case 'user_activation_key': |
| 195 |
case 'user_status': |
| 196 |
case 'display_name': |
| 197 |
$row[ $meta ] = $user_info->{$meta}; |
| 198 |
break; |
| 199 |
case 'username': |
| 200 |
$row['username'] = $user_info->user_login; |
| 201 |
break; |
| 202 |
case 'password': |
| 203 |
$row['password'] = $user_info->user_pass; |
| 204 |
break; |
| 205 |
case 'active': |
| 206 |
$row['active'] = wpmem_get_user_meta( $user, 'active' ) ? esc_html__( 'Yes' ) : esc_html__( 'No' ); |
| 207 |
break; |
| 208 |
case 'exp_type': |
| 209 |
$exp_type = wpmem_get_user_meta( $user, 'exp_type' ); |
| 210 |
$row['exp_type'] = ( false !== $exp_type ) ? $exp_type : ''; |
| 211 |
break; |
| 212 |
case 'expires': |
| 213 |
$expires = wpmem_get_user_meta( $user, 'expires' ); |
| 214 |
$row['expires'] = ( false !== $expires ) ? $expires : ''; |
| 215 |
break; |
| 216 |
case 'wpmem_reg_ip': |
| 217 |
$reg_ip = wpmem_get_user_meta( $user, 'wpmem_reg_ip' ); |
| 218 |
$row['wpmem_reg_ip'] = ( false !== $reg_ip ) ? $reg_ip : ''; |
| 219 |
break; |
| 220 |
case 'role': |
| 221 |
$role = wpmem_get_user_role( $user, true ); // As of 3.4, wpmem_get_user_role() can get all roles. |
| 222 |
$row['role'] = ( is_array( $role ) ) ? implode( ",", $role ) : $role; |
| 223 |
break; |
| 224 |
case ( self::$membership_product_stem === substr( $meta, 0, strlen( self::$membership_product_stem ) ) ): |
| 225 |
$product = str_replace( self::$membership_product_stem, '', $meta ); |
| 226 |
$row[ $meta ] = wpmem_get_user_meta( $user, $meta ); |
| 227 |
// If value is a date and false is not the format_date option... |
| 228 |
if ( false !== $args['date_format'] && '' != $row[ $meta ] && $row[ $meta ] > 2 ) { |
| 229 |
$date_format = ( 'wp' == $args['date_format'] ) ? get_option('date_format') : $args['date_format']; |
| 230 |
$row[ $meta ] = date( $date_format, $row[ $meta ] ); |
| 231 |
} |
| 232 |
break; |
| 233 |
default: |
| 234 |
if ( in_array( $meta, $wp_user_fields ) ) { |
| 235 |
$row[ $meta ] = ( 'username' == $meta ) ? $user_info->user_login : $user_info->{$meta}; |
| 236 |
} else { |
| 237 |
$raw_data = wpmem_get_user_meta( $user, $meta ); |
| 238 |
$raw_data = ( $raw_data ) ? $raw_data : ''; |
| 239 |
$row[ $meta ] = ( $args['entity_decode'] ) ? html_entity_decode( $raw_data ) : $raw_data; |
| 240 |
} |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Filter the user data before assembly. |
| 247 |
* |
| 248 |
* @since 3.2.1 |
| 249 |
* @since 3.4.0 Added user ID (it may not be included in the $row array if the field were filtered out). |
| 250 |
* @since 3.4.0 Added $tag. |
| 251 |
* |
| 252 |
* @param array $row The user data row. |
| 253 |
* @param int $user_id The user ID. |
| 254 |
* @param string $tag |
| 255 |
*/ |
| 256 |
$row = apply_filters( 'wpmem_user_export_row', $row, $user_info->ID, $tag ); |
| 257 |
|
| 258 |
fputcsv( $handle, $row, $args['separator'], $args['enclosure'] ); |
| 259 |
|
| 260 |
// Update the user record as being exported. |
| 261 |
if ( 'all' != $args['export'] ) { |
| 262 |
update_user_meta( $user, 'exported', 1 ); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
fclose( $handle ); |
| 267 |
print( ob_get_clean() ); |
| 268 |
|
| 269 |
exit(); |
| 270 |
} else { |
| 271 |
wp_die( esc_html__( 'You do not have the required user capabilities to export users.', 'wp-members' ) ); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
private static function get_export_fields() { |
| 276 |
|
| 277 |
$wpmem_fields = wpmem_fields(); |
| 278 |
|
| 279 |
// Fields to exclude. |
| 280 |
$exclude_fields = array( 'user_pass', 'password', 'confirm_password', 'confirm_email' ); |
| 281 |
|
| 282 |
// Prepare fields, add additional "special" fields. |
| 283 |
$export_fields = array( |
| 284 |
'ID' => esc_html__( 'User ID', 'wp-members' ), |
| 285 |
); |
| 286 |
foreach( $wpmem_fields as $meta_key => $value ) { |
| 287 |
if ( ! in_array( $meta_key, $exclude_fields ) ) { |
| 288 |
$export_fields[ $meta_key ] = $value['label']; |
| 289 |
} |
| 290 |
} |
| 291 |
$export_fields['username'] = esc_html__( 'Username', 'wp-members' ); |
| 292 |
if ( wpmem_is_enabled( 'mod_reg' ) ) { |
| 293 |
$export_fields['active'] = esc_html__( 'Activated?', 'wp-members' ); |
| 294 |
} |
| 295 |
if ( wpmem_is_enabled( 'act_link' ) ) { |
| 296 |
$export_fields['_wpmem_user_confirmed'] = esc_html__( 'Confirmed?', 'wp-members' ); |
| 297 |
} |
| 298 |
if ( wpmem_is_exp_enabled() && wpmem_is_enabled( 'use_exp' ) ) { |
| 299 |
$export_fields['exp_type'] = esc_html__( 'Subscription', 'wp-members' ); |
| 300 |
$export_fields['expires'] = esc_html__( 'Expires', 'wp-members' ); |
| 301 |
} |
| 302 |
$export_fields['user_registered'] = esc_html__( 'Registered', 'wp-members' ); |
| 303 |
$export_fields['wpmem_reg_ip'] = esc_html__( 'IP', 'wp-members' ); |
| 304 |
$export_fields['role'] = esc_html__( 'Role', 'wp-members' ); |
| 305 |
if ( wpmem_is_enabled( 'enable_products' ) ) { |
| 306 |
$membership_products = wpmem_get_memberships(); |
| 307 |
// Don't bother if it's empty (i.e. memberships enabled, but none created). |
| 308 |
if ( $membership_products && ! empty( $membership_products ) ) { |
| 309 |
foreach ( $membership_products as $product_key => $product ) { |
| 310 |
$export_fields[ self::$membership_product_stem . $product_key ] = $membership_products[ $product_key ]['title']; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
return $export_fields; |
| 316 |
} |
| 317 |
} |