'Add time to the timer', 'counter' => 'Counter', 'bonus' => 'Big-donation bonus alert', ); private const UNIT_SECONDS = array( 'seconds' => 1, 'minutes' => 60, 'hours' => 3600 ); /** * @param array $in Raw (already wp_unslash'ed) form fields. * @return array{config:?array,errors:string[]} */ public static function sanitize( string $type, array $in, string $currency ): array { $errors = array(); $basis = ( $in['basis'] ?? 'base' ) === 'total' ? 'total' : 'base'; $unit = Input::amountToMinor( $in['unit_amount'] ?? '', $currency ); switch ( $type ) { case 'add_time': $mult = self::UNIT_SECONDS[ (string) ( $in['time_unit'] ?? 'minutes' ) ] ?? null; $n = Input::int( $in['time_amount'] ?? '', 1, 1000000 ); if ( null === $mult ) { $errors[] = 'Choose seconds, minutes or hours.'; } if ( null === $n ) { $errors[] = 'Enter how much time to add (a whole number, 1 or more).'; } if ( null === $unit || $unit <= 0 ) { $errors[] = 'Enter the donation amount that earns that time (for example 1.00).'; } $cap = self::optionalMinutes( $in['cap_minutes'] ?? '', 'The per-donation limit', $errors ); $max = self::optionalMinutes( $in['max_total_minutes'] ?? '', 'The total limit', $errors ); $clk = self::optionalMinutes( $in['max_timer_minutes'] ?? '', 'The longest the timer can get', $errors ); if ( $errors ) { return array( 'config' => null, 'errors' => $errors ); } $secs = $n * $mult; if ( $secs > 31536000 ) { return array( 'config' => null, 'errors' => array( 'That is too much time per unit (more than a year).' ) ); } return array( 'config' => array( 'seconds_per_unit' => $secs, 'unit_minor' => $unit, 'cap_seconds_per_donation' => $cap, 'max_total_seconds' => $max, 'max_timer_seconds' => $clk, 'basis' => $basis, ), 'errors' => array(), ); case 'counter': $step = Input::int( $in['step'] ?? '', 1, 1000000 ); if ( null === $step ) { $errors[] = 'Enter how much the counter goes up each time (a whole number, 1 or more).'; } if ( null === $unit || $unit <= 0 ) { $errors[] = 'Enter the donation amount that earns each step (for example 10.00).'; } if ( $errors ) { return array( 'config' => null, 'errors' => $errors ); } return array( 'config' => array( 'step' => $step, 'per_minor' => $unit, 'carry_over' => ! empty( $in['carry_over'] ), 'basis' => $basis, 'label' => Input::text( $in['label'] ?? '', 40 ), ), 'errors' => array(), ); case 'bonus': $min = Input::amountToMinor( $in['min_amount'] ?? '', $currency ); if ( null === $min || $min <= 0 ) { $errors[] = 'Enter the smallest single donation that triggers the bonus (for example 50.00).'; return array( 'config' => null, 'errors' => $errors ); } return array( 'config' => array( 'min_minor' => $min, 'event' => Input::slug( $in['event'] ?? '' ), 'basis' => $basis, ), 'errors' => array(), ); } return array( 'config' => null, 'errors' => array( 'Unknown rule type.' ) ); } private static function optionalMinutes( mixed $raw, string $what, array &$errors ): ?int { if ( ! is_scalar( $raw ) || '' === trim( (string) $raw ) ) { return null; } $m = Input::int( $raw, 1, 525600 ); if ( null === $m ) { $errors[] = $what . ' must be a whole number of minutes (or leave it empty for no limit).'; return null; } return $m * 60; } /** Human-readable one-liner, e.g. "Adds 1 minute for every $1.00". */ public static function summary( string $type, array $c, string $currency ): string { $basisNote = 'total' === ( $c['basis'] ?? 'base' ) ? ' (including fees the donor covers)' : ''; switch ( $type ) { case 'add_time': $s = 'Adds ' . Format::duration( (int) ( $c['seconds_per_unit'] ?? 0 ) ) . ' for every ' . Format::money( (int) ( $c['unit_minor'] ?? 0 ), $currency ) . $basisNote; if ( isset( $c['cap_seconds_per_donation'] ) ) { $s .= ', at most ' . Format::duration( (int) $c['cap_seconds_per_donation'] ) . ' per donation'; } if ( isset( $c['max_total_seconds'] ) ) { $s .= ', ' . Format::duration( (int) $c['max_total_seconds'] ) . ' in total'; } if ( isset( $c['max_timer_seconds'] ) ) { $s .= ', up to ' . Format::duration( (int) $c['max_timer_seconds'] ) . ' on the clock'; } return $s; case 'counter': $label = (string) ( $c['label'] ?? '' ); $s = 'Adds ' . (int) ( $c['step'] ?? 1 ) . ( '' !== $label ? ' to "' . $label . '"' : ' to the counter' ) . ' for every ' . Format::money( (int) ( $c['per_minor'] ?? 0 ), $currency ) . $basisNote; return $s . ( ! empty( $c['carry_over'] ) ? ' (leftover amounts carry over to the next donation)' : ' (leftover amounts are not carried over)' ); case 'bonus': return 'Triggers the "' . (string) ( $c['event'] ?? 'bonus' ) . '" bonus for any single donation of ' . Format::money( (int) ( $c['min_minor'] ?? 0 ), $currency ) . ' or more' . $basisNote; } return 'Unknown rule'; } /** * What would a donation of $baseMinor (plus optional covered fee) do against these rules, * assuming a fresh state (no carry-over, no accumulated caps)? * * @param array $rules * @return string[] */ public static function preview( array $rules, int $baseMinor, string $currency, int $feeMinor = 0 ): array { $d = new Donation( 0, 0, $baseMinor, $feeMinor, $baseMinor + $feeMinor, $currency ); $lines = array(); foreach ( $rules as $r ) { $type = (string) ( $r['type'] ?? '' ); $cfg = (array) ( $r['config'] ?? array() ); try { if ( 'add_time' === $type ) { $res = AddTimeRule::fromConfig( $cfg )->apply( $d, 0, 0, 0 ); $lines[] = $res['delta'] > 0 ? 'Timer: +' . Format::duration( $res['delta'] ) : 'Timer: no time added'; if ( isset( $cfg['max_timer_seconds'] ) ) { $lines[ count( $lines ) - 1 ] .= ' (the timer never goes above ' . Format::duration( (int) $cfg['max_timer_seconds'] ) . ')'; } } elseif ( 'counter' === $type ) { $res = CounterRule::fromConfig( $cfg )->apply( $d, 0, 0 ); $lbl = (string) ( $cfg['label'] ?? '' ); $lines[] = ( '' !== $lbl ? $lbl : 'Counter' ) . ': +' . $res['delta']; } elseif ( 'bonus' === $type ) { $b = BonusRule::fromConfig( $cfg ); $lines[] = $b->apply( $d ) ? 'Bonus "' . $b->event . '" triggers' : 'Bonus "' . $b->event . '" does not trigger'; } } catch ( \InvalidArgumentException ) { $lines[] = 'A rule is misconfigured and will be skipped.'; } } return $lines; } /** Reverse of sanitize(): config -> editable form values. */ public static function toForm( string $type, array $c, string $currency ): array { $f = array( 'basis' => 'total' === ( $c['basis'] ?? 'base' ) ? 'total' : 'base' ); if ( 'add_time' === $type ) { $s = (int) ( $c['seconds_per_unit'] ?? 60 ); [ $f['time_amount'], $f['time_unit'] ] = 0 === $s % 3600 && $s > 0 ? array( $s / 3600, 'hours' ) : ( 0 === $s % 60 && $s > 0 ? array( $s / 60, 'minutes' ) : array( $s, 'seconds' ) ); $f['unit_amount'] = Format::decimal( (int) ( $c['unit_minor'] ?? 100 ), $currency ); $f['cap_minutes'] = isset( $c['cap_seconds_per_donation'] ) ? (string) intdiv( (int) $c['cap_seconds_per_donation'], 60 ) : ''; $f['max_total_minutes'] = isset( $c['max_total_seconds'] ) ? (string) intdiv( (int) $c['max_total_seconds'], 60 ) : ''; $f['max_timer_minutes'] = isset( $c['max_timer_seconds'] ) ? (string) intdiv( (int) $c['max_timer_seconds'], 60 ) : ''; } elseif ( 'counter' === $type ) { $f['step'] = (int) ( $c['step'] ?? 1 ); $f['unit_amount'] = Format::decimal( (int) ( $c['per_minor'] ?? 1000 ), $currency ); $f['carry_over'] = ! empty( $c['carry_over'] ); $f['label'] = (string) ( $c['label'] ?? '' ); } else { $f['min_amount'] = Format::decimal( (int) ( $c['min_minor'] ?? 5000 ), $currency ); $f['event'] = (string) ( $c['event'] ?? 'big_donation' ); } return $f; } }