Files
wp-donations/includes/Admin/Input.php
T
shadowdaoandClaude Sonnet 5.5 4efdef72f6
Test and Release / test (push) Successful in 2m34s
Test and Release / release (push) Successful in 9s
Add admin UI: setup wizard, goals, rules editor, donations, moderation, overlays, settings
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 10:26:03 -08:00

96 lines
3.4 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Admin;
use StreamGoals\Core\Money;
/** Pure (WordPress-free) input parsing/sanitizing helpers for the admin screens. */
final class Input {
/** Parse "$1,000.50" / "10" / "10.5" into minor units, or null when invalid. */
public static function amountToMinor( mixed $raw, string $currency ): ?int {
if ( ! is_scalar( $raw ) ) {
return null;
}
$s = str_replace( array( ',', ' ', "\u{00A0}" ), '', trim( (string) $raw ) );
$s = ltrim( $s, '$€£¥' );
if ( '' === $s ) {
return null;
}
try {
$minor = Money::fromDecimalString( $s, $currency )->minor;
} catch ( \InvalidArgumentException ) {
return null;
}
return $minor > 99999999999 ? null : $minor;
}
/** "3.49" (percent) -> 349 basis points; null when invalid or outside 0..30%. */
public static function percentToBps( mixed $raw ): ?int {
if ( ! is_scalar( $raw ) ) {
return null;
}
$s = rtrim( trim( str_replace( ',', '.', (string) $raw ) ), '%' );
if ( ! preg_match( '/^\d{1,2}(\.\d{1,2})?$/', $s ) ) {
return null;
}
[ $w, $f ] = array_pad( explode( '.', $s, 2 ), 2, '' );
$bps = (int) $w * 100 + (int) str_pad( $f, 2, '0' );
return $bps <= 3000 ? $bps : null;
}
/** Whole number within [min,max], else null. Empty string -> null. */
public static function int( mixed $raw, int $min, int $max ): ?int {
if ( ! is_scalar( $raw ) || ! preg_match( '/^\s*\d{1,12}\s*$/', (string) $raw ) ) {
return null;
}
$n = (int) trim( (string) $raw );
return ( $n >= $min && $n <= $max ) ? $n : null;
}
/** Normalise a "one word per line" textarea: trimmed, lowercase, deduplicated, bounded. */
public static function words( string $text ): string {
$out = array();
foreach ( preg_split( '/\r\n|\r|\n|,/', $text ) ?: array() as $w ) {
$w = preg_replace( '/[\x00-\x1F\x7F<>]/u', '', trim( $w ) ) ?? '';
$w = function_exists( 'mb_strtolower' ) ? mb_strtolower( $w ) : strtolower( $w );
if ( '' === $w || ( function_exists( 'mb_strlen' ) ? mb_strlen( $w ) : strlen( $w ) ) > 40 ) {
continue;
}
$out[ $w ] = true;
if ( count( $out ) >= 500 ) {
break;
}
}
return implode( "\n", array_keys( $out ) );
}
/** Event name slug: [a-z0-9_-], max 40, falling back to $default. */
public static function slug( mixed $raw, string $default = 'big_donation' ): string {
$s = is_scalar( $raw ) ? strtolower( trim( (string) $raw ) ) : '';
$s = preg_replace( '/[^a-z0-9_-]+/', '_', $s ) ?? '';
$s = substr( trim( $s, '_-' ), 0, 40 );
return '' === $s ? $default : $s;
}
/** Plain single-line text with control chars and tags removed, bounded in length. */
public static function text( mixed $raw, int $max ): string {
$s = is_scalar( $raw ) ? strip_tags( (string) $raw ) : '';
$s = preg_replace( '/[\x00-\x1F\x7F]/u', '', $s ) ?? '';
$s = trim( $s );
return function_exists( 'mb_substr' ) ? mb_substr( $s, 0, $max ) : substr( $s, 0, $max );
}
/** Mask for showing that a secret exists without revealing it. */
public static function maskSecret( string $secret ): string {
return '' === $secret ? '' : str_repeat( "\u{2022}", 12 );
}
/** True when the submitted value is the untouched placeholder or empty (=> keep the stored secret). */
public static function secretUnchanged( string $submitted ): bool {
$t = trim( $submitted );
return '' === $t || 1 === preg_match( '/^[\x{2022}*]+$/u', $t );
}
}