PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.14.23
Import WP – CSV & XML Import Export for WordPress v2.14.23
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Exporter / Mapper / UserMapper.php

UserMapper.php in Import WP – CSV & XML Import Export for WordPress 2.14.23, at class/Common/Exporter/Mapper/UserMapper.php

163 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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', 'jc-importer'),
46 'loop' => true,
47 'fields' => [],
48 'children' => [
49 'custom_fields' => [
50 'key' => 'custom_fields',
51 'label' => __('Custom Fields', 'jc-importer'),
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'][] = 'description';
64
65 // user meta
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', []);
68 foreach ($meta_fields as $field) {
69
70 if (in_array($field, ['first_name', 'last_name', 'description'])) {
71 continue;
72 }
73
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 }
84 }
85
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');
88
89 return $this->parse_fields($fields);
90 }
91
92 public function have_records($exporter_id)
93 {
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);
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
106 return $this->found_records() > 0;
107 }
108
109 public function found_records()
110 {
111 return count($this->items);
112 }
113
114 public function get_records()
115 {
116 return $this->items;
117 }
118
119 public function setup($i)
120 {
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
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;
161 }
162 }
163