From 09f89aa075f3d3650ba00a5723d91496dbeee70f Mon Sep 17 00:00:00 2001 From: amulero Date: Fri, 6 Nov 2026 12:36:56 +0100 Subject: [PATCH 1/2] Serve YouTube embeds from youtube-nocookie.com and Vimeo with dnt=1 tuwebcumple.es flagged "YouTube videos set cookies before consent". Editors paste normal YouTube/Vimeo links and WordPress stores the regular embed URL; the new PrivacyEmbeds class rewrites those iframes on output (src and lazy-load copies such as WP Rocket's data-lazy-src) to the cookieless modes, so videos stay visible but set no cookies before the visitor decides. Only active while the Cookie Notice is enabled; filter frcn_privacy_enhanced_embeds turns it off. Version 1.0.3-beta.youtube. Co-Authored-By: Claude Opus 5.5 --- frontconsent.php | 4 +- includes/Frontend/PrivacyEmbeds.php | 119 ++++++++++++++++++++++++++++ includes/Plugin_Main.php | 4 + readme.md | 1 + readme.txt | 3 +- tests/Unit/PrivacyEmbedsTest.php | 86 ++++++++++++++++++++ 6 files changed, 214 insertions(+), 3 deletions(-) create mode 100644 includes/Frontend/PrivacyEmbeds.php create mode 100644 tests/Unit/PrivacyEmbedsTest.php diff --git a/frontconsent.php b/frontconsent.php index 6780db7..60139b0 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.2 + * Version: 1.0.3-beta.youtube * 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.2' ); +define( 'FRCN_VERSION', '1.0.3-beta.youtube' ); define( 'FRCN_PLUGIN', __FILE__ ); define( 'FRCN_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'FRCN_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); diff --git a/includes/Frontend/PrivacyEmbeds.php b/includes/Frontend/PrivacyEmbeds.php new file mode 100644 index 0000000..6708107 --- /dev/null +++ b/includes/Frontend/PrivacyEmbeds.php @@ -0,0 +1,119 @@ + opening tags are + * touched. Every URL-carrying attribute in them is rewritten — src, plus + * lazy-load copies such as data-src or WP Rocket's data-lazy-src — so + * lazy-loaded embeds are covered too. + * + * @since 1.0.3 + */ +class PrivacyEmbeds { + + /** + * Constructor. + */ + public function __construct() { + if ( is_admin() ) { + return; + } + + add_action( 'template_redirect', array( $this, 'maybe_start_output_buffer' ) ); + } + + /** + * Whether YouTube/Vimeo iframes are switched to their cookieless modes. + * + * Only while the Cookie Notice is enabled: with it off, FrontConsent + * leaves the page output untouched. + * + * @return bool + */ + public static function is_enabled() { + $options = get_option( 'frontconsent_settings', array() ); + $enabled = (bool) ( $options['enable_cookie_notice'] ?? false ); + + /** + * Filters whether FrontConsent rewrites YouTube embeds to + * youtube-nocookie.com and adds Vimeo's "dnt=1" flag. + * + * @param bool $enabled Default true while the Cookie Notice is enabled. + */ + return (bool) apply_filters( 'frcn_privacy_enhanced_embeds', $enabled ); + } + + /** + * Start buffering the frontend HTML output, only for regular page + * requests and only when the feature is enabled. + * + * @return void + */ + public function maybe_start_output_buffer() { + if ( is_admin() || wp_doing_ajax() || wp_doing_cron() || ( function_exists( 'wp_is_json_request' ) && wp_is_json_request() ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) { + return; + } + + if ( ! self::is_enabled() ) { + return; + } + + ob_start( array( __CLASS__, 'rewrite_html' ) ); + } + + /** + * Switch YouTube and Vimeo iframes in the given HTML to their cookieless + * modes. + * + * Public static so it's directly unit-testable. + * + * @param string $html HTML to scan. + * @return string Rewritten HTML. + */ + public static function rewrite_html( $html ) { + if ( '' === $html || false === stripos( $html, '"\']|"[^"]*"|\'[^\']*\')*>/i', + function ( $matches ) { + $tag = preg_replace( '#(//)(?:www\.)?youtube\.com/embed/#i', '$1www.youtube-nocookie.com/embed/', $matches[0] ); + + return (string) preg_replace_callback( + '#(player\.vimeo\.com/video/[^"\'\s>]*)#i', + function ( $vimeo ) { + $url = $vimeo[1]; + + if ( preg_match( '/(?:\?|&|&)dnt=/i', $url ) ) { + return $url; + } + + return $url . ( false === strpos( $url, '?' ) ? '?' : '&' ) . 'dnt=1'; + }, + (string) $tag + ); + }, + $html + ); + } +} diff --git a/includes/Plugin_Main.php b/includes/Plugin_Main.php index 18c413b..a8830b9 100644 --- a/includes/Plugin_Main.php +++ b/includes/Plugin_Main.php @@ -60,6 +60,9 @@ private function init() { if ( ! class_exists( 'FrontConsent\Frontend\CookieNotice' ) ) { require_once FRCN_PLUGIN_PATH . 'includes/Frontend/CookieNotice.php'; } + if ( ! class_exists( 'FrontConsent\Frontend\PrivacyEmbeds' ) ) { + require_once FRCN_PLUGIN_PATH . 'includes/Frontend/PrivacyEmbeds.php'; + } if ( ! class_exists( 'FrontConsent\Migration' ) ) { require_once FRCN_PLUGIN_PATH . 'includes/Migration.php'; } @@ -77,6 +80,7 @@ private function init() { Migration::maybe_run(); new Frontend\CookieNotice(); + new Frontend\PrivacyEmbeds(); $this->load_modules(); } diff --git a/readme.md b/readme.md index 36a2290..1ba4f25 100644 --- a/readme.md +++ b/readme.md @@ -5,6 +5,7 @@ Repository for FrontConsent, a WordPress plugin providing a GDPR/ePrivacy-compli ## Functionalities - **Cookie Notice** — configurable cookie consent banner (bar/box/popup) that gates Google Tag Manager, GA4, and supported tracking integrations (Clientify, Brevo, ChatGPT Ads) behind consent, with Google Consent Mode v2 support for compatibility with Google Site Kit and other analytics plugins. +- **Cookieless video embeds** — YouTube iframes are switched to `youtube-nocookie.com` and Vimeo iframes get `dnt=1`, so embedded videos set no cookies before consent (filter `frcn_privacy_enhanced_embeds`). - **Acceptance stats** — aggregate accepted/rejected counter on the settings page. - **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`. diff --git a/readme.txt b/readme.txt index 677ee6f..dde821e 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Tags: cookies, consent, gdpr, cookie notice, aepd Requires at least: 5.8 Tested up to: 7.1 Stable tag: 1.0.2 -Version: 1.0.2 +Version: 1.0.3-beta.youtube License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -90,6 +90,7 @@ Service provided by OpenAI: [Terms of Use](https://openai.com/policies/terms-of- = Unreleased = * Added a Scanner tab that checks the site's cookie compliance through tuwebcumple.es and lists the result per requirement, plus a dashboard widget with the last grade. +* YouTube embeds now load from `youtube-nocookie.com` and Vimeo embeds get `dnt=1`, so videos set no cookies before the visitor decides. Editors keep pasting normal links; the URL is rewritten on output (lazy-loaded embeds included). Turn it off with the `frcn_privacy_enhanced_embeds` filter. = 1.0.2 = * Added an importer for GDPR Cookie Compliance settings in a new Integrations tab: migrates banner texts, layout, position, policy link and tracking IDs (GTM, GA4, GT-/AW-), enables the banner and reports any integration it couldn't map. diff --git a/tests/Unit/PrivacyEmbedsTest.php b/tests/Unit/PrivacyEmbedsTest.php new file mode 100644 index 0000000..c820208 --- /dev/null +++ b/tests/Unit/PrivacyEmbedsTest.php @@ -0,0 +1,86 @@ +'; + + $result = PrivacyEmbeds::rewrite_html( $html ); + + $this->assertStringContainsString( 'src="https://www.youtube-nocookie.com/embed/abc123?rel=0"', $result ); + $this->assertStringContainsString( 'data-lazy-src="https://www.youtube-nocookie.com/embed/abc123"', $result ); + $this->assertStringNotContainsString( '//www.youtube.com/embed', $result ); + } + + /** + * The oEmbed markup WordPress stores for a pasted YouTube link keeps every + * other attribute untouched; only the domain changes. + */ + public function test_oembed_markup_only_changes_the_domain() { + $html = ''; + + $this->assertSame( + str_replace( 'www.youtube.com', 'www.youtube-nocookie.com', $html ), + PrivacyEmbeds::rewrite_html( $html ) + ); + } + + /** + * A Vimeo player embed gets dnt=1, appended to an existing query string + * when there is one, and is left alone when it already carries it. + */ + public function test_vimeo_iframe_gets_do_not_track_flag_once() { + $this->assertStringContainsString( + 'src="https://player.vimeo.com/video/123?dnt=1"', + PrivacyEmbeds::rewrite_html( '' ) + ); + $this->assertStringContainsString( + 'src="https://player.vimeo.com/video/123?h=abc&dnt=1"', + PrivacyEmbeds::rewrite_html( '' ) + ); + + $already = ''; + $this->assertSame( $already, PrivacyEmbeds::rewrite_html( $already ) ); + } + + /** + * Only iframes are rewritten: a plain link to a YouTube video stays as is. + */ + public function test_youtube_links_outside_iframes_are_untouched() { + $html = 'Watch'; + + $this->assertSame( $html, PrivacyEmbeds::rewrite_html( $html ) ); + } + + /** + * The rewrite follows the Cookie Notice: off while it is disabled, on + * while it is enabled, and the filter can still turn it off. + */ + public function test_is_enabled_follows_the_cookie_notice_and_the_filter() { + $this->assertFalse( PrivacyEmbeds::is_enabled() ); + + update_option( 'frontconsent_settings', array( 'enable_cookie_notice' => true ) ); + $this->assertTrue( PrivacyEmbeds::is_enabled() ); + + add_filter( 'frcn_privacy_enhanced_embeds', '__return_false' ); + $this->assertFalse( PrivacyEmbeds::is_enabled() ); + remove_filter( 'frcn_privacy_enhanced_embeds', '__return_false' ); + } +} From c327c94c02a93154b8972e93d91e664e6cef1dab Mon Sep 17 00:00:00 2001 From: amulero Date: Tue, 10 Nov 2026 16:38:02 +0100 Subject: [PATCH 2/2] Leave version and changelog to the release commit The version bump and changelog entry are done once on main when 1.0.3 is released, so the open PRs do not conflict with each other. Co-Authored-By: Claude Opus 5.5 --- frontconsent.php | 4 ++-- readme.txt | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/frontconsent.php b/frontconsent.php index 60139b0..6780db7 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.3-beta.youtube + * Version: 1.0.2 * 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.3-beta.youtube' ); +define( 'FRCN_VERSION', '1.0.2' ); define( 'FRCN_PLUGIN', __FILE__ ); define( 'FRCN_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'FRCN_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); diff --git a/readme.txt b/readme.txt index dde821e..677ee6f 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Tags: cookies, consent, gdpr, cookie notice, aepd Requires at least: 5.8 Tested up to: 7.1 Stable tag: 1.0.2 -Version: 1.0.3-beta.youtube +Version: 1.0.2 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -90,7 +90,6 @@ Service provided by OpenAI: [Terms of Use](https://openai.com/policies/terms-of- = Unreleased = * Added a Scanner tab that checks the site's cookie compliance through tuwebcumple.es and lists the result per requirement, plus a dashboard widget with the last grade. -* YouTube embeds now load from `youtube-nocookie.com` and Vimeo embeds get `dnt=1`, so videos set no cookies before the visitor decides. Editors keep pasting normal links; the URL is rewritten on output (lazy-loaded embeds included). Turn it off with the `frcn_privacy_enhanced_embeds` filter. = 1.0.2 = * Added an importer for GDPR Cookie Compliance settings in a new Integrations tab: migrates banner texts, layout, position, policy link and tracking IDs (GTM, GA4, GT-/AW-), enables the banner and reports any integration it couldn't map.