<?php

/********************************************************************
 * Copyright (C) 2025 Upvimeo.com (https://upvimeo.com)
 *
 * This file is part of Upvimeo
 *
 * Upvimeo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Upvimeo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Upvimeo. If not, see <https://www.gnu.org/licenses/>.
 **********************************************************************/

/**
 * Class UpVimeo_Public
 *
 * The public facing functionality of the plugin
 *
 * @license GPLv2
 * @copyright Upvimeo.com (https://upvimeo.com)
 * @since 1.0.0
 */
class UpVimeo_Public
{

	private $db_helper;

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $plugin_name The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $version The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @param string $plugin_name The name of the plugin.
	 * @param string $version The version of this plugin.
	 *
	 * @since    1.0.0
	 */
	public function __construct($plugin_name, $version)
	{

		$this->plugin_name = $plugin_name;
		$this->version     = $version;
		$this->db_helper   = new UpVimeo_Db_Helper();
	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles()
	{
		wp_register_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/upvimeo-public.css', array(), $this->version, 'all');
	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {}

	/**
	 * The video shortcode
	 *
	 * @param $atts
	 *
	 * @return false|string
	 */
	public function shortcode_video($atts)
	{
		$a        = shortcode_atts(array('id' => '',), $atts);
		$content  = '';
		$video_id = isset($a['id']) ? $a['id'] : null;
		if (! empty($video_id)) {
			wp_enqueue_style($this->plugin_name);
			$content = upvimeo_get_view('public/partials/video', array(
				'upvimeo_vimeo_id' => $video_id
			));
		}

		$filtered = apply_filters('UPVIMEO_shortcode_output', $content, $video_id);

		return wp_kses($filtered, array(
			'div'    => array('class' => true, 'id' => true, 'style' => true),
			'iframe' => array(
				'src'                   => true,
				'frameborder'           => true,
				'allowfullscreen'       => true,
				'webkitallowfullscreen' => true,
				'mozallowfullscreen'    => true,
				'allow'                 => true,
				'title'                 => true,
				'width'                 => true,
				'height'                => true,
				'loading'               => true,
				'class'                 => true,
				'id'                    => true,
			),
		));
	}

	/**
	 * The video page content
	 *
	 * @param $content
	 *
	 * @return mixed|string|void
	 */
	public function video_contents($content)
	{
		if (is_singular(UpVimeo_Db_Helper::POST_TYPE_UPLOADS)) {
			global $post;
			$theme_path       = get_stylesheet_directory();
			$plugin_file_path = trailingslashit(UPVIMEO_PATH) . '/public/partials/single-content.php';
			$theme_file_path  = trailingslashit($theme_path) . '/upvimeo/single-content.php';
			ob_start();
			if (file_exists($theme_file_path)) {
				include $theme_file_path;
			} else {
				include $plugin_file_path;
			}
			$content = ob_get_clean();
		}

		return $content;
	}
}
