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
This commit is contained in:
@@ -32,36 +32,23 @@ final class YoutubeFacadeContentFilter
|
|||||||
*/
|
*/
|
||||||
public function filter(string $content): string
|
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)
|
// Check if content has YouTube embeds at all (quick check)
|
||||||
if (strpos($content, 'youtube.com/embed/') === false && strpos($content, 'youtube-nocookie.com/embed/') === false) {
|
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;
|
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
|
// Pattern to match YouTube iframes
|
||||||
// Handles: youtube.com/embed/ and youtube-nocookie.com/embed/
|
// Handles: youtube.com/embed/ and youtube-nocookie.com/embed/
|
||||||
// More flexible: allows any attributes in any order, whitespace variations
|
// More flexible: allows any attributes in any order, whitespace variations
|
||||||
$pattern = '/<iframe\s+[^>]*src=["\']https?:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9_-]+)[^"\']*["\'][^>]*>\s*<\/iframe>/is';
|
$pattern = '/<iframe\s+[^>]*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) {
|
||||||
$result = preg_replace_callback($pattern, function ($matches) use (&$matchCount, $logFile) {
|
|
||||||
$matchCount++;
|
|
||||||
$videoId = $matches[1];
|
$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);
|
return $this->renderer->render($videoId);
|
||||||
}, $content);
|
}, $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
|
// Return original content if regex failed
|
||||||
if ($result === null) {
|
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;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user