123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( ! class_exists( 'ACF_Form_User' ) ) :
- class ACF_Form_User {
-
- var $view = '';
-
- function __construct() {
-
- add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
- add_action( 'login_form_register', array( $this, 'login_form_register' ) );
-
- add_action( 'show_user_profile', array( $this, 'render_edit' ) );
- add_action( 'edit_user_profile', array( $this, 'render_edit' ) );
- add_action( 'user_new_form', array( $this, 'render_new' ) );
- add_action( 'register_form', array( $this, 'render_register' ) );
-
- add_action( 'user_register', array( $this, 'save_user' ) );
- add_action( 'profile_update', array( $this, 'save_user' ) );
-
- add_filter( 'registration_errors', array( $this, 'filter_registration_errors' ), 10, 3 );
- }
-
- function admin_enqueue_scripts() {
-
- if ( ! acf_is_screen( array( 'profile', 'user', 'user-edit' ) ) ) {
- return;
- }
-
- acf_enqueue_scripts();
- }
-
- function login_form_register() {
-
- acf_enqueue_scripts(
- array(
- 'context' => 'login',
- )
- );
- }
-
- function render_register() {
-
- $this->render(
- array(
- 'user_id' => 0,
- 'view' => 'register',
- 'el' => 'div',
- )
- );
- }
-
- function render_edit( $user ) {
-
- if ( ! is_admin() ) {
- acf_enqueue_scripts();
- }
-
- $this->render(
- array(
- 'user_id' => $user->ID,
- 'view' => 'edit',
- 'el' => 'tr',
- )
- );
- }
-
- function render_new() {
-
- if ( is_multisite() ) {
- return;
- }
-
- $this->render(
- array(
- 'user_id' => 0,
- 'view' => 'add',
- 'el' => 'tr',
- )
- );
- }
-
- function render( $args = array() ) {
-
- if ( isset( $_POST['acf'] ) ) {
- add_filter( 'acf/pre_load_value', array( $this, 'filter_pre_load_value' ), 10, 3 );
- }
-
- $args = wp_parse_args(
- $args,
- array(
- 'user_id' => 0,
- 'view' => 'edit',
- 'el' => 'tr',
- )
- );
-
- $post_id = 'user_' . $args['user_id'];
-
- $field_groups = acf_get_field_groups(
- array(
- 'user_id' => $args['user_id'] ? $args['user_id'] : 'new',
- 'user_form' => $args['view'],
- )
- );
-
- if ( empty( $field_groups ) ) {
- return;
- }
-
- acf_form_data(
- array(
- 'screen' => 'user',
- 'post_id' => $post_id,
- 'validation' => ( $args['view'] == 'register' ) ? 0 : 1,
- )
- );
-
- $before = '<table class="form-table"><tbody>';
- $after = '</tbody></table>';
- if ( $args['el'] == 'div' ) {
- $before = '<div class="acf-user-' . $args['view'] . '-fields acf-fields -clear">';
- $after = '</div>';
- }
-
- foreach ( $field_groups as $field_group ) {
-
- $fields = acf_get_fields( $field_group );
-
- if ( $field_group['style'] === 'default' ) {
- echo '<h2>' . $field_group['title'] . '</h2>';
- }
-
- echo $before;
- acf_render_fields( $fields, $post_id, $args['el'], $field_group['instruction_placement'] );
- echo $after;
- }
-
- add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ), 10, 1 );
- }
-
- function admin_footer() {
-
- ?>
- <script type="text/javascript">
- (function($) {
-
-
- var view = '<?php echo $this->view; ?>';
-
-
- var $submit = $('input.button-primary');
- if( !$submit.next('.spinner').length ) {
- $submit.after('<span class="spinner"></span>');
- }
-
- })(jQuery);
- </script>
- <?php
- }
-
- function save_user( $user_id ) {
-
- if ( ! acf_verify_nonce( 'user' ) ) {
- return $user_id;
- }
-
- if ( acf_validate_save_post( true ) ) {
- acf_save_post( "user_$user_id" );
- }
- }
-
- function filter_registration_errors( $errors, $sanitized_user_login, $user_email ) {
- if ( ! acf_validate_save_post() ) {
- $acf_errors = acf_get_validation_errors();
- foreach ( $acf_errors as $acf_error ) {
- $errors->add(
- acf_idify( $acf_error['input'] ),
- acf_esc_html( acf_punctify( sprintf( __( '<strong>Error</strong>: %s', 'acf' ), $acf_error['message'] ) ) )
- );
- }
- }
- return $errors;
- }
-
- function filter_pre_load_value( $null, $post_id, $field ) {
- $field_key = $field['key'];
-
- if ( isset( $_POST['acf'][ $field_key ] ) ) {
- return $_POST['acf'][ $field_key ];
- }
-
- return $null;
- }
- }
-
- acf_new_instance( 'ACF_Form_User' );
- endif;
- ?>
|