Tiered goals: ordered milestones with unlocks, tier-aware goal bar, TIER UNLOCKED alerts, next-unlock line
Test and Release / test (push) Successful in 1m21s
Test and Release / release (push) Successful in 4s

- 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>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 13:09:18 -08:00
1 parent 39840a20d7
commit 4c379a0245
20 files changed
+1089 -14

No files matched your search

+2
View File
@@ -25,6 +25,8 @@ final class Admin {
'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' ),
+139 -2
View File
@@ -4,6 +4,9 @@ declare( strict_types=1 );
namespace StreamGoals\Admin;
use StreamGoals\Core\Money;
use StreamGoals\Core\Settings;
use StreamGoals\Tiers\TierPolicy;
use StreamGoals\Tiers\TierRepository;
/** Goals list/create/edit/status/delete, plus the per-goal rules editor. */
final class GoalsPage {
@@ -17,6 +20,8 @@ final class GoalsPage {
self::renderForm( 'edit' === $view ? $id : 0 );
} elseif ( 'rules' === $view && $id ) {
self::renderRules( $id );
} elseif ( 'tiers' === $view && $id ) {
self::renderTiers( $id );
} else {
self::renderList();
}
@@ -29,6 +34,7 @@ final class GoalsPage {
echo '<p><a class="page-title-action" href="' . esc_url( Admin::url( Admin::HOME, array( 'view' => 'new' ) ) ) . '">Add new goal</a></p>';
echo '<p class="description">Overlays and the donation form use your newest <strong>active</strong> goal(s). Pause a goal to stop accepting donations; mark it complete when you are done. A goal that has donations cannot be deleted (complete it instead), so your records stay intact.</p>';
$goals = Store::goals()->all();
$tierCounts = $goals ? ( new TierRepository() )->countsByGoal() : array();
if ( ! $goals ) {
echo '<div class="sg-card"><p>No goals yet. <a href="' . esc_url( Admin::url( Admin::HOME, array( 'view' => 'new' ) ) ) . '">Create your first goal</a> or run the <a href="' . esc_url( SetupPage::url( 2 ) ) . '">setup guide</a>.</p></div>';
Admin::close();
@@ -47,6 +53,8 @@ final class GoalsPage {
echo '<td>' . esc_html( self::dateLabel( $g['starts_at'] ?? null, 'Starts' ) ) . '<br>' . esc_html( self::dateLabel( $g['ends_at'] ?? null, 'Ends' ) ) . '</td><td class="sg-actions">';
echo '<a class="button" href="' . esc_url( Admin::url( Admin::HOME, array( 'view' => 'edit', 'goal' => $gid ) ) ) . '">Edit</a> ';
echo '<a class="button" href="' . esc_url( Admin::url( Admin::HOME, array( 'view' => 'rules', 'goal' => $gid ) ) ) . '">Rules</a> ';
$nt = $tierCounts[ $gid ] ?? 0;
echo '<a class="button" href="' . esc_url( Admin::url( Admin::HOME, array( 'view' => 'tiers', 'goal' => $gid ) ) ) . '">Tiers' . ( $nt > 0 ? ' (' . (int) $nt . ')' : '' ) . '</a> ';
foreach ( array( 'active' => array( 'paused', 'Pause' ), 'paused' => array( 'active', 'Resume' ), 'complete' => array( 'active', 'Reopen' ) ) as $from => [ $to, $lbl ] ) {
if ( $g['status'] === $from ) {
Admin::form( 'sg_goal_status', array( 'goal_id' => $gid, 'status' => $to ), 'sg-inline' );
@@ -80,11 +88,16 @@ final class GoalsPage {
return;
}
$cur = $g ? Format::currency( $g['currency'] ) : Admin::currency();
$tiered = $id && (bool) ( new TierRepository() )->forGoal( $id );
Admin::open( $g ? 'Edit goal' : 'New goal' );
Admin::form( 'sg_goal_save', array( 'goal_id' => $id ) );
echo '<table class="form-table" role="presentation">';
echo '<tr><th scope="row"><label for="sg_title">Title</label></th><td><input type="text" required maxlength="190" id="sg_title" name="title" class="regular-text" value="' . esc_attr( (string) ( $g['title'] ?? '' ) ) . '"></td></tr>';
echo '<tr><th scope="row"><label for="sg_target">Target (' . esc_html( $cur ) . ')</label></th><td><input type="text" required inputmode="decimal" id="sg_target" name="target" class="small-text" value="' . esc_attr( $g ? Format::decimal( (int) $g['target_minor'], $cur ) : '' ) . '"></td></tr>';
if ( $tiered ) {
echo '<tr><th scope="row">Target (' . esc_html( $cur ) . ')</th><td><strong>' . esc_html( Format::money( (int) $g['target_minor'], $cur ) ) . '</strong> <span class="description">Set automatically from your highest tier. To change it, <a href="' . esc_url( Admin::url( Admin::HOME, array( 'view' => 'tiers', 'goal' => $id ) ) ) . '">edit the tiers</a>.</span></td></tr>';
} else {
echo '<tr><th scope="row"><label for="sg_target">Target (' . esc_html( $cur ) . ')</label></th><td><input type="text" required inputmode="decimal" id="sg_target" name="target" class="small-text" value="' . esc_attr( $g ? Format::decimal( (int) $g['target_minor'], $cur ) : '' ) . '"></td></tr>';
}
echo '<tr><th scope="row">Currency</th><td><strong>' . esc_html( $cur ) . '</strong> <span class="description">One currency is used across the whole site. Change it in <a href="' . esc_url( Admin::url( 'streamgoals-settings' ) ) . '">Settings</a> (applies to new goals only).</span></td></tr>';
$fmt = static fn( ?string $utc ): string => $utc ? get_date_from_gmt( $utc, 'Y-m-d\TH:i' ) : '';
echo '<tr><th scope="row"><label for="sg_start">Starts</label></th><td><input type="datetime-local" id="sg_start" name="starts_at" value="' . esc_attr( $fmt( $g['starts_at'] ?? null ) ) . '"> <span class="description">Optional. Your site time zone. Empty = starts immediately.</span></td></tr>';
@@ -116,10 +129,12 @@ final class GoalsPage {
$cur = $old ? Format::currency( $old['currency'] ) : Admin::currency();
$title = Input::text( $post['title'] ?? '', 190 );
$tgt = Input::amountToMinor( $post['target'] ?? '', $cur );
// A goal with tiers has a derived target (its highest tier): never taken from the form.
$tiered = $id && ( new TierRepository() )->forGoal( $id );
if ( '' === $title ) {
Admin::redirect( $back, 'error', 'Please give the goal a title.' );
}
if ( null === $tgt || $tgt <= 0 ) {
if ( ! $tiered && ( null === $tgt || $tgt <= 0 ) ) {
Admin::redirect( $back, 'error', 'Please enter a target greater than zero, for example 250.00.' );
}
$startRaw = Input::text( $post['starts_at'] ?? '', 20 );
@@ -133,6 +148,9 @@ final class GoalsPage {
Admin::redirect( $back, 'error', 'The end must be after the start.' );
}
$data = array( 'title' => $title, 'target_minor' => $tgt, 'starts_at' => $start, 'ends_at' => $end, 'counts_fees' => empty( $post['counts_fees'] ) ? 0 : 1, 'close_at_target' => \StreamGoals\Core\GoalTargetPolicy::flagFromPost( $post['close_at_target'] ?? null ) );
if ( $tiered ) {
unset( $data['target_minor'] );
}
if ( $id ) {
Store::goals()->update( $id, $data );
} else {
@@ -171,6 +189,125 @@ final class GoalsPage {
Admin::redirect( Admin::url(), 'success', 'Goal deleted.' );
}
/* ----------------------------------------------------------------- tiers */
private static function tiersUrl( int $goalId, array $extra = array() ): string {
return Admin::url( Admin::HOME, array_merge( array( 'view' => 'tiers', 'goal' => $goalId ), $extra ) );
}
private static function renderTiers( int $goalId ): void {
$g = Store::goals()->find( $goalId );
if ( ! $g ) {
Admin::open( 'Goal not found' );
echo '<p><a href="' . esc_url( Admin::url() ) . '">Back to goals</a></p>';
Admin::close();
return;
}
$cur = Format::currency( $g['currency'] );
$repo = new TierRepository();
$tiers = $repo->forGoal( $goalId );
$raised = Store::goals()->raised_minor( $goalId );
Admin::open( 'Tiers for "' . (string) $g['title'] . '"' );
echo '<p><a href="' . esc_url( Admin::url() ) . '">&larr; Back to goals</a></p>';
echo '<p class="sg-lead">Tiers turn one goal into a ladder of milestones. Each tier has a target and something you unlock or give away. When your total reaches a tier\'s target, that tier unlocks and the goal bar moves on to the next one. The goal\'s final target is your highest tier.</p>';
echo '<div class="sg-card"><h2>Your tiers</h2>';
echo '<p class="description">Raised so far: <strong>' . esc_html( Format::money( $raised, $cur ) ) . '</strong>. Targets are cumulative totals, not extra amounts: a tier at ' . esc_html( Format::money( 25000, $cur ) ) . ' unlocks when the goal total reaches ' . esc_html( Format::money( 25000, $cur ) ) . '.</p>';
if ( ! $tiers ) {
echo '<p>No tiers yet. This goal works as a single target until you add one.</p>';
} else {
echo '<table class="widefat striped sg-table"><thead><tr><th>#</th><th>Unlock</th><th>Target</th><th>Status</th><th>Actions</th></tr></thead><tbody>';
foreach ( TierPolicy::sorted( $tiers ) as $i => $t ) {
$tid = (int) $t['id'];
$status = TierPolicy::status( $tiers, $t );
$label = 'reached' === $status ? 'Reached on ' . get_date_from_gmt( (string) $t['reached_at'], 'Y-m-d H:i' ) : ( 'current' === $status ? 'Current' : 'Upcoming' );
echo '<tr><td>' . (int) ( $i + 1 ) . '</td><td><strong>' . esc_html( $t['title'] ) . '</strong>';
if ( '' !== $t['description'] ) {
echo '<br><span class="description">' . esc_html( $t['description'] ) . '</span>';
}
echo '</td><td>' . esc_html( Format::money( (int) $t['target_minor'], $cur ) ) . '</td>';
echo '<td><span class="sg-badge sg-' . ( 'reached' === $status ? 'complete' : ( 'current' === $status ? 'active' : 'paused' ) ) . '">' . esc_html( $label ) . '</span></td><td class="sg-actions">';
echo '<a class="button" href="' . esc_url( self::tiersUrl( $goalId, array( 'tier' => $tid ) ) ) . '#sg-tier-form">Edit</a> ';
Admin::form( 'sg_tier_delete', array( 'goal_id' => $goalId, 'tier_id' => $tid ), 'sg-inline sg-confirm' );
echo '<button class="button button-link-delete" data-confirm="Delete this tier?">Delete</button></form></td></tr>';
}
echo '</tbody></table>';
}
echo '</div>';
$editId = Admin::get_int( 'tier' );
$edit = $editId ? $repo->find( $editId ) : null;
if ( $edit && (int) $edit['goal_id'] !== $goalId ) {
$edit = null;
}
$locked = $edit && TierPolicy::isReached( $edit );
$min = Settings::effectiveMin( (int) Settings::get( 'min_donation_minor', 0 ) );
echo '<div class="sg-card" id="sg-tier-form"><h2>' . ( $edit ? 'Edit tier' : 'Add a tier' ) . '</h2>';
Admin::form( 'sg_tier_save', array( 'goal_id' => $goalId, 'tier_id' => $edit ? (int) $edit['id'] : 0 ) );
echo '<table class="form-table" role="presentation">';
echo '<tr><th scope="row"><label for="sg_t_target">Target (' . esc_html( $cur ) . ')</label></th><td><input type="text" ' . ( $locked ? 'disabled' : 'required' ) . ' inputmode="decimal" id="sg_t_target" name="target" class="small-text" value="' . esc_attr( $edit ? Format::decimal( (int) $edit['target_minor'], $cur ) : '' ) . '">';
echo '<p class="description">' . ( $locked ? 'This tier has been reached, so its target is locked. You can still change its name and description.' : 'When your total reaches this amount, this tier unlocks and the goal bar moves on to the next one. Must be above what has been raised so far (' . esc_html( Format::money( $raised, $cur ) ) . ') and at least ' . esc_html( Format::money( $min, $cur ) ) . '.' ) . '</p></td></tr>';
echo '<tr><th scope="row"><label for="sg_t_title">Item or unlock</label></th><td><input type="text" required maxlength="120" id="sg_t_title" name="title" class="regular-text" value="' . esc_attr( $edit ? (string) $edit['title'] : '' ) . '"><p class="description">What viewers get when this tier is reached, for example "New microphone". Up to 120 characters. Shown on your overlay and the donation form.</p></td></tr>';
echo '<tr><th scope="row"><label for="sg_t_desc">Description</label></th><td><input type="text" maxlength="255" id="sg_t_desc" name="description" class="large-text" value="' . esc_attr( $edit ? (string) $edit['description'] : '' ) . '"><p class="description">Optional. A short note, up to 255 characters.</p></td></tr>';
echo '</table><p><button class="button button-primary">' . ( $edit ? 'Save tier' : 'Add tier' ) . '</button>';
if ( $edit ) {
echo ' <a class="button" href="' . esc_url( self::tiersUrl( $goalId ) ) . '">Cancel</a>';
}
echo '</p></form></div>';
Admin::close();
}
public static function handleTierSave(): void {
Admin::guard( 'sg_tier_save' );
$post = Admin::post_raw();
$goalId = isset( $post['goal_id'] ) ? absint( $post['goal_id'] ) : 0;
$tierId = isset( $post['tier_id'] ) ? absint( $post['tier_id'] ) : 0;
$g = $goalId ? Store::goals()->find( $goalId ) : null;
if ( ! $g ) {
Admin::redirect( Admin::url(), 'error', 'That goal no longer exists.' );
}
$back = self::tiersUrl( $goalId, $tierId ? array( 'tier' => $tierId ) : array() );
$cur = Format::currency( $g['currency'] );
$repo = new TierRepository();
$min = Settings::effectiveMin( (int) Settings::get( 'min_donation_minor', 0 ) );
$res = TierPolicy::validate(
$repo->forGoal( $goalId ),
$tierId ?: null,
Input::amountToMinor( $post['target'] ?? '', $cur ),
Input::text( $post['title'] ?? '', 200 ),
Input::text( $post['description'] ?? '', 400 ),
Store::goals()->raised_minor( $goalId ),
$min,
Format::money( $min, $cur )
);
if ( null === $res['data'] ) {
Admin::redirect( $back, 'error', (string) $res['error'] );
}
if ( $tierId ) {
$repo->update( $tierId, $goalId, $res['data'] );
} elseif ( $repo->create( $goalId, $res['data'] ) <= 0 ) {
Admin::redirect( $back, 'error', 'The tier could not be saved. Another tier may already have that target.' );
}
$repo->syncGoalTarget( $goalId );
$msg = 'Tier saved. The goal target is now ' . Format::money( TierPolicy::derivedGoalTarget( $repo->forGoal( $goalId ) ), $cur ) . '.';
if ( 'complete' === ( $g['status'] ?? '' ) ) {
$msg .= ' This goal is marked complete; use "Reopen" on the goals list if you want donations to continue.';
}
Admin::redirect( self::tiersUrl( $goalId ), 'success', $msg );
}
public static function handleTierDelete(): void {
Admin::guard( 'sg_tier_delete' );
$post = Admin::post_raw();
$goalId = isset( $post['goal_id'] ) ? absint( $post['goal_id'] ) : 0;
if ( $goalId && Store::goals()->find( $goalId ) ) {
$repo = new TierRepository();
$repo->delete( isset( $post['tier_id'] ) ? absint( $post['tier_id'] ) : 0, $goalId );
$repo->syncGoalTarget( $goalId ); // no-op when the last tier was removed: the goal keeps its current target (editable again)
}
Admin::redirect( self::tiersUrl( $goalId ), 'success', 'Tier deleted.' );
}
/* ----------------------------------------------------------------- rules */
private static function renderRules( int $goalId ): void {
+1
View File
@@ -87,6 +87,7 @@ final class Store {
self::deleteRule( (int) $r['id'], $goalId );
}
$wpdb->delete( self::t( 'state' ), array( 'goal_id' => $goalId ), array( '%d' ) );
$wpdb->delete( self::t( 'goal_tiers' ), array( 'goal_id' => $goalId ), array( '%d' ) );
return self::goals()->delete( $goalId );
}