form-user.php 7.9 KB

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