diff --git a/assets/cookie-notice/frontconsent-cookie-notice.js b/assets/cookie-notice/frontconsent-cookie-notice.js index 27549ec..6db7e55 100644 --- a/assets/cookie-notice/frontconsent-cookie-notice.js +++ b/assets/cookie-notice/frontconsent-cookie-notice.js @@ -43,6 +43,40 @@ } } + function defineActivateGatedHelper() { + window.frcnCookieNoticeActivateGated = window.frcnCookieNoticeActivateGated || function (allowedCategories) { + if (allowedCategories && !allowedCategories.analytics) { + return; + } + + var run = function () { + var gated = Array.prototype.slice.call(document.querySelectorAll('script[data-frcn-consent=analytics]')).filter(function (node) { + return 'text/plain' === node.type; + }); + + var next = function () { + var inert = gated.shift(); + if (!inert) { + return; + } + var live = document.createElement('script'); + live.async = false; + live.onload = next; + live.onerror = next; + live.src = inert.src; + inert.parentNode.replaceChild(live, inert); + }; + next(); + }; + + if ('loading' === document.readyState) { + document.addEventListener('DOMContentLoaded', run); + } else { + run(); + } + }; + } + function defineInjectHelper() { window.frcnCookieNoticeInject = window.frcnCookieNoticeInject || function (gtmId, ga4Id, trackingIntegrations, allowedCategories) { var allowsCategory = function (category) { @@ -133,6 +167,8 @@ window.frcnCookieNoticePendingIntegrations.push(integration); } }); + + window.frcnCookieNoticeActivateGated(allowedCategories); }; } @@ -230,6 +266,7 @@ return; } + defineActivateGatedHelper(); defineInjectHelper(); var isStale = typeof window.frcnCookieNoticeIsConsentStale === 'function' && window.frcnCookieNoticeIsConsentStale(); diff --git a/assets/cookie-notice/frontconsent-cookie-notice.min.js b/assets/cookie-notice/frontconsent-cookie-notice.min.js index e48e341..0e84215 100644 --- a/assets/cookie-notice/frontconsent-cookie-notice.min.js +++ b/assets/cookie-notice/frontconsent-cookie-notice.min.js @@ -1 +1 @@ -!function(){"use strict";function e(e){var n=document.cookie.match(new RegExp("(?:^|; )"+e+"=([^;]*)"));if(!n)return"";try{return decodeURIComponent(n[1])}catch(e){return""}}function n(){var e=new FormData;e.append("action","frcn_get_cookie_notice_config"),fetch(frcnCookieNotice.ajaxUrl,{method:"POST",credentials:"same-origin",body:e}).then(function(e){return e.json()}).then(function(e){e&&e.success&&e.data&&window.frcnCookieNoticeInject&&window.frcnCookieNoticeInject(e.data.gtmId,e.data.ga4Id,e.data.trackingIntegrations,e.data.allowedCategories)}).catch(function(){})}function o(){if(!window.frcnCookieNoticeBootstrapped){window.frcnCookieNoticeInject=window.frcnCookieNoticeInject||function(e,n,o,t){var i=function(e){return!t||!!t[e]};if(e&&i("analytics")){window.dataLayer=window.dataLayer||[],window.dataLayer.push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var c=document.createElement("script");c.async=!0,c.src="https://www.googletagmanager.com/gtm.js?id="+encodeURIComponent(e),document.head.appendChild(c)}if(n&&i("analytics")){var a=document.createElement("script");a.async=!0,a.src="https://www.googletagmanager.com/gtag/js?id="+encodeURIComponent(n),document.head.appendChild(a),window.dataLayer=window.dataLayer||[],window.gtag=window.gtag||function(){window.dataLayer.push(arguments)},window.gtag("js",new Date),window.gtag("config",n)}Array.isArray(o)||(o=[]),o.forEach(function(e){var n=e&&e.type?e.type:"",o=e&&e.id?e.id:"",t=e&&e.category?e.category:"marketing";if(o&&i(t))if("clientify_analytics_plus"===n){var c=document.createElement("script");c.defer=!0,c.src="https://analyticsplusdev.clientify.net/analytics_plus/pixel/"+encodeURIComponent(o),document.head.appendChild(c)}else if("clientify_analytics_classic"===n)!function(e,n,o,t){n[t]=n[t]||function(){(n[t].q=n[t].q||[]).push(arguments)};var i=e.createElement("script"),c=e.getElementsByTagName("script")[0];i.async=1,i.src="https://analytics.clientify.net/tracker.js",c.parentNode.insertBefore(i,c)}(document,window,0,"ana"),window.ana("setTrackerUrl","https://analytics.clientify.net"),window.ana("setTrackingCode",o),window.ana("trackPageview");else if("brevo"===n){var a=document.createElement("script");a.async=!0,a.src="https://cdn.brevo.com/js/sdk-loader.js",document.head.appendChild(a),window.Brevo=window.Brevo||[],window.Brevo.push(["init",{client_key:o}])}else if("openai_chatgpt_ads"===n){if(!window.oaiq){window.oaiq=function(){window.oaiq.q.push(arguments)},window.oaiq.q=[];var d=document.createElement("script");d.async=!0,d.src="https://bzrcdn.openai.com/sdk/oaiq.min.js",document.head.appendChild(d)}window.oaiq("init",{pixelId:o,debug:!0})}else"function"==typeof window.frcnCookieNoticeInjectIntegration?window.frcnCookieNoticeInjectIntegration(e):(window.frcnCookieNoticePendingIntegrations=window.frcnCookieNoticePendingIntegrations||[],window.frcnCookieNoticePendingIntegrations.push(e))})};var o="function"==typeof window.frcnCookieNoticeIsConsentStale&&window.frcnCookieNoticeIsConsentStale();"accepted"!==e(frcnCookieNotice.cookieName)||o||n(),window.frcnCookieNoticeBootstrapped=!0}}"undefined"!=typeof frcnCookieNotice&&function(){window.dataLayer=window.dataLayer||[],window.gtag=window.gtag||function(){window.dataLayer.push(arguments)};var n="accepted"===e(frcnCookieNotice.cookieName)?"granted":"denied",o={ad_storage:n,ad_user_data:n,ad_personalization:n,analytics_storage:n};if("function"==typeof window.frcnCookieNoticeConsentModeState){var t=window.frcnCookieNoticeConsentModeState();t&&(o=t)}"function"==typeof window.frcnCookieNoticeIsConsentStale&&window.frcnCookieNoticeIsConsentStale()&&(o={ad_storage:"denied",ad_user_data:"denied",ad_personalization:"denied",analytics_storage:"denied"});window.gtag("consent","default",o)}(),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",c):c();var t=!1;function i(){var n=document.getElementById("frcn-cookie-reopen");if(n){var o=e(frcnCookieNotice.cookieName);"accepted"!==o&&"rejected"!==o||(n.hidden=!1,t||(t=!0,n.addEventListener("click",function(){document.cookie=frcnCookieNotice.cookieName+"=; path="+frcnCookieNotice.cookiePath+"; max-age=0; SameSite=Lax",frcnCookieNotice.isPolicyPage&&frcnCookieNotice.homeUrl?window.location.href=frcnCookieNotice.homeUrl:window.location.reload()})))}}function c(){if("undefined"!=typeof frcnCookieNotice){var t="undefined"!=typeof frcnCookieNoticeA11y?frcnCookieNoticeA11y:{bannerOpened:"Cookie consent banner opened.",accepted:"Cookies accepted.",rejected:"Cookies rejected."};o(),i();var c=document.getElementById("frcn-cookie-notice");if(c&&!function(n){var o=e(frcnCookieNotice.cookieName);return!("function"==typeof window.frcnCookieNoticeIsConsentStale&&window.frcnCookieNoticeIsConsentStale()||!n||"accepted"!==o&&"rejected"!==o||(n.style.display="none",0))}(c)){var a,d=c.querySelector('[data-frcn-cookie-action="accept"]'),r=c.querySelector('[data-frcn-cookie-action="reject"]'),s=c.classList.contains("frcn-cookie-notice--popup"),f=document.activeElement,u=document.getElementById("frcn-cookie-notice-announcer");a=s?150:20,window.setTimeout(function(){c.classList.remove("frcn-cookie-notice--init"),p(t.bannerOpened),s&&(document.body.classList.add("frcn-cookie-notice-lock-scroll"),d&&d.focus({preventScroll:!0}),document.addEventListener("keydown",k))},a),d&&d.addEventListener("click",function(e){e.preventDefault(),g("accepted")}),r&&r.addEventListener("click",function(e){e.preventDefault(),g("rejected")});var w=c.querySelector('[data-frcn-cookie-action="customize"]');w&&w.addEventListener("click",function(){var e;try{e=new CustomEvent("frcnCookieNoticeCustomize")}catch(n){(e=document.createEvent("CustomEvent")).initCustomEvent("frcnCookieNoticeCustomize",!0,!0,null)}document.dispatchEvent(e)});var l=!1}}function p(e){u&&(u.textContent=e)}function m(e){var n=Array.prototype.slice.call(c.querySelectorAll('a[href], button, input, [tabindex]:not([tabindex="-1"])'));if(n.length){var o=n[0],t=n[n.length-1];e.shiftKey&&document.activeElement===o?(e.preventDefault(),t.focus()):e.shiftKey||document.activeElement!==t||(e.preventDefault(),o.focus())}}function k(e){"Tab"===e.key?m(e):"Escape"!==e.key&&"Esc"!==e.key||(e.preventDefault(),g("rejected"))}function g(e){l||(l=!0,function(e){var n=24*parseInt(frcnCookieNotice.expirationDays,10)*60*60,o="https:"===window.location.protocol?"; Secure":"";document.cookie=frcnCookieNotice.cookieName+"="+e+"; path="+frcnCookieNotice.cookiePath+"; max-age="+n+"; SameSite=Lax"+o}(e),function(e){var n="accepted"===e?"granted":"denied";window.dataLayer=window.dataLayer||[],window.gtag=window.gtag||function(){window.dataLayer.push(arguments)},window.gtag("consent","update",{ad_storage:n,ad_user_data:n,ad_personalization:n,analytics_storage:n})}(e),p("accepted"===e?t.accepted:t.rejected),function(){c.classList.add("frcn-cookie-notice--hidden"),document.body.classList.remove("frcn-cookie-notice-lock-scroll"),document.removeEventListener("keydown",k),s&&f&&"function"==typeof f.focus&&f.focus({preventScroll:!0});window.setTimeout(function(){c.parentNode&&c.parentNode.removeChild(c)},300)}(),i(),function(e){var n;try{n=new CustomEvent("frcnCookieConsent",{detail:{consent:e}})}catch(o){(n=document.createEvent("CustomEvent")).initCustomEvent("frcnCookieConsent",!0,!0,{consent:e})}document.dispatchEvent(n)}(e),function(e){var n=new FormData;n.append("action","frcn_get_cookie_notice_log_nonce"),fetch(frcnCookieNotice.ajaxUrl,{method:"POST",credentials:"same-origin",body:n}).then(function(e){return e.json()}).then(function(n){if(n&&n.success&&n.data){var o=new FormData;return o.append("action","frcn_log_cookie_consent"),o.append("nonce",n.data.nonce),o.append("decision",e),fetch(frcnCookieNotice.ajaxUrl,{method:"POST",credentials:"same-origin",body:o})}}).catch(function(){})}(e),"accepted"===e&&n())}}}(); \ No newline at end of file +!function(){"use strict";function e(e){var n=document.cookie.match(new RegExp("(?:^|; )"+e+"=([^;]*)"));if(!n)return"";try{return decodeURIComponent(n[1])}catch(e){return""}}function n(){var e=new FormData;e.append("action","frcn_get_cookie_notice_config"),fetch(frcnCookieNotice.ajaxUrl,{method:"POST",credentials:"same-origin",body:e}).then(function(e){return e.json()}).then(function(e){e&&e.success&&e.data&&window.frcnCookieNoticeInject&&window.frcnCookieNoticeInject(e.data.gtmId,e.data.ga4Id,e.data.trackingIntegrations,e.data.allowedCategories)}).catch(function(){})}function t(){if(!window.frcnCookieNoticeBootstrapped){window.frcnCookieNoticeActivateGated=window.frcnCookieNoticeActivateGated||function(e){if(!e||e.analytics){var n=function(){var e=Array.prototype.slice.call(document.querySelectorAll("script[data-frcn-consent=analytics]")).filter(function(e){return"text/plain"===e.type}),n=function(){var t=e.shift();if(t){var o=document.createElement("script");o.async=!1,o.onload=n,o.onerror=n,o.src=t.src,t.parentNode.replaceChild(o,t)}};n()};"loading"===document.readyState?document.addEventListener("DOMContentLoaded",n):n()}},window.frcnCookieNoticeInject=window.frcnCookieNoticeInject||function(e,n,t,o){var i=function(e){return!o||!!o[e]};if(e&&i("analytics")){window.dataLayer=window.dataLayer||[],window.dataLayer.push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var c=document.createElement("script");c.async=!0,c.src="https://www.googletagmanager.com/gtm.js?id="+encodeURIComponent(e),document.head.appendChild(c)}if(n&&i("analytics")){var a=document.createElement("script");a.async=!0,a.src="https://www.googletagmanager.com/gtag/js?id="+encodeURIComponent(n),document.head.appendChild(a),window.dataLayer=window.dataLayer||[],window.gtag=window.gtag||function(){window.dataLayer.push(arguments)},window.gtag("js",new Date),window.gtag("config",n)}Array.isArray(t)||(t=[]),t.forEach(function(e){var n=e&&e.type?e.type:"",t=e&&e.id?e.id:"",o=e&&e.category?e.category:"marketing";if(t&&i(o))if("clientify_analytics_plus"===n){var c=document.createElement("script");c.defer=!0,c.src="https://analyticsplusdev.clientify.net/analytics_plus/pixel/"+encodeURIComponent(t),document.head.appendChild(c)}else if("clientify_analytics_classic"===n)!function(e,n,t,o){n[o]=n[o]||function(){(n[o].q=n[o].q||[]).push(arguments)};var i=e.createElement("script"),c=e.getElementsByTagName("script")[0];i.async=1,i.src="https://analytics.clientify.net/tracker.js",c.parentNode.insertBefore(i,c)}(document,window,0,"ana"),window.ana("setTrackerUrl","https://analytics.clientify.net"),window.ana("setTrackingCode",t),window.ana("trackPageview");else if("brevo"===n){var a=document.createElement("script");a.async=!0,a.src="https://cdn.brevo.com/js/sdk-loader.js",document.head.appendChild(a),window.Brevo=window.Brevo||[],window.Brevo.push(["init",{client_key:t}])}else if("openai_chatgpt_ads"===n){if(!window.oaiq){window.oaiq=function(){window.oaiq.q.push(arguments)},window.oaiq.q=[];var r=document.createElement("script");r.async=!0,r.src="https://bzrcdn.openai.com/sdk/oaiq.min.js",document.head.appendChild(r)}window.oaiq("init",{pixelId:t,debug:!0})}else"function"==typeof window.frcnCookieNoticeInjectIntegration?window.frcnCookieNoticeInjectIntegration(e):(window.frcnCookieNoticePendingIntegrations=window.frcnCookieNoticePendingIntegrations||[],window.frcnCookieNoticePendingIntegrations.push(e))}),window.frcnCookieNoticeActivateGated(o)};var t="function"==typeof window.frcnCookieNoticeIsConsentStale&&window.frcnCookieNoticeIsConsentStale();"accepted"!==e(frcnCookieNotice.cookieName)||t||n(),window.frcnCookieNoticeBootstrapped=!0}}"undefined"!=typeof frcnCookieNotice&&function(){window.dataLayer=window.dataLayer||[],window.gtag=window.gtag||function(){window.dataLayer.push(arguments)};var n="accepted"===e(frcnCookieNotice.cookieName)?"granted":"denied",t={ad_storage:n,ad_user_data:n,ad_personalization:n,analytics_storage:n};if("function"==typeof window.frcnCookieNoticeConsentModeState){var o=window.frcnCookieNoticeConsentModeState();o&&(t=o)}"function"==typeof window.frcnCookieNoticeIsConsentStale&&window.frcnCookieNoticeIsConsentStale()&&(t={ad_storage:"denied",ad_user_data:"denied",ad_personalization:"denied",analytics_storage:"denied"});window.gtag("consent","default",t)}(),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",c):c();var o=!1;function i(){var n=document.getElementById("frcn-cookie-reopen");if(n){var t=e(frcnCookieNotice.cookieName);"accepted"!==t&&"rejected"!==t||(n.hidden=!1,o||(o=!0,n.addEventListener("click",function(){document.cookie=frcnCookieNotice.cookieName+"=; path="+frcnCookieNotice.cookiePath+"; max-age=0; SameSite=Lax",frcnCookieNotice.isPolicyPage&&frcnCookieNotice.homeUrl?window.location.href=frcnCookieNotice.homeUrl:window.location.reload()})))}}function c(){if("undefined"!=typeof frcnCookieNotice){var o="undefined"!=typeof frcnCookieNoticeA11y?frcnCookieNoticeA11y:{bannerOpened:"Cookie consent banner opened.",accepted:"Cookies accepted.",rejected:"Cookies rejected."};t(),i();var c=document.getElementById("frcn-cookie-notice");if(c&&!function(n){var t=e(frcnCookieNotice.cookieName);return!("function"==typeof window.frcnCookieNoticeIsConsentStale&&window.frcnCookieNoticeIsConsentStale()||!n||"accepted"!==t&&"rejected"!==t||(n.style.display="none",0))}(c)){var a,r=c.querySelector('[data-frcn-cookie-action="accept"]'),d=c.querySelector('[data-frcn-cookie-action="reject"]'),s=c.classList.contains("frcn-cookie-notice--popup"),f=document.activeElement,u=document.getElementById("frcn-cookie-notice-announcer");a=s?150:20,window.setTimeout(function(){c.classList.remove("frcn-cookie-notice--init"),p(o.bannerOpened),s&&(document.body.classList.add("frcn-cookie-notice-lock-scroll"),r&&r.focus({preventScroll:!0}),document.addEventListener("keydown",k))},a),r&&r.addEventListener("click",function(e){e.preventDefault(),y("accepted")}),d&&d.addEventListener("click",function(e){e.preventDefault(),y("rejected")});var w=c.querySelector('[data-frcn-cookie-action="customize"]');w&&w.addEventListener("click",function(){var e;try{e=new CustomEvent("frcnCookieNoticeCustomize")}catch(n){(e=document.createEvent("CustomEvent")).initCustomEvent("frcnCookieNoticeCustomize",!0,!0,null)}document.dispatchEvent(e)});var l=!1}}function p(e){u&&(u.textContent=e)}function m(e){var n=Array.prototype.slice.call(c.querySelectorAll('a[href], button, input, [tabindex]:not([tabindex="-1"])'));if(n.length){var t=n[0],o=n[n.length-1];e.shiftKey&&document.activeElement===t?(e.preventDefault(),o.focus()):e.shiftKey||document.activeElement!==o||(e.preventDefault(),t.focus())}}function k(e){"Tab"===e.key?m(e):"Escape"!==e.key&&"Esc"!==e.key||(e.preventDefault(),y("rejected"))}function y(e){l||(l=!0,function(e){var n=24*parseInt(frcnCookieNotice.expirationDays,10)*60*60,t="https:"===window.location.protocol?"; Secure":"";document.cookie=frcnCookieNotice.cookieName+"="+e+"; path="+frcnCookieNotice.cookiePath+"; max-age="+n+"; SameSite=Lax"+t}(e),function(e){var n="accepted"===e?"granted":"denied";window.dataLayer=window.dataLayer||[],window.gtag=window.gtag||function(){window.dataLayer.push(arguments)},window.gtag("consent","update",{ad_storage:n,ad_user_data:n,ad_personalization:n,analytics_storage:n})}(e),p("accepted"===e?o.accepted:o.rejected),function(){c.classList.add("frcn-cookie-notice--hidden"),document.body.classList.remove("frcn-cookie-notice-lock-scroll"),document.removeEventListener("keydown",k),s&&f&&"function"==typeof f.focus&&f.focus({preventScroll:!0});window.setTimeout(function(){c.parentNode&&c.parentNode.removeChild(c)},300)}(),i(),function(e){var n;try{n=new CustomEvent("frcnCookieConsent",{detail:{consent:e}})}catch(t){(n=document.createEvent("CustomEvent")).initCustomEvent("frcnCookieConsent",!0,!0,{consent:e})}document.dispatchEvent(n)}(e),function(e){var n=new FormData;n.append("action","frcn_get_cookie_notice_log_nonce"),fetch(frcnCookieNotice.ajaxUrl,{method:"POST",credentials:"same-origin",body:n}).then(function(e){return e.json()}).then(function(n){if(n&&n.success&&n.data){var t=new FormData;return t.append("action","frcn_log_cookie_consent"),t.append("nonce",n.data.nonce),t.append("decision",e),fetch(frcnCookieNotice.ajaxUrl,{method:"POST",credentials:"same-origin",body:t})}}).catch(function(){})}(e),"accepted"===e&&n())}}}(); \ No newline at end of file diff --git a/frontconsent.php b/frontconsent.php index e316256..972e280 100644 --- a/frontconsent.php +++ b/frontconsent.php @@ -3,7 +3,7 @@ * Plugin Name: FrontConsent * Plugin URI: https://wordpress.org/plugins/frontconsent/ * Description: GDPR/ePrivacy-compliant cookie consent banner for WordPress, following the AEPD guide. Configurable banner, Google Consent Mode v2, and tracking integrations that only load after consent. - * Version: 1.0.1 + * Version: 1.0.2-beta.gdpr * Author: CloseTechnology * Author URI: https://close.technology * Text Domain: frontconsent @@ -26,7 +26,7 @@ defined( 'ABSPATH' ) || die( 'No script kiddies please!' ); -define( 'FRCN_VERSION', '1.0.1' ); +define( 'FRCN_VERSION', '1.0.2-beta.gdpr' ); define( 'FRCN_PLUGIN', __FILE__ ); define( 'FRCN_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'FRCN_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); diff --git a/includes/Admin/Settings.php b/includes/Admin/Settings.php index 1c90015..87da31e 100644 --- a/includes/Admin/Settings.php +++ b/includes/Admin/Settings.php @@ -11,6 +11,7 @@ namespace FrontConsent\Admin; use FrontConsent\Frontend\CookieNotice; +use FrontConsent\GDPR_Cookie_Compliance_Migration; defined( 'ABSPATH' ) || exit; @@ -56,6 +57,82 @@ public function __construct() { add_action( 'admin_menu', array( $this, 'register_menu' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); + add_action( 'admin_post_frcn_import_gdpr_cookie_compliance', array( $this, 'handle_gdpr_cookie_compliance_import' ) ); + } + + /** + * Run the GDPR Cookie Compliance importer from its admin-post form. + * + * @return void + */ + public function handle_gdpr_cookie_compliance_import() { + if ( ! current_user_can( 'manage_options' ) ) { + wp_die( esc_html__( 'You do not have permission to do this.', 'frontconsent' ), '', array( 'response' => 403 ) ); + } + + check_admin_referer( 'frcn_import_gdpr_cookie_compliance' ); + + $report = GDPR_Cookie_Compliance_Migration::run(); + if ( null !== $report ) { + set_transient( 'frcn_gdpr_import_report_' . get_current_user_id(), $report, 5 * MINUTE_IN_SECONDS ); + } + + wp_safe_redirect( admin_url( 'options-general.php?page=' . $this->page_slug ) ); + exit; + } + + /** + * Render the GDPR Cookie Compliance import prompt, or the report of the + * import the current user just ran. + * + * @return void + */ + private function render_gdpr_import_notice() { + $report_key = 'frcn_gdpr_import_report_' . get_current_user_id(); + $report = get_transient( $report_key ); + + if ( is_array( $report ) ) { + delete_transient( $report_key ); + $labels = array( + GDPR_Cookie_Compliance_Migration::STATUS_MIGRATED => __( 'Imported', 'frontconsent' ), + GDPR_Cookie_Compliance_Migration::STATUS_EXISTING => __( 'Kept existing', 'frontconsent' ), + GDPR_Cookie_Compliance_Migration::STATUS_UNSUPPORTED => __( 'Not supported', 'frontconsent' ), + GDPR_Cookie_Compliance_Migration::STATUS_REVIEW => __( 'Needs review', 'frontconsent' ), + ); + ?> +
+ +
We use cookies.
', + 'moove_gdpr_infobar_accept_button_label' => 'Accept all', + 'moove_gdpr_consent_expiration' => '90', + 'moove_gdpr_infobar_position' => 'left', + ), + array( 'cookie_notice_accept_label' => 'OK' ) + ); + + $this->assertSame( 'We use cookies.', $result['settings']['cookie_notice_message'] ); + $this->assertSame( 'OK', $result['settings']['cookie_notice_accept_label'] ); + $this->assertSame( 90, $result['settings']['cookie_notice_expiration_days'] ); + $this->assertSame( 'bottom-left', $result['settings']['cookie_notice_position'] ); + $this->assertSame( 'box', $result['settings']['cookie_notice_layout'] ); + } + + public function test_banner_is_enabled_on_import_unless_already_chosen() { + $source = $this->source( + array( + 'ga4' => 2, + 'ga4_id' => 'G-ABC123', + ) + ); + + $fresh = Importer::build( $source, array() ); + $this->assertTrue( $fresh['settings']['enable_cookie_notice'] ); + + $disabled = Importer::build( $source, array( 'enable_cookie_notice' => false ) ); + $this->assertFalse( $disabled['settings']['enable_cookie_notice'] ); + } + + public function test_google_tag_ids_are_reported_as_unsupported() { + $result = Importer::build( + $this->source( + array( + 'gtm' => 2, + 'gtm_id' => 'AW-123456789', + ) + ), + array() + ); + + $this->assertArrayNotHasKey( 'cookie_notice_tracking_integrations', $result['settings'] ); + $this->assertSame( array( 'unsupported' ), $this->statuses( $result['report'] ) ); + } + + public function test_run_returns_null_without_source_and_never_writes_source() { + delete_option( Importer::SOURCE_OPTION ); + $this->assertNull( Importer::run() ); + + $source = $this->source( + array( + 'ga4' => 2, + 'ga4_id' => 'G-ABC123', + ) + ); + update_option( Importer::SOURCE_OPTION, $source ); + delete_option( 'frontconsent_settings' ); + + Importer::run(); + + $this->assertSame( $source, get_option( Importer::SOURCE_OPTION ) ); + $this->assertSame( 'G-ABC123', get_option( 'frontconsent_settings' )['cookie_notice_tracking_integrations'][0]['id'] ); + + delete_option( Importer::SOURCE_OPTION ); + delete_option( Importer::DONE_FLAG ); + delete_option( 'frontconsent_settings' ); + } + + public function test_unknown_enabled_integration_is_reported() { + $result = Importer::build( $this->source( array( 'somethingnew' => 2 ) ), array() ); + + $this->assertSame( array( 'unsupported' ), $this->statuses( $result['report'] ) ); + $this->assertArrayNotHasKey( 'enable_cookie_notice', $result['settings'] ); + } + + public function test_gtm4wp_container_is_imported_as_gtm() { + $result = Importer::build( + $this->source( array( 'gtm4wp' => 2 ) ), + array(), + array( + 'gtm-code' => 'GTM-TWKQP7Z', + 'gtm-code-placement' => 3, + ) + ); + + $this->assertSame( + array( + array( + 'type' => 'gtm', + 'id' => 'GTM-TWKQP7Z', + ), + ), + $result['settings']['cookie_notice_tracking_integrations'] + ); + $this->assertSame( array( 'migrated' ), $this->statuses( $result['report'] ) ); + } + + public function test_gtm4wp_still_placing_the_container_is_flagged_for_review() { + $result = Importer::build( + $this->source( array( 'gtm4wp' => 2 ) ), + array(), + array( + 'gtm-containers' => array( array( 'id' => 'gtm-abc123' ), array( 'id' => 'GTM-OTHER1' ) ), + 'gtm-code-placement' => 0, + ) + ); + + $this->assertSame( 'GTM-ABC123', $result['settings']['cookie_notice_tracking_integrations'][0]['id'] ); + $this->assertSame( array( 'needs_review' ), $this->statuses( $result['report'] ) ); + } + + public function test_gtm4wp_without_a_container_id_is_reported_and_not_imported() { + $result = Importer::build( $this->source( array( 'gtm4wp' => 2 ) ), array(), array() ); + + $this->assertArrayNotHasKey( 'cookie_notice_tracking_integrations', $result['settings'] ); + $this->assertSame( array( 'needs_review' ), $this->statuses( $result['report'] ) ); + } + + public function test_consent_mode_gtm_alone_enables_the_banner() { + $result = Importer::build( + $this->source( + array( + 'gtmc2' => 2, + 'gtmc2_id' => 'GTM-CMV2AB', + ) + ), + array() + ); + + $this->assertTrue( $result['settings']['enable_cookie_notice'] ); + } + + public function test_top_position_is_reported_and_not_imported() { + $result = Importer::build( array( 'moove_gdpr_infobar_position' => 'top' ), array() ); + + $this->assertArrayNotHasKey( 'cookie_notice_position', $result['settings'] ); + $this->assertArrayNotHasKey( 'cookie_notice_layout', $result['settings'] ); + $this->assertSame( array( 'needs_review' ), $this->statuses( $result['report'] ) ); + } + + public function test_block_boundaries_keep_a_separator_in_the_message() { + $result = Importer::build( array( 'moove_gdpr_info_bar_content' => 'One.
Two.
' ), array() ); + + $this->assertSame( "One.\nTwo.", $result['settings']['cookie_notice_message'] ); + } + + public function test_integration_disabled_in_the_source_is_skipped() { + $result = Importer::build( + $this->source( + array( + 'ga4' => 0, + 'ga4_id' => 'G-ABC123', + ) + ), + array() + ); + + $this->assertSame( array(), $result['report'] ); + $this->assertArrayNotHasKey( 'cookie_notice_tracking_integrations', $result['settings'] ); + } + + public function test_local_policy_link_sets_the_policy_page_and_foreign_link_is_reported() { + $page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + + $result = Importer::build( array( 'moove_gdpr_info_bar_content' => 'See policy.
' ), array() ); + $this->assertSame( $page_id, $result['settings']['cookie_notice_policy_page_id'] ); + + $result = Importer::build( array( 'moove_gdpr_info_bar_content' => 'See policy.
' ), array() ); + $this->assertContains( 'needs_review', $this->statuses( $result['report'] ) ); + $this->assertArrayNotHasKey( 'cookie_notice_policy_page_id', $result['settings'] ); + } + + public function test_import_stays_available_when_source_data_is_unreadable() { + update_option( Importer::SOURCE_OPTION, array( 'gdin_values' => '{not json' ) ); + delete_option( Importer::DONE_FLAG ); + + Importer::run(); + + $this->assertFalse( get_option( Importer::DONE_FLAG ) ); + + delete_option( Importer::SOURCE_OPTION ); + delete_option( 'frontconsent_settings' ); + } + + public function test_every_removed_link_is_counted_in_the_report() { + $page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + + $html = 'a b c'; + $result = Importer::build( array( 'moove_gdpr_info_bar_content' => $html ), array() ); + + $this->assertSame( $page_id, $result['settings']['cookie_notice_policy_page_id'] ); + $review = array_values( array_filter( $result['report'], static function ( $row ) { + return 'needs_review' === $row['status']; + } ) ); + $this->assertCount( 1, $review ); + $this->assertStringContainsString( '2', $review[0]['note'] ); + } + + public function test_position_is_not_mixed_with_a_partially_configured_layout() { + foreach ( array( array( 'cookie_notice_layout' => 'bar' ), array( 'cookie_notice_position' => 'bottom-right' ) ) as $current ) { + $result = Importer::build( array( 'moove_gdpr_infobar_position' => 'left' ), $current ); + + $this->assertSame( $current, $result['settings'] ); + $this->assertSame( array( 'needs_review' ), $this->statuses( $result['report'] ) ); + } + } +}