| 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 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public function get_fields() |
| 29 |
{ |
| 30 |
/** |
| 31 |
* @var \WPDB |
| 32 |
*/ |
| 33 |
global $wpdb; |
| 34 |
|
| 35 |
$fields = [ |
| 36 |
'key' => 'main', |
| 37 |
'label' => 'User', |
| 38 |
'loop' => true, |
| 39 |
'fields' => [], |
| 40 |
'children' => [ |
| 41 |
'custom_fields' => [ |
| 42 |
'key' => 'custom_fields', |
| 43 |
'label' => 'Custom Fields', |
| 44 |
'loop' => true, |
| 45 |
'loop_fields' => ['meta_key', 'meta_value'], |
| 46 |
'fields' => [], |
| 47 |
'children' => [] |
| 48 |
] |
| 49 |
] |
| 50 |
|
| 51 |
]; |
| 52 |
|
| 53 |
$fields['fields'] = $this->get_core_fields(); |
| 54 |
|
| 55 |
|
| 56 |
// user meta |
| 57 |
$meta_fields = $wpdb->get_col("SELECT DISTINCT meta_key FROM " . $wpdb->usermeta . " WHERE user_id IN (SELECT DISTINCT ID FROM " . $wpdb->users . " )"); |
| 58 |
foreach ($meta_fields as $field) { |
| 59 |
$fields['children']['custom_fields']['fields'][] = $field; |
| 60 |
} |
| 61 |
|
| 62 |
$fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/user/custom_field_list', $fields['children']['custom_fields']['fields'], null); |
| 63 |
|
| 64 |
return $fields; |
| 65 |
} |
| 66 |
|
| 67 |
public function have_records() |
| 68 |
{ |
| 69 |
$this->query = new \WP_User_Query(array( |
| 70 |
'number' => -1 |
| 71 |
)); |
| 72 |
|
| 73 |
return $this->found_records() > 0; |
| 74 |
} |
| 75 |
|
| 76 |
public function found_records() |
| 77 |
{ |
| 78 |
return $this->query->get_total(); |
| 79 |
} |
| 80 |
|
| 81 |
public function setup($i) |
| 82 |
{ |
| 83 |
$user = $this->query->results[$i]; |
| 84 |
$this->record = (array)$user->data; |
| 85 |
$this->record['custom_fields'] = get_user_meta($this->record['ID']); |
| 86 |
return true; |
| 87 |
} |
| 88 |
} |
| 89 |
|