diff --git a/includes/Frontend/CookieNotice.php b/includes/Frontend/CookieNotice.php index 6d54ddc..0448402 100644 --- a/includes/Frontend/CookieNotice.php +++ b/includes/Frontend/CookieNotice.php @@ -83,6 +83,7 @@ public function __construct() { add_action( 'wp_footer', array( $this, 'render_banner' ) ); add_filter( 'script_loader_tag', array( $this, 'gate_analytics_scripts' ), 10, 2 ); add_filter( 'gtm4wp_consent_mode_default_enabled', array( $this, 'filter_gtm4wp_consent_default' ) ); + add_filter( 'rocket_delay_js_exclusions', array( $this, 'exclude_from_wp_rocket_delay_js' ) ); } // The endpoints must stay available for logged-out and logged-in visitors alike. @@ -181,6 +182,32 @@ public function gate_analytics_scripts( $tag, $handle ) { return preg_replace( '/^ tag. + * + * @param string[] $exclusions WP Rocket delay JS exclusion patterns. + * @return string[] + */ + public function exclude_from_wp_rocket_delay_js( $exclusions ) { + $exclusions = is_array( $exclusions ) ? $exclusions : array(); + + return array_merge( + $exclusions, + array( + '/frontconsent/assets/', + 'frontconsent-cookie-notice-js', + 'frontconsent-consent-mode-default', + 'frontconsent-consent-bootstrap', + 'frcnCookieNotice', + ) + ); + } + /** * Stop GTM4WP from sending its own Consent Mode "default" command (it can * grant analytics storage before consent): FrontConsent already sends the diff --git a/readme.md b/readme.md index 2baa05e..7effb2a 100644 --- a/readme.md +++ b/readme.md @@ -10,6 +10,7 @@ Repository for FrontConsent, a WordPress plugin providing a GDPR/ePrivacy-compli - **Migration from FrontBlocks** — automatically imports settings and stats from FrontBlocks Site Tools' bundled Cookie Notice module, which FrontConsent replaces. - **Import from GDPR Cookie Compliance** — explicit "Import settings" action that maps its Integrations tab (GA4, GTM, and the container configured in GTM4WP) and banner texts/expiration/position into FrontConsent without overwriting existing values; unsupported integrations (Google Ads, Meta Pixel, Microsoft UET/Clarity, reCAPTCHA) are reported, not imported. See `includes/GDPR_Cookie_Compliance_Migration.php`. - **Consent-gated attribution cookies** — WooCommerce order attribution scripts (`sbjs_*` cookies) stay inert until the visitor accepts (filter `frcn_cookie_notice_gated_script_handles`), and GTM4WP's own Consent Mode default is suppressed so FrontConsent's denied default is the only one (filter `frcn_cookie_notice_suppress_gtm4wp_consent_default`). +- **WP Rocket compatibility** — FrontConsent's scripts are excluded from WP Rocket's "Delay JavaScript execution" (filter `rocket_delay_js_exclusions`), so the banner responds to the first click and Consent Mode reaches Google tags before they fire. - **Tracking cookies deleted on rejection** — rejecting, withdrawing an earlier acceptance, or returning after a rejection deletes known analytics/marketing cookies (`_ga`, `_ga_*`, `_gid`, `_gcl_*`, `_fbp`, `__obref`, `sbjs_*`…) on the host and its parent domains, plus matching localStorage/sessionStorage entries (`oaiq_*`), so nothing set while consent was granted stays behind (filter `frcn_cookie_notice_purge_cookies`). - **Compliance scanner** — a Scanner tab that, on an administrator's click, sends the site's home URL to the tuwebcumple.es API (`https://tuwebcumple.es/api/v1/`, filter `frontconsent_scanner_api_url`), follows the scan until it finishes and shows the grade, headline and per-requirement results; a dashboard widget shows the last grade. Results are stored in the `frontconsent_scan_result` option. One scan every 10 minutes per site, on top of the API's own rate limits. See `includes/Admin/Scanner.php`. - **Extensible settings tabs** — the settings page renders as a tab shell (Settings, plus a PRO upsell tab by default). A companion plugin such as FrontConsent PRO can add its own tab via the `frontconsent_settings_tabs` filter and render its panel on the `frontconsent_settings_form_tab_panels` action (fields saved through the main form) or `frontconsent_settings_tab_panels` action (a panel with its own form, e.g. a License tab). diff --git a/tests/Unit/CookieNoticeWpRocketTest.php b/tests/Unit/CookieNoticeWpRocketTest.php new file mode 100644 index 0000000..dac1ecc --- /dev/null +++ b/tests/Unit/CookieNoticeWpRocketTest.php @@ -0,0 +1,145 @@ + true ) ); + $this->cookie_notice = new CookieNotice(); + } + + /** + * Reset the scripts and hooks registered by the test. + * + * @return void + */ + public function tear_down() { + wp_deregister_script( 'frontconsent-cookie-notice' ); + wp_deregister_script( 'frontconsent-consent-mode-default' ); + wp_deregister_script( 'frontconsent-consent-bootstrap' ); + wp_deregister_style( 'frontconsent-cookie-notice' ); + + remove_all_filters( 'rocket_delay_js_exclusions' ); + remove_all_actions( 'wp_head' ); + remove_all_actions( 'wp_footer' ); + remove_all_actions( 'wp_enqueue_scripts' ); + delete_option( 'frontconsent_settings' ); + + parent::tear_down(); + } + + /** + * The filter is registered whenever the banner is enabled, and keeps the + * exclusions WP Rocket or the site admin already configured. + * + * @return void + */ + public function test_adds_exclusions_and_keeps_existing_ones() { + $this->assertNotFalse( has_filter( 'rocket_delay_js_exclusions', array( $this->cookie_notice, 'exclude_from_wp_rocket_delay_js' ) ) ); + + $exclusions = apply_filters( 'rocket_delay_js_exclusions', array( 'existing-pattern' ) ); + + $this->assertContains( 'existing-pattern', $exclusions ); + $this->assertGreaterThan( 1, count( $exclusions ) ); + } + + /** + * Every ', $patterns ) ); + $this->assertFalse( $this->is_excluded( '', $patterns ) ); + } + + /** + * Print every script FrontConsent outputs on a frontend page. + * + * @return string + */ + private function render_frontconsent_scripts() { + ob_start(); + $this->cookie_notice->render_consent_mode_default(); + $this->cookie_notice->render_consent_bootstrap_script(); + $this->cookie_notice->enqueue_assets(); + wp_print_scripts( 'frontconsent-cookie-notice' ); + return ob_get_clean(); + } + + /** + * Extract the