Skip to content

Commit e727519

Browse files
superdav42claude
andcommitted
refactor(checkout): replace captcha-specific code with generic JS hooks
The inline-login flow in checkout.js previously read and reset reCAPTCHA / hCaptcha / Cap token inputs directly, coupling the core plugin to specific captcha providers. Replace that with generic wp.hooks extension points so any addon can participate in the lifecycle: * wu_inline_login_data (filter) — augment the AJAX request payload (e.g. append captcha tokens) * wu_inline_login_success (action) — react to a successful login * wu_inline_login_error (action) — react to a failed login (e.g. reset a captcha widget) * wu_inline_login_prompt_ready (action) — initialize widgets once the prompt container is live in the DOM The captcha addon now hooks these instead of relying on hardcoded selectors and a window.wuCaptchaResetInlineLogin global. Supersedes #901. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 0ca23fb commit e727519

2 files changed

Lines changed: 76 additions & 52 deletions

File tree

assets/js/checkout.js

Lines changed: 75 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,45 +1205,40 @@
12051205
this.login_error = '';
12061206

12071207
const that = this;
1208-
const username_or_email = this.login_prompt_field === 'email'
1208+
const field_type = this.login_prompt_field;
1209+
const username_or_email = field_type === 'email'
12091210
? this.email_address || ''
12101211
: this.username || '';
12111212

1212-
// Include captcha tokens if present on the page.
1213-
const login_data = {
1213+
/**
1214+
* Filter the inline login request data.
1215+
*
1216+
* Addons can hook into this filter to append extra fields to the
1217+
* inline login request (for example, captcha tokens).
1218+
*
1219+
* @param {Object} data The data object to be sent.
1220+
* @param {string} field_type The field type ('email' or 'username').
1221+
*/
1222+
const login_data = hooks.applyFilters('wu_inline_login_data', {
12141223
username_or_email,
12151224
password: this.inline_login_password,
12161225
_wpnonce: jQuery('[name="_wpnonce"]').val()
1217-
};
1218-
1219-
const recaptcha_token = jQuery('input[name="g-recaptcha-response"]').filter(function() {
1220-
return this.value;
1221-
}).first().val();
1222-
const hcaptcha_token = jQuery('input[name="h-captcha-response"]').filter(function() {
1223-
return this.value;
1224-
}).first().val();
1225-
const cap_token = jQuery('input[name="cap-token"]').filter(function() {
1226-
return this.value;
1227-
}).first().val();
1228-
1229-
if (recaptcha_token) {
1230-
login_data[ 'g-recaptcha-response' ] = recaptcha_token;
1231-
}
1232-
1233-
if (hcaptcha_token) {
1234-
login_data[ 'h-captcha-response' ] = hcaptcha_token;
1235-
}
1236-
1237-
if (cap_token) {
1238-
login_data[ 'cap-token' ] = cap_token;
1239-
}
1226+
}, field_type);
12401227

12411228
this.request('wu_inline_login', login_data, function(results) {
12421229

12431230
that.logging_in = false;
12441231

12451232
if (results.success) {
12461233

1234+
/**
1235+
* Fires when an inline login attempt succeeds.
1236+
*
1237+
* @param {Object} results The AJAX success response.
1238+
* @param {string} field_type The field type ('email' or 'username').
1239+
*/
1240+
hooks.doAction('wu_inline_login_success', results, field_type);
1241+
12471242
// Login successful - reload page to show logged-in state
12481243
window.location.reload();
12491244

@@ -1263,6 +1258,17 @@
12631258

12641259
}
12651260

1261+
/**
1262+
* Fires when an inline login attempt fails.
1263+
*
1264+
* Addons can hook into this action to react to failed logins
1265+
* (for example, resetting a captcha widget).
1266+
*
1267+
* @param {Object} error The AJAX error response.
1268+
* @param {string} field_type The field type ('email' or 'username').
1269+
*/
1270+
hooks.doAction('wu_inline_login_error', error, field_type);
1271+
12661272
});
12671273

12681274
return false;
@@ -1351,6 +1357,17 @@
13511357

13521358
}
13531359

1360+
/**
1361+
* Fires when an inline login attempt fails.
1362+
*
1363+
* Addons can hook into this action to react to failed logins
1364+
* (for example, resetting a captcha widget).
1365+
*
1366+
* @param {Object} error The AJAX error response or jqXHR.
1367+
* @param {string} fieldType The field type ('email' or 'username').
1368+
*/
1369+
hooks.doAction('wu_inline_login_error', error, fieldType);
1370+
13541371
}
13551372

13561373
function handleLogin(e) {
@@ -1375,34 +1392,20 @@
13751392

13761393
const username_or_email = fieldType === 'email' ? that.email_address : that.username;
13771394

1378-
// Include captcha tokens if present on the page.
1379-
const inline_login_data = {
1395+
/**
1396+
* Filter the inline login request data.
1397+
*
1398+
* Addons can hook into this filter to append extra fields
1399+
* to the inline login request (for example, captcha tokens).
1400+
*
1401+
* @param {Object} data The data object to be sent.
1402+
* @param {string} fieldType The field type ('email' or 'username').
1403+
*/
1404+
const inline_login_data = hooks.applyFilters('wu_inline_login_data', {
13801405
username_or_email,
13811406
password,
13821407
_wpnonce: jQuery('[name="_wpnonce"]').val()
1383-
};
1384-
1385-
const recaptcha_val = jQuery('input[name="g-recaptcha-response"]').filter(function() {
1386-
return this.value;
1387-
}).first().val();
1388-
const hcaptcha_val = jQuery('input[name="h-captcha-response"]').filter(function() {
1389-
return this.value;
1390-
}).first().val();
1391-
const cap_val = jQuery('input[name="cap-token"]').filter(function() {
1392-
return this.value;
1393-
}).first().val();
1394-
1395-
if (recaptcha_val) {
1396-
inline_login_data[ 'g-recaptcha-response' ] = recaptcha_val;
1397-
}
1398-
1399-
if (hcaptcha_val) {
1400-
inline_login_data[ 'h-captcha-response' ] = hcaptcha_val;
1401-
}
1402-
1403-
if (cap_val) {
1404-
inline_login_data[ 'cap-token' ] = cap_val;
1405-
}
1408+
}, fieldType);
14061409

14071410
jQuery.ajax({
14081411
method: 'POST',
@@ -1412,6 +1415,14 @@
14121415

14131416
if (results.success) {
14141417

1418+
/**
1419+
* Fires when an inline login attempt succeeds.
1420+
*
1421+
* @param {Object} results The AJAX success response.
1422+
* @param {string} fieldType The field type.
1423+
*/
1424+
hooks.doAction('wu_inline_login_success', results, fieldType);
1425+
14151426
window.location.reload();
14161427

14171428
} else {
@@ -1457,6 +1468,19 @@
14571468

14581469
});
14591470

1471+
/**
1472+
* Fires once the inline login prompt is attached and ready.
1473+
*
1474+
* Addons can hook into this action to initialize their own
1475+
* widgets inside the prompt (for example, rendering a captcha
1476+
* widget whose markup was injected via the
1477+
* `wu_inline_login_prompt_before_submit` server-side action).
1478+
*
1479+
* @param {string} fieldType The field type ('email' or 'username').
1480+
* @param {HTMLElement} loginPromptContainer The prompt container element.
1481+
*/
1482+
hooks.doAction('wu_inline_login_prompt_ready', fieldType, loginPromptContainer);
1483+
14601484
});
14611485

14621486
},

0 commit comments

Comments
 (0)