PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.12
Import WP – CSV & XML Import Export for WordPress v2.7.12
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.7.12, at class/Common/Exporter/Mapper/UserMapper.php

128 lines 3.4 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\MapperInterface;
6
7 class UserMapper extends AbstractMapper implements MapperInterface
8 {
9 /**
10 * @var \WP_User_Query
11 */
12 private $query;
13
14 public 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 'first_name',
26 'last_name'
27 );
28 }
29
30 public function get_fields()
31 {
32 /**
33 * @var \WPDB
34 */
35 global $wpdb;
36
37 $fields = [
38 'key' => 'main',
39 'label' => 'User',
40 'loop' => true,
41 'fields' => [],
42 'children' => [
43 'custom_fields' => [
44 'key' => 'custom_fields',
45 'label' => 'Custom Fields',
46 'loop' => true,
47 'loop_fields' => ['meta_key', 'meta_value'],
48 'fields' => [],
49 'children' => []
50 ]
51 ]
52
53 ];
54
55 $fields['fields'] = $this->get_core_fields();
56 $fields['fields'][] = 'role';
57 $fields['fields'][] = 'first_name';
58 $fields['fields'][] = 'last_name';
59 $fields['fields'][] = 'description';
60
61 // user meta
62 $meta_fields = $wpdb->get_col("SELECT DISTINCT meta_key FROM " . $wpdb->usermeta . " WHERE user_id IN (SELECT DISTINCT ID FROM " . $wpdb->users . " )");
63 foreach ($meta_fields as $field) {
64
65 if (in_array($field, ['first_name', 'last_name', 'description'])) {
66 continue;
67 }
68
69 $fields['children']['custom_fields']['fields'][] = $field;
70 }
71
72 $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/user/custom_field_list', $fields['children']['custom_fields']['fields'], null);
73
74 return $fields;
75 }
76
77 public function have_records($exporter_id)
78 {
79 $query_args = [];
80 $query_args = apply_filters('iwp/exporter/user_query', $query_args);
81 $query_args = apply_filters(sprintf('iwp/exporter/%d/user_query', $exporter_id), $query_args);
82
83 $query_args = wp_parse_args($query_args, [
84 'number' => -1,
85 'fields' => 'ids'
86 ]);
87
88 $this->query = new \WP_User_Query($query_args);
89 $this->items = $this->query->results;
90
91 return $this->found_records() > 0;
92 }
93
94 public function found_records()
95 {
96 return count($this->items);
97 }
98
99 public function get_records()
100 {
101 return $this->items;
102 }
103
104 public function setup($i)
105 {
106 $user = get_user_by('id', $this->items[$i]);
107 $this->record = (array)$user->data;
108 $this->record['custom_fields'] = get_user_meta($this->record['ID']);
109
110 $userdata = get_userdata($this->record['ID']);
111 $this->record['role'] = (array)$userdata->roles;
112
113 foreach (['first_name', 'last_name', 'description'] as $field) {
114
115 if (isset($this->record['custom_fields'][$field])) {
116
117 $this->record[$field] = $this->record['custom_fields'][$field][0];
118 unset($this->record['custom_fields'][$field]);
119 } else {
120
121 $this->record[$field] = '';
122 }
123 }
124
125 return true;
126 }
127 }
128