| @@ -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', |
| @@ -20,9 +26,11 @@ | ||
| 20 | 26 | 'user_email', |
| 21 | 27 | 'user_url', |
| 22 | 28 | 'user_registered', |
| 23 | 29 | 'user_status', |
| 24 | - 'display_name' | |
| 30 | + 'display_name', | |
| 31 | + 'first_name', | |
| 32 | + 'last_name' | |
| 25 | 33 | ); |
| 26 | 34 | } |
| 27 | 35 | |
| 28 | 36 | public function get_fields() |
| @@ -33,15 +41,15 @@ | ||
| 33 | 41 | global $wpdb; |
| 34 | 42 | |
| 35 | 43 | $fields = [ |
| 36 | 44 | 'key' => 'main', |
| 37 | - 'label' => 'User', | |
| 45 | + 'label' => __('User', 'jc-importer'), | |
| 38 | 46 | 'loop' => true, |
| 39 | 47 | 'fields' => [], |
| 40 | 48 | 'children' => [ |
| 41 | 49 | 'custom_fields' => [ |
| 42 | 50 | 'key' => 'custom_fields', |
| 43 | - 'label' => 'Custom Fields', | |
| 51 | + 'label' => __('Custom Fields', 'jc-importer'), | |
| 44 | 52 | 'loop' => true, |
| 45 | 53 | 'loop_fields' => ['meta_key', 'meta_value'], |
| 46 | 54 | 'fields' => [], |
| 47 | 55 | 'children' => [] |
| @@ -50,39 +58,105 @@ | ||
| 50 | 58 | |
| 51 | 59 | ]; |
| 52 | 60 | |
| 53 | 61 | $fields['fields'] = $this->get_core_fields(); |
| 62 | + $fields['fields'][] = 'role'; | |
| 63 | + $fields['fields'][] = 'description'; | |
| 54 | 64 | |
| 55 | - | |
| 56 | 65 | // user meta |
| 57 | 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', []); | |
| 58 | 68 | foreach ($meta_fields as $field) { |
| 69 | + | |
| 70 | + if (in_array($field, ['first_name', 'last_name', 'description'])) { | |
| 71 | + continue; | |
| 72 | + } | |
| 73 | + | |
| 59 | 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 | + } | |
| 60 | 84 | } |
| 61 | 85 | |
| 62 | 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'); | |
| 63 | 88 | |
| 64 | - return $fields; | |
| 89 | + return $this->parse_fields($fields); | |
| 65 | 90 | } |
| 66 | 91 | |
| 67 | - public function have_records() | |
| 92 | + public function have_records($exporter_id) | |
| 68 | 93 | { |
| 69 | - $this->query = new \WP_User_Query(array( | |
| 70 | - 'number' => -1 | |
| 71 | - )); | |
| 94 | + $query_args = []; | |
| 95 | + $query_args = apply_filters('iwp/exporter/user_query', $query_args); | |
| 96 | + $query_args = apply_filters(sprintf('iwp/exporter/%d/user_query', $exporter_id), $query_args); | |
| 72 | 97 | |
| 98 | + $query_args = wp_parse_args($query_args, [ | |
| 99 | + 'number' => -1, | |
| 100 | + 'fields' => 'ids' | |
| 101 | + ]); | |
| 102 | + | |
| 103 | + $this->query = new \WP_User_Query($query_args); | |
| 104 | + $this->items = $this->query->results; | |
| 105 | + | |
| 73 | 106 | return $this->found_records() > 0; |
| 74 | 107 | } |
| 75 | 108 | |
| 76 | 109 | public function found_records() |
| 77 | 110 | { |
| 78 | - return $this->query->get_total(); | |
| 111 | + return count($this->items); | |
| 79 | 112 | } |
| 80 | 113 | |
| 114 | + public function get_records() | |
| 115 | + { | |
| 116 | + return $this->items; | |
| 117 | + } | |
| 118 | + | |
| 81 | 119 | public function setup($i) |
| 82 | 120 | { |
| 83 | - $user = $this->query->results[$i]; | |
| 84 | - $this->record = (array)$user->data; | |
| 85 | - $this->record['custom_fields'] = get_user_meta($this->record['ID']); | |
| 121 | + $user = get_user_by('id', $this->items[$i]); | |
| 122 | + | |
| 123 | + $this->record = new ExporterRecord((array)$user->data, 'user'); | |
| 124 | + $this->record = apply_filters('iwp/exporter/user/setup_data', $this->record, 'user'); | |
| 125 | + | |
| 86 | 126 | return true; |
| 127 | + } | |
| 128 | + | |
| 129 | + public function get_record_data($value, $key, $record) | |
| 130 | + { | |
| 131 | + switch ($key) { | |
| 132 | + case 'custom_fields': | |
| 133 | + $value = get_user_meta($record['ID']); | |
| 134 | + | |
| 135 | + if (isset($value['first_name'])) { | |
| 136 | + unset($value['first_name']); | |
| 137 | + } | |
| 138 | + | |
| 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; | |
| 159 | + } | |
| 160 | + return $value; | |
| 87 | 161 | } |
| 88 | 162 | } |