: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @param {string} time1 Time 1.
* @param {string} time2 Time 2.
* @return {boolean} True if time1 is greater than time2.
compareTimesGreaterThan( time1, time2 ) {
// Proper format time: add space before AM/PM, make uppercase.
time1 = time1.replace( /(am|pm)/g, ' $1' ).toUpperCase();
time2 = time2.replace( /(am|pm)/g, ' $1' ).toUpperCase();
const time1Date = Date.parse( '01 Jan 2021 ' + time1 ),
time2Date = Date.parse( '01 Jan 2021 ' + time2 );
return time1Date >= time2Date;
* Determine whether the modern markup setting is enabled.
* @return {boolean} True if modern markup is enabled.
isModernMarkupEnabled() {
return !! wpforms_settings.isModernMarkupEnabled;
* Initialize token updater.
* Maybe update token via AJAX if it looks like outdated.
// Attach event handler to all forms with class `wpforms-form`
$( '.wpforms-form' ).on( 'focusin', function( event ) {
const $form = $( event.target.closest( 'form' ) );
const timestamp = Date.now();
if ( ! this.needsTokenUpdate( timestamp, $form ) ) {
this.updateToken( timestamp, $form, event );
}.bind( this ) ); // Bind `this` to maintain context inside the function
* Check if the form needs a new token.
* @param {number} timestamp Timestamp.
* @param {jQuery} $form Form.
* @return {boolean} Whether token needs update or not.
needsTokenUpdate( timestamp, $form ) {
const tokenTime = $form.attr( 'data-token-time' ) || 0;
const diff = timestamp - ( tokenTime * 1000 );
// Check if the token is expired.
return diff >= wpforms_settings.token_cache_lifetime * 1000 && ! this.isUpdatingToken;
* Update the token for the form.
* @param {number} timestamp Timestamp.
* @param {jQuery} $form Form.
* @param {Event} event Event.
updateToken( timestamp, $form, event ) {
const formId = $form.data( 'formid' );
const $submitBtn = $form.find( '.wpforms-submit' );
this.isUpdatingToken = true;
$submitBtn.prop( 'disabled', true );
$.post( wpforms_settings.ajaxurl, {
action: 'wpforms_get_token',
} ).done( function( response ) {
if ( response.success ) {
$form.attr( 'data-token-time', timestamp );
$form.attr( 'data-token', response.data.token );
// Re-enable the 'submit' button.
$submitBtn.prop( 'disabled', false );
// Trigger form submission if the focus was on the 'submit' button.
if ( event.target === $submitBtn[ 0 ] ) {
$submitBtn.trigger( 'click' );
// eslint-disable-next-line no-console
console.error( 'Failed to update token: ', response );
} ).fail( function( jqXHR, textStatus, errorThrown ) {
// eslint-disable-next-line no-console
console.error( 'AJAX request failed: ', textStatus, errorThrown );
this.isUpdatingToken = false;
// Re-enable the 'submit' button.
$submitBtn.prop( 'disabled', false );
* Restore Submit button on Mobile.
restoreSubmitButtonOnEventPersisted() {
window.onpageshow = function( event ) {
// If back/forward button has been clicked, restore submit button for all forms on the page.
$( '.wpforms-form' ).each( function() {
app.restoreSubmitButton( $form, $form.closest( '.wpforms-container' ) );
}( document, window, jQuery ) );