<?php
if (!defined('ABSPATH')) {
	exit;
}



/********************************************************************
 * 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
 *
 * Main class for bootstrapping the plugin
 *
 * @license GPLv2
 * @copyright Upvimeo.com (https://upvimeo.com)
 * @since 1.0.0
 */
class UpVimeo
{

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      UpVimeo_Loader $loader Maintains and registers all hooks for the plugin.
	 */
	protected $loader;

	/**
	 * The unique identifier of this plugin on the remote server for update purposes.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      int $plugin_name
	 */
	protected $plugin_ID;

	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string $plugin_name The string used to uniquely identify this plugin.
	 */
	protected $plugin_name;

	/**
	 * The basename of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string $plugin_name The string used to uniquely identify this plugin.
	 */
	protected $plugin_basename;

	/**
	 * The plugin path of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string $plugin_name The string used to uniquely identify this plugin.
	 */
	protected $plugin_path;

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

	/**
	 * The pro version updater instance
	 *
	 * @since    1.7.4
	 * @access   protected
	 * @var      string $updater The pro version updater instance
	 */
	protected $updater;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct()
	{
		$this->version = UPVIMEO_VERSION;
		$this->plugin_ID = UPVIMEO_PLUGIN_ID;
		$this->plugin_name = 'upvimeo';
		$this->plugin_basename = 'upvimeo/upvimeo.php';
		$this->plugin_path = UPVIMEO_FILE;

		$this->load_dependencies();
		$this->set_locale();
		$this->define_shared_hooks(); // Note: Must be here.
		$this->define_admin_hooks();
		$this->define_public_hooks();
		$this->setup_updater();
	}


	/**
	 * Load the vimeo API library
	 * @return void
	 * @since 1.8.1
	 */
	private function load_vimeo_api_lib()
	{




		require_once UPVIMEO_PATH . 'includes/class-upvimeo-vimeo.php';
	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function load_dependencies()
	{

		/**
		 * Load composer
		 */
		if (!file_exists(UPVIMEO_PATH . 'vendor/autoload.php')) {
			wp_die( esc_html__( 'Upvimeo: vendor dependencies are missing. Please run composer install inside the plugin folder.', 'upvimeo-direct-uploads' ) );
		} else {
			require_once UPVIMEO_PATH . 'vendor/autoload.php';
		}

		/**
		 *  Load vimeo library
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-vimeo.php';

		/**
		 * The class responsible for settings management
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-singleton.php';

		/**
		 * The class responsible for settings management
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-settings-base.php';
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-settings-helper.php';

		/**
		 * The class responsible for communicating with the vimeo API
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-flash.php';

		/**
		 * The class responsible for logging
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-logger.php';

		/**
		 * Load the utility functions
		 */
		require_once UPVIMEO_PATH . 'includes/helpers.php';

		/**
		 * Load the deprecated utility functions
		 */
		require_once UPVIMEO_PATH . 'includes/deprecated.php';

		/**
		 * The class responsible for displaying notices
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-process-manager.php';

		/**
		 * The class responsible for displaying notices
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-notices-helper.php';

		/**
		 * The class responsible for communicating with the vimeo API
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-api-base.php';

		/**
		 * The class responsible for communicating with the vimeo API
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-api-helper.php';

		/**
		 * The class responsible for communicating with the database and querying data
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-db-base.php';
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-db-helper.php';

		/**
		 * The class responsible for communicating with the news service
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-news-helper.php';

		/**
		 * The class responsible for registering the post types
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-post-types.php';

		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-loader.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-i18n.php';

		/**
		 * The class responsible for creating list table of the videos
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-list-table.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once UPVIMEO_PATH . 'shared/class-upvimeo-shared.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once UPVIMEO_PATH . 'admin/class-upvimeo-admin-screen-options.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once UPVIMEO_PATH . 'admin/class-upvimeo-admin.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once UPVIMEO_PATH . 'public/class-upvimeo-public.php';

		/**
		 * The class responsible for handling all ajax requests
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-cron.php';

		/**
		 * The class responsible defining the internal hooks
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-internal-hooks.php';

		/**
		 * The class responsible for handling all ajax requests
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-ajax-handler.php';

		/**
		 * The class responsible for working with a chunked upload (storing chunks, etc)
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-file-chunk.php';
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-file-chunk-handler.php';
		/**
		 * The base integration class
		 */
		// require_once UPVIMEO_PATH . 'includes/class-upvimeo-integration.php'; // REMOVED: Moved to Pro

		/**
		 * The class responsible for checking updates from our third party server.
		 */
		// require_once UPVIMEO_PATH . 'includes/class-upvimeo-integrations.php'; // REMOVED: Moved to Pro


		// Hook for Pro modules to load their dependencies (if any specific ones needed before init)
		do_action('upvimeo_load_pro_dependencies');


		/**
		 * The class responsible for logging
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-migrator.php';

		/**
		 * The class responsible for syncing videos locally
		 */
		require_once UPVIMEO_PATH . 'includes/class-upvimeo-sync.php';

		/**
		 * Initialize the loader
		 */
		$this->loader = new UpVimeo_Loader();
	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the UpVimeo_i18n class in order to set the domain and to register the hook
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function set_locale()
	{

		// WordPress 4.6+ loads plugin translations automatically from the languages directory.
		// No manual load_plugin_textdomain() call is needed.
	}

	/**
	 * Register all of the hooks related to both admin area and fron area.
	 *
	 * @since 1.4.0
	 * @package  private
	 */
	private function define_shared_hooks()
	{
		$plugin_shared = new UpVimeo_Shared();
		$this->loader->add_action('init', $plugin_shared, 'register_scripts', 0);

		$this->loader->add_action('wp_enqueue_editor', $plugin_shared, 'enqueue_scripts_tinymce', 1000);

		$this->loader->add_action('before_wp_tiny_mce', $plugin_shared, 'tinymce_globals');
		$this->loader->add_action('after_setup_theme', $plugin_shared, 'tinymce_styles');
		$this->loader->add_filter('mce_buttons', $plugin_shared, 'tinymce_vimeo_button');
		$this->loader->add_filter('mce_external_plugins', $plugin_shared, 'tinymce_vimeo_plugin');
		$this->loader->add_action('media_buttons', $plugin_shared, 'add_media_button');
	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_admin_hooks()
	{

		// Init Classes
		$plugin_admin = new UpVimeo_Admin($this->get_plugin_name(), $this->get_version());

		$ajax_handler = null;
		$integrations = null;

		$ajax_handler = new UpVimeo_Ajax_Handler($this->get_plugin_name(), $this->get_version());

		$cron_system = new UpVimeo_Cron();
		$migrator = new UpVimeo_Migrator();
		$internal_hooks = new UpVimeo_Internal_Hooks();
		$post_types = new UpVimeo_Post_Types();

		$screen_options = new UpVimeo_Admin_Screen_Options([
			UpVimeo_Admin::PAGE_VIMEO => [
				'description' => __('Show Description', 'upvimeo-direct-uploads'),
				'link_insteadof_shortcode' => __('Show Link instead of shortcode', 'upvimeo-direct-uploads'),
			]
		]);

		// Init Migration
		$this->loader->add_action('admin_init', $migrator, 'init');

		// Init Dashboard
		$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
		$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
		$this->loader->add_action('in_admin_header', $plugin_admin, 'do_admin_notices', 50);
		$this->loader->add_action('admin_menu', $plugin_admin, 'register_admin_menu', 20);
		// Run as late as possible to catch Freemius additions
		$this->loader->add_action('admin_menu', $plugin_admin, 'remove_freemius_menus', PHP_INT_MAX);
		$this->loader->add_action('add_meta_boxes', $plugin_admin, 'register_media_library_upload_metabox');
		$this->loader->add_filter('manage_media_columns', $plugin_admin, 'manage_media_columns', 15, 1);
		$this->loader->add_action('manage_media_custom_column', $plugin_admin, 'manage_media_custom_column', 15, 2);
		$this->loader->add_filter('plugin_action_links_' . UPVIMEO_BASENAME, $plugin_admin, 'plugin_action_links', 100, 1);
		$this->loader->add_action('admin_head', $plugin_admin, 'hook_admin_head_style');

		// Init Post Types
		$this->loader->add_action('init', $post_types, 'init');

		// Int Cron tasks
		$cron_system->init($this);

		// Init Ajax endpoints
		if ($ajax_handler) {
			$this->loader->add_action('wp_ajax_UPVIMEO_handle_upload', $ajax_handler, 'handle_upload');
			$this->loader->add_action('wp_ajax_UPVIMEO_fetch_domains', $ajax_handler, 'handle_fetch_domains');

			$this->loader->add_action('wp_ajax_UPVIMEO_handle_settings', $ajax_handler, 'handle_settings');
			$this->loader->add_action('wp_ajax_UPVIMEO_handle_delete', $ajax_handler, 'handle_delete');
			$this->loader->add_action('wp_ajax_UPVIMEO_store_upload', $ajax_handler, 'store_upload');
			$this->loader->add_action('wp_ajax_UPVIMEO_handle_basic_edit', $ajax_handler, 'handle_basic_edit');
			$this->loader->add_action('wp_ajax_UPVIMEO_handle_embed_privacy', $ajax_handler, 'handle_embed_privacy');
			$this->loader->add_action('wp_ajax_UPVIMEO_delete_embed_privacy_domain', $ajax_handler, 'handle_embed_privacy_whitelist_remove');
			$this->loader->add_action('wp_ajax_UPVIMEO_handle_video_embed_preset_set', $ajax_handler, 'handle_video_embed_preset_set');
			$this->loader->add_action('wp_ajax_UPVIMEO_handle_video_folder_set', $ajax_handler, 'handle_video_folder_set');
			$this->loader->add_action('wp_ajax_UPVIMEO_get_uploads', $ajax_handler, 'get_uploads');
			$this->loader->add_action('wp_ajax_UPVIMEO_attachment2vimeo', $ajax_handler, 'handle_attachment2vimeo');
			$this->loader->add_action('wp_ajax_UPVIMEO_attachment2vimeo_delete', $ajax_handler, 'handle_attachment2vimeo_delete');
			$this->loader->add_action('wp_ajax_UPVIMEO_user_search', $ajax_handler, 'handle_user_search');
			$this->loader->add_action('wp_ajax_UPVIMEO_folder_search', $ajax_handler, 'handle_folder_search');
			$this->loader->add_action('wp_ajax_UPVIMEO_generate_stats', $ajax_handler, 'handle_generate_stats');
			$this->loader->add_action('wp_ajax_UPVIMEO_import_video', $ajax_handler, 'handle_import_video');
			$this->loader->add_action('wp_ajax_UPVIMEO_create_video', $ajax_handler, 'handle_create_video');
			$this->loader->add_action('wp_ajax_UPVIMEO_search_videos', $ajax_handler, 'handle_search_videos');
		}

		// Hooks
		$this->loader->add_filter('upload_mimes', $internal_hooks, 'add_mimes', 15);

		$this->loader->add_action('UPVIMEO_backend_after_upload', $internal_hooks, 'backend_after_upload', 5);
		$this->loader->add_action('wp_vimeo_upload_process_default_time_limit', $internal_hooks, 'upload_process_default_time_limit', 10, 1);
		$this->loader->add_filter('elementor/atomic/dynamic_tags/select_control_options', $internal_hooks, 'add_upvimeo_to_elementor_acf_url_tag', 20, 3);
		$this->loader->add_action('elementor/dynamic_tags/register', $internal_hooks, 'register_elementor_dynamic_tag');

		// Integrations
		/*
		if ($integrations) {
			$integrations->register();
		}
		*/


		// Hook for Pro to register integrations
		do_action('upvimeo_init_pro_modules', $this->loader);
		if ( file_exists( UPVIMEO_PATH . 'includes/pro/premium-hooks.php' ) ) {
			include_once UPVIMEO_PATH . 'includes/pro/premium-hooks.php';
		}
	}

	/**
	 * Register all of the hooks related to the public-facing functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_public_hooks()
	{

		$plugin_public = new UpVimeo_Public($this->get_plugin_name(), $this->get_version());

		$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles');
		$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');
		$this->loader->add_filter('the_content', $plugin_public, 'video_contents');

		add_shortcode('upvimeo_video', array($plugin_public, 'shortcode_video'));
	}

	/**
	 * Setup the plugin updater
	 *
	 * @since    1.2.0
	 * @access   private
	 */
	private function setup_updater()
	{
		// Updater disabled.
	}

	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @since    1.0.0
	 */
	public function run()
	{
		$this->loader->run();
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @return    string    The name of the plugin.
	 * @since     1.0.0
	 */
	public function get_plugin_name()
	{
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @return    UpVimeo_Loader    Orchestrates the hooks of the plugin.
	 * @since     1.0.0
	 */
	public function get_loader()
	{
		return $this->loader;
	}

	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @return    string    The version number of the plugin.
	 * @since     1.0.0
	 */
	public function get_version()
	{
		return $this->version;
	}
}
