Skip to content

Commit

Permalink
fix(recaptcha): replace alerts with generic errors (#3627)
Browse files Browse the repository at this point in the history
This PR replaces the default recaptcha failure alert behavior with generic errors appended to the relevant forms.
  • Loading branch information
chickenn00dle committed Dec 16, 2024
1 parent 0182af8 commit 45c8152
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
52 changes: 45 additions & 7 deletions src/other-scripts/recaptcha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,39 @@ function addHiddenField( form ) {
}
}

/**
* Append a generic error message above the given form.
*
* @param {HTMLElement} form The form element.
* @param {string} message The error message to display.
*/
function addErrorMessage( form, message ) {
const errorText = document.createElement( 'p' );
errorText.textContent = message;
const container = document.createElement( 'div' );
container.classList.add( 'newspack-recaptcha-error' );
container.appendChild( errorText );
// Newsletters block errors render below the form.
if ( form.parentElement.classList.contains( 'newspack-newsletters-subscribe' ) ) {
form.append( container );
} else {
container.classList.add( 'newspack-ui__notice', 'newspack-ui__notice--error' );
form.insertBefore( container, form.firstChild );
}
}

/**
* Remove generic error messages from form if present.
*
* @param {HTMLElement} form The form element.
*/
function removeErrorMessages( form ) {
const errors = form.querySelectorAll( '.newspack-recaptcha-error' );
for ( const error of errors ) {
error.parentElement.removeChild( error );
}
}

/**
* Remove the hidden reCAPTCHA v3 token field from the given form.
*
Expand Down Expand Up @@ -168,6 +201,14 @@ function renderWidget( form, onSuccess = null, onError = null ) {
refreshWidget( button );
};

const errorCallback = ( message ) => {
if ( onError ) {
onError( message );
} else {
addErrorMessage( form, message );
}
}

// Render reCAPTCHA widget. See https://developers.google.com/recaptcha/docs/invisible#js_api for API reference.
const widgetId = grecaptcha.render( button, {
...options,
Expand All @@ -179,17 +220,12 @@ function renderWidget( form, onSuccess = null, onError = null ) {
refreshWidget( button );
} else {
clearInterval( refreshIntervalId );
button.disabled = true;
}
const message = retryCount < 3
? wp.i18n.__( 'There was an error connecting with reCAPTCHA. Please try submitting again.', 'newspack-plugin' )
: wp.i18n.__( 'There was an error connecting with reCAPTCHA. Please reload the page and try again.', 'newspack-plugin' );
if ( onError ) {
onError( message );
} else {
// Recaptcha's default error behavior is to alert with the above message.
// eslint-disable-next-line no-alert
alert( message );
}
errorCallback( message );
},
'expired-callback': () => {
refreshWidget( button );
Expand All @@ -202,6 +238,8 @@ function renderWidget( form, onSuccess = null, onError = null ) {
button.addEventListener( 'click', e => {
e.preventDefault();
e.stopImmediatePropagation();
// Empty error messages if present.
removeErrorMessages( form );
// Skip reCAPTCHA verification if the button has a data-skip-recaptcha attribute.
if ( button.hasAttribute( 'data-skip-recaptcha' ) ) {
successCallback();
Expand Down
6 changes: 6 additions & 0 deletions src/other-scripts/recaptcha/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@
inset: 0 !important; // Prevents an odd side-scroll issue in the registration modal & block.
visibility: hidden !important;
}

.newspack-recaptcha-error {
&.newspack-ui__notice--error {
margin-top: 0 !important;
}
}

0 comments on commit 45c8152

Please sign in to comment.