name = 'user_password'; $this->label = __( 'User Password', 'advanced-members' ); $this->category = 'Advanced Members'; $this->description = __( 'Login password of the user. Synced to `user_pass` user data.', 'advanced-members' ); $this->preview_image = amem_get_url('images/field-type-previews/field-preview-password.png', 'assets'); $this->defaults = array( 'placeholder' => '', 'prepend' => '', 'append' => '', 'force_edit' => 0, 'password_strength' => '3', 'show_pass_confirm' => 0, 'bypass_empty' => 0, ); add_filter( 'acf/load_field/type=password', array( $this, 'load_user_password_field' ) ); add_filter( 'acf/update_value/type=' . $this->name, array( $this, 'pre_update_value' ), 9, 3 ); } function load_user_password_field( $field ) { if ( ! empty( $field['custom_password'] ) ) { $field['type'] = 'user_password'; } return $field; } function prepare_field( $field ) { $field['required'] = 1; if ( !empty($field['bypass_empty']) ) $field['required'] = 0; if ( isset( $field['wrapper']['class'] ) ) { $field['wrapper']['class'] .= ' amem-password-main amem-pwd'; } else { $field['wrapper']['class'] = 'amem-password-main amem-pwd'; } if ( ! $field['value'] ) { return $field; } // if ( empty( $field['force_edit'] ) ) { // $field['required'] = false; // $field['wrapper']['class'] .= ' edit_password'; // $field['edit_user_password'] = true; // } $form = amem()->form; if ( isset( $form['approval'] ) ) { return false; } else { $field['value'] = ''; } return $field; } public function load_value( $value, $post_id = false, $field = false ) { if ( $user_id = $this->_user_id($post_id) ) { $edit_user = get_user_by( 'ID', $user_id ); if ( $edit_user instanceof \WP_User ) { $value = 'i'; } } return $value; } function validate_value( $is_valid, $value, $field, $input ) { // if ( is_numeric( $_POST['_acf_user'] ) && ! isset( $_POST['edit_user_password'] ) ) { // return $is_valid; // } if ( $this->mode() == 'login' ) { return $is_valid; } if ( !empty($field['bypass_empty']) && empty($value) ) return $is_valid; // Empty password must not be reported as "too weak" (hidden strength field is 0 when empty). // Return $is_valid so ACF keeps its default "%s value is required" message (field label). if ( '' === trim( (string) $value ) ) { return $is_valid; } if ( $field['show_pass_confirm'] && amem()->is_amem() ) { $ps_confirm_field = sanitize_key( $_POST['custom_password_confirm'] ); // phpcs:disable WordPress.Security.NonceVerification -- already verified by ACF if ( !isset($_POST[ $ps_confirm_field ]) || sanitize_text_field($_POST[ $ps_confirm_field ]) != $value ) { // phpcs:disable WordPress.Security.NonceVerification -- already verified by ACF return __( 'The passwords do not match', 'advanced-members' ); } } if ( isset( $_POST['password-strength'] ) && isset( $_POST['required-strength'] ) ) { // phpcs:disable WordPress.Security.NonceVerification -- already verified by ACF if( absint( $_POST['password-strength'] ) < absint( $_POST['required-strength'] ) ) { // phpcs:disable WordPress.Security.NonceVerification -- already verified by ACF // if ( ! $field['required'] && $value == '' && ! isset( $_POST['edit_user_password'] ) ) { // return $is_valid; // } return __( 'The password is too weak. Please make it stronger.', 'advanced-members' ); } } return $is_valid; } function pre_update_value( $value, $post_id = false, $field = false ) { // if ( empty( $_POST['edit_user_password'] ) ) { // return null; // } $user_id = (int) $this->_user_id( $post_id ); if ( $user_id && ! empty( $value ) ) { if ( ! $this->_can_edit( $user_id ) ) { return null; } remove_action( 'acf/save_post', '_acf_do_save_post' ); wp_update_user( array( 'ID' => $user_id, 'user_pass' => $value, ) ); add_action( 'acf/save_post', '_acf_do_save_post' ); } return null; } function update_value( $field ) { return null; } function render_field( $field ) { $field['type'] = 'password'; parent::render_field( $field ); if ( $this->mode() == 'login' ) { return; } wp_enqueue_script( 'password-strength-meter' ); wp_enqueue_script( 'amem-password-strength' ); echo ''; if ( isset( $field['password_strength'] ) ) { echo '
'; echo '