From d925e9144fee7071166698756f6d6f22e601a381 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Wed, 22 Jul 2020 19:39:17 +0300 Subject: [PATCH] Link to the pro --- assets/js/controls.js | 118 +++++++++++++++++++++++++++--------------- plugin.php | 2 +- 2 files changed, 78 insertions(+), 42 deletions(-) diff --git a/assets/js/controls.js b/assets/js/controls.js index 050c379..fc49a38 100644 --- a/assets/js/controls.js +++ b/assets/js/controls.js @@ -1,9 +1,66 @@ /* eslint camelcase: warn */ ( function( $ ) { - if ( ! window.cf7_extras ) { - return; - } + + var jQueryEvent, formEventCallback; + + var formEventCallbacks = { + wpcf7mailsent: function( form ) { + var formConfig; + + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { + formConfig = getFormConfig( form.contactFormId ); + trackAnalyticsEvent( 'Contact Form', 'Sent', formConfig.title ); + } + }, + wpcf7mailfailed: function( form ) { + var formConfig; + + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { + formConfig = getFormConfig( form.contactFormId ); + trackAnalyticsEvent( 'Contact Form', 'Error', formConfig.title ); + } + }, + wpcf7spam: function( form ) { + var formConfig; + + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { + formConfig = getFormConfig( form.contactFormId ); + trackAnalyticsEvent( 'Contact Form', 'Spam', formConfig.title ); + } + }, + wpcf7submit: function( form ) { + var formConfig; + + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { + formConfig = getFormConfig( form.contactFormId ); + trackAnalyticsEvent( 'Contact Form', 'Submit', formConfig.title ); + } + + if ( form.contactFormId && 'mail_sent' === form.status && formEventEnabled( form.contactFormId, 'redirect-success' ) ) { + formConfig = getFormConfig( form.contactFormId ); + + if ( formConfig.redirect_url ) { + window.location = formConfig.redirect_url; + } + } + } + }; + + var jQueryEvents = { + 'wpcf7:mailsent': function( event, form ) { + formCallbacks.wpcf7mailsent( form ); + }, + 'wpcf7:mailfailed': function( event, form ) { + formCallbacks.wpcf7mailfailed( form ); + }, + 'wpcf7:spam': function( event, form ) { + formCallbacks.wpcf7spam( form ); + }, + 'wpcf7:submit': function( event, form ) { + formCallbacks.wpcf7submit( form ); + } + }; function trackAnalyticsEvent( eventCategory, eventAction, eventTitle ) { @@ -64,47 +121,26 @@ return false; }; - $( document ).on( 'wpcf7:mailsent', function( event, form ) { - var formConfig; - - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { - formConfig = getFormConfig( form.contactFormId ); - trackAnalyticsEvent( 'Contact Form', 'Sent', formConfig.title ); - } - } ); - - $( document ).on( 'wpcf7:mailfailed', function( event, form ) { - var formConfig; - - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { - formConfig = getFormConfig( form.contactFormId ); - trackAnalyticsEvent( 'Contact Form', 'Error', formConfig.title ); - } - } ); - - $( document ).on( 'wpcf7:spam', function( event, form ) { - var formConfig; + // We need the event config for each form to do anything. + if ( ! window.cf7_extras ) { + return; + } - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { - formConfig = getFormConfig( form.contactFormId ); - trackAnalyticsEvent( 'Contact Form', 'Spam', formConfig.title ); + // Register the new JS events in CF7 version 5.2 and above. + if ( 'function' === typeof document.addEventListener ) { + for ( formEventCallback in formEventCallbacks ) { + document.addEventListener( formEventCallback, function( event ) { + if ( event.detail ) { + formEventCallbacks[ formEventCallback ].call( this, event.detail ); + } + } ); } - } ); - - $( document ).on( 'wpcf7:submit', function( event, form ) { - var formConfig; - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { - formConfig = getFormConfig( form.contactFormId ); - trackAnalyticsEvent( 'Contact Form', 'Submit', formConfig.title ); + // Register the legacy jQuery events pre CF7 version 5.2. + } else if ( 'function' === typeof $ ) { + for ( jQueryEvent in jQueryEvents ) { + $( document ).on( jQueryEvent, jQueryEvents[ jQueryEvent ] ); } + } - if ( form.contactFormId && 'mail_sent' === form.status && formEventEnabled( form.contactFormId, 'redirect-success' ) ) { - formConfig = getFormConfig( form.contactFormId ); - - if ( formConfig.redirect_url ) { - window.location = formConfig.redirect_url; - } - } - } ); }( jQuery ) ); diff --git a/plugin.php b/plugin.php index 104df09..3fc9e65 100644 --- a/plugin.php +++ b/plugin.php @@ -1,7 +1,7 @@ 🚀 PRO version for advanced analytics and tracking features. * Plugin URI: https://formcontrols.com * Author: Kaspars Dambis * Author URI: https://formcontrols.com