Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save glaubersilva/305aec3d2979c68e90efc34d84e27db0 to your computer and use it in GitHub Desktop.

Select an option

Save glaubersilva/305aec3d2979c68e90efc34d84e27db0 to your computer and use it in GitHub Desktop.
[Forminator] Automatic Actions
<?php
/**
* Plugin Name: [Forminator] Automatic Actions
* Plugin URI: https://wpmudev.com
* Description: *** ACTION 1 - When a field with the "sls-automatic-next-step" class became visible, this plugin automatically clicks on the "Next" button in case it exists. *** ACTION 2 - When a field with the "sls-automatic-submit" class became visible, this plugin automatically clicks on the "Submit" button. *** SETUP - Go to "Your Field > Edit Field > Styling" and add the "sls-automatic-next-step" class for active the action 1 OR add the "sls-automatic-submit" class to active the action 2 (always add the class name without the quotes)
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://wpmudev.com
* Jira Task: SLS-1399 and SLS-3171
* License: GPLv2 or later
*
* @package WPMUDEV_Forminator_Automatic_Actions
*/
defined( 'ABSPATH' ) || exit;
function wpmudev_forminator_automatic_actions( $html ) {
ob_start();
?>
<script type="text/javascript">
function readyHandler() {
var TIMEOUT = 1000;
var HTMLfields = document.querySelectorAll( '.sls-automatic-next-step, .sls-automatic-submit' );
if ( typeof( HTMLfields ) != 'undefined' && HTMLfields != null ) {
for ( var n = 0, max = HTMLfields.length; n < max; n++ ) {
//console.log('HTMLfield ' + n, HTMLfields[n]);
observeThis( HTMLfields[n] );
}
}
function maybeNextStep( target ) {
if ( ! target.classList.contains( 'forminator-hidden' ) ) {
//console.log('NEXT');
var btn_next = document.querySelector( '.forminator-button-next' );
if ( typeof( btn_next ) != 'undefined' && btn_next != null ) {
setTimeout( function(){
btn_next.click();
}, TIMEOUT );
}
}
}
function maybeSubmit( target ) {
if ( ! target.classList.contains( 'forminator-hidden' ) ) {
//console.log('SUBMIT', target);
var btn_submit = document.querySelector( '.forminator-button-submit' );
if ( typeof( btn_submit ) != 'undefined' && btn_submit != null ) {
setTimeout( function(){
btn_submit.click();
}, TIMEOUT );
}
}
}
function observeThis( target ) {
// Create an observer instance
var observer = new MutationObserver( function( mutations ) {
//console.log('Target Changed: ',target);
if ( target.classList.contains( 'sls-automatic-next-step' ) ) {
maybeNextStep( target );
}
if ( target.classList.contains( 'sls-automatic-submit' ) ) {
maybeSubmit( target );
}
});
// Configuration of the observer
var config = {
attributes: true,
childList: true,
characterData: true
};
// Pass in the target node, as well as the observer options
observer.observe( target, config );
}
}
// Check if the DOMContentLoaded has already been completed
if ( document.readyState !== 'loading' ) {
readyHandler();
} else {
document.addEventListener( 'DOMContentLoaded', readyHandler );
}
</script>
<?php
$javascript = ob_get_clean();
return $html . $javascript; // phpcs:ignore
}
add_filter( 'forminator_render_form_markup', 'wpmudev_forminator_automatic_actions', PHP_INT_MAX );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment