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