<?php

namespace Devnet\PingVid\Includes;


if (!defined('ABSPATH')) {
    exit;
}


class Helper
{

    public static function get_youtube_id($url)
    {
        // Parse the URL
        $parts = wp_parse_url($url);

        // Handle short URLs (e.g., youtu.be/ID)
        if (isset($parts['host']) && strpos($parts['host'], 'youtu.be') !== false) {
            return ltrim($parts['path'], '/');
        }

        // Handle normal and shorts URLs (youtube.com/watch?v=ID or youtube.com/shorts/ID)
        if (isset($parts['host']) && strpos($parts['host'], 'youtube.com') !== false) {
            if (isset($parts['path'])) {
                // Match /shorts/ID or /embed/ID or /v/ID
                if (preg_match('#/(embed|shorts|v)/([^/?]+)#', $parts['path'], $matches)) {
                    return $matches[2];
                }

                // Fallback to query string
                if (isset($parts['query'])) {
                    parse_str($parts['query'], $query);
                    if (isset($query['v'])) {
                        return $query['v'];
                    }
                }
            }
        }

        return false; // ID not found
    }
}
