In-memory settings cache. */ private array $cache = array(); /** @var bool Whether the cache has been populated. */ private bool $loaded = false; /** * Get singleton instance. */ public static function instance(): self { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Private constructor — use instance(). */ private function __construct() {} /** * Load all settings from DB into cache (lazy, runs once). */ private function load(): void { if ( $this->loaded ) { return; } $this->cache = ADSD_RCC_DB::instance()->get_all_settings(); $this->loaded = true; } /** * Get a setting value. * * @param string $key Setting key. * @param mixed $default Default value if not found. * @return mixed */ public function get( string $key, $default = null ) { $this->load(); return $this->cache[ $key ] ?? $default; } /** * Get a setting as integer. * * @param string $key Setting key. * @param int $default Default integer value. */ public function get_int( string $key, int $default = 0 ): int { return (int) $this->get( $key, $default ); } /** * Get a setting as float. * * @param string $key Setting key. * @param float $default Default float value. */ public function get_float( string $key, float $default = 0.0 ): float { return (float) $this->get( $key, $default ); } /** * Get a setting as boolean. * * @param string $key Setting key. * @param bool $default Default boolean value. */ public function get_bool( string $key, bool $default = false ): bool { $val = $this->get( $key ); if ( null === $val ) { return $default; } return filter_var( $val, FILTER_VALIDATE_BOOLEAN ); } /** * Set a setting value and persist to DB. * * @param string $key Setting key. * @param mixed $value Setting value. */ public function set( string $key, $value ): bool { $this->load(); $this->cache[ $key ] = $value; return ADSD_RCC_DB::instance()->set_setting( $key, $value ); } /** * Save multiple settings at once. * * @param array $data Key-value pairs to save. */ public function save_batch( array $data ): void { foreach ( $data as $key => $value ) { $this->set( $key, $value ); } } /** * Clear the in-memory settings cache. */ public function bust_cache(): void { $this->cache = array(); $this->loaded = false; } // ── Typed convenience getters ───────────────────────────────────────────── /** * Get the reward recipient setting. */ public function reward_recipient(): string { return (string) $this->get( 'reward_recipient', 'both' ); } /** * Get the offer type setting. */ public function offer_type(): string { return (string) $this->get( 'offer_type', 'automatic' ); } /** * Get the code expiry in days. */ public function code_expiry_days(): int { return $this->get_int( 'code_expiry_days', 30 ); } /** * Get the daily link limit. */ public function daily_link_limit(): int { return $this->get_int( 'daily_link_limit', 5 ); } /** * Whether WooCommerce integration is enabled. */ public function woo_enabled(): bool { return $this->get_bool( 'woo_enabled', false ); } /** * Get the reward code format. */ public function code_format(): string { return (string) $this->get( 'code_format', 'mixed' ); } /** * Get the reward code custom prefix. */ public function code_prefix(): string { return (string) $this->get( 'code_prefix', '' ); } /** * Get the WordPress page ID assigned to a plugin page. * * @param string $page Page key (dashboard, register, etc.). */ public function page_id( string $page ): int { return $this->get_int( 'page_' . $page, 0 ); } /** * Get the URL for a plugin page. * * @param string $page Page key. */ public function page_url( string $page ): string { $id = $this->page_id( $page ); return $id ? (string) get_permalink( $id ) : home_url( '/' ); } /** * Get the email verification token expiry in hours. */ public function verification_expiry_hours(): int { return $this->get_int( 'verification_expiry_hours', 24 ); } } /** * Global shorthand for retrieving a setting. * * @param string $key Setting key. * @param mixed $default Default value. * @return mixed */ function adsd_rcc_setting( string $key, $default = null ) { return ADSD_RCC_Settings::instance()->get( $key, $default ); }