Files
wp-donations/includes/Admin/SettingsPage.php
T
shadowdaoandClaude Sonnet 5.5 68bd917660
Test and Release / test (push) Successful in 1m28s
Test and Release / release (push) Successful in 4s
LICENSE, clean uninstall (opt-in data removal), privacy policy text, Update URI header
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 14:00:04 -08:00

169 lines
15 KiB
PHP

<?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( \StreamGoals\Core\Settings::effectiveMin( (int) $s['min_donation_minor'] ), $cur ) ) . '"> <span class="description">At least ' . esc_html( Format::money( \StreamGoals\Core\Settings::HARD_MIN_MINOR, $cur ) ) . '. Visitors cannot donate less than this.</span></td></tr>';
echo '<tr><th scope="row"><label for="sg_notice">Notice under the donation form</label></th><td><input type="text" id="sg_notice" name="donation_notice" class="large-text" maxlength="200" value="' . esc_attr( (string) $s['donation_notice'] ) . '"><p class="description">Shown right under the PayPal buttons so donors see it before paying. Plain text, up to 200 characters. The default says donations are non-refundable. Leave it empty to hide the notice.</p></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>Checkout buttons</h2><table class="form-table" role="presentation"><tr><th scope="row">Payment options</th><td>';
echo '<label><input type="checkbox" name="funding_card" value="1"' . checked( ! empty( $s['funding_card'] ), true, false ) . '> Show "Debit or Credit Card" (donors without a PayPal account)</label><br>';
echo '<label><input type="checkbox" name="funding_paylater" value="1"' . checked( ! empty( $s['funding_paylater'] ), true, false ) . '> Show "Pay Later" (donors pay in instalments; you are still paid in full)</label>';
echo '<p class="description">The PayPal button is always shown. Which extra buttons appear can also depend on the donor\'s country.</p></td></tr></table></div>';
echo '<div class="sg-card"><h2>Advanced</h2><table class="form-table" role="presentation"><tr><th scope="row"><label for="sg_ip">Visitor IP address</label></th><td><select id="sg_ip" name="visitor_ip_header">';
foreach ( \StreamGoals\Core\ClientIp::MODES as $k => $l ) {
echo '<option value="' . esc_attr( $k ) . '"' . selected( (string) $s['visitor_ip_header'], $k, false ) . '>' . esc_html( $l ) . '</option>';
}
echo '</select><p class="description">Donation forms limit how fast one visitor can start payments (to stop abuse). Behind a proxy, every visitor can look like the same address, and a busy moment, like a raid, could block donations. <strong>Automatic</strong> recognises Cloudflare by its published addresses and counts each visitor separately; it ignores forged headers from anywhere else. For another kind of proxy, choose its option here, but only if <em>all</em> traffic to your site really goes through it.</p>';
self::ipDiagnostics( (string) $s['visitor_ip_header'] );
echo '</td></tr>';
echo '<tr><th scope="row">Uninstall</th><td><label><input type="checkbox" name="delete_data_on_uninstall" value="1"' . checked( ! empty( $s['delete_data_on_uninstall'] ), true, false ) . '> Delete all data when the plugin is deleted</label>';
echo '<p class="description"><strong>When you delete the plugin from the Plugins screen, also delete ALL donations, goals, rules, rewards, alerts, tiers and settings. This cannot be undone. Leave off to keep your data if you reinstall later.</strong></p></td></tr></table></div>';
$rel = \StreamGoals\Update\Updater::latest();
echo '<div class="sg-card"><h2>Plugin updates</h2><table class="form-table" role="presentation">';
echo '<tr><th scope="row">Installed version</th><td><code>' . esc_html( STREAMGOALS_VERSION ) . '</code></td></tr>';
echo '<tr><th scope="row">Latest release</th><td>' . ( $rel ? '<code>' . esc_html( $rel['version'] ) . '</code>' . ( \StreamGoals\Update\ReleaseParser::isNewer( $rel['version'], STREAMGOALS_VERSION ) ? ' (update available)' : ' (up to date)' ) : 'Not found yet (or the check failed).' ) . ' <button type="submit" form="sg_update_check_form" class="button">Check for updates now</button></td></tr>';
echo '<tr><th scope="row"><label for="sg_utoken">Access token (only needed while the update repository is private)</label></th><td><input type="password" id="sg_utoken" name="update_token" class="regular-text code" autocomplete="new-password" spellcheck="false" value="" placeholder="' . esc_attr( '' !== (string) $s['update_token'] ? Input::maskSecret( (string) $s['update_token'] ) : 'Paste a read-only token' ) . '">';
echo '<p class="description">' . ( '' !== (string) $s['update_token'] ? 'A token is saved. Leave this empty to keep it, or paste a new one to replace it.' : 'Not needed once the repository is public.' ) . '</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>';
echo '<form id="sg_update_check_form" method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '"><input type="hidden" name="action" value="' . esc_attr( \StreamGoals\Update\Updater::ACTION_CHECK ) . '">';
wp_nonce_field( \StreamGoals\Update\Updater::ACTION_CHECK );
echo '</form>';
Admin::close();
}
/** Shows the owner what their server passes to the plugin right now, to help pick the right option. */
private static function ipDiagnostics( string $mode ): void {
$keys = array( 'REMOTE_ADDR' => 'Connection address', 'HTTP_CF_CONNECTING_IP' => 'CF-Connecting-IP', 'HTTP_CF_RAY' => 'CF-Ray', 'HTTP_X_REAL_IP' => 'X-Real-IP', 'HTTP_X_FORWARDED_FOR' => 'X-Forwarded-For' );
$server = array();
foreach ( $keys as $k => $_ ) {
if ( isset( $_SERVER[ $k ] ) && is_string( $_SERVER[ $k ] ) ) {
$server[ $k ] = sanitize_text_field( wp_unslash( $_SERVER[ $k ] ) );
}
}
echo '<details class="sg-ipdiag"><summary>What does my server see right now?</summary><table class="widefat striped" style="max-width:36rem"><tbody>';
foreach ( $keys as $k => $label ) {
echo '<tr><td>' . esc_html( $label ) . '</td><td><code>' . ( isset( $server[ $k ] ) ? esc_html( $server[ $k ] ) : '(not sent)' ) . '</code></td></tr>';
}
$viaCf = isset( $server['REMOTE_ADDR'] ) && \StreamGoals\Core\CloudflareRanges::isCloudflare( $server['REMOTE_ADDR'], (array) apply_filters( 'streamgoals_cloudflare_ranges', array() ) );
echo '<tr><td>Is this connection from Cloudflare?</td><td>' . ( $viaCf ? '<strong>Yes</strong> - the connection comes from a Cloudflare address' : 'No' ) . '</td></tr>';
echo '<tr><td><strong>Visitor address used with the saved option</strong></td><td><code>' . esc_html( \StreamGoals\Core\ClientIp::resolve( $server, $mode ) ) . '</code></td></tr>';
echo '</tbody></table>' . ( ! $viaCf && ( isset( $server['HTTP_CF_RAY'] ) || isset( $server['HTTP_CF_CONNECTING_IP'] ) ) ? '<p class="sg-warn">Cloudflare headers were sent, but the connection is not from a known Cloudflare address, so "Automatic" ignores them. If your site really is behind Cloudflare, choose "Cloudflare" above (or update the plugin, as Cloudflare may have added new addresses).</p>' : '' ) . '<p class="description">This is your own visit. If the connection address belongs to your proxy or CDN (not to you) and your proxy\'s header shows your real address, choose that proxy option above and save. Then the "used" row should show your real address.</p></details>';
}
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 < \StreamGoals\Core\Settings::HARD_MIN_MINOR ) {
$errors[] = 'Minimum donation must be at least ' . Format::money( \StreamGoals\Core\Settings::HARD_MIN_MINOR, $cur ) . '. PayPal\'s fixed fee makes smaller donations lose money.';
} 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;
}
$ipMode = isset( $post['visitor_ip_header'] ) && is_string( $post['visitor_ip_header'] ) ? $post['visitor_ip_header'] : '';
if ( ! array_key_exists( $ipMode, \StreamGoals\Core\ClientIp::MODES ) ) {
$errors[] = 'Please choose a visitor IP option.';
} else {
$upd['visitor_ip_header'] = $ipMode;
}
$tok = $post['update_token'] ?? '';
if ( ! Input::secretUnchanged( is_string( $tok ) ? $tok : '' ) ) {
$upd['update_token'] = Input::text( $tok, 300 );
}
$upd['funding_card'] = ! empty( $post['funding_card'] );
$upd['funding_paylater'] = ! empty( $post['funding_paylater'] );
$upd['delete_data_on_uninstall'] = ! empty( $post['delete_data_on_uninstall'] );
$upd['donation_notice'] = Input::text( $post['donation_notice'] ?? '', 200 );
$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.' );
}
}