Add admin UI: setup wizard, goals, rules editor, donations, moderation, overlays, settings
Test and Release / test (push) Successful in 2m34s
Test and Release / release (push) Successful in 9s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 10:26:03 -08:00
1 parent 0b9e6e7cbb
commit 4efdef72f6
18 files changed
+2169

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Admin;
use StreamGoals\Core\Settings;
/** Settings: PayPal credentials (own form), fees, anonymity, moderation, profanity, limits, currency. */
final class SettingsPage {
private const POLICIES = array(
'allow' => 'Donors choose: they can give a name or stay anonymous',
'require_name' => 'Donors must give a name',
'force_anonymous' => 'Everyone is anonymous (no names are collected or stored)',
);
public static function render(): void {
Admin::open( 'StreamGoals settings' );
$s = Settings::all();
$cur = Admin::currency();
echo '<div class="sg-card"><h2>PayPal connection</h2>';
echo PayPalConnector::isConnected() ? '<p class="sg-ok">Connected (' . esc_html( 'live' === $s['paypal_mode'] ? 'Live' : 'Sandbox' ) . ').</p>' : '<p class="sg-warn">Not connected yet. Follow the steps in the <a href="' . esc_url( SetupPage::url( 1 ) ) . '">setup guide</a>.</p>';
Admin::form( 'sg_paypal_connect', array( 'return' => 'settings' ) );
PayPalConnector::fields();
echo '<p class="description">Webhook address (registered automatically): <code>' . esc_html( PayPalConnector::webhookUrl() ) . '</code><br>Webhook status: ' . ( '' !== (string) $s['paypal_webhook_id'] ? 'registered' : 'not registered' ) . '</p>';
echo '<p><button class="button button-primary">Save and test connection</button></p></form></div>';
Admin::form( 'sg_settings_save' );
echo '<div class="sg-card"><h2>Donations</h2><table class="form-table" role="presentation">';
echo '<tr><th scope="row"><label for="sg_cur">Currency</label></th><td><select id="sg_cur" name="currency">';
foreach ( Format::CURRENCIES as $c => $name ) {
echo '<option value="' . esc_attr( $c ) . '"' . selected( $cur, $c, false ) . '>' . esc_html( $c . ' - ' . $name ) . '</option>';
}
echo '</select><p class="description">One currency for the whole site. It applies to goals you create from now on; existing goals keep their currency. Check the fixed fee below after changing it.</p></td></tr>';
echo '<tr><th scope="row"><label for="sg_min">Minimum donation (' . esc_html( $cur ) . ')</label></th><td><input type="text" id="sg_min" name="min_donation" class="small-text" inputmode="decimal" value="' . esc_attr( Format::decimal( (int) $s['min_donation_minor'], $cur ) ) . '"></td></tr>';
echo '<tr><th scope="row"><label for="sg_len">Longest message</label></th><td><input type="number" id="sg_len" name="message_max_length" min="0" max="500" class="small-text" value="' . (int) $s['message_max_length'] . '"> characters <span class="description">(0 to 500; 0 = no messages)</span></td></tr>';
echo '<tr><th scope="row">Names</th><td><fieldset>';
foreach ( self::POLICIES as $k => $l ) {
echo '<label><input type="radio" name="anonymity_policy" value="' . esc_attr( $k ) . '"' . checked( $s['anonymity_policy'], $k, false ) . '> ' . esc_html( $l ) . '</label><br>';
}
echo '</fieldset><p class="description">Anonymous donors\' names are never saved.</p></td></tr></table></div>';
echo '<div class="sg-card"><h2>Messages</h2><table class="form-table" role="presentation">';
echo '<tr><th scope="row">Moderation</th><td><label><input type="checkbox" name="moderate_messages" value="1"' . checked( ! empty( $s['moderate_messages'] ), true, false ) . '> Hold messages for my approval before they can be shown in alerts</label></td></tr>';
echo '<tr><th scope="row">Profanity filter</th><td><label><input type="checkbox" name="profanity_filter" value="1"' . checked( ! empty( $s['profanity_filter'] ), true, false ) . '> Filter bad language in messages and names</label><br>';
echo '<label><input type="checkbox" name="profanity_use_builtin" value="1"' . checked( ! empty( $s['profanity_use_builtin'] ), true, false ) . '> Use the built-in word list</label>';
echo '<p><label for="sg_words">Your own words to block (one per line)</label><br><textarea id="sg_words" name="profanity_words" rows="6" class="large-text code">' . esc_textarea( (string) $s['profanity_words'] ) . '</textarea></p></td></tr></table></div>';
echo '<div class="sg-card"><h2>PayPal fee estimate</h2>';
echo '<p>When a donor chooses "cover the fees", the form adds the estimated PayPal fee so you receive the amount they intended. The defaults (<strong>3.49% + $0.49</strong>) are <strong>US domestic estimates</strong>. Fees for international (cross-border) payments and currency conversion are higher and are <strong>not</strong> included, so you may receive slightly less than expected in those cases. Check PayPal\'s current fee page for your country and adjust.</p>';
echo '<table class="form-table" role="presentation"><tr><th scope="row"><label for="sg_pct">Percentage fee</label></th><td><input type="text" id="sg_pct" name="fee_percent" class="small-text" inputmode="decimal" value="' . esc_attr( number_format( (int) $s['fee_percent_bps'] / 100, 2, '.', '' ) ) . '"> %</td></tr>';
echo '<tr><th scope="row"><label for="sg_fix">Fixed fee (' . esc_html( $cur ) . ')</label></th><td><input type="text" id="sg_fix" name="fee_fixed" class="small-text" inputmode="decimal" value="' . esc_attr( Format::decimal( (int) $s['fee_fixed_minor'], $cur ) ) . '"></td></tr></table></div>';
echo '<p><button class="button button-primary button-hero">Save settings</button></p></form>';
Admin::close();
}
public static function handleSave(): void {
Admin::guard( 'sg_settings_save' );
$post = Admin::post_raw();
$back = Admin::url( 'streamgoals-settings' );
$errors = array();
$cur = Format::currency( $post['currency'] ?? 'USD' );
$upd = array();
$min = Input::amountToMinor( $post['min_donation'] ?? '', $cur );
if ( null === $min || $min < 1 ) {
$errors[] = 'Minimum donation must be an amount greater than zero.';
} else {
$upd['min_donation_minor'] = $min;
}
$len = Input::int( $post['message_max_length'] ?? '', 0, 500 );
if ( null === $len ) {
$errors[] = 'Longest message must be a number from 0 to 500.';
} else {
$upd['message_max_length'] = $len;
}
$pct = Input::percentToBps( $post['fee_percent'] ?? '' );
if ( null === $pct ) {
$errors[] = 'Percentage fee must be between 0 and 30 (for example 3.49).';
} else {
$upd['fee_percent_bps'] = $pct;
}
$fix = Input::amountToMinor( $post['fee_fixed'] ?? '', $cur );
if ( null === $fix || $fix > 10000 ) {
$errors[] = 'Fixed fee must be a small amount (for example 0.49).';
} else {
$upd['fee_fixed_minor'] = $fix;
}
$pol = isset( $post['anonymity_policy'] ) && is_string( $post['anonymity_policy'] ) ? $post['anonymity_policy'] : '';
if ( ! isset( self::POLICIES[ $pol ] ) ) {
$errors[] = 'Please choose a names option.';
} else {
$upd['anonymity_policy'] = $pol;
}
$upd['moderate_messages'] = ! empty( $post['moderate_messages'] );
$upd['profanity_filter'] = ! empty( $post['profanity_filter'] );
$upd['profanity_use_builtin'] = ! empty( $post['profanity_use_builtin'] );
$upd['profanity_words'] = Input::words( isset( $post['profanity_words'] ) && is_string( $post['profanity_words'] ) ? $post['profanity_words'] : '' );
Settings::update( $upd );
update_option( 'sg_currency', $cur, false ); // Settings has no currency key, so it lives in its own option.
if ( $errors ) {
Admin::redirect( $back, 'warning', 'Some settings were not saved: ' . implode( ' ', $errors ) . ' Everything else was saved.' );
}
Admin::redirect( $back, 'success', 'Settings saved.' );
}
}