Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace recaptcha alerts with generic errors #3627

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions src/other-scripts/recaptcha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,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 @@ -147,6 +180,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 @@ -158,17 +199,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 with reCAPTCHA. Please try again.', 'newspack-plugin' )
: wp.i18n.__( 'There was an error 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 );
},
} );

Expand All @@ -177,6 +213,8 @@ function renderWidget( form, onSuccess = null, onError = null ) {

button.addEventListener( 'click', e => {
e.preventDefault();
// 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;
}
}