PluginProbe
Advanced Custom Fields (ACF®) / 5.9.7
Advanced Custom Fields (ACF®) v5.9.7
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
370 lines 7.1 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' ) ) exit; // Exit if accessed directly
4
5 if( ! class_exists('ACF_Form_User') ) :
6
7 class ACF_Form_User {
8
9 /** @var string The current view (new, edit, register) */
10 var $view = '';
11
12
13 /*
14 * __construct
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')) ) {
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(array(
87 'context' => 'login'
88 ));
89 }
90
91
92 /*
93 * register_user
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(array(
109 'user_id' => 0,
110 'view' => 'register',
111 'el' => 'div'
112 ));
113 }
114
115
116 /*
117 * render_edit
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(array(
138 'user_id' => $user->ID,
139 'view' => 'edit',
140 'el' => 'tr'
141 ));
142 }
143
144
145 /*
146 * user_new_form
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(array(
167 'user_id' => 0,
168 'view' => 'add',
169 'el' => 'tr'
170 ));
171 }
172
173
174 /*
175 * render
176 *
177 * This function will render ACF fields for a given $post_id parameter
178 *
179 * @type function
180 * @date 7/10/13
181 * @since 5.0.0
182 *
183 * @param $user_id (int) this can be set to 0 for a new user
184 * @param $user_form (string) used for location rule matching. edit | add | register
185 * @param $el (string)
186 * @return n/a
187 */
188
189 function render( $args = array() ) {
190
191 // Allow $_POST data to persist across form submission attempts.
192 if( isset($_POST['acf']) ) {
193 add_filter('acf/pre_load_value', array($this, 'filter_pre_load_value'), 10, 3);
194 }
195
196 // defaults
197 $args = wp_parse_args($args, array(
198 'user_id' => 0,
199 'view' => 'edit',
200 'el' => 'tr',
201 ));
202
203 // vars
204 $post_id = 'user_' . $args['user_id'];
205
206 // get field groups
207 $field_groups = acf_get_field_groups(array(
208 'user_id' => $args['user_id'] ? $args['user_id'] : 'new',
209 'user_form' => $args['view']
210 ));
211
212 // bail early if no field groups
213 if( empty($field_groups) ) {
214 return;
215 }
216
217 // form data
218 acf_form_data(array(
219 'screen' => 'user',
220 'post_id' => $post_id,
221 'validation' => ($args['view'] == 'register') ? 0 : 1
222 ));
223
224 // elements
225 $before = '<table class="form-table"><tbody>';
226 $after = '</tbody></table>';
227
228 if( $args['el'] == 'div') {
229 $before = '<div class="acf-user-' . $args['view'] . '-fields acf-fields -clear">';
230 $after = '</div>';
231 }
232
233 // loop
234 foreach( $field_groups as $field_group ) {
235
236 // vars
237 $fields = acf_get_fields( $field_group );
238
239 // title
240 if( $field_group['style'] === 'default' ) {
241 echo '<h2>' . $field_group['title'] . '</h2>';
242 }
243
244 // render
245 echo $before;
246 acf_render_fields( $fields, $post_id, $args['el'], $field_group['instruction_placement'] );
247 echo $after;
248 }
249
250 // actions
251 add_action('acf/input/admin_footer', array($this, 'admin_footer'), 10, 1);
252 }
253
254
255 /*
256 * admin_footer
257 *
258 * description
259 *
260 * @type function
261 * @date 27/03/2015
262 * @since 5.1.5
263 *
264 * @param $post_id (int)
265 * @return $post_id (int)
266 */
267
268 function admin_footer() {
269
270 // script
271 ?>
272 <script type="text/javascript">
273 (function($) {
274
275 // vars
276 var view = '<?php echo $this->view; ?>';
277
278 // add missing spinners
279 var $submit = $('input.button-primary');
280 if( !$submit.next('.spinner').length ) {
281 $submit.after('<span class="spinner"></span>');
282 }
283
284 })(jQuery);
285 </script>
286 <?php
287
288 }
289
290
291 /*
292 * save_user
293 *
294 * description
295 *
296 * @type function
297 * @date 8/10/13
298 * @since 5.0.0
299 *
300 * @param $post_id (int)
301 * @return $post_id (int)
302 */
303
304 function save_user( $user_id ) {
305
306 // verify nonce
307 if( !acf_verify_nonce('user') ) {
308 return $user_id;
309 }
310
311 // save
312 if( acf_validate_save_post(true) ) {
313 acf_save_post( "user_$user_id" );
314 }
315 }
316
317 /**
318 * filter_registration_errors
319 *
320 * Validates $_POST data and appends any errors to prevent new user registration.
321 *
322 * @date 12/7/19
323 * @since 5.8.1
324 *
325 * @param WP_Error $errors A WP_Error object containing any errors encountered during registration.
326 * @param string $sanitized_user_login User's username after it has been sanitized.
327 * @param string $user_email User's email.
328 * @return WP_Error
329 */
330 function filter_registration_errors( $errors, $sanitized_user_login, $user_email ) {
331 if( !acf_validate_save_post() ) {
332 $acf_errors = acf_get_validation_errors();
333 foreach( $acf_errors as $acf_error ) {
334 $errors->add(
335 acf_idify( $acf_error['input'] ),
336 acf_esc_html( acf_punctify( sprintf( __('<strong>Error</strong>: %s', 'acf'), $acf_error['message'] ) ) )
337 );
338 }
339 }
340 return $errors;
341 }
342
343 /**
344 * filter_pre_load_value
345 *
346 * Checks if a $_POST value exists for this field to allow persistent values.
347 *
348 * @date 12/7/19
349 * @since 5.8.2
350 *
351 * @param null $null A null placeholder.
352 * @param (int|string) $post_id The post id.
353 * @param array $field The field array.
354 * @return mixed
355 */
356 function filter_pre_load_value( $null, $post_id, $field ) {
357 $field_key = $field['key'];
358 if( isset( $_POST['acf'][ $field_key ] )) {
359 return $_POST['acf'][ $field_key ];
360 }
361 return $null;
362 }
363 }
364
365 // instantiate
366 acf_new_instance('ACF_Form_User');
367
368 endif; // class_exists check
369
370 ?>