Last active
June 2, 2022 11:54
-
-
Save New0/3910a7b3a68a38bacea691255808432e to your computer and use it in GitHub Desktop.
DRAFT used for beta testing a precise Ninja Forms Version, leave a comment if interested in testing those hooks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Ninja Forms consent hooks example | |
* Description: Example use of the nf_consent_status_check and nf_consent_link JS events that help integrate custom consent check and activation into Ninja Forms. | |
* Version: 0.0.1 | |
* Author: Nicolas Figueira | |
* Author URI: https://newo.me | |
*/ | |
add_action("wp_footer", function() { ?> | |
<script type="text/javascript"> | |
window.addEventListener("DOMContentLoaded", (event) => { | |
/** | |
* Check consent using nf_consent_status_check custom event | |
* Store the result in the nfFrontEnd.nf_consent_status_response global variable | |
* | |
* The detail variable of the event holds the current consent resppnse object which at this point should be equal to nfFrontEnd.nf_consent_status_response global variable | |
* nfFrontEnd.nf_consent_status_response holds: | |
* nfFrontEnd.nf_consent_status_response.services an array of services that are using the consent check | |
* nfFrontEnd.nf_consent_status_response.consent_status an array of statuses ( => the global consent status can be determined by : | |
* !nfFrontEnd.nf_consent_status_response.consent_status.includes( false ); ( FALSE if array includes false ) | |
* | |
*/ | |
document.addEventListener("nf_consent_status_check", function(e) { | |
//complianz plugin api | |
if (typeof cmplz_has_service_consent == 'function' && !cmplz_has_service_consent('google-recaptcha','marketing')) { | |
nfFrontEnd.nf_consent_status_response.consent_state.push(cmplz_has_service_consent('google-recaptcha','marketing')); | |
nfFrontEnd.nf_consent_status_response.services.push("complianz"); | |
} | |
//wp consent api | |
if (typeof wp_has_consent == 'function' && !wp_has_consent('marketing')) { | |
nfFrontEnd.nf_consent_status_response.consent_state.push(wp_has_consent('marketing')); | |
nfFrontEnd.nf_consent_status_response.services.push("wp_has_consent"); | |
} | |
}); | |
/** | |
* nf_consent_link is a JS custom event | |
* | |
* The detail variable of the event holds: | |
* e.detail.services array of services that checked the consent | |
* e.detail.element JS html object that can be customized here in rder to consent for a particular service | |
* e.detail.submitFieldID Submit field ID, useful to trigger reload_after_consent global function in order to reload the form without reloading the whole page | |
* e.detail.layoutView JS object, useful to trigger reload_after_consent global function in order to reload the form without reloading the whole page | |
*/ | |
document.addEventListener("nf_consent_link", function (e) { | |
//wp_has_consent API detected | |
if ( e.detail.services.includes("wp_has_consent") ) { | |
//listen to consent change event fo wp_has_consent | |
document.addEventListener("wp_listen_for_consent_change", function (e) { | |
var changedConsentCategory = e.detail; | |
for (var key in changedConsentCategory) { | |
if (changedConsentCategory.hasOwnProperty(key)) { | |
if (key === 'marketing' && changedConsentCategory[key] === 'allow') { | |
reload_after_consent( e.detail.submitFieldID, e.detail.layoutView); | |
} | |
} | |
} | |
}); | |
//Change consent on action element Click | |
e.detail.element.onclick(() => { | |
wp_set_consent('marketing', 'allow'); | |
}) | |
}// End WP_consent_api related code | |
//Set link for complianz when detected | |
if ( e.detail.services.includes("complianz") ) { | |
const submitFieldID = e.detail.submitFieldID, layoutView = e.detail.layoutView; | |
//Listen to consent change and reload form when | |
document.addEventListener("cmplz_status_change", function (e) { | |
if (e.detail.category === 'marketing' && e.detail.value === 'allow') { | |
reload_after_consent( submitFieldID, layoutView); | |
} | |
}); | |
//Set complianz link | |
e.detail.element.dataset.service = "google-recaptcha"; | |
e.detail.element.dataset.category = "marketing"; | |
e.detail.element.className = "cmplz-blocked-content-container cmplz-blocked-content-notice cmplz-accept-service cmplz-accept-marketing cmplz-ninja-forms" | |
e.detail.element.style = "cursor: pointer; margin-top: 1rem;" | |
}//End complianz specific | |
}); | |
}); | |
</script> | |
<?php }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment