2026-11-15 10:26:03 -08:00
<? php
declare ( strict_types = 1 );
namespace StreamGoals\Admin ;
use StreamGoals\Core\Money ;
/** Goals list/create/edit/status/delete, plus the per-goal rules editor. */
final class GoalsPage {
private const STATUSES = array ( 'active' => 'Active' , 'paused' => 'Paused' , 'complete' => 'Complete' );
public static function render () : void {
$view = Admin :: get_str ( 'view' );
$id = Admin :: get_int ( 'goal' );
if ( in_array ( $view , array ( 'new' , 'edit' ), true ) ) {
self :: renderForm ( 'edit' === $view ? $id : 0 );
} elseif ( 'rules' === $view && $id ) {
self :: renderRules ( $id );
} else {
self :: renderList ();
}
}
/* ------------------------------------------------------------------ list */
private static function renderList () : void {
Admin :: open ( 'Goals' );
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 ();
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 ();
return ;
}
echo '<table class="widefat striped sg-table"><thead><tr><th>Goal</th><th>Status</th><th>Raised</th><th>Dates</th><th>Actions</th></tr></thead><tbody>' ;
foreach ( $goals as $g ) {
$gid = ( int ) $g [ 'id' ];
$cur = Format :: currency ( $g [ 'currency' ] );
$raised = Store :: goals () -> raised_minor ( $gid );
$target = ( int ) $g [ 'target_minor' ];
$pct = $target > 0 ? min ( 100 , intdiv ( $raised * 100 , $target ) ) : 0 ;
echo '<tr><td><strong>' . esc_html ( ( string ) $g [ 'title' ] ) . '</strong><br><span class="description">ID ' . $gid . ' · <code>[streamgoals_donate goal="' . $gid . '"]</code></span></td>' ;
echo '<td><span class="sg-badge sg-' . esc_attr ( ( string ) $g [ 'status' ] ) . '">' . esc_html ( self :: STATUSES [ $g [ 'status' ] ] ?? ( string ) $g [ 'status' ] ) . '</span></td>' ;
echo '<td>' . esc_html ( Format :: money ( $raised , $cur ) . ' of ' . Format :: money ( $target , $cur ) ) . ' (' . ( int ) $pct . '%)<div class="sg-bar"><span style="width:' . ( int ) $pct . '%"></span></div></td>' ;
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> ' ;
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' );
echo '<button class="button">' . esc_html ( $lbl ) . '</button></form> ' ;
}
}
if ( 'complete' !== $g [ 'status' ] ) {
Admin :: form ( 'sg_goal_status' , array ( 'goal_id' => $gid , 'status' => 'complete' ), 'sg-inline' );
echo '<button class="button">Mark complete</button></form> ' ;
}
Admin :: form ( 'sg_goal_delete' , array ( 'goal_id' => $gid ), 'sg-inline sg-confirm' );
echo '<button class="button button-link-delete" data-confirm="Delete this goal and its rules? This cannot be undone.">Delete</button></form>' ;
echo '</td></tr>' ;
}
echo '</tbody></table>' ;
Admin :: close ();
}
private static function dateLabel ( ? string $utc , string $prefix ) : string {
return $utc ? $prefix . ': ' . get_date_from_gmt ( $utc , 'Y-m-d H:i' ) : $prefix . ': no date' ;
}
/* ------------------------------------------------------------------ form */
private static function renderForm ( int $id ) : void {
$g = $id ? Store :: goals () -> find ( $id ) : null ;
if ( $id && ! $g ) {
Admin :: open ( 'Goal not found' );
echo '<p><a href="' . esc_url ( Admin :: url () ) . '">Back to goals</a></p>' ;
Admin :: close ();
return ;
}
$cur = $g ? Format :: currency ( $g [ 'currency' ] ) : Admin :: currency ();
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>' ;
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>' ;
echo '<tr><th scope="row"><label for="sg_end">Ends</label></th><td><input type="datetime-local" id="sg_end" name="ends_at" value="' . esc_attr ( $fmt ( $g [ 'ends_at' ] ?? null ) ) . '"> <span class="description">Optional. Donations stop after this time.</span></td></tr>' ;
echo '<tr><th scope="row">Fees</th><td><label><input type="checkbox" name="counts_fees" value="1"' . checked ( ! empty ( $g [ 'counts_fees' ] ), true , false ) . '> Count fees covered by donors towards the goal</label><p class="description">Off (recommended): only the amount the donor chose counts, so a $10 donation always adds $10 even if they also covered the fee.</p></td></tr>' ;
echo '</table><p><button class="button button-primary">' . ( $g ? 'Save goal' : 'Create goal' ) . '</button> <a class="button" href="' . esc_url ( Admin :: url () ) . '">Cancel</a></p></form>' ;
Admin :: close ();
}
/** 'Y-m-d\TH:i' in the site time zone -> UTC 'Y-m-d H:i:s', or null when empty/invalid. */
private static function toUtc ( string $local ) : ? string {
if ( ! preg_match ( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/' , $local ) ) {
return null ;
}
$utc = get_gmt_from_date ( str_replace ( 'T' , ' ' , $local ) . ':00' );
return $utc ?: null ;
}
public static function handleSave () : void {
Admin :: guard ( 'sg_goal_save' );
$post = Admin :: post_raw ();
$id = isset ( $post [ 'goal_id' ] ) ? absint ( $post [ 'goal_id' ] ) : 0 ;
$back = Admin :: url ( Admin :: HOME , $id ? array ( 'view' => 'edit' , 'goal' => $id ) : array ( 'view' => 'new' ) );
$old = $id ? Store :: goals () -> find ( $id ) : null ;
if ( $id && ! $old ) {
Admin :: redirect ( Admin :: url (), 'error' , 'That goal no longer exists.' );
}
$cur = $old ? Format :: currency ( $old [ 'currency' ] ) : Admin :: currency ();
$title = Input :: text ( $post [ 'title' ] ?? '' , 190 );
$tgt = Input :: amountToMinor ( $post [ 'target' ] ?? '' , $cur );
if ( '' === $title ) {
Admin :: redirect ( $back , 'error' , 'Please give the goal a title.' );
}
if ( 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 );
$endRaw = Input :: text ( $post [ 'ends_at' ] ?? '' , 20 );
$start = '' === $startRaw ? null : self :: toUtc ( $startRaw );
$end = '' === $endRaw ? null : self :: toUtc ( $endRaw );
if ( ( '' !== $startRaw && null === $start ) || ( '' !== $endRaw && null === $end ) ) {
Admin :: redirect ( $back , 'error' , 'That date is not valid.' );
}
if ( $start && $end && strtotime ( $end . ' UTC' ) <= strtotime ( $start . ' UTC' ) ) {
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 );
if ( $id ) {
Store :: goals () -> update ( $id , $data );
} else {
$data [ 'currency' ] = $cur ;
$data [ 'status' ] = 'active' ;
$id = Store :: goals () -> create ( $data );
if ( $id <= 0 ) {
Admin :: redirect ( $back , 'error' , 'The goal could not be saved.' );
}
Admin :: redirect ( Admin :: url ( Admin :: HOME , array ( 'view' => 'rules' , 'goal' => $id ) ), 'success' , 'Goal created. Add rules below if you want a timer or counter.' );
}
Admin :: redirect ( Admin :: url (), 'success' , 'Goal saved.' );
}
public static function handleStatus () : void {
Admin :: guard ( 'sg_goal_status' );
$post = Admin :: post_raw ();
$id = isset ( $post [ 'goal_id' ] ) ? absint ( $post [ 'goal_id' ] ) : 0 ;
$status = isset ( $post [ 'status' ] ) && is_string ( $post [ 'status' ] ) ? $post [ 'status' ] : '' ;
if ( ! $id || ! isset ( self :: STATUSES [ $status ] ) || ! Store :: goals () -> find ( $id ) ) {
Admin :: redirect ( Admin :: url (), 'error' , 'Could not change that goal.' );
}
Store :: goals () -> update ( $id , array ( 'status' => $status ) );
Admin :: redirect ( Admin :: url (), 'success' , 'Goal is now ' . strtolower ( self :: STATUSES [ $status ] ) . '.' );
}
public static function handleDelete () : void {
Admin :: guard ( 'sg_goal_delete' );
$id = isset ( $_POST [ 'goal_id' ] ) ? absint ( wp_unslash ( $_POST [ 'goal_id' ] ) ) : 0 ; // phpcs:ignore WordPress.Security.NonceVerification
if ( ! $id || ! Store :: goals () -> find ( $id ) ) {
Admin :: redirect ( Admin :: url (), 'error' , 'That goal no longer exists.' );
}
if ( ! Store :: deleteGoal ( $id ) ) {
Admin :: redirect ( Admin :: url (), 'error' , 'This goal has donations, so it cannot be deleted. Mark it complete instead.' );
}
Admin :: redirect ( Admin :: url (), 'success' , 'Goal deleted.' );
}
/* ----------------------------------------------------------------- rules */
private static function renderRules ( 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' ] );
$rules = Store :: rules ( $goalId );
Admin :: open ( 'Rules for "' . ( string ) $g [ 'title' ] . '"' );
echo '<p><a href="' . esc_url ( Admin :: url () ) . '">← Back to goals</a></p>' ;
echo '<p class="sg-lead">Rules decide what each donation does besides filling the progress bar: add time to a subathon timer, bump a counter, or fire a bonus alert.</p>' ;
$editId = Admin :: get_int ( 'rule' );
$editRule = $editId ? Store :: rule ( $editId , $goalId ) : null ;
echo '<div class="sg-card"><h2>Current rules</h2>' ;
if ( ! $rules ) {
echo '<p>No rules yet. The goal bar works without any rules.</p>' ;
} else {
echo '<table class="widefat striped sg-table"><thead><tr><th>Rule</th><th>What it does</th><th>Actions</th></tr></thead><tbody>' ;
foreach ( $rules as $r ) {
$rid = ( int ) $r [ 'id' ];
echo '<tr class="' . ( $r [ 'enabled' ] ? '' : 'sg-off' ) . '"><td>' . esc_html ( RuleConfig :: TYPES [ $r [ 'type' ] ] ?? ( string ) $r [ 'type' ] ) . ( $r [ 'enabled' ] ? '' : ' <em>(off)</em>' ) . '</td>' ;
2026-11-15 11:47:13 -08:00
try {
$summary = RuleConfig :: summary ( ( string ) $r [ 'type' ], $r [ 'config' ], $cur );
} catch ( \Throwable $e ) {
// One unreadable rule must not take down the whole screen; say what is wrong so it can be fixed or deleted.
$summary = 'Could not read this rule (' . $e -> getMessage () . '). You can delete it and add it again.' ;
}
echo '<td>' . esc_html ( $summary ) . '</td><td class="sg-actions">' ;
2026-11-15 10:26:03 -08:00
echo '<a class="button" href="' . esc_url ( Admin :: url ( Admin :: HOME , array ( 'view' => 'rules' , 'goal' => $goalId , 'rule' => $rid ) ) ) . '#sg-rule-form">Edit</a> ' ;
Admin :: form ( 'sg_rule_toggle' , array ( 'goal_id' => $goalId , 'rule_id' => $rid , 'enabled' => $r [ 'enabled' ] ? 0 : 1 ), 'sg-inline' );
echo '<button class="button">' . ( $r [ 'enabled' ] ? 'Turn off' : 'Turn on' ) . '</button></form> ' ;
Admin :: form ( 'sg_rule_delete' , array ( 'goal_id' => $goalId , 'rule_id' => $rid ), 'sg-inline sg-confirm' );
echo '<button class="button button-link-delete" data-confirm="Delete this rule?">Delete</button></form></td></tr>' ;
}
echo '</tbody></table>' ;
}
echo '</div>' ;
// Live preview of saved, enabled rules.
$data = array (
'currency' => $cur ,
'exp' => Money :: exponent ( $cur ),
'rules' => array_values ( array_map ( static fn ( $r ) => array ( 'type' => $r [ 'type' ], 'config' => $r [ 'config' ] ), array_filter ( $rules , static fn ( $r ) => ( bool ) $r [ 'enabled' ] ) ) ),
);
echo '<div class="sg-card"><h2>What would a donation do?</h2><label>Donation amount (' . esc_html ( $cur ) . ') <input type="text" id="sg-preview-amount" class="small-text" inputmode="decimal" value="' . esc_attr ( 0 === $data [ 'exp' ] ? '2500' : '25.00' ) . '"></label>' ;
echo '<ul id="sg-preview-out" class="sg-preview-list" aria-live="polite"></ul>' ;
echo '<p class="description">Based on your enabled rules, for a single donation on its own. Carry-over from earlier donations and total limits are not included.</p>' ;
echo '<script type="application/json" id="sg-rules-data">' . wp_json_encode ( $data , JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . '</script></div>' ;
self :: renderRuleForm ( $goalId , $cur , $editRule );
Admin :: close ();
}
private static function renderRuleForm ( int $goalId , string $cur , ? array $edit ) : void {
$type = $edit ? ( string ) $edit [ 'type' ] : 'add_time' ;
$vals = $edit ? RuleConfig :: toForm ( $type , $edit [ 'config' ], $cur ) : array ();
$v = static fn ( string $t , string $k , string $d = '' ) : string => esc_attr ( ( string ) ( $type === $t && isset ( $vals [ $k ] ) ? $vals [ $k ] : $d ) );
$basisOpts = static function ( string $t ) use ( $type , $vals ) : string {
$cur = $type === $t ? ( $vals [ 'basis' ] ?? 'base' ) : 'base' ;
return '<select name="rule[' . $t . '][basis]"><option value="base"' . selected ( $cur , 'base' , false ) . '>The amount the donor chose (recommended)</option><option value="total"' . selected ( $cur , 'total' , false ) . '>The amount including covered fees</option></select>' ;
};
echo '<div class="sg-card" id="sg-rule-form"><h2>' . ( $edit ? 'Edit rule' : 'Add a rule' ) . '</h2>' ;
Admin :: form ( 'sg_rule_save' , array ( 'goal_id' => $goalId , 'rule_id' => $edit ? ( int ) $edit [ 'id' ] : 0 ), 'sg-rule-form' );
if ( $edit ) {
echo '<input type="hidden" name="type" value="' . esc_attr ( $type ) . '"><p><strong>' . esc_html ( RuleConfig :: TYPES [ $type ] ?? $type ) . '</strong></p>' ;
} else {
echo '<p><label>Type <select name="type" id="sg-rule-type">' ;
foreach ( RuleConfig :: TYPES as $k => $l ) {
echo '<option value="' . esc_attr ( $k ) . '">' . esc_html ( $l ) . '</option>' ;
}
echo '</select></label></p>' ;
}
// add_time
echo '<div class="sg-rule-group" data-type="add_time"' . ( 'add_time' === $type ? '' : ' hidden' ) . '><p>Add <input type="number" min="1" name="rule[add_time][time_amount]" class="small-text" data-f="time_amount" value="' . $v ( 'add_time' , 'time_amount' , '1' ) . '">' ;
echo ' <select name="rule[add_time][time_unit]" data-f="time_unit">' ;
foreach ( array ( 'seconds' , 'minutes' , 'hours' ) as $u ) {
echo '<option value="' . esc_attr ( $u ) . '"' . selected ( 'add_time' === $type ? ( $vals [ 'time_unit' ] ?? 'minutes' ) : 'minutes' , $u , false ) . '>' . esc_html ( $u ) . '</option>' ;
}
echo '</select> to the timer for every ' . esc_html ( $cur ) . ' <input type="text" inputmode="decimal" name="rule[add_time][unit_amount]" class="small-text" data-f="unit_amount" value="' . $v ( 'add_time' , 'unit_amount' , 0 === Money :: exponent ( $cur ) ? '100' : '1.00' ) . '"> donated.</p>' ;
2026-11-15 11:40:06 -08:00
echo '<details' . ( ( 'add_time' === $type && ( ( $vals [ 'cap_minutes' ] ?? '' ) !== '' || ( $vals [ 'max_total_minutes' ] ?? '' ) !== '' || ( $vals [ 'max_timer_minutes' ] ?? '' ) !== '' ) ) ? ' open' : '' ) . '><summary>Limits (optional)</summary>' ;
2026-11-15 10:26:03 -08:00
echo '<p><label>Most minutes one donation can add <input type="number" min="1" class="small-text" name="rule[add_time][cap_minutes]" data-f="cap_minutes" value="' . $v ( 'add_time' , 'cap_minutes' ) . '"></label></p>' ;
echo '<p><label>Most minutes this rule can add in total <input type="number" min="1" class="small-text" name="rule[add_time][max_total_minutes]" data-f="max_total_minutes" value="' . $v ( 'add_time' , 'max_total_minutes' ) . '"></label></p>' ;
2026-11-15 11:40:06 -08:00
echo '<p><label>Longest the timer can ever get (minutes) <input type="number" min="1" class="small-text" name="rule[add_time][max_timer_minutes]" data-f="max_timer_minutes" value="' . $v ( 'add_time' , 'max_timer_minutes' ) . '"></label> <span class="description">Leave empty for no limit. For example, 1440 means the clock never shows more than 24 hours.</span></p>' ;
2026-11-15 10:26:03 -08:00
echo '<p><label>Count ' . $basisOpts ( 'add_time' ) . '</label></p></details></div>' ; // phpcs:ignore WordPress.Security.EscapeOutput -- built from escaped parts
// counter
echo '<div class="sg-rule-group" data-type="counter"' . ( 'counter' === $type ? '' : ' hidden' ) . '><p>Add <input type="number" min="1" class="small-text" name="rule[counter][step]" data-f="step" value="' . $v ( 'counter' , 'step' , '1' ) . '"> to the counter for every ' . esc_html ( $cur ) . ' <input type="text" inputmode="decimal" class="small-text" name="rule[counter][unit_amount]" data-f="unit_amount" value="' . $v ( 'counter' , 'unit_amount' , 0 === Money :: exponent ( $cur ) ? '1000' : '10.00' ) . '"> donated.</p>' ;
echo '<p><label>Counter name (shown on the overlay) <input type="text" maxlength="40" name="rule[counter][label]" data-f="label" placeholder="Pies thrown" value="' . $v ( 'counter' , 'label' ) . '"></label></p>' ;
echo '<p><label><input type="checkbox" name="rule[counter][carry_over]" value="1" data-f="carry_over"' . checked ( 'counter' === $type ? ! empty ( $vals [ 'carry_over' ] ) : true , true , false ) . '> Carry leftovers to the next donation (so two $5 donations count as one $10)</label></p>' ;
echo '<details><summary>Advanced</summary><p><label>Count ' . $basisOpts ( 'counter' ) . '</label></p></details></div>' ; // phpcs:ignore WordPress.Security.EscapeOutput
// bonus
echo '<div class="sg-rule-group" data-type="bonus"' . ( 'bonus' === $type ? '' : ' hidden' ) . '><p>Trigger a bonus when a single donation is at least ' . esc_html ( $cur ) . ' <input type="text" inputmode="decimal" class="small-text" name="rule[bonus][min_amount]" data-f="min_amount" value="' . $v ( 'bonus' , 'min_amount' , 0 === Money :: exponent ( $cur ) ? '5000' : '50.00' ) . '">.</p>' ;
echo '<p><label>Bonus name <input type="text" maxlength="40" name="rule[bonus][event]" data-f="event" value="' . $v ( 'bonus' , 'event' , 'big_donation' ) . '"></label> <span class="description">Letters, numbers, _ and - only.</span></p>' ;
echo '<details><summary>Advanced</summary><p><label>Count ' . $basisOpts ( 'bonus' ) . '</label></p></details></div>' ; // phpcs:ignore WordPress.Security.EscapeOutput
echo '<p class="sg-form-preview" id="sg-form-preview" aria-live="polite"></p>' ;
echo '<p><button class="button button-primary">' . ( $edit ? 'Save rule' : 'Add rule' ) . '</button>' ;
if ( $edit ) {
echo ' <a class="button" href="' . esc_url ( Admin :: url ( Admin :: HOME , array ( 'view' => 'rules' , 'goal' => $goalId ) ) ) . '">Cancel</a>' ;
}
echo '</p></form></div>' ;
}
public static function handleRuleSave () : void {
Admin :: guard ( 'sg_rule_save' );
$post = Admin :: post_raw ();
$goalId = isset ( $post [ 'goal_id' ] ) ? absint ( $post [ 'goal_id' ] ) : 0 ;
$ruleId = isset ( $post [ 'rule_id' ] ) ? absint ( $post [ 'rule_id' ] ) : 0 ;
$g = $goalId ? Store :: goals () -> find ( $goalId ) : null ;
$back = Admin :: url ( Admin :: HOME , array ( 'view' => 'rules' , 'goal' => $goalId ) );
if ( ! $g ) {
Admin :: redirect ( Admin :: url (), 'error' , 'That goal no longer exists.' );
}
$type = isset ( $post [ 'type' ] ) && is_string ( $post [ 'type' ] ) ? $post [ 'type' ] : '' ;
if ( ! isset ( RuleConfig :: TYPES [ $type ] ) ) {
Admin :: redirect ( $back , 'error' , 'Please choose a rule type.' );
}
$existing = null ;
if ( $ruleId ) {
$existing = Store :: rule ( $ruleId , $goalId );
if ( ! $existing || $existing [ 'type' ] !== $type ) {
Admin :: redirect ( $back , 'error' , 'That rule no longer exists.' );
}
}
$input = isset ( $post [ 'rule' ][ $type ] ) && is_array ( $post [ 'rule' ][ $type ] ) ? $post [ 'rule' ][ $type ] : array ();
$res = RuleConfig :: sanitize ( $type , $input , Format :: currency ( $g [ 'currency' ] ) );
if ( null === $res [ 'config' ] ) {
Admin :: redirect ( $back . ( $ruleId ? '&rule=' . $ruleId : '' ), 'error' , implode ( ' ' , $res [ 'errors' ] ) );
}
Store :: saveRule ( $goalId , $type , $res [ 'config' ], $existing ? $ruleId : null );
Admin :: redirect ( $back , 'success' , 'Rule saved: ' . RuleConfig :: summary ( $type , $res [ 'config' ], Format :: currency ( $g [ 'currency' ] ) ) . '.' );
}
public static function handleRuleToggle () : void {
Admin :: guard ( 'sg_rule_toggle' );
$post = Admin :: post_raw ();
$gid = isset ( $post [ 'goal_id' ] ) ? absint ( $post [ 'goal_id' ] ) : 0 ;
Store :: setRuleEnabled ( isset ( $post [ 'rule_id' ] ) ? absint ( $post [ 'rule_id' ] ) : 0 , $gid , ! empty ( $post [ 'enabled' ] ) );
Admin :: redirect ( Admin :: url ( Admin :: HOME , array ( 'view' => 'rules' , 'goal' => $gid ) ), 'success' , 'Rule updated.' );
}
public static function handleRuleDelete () : void {
Admin :: guard ( 'sg_rule_delete' );
$post = Admin :: post_raw ();
$gid = isset ( $post [ 'goal_id' ] ) ? absint ( $post [ 'goal_id' ] ) : 0 ;
Store :: deleteRule ( isset ( $post [ 'rule_id' ] ) ? absint ( $post [ 'rule_id' ] ) : 0 , $gid );
Admin :: redirect ( Admin :: url ( Admin :: HOME , array ( 'view' => 'rules' , 'goal' => $gid ) ), 'success' , 'Rule deleted.' );
}
}