File: //proc/self/cwd/wp-content/plugins/luman-plus/assets/js/custom/register-form-control.js
/**
* کنترل فیلدهای فرم ثبتنام بر اساس تنظیمات
*/
(function($) {
'use strict';
function applyRegisterFormSettings() {
if (typeof lumanRegisterSettings === 'undefined') {
return;
}
const settings = lumanRegisterSettings;
// پیدا کردن فیلدهای فرم ثبتنام با selector های مختلف (با توجه به ID های استفاده شده در template)
const $emailField = $('#luman-email, input[name="user_email"], input[name="user-email"], input[type="email"][name*="email"], #user_email, .user-email-field');
const $passwordField = $('#luman-password, input[name="user_password"], input[name="user-password"], input[type="password"][name*="password"], #user_password, .user-password-field');
// پیدا کردن wrapper ها (div های parent)
const $emailWrapper = $emailField.closest('.luman-input-group');
const $passwordWrapper = $passwordField.closest('.luman-input-group');
// پیدا کردن container های password strength (که باید مخفی شوند)
const $passwordStrengthContainer = $passwordField.closest('#luman-page-register').find('.luman-password-strength-container');
// مدیریت فیلد ایمیل
if ($emailField.length > 0) {
if (settings.email_field === 'hidden') {
// مخفی کردن کامل فیلد ایمیل و wrapper آن
if ($emailWrapper.length > 0) {
$emailWrapper.hide();
} else {
$emailField.hide();
}
$emailField.removeAttr('required');
$emailField.removeClass('required');
$emailField.prop('required', false);
// حذف required از native HTML
$emailField.each(function() {
this.removeAttribute('required');
this.required = false;
});
} else if (settings.email_field === 'optional') {
// نمایش فیلد اما غیراجباری
if ($emailWrapper.length > 0) {
$emailWrapper.show();
} else {
$emailField.show();
}
$emailField.removeAttr('required');
$emailField.removeClass('required');
$emailField.prop('required', false);
$emailField.attr('data-optional', 'true');
// حذف required از native HTML
$emailField.each(function() {
this.removeAttribute('required');
this.required = false;
});
// تغییر placeholder
$emailField.each(function() {
const $field = $(this);
const currentPlaceholder = $field.attr('placeholder') || '';
if (currentPlaceholder && !currentPlaceholder.includes('اختیاری') && !currentPlaceholder.includes('optional')) {
$field.attr('placeholder', currentPlaceholder.replace(/^(.+)$/, '$1 (اختیاری)'));
} else if (!currentPlaceholder) {
$field.attr('placeholder', 'ایمیل (اختیاری)');
}
});
} else if (settings.email_field === 'required') {
// اجباری کردن فیلد ایمیل
if ($emailWrapper.length > 0) {
$emailWrapper.show();
} else {
$emailField.show();
}
$emailField.attr('required', 'required');
$emailField.addClass('required');
$emailField.prop('required', true);
}
}
// مدیریت فیلد رمز عبور
if ($passwordField.length > 0) {
if (settings.password_field === 'hidden') {
// مخفی کردن کامل فیلد رمز عبور و wrapper آن
if ($passwordWrapper.length > 0) {
$passwordWrapper.hide();
} else {
$passwordField.hide();
}
// مخفی کردن container امنیت پسورد
if ($passwordStrengthContainer.length > 0) {
$passwordStrengthContainer.hide();
}
$passwordField.removeAttr('required');
$passwordField.removeClass('required');
$passwordField.prop('required', false);
// حذف required از native HTML
$passwordField.each(function() {
this.removeAttribute('required');
this.required = false;
});
} else if (settings.password_field === 'show') {
// نمایش و اجباری کردن فیلد رمز عبور
if ($passwordWrapper.length > 0) {
$passwordWrapper.show();
} else {
$passwordField.show();
}
if ($passwordStrengthContainer.length > 0) {
$passwordStrengthContainer.show();
}
$passwordField.attr('required', 'required');
$passwordField.addClass('required');
$passwordField.prop('required', true);
}
}
}
// اجرا در DOM ready
$(document).ready(function() {
applyRegisterFormSettings();
// در صورت تغییرات دینامیک در DOM، دوباره اجرا شود
setTimeout(applyRegisterFormSettings, 500);
setTimeout(applyRegisterFormSettings, 1000);
});
// برای صفحاتی که با AJAX محتوا لود میشود
$(document).on('DOMNodeInserted', function() {
setTimeout(applyRegisterFormSettings, 100);
});
// مدیریت submit فرم - حذف required قبل از ارسال
$(document).on('submit', 'form', function(e) {
if (typeof lumanRegisterSettings === 'undefined') {
return;
}
const settings = lumanRegisterSettings;
const $form = $(this);
// بررسی اینکه آیا این فرم ثبتنام است
const $emailField = $form.find('input[name="user_email"], input[name="user-email"]');
const $passwordField = $form.find('input[name="user_password"], input[name="user-password"]');
if ($emailField.length > 0 || $passwordField.length > 0) {
// اگر فیلد ایمیل مخفی است یا اختیاری است
if (settings.email_field === 'hidden' || settings.email_field === 'optional') {
$emailField.removeAttr('required');
$emailField.prop('required', false);
$emailField.each(function() {
this.removeAttribute('required');
this.required = false;
});
}
// اگر فیلد رمز عبور مخفی است
if (settings.password_field === 'hidden') {
$passwordField.removeAttr('required');
$passwordField.prop('required', false);
$passwordField.each(function() {
this.removeAttribute('required');
this.required = false;
});
}
}
});
// Override validation در script.js اصلی (باید بعد از لود script.js اصلی اجرا شود)
function overrideRegisterValidation() {
if (typeof lumanRegisterSettings === 'undefined') {
return;
}
const settings = lumanRegisterSettings;
// حذف event handler قبلی (اگر وجود دارد)
$('#luman-btn-register-submit').off('click');
// اضافه کردن event handler جدید
$('#luman-btn-register-submit').on('click', function(e) {
const $form = $(".luman-form-card");
$form.addClass("loading");
const fullName = $("#luman-full-name").val().trim();
const email = $("#luman-email").val();
const password = $("#luman-password").val();
const nameParts = fullName.split(" ");
const firstName = nameParts[0] ?? "";
const lastName = nameParts.slice(1).join(" ") ?? "";
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// Validation اصلاح شده بر اساس تنظیمات
if (!firstName || !lastName) {
toastr.error("لطفا نام و نام خانوادگی را وارد کنید.");
$form.removeClass("loading");
return;
}
// بررسی ایمیل فقط اگر فیلد hidden نیست
if (settings.email_field !== 'hidden') {
// اگر required است، باید پر شود
if (settings.email_field === 'required' && !email) {
toastr.error("لطفا ایمیل را وارد کنید.");
$form.removeClass("loading");
return;
}
// اگر optional است و پر شده، باید معتبر باشد
if (email && !emailRegex.test(email)) {
toastr.warning("ایمیل وارد شده معتبر نیست.");
$form.removeClass("loading");
return;
}
}
// بررسی پسورد فقط اگر فیلد hidden نیست
if (settings.password_field !== 'hidden' && !password) {
toastr.error("لطفا رمز عبور را وارد کنید.");
$form.removeClass("loading");
return;
}
// ارسال داده به سرور
$.ajax({
url: luman_ajax_object.ajaxurl,
type: "POST",
data: {
action: "luman_plus_register_user",
first_name: firstName,
last_name: lastName,
user_email: email || '', // اگر hidden یا optional باشد، خالی بفرست
user_password: password || '', // اگر hidden باشد، خالی بفرست
},
success: function (response) {
$form.removeClass("loading");
if (response.success) {
toastr.success(response.data.message);
setTimeout(() => {
window.location.href = response.data.redirect;
}, 1500);
}
if (!response.success) {
toastr.warning(response.data);
}
},
error: function () {
$form.removeClass("loading");
toastr.error("خطا در ارتباط با سرور.");
},
});
});
}
// اجرای override با تاخیر برای اطمینان از لود script.js اصلی
function initOverride() {
// چند بار تلاش برای override کردن
setTimeout(overrideRegisterValidation, 100);
setTimeout(overrideRegisterValidation, 500);
setTimeout(overrideRegisterValidation, 1000);
}
$(document).ready(function() {
initOverride();
});
// همچنین بعد از لود کامل صفحه (بعد از همه scripts با defer)
$(window).on('load', function() {
setTimeout(overrideRegisterValidation, 200);
setTimeout(overrideRegisterValidation, 500);
});
// استفاده از MutationObserver برای شناسایی زمانی که صفحه register نمایش داده میشود
if (typeof MutationObserver !== 'undefined') {
const observer = new MutationObserver(function(mutations) {
const $registerPage = $('#luman-page-register');
if ($registerPage.length > 0 && $registerPage.is(':visible')) {
// صفحه register نمایش داده شد، validation را override کن
setTimeout(overrideRegisterValidation, 50);
}
});
$(document).ready(function() {
const targetNode = document.querySelector('.luman-form-card') || document.body;
if (targetNode) {
observer.observe(targetNode, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['style', 'class']
});
}
});
}
})(jQuery);