Changeset 3384845
- Timestamp:
- 10/26/2025 09:05:39 PM (4 months ago)
- Location:
- fitconsent-cmp/trunk
- Files:
-
- 2 edited
-
fitconsent-cmp.php (modified) (3 diffs)
-
js/integration.js (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
fitconsent-cmp/trunk/fitconsent-cmp.php
r3360183 r3384845 4 4 * Plugin URI: https://app.fitconsent.com 5 5 * Description: Integrates the FitConsent Consent Management Platform with WordPress and the WP Consent API for Google Site Kit compatibility. 6 * Version: 1.0. 16 * Version: 1.0.2 7 7 * Author: FitConsent 8 8 * Author URI: https://fitconsent.com … … 16 16 } 17 17 18 define( 'FITCONSENT_VERSION', '1.0. 1' );18 define( 'FITCONSENT_VERSION', '1.0.2' ); 19 19 define( 'FITCONSENT_PLUGIN_FILE', __FILE__ ); 20 20 define( 'FITCONSENT_PLUGIN_PATH', plugin_dir_path( FITCONSENT_PLUGIN_FILE ) ); … … 49 49 'fitconsent-wp-integration', 50 50 plugins_url( 'js/integration.js', FITCONSENT_PLUGIN_FILE ), 51 [ ], // No dependencies, it will wait for the consent event51 [ 'wp-consent-api' ], // Explicitly depend on the WP Consent API. 52 52 FITCONSENT_VERSION, 53 53 true -
fitconsent-cmp/trunk/js/integration.js
r3360183 r3384845 2 2 * FitConsent - WP Consent API Integration 3 3 * 4 * This script listens for the FitConsent banner's consent event and translates it 5 * into a WP Consent API update, ensuring compatibility with Google Site Kit. 4 * This script sets a default consent state on page load and then listens for the 5 * FitConsent banner's consent event to update the WP Consent API, ensuring 6 * compatibility with Google Site Kit. 6 7 */ 7 (function ( ) {8 (function (wp) { 8 9 'use strict'; 9 10 … … 21 22 22 23 /** 24 * Creates a baseline consent state object, denying all optional categories. 25 * @returns {Object} The baseline consent state. 26 */ 27 function createBaselineConsentState() { 28 const state = {}; 29 for (const key in categoryMap) { 30 const wpCategory = categoryMap[key]; 31 state[wpCategory] = (wpCategory === 'necessary') ? 'allow' : 'deny'; 32 } 33 return state; 34 } 35 36 /** 37 * Sets the default consent state before the user has interacted with the banner. 38 */ 39 function setDefaultConsentState() { 40 if (wp && wp.set_consent) { 41 const defaultState = createBaselineConsentState(); 42 console.log('FitConsent cmp: Setting default WP Consent API state:', defaultState); 43 wp.set_consent('page', defaultState); 44 } 45 } 46 47 /** 23 48 * Handles the consent update from the FitConsent banner. 24 49 * @param {Event} event The custom event fired by the banner. … … 31 56 32 57 const choices = event.detail.choices; 33 const consentType = 'page'; // 'page' or 'session' 34 const consentState = {}; 35 36 let allAccepted = true; 58 const consentType = 'page'; 59 60 const consentState = createBaselineConsentState(); 37 61 38 62 for (const fitconsentCategory in choices) { 39 63 if (Object.prototype.hasOwnProperty.call(choices, fitconsentCategory)) { 40 const wpCategory = categoryMap[fitconsentCategory] || fitconsentCategory; 41 const consentValue = choices[fitconsentCategory] ? 'allow' : 'deny'; 64 // Use the mapped category if it exists, otherwise fall back to the original name. 65 // This ensures custom categories are passed through correctly. 66 const wpCategory = categoryMap[fitconsentCategory] ?? fitconsentCategory; 42 67 43 if (c onsentValue === 'deny') {44 allAccepted = false;68 if (choices[fitconsentCategory]) { 69 consentState[wpCategory] = 'allow'; 45 70 } 46 47 consentState[wpCategory] = consentValue;48 71 } 49 72 } 50 51 // Ensure all standard categories have a defined state.52 if (allAccepted) {53 consentState.statistics = 'allow';54 consentState.marketing = 'allow';55 } else {56 if (typeof consentState.statistics === 'undefined') consentState.statistics = 'deny';57 if (typeof consentState.marketing === 'undefined') consentState.marketing = 'deny';58 }59 60 73 61 74 // Use the WP Consent API to set the consent state. 62 if (w indow.wp_set_consent) {75 if (wp && wp.set_consent) { 63 76 console.log('FitConsent cmp: Updating WP Consent API with state:', consentState); 64 w indow.wp_set_consent(consentType, consentState);77 wp.set_consent(consentType, consentState); 65 78 } else { 66 79 console.warn('FitConsent cmp: WP Consent API (wp_set_consent) not found.'); 67 80 } 68 81 } 69 70 // Listen for the custom event dispatched by the FitConsent banner upon user interaction. 71 // We assume the banner script will dispatch a `fitconsent:consentUpdated` event. 72 // The banner script needs to be modified to do this. 82 83 // Set the default state as soon as this script runs. 84 setDefaultConsentState(); 85 86 // Listen for the custom event dispatched by the FitConsent banner. 73 87 document.addEventListener('fitconsent:consentUpdated', handleConsentUpdate); 74 88 75 })( );89 })(window);
Note: See TracChangeset
for help on using the changeset viewer.