<?php
namespace WPDRMS\ASP\Utils;

defined('ABSPATH') or die("You can't access this file directly.");

if ( !class_exists(__NAMESPACE__ . '\Plugin') ) {
	class Plugin {
		/**
		 * Version comparator for previous plugin versions, based on PHP version_check
		 *
		 * @param string $ver version to check against
		 * @param string $operator opertator, same as in version_check()
		 * @param false $check_updated if true, returns if the version was just updated from an older one
		 * @return bool|int
		 */
		public static function previousVersion(string $ver = '', string $operator = '<=', bool $check_updated = false ) {
			$updated = false;

			$version = get_site_option('_asp_version', array(
				'current' => ASP_CURR_VER_STRING,
				'previous' => ASP_CURR_VER_STRING, // version, when this feature was implemented
				'new' => true
			));
			// The option does not exist yet
			if ( isset($version['new']) ) {
				$version = array(
					'current' => ASP_CURR_VER_STRING,
					'previous' => get_option('asp_version', 0) == 0 ? ASP_CURR_VER_STRING : '4.20.2'
				);
				update_site_option('_asp_version', $version);
				delete_option('asp_version');
			}
			// It has been an update - this executes only once per each version activation
			if ( $version['current'] != ASP_CURR_VER_STRING ) {
				$version = array(
					'current' => ASP_CURR_VER_STRING,
					'previous' => $version['current']
				);
				update_site_option('_asp_version', $version);
				$updated = true;
			}

			if ( $check_updated ) {
				return $updated;
			} else {
				return version_compare($version['previous'], $ver, $operator);
			}
		}


		/**
		 * Returns the template file path, considering templating feature
		 *
		 * @param string $file File path relative to the /views/ directory, without starting slash.
		 * @return string
		 */
		public static function templateFilePath(string $file): string {
			$theme_path = get_stylesheet_directory() . "/asp/";
			if ( file_exists( $theme_path . $file ) )
				return $theme_path . $file;
			else
				return ASP_INCLUDES_PATH . 'views/' . $file;
		}

		/**
		 * Checks if user had Ajax Search Lite installed before
		 *
		 * @return bool
		 */
		public static function hadLiteVersionInstalledBefore(): bool {
			return count( (array)get_option('asl_options', array()) ) > 0 && get_option('asl_version', 0) > 4732;
		}

		/**
		 * Whether option writes must be blocked for the current request.
		 *
		 * Demo mode (`ASP_DEMO`) exposes the admin menu to any logged-in user
		 * (`read` capability, see WPDRMS\ASP\Core\Menu) so visitors can explore the
		 * panels. Those non-admin visitors must not be able to save, reset or delete
		 * any settings — the only admin area they may change is the legacy
		 * search-instance list and the per-instance options editor.
		 *
		 * Real administrators and multisite super-admins are never locked, as they
		 * satisfy the `manage_options` capability.
		 *
		 * @return bool
		 */
		public static function isDemoWriteLocked(): bool {
			$locked = defined('ASP_DEMO') && ASP_DEMO && !current_user_can('manage_options');

			/**
			 * Filters whether the demo-mode write lock applies to the current request.
			 *
			 * @param bool $locked True when the site is in demo mode and the current
			 *                     user is not an administrator / super-admin.
			 */
			return (bool) apply_filters('asp_demo_write_locked', $locked);
		}
	}
}