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-573 Feature - Pattern validation for input fields #588

Merged
merged 4 commits into from
Aug 29, 2023
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
4 changes: 2 additions & 2 deletions assets/css/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3172,11 +3172,11 @@ body {
box-shadow: inset 0 1px 2px rgba(35, 40, 45, 0.1);
}

.ur-advance-setting.ur-advance-default_value, .ur-general-setting.ur-general-setting-hidden-value {
.ur-advance-setting.ur-advance-default_value, .ur-general-setting.ur-general-setting-hidden-value, .ur-advance-setting.ur-advance-pattern_value {
position: relative;
margin-bottom: 15px;

& input.ur_advance_setting.ur-settings-default-value, & input.ur-general-setting-field {
& input.ur_advance_setting.ur-settings-default-value, & input.ur-general-setting-field, & input.ur_advance_setting.ur-settings-pattern_value {
width: calc(100% - 40px) !important;
min-height: 27px;
}
Expand Down
61 changes: 54 additions & 7 deletions assets/js/admin/form-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3467,6 +3467,30 @@
.toggle();
});
break;
case "enable_pattern" :
if (!$this_node.is(":checked")) {
$(this)
.closest(".ur-advance-setting-block")
.find(".ur-advance-pattern_value")
.hide();
$(this)
.closest(".ur-advance-setting-block")
.find(".ur-advance-pattern_message")
.hide();
}

$this_node.on("change", function () {
$(this)
.closest(".ur-advance-setting-block")
.find(".ur-advance-pattern_value")
.toggle();

$(this)
.closest(".ur-advance-setting-block")
.find(".ur-advance-pattern_message")
.toggle();
});
break;
}
var node_type = $this_node.get(0).tagName.toLowerCase();

Expand Down Expand Up @@ -4460,8 +4484,12 @@
.find("input")
.val();
smart_tag = $(this).data("key");
input_value += smart_tag;
update_input(input_value);
input_value = smart_tag;
var inputElement = $(this).parent().parent().parent().find("input"),
advanceFieldData = inputElement.data("advance-field"),
fieldData = inputElement.data("field"),
field_name = advanceFieldData !== undefined ? advanceFieldData : fieldData;
update_input(field_name,input_value);

$(this).parent().parent().parent().find("input").val(input_value);
$(document.body).find(".ur-smart-tags-list").hide();
Expand All @@ -4472,6 +4500,7 @@
".ur_advance_setting.ur-settings-default-value",
function () {
input_value = $(this).val();
field_name = $(this).data("advance-field");
update_input(input_value);
}
);
Expand All @@ -4480,14 +4509,25 @@
".ur-general-setting.ur-general-setting-hidden-value input",
function () {
input_value = $(this).val();
field_name = $(this).data("field");
update_input(input_value);
}
);

$(document.body).on(
"change",
".ur_advance_setting.ur-settings-pattern_value",
function () {
input_value = $(this).val();
field_name = $(this).data("advance-field");
update_input(input_value);
}
);

/**
* For update the default value.
*/
function update_input(input_value) {
function update_input(field_name,input_value) {
active_field = $(".ur-item-active");
target_input_field = $(active_field).find(
".user-registration-field-option-group.ur-advance-setting-block"
Expand All @@ -4496,10 +4536,10 @@
".ur-advance-setting.ur-advance-default_value"
);
target_input = $(ur_toggle_content).find(
"input[data-id=text_advance_setting_default_value]"
'input[data-advance-field="' + field_name + '"]'
);
target_textarea = $(ur_toggle_content).find(
"input[data-id=textarea_advance_setting_default_value]"
'input[data-advance-field="' + field_name + '"]'
);

target_input_hidden_field = $(active_field).find(
Expand All @@ -4509,12 +4549,19 @@
".ur-general-setting.ur-general-setting-hidden-value"
);
target_hidden_input = $(ur_toggle_hidden_content).find(
'input[data-field="hidden_value"]'
'input[data-field="' + field_name + '"]'
);
// pattern value
ur_toggle_pattern_content = target_input_field.find(
".ur-advance-setting.ur-advance-pattern_value"
);
target_pattern_input = $(ur_toggle_pattern_content).find(
'input[data-advance-field="' + field_name + '"]'
);

target_input.val(input_value);
target_textarea.val(input_value);
target_hidden_input.val(input_value);
target_pattern_input.val(input_value);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions includes/abstracts/abstract-ur-field-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public function render_html( $fields ) {
$smart_tags = apply_filters( 'ur_smart_tags_list_in_general', $smart_tags );
}

$pattern_validationList = '';
if ( 'pattern_value' === $field_key ) {
$pattern_validationList = apply_filters( 'ur_pattern_validation_list_in_advanced_settings', $pattern_validationList );
}

$this->fields_html .= '<div class="ur-advance-setting ur-advance-' . esc_attr( $field_key ) . '">';

if ( 'toggle' !== $field['type'] ) {
Expand Down Expand Up @@ -121,6 +126,7 @@ public function render_html( $fields ) {

$this->fields_html .= ' />';
$this->fields_html .= $smart_tags;
$this->fields_html .= $pattern_validationList;
break;

case 'select':
Expand Down
15 changes: 9 additions & 6 deletions includes/class-ur-emailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,10 @@ public static function send_mail_to_user( $email, $username, $user_id, $data_htm
*/
public static function send_mail_to_admin( $user_email, $username, $user_id, $data_html, $name_value, $attachments, $template_id ) {

$header = "Reply-To: {{email}} \r\n";
$header .= 'Content-Type: text/html; charset=UTF-8';
$header = array(
'Reply-To:' . $user_email . '\r\n',
'Content-Type: text/html; charset=UTF-8'
);

$attachment = isset( $attachments['admin'] ) ? $attachments['admin'] : '';
$admin_email = get_option( 'user_registration_admin_email_receipents', get_option( 'admin_email' ) );
Expand Down Expand Up @@ -387,7 +389,6 @@ public static function send_mail_to_admin( $user_email, $username, $user_id, $da
list( $message, $subject ) = user_registration_email_content_overrider( ur_get_form_id_by_userid( $user_id ), $settings, $message, $subject );
$message = self::parse_smart_tags( $message, $values, $name_value );
$subject = self::parse_smart_tags( $subject, $values, $name_value );
$header = self::parse_smart_tags( $header, $values, $name_value );

if ( ur_option_checked( 'user_registration_enable_admin_email', true ) ) {
foreach ( $admin_email as $email ) {
Expand Down Expand Up @@ -523,8 +524,10 @@ public static function lost_password_email( $user_login, $user_data, $key ) {
*/
public static function send_profile_changed_email_to_admin( $user_email, $username, $user_id, $data_html, $name_value, $attachments ) {

$header = "Reply-To: {{email}} \r\n";
$header .= 'Content-Type: text/html; charset=UTF-8';
$header = array(
'Reply-To:' . $user_email . '\r\n',
'Content-Type: text/html; charset=UTF-8'
);

$attachment = isset( $attachments['admin'] ) ? $attachments['admin'] : '';
$admin_email = get_option( 'user_registration_edit_profile_email_receipents', get_option( 'admin_email' ) );
Expand All @@ -547,7 +550,7 @@ public static function send_profile_changed_email_to_admin( $user_email, $userna
list( $message, $subject ) = user_registration_email_content_overrider( $form_id, $settings, $message, $subject );
$message = self::parse_smart_tags( $message, $values, $name_value );
$subject = self::parse_smart_tags( $subject, $values, $name_value );
$header = self::parse_smart_tags( $header, $values, $name_value );


if ( ur_option_checked( 'user_registration_enable_profile_details_changed_email', true ) ) {
foreach ( $admin_email as $email ) {
Expand Down
63 changes: 63 additions & 0 deletions includes/class-ur-smart-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class UR_Smart_Tags {
public function __construct() {
add_filter( 'user_registration_process_smart_tags', array( $this, 'process' ), 10, 3 );
add_filter( 'ur_smart_tags_list_in_general', array( $this, 'select_smart_tags_in_general' ), 10, 1 );
add_filter( 'ur_pattern_validation_list_in_advanced_settings', array( $this, 'select_pattern_validation' ), 10, 1 );
}

/**
Expand Down Expand Up @@ -356,6 +357,68 @@ public function select_smart_tags_in_general( $smart_tags ) {
$smart_tags .= '</ul></div>';
return $smart_tags;
}

/**
* List of Pattern which can checked against.
*
* @return array array of pattern lists.
*/
public static function ur_pattern_validation_lists() {
$pattern_lists = apply_filters(
'user_registration_pattern_validation_lists',
array(
'^[a-zA-Z]+$' => __( 'Alpha', 'user-registration' ),
'^[a-zA-Z0-9]+$' => __( 'Alphanumeric', 'user-registration' ),
'^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$' => __( 'Color', 'user-registration' ),
'^[A-Za-z]{2}$' => __( 'Country Code (2 Character)', 'user-registration' ),
'^[A-Za-z]{3}$' => __( 'Country Code (3 Character)', 'user-registration' ),
'^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])$' => __( 'Date (mm/dd)', 'user-registration' ),
'^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])$' => __( 'Date (dd/mm)', 'user-registration' ),
'^(0[1-9]|1[0-2])\.(0[1-9]|1\d|2\d|3[01])\.\d{4}$' => __( 'Date (mm.dd.yyyy)', 'user-registration' ),
'^(0[1-9]|1\d|2\d|3[01])\.(0[1-9]|1[0-2])\.\d{4}$' => __( 'Date (dd.mm.yyyy)', 'user-registration' ),
'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|3[01])$' => __( 'Date (yyyy-mm-dd)', 'user-registration' ),
'^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/\d{4}$' => __( 'Date (mm/dd/yyyy)', 'user-registration' ),
'^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$' => __( 'Date (dd/mm/yyyy)', 'user-registration' ),
'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$' => __( 'Email', 'user-registration' ),
'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' => __( 'IP (Version 4)', 'user-registration' ),
'((^|:)([0-9a-fA-F]{0,4})){1,8}$' => __( 'IP (Version 6)', 'user-registration' ),
'^978(?:-[\d]+){3}-[\d]$' => __( 'ISBN', 'user-registration' ),
'-?\d{1,3}\.\d+' => __( 'Latitude or Longitude', 'user-registration' ),
'^[0-9]+$' => __( 'Numeric', 'user-registration' ),
'^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$' => __( 'Password (Numeric, lower, upper)', 'user-registration' ),
'(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}' => __( 'Password (Numeric, lower, upper, min 8)', 'user-registration' ),
'[0-9+()-. ]+' => __( 'Phone - General', 'user-registration' ),
'^\+44\d{10}$' => __( 'Phone - UK', 'user-registration' ),
'\d{3}[\-]\d{3}[\-]\d{4}' => __( 'Phone - US: 123-456-7890', 'user-registration' ),
'\([0-9]{3}\)[0-9]{3}-[0-9]{4}' => __( 'Phone - US: (123)456-7890', 'user-registration' ),
'(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}' => __( 'Phone - US: Flexible', 'user-registration' ),
'^[A-Za-z]{1,2}\d{1,2}[A-Za-z]?\s?\d[A-Za-z]{2}$' => __( 'Postal Code (UK)', 'user-registration' ),
'\d+(\.\d{2})?$' => __( 'Price (1.23)', 'user-registration' ),
'^[a-zA-Z0-9-]+$' => __( 'Slug', 'user-registration' ),
'(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){2}' => __( 'Time (hh:mm:ss)', 'user-registration' ),
'^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$' => __( 'URL', 'user-registration' ),
'(\d{5}([\-]\d{4})?)' => __( 'Zip Code', 'user-registration' ),
)
);
return $pattern_lists;
}

/**
* Smart tag list button in general setting and advanced settin of field.
*
* @param string $smart_tags list of smart tags.
*/
public function select_pattern_validation( $pattern_lists ) {
$pattern_validation_list = self::ur_pattern_validation_lists();
$pattern_lists .= '<a href="#" class="button ur-smart-tags-list-button"><span class="dashicons dashicons-editor-code"></span></a>';
$pattern_lists .= '<div class="ur-smart-tags-list" style="display: none">';
$pattern_lists .= '<div class="smart-tag-title ur-smart-tag-title">Regular Expression</div><ul class="ur-smart-tags">';
foreach ( $pattern_validation_list as $key => $value ) {
$pattern_lists .= '<li class="ur-select-smart-tag" data-key = "' . esc_attr( $key ) . '">' . esc_html( $value ) . '</li>';
}
$pattern_lists .= '</ul></div>';
return $pattern_lists;
}
}

new UR_Smart_Tags();
Loading