| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter\Mapper; |
| 4 |
|
| 5 |
use ImportWP\Common\Exporter\MapperInterface; |
| 6 |
|
| 7 |
class UserMapper extends AbstractMapper implements MapperInterface |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var \WP_User_Query |
| 11 |
*/ |
| 12 |
private $query; |
| 13 |
|
| 14 |
private function get_core_fields() |
| 15 |
{ |
| 16 |
return array( |
| 17 |
'ID', |
| 18 |
'user_login', |
| 19 |
'user_nicename', |
| 20 |
'user_email', |
| 21 |
'user_url', |
| 22 |
'user_registered', |
| 23 |
'user_status', |
| 24 |
'display_name', |
| 25 |
'first_name', |
| 26 |
'last_name' |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
public function get_fields() |
| 31 |
{ |
| 32 |
/** |
| 33 |
* @var \WPDB |
| 34 |
*/ |
| 35 |
global $wpdb; |
| 36 |
|
| 37 |
$fields = [ |
| 38 |
'key' => 'main', |
| 39 |
'label' => 'User', |
| 40 |
'loop' => true, |
| 41 |
'fields' => [], |
| 42 |
'children' => [ |
| 43 |
'custom_fields' => [ |
| 44 |
'key' => 'custom_fields', |
| 45 |
'label' => 'Custom Fields', |
| 46 |
'loop' => true, |
| 47 |
'loop_fields' => ['meta_key', 'meta_value'], |
| 48 |
'fields' => [], |
| 49 |
'children' => [] |
| 50 |
] |
| 51 |
] |
| 52 |
|
| 53 |
]; |
| 54 |
|
| 55 |
$fields['fields'] = $this->get_core_fields(); |
| 56 |
$fields['fields'][] = 'role'; |
| 57 |
$fields['fields'][] = 'first_name'; |
| 58 |
$fields['fields'][] = 'last_name'; |
| 59 |
$fields['fields'][] = 'description'; |
| 60 |
|
| 61 |
// user meta |
| 62 |
$meta_fields = $wpdb->get_col("SELECT DISTINCT meta_key FROM " . $wpdb->usermeta . " WHERE user_id IN (SELECT DISTINCT ID FROM " . $wpdb->users . " )"); |
| 63 |
foreach ($meta_fields as $field) { |
| 64 |
|
| 65 |
if (in_array($field, ['first_name', 'last_name', 'description'])) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
$fields['children']['custom_fields']['fields'][] = $field; |
| 70 |
} |
| 71 |
|
| 72 |
$fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/user/custom_field_list', $fields['children']['custom_fields']['fields'], null); |
| 73 |
|
| 74 |
return $fields; |
| 75 |
} |
| 76 |
|
| 77 |
public function have_records($exporter_id) |
| 78 |
{ |
| 79 |
$query_args = []; |
| 80 |
$query_args = apply_filters('iwp/exporter/user_query', $query_args); |
| 81 |
$query_args = apply_filters(sprintf('iwp/exporter/%d/user_query', $exporter_id), $query_args); |
| 82 |
|
| 83 |
$query_args = wp_parse_args($query_args, [ |
| 84 |
'number' => -1 |
| 85 |
]); |
| 86 |
|
| 87 |
$this->query = new \WP_User_Query($query_args); |
| 88 |
|
| 89 |
return $this->found_records() > 0; |
| 90 |
} |
| 91 |
|
| 92 |
public function found_records() |
| 93 |
{ |
| 94 |
return $this->query->get_total(); |
| 95 |
} |
| 96 |
|
| 97 |
public function setup($i) |
| 98 |
{ |
| 99 |
$user = $this->query->results[$i]; |
| 100 |
$this->record = (array)$user->data; |
| 101 |
$this->record['custom_fields'] = get_user_meta($this->record['ID']); |
| 102 |
|
| 103 |
$userdata = get_userdata($this->record['ID']); |
| 104 |
$this->record['role'] = (array)$userdata->roles; |
| 105 |
|
| 106 |
foreach (['first_name', 'last_name', 'description'] as $field) { |
| 107 |
|
| 108 |
if (isset($this->record['custom_fields'][$field])) { |
| 109 |
|
| 110 |
$this->record[$field] = $this->record['custom_fields'][$field][0]; |
| 111 |
unset($this->record['custom_fields'][$field]); |
| 112 |
} else { |
| 113 |
|
| 114 |
$this->record[$field] = ''; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return true; |
| 119 |
} |
| 120 |
} |
| 121 |
|