148 lines
7.5 KiB
PHP
148 lines
7.5 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Admin;
|
|
|
|
use StreamGoals\Core\Settings;
|
|
|
|
/** Donations table (search, filter, pagination), CSV export, and the message moderation queue. */
|
|
final class DonationsPage {
|
|
|
|
private const PER_PAGE = 25;
|
|
|
|
private static function fmtDate( string $utc ): string {
|
|
return get_date_from_gmt( $utc, 'Y-m-d H:i' );
|
|
}
|
|
|
|
public static function render(): void {
|
|
Admin::open( 'Donations' );
|
|
$status = Admin::get_str( 'status' );
|
|
$tests = 'hide' === Admin::get_str( 'tests' ) ? 'hide' : '';
|
|
$search = isset( $_GET['s'] ) && is_scalar( $_GET['s'] ) ? Input::text( wp_unslash( $_GET['s'] ), 100 ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
|
|
$page = max( 1, Admin::get_int( 'paged' ) );
|
|
$res = Store::searchDonations( array( 'status' => $status, 'search' => $search, 'tests' => $tests ), $page, self::PER_PAGE );
|
|
|
|
echo '<form method="get" class="sg-filter"><input type="hidden" name="page" value="streamgoals-donations">';
|
|
echo '<select name="status" aria-label="Status"><option value="">All statuses</option>';
|
|
foreach ( array( 'completed', 'pending' ) as $s ) {
|
|
echo '<option value="' . esc_attr( $s ) . '"' . selected( $status, $s, false ) . '>' . esc_html( ucfirst( $s ) ) . '</option>';
|
|
}
|
|
echo '</select> <label><input type="checkbox" name="tests" value="hide"' . checked( $tests, 'hide', false ) . '> Hide test donations</label> ';
|
|
echo '<input type="search" name="s" value="' . esc_attr( $search ) . '" placeholder="Search name, message or PayPal ID"> <button class="button">Filter</button> ';
|
|
echo '</form>';
|
|
Admin::form( 'sg_export_csv', array(), 'sg-inline' );
|
|
echo '<button class="button">Export CSV (all real donations)</button></form>';
|
|
|
|
echo '<p class="description">Donors who chose to be anonymous are shown as "Anonymous". Their name is never stored. Test donations are marked TEST and left out of the CSV and your totals.</p>';
|
|
|
|
if ( ! $res['rows'] ) {
|
|
echo '<div class="sg-card"><p>No donations found.</p></div>';
|
|
Admin::close();
|
|
return;
|
|
}
|
|
$goals = array();
|
|
foreach ( Store::goals()->all() as $g ) {
|
|
$goals[ (int) $g['id'] ] = (string) $g['title'];
|
|
}
|
|
echo '<table class="widefat striped sg-table"><thead><tr><th>Date</th><th>Donor</th><th>Amount</th><th>Fee</th><th>Total paid</th><th>Goal</th><th>Status</th><th>Message</th></tr></thead><tbody>';
|
|
foreach ( $res['rows'] as $r ) {
|
|
$cur = Format::currency( $r['currency'] );
|
|
$test = Format::isTest( $r );
|
|
echo '<tr><td>' . esc_html( self::fmtDate( (string) $r['created_at'] ) ) . '</td>';
|
|
echo '<td>' . esc_html( Format::donorName( $r ) ) . ( $test ? ' <span class="sg-badge sg-test">TEST</span>' : '' ) . '</td>';
|
|
echo '<td>' . esc_html( Format::money( (int) $r['base_minor'], $cur ) ) . '</td><td>' . esc_html( Format::money( (int) $r['fee_minor'], $cur ) ) . '</td><td>' . esc_html( Format::money( (int) $r['total_minor'], $cur ) ) . '</td>';
|
|
echo '<td>' . esc_html( $test ? '(test)' : ( $goals[ (int) $r['goal_id'] ] ?? '#' . (int) $r['goal_id'] ) ) . '</td>';
|
|
echo '<td>' . esc_html( ucfirst( (string) $r['status'] ) ) . ( 'approved' === $r['moderation'] ? '' : ' <span class="sg-badge sg-' . esc_attr( (string) $r['moderation'] ) . '">message ' . esc_html( (string) $r['moderation'] ) . '</span>' ) . '</td>';
|
|
echo '<td>' . esc_html( (string) $r['message'] ) . '</td></tr>';
|
|
}
|
|
echo '</tbody></table>';
|
|
$pages = (int) ceil( $res['total'] / self::PER_PAGE );
|
|
if ( $pages > 1 ) {
|
|
echo '<div class="tablenav"><div class="tablenav-pages">' . wp_kses_post( (string) paginate_links( array(
|
|
'base' => add_query_arg( 'paged', '%#%' ),
|
|
'format' => '',
|
|
'current' => $page,
|
|
'total' => $pages,
|
|
'prev_text' => '«',
|
|
'next_text' => '»',
|
|
) ) ) . '</div></div>';
|
|
}
|
|
echo '<p class="description">' . (int) $res['total'] . ' donation(s).</p>';
|
|
Admin::close();
|
|
}
|
|
|
|
public static function renderModeration(): void {
|
|
Admin::open( 'Message moderation' );
|
|
if ( ! Settings::get( 'moderate_messages' ) ) {
|
|
echo '<div class="notice notice-info inline"><p>Message moderation is currently off in Settings, so new messages are shown without review. Anything still waiting below was submitted while it was on.</p></div>';
|
|
}
|
|
echo '<p class="description">Approved messages can appear in alerts. Rejected messages are hidden. The donation itself still counts towards your goal either way.</p>';
|
|
$res = Store::searchDonations( array( 'status' => 'completed', 'moderation' => 'pending' ), 1, 100 );
|
|
if ( ! $res['rows'] ) {
|
|
echo '<div class="sg-card"><p>Nothing waiting for review.</p></div>';
|
|
Admin::close();
|
|
return;
|
|
}
|
|
echo '<table class="widefat striped sg-table"><thead><tr><th>Date</th><th>Donor</th><th>Amount</th><th>Message</th><th>Decision</th></tr></thead><tbody>';
|
|
foreach ( $res['rows'] as $r ) {
|
|
$cur = Format::currency( $r['currency'] );
|
|
echo '<tr><td>' . esc_html( self::fmtDate( (string) $r['created_at'] ) ) . '</td><td>' . esc_html( Format::donorName( $r ) ) . '</td>';
|
|
echo '<td>' . esc_html( Format::money( (int) $r['base_minor'], $cur ) ) . '</td><td>' . esc_html( (string) $r['message'] ) . '</td><td class="sg-actions">';
|
|
foreach ( array( 'approved' => 'Approve', 'rejected' => 'Reject' ) as $to => $lbl ) {
|
|
Admin::form( 'sg_moderate', array( 'donation_id' => (int) $r['id'], 'decision' => $to ), 'sg-inline' );
|
|
echo '<button class="button' . ( 'approved' === $to ? ' button-primary' : '' ) . '">' . esc_html( $lbl ) . '</button></form> ';
|
|
}
|
|
echo '</td></tr>';
|
|
}
|
|
echo '</tbody></table>';
|
|
Admin::close();
|
|
}
|
|
|
|
public static function handleModerate(): void {
|
|
Admin::guard( 'sg_moderate' );
|
|
$post = Admin::post_raw();
|
|
$id = isset( $post['donation_id'] ) ? absint( $post['donation_id'] ) : 0;
|
|
$to = isset( $post['decision'] ) && is_string( $post['decision'] ) ? $post['decision'] : '';
|
|
$ok = $id && Store::setModeration( $id, $to );
|
|
Admin::redirect( Admin::url( 'streamgoals-moderation' ), $ok ? 'success' : 'error', $ok ? ( 'approved' === $to ? 'Message approved.' : 'Message rejected.' ) : 'That message could not be updated (it may already be reviewed).' );
|
|
}
|
|
|
|
public static function handleExport(): void {
|
|
Admin::guard( 'sg_export_csv' );
|
|
$goals = array();
|
|
foreach ( Store::goals()->all() as $g ) {
|
|
$goals[ (int) $g['id'] ] = (string) $g['title'];
|
|
}
|
|
nocache_headers();
|
|
header( 'Content-Type: text/csv; charset=utf-8' );
|
|
header( 'Content-Disposition: attachment; filename="streamgoals-donations-' . gmdate( 'Y-m-d' ) . '.csv"' );
|
|
header( 'X-Content-Type-Options: nosniff' );
|
|
echo "\xEF\xBB\xBF"; // UTF-8 BOM so Excel reads accents correctly
|
|
echo Csv::line( array( 'ID', 'Date (UTC)', 'Goal', 'Donor', 'Amount', 'Fee covered', 'Total paid', 'Currency', 'Status', 'Message status', 'Message', 'PayPal order', 'PayPal capture' ) ); // phpcs:ignore WordPress.Security.EscapeOutput -- CSV, not HTML
|
|
$after = 0;
|
|
do {
|
|
$rows = Store::exportBatch( $after, 500 );
|
|
foreach ( $rows as $r ) {
|
|
$after = (int) $r['id'];
|
|
$cur = Format::currency( $r['currency'] );
|
|
echo Csv::line( array( // phpcs:ignore WordPress.Security.EscapeOutput
|
|
$r['id'],
|
|
$r['created_at'],
|
|
$goals[ (int) $r['goal_id'] ] ?? '',
|
|
Format::donorName( $r ),
|
|
Format::decimal( (int) $r['base_minor'], $cur ),
|
|
Format::decimal( (int) $r['fee_minor'], $cur ),
|
|
Format::decimal( (int) $r['total_minor'], $cur ),
|
|
$cur,
|
|
$r['status'],
|
|
$r['moderation'],
|
|
$r['message'],
|
|
$r['paypal_order_id'],
|
|
$r['paypal_capture_id'],
|
|
) );
|
|
}
|
|
} while ( count( $rows ) === 500 );
|
|
exit;
|
|
}
|
|
}
|