<?php
// IMPORTANT: This plugin is dynamically updated - MODIFICATIONS WILL BE OVERWRITTEN

/**************************************************
 * Name: Gravity Forms
 * Description: Automatically track submissions from Gravity Forms
 *************************************************/

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
?>
<!-- Nutshell Integration: Gravity Forms -->
<script type="text/javascript" data-registered="nutshell-plugin">
	document.addEventListener( 'submit.gravityforms', function( e ) {
		if( 'function' !== typeof mcfx ) {
			console.warn( '"mcfx" is not defined. Please ensure that the RCFX pixel loads before usage.' );
		} else {
			mcfx( 'capture', e.target );
		}
	});
</script>


<?php
/**
 * Gravity Forms changed how AJAX submissions worked in >= 2.9.
 * 
 * This is a fallback in case the above capture doesn't work.
 * 
 * @todo Clean up this script to match other scripts
 */
if( class_exists( 'GFForms' ) && version_compare( GFForms::$version, '2.9', '>=' ) ) {
	?>
		<script type="text/javascript" data-registered="nutshell-plugin">
			( () => {
				document.addEventListener( 'gform/post_init', () => {
					gform.utils.addAsyncFilter( 'gform/submission/pre_submission', async data => {
						if( 'function' === typeof mcfx && data?.form instanceof HTMLFormElement ) {
							const targetPage = data.form.querySelector( '[name^="gform_target_page_number_"]' );
							if ( targetPage && parseInt( targetPage.value, 10 ) === 0 ) {
								mcfx( 'capture', data.form );
							}
						}

						return data
					})
				})
			}) ()
		</script>
	<?php
}
?>

<?php
// Check if GravityForm reCaptcha v3 addon is active
if (
	function_exists( 'is_plugin_active' ) &&
	is_plugin_active( 'gravityformsrecaptcha/recaptcha.php' ) &&
	class_exists( 'GF_RECAPTCHA' )
) {
	$gf_recaptcha = GF_RECAPTCHA::get_instance();

	// Check if GravityForm reCaptcha v3 keys are valid
	if ( method_exists( $gf_recaptcha, 'get_plugin_setting' ) && $gf_recaptcha->get_plugin_setting( 'recaptcha_keys_status_v3' ) == '1' ) {
	?>
<script type="text/javascript" data-registered="nutshell-plugin">
	if (
		/* global mcfx */
		'undefined' !== typeof mcfx
	) {
		(function(){
			document.querySelectorAll('.gform_wrapper form').forEach(function(item){
				//Match the form's submit button. We cover input and button elements, including buttons without an explicit type attribute, since those implicitly use the default type of "submit".
				item.querySelector('[type="submit"], button:not([type])')?.addEventListener('click', function(){
					if( 'function' !== typeof mcfx ) {
						console.warn( '"mcfx" is not defined. Please ensure that the RCFX pixel loads before usage.' );
					} else {
						mcfx('capture', item);
					}
				});
			});
			jQuery(document).on('gform_page_loaded', function(){
				document.querySelectorAll('.gform_wrapper form').forEach(function(item){
					if(!item.getAttribute('fx-target')){
						item.querySelector('[type="submit"], button:not([type])')?.addEventListener('click', function(){
							if( 'function' !== typeof mcfx ) {
								console.warn( '"mcfx" is not defined. Please ensure that the RCFX pixel loads before usage.' );
							} else {
								mcfx('capture', item);
							}
						});
						item.setAttribute('fx-target', true);
					}
				});
			});
		})();
	}
</script>
	<?php
	}
}
?>


<?php // IMPORTANT: This plugin is dynamically updated - MODIFICATIONS WILL BE OVERWRITTEN ?>
