| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter\Mapper; |
| 4 |
|
| 5 |
use ImportWP\Common\Exporter\ExporterRecord; |
| 6 |
use ImportWP\Common\Exporter\MapperInterface; |
| 7 |
|
| 8 |
class UserMapper extends AbstractMapper implements MapperInterface |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var \WP_User_Query |
| 12 |
*/ |
| 13 |
private $query; |
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
add_filter('iwp/exporter_record/user', [$this, 'get_record_data'], 10, 3); |
| 18 |
} |
| 19 |
|
| 20 |
public function get_core_fields() |
| 21 |
{ |
| 22 |
return array( |
| 23 |
'ID', |
| 24 |
'user_login', |
| 25 |
'user_nicename', |
| 26 |
'user_email', |
| 27 |
'user_url', |
| 28 |
'user_registered', |
| 29 |
'user_status', |
| 30 |
'display_name', |
| 31 |
'first_name', |
| 32 |
'last_name' |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
public function get_fields() |
| 37 |
{ |
| 38 |
/** |
| 39 |
* @var \WPDB |
| 40 |
*/ |
| 41 |
global $wpdb; |
| 42 |
|
| 43 |
$fields = [ |
| 44 |
'key' => 'main', |
| 45 |
'label' => 'User', |
| 46 |
'loop' => true, |
| 47 |
'fields' => [], |
| 48 |
'children' => [ |
| 49 |
'custom_fields' => [ |
| 50 |
'key' => 'custom_fields', |
| 51 |
'label' => 'Custom Fields', |
| 52 |
'loop' => true, |
| 53 |
'loop_fields' => ['meta_key', 'meta_value'], |
| 54 |
'fields' => [], |
| 55 |
'children' => [] |
| 56 |
] |
| 57 |
] |
| 58 |
|
| 59 |
]; |
| 60 |
|
| 61 |
$fields['fields'] = $this->get_core_fields(); |
| 62 |
$fields['fields'][] = 'role'; |
| 63 |
$fields['fields'][] = 'first_name'; |
| 64 |
$fields['fields'][] = 'last_name'; |
| 65 |
$fields['fields'][] = 'description'; |
| 66 |
|
| 67 |
// user meta |
| 68 |
$meta_fields = $wpdb->get_col("SELECT DISTINCT meta_key FROM " . $wpdb->usermeta . " WHERE user_id IN (SELECT DISTINCT ID FROM " . $wpdb->users . " )"); |
| 69 |
$custom_file_fields = apply_filters('iwp/exporter/user/custom_file_id_fields', []); |
| 70 |
foreach ($meta_fields as $field) { |
| 71 |
|
| 72 |
if (in_array($field, ['first_name', 'last_name', 'description'])) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
$fields['children']['custom_fields']['fields'][] = $field; |
| 77 |
|
| 78 |
if (in_array($field, $custom_file_fields)) { |
| 79 |
$fields['children']['custom_fields']['fields'][] = $field . '::id'; |
| 80 |
$fields['children']['custom_fields']['fields'][] = $field . '::url'; |
| 81 |
$fields['children']['custom_fields']['fields'][] = $field . '::title'; |
| 82 |
$fields['children']['custom_fields']['fields'][] = $field . '::alt'; |
| 83 |
$fields['children']['custom_fields']['fields'][] = $field . '::caption'; |
| 84 |
$fields['children']['custom_fields']['fields'][] = $field . '::description'; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
$fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/user/custom_field_list', $fields['children']['custom_fields']['fields'], null); |
| 89 |
|
| 90 |
return $fields; |
| 91 |
} |
| 92 |
|
| 93 |
public function have_records($exporter_id) |
| 94 |
{ |
| 95 |
$query_args = []; |
| 96 |
$query_args = apply_filters('iwp/exporter/user_query', $query_args); |
| 97 |
$query_args = apply_filters(sprintf('iwp/exporter/%d/user_query', $exporter_id), $query_args); |
| 98 |
|
| 99 |
$query_args = wp_parse_args($query_args, [ |
| 100 |
'number' => -1, |
| 101 |
'fields' => 'ids' |
| 102 |
]); |
| 103 |
|
| 104 |
$this->query = new \WP_User_Query($query_args); |
| 105 |
$this->items = $this->query->results; |
| 106 |
|
| 107 |
return $this->found_records() > 0; |
| 108 |
} |
| 109 |
|
| 110 |
public function found_records() |
| 111 |
{ |
| 112 |
return count($this->items); |
| 113 |
} |
| 114 |
|
| 115 |
public function get_records() |
| 116 |
{ |
| 117 |
return $this->items; |
| 118 |
} |
| 119 |
|
| 120 |
public function setup($i) |
| 121 |
{ |
| 122 |
$user = get_user_by('id', $this->items[$i]); |
| 123 |
|
| 124 |
$this->record = new ExporterRecord((array)$user->data, 'user'); |
| 125 |
$this->record = apply_filters('iwp/exporter/user/setup_data', $this->record); |
| 126 |
|
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
public function get_record_data($value, $key, $record) |
| 131 |
{ |
| 132 |
switch ($key) { |
| 133 |
case 'custom_fields': |
| 134 |
$value = get_user_meta($record['ID']); |
| 135 |
|
| 136 |
if (isset($value['first_name'])) { |
| 137 |
unset($value['first_name']); |
| 138 |
} |
| 139 |
|
| 140 |
if (isset($value['last_name'])) { |
| 141 |
unset($value['last_name']); |
| 142 |
} |
| 143 |
|
| 144 |
if (isset($value['description'])) { |
| 145 |
unset($value['description']); |
| 146 |
} |
| 147 |
|
| 148 |
$value = $this->modify_custom_field_data($value, 'user'); |
| 149 |
break; |
| 150 |
case 'role': |
| 151 |
$userdata = get_userdata($record['ID']); |
| 152 |
$value = (array)$userdata->roles; |
| 153 |
break; |
| 154 |
case 'first_name': |
| 155 |
case 'last_name': |
| 156 |
case 'description': |
| 157 |
|
| 158 |
$value = get_user_meta($record['ID'], $key, true); |
| 159 |
break; |
| 160 |
} |
| 161 |
return $value; |
| 162 |
} |
| 163 |
} |
| 164 |
|