Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions includes/Frontend/CookieNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -181,6 +182,32 @@ public function gate_analytics_scripts( $tag, $handle ) {
return preg_replace( '/^<script\b/i', '<script type="text/plain" data-frcn-consent="analytics"', (string) $tag, 1 );
}

/**
* Keep the consent scripts out of WP Rocket's "Delay JavaScript execution".
*
* Delayed until the first scroll or click, the banner's buttons do nothing
* on that first click, a returning visitor's Consent Mode "granted" reaches
* the tags after they have already fired, and leftover tracking cookies are
* not cleared. Each pattern is matched against the whole <script> 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
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
145 changes: 145 additions & 0 deletions tests/Unit/CookieNoticeWpRocketTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php
/**
* Tests for CookieNotice's WP Rocket "Delay JavaScript execution" exclusions.
*
* @package FrontConsent
*/

use FrontConsent\Frontend\CookieNotice;
use Yoast\WPTestUtils\WPIntegration\TestCase;

/**
* WP Rocket delay JS compatibility test case.
*/
class CookieNoticeWpRocketTest extends TestCase {

/**
* CookieNotice instance under test.
*
* @var CookieNotice
*/
private $cookie_notice;

/**
* Enable the banner and build the CookieNotice instance.
*
* @return void
*/
public function set_up() {
parent::set_up();
update_option( 'frontconsent_settings', array( 'enable_cookie_notice' => 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 <script> tag FrontConsent prints must match one of its patterns,
* applied the same way WP Rocket does, or WP Rocket would delay it until
* the visitor's first interaction.
*
* @return void
*/
public function test_every_frontconsent_script_tag_is_excluded() {
$patterns = $this->cookie_notice->exclude_from_wp_rocket_delay_js( array() );
$tags = $this->get_script_tags( $this->render_frontconsent_scripts() );

// Consent Mode default, bootstrap, localized data and the banner script.
$this->assertGreaterThanOrEqual( 4, count( $tags ) );

foreach ( $tags as $tag ) {
$this->assertTrue( $this->is_excluded( $tag, $patterns ), 'Not excluded from WP Rocket delay JS: ' . $tag );
}
}

/**
* The patterns must not be so broad that they exclude unrelated scripts.
*
* @return void
*/
public function test_unrelated_scripts_are_not_excluded() {
$patterns = $this->cookie_notice->exclude_from_wp_rocket_delay_js( array() );

$this->assertFalse( $this->is_excluded( '<script src="https://example.com/wp-content/plugins/other-plugin/assets/script.js"></script>', $patterns ) );
$this->assertFalse( $this->is_excluded( '<script>window.dataLayer = window.dataLayer || [];</script>', $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 <script> tags with the same regex WP Rocket uses.
*
* @param string $html HTML.
* @return string[]
*/
private function get_script_tags( $html ) {
preg_match_all( '/<\s*script(?<attr>\s*[^>]*?)?>(?<content>.*?)?<\s*\/\s*script\s*>/ims', $html, $matches );

return $matches[0];
}

/**
* Whether WP Rocket would leave this tag alone, using its own matching:
* each pattern is a regex fragment tested against the whole tag.
*
* @param string $tag Script tag HTML.
* @param string[] $patterns Exclusion patterns.
* @return bool
*/
private function is_excluded( $tag, $patterns ) {
foreach ( $patterns as $pattern ) {
$pattern = str_replace( array( '+', '?ver', '#' ), array( '\+', '\?ver', '\#' ), $pattern );

if ( preg_match( "#{$pattern}#i", $tag ) ) {
return true;
}
}

return false;
}
}
Loading