|
1205 | 1205 | this.login_error = ''; |
1206 | 1206 |
|
1207 | 1207 | 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' |
1209 | 1210 | ? this.email_address || '' |
1210 | 1211 | : this.username || ''; |
1211 | 1212 |
|
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', { |
1214 | 1223 | username_or_email, |
1215 | 1224 | password: this.inline_login_password, |
1216 | 1225 | _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); |
1240 | 1227 |
|
1241 | 1228 | this.request('wu_inline_login', login_data, function(results) { |
1242 | 1229 |
|
1243 | 1230 | that.logging_in = false; |
1244 | 1231 |
|
1245 | 1232 | if (results.success) { |
1246 | 1233 |
|
| 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 | + |
1247 | 1242 | // Login successful - reload page to show logged-in state |
1248 | 1243 | window.location.reload(); |
1249 | 1244 |
|
|
1263 | 1258 |
|
1264 | 1259 | } |
1265 | 1260 |
|
| 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 | + |
1266 | 1272 | }); |
1267 | 1273 |
|
1268 | 1274 | return false; |
|
1351 | 1357 |
|
1352 | 1358 | } |
1353 | 1359 |
|
| 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 | + |
1354 | 1371 | } |
1355 | 1372 |
|
1356 | 1373 | function handleLogin(e) { |
|
1375 | 1392 |
|
1376 | 1393 | const username_or_email = fieldType === 'email' ? that.email_address : that.username; |
1377 | 1394 |
|
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', { |
1380 | 1405 | username_or_email, |
1381 | 1406 | password, |
1382 | 1407 | _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); |
1406 | 1409 |
|
1407 | 1410 | jQuery.ajax({ |
1408 | 1411 | method: 'POST', |
|
1412 | 1415 |
|
1413 | 1416 | if (results.success) { |
1414 | 1417 |
|
| 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 | + |
1415 | 1426 | window.location.reload(); |
1416 | 1427 |
|
1417 | 1428 | } else { |
|
1457 | 1468 |
|
1458 | 1469 | }); |
1459 | 1470 |
|
| 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 | + |
1460 | 1484 | }); |
1461 | 1485 |
|
1462 | 1486 | }, |
|
0 commit comments