<?php
/**
 * Twitch API for embed.
 *
 * @package cyberpress/admin
 */

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


/**
 * CyberPress_Twitch Class
 */
class CyberPress_Twitch {
    /**
     * Scripts for embed already enqueued.
     *
     * @var boolean
     */
    public static $scripts_enqueued = false;

    /**
     * Enqueue Scripts.
     */
    public static function enqueue_scripts() {
        if ( self::$scripts_enqueued ) {
            return;
        }
        self::$scripts_enqueued = true;

        wp_register_script( 'twitch-embed', 'https://embed.twitch.tv/embed/v1.js', array(), '1.0.0', true );
        wp_enqueue_script( 'cyberpress-twitch-embed', cyberpress()->plugin_url . 'assets/js/twitch-embed.min.js', array( 'twitch-embed' ), '2.5.7', true );
    }

    /**
     * Embed Player.
     *
     * @param string $channel - channel name.
     *
     * @return string
     */
    public static function embed( $channel ) {
        self::enqueue_scripts();

        return $channel ? ( '<div class="cyberpress-twitch" data-channel="' . esc_attr( $channel ) . '"></div>' ) : '';
    }
}
