<?php

class roverOPTIONS {
	public const THEME								= 'roveridx_theming';
	public const REGIONS							= 'roveridx_regions';
	public const SEO								= 'roveridx_seo';
	public const SOCIAL								= 'roveridx_social';
	public const REGION_REFRESH						= 'roveridx_region_refresh';
	}

class roverKEYS {

	public const IDX_PLUGIN_NAME					= 'rover-idx';

	public const DEBUG_KEY							= 'roveridx_debug';
	public const NONCE_DEF							= 'nonce-idx-default';
	public const NONCE_SS							= 'nonce-site-search';
	public const DEF_POST_ID						= -487;

	public const IDX_PANEL_SLUG						= 'rover-panel-';

	public const WP_TEMPLATE_KEY					= '_wp_page_template';
	public const META_PAGE_ID						= '_roveridx_page_id';
	public const FBML_NS_URI						= 'http://www.facebook.com/2008/fbml';
	}

class Rover_IDX_COMMON
	{
	public $is_debuggable							= null;
	public $url_js									= 'https://ep4.roveridx.com/js/';

	private Rover_IDX $rover_idx;
	private ?array $path_parts						= null;
	private $rover_post_id							= null;

	public function __construct(Rover_IDX $rover_idx)
		{
		$this->rover_idx						= $rover_idx;
		}

	public function settings_val_is_checked($settings, $key)	{

		if (is_array($settings) && isset($settings[$key]) && $settings[$key] == true)
			return 'checked=checked';

		return '';
		}

	public function settings_val_is_selected($settings, $key, $val_to_compare)	{

		if (is_array($settings) && isset($settings[$key]) && $settings[$key] == $val_to_compare)
			return 'selected=selected';

		  return '';
		}

	public function get_settings_val($settings, $key)	{

		if (is_array($settings) && isset($settings[$key]))
			return $settings[$key];

		return '';
		}

	public function get_rover_post_id() {

		if (is_null($this->rover_post_id))
			{
			if (
				isset($this->rover_idx->roveridx_theming) &&
				is_array($this->rover_idx->roveridx_theming) &&
				isset($this->rover_idx->roveridx_theming['rover_post_id'])
				)
				{
				$this->rover_post_id				= $this->rover_idx->roveridx_theming['rover_post_id'];		//	WooThemes 'Empire' conflicts with -1, so we're using a more unique value
				}
			else
				{
				$this->rover_post_id				= roverKEYS::DEF_POST_ID;
				}
			}

		return $this->rover_post_id;
		}

	public function clean_domain($domain)
		{
		$clean_domain				= str_replace(
												array('http://', 'https://', 'www.', '//'),
												'',
												strtolower($domain)
												);

		if ('/' == substr($clean_domain, -1, 1))
			$clean_domain			= substr($clean_domain, 0, -1);	//	Return all but last char

		return $clean_domain;
		}

	public function error_log($file, $func, $line, $str)	{

		if ($this->rover_idx && $this->is_debuggable())
			{
			$debug_str							= sprintf( '%s %s %s: %s', basename($file), $func, $line, $str);

			$this->rover_idx->debug_html[]		= $debug_str;

			error_log($debug_str);
			}

		}

	public function is_debuggable()	{

		if (is_null($this->is_debuggable))
			{
			$this->is_debuggable				= false;

			if (defined('WP_DEBUG') && WP_DEBUG === true)
				$this->is_debuggable			= true;

			if (defined('ROVER_IDX_DEBUG') && ROVER_IDX_DEBUG === true)
				$this->is_debuggable			= true;

			if (!$this->is_debuggable && $this->request_is_debuggable())
				$this->is_debuggable			= true;
			}

		return $this->is_debuggable;
		}

	public function request_is_debuggable()	{

		if (isset($_GET['roveridx_debug']) && intval($_GET['roveridx_debug']) > 0)
			return true;

		return false;
		}

	public function validate_bool($val)	{

		#	FILTER_VALIDATE_BOOLEAN is implemented in C and handles:
		#	- true
		#	- "true"
		#	- 1
		#	- "1"
		#	- "yes" / "on"

		return filter_var($val, FILTER_VALIDATE_BOOLEAN);

		}

	public function validate_yes_no($val)	{

		return (strcasecmp($val, 'Yes') === 0) ? 'Yes' : 'No';

		}

	public function css_and_js_admin(): void
		{
		$page									= $_GET['page'] ?? '';
		$is_rover_admin							=
												(strpos($page, roverKEYS::IDX_PLUGIN_NAME) !== false) ||
												(strpos($page, roverKEYS::IDX_PANEL_SLUG) !== false);

		$this->load_bootstrap($is_rover_admin);

		$screen									= get_current_screen();
		if ($screen && $screen->base === 'dashboard') {
			wp_enqueue_script(
				'idx-wp-dash',
				$this->url_js . 'rover_panel/wp-admin/dashboard.js',
				[],
				roverVERSIONS::JS,
				true
				);
			}
		}


	public function css_and_js_front()
		{
		$this->load_bootstrap(false);
		}

	public function load_blocks()
		{
		wp_register_script( 'rover-blocks-js',
								ROVER_IDX_PLUGIN_URL . 'js/blocks.js',
								[ 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-editor' ],
								$ver = strtotime("Now"),	#	roverVERSIONS - JS,
								$in_footer = true);
		wp_enqueue_script( 'rover-blocks-js' );
		}

	public function is_rover_panel($component)
		{
		if ((substr($component, 6) === 'rover-') && (substr($component, -6) === '-panel'))
			return true;

		return false;
		}

	private function is_panel(string $panel): bool
		{
		if ($this->path_parts === null) {
			$this->path_parts = explode('/', (string) $this->rover_idx->curr_path);
			}

		return in_array($panel, $this->path_parts, true);
		}

	private function load_bootstrap($is_rover_admin)
		{
		$do_it					= false;

		if ( is_admin() )		#	On a Rover settings page in Admin.  Purposefully do not load Bootstrap if we are in admin, but not on a Rover page
			{
			$do_it				= ($is_rover_admin) ? true : false;
			}
		else if ($this->is_panel('rover-control-panel') || $this->is_panel('rover-custom-listing-panel') || $this->is_panel('rover-market-conditions'))
			{
			$do_it				= true;
			}
		if (isset($this->rover_idx->roveridx_theming['load_admin_bootstrap']) && $this->rover_idx->roveridx_theming['load_admin_bootstrap'] == 'No')
			{
			$do_it				= false;
			}

		if ($do_it)
			{
			$this->error_log(__FILE__, __FUNCTION__, __LINE__, 'Loading Bootstrap ');

			wp_register_style(	'rover-bootstrap-css',
								$this->url_js . 'bootstrap5.3.3/css/bootstrap.min.css',
								array(),
								$ver = roverVERSIONS::JS,
								'all');
			wp_enqueue_style(	'rover-bootstrap-css' );

			wp_register_script( 'rover-bootstrap-js',
								$this->url_js . 'bootstrap5.3.3/js/bootstrap.bundle.min.js',
								$dep = array(),
								$ver = roverVERSIONS::JS,
								$in_footer = true);
			wp_enqueue_script( 'rover-bootstrap-js' );
			}
		}

	}
?>