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
119 changes: 119 additions & 0 deletions includes/Frontend/PrivacyEmbeds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Cookieless video embeds for FrontConsent.
*
* @package FrontConsent
* @author Closemarketing
* @copyright 2026 Closemarketing
* @version 1.0
*/

namespace FrontConsent\Frontend;

defined( 'ABSPATH' ) || exit;

/**
* PrivacyEmbeds class.
*
* Switches YouTube and Vimeo iframes to their cookieless modes before the
* page reaches the visitor: YouTube's privacy-enhanced youtube-nocookie.com
* domain, and Vimeo's dnt=1 (do not track) flag. Editors keep pasting normal
* YouTube/Vimeo links; WordPress stores the regular embed URL and this class
* rewrites it on output, so videos no longer set cookies before consent.
*
* The page is buffered (ob_start()) and only <iframe> 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, '<iframe' ) || ( false === stripos( $html, 'youtube.com/embed' ) && false === stripos( $html, 'player.vimeo.com/video' ) ) ) {
return $html;
}

return (string) preg_replace_callback(
'/<iframe\b(?:[^>"\']|"[^"]*"|\'[^\']*\')*>/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( '/(?:\?|&amp;|&)dnt=/i', $url ) ) {
return $url;
}

return $url . ( false === strpos( $url, '?' ) ? '?' : '&amp;' ) . 'dnt=1';
},
(string) $tag
);
},
$html
);
}
}
4 changes: 4 additions & 0 deletions includes/Plugin_Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -77,6 +80,7 @@ private function init() {
Migration::maybe_run();

new Frontend\CookieNotice();
new Frontend\PrivacyEmbeds();

$this->load_modules();
}
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
86 changes: 86 additions & 0 deletions tests/Unit/PrivacyEmbedsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Tests for the cookieless YouTube/Vimeo embed rewrite.
*
* @package FrontConsent
*/

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

class PrivacyEmbedsTest extends TestCase {

public function tear_down() {
delete_option( 'frontconsent_settings' );
parent::tear_down();
}

/**
* A youtube.com embed is switched to the privacy-enhanced
* youtube-nocookie.com domain, including a lazy-loaded copy of the URL.
*/
public function test_youtube_iframe_is_switched_to_nocookie_domain() {
$html = '<iframe src="https://www.youtube.com/embed/abc123?rel=0" data-lazy-src="https://youtube.com/embed/abc123"></iframe>';

$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 = '<iframe title="Video" width="500" height="281" src="https://www.youtube.com/embed/-O1XnBORs9w?feature=oembed" frameborder="0" allowfullscreen></iframe>';

$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( '<iframe src="https://player.vimeo.com/video/123"></iframe>' )
);
$this->assertStringContainsString(
'src="https://player.vimeo.com/video/123?h=abc&amp;dnt=1"',
PrivacyEmbeds::rewrite_html( '<iframe src="https://player.vimeo.com/video/123?h=abc"></iframe>' )
);

$already = '<iframe src="https://player.vimeo.com/video/123?h=abc&amp;dnt=1"></iframe>';
$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 = '<a href="https://www.youtube.com/embed/abc123">Watch</a><iframe src="https://example.com/"></iframe>';

$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' );
}
}
Loading