PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.1
Import WP – CSV & XML Import Export for WordPress v2.7.1
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 / Importer / Template / UserTemplate.php

UserTemplate.php in Import WP – CSV & XML Import Export for WordPress 2.7.1, at class/Common/Importer/Template/UserTemplate.php

210 lines 6.7 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\Importer\Template;
4
5 use ImportWP\Common\Importer\Exception\MapperException;
6 use ImportWP\Common\Importer\ParsedData;
7 use ImportWP\Common\Importer\TemplateInterface;
8 use ImportWP\EventHandler;
9
10 class UserTemplate extends Template implements TemplateInterface
11 {
12 protected $name = 'User';
13 protected $mapper = 'user';
14
15 public function __construct(EventHandler $event_handler)
16 {
17 parent::__construct($event_handler);
18
19 $this->groups[] = 'user';
20 $this->field_options = array_merge($this->field_options, [
21 'user.role' => [$this, 'get_user_role_options'],
22 ]);
23 }
24
25 public function register()
26 {
27 $groups = [];
28
29 // User
30 $groups[] = $this->register_group('User Fields', 'user', [
31 $this->register_field('ID', 'ID', [
32 'tooltip' => __('ID is only used to reference existing records', 'importwp')
33 ]),
34 $this->register_core_field('Username', 'user_login', [
35 'tooltip' => __('The user\'s login username', 'importwp')
36 ]),
37 $this->register_core_field('Email', 'user_email', [
38 'tooltip' => __('The user email address', 'importwp')
39 ]),
40 $this->register_core_field('First Name', 'first_name', [
41 'tooltip' => __('The user\'s first name', 'importwp')
42 ]),
43 $this->register_core_field('Last Name', 'last_name', [
44 'tooltip' => __('The user\'s last name', 'importwp')
45 ]),
46 $this->register_field('Role', 'role', [
47 'tooltip' => __('The plain-text user password', 'importwp'),
48 'options' => 'callback',
49 'default' => 'subscriber'
50 ]),
51 $this->register_field('Website', 'user_url', [
52 'tooltip' => __('The user\'s website address', 'importwp')
53 ]),
54 $this->register_field('Password', 'user_pass', [
55 'tooltip' => __('The plain-text user password', 'importwp')
56 ]),
57 $this->register_field('Nicename', 'user_nicename', []),
58 $this->register_field('Display Name', 'display_name', []),
59 $this->register_field('Description', 'description', []),
60 ]);
61
62 return $groups;
63 }
64
65
66
67 public function register_settings()
68 {
69 return [
70 $this->register_field('Enable user notifications.', 'notify_users', [
71 'type' => 'checkbox',
72 'tooltip' => __('Trigger the action to send WordPress notification emails', 'importwp')
73 ]),
74 $this->register_field('Generate user password.', 'generate_pass', [
75 'type' => 'checkbox',
76 'tooltip' => __('Enable the generation of user passwords', 'importwp')
77 ]),
78 ];
79 }
80
81 public function register_options()
82 {
83 return [];
84 }
85
86 /**
87 * Process data before record is importer.
88 *
89 * Alter data that is passed to the mapper.
90 *
91 * @param ParsedData $data
92 * @return ParsedData
93 */
94 public function pre_process(ParsedData $data)
95 {
96 $data = parent::pre_process($data);
97
98 $user_field_map = [
99 'ID' => $data->getValue('user.ID'),
100 'user_pass' => $data->getValue('user.user_pass'),
101 'user_login' => $data->getValue('user.user_login'),
102 'user_nicename' => $data->getValue('user.user_nicename'),
103 'user_url' => $data->getValue('user.user_url'),
104 'user_email' => $data->getValue('user.user_email'),
105 'display_name' => $data->getValue('user.display_name'),
106 'nickname' => $data->getValue('user.nickname'),
107 'first_name' => $data->getValue('user.first_name'),
108 'last_name' => $data->getValue('user.last_name'),
109 'description' => $data->getValue('user.description'),
110 'rich_editing' => $data->getValue('user.rich_editing'),
111 'user_registered' => $data->getValue('user.user_registered'),
112 'role' => $data->getValue('user.role'),
113 'jabber' => $data->getValue('user.jabber'),
114 'aim' => $data->getValue('user.aim'),
115 'yim' => $data->getValue('user.yim'),
116 ];
117
118 // remove fields that have not been set
119 foreach ($user_field_map as $field_key => $field_map) {
120
121 if ($field_map === false) {
122 unset($user_field_map[$field_key]);
123 continue;
124 }
125 }
126
127 $optional_fields = [
128 'ID',
129 'user_url',
130 'user_pass',
131 'role',
132 'user_nicename',
133 'display_name',
134 'description'
135 ];
136
137 foreach ($optional_fields as $optional_field) {
138 if (true !== $this->importer->isEnabledField('user.' . $optional_field)) {
139 unset($user_field_map[$optional_field]);
140 }
141 }
142
143 $generate_pass = $this->importer->getSetting('generate_pass');
144 if ($generate_pass === true) {
145 $user_field_map['user_pass'] = wp_generate_password(10);
146 }
147
148 $notify_users = $this->importer->getSetting('notify_users');
149 if ($notify_users !== true) {
150
151 // Disable password changed email
152 if (isset($user_field_map['user_pass'])) {
153 add_filter('send_password_change_email', '__return_false');
154 }
155
156 // Disable user_email changed email
157 if (isset($user_field_map['user_email'])) {
158 add_filter('send_email_change_email', '__return_false');
159 }
160 }
161
162 foreach ($user_field_map as $key => $value) {
163 $user_field_map[$key] = apply_filters('iwp/template/process_field', $value, $key, $this->importer);
164 }
165
166 $data->replace($user_field_map, 'default');
167
168 return $data;
169 }
170
171 /**
172 * Process data after record is importer.
173 *
174 * Use data that is returned from the mapper.
175 *
176 * @param int $user_id
177 * @param ParsedData $data
178 * @return void
179 */
180 final public function post_process($user_id, ParsedData $data)
181 {
182 $notify_users = $this->importer->getSetting('notify_users');
183 if ($notify_users === true) {
184 wp_new_user_notification($user_id, null, 'user');
185 }
186
187 parent::post_process($user_id, $data);
188 }
189
190 /**
191 * Get list of posts
192 *
193 * @return array|\WP_Error
194 */
195 public function get_user_role_options($importer_data)
196 {
197 $result = [];
198
199 global $wp_roles;
200 foreach ($wp_roles->roles as $role => $role_arr) {
201 $result[] = [
202 'value' => '' . $role,
203 'label' => $role_arr['name']
204 ];
205 }
206
207 return $result;
208 }
209 }
210