Skip to content

Commit

Permalink
Tweak - Hide cloudflare turnstile test wrapper when Theme of cloudfla…
Browse files Browse the repository at this point in the history
…re changed
  • Loading branch information
iamprazol committed Sep 27, 2023
1 parent 53a2e2d commit c89b4f8
Showing 1 changed file with 149 additions and 115 deletions.
264 changes: 149 additions & 115 deletions assets/js/admin/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,17 @@
)
.closest(".user-registration-global-settings")
.hide();
$("#user_registration_captcha_setting_recaptcha_site_key_cloudflare")
$(
"#user_registration_captcha_setting_recaptcha_site_key_cloudflare"
)
.closest(".user-registration-global-settings")
.hide();
$(
"#user_registration_captcha_setting_recaptcha_site_secret_cloudflare"
)
.closest(".user-registration-global-settings")
.hide();
$(
"#user_registration_captcha_setting_recaptcha_cloudflare_theme"
)
$("#user_registration_captcha_setting_recaptcha_cloudflare_theme")
.closest(".user-registration-global-settings")
.hide();
} else if (value == "hCaptcha") {
Expand Down Expand Up @@ -274,34 +274,34 @@
$("#user_registration_captcha_setting_invisible_recaptcha_v2")
.closest(".user-registration-global-settings")
.hide();
$("#user_registration_captcha_setting_recaptcha_site_key_cloudflare")
$(
"#user_registration_captcha_setting_recaptcha_site_key_cloudflare"
)
.closest(".user-registration-global-settings")
.hide();
$(
"#user_registration_captcha_setting_recaptcha_site_secret_cloudflare"
)
.closest(".user-registration-global-settings")
.hide();
$(
"#user_registration_captcha_setting_recaptcha_cloudflare_theme"
)
$("#user_registration_captcha_setting_recaptcha_cloudflare_theme")
.closest(".user-registration-global-settings")
.hide();
} else if (value == "cloudflare") {
$("#user_registration_captcha_setting_recaptcha_site_key_cloudflare")
$(
"#user_registration_captcha_setting_recaptcha_site_key_cloudflare"
)
.closest(".user-registration-global-settings")
.show();
$(
"#user_registration_captcha_setting_recaptcha_site_secret_cloudflare"
)
.closest(".user-registration-global-settings")
.show();
$(
"#user_registration_captcha_setting_recaptcha_cloudflare_theme"
)
$("#user_registration_captcha_setting_recaptcha_cloudflare_theme")
.closest(".user-registration-global-settings")
.show();
$("#user_registration_captcha_setting_recaptcha_site_key_hcaptcha")
$("#user_registration_captcha_setting_recaptcha_site_key_hcaptcha")
.closest(".user-registration-global-settings")
.hide();
$(
Expand Down Expand Up @@ -354,7 +354,9 @@
$("#user_registration_captcha_setting_recaptcha_site_secret")
.closest(".user-registration-global-settings")
.hide();
$("#user_registration_captcha_setting_recaptcha_site_key_cloudflare")
$(
"#user_registration_captcha_setting_recaptcha_site_key_cloudflare"
)
.closest(".user-registration-global-settings")
.hide();
$(
Expand Down Expand Up @@ -414,17 +416,17 @@
$("#user_registration_captcha_setting_recaptcha_site_secret_v3")
.closest(".user-registration-global-settings")
.hide();
$("#user_registration_captcha_setting_recaptcha_site_key_cloudflare")
$(
"#user_registration_captcha_setting_recaptcha_site_key_cloudflare"
)
.closest(".user-registration-global-settings")
.hide();
$(
"#user_registration_captcha_setting_recaptcha_site_secret_cloudflare"
)
.closest(".user-registration-global-settings")
.hide();
$(
"#user_registration_captcha_setting_recaptcha_cloudflare_theme"
)
$("#user_registration_captcha_setting_recaptcha_cloudflare_theme")
.closest(".user-registration-global-settings")
.hide();
$("#user_registration_captcha_setting_invisible_recaptcha_v2")
Expand All @@ -435,139 +437,171 @@

var captchaSettingsChanged = false;

$('.user-registration-global-settings').find('input').on('change', function() {
captchaSettingsChanged = true;
$( '#user_registration_captcha_setting_captcha_test' ).parent().hide();
});
$(".user-registration-global-settings")
.find("input, select")
.on("change", function () {
captchaSettingsChanged = true;
$("#user_registration_captcha_setting_captcha_test")
.parent()
.hide();
});

/**
* Test Captcha from settings page.
*/
$('.user-registration').on('click', '#user_registration_captcha_setting_captcha_test', function (e) {
e.preventDefault();
e.stopPropagation();
$(".user-registration").on(
"click",
"#user_registration_captcha_setting_captcha_test",
function (e) {
e.preventDefault();
e.stopPropagation();

if (captchaSettingsChanged) {
alert(user_registration_settings_params.i18n.unsaved_changes);
return;
}
if (captchaSettingsChanged) {
alert(user_registration_settings_params.i18n.unsaved_changes);
return;
}

var spinner = $('#user_registration_captcha_setting_captcha_test .spinner');
spinner.show();
var spinner = $(
"#user_registration_captcha_setting_captcha_test .spinner"
);
spinner.show();

setTimeout(
function () {
setTimeout(function () {
spinner.hide();
},
2500
);
}, 2500);

var ur_recaptcha_node = $("#ur-captcha-node");
if (
"undefined" !== typeof ur_recaptcha_code &&
ur_recaptcha_code.site_key.length
) {
if (ur_recaptcha_node.length !== 0) {
switch (ur_recaptcha_code.version) {
case 'v2':
google_recaptcha_login = grecaptcha.render(
ur_recaptcha_node.find(".g-recaptcha").attr("id"),
{
sitekey: ur_recaptcha_code.site_key,
theme: "light",
style: "transform:scale(0.77);-webkit-transform:scale(0.77);transform-origin:0 0;-webkit-transform-origin:0 0;",
}
);

if (ur_recaptcha_code.is_invisible) {
grecaptcha
.execute(google_recaptcha_login)
.then(function (token) {
if (null !== token) {
display_captcha_test_status(user_registration_settings_params.i18n.captcha_failed, 'error');
return;
} else {
display_captcha_test_status(user_registration_settings_params.i18n.captcha_success, 'success');
}
});
}
break;

case 'v3':
try {
grecaptcha.execute(
ur_recaptcha_code.site_key,
var ur_recaptcha_node = $("#ur-captcha-node");
if (
"undefined" !== typeof ur_recaptcha_code &&
ur_recaptcha_code.site_key.length
) {
if (ur_recaptcha_node.length !== 0) {
switch (ur_recaptcha_code.version) {
case "v2":
google_recaptcha_login = grecaptcha.render(
ur_recaptcha_node
.find(".g-recaptcha")
.attr("id"),
{
action: 'click'
sitekey: ur_recaptcha_code.site_key,
theme: "light",
style: "transform:scale(0.77);-webkit-transform:scale(0.77);transform-origin:0 0;-webkit-transform-origin:0 0;",
}
).then(function (d) {
display_captcha_test_status(user_registration_settings_params.i18n.captcha_success, 'success');
});
} catch (err) {
display_captcha_test_status(err.message, 'error');
}
break;

case 'hCaptcha':
google_recaptcha_login = hcaptcha.render(
ur_recaptcha_node
.find(".g-recaptcha-hcaptcha")
.attr("id"),
{
sitekey: ur_recaptcha_code.site_key,
theme: "light",
style: "transform:scale(0.77);-webkit-transform:scale(0.77);transform-origin:0 0;-webkit-transform-origin:0 0;",
);

if (ur_recaptcha_code.is_invisible) {
grecaptcha
.execute(google_recaptcha_login)
.then(function (token) {
if (null !== token) {
display_captcha_test_status(
user_registration_settings_params
.i18n.captcha_failed,
"error"
);
return;
} else {
display_captcha_test_status(
user_registration_settings_params
.i18n.captcha_success,
"success"
);
}
});
}
)
break;
break;

case "v3":
try {
grecaptcha
.execute(ur_recaptcha_code.site_key, {
action: "click",
})
.then(function (d) {
display_captcha_test_status(
user_registration_settings_params
.i18n.captcha_success,
"success"
);
});
} catch (err) {
display_captcha_test_status(
err.message,
"error"
);
}
break;

case 'cloudflare':
try {
turnstile.render(
'#' + ur_recaptcha_node.find('.cf-turnstile').attr('id'),
case "hCaptcha":
google_recaptcha_login = hcaptcha.render(
ur_recaptcha_node
.find(".g-recaptcha-hcaptcha")
.attr("id"),
{
sitekey: ur_recaptcha_code.site_key,
theme: ur_recaptcha_code.theme_mode,
theme: "light",
style: "transform:scale(0.77);-webkit-transform:scale(0.77);transform-origin:0 0;-webkit-transform-origin:0 0;",
}
);

ur_recaptcha_node.find('iframe').css('display', 'block');
} catch (err) {
display_captcha_test_status(err.message, 'error');
}
break;
break;

case "cloudflare":
try {
turnstile.render(
"#" +
ur_recaptcha_node
.find(".cf-turnstile")
.attr("id"),
{
sitekey: ur_recaptcha_code.site_key,
theme: ur_recaptcha_code.theme_mode,
style: "transform:scale(0.77);-webkit-transform:scale(0.77);transform-origin:0 0;-webkit-transform-origin:0 0;",
}
);

ur_recaptcha_node
.find("iframe")
.css("display", "block");
} catch (err) {
display_captcha_test_status(
err.message,
"error"
);
}
break;
}
}
}
}
});
);

/**
*
* @param {string} notice Notice message.
* @param {string} type Notice type.
*/
function display_captcha_test_status(notice = '', type = 'success') {

function display_captcha_test_status(notice = "", type = "success") {
if (notice.length) {
var notice_container = $('#ur-captcha-notice');
var notice_icon = $('#ur-captcha-notice--icon');
var notice_text = $('#ur-captcha-notice--text');
var notice_container = $("#ur-captcha-notice");
var notice_icon = $("#ur-captcha-notice--icon");
var notice_text = $("#ur-captcha-notice--text");

if (notice_text.length) {
notice_text.html(notice);

if ('success' === type) {
notice_container.removeClass().addClass('success');
notice_icon.addClass('dashicons dashicons-yes-alt');
} else if ('error' === type) {
notice_container.removeClass().addClass('error');
notice_icon.addClass('dashicons dashicons-dismiss');
if ("success" === type) {
notice_container.removeClass().addClass("success");
notice_icon.addClass("dashicons dashicons-yes-alt");
} else if ("error" === type) {
notice_container.removeClass().addClass("error");
notice_icon.addClass("dashicons dashicons-dismiss");
}
}
}

var spinner = $('#user_registration_captcha_setting_captcha_test .spinner');
var spinner = $(
"#user_registration_captcha_setting_captcha_test .spinner"
);
spinner.hide();
}

Expand Down

0 comments on commit c89b4f8

Please sign in to comment.