'0', 'autoplay' => '1', ]; $start = self::youtube_start($query); if ($start > 0) { $params['start'] = (string) $start; } return 'https://www.youtube.com/embed/' . $id . '?' . http_build_query($params); } /** * @param array $query */ private static function youtube_start(array $query): int { if (isset($query['start']) && is_numeric($query['start'])) { return max(0, (int) $query['start']); } $t = isset($query['t']) ? (string) $query['t'] : ''; if ($t === '') { return 0; } if (is_numeric($t)) { return max(0, (int) $t); } $seconds = 0; if (preg_match('/(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?/', $t, $m) === 1) { $seconds += (int) ($m[1] ?? 0) * 3600; $seconds += (int) ($m[2] ?? 0) * 60; $seconds += (int) ($m[3] ?? 0); } return max(0, $seconds); } private static function vimeo_embed(string $raw): string { $host = strtolower((string) (wp_parse_url($raw, PHP_URL_HOST) ?? '')); $host = preg_replace('/^www\./', '', $host) ?? $host; if (! in_array($host, ['vimeo.com', 'player.vimeo.com'], true)) { return ''; } $path = trim((string) (wp_parse_url($raw, PHP_URL_PATH) ?? ''), '/'); $parts = array_values(array_filter(explode('/', $path), static fn (string $p): bool => $p !== '')); if ($host === 'player.vimeo.com') { if (($parts[0] ?? '') !== 'video' || ! isset($parts[1]) || ! ctype_digit($parts[1])) { return ''; } $id = $parts[1]; $hash = $parts[2] ?? ''; } else { $skip = ['channels', 'groups', 'album', 'ondemand', 'showcase', 'event']; if (isset($parts[0]) && in_array($parts[0], $skip, true)) { return ''; } if (! isset($parts[0]) || ! ctype_digit($parts[0])) { return ''; } $id = $parts[0]; $hash = isset($parts[1]) && preg_match('/^[a-f0-9]+$/i', $parts[1]) === 1 ? $parts[1] : ''; } $params = ['autoplay' => '1']; $query = []; parse_str((string) (wp_parse_url($raw, PHP_URL_QUERY) ?? ''), $query); if ($hash === '' && isset($query['h']) && is_string($query['h'])) { $hash = $query['h']; } if ($hash !== '') { $params['h'] = $hash; } return 'https://player.vimeo.com/video/' . $id . '?' . http_build_query($params); } private static function loom_embed(string $raw): string { $host = strtolower((string) (wp_parse_url($raw, PHP_URL_HOST) ?? '')); $host = preg_replace('/^www\./', '', $host) ?? $host; if ($host !== 'loom.com') { return ''; } $path = (string) (wp_parse_url($raw, PHP_URL_PATH) ?? ''); if (preg_match('#/(share|embed)/([A-Za-z0-9]+)#', $path, $m) !== 1) { return ''; } return 'https://www.loom.com/embed/' . $m[2]; } }