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/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' ); + } +}