<?php
/**
 * Loads all plugin settings from wp_options into a stdClass, cached in a
 * transient for 30 minutes.
 *
 * BUG 6 FIX: cache_safe_mode was never loaded from the DB; added below.
 *
 * @package FENSE_BPVT
 */

if ( !defined( 'ABSPATH' ) ) {
	exit;
}

function FENSE_BPVT_plugin_settings( FENSE_BPVT_FUN $fun ): stdClass
{
	$cached = FENSE_BPVT_Cache::wp_get( FENSE_BPVT_PLUGIN::DB_KEY . 'plugin_settings' );
	$expires = is_array( $cached ) && isset( $cached['time'] ) ? (int)$cached['time'] : 0;
	
	if ( time() < $expires ) {
		return $cached['settings'];
	}
	
	$k = FENSE_BPVT_PLUGIN::DB_KEY;
	$s = new stdClass();
	
	// General.
	$s->enable = (bool)get_option( $k . 'enable_dtg' );
	$s->appKey = (string)get_option( $k . 'licence_key', '' );
	$s->enable_on = (string)get_option( $k . 'enable_on_custom', 'all' );
	$s->enable_on_logged = (bool)get_option( $k . 'enable_on_logged_users', true );
	$s->enable_on_posts = (bool)get_option( $k . 'enable_on_posts', true );
	$s->enable_on_pages = (bool)get_option( $k . 'enable_on_pages', true );
	$s->enable_on_login = (bool)get_option( $k . 'enable_on_login', true );
	$s->enable_cache = max( 1, (int)get_option( $k . 'enable_cache', 1 ) );
	// BUG 6 FIX: cache_safe_mode was missing entirely.
	$s->cache_safe_mode = (bool)get_option( $k . 'cache_safe_mode', false );
	
	$s->is_daily_limit_reached = (bool)get_option( $k . 'is_daily_limit_reached', false );
	
	// Pages & URLs.
	$s->url_mode = (string)get_option( $k . 'url_mode', 'exclude' );
	$s->specific_pages = (array)( get_option( $k . 'specific_pages' ) ?: [] );
	$s->url_prefixes = (string)get_option( $k . 'url_prefixes', '' );
	$s->excluded_urls = (string)get_option( $k . 'excluded_urls', '' );
	
	// Protection.
	$s->protection_mode = (string)get_option( $k . 'protection_mode', 'message' );
	$s->message = (string)get_option( $k . 'restrict_message', '' );
	$s->url = esc_url_raw( (string)get_option( $k . 'redirect_to_url', 'https://google.com' ) );
	$s->disable_link = (string)get_option( $k . 'show_link', 'yes' );
	$s->link = esc_url_raw( (string)get_option( $k . 'show_link_val', '' ) );
	
	// Country.
	$s->enable_country = (array)( get_option( $k . 'enable_country' ) ?: [] );
	$s->disable_country = (array)( get_option( $k . 'disable_country' ) ?: [] );
	$s->re_enable_country = (array)( get_option( $k . 'restrict_enable_country' ) ?: [] );
	$s->re_disable_country = (array)( get_option( $k . 'restrict_disable_country' ) ?: [] );
	
	// Domains.
	$s->block_domains = $fun->banned_multi_domains_list( (string)get_option( $k . 'restrict_domain', '' ) );
	
	// Bots.
	$s->crawlers_bots = (string)get_option( $k . 'crawlers_bots', 'yes' );
	$s->crawlers_bots_data = (string)get_option( $k . 'crawlers_bots_data', '' );
	
	// IP lists.
	$s->ip_whitelisting_enable = (bool)get_option( $k . 'ip_whitelisting_enable', false );
	$s->ip_whitelisting = (string)get_option( $k . 'ip_whitelisting', '' );
	$s->ip_whitelisting_range = (array)( get_option( $k . 'ip_whitelisting_range' ) ?: [] );
	$s->ip_blacklisting_enable = (bool)get_option( $k . 'ip_blacklisting_enable', false );
	$s->ip_blacklisting = (string)get_option( $k . 'ip_blacklisting', '' );
	$s->ip_blacklisting_range = (array)( get_option( $k . 'ip_blacklisting_range' ) ?: [] );
	
	$ttl = 30 * MINUTE_IN_SECONDS;
	FENSE_BPVT_Cache::wp_set( FENSE_BPVT_PLUGIN::DB_KEY . 'plugin_settings', [ 'settings' => $s, 'time' => time() + $ttl ], $ttl );
	
	return $s;
}
