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