Tiered goals: ordered milestones with unlocks, tier-aware goal bar, TIER UNLOCKED alerts, next-unlock line
- 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:
1 parent
39840a20d7
commit
4c379a0245
20 files changed
+1089
-14
No files matched your search
@@ -31,7 +31,8 @@ final class OverlayService {
|
||||
$gid = $goal ? (int) $goal['id'] : 0;
|
||||
switch ( $widget ) {
|
||||
case 'goal':
|
||||
return Payloads::goal( $goal, $goal ? $this->source->raisedMinor( $goal ) : 0 );
|
||||
$tiers = ( $gid && $this->source instanceof TiersSource ) ? $this->source->tiersFor( $gid ) : array();
|
||||
return Payloads::goal( $goal, $goal ? $this->source->raisedMinor( $goal ) : 0, $tiers ?: null );
|
||||
case 'timer':
|
||||
return Payloads::timer(
|
||||
$gid ? $this->source->stateValue( $gid, 'timer_ends_at' ) : null,
|
||||
@@ -55,9 +56,11 @@ final class OverlayService {
|
||||
$ids = array_values( array_filter( array_map( static fn( $r ) => (int) ( $r['id'] ?? 0 ), $rows ) ) );
|
||||
$bonuses = ( ( $recent || null !== $since ) && $ids ) ? $this->source->bonusesFor( $ids ) : array();
|
||||
$actions = ( ( $recent || null !== $since ) && $ids && $this->source instanceof ActionsSource ) ? $this->source->actionsFor( $ids ) : array();
|
||||
$tiers = ( ( $recent || null !== $since ) && $ids && $this->source instanceof TiersSource ) ? $this->source->tiersReachedFor( $ids ) : array();
|
||||
foreach ( $rows as $i => $r ) {
|
||||
$rows[ $i ]['bonuses'] = $bonuses[ (int) ( $r['id'] ?? 0 ) ] ?? array();
|
||||
$rows[ $i ]['actions'] = $actions[ (int) ( $r['id'] ?? 0 ) ] ?? array();
|
||||
$rows[ $i ]['tiers'] = $tiers[ (int) ( $r['id'] ?? 0 ) ] ?? array();
|
||||
}
|
||||
$mediaFor = null;
|
||||
$defs = ( $recent || null !== $since ) ? $this->defs() : array();
|
||||
|
||||
@@ -4,6 +4,7 @@ declare( strict_types=1 );
|
||||
namespace StreamGoals\Overlay;
|
||||
|
||||
use StreamGoals\Core\Money;
|
||||
use StreamGoals\Tiers\TierPolicy;
|
||||
|
||||
/**
|
||||
* Pure, WordPress-free payload builders. Everything returned here is public-safe.
|
||||
@@ -31,7 +32,10 @@ final class Payloads {
|
||||
return max( 0, min( 100, intdiv( max( 0, $raised ) * 100, $target ) ) );
|
||||
}
|
||||
|
||||
public static function goal( ?array $goal, int $raised ): array {
|
||||
/**
|
||||
* @param array<int,array<string,mixed>>|null $tiers Raw sg_goal_tiers rows; null/empty = an ordinary (untiered) goal, payload unchanged.
|
||||
*/
|
||||
public static function goal( ?array $goal, int $raised, ?array $tiers = null ): array {
|
||||
if ( null === $goal ) {
|
||||
return array( 'active' => false, 'title' => '', 'raised_minor' => 0, 'target_minor' => 0, 'percent' => 0,
|
||||
'currency' => 'USD', 'raised_display' => '', 'target_display' => '' );
|
||||
@@ -39,6 +43,9 @@ final class Payloads {
|
||||
$cur = strtoupper( (string) ( $goal['currency'] ?? 'USD' ) );
|
||||
$target = max( 0, (int) ( $goal['target_minor'] ?? 0 ) );
|
||||
$raised = max( 0, $raised );
|
||||
if ( $tiers ) {
|
||||
return self::tieredGoal( $goal, $raised, $cur, $tiers );
|
||||
}
|
||||
return array(
|
||||
'active' => true,
|
||||
'title' => (string) ( $goal['title'] ?? '' ),
|
||||
@@ -51,6 +58,40 @@ final class Payloads {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tiered goal: raised stays cumulative; target/percent describe the CURRENT tier (percent is within the tier).
|
||||
* When every tier is reached the bar is full at the top tier's target.
|
||||
*/
|
||||
private static function tieredGoal( array $goal, int $raised, string $cur, array $tiers ): array {
|
||||
$sorted = TierPolicy::sorted( $tiers );
|
||||
$current = TierPolicy::currentTier( $sorted );
|
||||
$all = null === $current;
|
||||
$target = $all ? TierPolicy::derivedGoalTarget( $sorted ) : max( 0, (int) $current['target_minor'] );
|
||||
$floor = TierPolicy::floorMinor( $sorted );
|
||||
return array(
|
||||
'active' => true,
|
||||
'title' => (string) ( $goal['title'] ?? '' ),
|
||||
'raised_minor' => $raised,
|
||||
'target_minor' => $target,
|
||||
'percent' => TierPolicy::percentFor( $sorted, $raised ),
|
||||
'currency' => $cur,
|
||||
'raised_display' => self::money( $raised, $cur ),
|
||||
'target_display' => self::money( $target, $cur ),
|
||||
'tiers' => array(
|
||||
'count' => count( $sorted ),
|
||||
'index' => TierPolicy::currentIndex( $sorted ),
|
||||
'current' => $all ? null : array(
|
||||
'title' => self::text( $current['title'] ?? '', 120 ),
|
||||
'description' => self::text( $current['description'] ?? '', 255 ),
|
||||
'target_minor' => $target,
|
||||
'target_display' => self::money( $target, $cur ),
|
||||
'floor_minor' => $floor,
|
||||
),
|
||||
'all_reached' => $all,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $endsAt sg_state `timer_ends_at` (epoch seconds, or a UTC datetime string).
|
||||
* @param string|null $pausedRemaining sg_state `paused_remaining` seconds; non-null means paused.
|
||||
@@ -132,6 +173,7 @@ final class Payloads {
|
||||
'message' => (string) ( $r['message'] ?? '' ),
|
||||
'bonuses' => self::bonuses( $r['bonuses'] ?? array() ),
|
||||
'actions' => self::actionEvents( $r['actions'] ?? array() ),
|
||||
'tiers' => self::tierEvents( $r['tiers'] ?? array() ),
|
||||
'media' => $mediaFor ? $mediaFor( $r, $pub, $disp ) : null,
|
||||
);
|
||||
}
|
||||
@@ -200,6 +242,27 @@ final class Payloads {
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tiers a donation unlocked, for the "TIER UNLOCKED" pill: [ {title} ]. At most 5, titles capped at 80 chars, blanks dropped.
|
||||
*
|
||||
* @param mixed $events
|
||||
* @return array<int,array{title:string}>
|
||||
*/
|
||||
public static function tierEvents( mixed $events ): array {
|
||||
$out = array();
|
||||
foreach ( is_array( $events ) ? $events : array() as $e ) {
|
||||
$title = self::text( is_array( $e ) ? ( $e['title'] ?? '' ) : '', 80 );
|
||||
if ( '' === $title ) {
|
||||
continue;
|
||||
}
|
||||
$out[] = array( 'title' => $title );
|
||||
if ( count( $out ) >= 5 ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The on-stream reward menu for the active goal. Empty list means "render nothing".
|
||||
*
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Overlay;
|
||||
|
||||
/** Optional capability: goal tiers (milestones). Sources without it behave as if no goal had tiers. */
|
||||
interface TiersSource {
|
||||
|
||||
/**
|
||||
* Tiers of a goal ordered by target ascending. Rows with keys id, target_minor, title, description, reached_at.
|
||||
*
|
||||
* @return array<int,array<string,mixed>>
|
||||
*/
|
||||
public function tiersFor( int $goalId ): array;
|
||||
|
||||
/**
|
||||
* Tiers unlocked by each donation, ONE lookup for the whole page: donation id => [ {title} ].
|
||||
*
|
||||
* @param int[] $donationIds
|
||||
* @return array<int,array<int,array<string,mixed>>>
|
||||
*/
|
||||
public function tiersReachedFor( array $donationIds ): array;
|
||||
}
|
||||
@@ -4,7 +4,7 @@ declare( strict_types=1 );
|
||||
namespace StreamGoals\Overlay;
|
||||
|
||||
/** $wpdb adapter reading sg_goals, sg_donations, sg_state, sg_rules directly. */
|
||||
final class WpdbDataSource implements OverlayDataSource, RecentDonationsSource, ActionsSource, AlertMediaSource {
|
||||
final class WpdbDataSource implements OverlayDataSource, RecentDonationsSource, ActionsSource, AlertMediaSource, TiersSource {
|
||||
|
||||
/** @var array<int,?array{url:string,mime:string}> */
|
||||
private array $attachments = array();
|
||||
@@ -120,6 +120,14 @@ final class WpdbDataSource implements OverlayDataSource, RecentDonationsSource,
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function tiersFor( int $goalId ): array {
|
||||
return ( new \StreamGoals\Tiers\TierRepository( $GLOBALS['wpdb'] ) )->forGoal( $goalId );
|
||||
}
|
||||
|
||||
public function tiersReachedFor( array $donationIds ): array {
|
||||
return ( new \StreamGoals\Tiers\TierRepository( $GLOBALS['wpdb'] ) )->reachedByDonation( $donationIds );
|
||||
}
|
||||
|
||||
public function alertDefs(): array {
|
||||
return ( new \StreamGoals\Alerts\DefRepository( $GLOBALS['wpdb'] ) )->enabled();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user