/
class-llms-admin-profile.php
279 lines (227 loc) · 6.31 KB
/
class-llms-admin-profile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* Handle extra profile fields for users in admin
*
* @package LifterLMS/Admin/Classes
*
* @since [verson]
* @version 5.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Handle extra profile fields for users in admin
*
* Applies to edit-user.php & profile.php.
*
* @since 5.0.0
*/
class LLMS_Admin_Profile {
/**
* Array of user profile fields
*
* @var array
*/
private $fields;
/**
* Submission errors
*
* @var null|WP_Error
*/
private $errors;
/**
* Constructor
*
* @since 5.0.0
*
* @return void
*/
public function __construct() {
add_action( 'show_user_profile', array( $this, 'add_user_meta_fields' ) );
add_action( 'edit_user_profile', array( $this, 'add_user_meta_fields' ) );
add_action( 'personal_options_update', array( $this, 'save_user_meta_fields' ) );
add_action( 'edit_user_profile_update', array( $this, 'save_user_meta_fields' ) );
// Allow errors to be output.
add_action( 'user_profile_update_errors', array( $this, 'add_errors' ) );
}
/**
* Add user meta fields to the profile screens
*
* @since 5.0.0
*
* @param WP_User $user Instance of WP_User for the user being updated.
* @return bool `true` if fields were added, `false` otherwise.
*/
public function add_user_meta_fields( $user ) {
if ( ! $this->current_user_can_edit_admin_custom_fields() ) {
return false;
}
$fields = $this->get_fields();
if ( empty( $fields ) ) {
return false;
}
/**
* Enqueue select2 scripts and styles.
*/
wp_enqueue_script( 'llms-metaboxes' );
wp_enqueue_script( 'llms-select2' );
llms()->assets->enqueue_style( 'llms-select2-styles' );
wp_add_inline_script(
'llms',
"window.llms.address_info = '" . wp_json_encode( llms_get_countries_address_info() ) . "';"
);
include_once LLMS_PLUGIN_DIR . 'includes/admin/views/user-edit-fields.php';
return true;
}
/**
* Maybe save user meta fields
*
* @since 5.0.0
*
* @param int $user_id WP_User ID for the user being updated.
* @return void
*/
public function save_user_meta_fields( $user_id ) {
if ( ! $this->current_user_can_edit_admin_custom_fields() ) {
return;
}
$fields = $this->get_fields();
$posted_data = array();
foreach ( $this->fields as $field ) {
//phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce is verified prior to reaching this method.
if ( isset( $_POST[ $field['name'] ] ) &&
isset( $field['data_store_key'] ) &&
$field['data_store'] && 'usermeta' === $field['data_store'] ) {
//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- sanitization and unslashing happens in `LLMS_Form_Handler::instance()->submit_form_fields()` below.
$posted_data[ $field['name'] ] = $_POST[ $field['name'] ];
}
//phpcs:disable WordPress.Security.NonceVerification.Missing
}
if ( empty( $posted_data ) ) {
return;
}
$posted_data['user_id'] = $user_id;
$submit = LLMS_Form_Handler::instance()->submit_fields( $posted_data, 'admin-profile', $fields, 'update' );
if ( is_wp_error( $submit ) ) {
$this->errors = $submit;
}
}
/**
* Maybe print validation errors
*
* @since 5.0.0
*
* @param WP_Error $errors Instance of WP_Error, passed by reference.
* @return void
*/
public function add_errors( &$errors ) {
if ( is_wp_error( $this->errors ) && $this->errors->has_errors() ) {
$this->merge_llms_fields_errors( $errors );
}
}
/**
* Check whether the current user can edit users custom fields
*
* @since 5.0.0
*
* @return boolean
*/
private function current_user_can_edit_admin_custom_fields() {
return current_user_can( 'manage_lifterlms' ) && current_user_can( 'edit_users' );
}
/**
* Merge llms fields errors into the passed WP_Error
*
* @since 5.0.0
* @todo Remove the fallback when minimum required WP version will be 5.6+.
*
* @param WP_Error $errors Instance of WP_Error, passed by reference.
* @return void
*/
private function merge_llms_fields_errors( &$errors ) {
foreach ( $this->errors->get_error_codes() as $code ) {
foreach ( $this->errors->get_error_messages( $code ) as $error_message ) {
$errors->add(
$code,
sprintf(
// Translators: %1$s = Opening strong tag; %2$s = Closing strong tag; %3$s = The error message.
esc_html__( '%1$sError%2$s: %3$s', 'lifterlms' ),
'<strong>',
'</strong>',
$error_message
)
);
}
// `WP_Error::get_all_error_data()` has been introduced in WP 5.6.0.
$error_data = method_exists( $this->errors, 'get_all_error_data' ) ?
$this->errors->get_all_error_data( $code ) : $this->errors->get_error_data( $code );
foreach ( $error_data as $data ) {
$errors->add_data( $data, $code );
}
}
}
/**
* Get fields to be added in the profile screen
*
* @since 5.0.0
*
* @return array
*/
private function get_fields() {
if ( ! isset( $this->fields ) ) {
$this->fields = $this->prepare_fields();
}
return $this->fields;
}
/**
* Setup fields to be added to the profile screen
*
* @since 5.0.0
*
* @return array
*/
private function prepare_fields() {
$fields = llms_get_user_information_fields();
$prepared = array();
/**
* Filters the list of user information fields which are excluded from the admin profile.
*
* By default WP core fields are excluded as they are automatically rendered on the screen
* by the WP core.
*
* @since 5.0.0
*
* @param string[] $fields A list of field ids to be excluded.
*/
$excluded = apply_filters(
'llms_admin_profile_excluded_fields',
array(
'user_login',
'email_address',
'password',
'first_name',
'last_name',
'display_name',
)
);
foreach ( $fields as $field ) {
// Skip excluded fields.
if ( in_array( $field['name'], $excluded, true ) ) {
continue;
}
// For display purposes.
$field['columns'] = 6;
// Handle weird exception.
$field['label'] = ( 'llms_billing_address_2' === $field['name'] ) ? __( 'Address line 2', 'lifterlms' ) : $field['label'];
$prepared[] = $field;
}
/**
* Fields to be added in the profile screen
*
* @since 5.0.0
*
* @param array[] $fields Array of fields.
*/
return apply_filters( 'llms_admin_profile_fields', $prepared );
}
}
return new LLMS_Admin_Profile();