From 52e26982791d47c643392041fb2cce31ebb59971 Mon Sep 17 00:00:00 2001 From: FrankZamora Date: Thu, 27 Nov 2025 13:37:36 -0600 Subject: [PATCH] feat(youtube-facade): Phase 2.4 - YouTube Facade Pattern for PageSpeed optimization - Replace YouTube iframes with lightweight facade (thumbnail + play button) - Load real iframe only on user click - Reduces TBT by ~2000ms - Uses youtube-nocookie.com for privacy-enhanced mode --- .../Services/YoutubeFacadeContentFilter.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/Public/YoutubeFacade/Infrastructure/Services/YoutubeFacadeContentFilter.php b/Public/YoutubeFacade/Infrastructure/Services/YoutubeFacadeContentFilter.php index e1e2c43e..915a72aa 100644 --- a/Public/YoutubeFacade/Infrastructure/Services/YoutubeFacadeContentFilter.php +++ b/Public/YoutubeFacade/Infrastructure/Services/YoutubeFacadeContentFilter.php @@ -32,36 +32,23 @@ final class YoutubeFacadeContentFilter */ public function filter(string $content): string { - // DEBUG: File-based logging (bypass WP_DEBUG) - $logFile = '/tmp/youtube-facade-debug.log'; - file_put_contents($logFile, date('Y-m-d H:i:s') . " - filter() called, content length: " . strlen($content) . "\n", FILE_APPEND); - // Check if content has YouTube embeds at all (quick check) if (strpos($content, 'youtube.com/embed/') === false && strpos($content, 'youtube-nocookie.com/embed/') === false) { - file_put_contents($logFile, date('Y-m-d H:i:s') . " - No YouTube embeds found in quick check\n", FILE_APPEND); return $content; } - file_put_contents($logFile, date('Y-m-d H:i:s') . " - YouTube embed detected, applying regex\n", FILE_APPEND); - // Pattern to match YouTube iframes // Handles: youtube.com/embed/ and youtube-nocookie.com/embed/ // More flexible: allows any attributes in any order, whitespace variations $pattern = '/]*src=["\']https?:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9_-]+)[^"\']*["\'][^>]*>\s*<\/iframe>/is'; - $matchCount = 0; - $result = preg_replace_callback($pattern, function ($matches) use (&$matchCount, $logFile) { - $matchCount++; + $result = preg_replace_callback($pattern, function ($matches) { $videoId = $matches[1]; - file_put_contents($logFile, date('Y-m-d H:i:s') . " - Matched video ID: " . $videoId . "\n", FILE_APPEND); return $this->renderer->render($videoId); }, $content); - file_put_contents($logFile, date('Y-m-d H:i:s') . " - Regex replaced " . $matchCount . " iframes\n", FILE_APPEND); - // Return original content if regex failed if ($result === null) { - file_put_contents($logFile, date('Y-m-d H:i:s') . " - preg_replace_callback returned null (regex error)\n", FILE_APPEND); return $content; }