PluginProbe
Advanced Custom Fields (ACF®) / 6.2.8
Advanced Custom Fields (ACF®) v6.2.8
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / forms / form-user.php
form-user.php
373 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if ( ! class_exists( 'ACF_Form_User' ) ) :
8
9 class ACF_Form_User {
10
11 /** @var string The current view (new, edit, register) */
12 var $view = '';
13
14
15 /**
16 * This function will setup the class functionality
17 *
18 * @type function
19 * @date 5/03/2014
20 * @since 5.0.0
21 *
22 * @param n/a
23 * @return n/a
24 */
25
26 function __construct() {
27
28 // enqueue
29 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
30 add_action( 'login_form_register', array( $this, 'login_form_register' ) );
31
32 // render
33 add_action( 'show_user_profile', array( $this, 'render_edit' ) );
34 add_action( 'edit_user_profile', array( $this, 'render_edit' ) );
35 add_action( 'user_new_form', array( $this, 'render_new' ) );
36 add_action( 'register_form', array( $this, 'render_register' ) );
37
38 // save
39 add_action( 'user_register', array( $this, 'save_user' ) );
40 add_action( 'profile_update', array( $this, 'save_user' ) );
41
42 // Perform validation before new user is registered.
43 add_filter( 'registration_errors', array( $this, 'filter_registration_errors' ), 10, 3 );
44 }
45
46
47 /**
48 * admin_enqueue_scripts
49 *
50 * Checks current screen and enqueues scripts
51 *
52 * @date 17/4/18
53 * @since 5.6.9
54 *
55 * @param void
56 * @return void
57 */
58
59 function admin_enqueue_scripts() {
60
61 // bail early if not valid screen
62 if ( ! acf_is_screen( array( 'profile', 'user', 'user-edit', 'profile-network', 'user-network', 'user-edit-network' ) ) ) {
63 return;
64 }
65
66 // enqueue
67 acf_enqueue_scripts();
68 }
69
70
71 /**
72 * login_form_register
73 *
74 * Customizes and enqueues scripts
75 *
76 * @date 17/4/18
77 * @since 5.6.9
78 *
79 * @param void
80 * @return void
81 */
82
83 function login_form_register() {
84
85 // customize action prefix so that "admin_head" = "login_head"
86 acf_enqueue_scripts(
87 array(
88 'context' => 'login',
89 )
90 );
91 }
92
93
94 /**
95 * Called during the user register form
96 *
97 * @type function
98 * @date 8/10/13
99 * @since 5.0.0
100 *
101 * @param void
102 * @return void
103 */
104
105 function render_register() {
106
107 // render
108 $this->render(
109 array(
110 'user_id' => 0,
111 'view' => 'register',
112 'el' => 'div',
113 )
114 );
115 }
116
117
118 /**
119 * Called during the user edit form
120 *
121 * @type function
122 * @date 8/10/13
123 * @since 5.0.0
124 *
125 * @param void
126 * @return void
127 */
128
129 function render_edit( $user ) {
130
131 // add compatibility with front-end user profile edit forms such as bbPress
132 if ( ! is_admin() ) {
133 acf_enqueue_scripts();
134 }
135
136 // render
137 $this->render(
138 array(
139 'user_id' => $user->ID,
140 'view' => 'edit',
141 'el' => 'tr',
142 )
143 );
144 }
145
146
147 /**
148 * description
149 *
150 * @type function
151 * @date 8/10/13
152 * @since 5.0.0
153 *
154 * @param $post_id (int)
155 * @return $post_id (int)
156 */
157
158 function render_new() {
159
160 // Multisite uses a different 'user-new.php' form. Don't render fields here
161 if ( is_multisite() ) {
162 return;
163 }
164
165 // render
166 $this->render(
167 array(
168 'user_id' => 0,
169 'view' => 'add',
170 'el' => 'tr',
171 )
172 );
173 }
174
175
176 /**
177 * This function will render ACF fields for a given $post_id parameter
178 *
179 * @type function
180 * @since 5.0.0
181 *
182 * @param $user_id (int) this can be set to 0 for a new user
183 * @param $user_form (string) used for location rule matching. edit | add | register
184 * @param $el (string)
185 * @return n/a
186 */
187
188 function render( $args = array() ) {
189
190 // Allow $_POST data to persist across form submission attempts.
191 if ( isset( $_POST['acf'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
192 add_filter( 'acf/pre_load_value', array( $this, 'filter_pre_load_value' ), 10, 3 );
193 }
194
195 // defaults
196 $args = wp_parse_args(
197 $args,
198 array(
199 'user_id' => 0,
200 'view' => 'edit',
201 'el' => 'tr',
202 )
203 );
204
205 // vars
206 $post_id = 'user_' . $args['user_id'];
207
208 // get field groups
209 $field_groups = acf_get_field_groups(
210 array(
211 'user_id' => $args['user_id'] ? $args['user_id'] : 'new',
212 'user_form' => $args['view'],
213 )
214 );
215
216 // bail early if no field groups
217 if ( empty( $field_groups ) ) {
218 return;
219 }
220
221 // form data
222 acf_form_data(
223 array(
224 'screen' => 'user',
225 'post_id' => $post_id,
226 'validation' => ( $args['view'] == 'register' ) ? 0 : 1,
227 )
228 );
229
230 // elements
231 $before = '<table class="form-table"><tbody>';
232 $after = '</tbody></table>';
233
234 if ( $args['el'] == 'div' ) {
235 $before = '<div class="acf-user-' . esc_attr( $args['view'] ) . '-fields acf-fields -clear">';
236 $after = '</div>';
237 }
238
239 // loop
240 foreach ( $field_groups as $field_group ) {
241
242 // vars
243 $fields = acf_get_fields( $field_group );
244
245 // title
246 if ( $field_group['style'] === 'default' ) {
247 echo '<h2>' . esc_html( $field_group['title'] ) . '</h2>';
248 }
249
250 // render
251 echo $before; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe HTML string.
252 acf_render_fields( $fields, $post_id, $args['el'], $field_group['instruction_placement'] );
253 echo $after; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe HTML string.
254 }
255
256 // actions
257 add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ), 10, 1 );
258 }
259
260
261 /**
262 * description
263 *
264 * @type function
265 * @date 27/03/2015
266 * @since 5.1.5
267 *
268 * @param $post_id (int)
269 * @return $post_id (int)
270 */
271
272 function admin_footer() {
273
274 // script
275 ?>
276 <script type="text/javascript">
277 (function($) {
278
279 // vars
280 var view = '<?php echo esc_attr( $this->view ); ?>';
281
282 // add missing spinners
283 var $submit = $('input.button-primary');
284 if( !$submit.next('.spinner').length ) {
285 $submit.after('<span class="spinner"></span>');
286 }
287
288 })(jQuery);
289 </script>
290 <?php
291 }
292
293
294 /**
295 * description
296 *
297 * @type function
298 * @date 8/10/13
299 * @since 5.0.0
300 *
301 * @param $post_id (int)
302 * @return $post_id (int)
303 */
304
305 function save_user( $user_id ) {
306
307 // verify nonce
308 if ( ! acf_verify_nonce( 'user' ) ) {
309 return $user_id;
310 }
311
312 // save
313 if ( acf_validate_save_post( true ) ) {
314 acf_save_post( "user_$user_id" );
315 }
316 }
317
318 /**
319 * filter_registration_errors
320 *
321 * Validates $_POST data and appends any errors to prevent new user registration.
322 *
323 * @date 12/7/19
324 * @since 5.8.1
325 *
326 * @param WP_Error $errors A WP_Error object containing any errors encountered during registration.
327 * @param string $sanitized_user_login User's username after it has been sanitized.
328 * @param string $user_email User's email.
329 * @return WP_Error
330 */
331 function filter_registration_errors( $errors, $sanitized_user_login, $user_email ) {
332 if ( ! acf_validate_save_post() ) {
333 $acf_errors = acf_get_validation_errors();
334 foreach ( $acf_errors as $acf_error ) {
335 $errors->add(
336 acf_idify( $acf_error['input'] ),
337 acf_esc_html( acf_punctify( sprintf( __( '<strong>Error</strong>: %s', 'acf' ), $acf_error['message'] ) ) )
338 );
339 }
340 }
341 return $errors;
342 }
343
344 /**
345 * filter_pre_load_value
346 *
347 * Checks if a $_POST value exists for this field to allow persistent values.
348 *
349 * @date 12/7/19
350 * @since 5.8.2
351 *
352 * @param null $null A null placeholder.
353 * @param (int|string) $post_id The post id.
354 * @param array $field The field array.
355 * @return mixed
356 */
357 function filter_pre_load_value( $null, $post_id, $field ) {
358 $field_key = $field['key'];
359 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified in save_user().
360 if ( isset( $_POST['acf'][ $field_key ] ) ) {
361 return $_POST['acf'][ $field_key ]; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized elsewhere.
362 }
363 // phpcs:enable WordPress.Security.NonceVerification.Missing
364 return $null;
365 }
366 }
367
368 // instantiate
369 acf_new_instance( 'ACF_Form_User' );
370 endif; // class_exists check
371
372 ?>
373