'', 'api_key_secret' => '', 'callback_key' => '', 'lifetime' => self::DEFAULT_LIFETIME, 'currency' => 'USD', 'email_enabled' => 'no', 'email_to' => '', 'coins' => [], ]; private function __construct() {} /** All settings with defaults filled in. */ public static function all(): array { $stored = get_option( self::OPTION, [] ); return array_merge( self::DEFAULTS, is_array( $stored ) ? $stored : [] ); } public static function get( string $key ): string { $all = self::all(); return isset( $all[ $key ] ) && is_scalar( $all[ $key ] ) ? (string) $all[ $key ] : ''; } /** Write one setting, keeping the others. */ public static function set( string $key, string $value ): void { $all = self::all(); $all[ $key ] = $value; update_option( self::OPTION, $all ); } /** Write several settings at once, keeping the others. */ public static function update( array $values ): void { $all = array_merge( self::all(), $values ); update_option( self::OPTION, $all ); } /** Whether both API keys are set. */ public static function keys_present(): bool { return '' !== trim( self::get( 'api_key_id' ) ) && '' !== trim( self::get( 'api_key_secret' ) ); } /** The visitor texts live in their own option: their screen must never touch the keys. */ public const TEXTS_OPTION = 'cryptoway_button_texts'; /** * The site owner's own wording of visitor texts, by key (see Texts::catalogue()). * * @return array */ public static function texts(): array { $texts = get_option( self::TEXTS_OPTION, [] ); if ( ! is_array( $texts ) ) { return []; } $out = []; foreach ( $texts as $key => $value ) { if ( is_string( $key ) && is_scalar( $value ) && '' !== (string) $value ) { $out[ $key ] = (string) $value; } } return $out; } /** Time to pay in minutes, within 15–1440. */ public static function lifetime(): int { $minutes = (int) self::get( 'lifetime' ); if ( $minutes <= 0 ) { $minutes = self::DEFAULT_LIFETIME; } return max( self::MIN_LIFETIME, min( self::MAX_LIFETIME, $minutes ) ); } /** The default fiat currency for buttons without one of their own. */ public static function currency(): string { $currency = strtoupper( trim( self::get( 'currency' ) ) ); return in_array( $currency, self::CURRENCIES, true ) ? $currency : 'USD'; } public static function email_enabled(): bool { return 'yes' === self::get( 'email_enabled' ); } /** * The coins a visitor may pick when paying on the page: the ticked ones, or every coin * Cryptoway lists when none is ticked. * * @return string[] Symbol ids. */ public static function enabled_coins(): array { $all = self::all(); $chosen = isset( $all['coins'] ) && is_array( $all['coins'] ) ? array_values( array_filter( array_map( 'strval', $all['coins'] ) ) ) : []; return $chosen ? $chosen : Coins::available(); } /** Where payment e-mails go: the setting, or the site administrator's address. */ public static function email_to(): string { $to = sanitize_email( self::get( 'email_to' ) ); return '' !== $to ? $to : (string) get_option( 'admin_email' ); } /** * Keys stored by the Cryptoway WooCommerce or EDD plugin on this site, for the * "use those keys" buttons. Only whether they exist and where: the values are read * at import time. * * @return array source => label. */ public static function importable_sources(): array { $sources = []; $woo = get_option( 'woocommerce_cryptoway_settings', [] ); if ( is_array( $woo ) && '' !== trim( (string) ( $woo['api_key_id'] ?? '' ) ) && '' !== trim( (string) ( $woo['api_key_secret'] ?? '' ) ) ) { $sources['woo'] = 'WooCommerce'; } $edd = get_option( 'edd_settings', [] ); if ( is_array( $edd ) && '' !== trim( (string) ( $edd['cryptoway_api_key_id'] ?? '' ) ) && '' !== trim( (string) ( $edd['cryptoway_api_key_secret'] ?? '' ) ) ) { $sources['edd'] = 'Easy Digital Downloads'; } return $sources; } /** * Copy the keys from another Cryptoway plugin on this site. * * @return bool Whether both keys were found and copied. */ public static function import_keys( string $source ): bool { if ( 'woo' === $source ) { $from = get_option( 'woocommerce_cryptoway_settings', [] ); $id = (string) ( $from['api_key_id'] ?? '' ); $secret = (string) ( $from['api_key_secret'] ?? '' ); } elseif ( 'edd' === $source ) { $from = get_option( 'edd_settings', [] ); $id = (string) ( $from['cryptoway_api_key_id'] ?? '' ); $secret = (string) ( $from['cryptoway_api_key_secret'] ?? '' ); } else { return false; } $id = trim( $id ); $secret = trim( $secret ); if ( '' === $id || '' === $secret ) { return false; } // The callback key is fetched for this plugin's own copy, like after a manual save. self::update( [ 'api_key_id' => $id, 'api_key_secret' => $secret, 'callback_key' => '' ] ); return true; } }