- Tiers\TierPolicy (pure), TierRepository (atomic reached marking), Tiers\Recorder hooked before close-at-target - Admin tiers screen; goal target derived from the final tier - Overlay goal widget shows Tier N of M and fills within the tier; alert pill; donate form 'Next unlock' - Installer DB_VERSION 4 (sg_goal_tiers) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
199 lines
10 KiB
PHP
199 lines
10 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Admin;
|
|
|
|
use StreamGoals\Core\Settings;
|
|
|
|
/**
|
|
* Admin bootstrap: one top-level "StreamGoals" menu, assets, shared helpers (nonce/capability guard,
|
|
* flash notices). Every screen requires manage_options; every admin-post handler goes through guard().
|
|
*/
|
|
final class Admin {
|
|
|
|
public const CAP = 'manage_options';
|
|
public const HOME = 'streamgoals';
|
|
|
|
/** admin-post action => [class, method]. */
|
|
private const HANDLERS = array(
|
|
'sg_paypal_connect' => array( PayPalConnector::class, 'handle' ),
|
|
'sg_wizard_goal' => array( SetupPage::class, 'handleGoal' ),
|
|
'sg_wizard_page' => array( SetupPage::class, 'handlePage' ),
|
|
'sg_goal_save' => array( GoalsPage::class, 'handleSave' ),
|
|
'sg_goal_status' => array( GoalsPage::class, 'handleStatus' ),
|
|
'sg_goal_delete' => array( GoalsPage::class, 'handleDelete' ),
|
|
'sg_rule_save' => array( GoalsPage::class, 'handleRuleSave' ),
|
|
'sg_rule_toggle' => array( GoalsPage::class, 'handleRuleToggle' ),
|
|
'sg_rule_delete' => array( GoalsPage::class, 'handleRuleDelete' ),
|
|
'sg_tier_save' => array( GoalsPage::class, 'handleTierSave' ),
|
|
'sg_tier_delete' => array( GoalsPage::class, 'handleTierDelete' ),
|
|
'sg_action_save' => array( ActionsPage::class, 'handleSave' ),
|
|
'sg_action_toggle' => array( ActionsPage::class, 'handleToggle' ),
|
|
'sg_action_delete' => array( ActionsPage::class, 'handleDelete' ),
|
|
'sg_alert_save' => array( AlertsPage::class, 'handleSave' ),
|
|
'sg_alert_toggle' => array( AlertsPage::class, 'handleToggle' ),
|
|
'sg_alert_delete' => array( AlertsPage::class, 'handleDelete' ),
|
|
'sg_event_done' => array( ActionsPage::class, 'handleDone' ),
|
|
'sg_event_done_all' => array( ActionsPage::class, 'handleDoneAll' ),
|
|
'sg_export_csv' => array( DonationsPage::class, 'handleExport' ),
|
|
'sg_moderate' => array( DonationsPage::class, 'handleModerate' ),
|
|
'sg_test_donation' => array( OverlaysPage::class, 'handleTestDonation' ),
|
|
'sg_delete_tests' => array( OverlaysPage::class, 'handleDeleteTests' ),
|
|
'sg_regen_token' => array( OverlaysPage::class, 'handleRegenerate' ),
|
|
'sg_settings_save' => array( SettingsPage::class, 'handleSave' ),
|
|
);
|
|
|
|
public static function register(): void {
|
|
add_action( 'admin_menu', array( self::class, 'menu' ) );
|
|
add_action( 'admin_init', array( self::class, 'maybe_flush_rewrites' ) );
|
|
add_action( 'admin_enqueue_scripts', array( self::class, 'assets' ) );
|
|
foreach ( self::HANDLERS as $action => $cb ) {
|
|
add_action( 'admin_post_' . $action, $cb );
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ menu */
|
|
|
|
public static function menu(): void {
|
|
$connected = PayPalConnector::isConnected();
|
|
add_menu_page( 'StreamGoals', 'StreamGoals', self::CAP, self::HOME, array( self::class, 'page_home' ), 'dashicons-heart', 58 );
|
|
add_submenu_page( self::HOME, $connected ? 'Goals' : 'Setup', $connected ? 'Goals' : 'Setup', self::CAP, self::HOME, array( self::class, 'page_home' ) );
|
|
if ( $connected ) {
|
|
add_submenu_page( self::HOME, 'Setup guide', 'Setup guide', self::CAP, 'streamgoals-setup', array( SetupPage::class, 'render' ) );
|
|
}
|
|
add_submenu_page( self::HOME, 'Donations', 'Donations', self::CAP, 'streamgoals-donations', array( DonationsPage::class, 'render' ) );
|
|
$pending = Store::pendingModerationCount();
|
|
if ( Settings::get( 'moderate_messages' ) || $pending > 0 ) {
|
|
$badge = $pending > 0 ? ' <span class="awaiting-mod">' . (int) $pending . '</span>' : '';
|
|
add_submenu_page( self::HOME, 'Moderation', 'Moderation' . $badge, self::CAP, 'streamgoals-moderation', array( DonationsPage::class, 'renderModeration' ) );
|
|
}
|
|
$queued = ActionsPage::queuedCount();
|
|
$qbadge = $queued > 0 ? ' <span class="awaiting-mod">' . (int) $queued . '</span>' : '';
|
|
add_submenu_page( self::HOME, 'Rewards', 'Rewards' . $qbadge, self::CAP, ActionsPage::PAGE, array( ActionsPage::class, 'render' ) );
|
|
add_submenu_page( self::HOME, 'Alerts', 'Alerts', self::CAP, AlertsPage::PAGE, array( AlertsPage::class, 'render' ) );
|
|
add_submenu_page( self::HOME, 'Overlays', 'Overlays', self::CAP, 'streamgoals-overlays', array( OverlaysPage::class, 'render' ) );
|
|
add_submenu_page( self::HOME, 'Settings', 'Settings', self::CAP, 'streamgoals-settings', array( SettingsPage::class, 'render' ) );
|
|
}
|
|
|
|
/** First screen: the setup wizard until PayPal is connected, then the goals list. */
|
|
public static function page_home(): void {
|
|
if ( PayPalConnector::isConnected() ) {
|
|
GoalsPage::render();
|
|
} else {
|
|
SetupPage::render();
|
|
}
|
|
}
|
|
|
|
/** Overlay rewrite rules (registered by Overlay\Router on init) need a one-off flush. */
|
|
public static function maybe_flush_rewrites(): void {
|
|
if ( current_user_can( self::CAP ) && class_exists( \StreamGoals\Overlay\Router::class ) && \StreamGoals\Overlay\Router::flush_rules_needed() ) {
|
|
flush_rewrite_rules( false );
|
|
\StreamGoals\Overlay\Router::mark_flushed();
|
|
}
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- assets */
|
|
|
|
public static function assets( string $hook ): void {
|
|
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
|
|
if ( 0 !== strpos( $page, 'streamgoals' ) ) {
|
|
return;
|
|
}
|
|
wp_enqueue_style( 'streamgoals-admin', STREAMGOALS_URL . 'assets/admin/admin.css', array(), STREAMGOALS_VERSION );
|
|
wp_enqueue_script( 'streamgoals-admin', STREAMGOALS_URL . 'assets/admin/admin.js', array(), STREAMGOALS_VERSION, true );
|
|
if ( self::HOME === $page ) {
|
|
wp_enqueue_script( 'streamgoals-rules', STREAMGOALS_URL . 'assets/admin/rules.js', array( 'streamgoals-admin' ), STREAMGOALS_VERSION, true );
|
|
}
|
|
if ( ActionsPage::PAGE === $page ) {
|
|
wp_enqueue_script( 'streamgoals-actions', STREAMGOALS_URL . 'assets/admin/actions.js', array( 'streamgoals-admin' ), STREAMGOALS_VERSION, true );
|
|
}
|
|
if ( AlertsPage::PAGE === $page ) {
|
|
wp_enqueue_media();
|
|
wp_enqueue_style( 'streamgoals-alerts', STREAMGOALS_URL . 'assets/admin/alerts.css', array( 'streamgoals-admin' ), STREAMGOALS_VERSION );
|
|
wp_enqueue_script( 'streamgoals-alerts', STREAMGOALS_URL . 'assets/admin/alerts.js', array( 'streamgoals-admin', 'jquery' ), STREAMGOALS_VERSION, true );
|
|
}
|
|
if ( 'streamgoals-overlays' === $page ) {
|
|
wp_enqueue_script( 'streamgoals-overlays', STREAMGOALS_URL . 'assets/admin/overlays.js', array( 'streamgoals-admin' ), STREAMGOALS_VERSION, true );
|
|
}
|
|
}
|
|
|
|
/* --------------------------------------------------------------- helpers */
|
|
|
|
/** Capability + nonce check for every admin-post handler. Dies on failure. */
|
|
public static function guard( string $action ): void {
|
|
if ( ! current_user_can( self::CAP ) ) {
|
|
wp_die( esc_html__( 'You do not have permission to do that.', 'streamgoals-donate' ), 403 );
|
|
}
|
|
check_admin_referer( $action );
|
|
}
|
|
|
|
/** Echo an opening <form> posting to admin-post.php with the nonce for $action. */
|
|
public static function form( string $action, array $hidden = array(), string $class = '' ): void {
|
|
echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '"' . ( '' !== $class ? ' class="' . esc_attr( $class ) . '"' : '' ) . '>';
|
|
echo '<input type="hidden" name="action" value="' . esc_attr( $action ) . '">';
|
|
wp_nonce_field( $action );
|
|
foreach ( $hidden as $k => $v ) {
|
|
echo '<input type="hidden" name="' . esc_attr( (string) $k ) . '" value="' . esc_attr( (string) $v ) . '">';
|
|
}
|
|
}
|
|
|
|
public static function url( string $page = self::HOME, array $args = array() ): string {
|
|
return add_query_arg( array_merge( array( 'page' => $page ), $args ), admin_url( 'admin.php' ) );
|
|
}
|
|
|
|
/** Redirect back with a one-time notice shown on the next screen load. */
|
|
public static function redirect( string $url, string $type, string $message ): void {
|
|
set_transient( 'sg_notice_' . get_current_user_id(), array( 'type' => $type, 'msg' => $message ), 120 );
|
|
wp_safe_redirect( $url );
|
|
exit;
|
|
}
|
|
|
|
public static function notices(): void {
|
|
$key = 'sg_notice_' . get_current_user_id();
|
|
$n = get_transient( $key );
|
|
if ( is_array( $n ) && isset( $n['msg'] ) ) {
|
|
delete_transient( $key );
|
|
$cls = in_array( $n['type'] ?? '', array( 'success', 'error', 'warning', 'info' ), true ) ? $n['type'] : 'info';
|
|
echo '<div class="notice notice-' . esc_attr( $cls ) . ' is-dismissible"><p>' . esc_html( (string) $n['msg'] ) . '</p></div>';
|
|
}
|
|
}
|
|
|
|
public static function currency(): string {
|
|
return Format::currency( get_option( 'sg_currency', 'USD' ) );
|
|
}
|
|
|
|
/** Start a screen: capability check, wrapper and flash notices. */
|
|
public static function open( string $title ): void {
|
|
if ( ! current_user_can( self::CAP ) ) {
|
|
wp_die( esc_html__( 'You do not have permission to view this page.', 'streamgoals-donate' ), 403 );
|
|
}
|
|
echo '<div class="wrap sg-admin"><h1>' . esc_html( $title ) . '</h1>';
|
|
self::notices();
|
|
if ( PayPalConnector::isConnected() && '' === (string) Settings::get( 'paypal_webhook_id', '' ) ) {
|
|
echo '<div class="notice notice-warning"><p>' . esc_html( 'PayPal is connected, but the webhook is not set up yet, so refunds/reversals will not be noticed. Open Settings and press "Save and test connection" to register it.' ) . '</p></div>';
|
|
}
|
|
}
|
|
|
|
public static function close(): void {
|
|
echo '</div>';
|
|
}
|
|
|
|
/** Safe int from $_GET. */
|
|
public static function get_int( string $key ): int {
|
|
return isset( $_GET[ $key ] ) ? absint( wp_unslash( $_GET[ $key ] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification
|
|
}
|
|
|
|
public static function get_str( string $key ): string {
|
|
return isset( $_GET[ $key ] ) && is_scalar( $_GET[ $key ] ) ? sanitize_key( wp_unslash( $_GET[ $key ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
|
|
}
|
|
|
|
/** Safe string from $_POST (already nonce-checked by guard()). */
|
|
public static function post_str( string $key, int $max = 200 ): string {
|
|
return isset( $_POST[ $key ] ) && is_scalar( $_POST[ $key ] ) ? Input::text( wp_unslash( $_POST[ $key ] ), $max ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
|
|
}
|
|
|
|
public static function post_raw(): array {
|
|
return is_array( $_POST ) ? wp_unslash( $_POST ) : array(); // phpcs:ignore WordPress.Security.NonceVerification
|
|
}
|
|
}
|