From e4d9042abae47a14e12a2d93989108135a3a043c Mon Sep 17 00:00:00 2001
From: Josh Knapp
Date: Sun, 15 Nov 2026 11:40:06 -0800
Subject: [PATCH] Add visitor-IP header setting, checkout-button toggles, and
configurable timer maximum
- Core\ClientIp: trusted-header resolution for rate limiting behind Cloudflare/proxies
- Settings: show/hide Pay Later and Debit/Credit Card (Pay Later off by default)
- add_time rule: optional max_timer_seconds (uncapped by default), editable in rule form
Co-Authored-By: Claude Sonnet 5.5
---
assets/admin/rules.js | 7 ++-
assets/donate/donate.js | 3 +-
docs/ARCHITECTURE.md | 4 +-
includes/Admin/GoalsPage.php | 3 +-
includes/Admin/RuleConfig.php | 9 ++++
includes/Admin/SettingsPage.php | 19 +++++++
includes/Admin/Templates.php | 2 +-
includes/Core/ClientIp.php | 56 ++++++++++++++++++++
includes/Core/Settings.php | 3 ++
includes/Rest/DonateController.php | 9 +++-
includes/Rules/AddTimeRule.php | 19 ++++---
tests/Unit/AdminLogicTest.php | 19 ++++++-
tests/Unit/ClientIpTest.php | 59 +++++++++++++++++++++
tests/Unit/RulesEngineTest.php | 82 ++++++++++++++++++++++++++++++
14 files changed, 278 insertions(+), 16 deletions(-)
create mode 100644 includes/Core/ClientIp.php
create mode 100644 tests/Unit/ClientIpTest.php
diff --git a/assets/admin/rules.js b/assets/admin/rules.js
index 940547d..72185e2 100644
--- a/assets/admin/rules.js
+++ b/assets/admin/rules.js
@@ -42,7 +42,8 @@
if ( ! c.unit_minor ) { return null; }
var d = Math.floor( amount * c.seconds_per_unit / c.unit_minor );
if ( c.cap_seconds_per_donation != null ) { d = Math.min( d, c.cap_seconds_per_donation ); }
- return d > 0 ? 'Timer: +' + duration( d ) : 'Timer: no time added';
+ var note = c.max_timer_seconds != null ? ' (the timer never goes above ' + duration( c.max_timer_seconds ) + ')' : '';
+ return ( d > 0 ? 'Timer: +' + duration( d ) : 'Timer: no time added' ) + note;
}
if ( rule.type === 'counter' ) {
if ( ! c.per_minor ) { return null; }
@@ -74,6 +75,7 @@
/* Type switch + preview of the rule being edited. */
if ( ! form ) { return; }
+
var typeSel = document.getElementById( 'sg-rule-type' );
var typeHidden = form.querySelector( 'input[name="type"]' );
var formPrev = document.getElementById( 'sg-form-preview' );
@@ -93,7 +95,8 @@
var n = parseInt( val( g, 'time_amount' ), 10 );
if ( ! unit || ! n ) { return null; }
var cap = parseInt( val( g, 'cap_minutes' ), 10 );
- return { type: type, config: { seconds_per_unit: n * mult, unit_minor: unit, cap_seconds_per_donation: cap > 0 ? cap * 60 : null } };
+ var clock = parseInt( val( g, 'max_timer_minutes' ), 10 );
+ return { type: type, config: { seconds_per_unit: n * mult, unit_minor: unit, cap_seconds_per_donation: cap > 0 ? cap * 60 : null, max_timer_seconds: clock > 0 ? clock * 60 : null } };
}
if ( type === 'counter' ) {
var step = parseInt( val( g, 'step' ), 10 );
diff --git a/assets/donate/donate.js b/assets/donate/donate.js
index 4b88671..abed65f 100644
--- a/assets/donate/donate.js
+++ b/assets/donate/donate.js
@@ -41,7 +41,8 @@
var s = document.createElement('script');
s.id = 'sg-paypal-sdk';
s.src = 'https://www.paypal.com/sdk/js?client-id=' + encodeURIComponent(C.clientId) +
- '¤cy=' + encodeURIComponent(currency) + '&intent=capture&components=buttons';
+ '¤cy=' + encodeURIComponent(currency) + '&intent=capture&components=buttons' +
+ ((C.disableFunding && C.disableFunding.length) ? '&disable-funding=' + encodeURIComponent(C.disableFunding.join(',')) : '');
s.onload = function () { window.paypal ? resolve(window.paypal) : reject(new Error(T.sdkError)); };
s.onerror = function () { reject(new Error(T.sdkError)); };
document.head.appendChild(s);
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index 6861b63..fa0fb9c 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -37,9 +37,9 @@ Rules engine entry point (Agent 2 implements, Agent 1 calls after a capture is r
Rules\Engine::applyDonation(Donation $d): void; // idempotent via sg_rule_events UNIQUE
Rules\Engine::reverseDonation(Donation $d): void; // honors per-rule "reverse on refund"
```
-Rule `config` examples:
+Rule `config` examples (`max_total_seconds` caps what one rule has added over time; `max_timer_seconds` caps how long the timer can ever get, null = uncapped):
```json
-{"type":"add_time","seconds_per_unit":60,"unit_minor":100,"cap_seconds_per_donation":null,"max_total_seconds":null,"basis":"base"}
+{"type":"add_time","seconds_per_unit":60,"unit_minor":100,"cap_seconds_per_donation":null,"max_total_seconds":null,"max_timer_seconds":null,"basis":"base"}
{"type":"counter","step":1,"per_minor":1000,"carry_over":true,"basis":"base","label":"Pies"}
{"type":"bonus","min_minor":5000,"event":"big_donation"}
```
diff --git a/includes/Admin/GoalsPage.php b/includes/Admin/GoalsPage.php
index 919cfb0..8f51570 100644
--- a/includes/Admin/GoalsPage.php
+++ b/includes/Admin/GoalsPage.php
@@ -249,9 +249,10 @@ final class GoalsPage {
echo '' . esc_html( $u ) . ' ';
}
echo ' to the timer for every ' . esc_html( $cur ) . ' donated.
';
- echo 'Limits (optional) ';
+ echo 'Limits (optional) ';
echo 'Most minutes one donation can add
';
echo 'Most minutes this rule can add in total
';
+ echo 'Longest the timer can ever get (minutes) Leave empty for no limit. For example, 1440 means the clock never shows more than 24 hours.
';
echo 'Count ' . $basisOpts( 'add_time' ) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput -- built from escaped parts
// counter
echo 'Add to the counter for every ' . esc_html( $cur ) . ' donated.
';
diff --git a/includes/Admin/RuleConfig.php b/includes/Admin/RuleConfig.php
index e0e6072..cba5536 100644
--- a/includes/Admin/RuleConfig.php
+++ b/includes/Admin/RuleConfig.php
@@ -46,6 +46,7 @@ final class RuleConfig {
}
$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 );
}
@@ -59,6 +60,7 @@ final class RuleConfig {
'unit_minor' => $unit,
'cap_seconds_per_donation' => $cap,
'max_total_seconds' => $max,
+ 'max_timer_seconds' => $clk,
'basis' => $basis,
),
'errors' => array(),
@@ -128,6 +130,9 @@ final class RuleConfig {
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'] ?? '' );
@@ -158,6 +163,9 @@ final class RuleConfig {
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'] ?? '' );
@@ -182,6 +190,7 @@ final class RuleConfig {
$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 );
diff --git a/includes/Admin/SettingsPage.php b/includes/Admin/SettingsPage.php
index 5e9afa9..37f8c58 100644
--- a/includes/Admin/SettingsPage.php
+++ b/includes/Admin/SettingsPage.php
@@ -47,6 +47,17 @@ final class SettingsPage {
echo '
Use the built-in word list';
echo '
Your own words to block (one per line)
';
+ echo '';
+
+ echo '';
+
echo 'PayPal fee estimate ';
echo '
When a donor chooses "cover the fees", the form adds the estimated PayPal fee so you receive the amount they intended. The defaults (3.49% + $0.49 ) are US domestic estimates . Fees for international (cross-border) payments and currency conversion are higher and are not included, so you may receive slightly less than expected in those cases. Check PayPal\'s current fee page for your country and adjust.
';
echo '