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-749 Dev - Merge two duplicate methods on form validation #590

Merged
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
2 changes: 1 addition & 1 deletion .github/sync-file-list.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wpeverest/user-registration-pro@master:
wpeverest/user-registration-pro@UR-749-merge-two-duplicate-methods-on-ur-form-validation:
- assets
- includes
- src
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Sync Files To Pro
on:
push:
branches:
- master
- UR-749-merge-two-duplicate-methods-on-ur-form-validation
workflow_dispatch:
jobs:
sync:
Expand Down
30 changes: 1 addition & 29 deletions includes/class-ur-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,40 +319,12 @@ public static function update_profile_details() {
$single_field[ $key ] = isset( $single_field[ $key ] ) ? $single_field[ $key ] : '';
break;
}

// Hook to allow modification of value.
$single_field[ $key ] = apply_filters( 'user_registration_process_myaccount_field_' . $key, $single_field[ $key ] );

if ( 'email' === $field['type'] ) {
do_action( 'user_registration_validate_email_whitelist', $single_field[ $key ], '', $single_field, $form_id );
}

if ( 'user_registration_user_email' === $key ) {
// Check if email already exists before updating user details.
if ( email_exists( $single_field[ $key ] ) && email_exists( $single_field[ $key ] ) !== $user_id ) {
wp_send_json_error(
array(
'message' => __( 'Email already exists.', 'user-registration' ),
)
);
}
}

$disabled = false;
if ( isset( $field['custom_attributes'] ) && isset( $field['custom_attributes']['readonly'] ) && isset( $field['custom_attributes']['disabled'] ) ) {
if ( 'readonly' === $field['custom_attributes']['readonly'] || 'disabled' === $field['custom_attributes']['disabled'] ) {
$disabled = true;
}
}

// Action to add extra validation to edit profile fields.
do_action( 'user_registration_validate_' . $key, $single_field[ $key ] );
}

/**
* Hook to perform validation of edit profile form.
*/
do_action( 'user_registration_validate_profile_update_ajax', $profile, $form_data, $form_id );
do_action( 'user_registration_validate_profile_update', $profile, $form_data, $form_id );

do_action( 'user_registration_after_save_profile_validation', $user_id, $profile );

Expand Down
85 changes: 83 additions & 2 deletions includes/class-ur-form-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ public static function save_profile_details() {
$form_id = $form_id_array[0];
}

$profile = user_registration_form_data( $user_id, $form_id );
$profile = user_registration_form_data( $user_id, $form_id );
$form_data = self::get_form_data_from_post( $form_id );

do_action( 'user_registration_validate_profile_update_post', $profile, $form_id );
do_action( 'user_registration_validate_profile_update', $profile, $form_data, $form_id );

do_action( 'user_registration_after_save_profile_validation', $user_id, $profile );

Expand Down Expand Up @@ -160,6 +161,86 @@ public static function save_profile_details() {
}
}

/**
* This format returns form field data in object format.
*
* In Non-ajax method of update profile, form data is received in key => value format
* which is different from the data received while using ajax submission.
*
* So, to maintain consistency of form data object while passing to different functions,
* data is formatted properly.
*
* @param [int] $form_id Form Id.
* @return array
*/
public static function get_form_data_from_post( $form_id ) {

$form_field_data = ur_get_form_field_data( $form_id );

$fields = array();

foreach ( $form_field_data as $field ) {
$field_name = $field->general_setting->field_name;
$key = 'user_registration_' . $field_name;

$field_obj = new StdClass();
$field_obj->field_name = $field_name;

$value = '';

switch ( $field->field_key ) {
case 'checkbox':
if ( isset( $_POST[ $key ] ) && is_array( $_POST[ $key ] ) ) {
$value = wp_unslash( $_POST[ $key ] ); // phpcs:ignore
} else {
$value = (int) isset( $_POST[ $key ] );
}
break;

case 'wysiwyg':
if ( isset( $_POST[ $key ] ) ) {
$value = sanitize_text_field( htmlentities( wp_unslash( $_POST[ $key ] ) ) ); // phpcs:ignore
} else {
$value = '';
}
break;

case 'email':
if ( isset( $_POST[ $key ] ) ) {
$value = sanitize_email( wp_unslash( $_POST[ $key ] ) );
} else {
$user_data = get_userdata( $user_id );
$value = $user_data->data->user_email;
}
break;
case 'profile_picture':
if ( isset( $_POST['profile_pic_url'] ) ) {
$value = sanitize_text_field( wp_unslash( $_POST['profile_pic_url'] ) );
} else {
$value = '';
}
break;

default:
$value = isset( $_POST[ $key ] ) ? $_POST[ $key ] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
break;
}

$field_obj->value = ur_clean( $value );

if ( isset( $field->field_key ) ) {
$field_obj->field_type = $field->field_key;
}

if ( isset( $field->general_setting->label ) ) {
$field_obj->label = $field->general_setting->label;
}

$fields[ $field_name ] = $field_obj;
}
return $fields;
}

/**
* Send confirmation email.
*
Expand Down
67 changes: 67 additions & 0 deletions includes/functions-ur-form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* UserRegistration Form Functions
*
* Functions related to forms.
*
* @package UserRegistration/Functions
* @version 1.0.0
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

function ur_get_form_fields( $form_id ) {
$form_id = (int) $form_id;

$form_fields = array();

if ( ! empty( $form_id ) ) {
$post_content_array = ( $form_id ) ? UR()->form->get_form( $form_id, array( 'content_only' => true ) ) : array();

foreach ( $post_content_array as $row_index => $row ) {
foreach ( $row as $grid_index => $grid ) {
foreach ( $grid as $field_index => $field ) {
$field_name = $field->general_setting->field_name;

if ( $field_name ) {
$form_fields[ $field_name ] = $field;
}
}
}
}
}

return $form_fields;
}

function ur_get_form_field_keys( $form_id ) {
$form_fields = ur_get_form_fields( $form_id );

$field_keys = array();

if ( ! empty( $form_fields ) && is_array( $form_fields )) {
$field_keys = array_keys( $form_fields );
}

return $field_keys;
}

/**
* Returns settings for all form fields in proper array format.
*
* Uses UR_FrontEnd_Form_Handler::get_form_field_data() function.
*
* @param integer $form_id Form Id.
* @return array
*/
function ur_get_form_field_data( $form_id = 0 ) {

$post_content_array = ( $form_id ) ? UR()->form->get_form( $form_id, array( 'content_only' => true ) ) : array();

$form_field_data = UR_Frontend_Form_Handler::get_form_field_data( $post_content_array );

return $form_field_data;
}

Loading
Loading