Files

76 lines
3.0 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Core;
/**
* Typed access to the single `sg_settings` option.
*/
final class Settings {
/**
* Absolute minimum donation in minor units (1.00 of the currency). PayPal's fixed per-transaction fee
* makes smaller donations a net loss for the streamer, so the configured minimum can never go below this.
*/
public const HARD_MIN_MINOR = 100;
/** The minimum that is actually enforced: the configured value, but never below the hard floor. */
public static function effectiveMin( int $configured ): int {
return max( self::HARD_MIN_MINOR, $configured );
}
public static function defaults(): array {
return array(
'paypal_mode' => 'sandbox', // sandbox | live
'paypal_client_id' => '',
'paypal_client_secret' => '',
'paypal_webhook_id' => '',
'fee_percent_bps' => 349, // 3.49%
'fee_fixed_minor' => 49,
'anonymity_policy' => 'allow', // allow | require_name | force_anonymous
'moderate_messages' => false,
'profanity_filter' => true, // streamer-controlled master switch
'profanity_use_builtin' => true, // small built-in list
'profanity_words' => '', // streamer's extra words, one per line
'message_max_length' => 200,
'min_donation_minor' => 100,
'donation_notice' => 'Donations are non-refundable.', // shown under the donation form; empty hides it
'overlay_token' => '',
'visitor_ip_header' => 'auto', // auto | '' (direct) | cloudflare | real_ip | forwarded (see Core\ClientIp)
'funding_paylater' => false, // show PayPal "Pay Later" button
'update_token' => '', // optional Gitea read token (only while the update repo is private)
'funding_card' => true, // show the Debit/Credit Card button
'delete_data_on_uninstall' => false, // when the plugin is deleted, also drop all tables/options (see Core\Uninstaller)
);
}
public static function all(): array {
$saved = get_option( 'sg_settings', array() );
return array_merge( self::defaults(), is_array( $saved ) ? $saved : array() );
}
public static function get( string $key, mixed $default = null ): mixed {
$all = self::all();
return $all[ $key ] ?? $default;
}
public static function update( array $values ): void {
$allowed = array_intersect_key( $values, self::defaults() );
update_option( 'sg_settings', array_merge( self::all(), $allowed ), false );
}
/** Generates the overlay token if none is stored (a DB write: only from activation or an authenticated admin screen). */
public static function ensure_overlay_token(): string {
$token = (string) self::get( 'overlay_token', '' );
if ( strlen( $token ) < 32 ) {
$token = bin2hex( random_bytes( 16 ) );
self::update( array( 'overlay_token' => $token ) );
}
return $token;
}
public static function fee_calculator(): FeeCalculator {
return new FeeCalculator( (int) self::get( 'fee_percent_bps' ), (int) self::get( 'fee_fixed_minor' ) );
}
}