<?php

/********************************************************************
 * Copyright (C) 2025 Hassine RIANI (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_Settings_Helper
 *
 * Responsible for read/write plugin settings
 *
 * Note: The prefix UPVIMEO_ in the input is no longer needed and is removed during the sanitization.
 * eg. If you call the methods ->get('UPVIMEO_setting_name') it translates to ->get('setting_name')
 *
 * @license GPLv2
 * @copyright Hassine RIANI (https://upvimeo.com)
 * @since 1.4.0
 */
class UpVimeo_Settings_Helper extends UpVimeo_Settings_Base
{

	/**
	 * Returns the default front-end view privacy
	 * @return string
	 */
	public function get_default_frontend_view_privacy()
	{
		$privacy = 'anybody';

		return apply_filters('UPVIMEO_default_privacy_frontend', $privacy);
	}

	/**
	 * Returns list of whitelisted domains
	 * @return array
	 */
	public function get_whitelisted_domains()
	{
		$domains   = array();
		$whitelist = $this->get('upvimeo_embed_domains');
		if (! empty($whitelist)) {
			$parts = explode(',', $whitelist);
			foreach ($parts as $domain) {
				if (empty($domain) || false === filter_var($domain, FILTER_VALIDATE_DOMAIN)) {
					continue;
				}
				array_push($domains, $domain);
			}
		}

		return $domains;
	}

	/**
	 * Returns the api credentials
	 *
	 * @param  bool  $force
	 *
	 * @return void
	 */
	public function import_defaults($force = false)
	{

		$defaults = $this->get_defaults();
		$values   = array();

		// Find out if the defaults are not yet initialized
		if (! $force) {
			foreach ($defaults as $key) {
				$value = $this->get($key);
				if (! empty($value)) {
					array_push($values, $value);
				}
			}
		}


		// If the defaults are not yet initialized, import as defaults.
		if (empty($values) || $force) {
			foreach ($defaults as $key => $value) {
				if (empty($value)) {
					continue;
				}
				$this->set($key, $value);
			}
		}

		$this->save();
	}

	/**
	 * Return the plugin defaults
	 * @return string[]
	 */
	public function get_defaults()
	{
		$defaults = array(
			'pro_version'                         => UPVIMEO_DB_PROVERSION,
			'upvimeo_view_privacy'                    => 'anybody',
			'upvimeo_upload_folder'                   => 'default',
			'upvimeo_mce_enable_account_search'       => '1',
			'upvimeo_mce_enable_local_search'         => '1',
			'upvimeo_gtb_enable_account_search'       => '1',
			'upvimeo_enable_single_pages'             => '1',
			'upvimeo_local_current_user_only'         => '1',
			'upvimeo_enable_embed_presets_management' => '1',
			'upvimeo_enable_embed_privacy_management' => '1',
			'upvimeo_enable_folders_management'       => '1',
			'upvimeo_tutor_force_vimeo_only'          => '',
			'upvimeo_learnpress_force_vimeo_only'     => '',
		);


		return apply_filters('upvimeo_settings_defaults', $defaults);
	}
}
