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