Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ur-1678 Feature - User edit feature for admin. (Sync) #752

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions includes/class-ur-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public static function update_profile_details() {
}

// Current user id.
$user_id = get_current_user_id();
$user_id = !empty($_REQUEST['user_id']) ? absint($_REQUEST['user_id']) : get_current_user_id();

if ( $user_id <= 0 ) {
return;
Expand Down Expand Up @@ -388,9 +388,9 @@ public static function update_profile_details() {
* @param array $profile User profile data.
* @param array $form_data The form data.
* @param int $form_id The form ID.
* @param int $user_id The user id.
*/
do_action( 'user_registration_validate_profile_update', $profile, $form_data, $form_id );

do_action( 'user_registration_validate_profile_update', $profile, $form_data, $form_id, $user_id );
/**
* Action after the save profile validation.
*
Expand All @@ -408,7 +408,7 @@ public static function update_profile_details() {
$is_email_change_confirmation = (bool) apply_filters( 'user_registration_email_change_confirmation', true );
$email_updated = false;
$pending_email = '';
$user = wp_get_current_user();
$user = get_userdata( $user_id );
/**
* Filter to modify the field settings.
*
Expand Down Expand Up @@ -452,7 +452,7 @@ public static function update_profile_details() {
}

if ( count( $user_data ) > 0 ) {
$user_data['ID'] = get_current_user_id();
$user_data['ID'] = $user_id;
wp_update_user( $user_data );
}
/**
Expand Down
3 changes: 2 additions & 1 deletion includes/class-ur-form-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ public static function save_profile_details() {
* @param array $profile The user profile data.
* @param array $form_data The form data.
* @param int $form_id The form ID.
* @param int $user_id The user id.
*/
do_action( 'user_registration_validate_profile_update', $profile, $form_data, $form_id );
do_action( 'user_registration_validate_profile_update', $profile, $form_data, $form_id, $user_id );

/**
* Action validate profile on update.
Expand Down
23 changes: 15 additions & 8 deletions includes/functions-ur-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -5051,8 +5051,9 @@ function user_registration_validate_form_field_data( $data, $form_data, $form_id
* @param int $form_id Form id.
* @param array $form_field_data Form Field Data..
* @param array $form_fields Form Fields.
* @param int $user_id User ID.
*/
function user_registration_validate_edit_profile_form_field_data( $data, $form_data, $form_id, $form_field_data, $form_fields ) {
function user_registration_validate_edit_profile_form_field_data( $data, $form_data, $form_id, $form_field_data, $form_fields, $user_id ) {
$form_validator = new UR_Form_Validation();
$skippable_fields = $form_validator->get_update_profile_validation_skippable_fields( $form_field_data );
$form_key_list = wp_list_pluck( wp_list_pluck( $form_field_data, 'general_setting' ), 'field_name' );
Expand Down Expand Up @@ -5140,9 +5141,12 @@ function user_registration_validate_edit_profile_form_field_data( $data, $form_d
}

if ( 'user_email' === $single_form_field->field_key ) {

// Do not allow admin to update others email, case may change in future
if ( !email_exists( sanitize_text_field( wp_unslash( $single_field_value ) ) ) && $user_id !== get_current_user_id() ) {
ur_add_notice( esc_html__( 'Email field is not editable.', 'user-registration' ), 'error' );
}
// Check if email already exists before updating user details.
if ( email_exists( sanitize_text_field( wp_unslash( $single_field_value ) ) ) && email_exists( sanitize_text_field( wp_unslash( $single_field_value ) ) ) !== get_current_user_id() ) {
if ( email_exists( sanitize_text_field( wp_unslash( $single_field_value ) ) ) && email_exists( sanitize_text_field( wp_unslash( $single_field_value ) ) ) !== $user_id ) {
ur_add_notice( esc_html__( 'Email already exists', 'user-registration' ), 'error' );
}
}
Expand Down Expand Up @@ -5173,9 +5177,10 @@ function user_registration_validate_edit_profile_form_field_data( $data, $form_d
*/
function user_registration_edit_profile_row_template( $data, $profile, $current_row = '', $row_count = '' ) {

$user_id = get_current_user_id();
$user_id = !empty($_REQUEST['user_id']) ? absint($_REQUEST['user_id']) : get_current_user_id();
$form_id = ur_get_form_id_by_userid( $user_id );
$width = floor( 100 / count( $data ) ) - count( $data );
$is_edit = isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'edit' && $user_id !== get_current_user_id();

foreach ( $data as $grid_key => $grid_data ) {
$found_field = false;
Expand Down Expand Up @@ -5209,7 +5214,6 @@ function user_registration_edit_profile_row_template( $data, $profile, $current_
$key = 'user_registration_' . $single_item->general_setting->field_name;

if ( $found_field ) {
$user_id = get_current_user_id();
$form_id = ur_get_form_id_by_userid( $user_id );
$field = isset( $profile[ $key ] ) ? $profile[ $key ] : array();

Expand Down Expand Up @@ -5258,7 +5262,9 @@ function user_registration_edit_profile_row_template( $data, $profile, $current_
<div class="ur-field-item field-<?php echo esc_attr( $single_item->field_key );?> <?php echo esc_attr( ! empty( $single_item->advance_setting->custom_class ) ? $single_item->advance_setting->custom_class : '' ); ?>" <?php echo $cl_props; //PHPCS:ignore?> data-field-id="<?php echo esc_attr( $field_id ); ?>" data-ref-id="<?php echo esc_attr( $key ); ?>">
<?php
$readonly_fields = ur_readonly_profile_details_fields();

if ( $is_edit ) {
unset( $readonly_fields['user_pass'] );
}
if ( isset( $field['field_key'] ) && array_key_exists( $field['field_key'], $readonly_fields ) ) {
$field['custom_attributes']['readonly'] = 'readonly';
if ( isset( $readonly_fields[ $field['field_key'] ] ['value'] ) ) {
Expand Down Expand Up @@ -5521,8 +5527,9 @@ function user_registration_edit_profile_row_template( $data, $profile, $current_
<?php
}
}

echo apply_filters( 'user_registration_frontend_form_row_end', '', $form_id, $current_row ); // phpcs:ignore
if( !$is_edit ) {
echo apply_filters( 'user_registration_frontend_form_row_end', '', $form_id, $current_row ); // phpcs:ignore
}
}
}

Expand Down
Loading
Loading