Files
wp-donations/includes/Overlay/Progress.php
T

92 lines
4.2 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Overlay;
use StreamGoals\Core\GoalTargetPolicy;
use StreamGoals\Tiers\TierPolicy;
/**
* Pure, WordPress-free public progress view for the donate form / [streamgoals_progress] / GET /progress.
* Numbers come from Payloads::goal() so they match the overlay exactly. Public-safe fields only (no donor data).
*/
final class Progress {
/** Parse a shortcode yes/no attribute. Unknown or empty values fall back to $default. */
public static function flag( mixed $raw, bool $default ): bool {
if ( is_bool( $raw ) ) {
return $raw;
}
if ( is_array( $raw ) || null === $raw ) {
return $default;
}
$v = strtolower( trim( (string) $raw ) );
if ( in_array( $v, array( 'yes', 'true', '1', 'on' ), true ) ) {
return true;
}
if ( in_array( $v, array( 'no', 'false', '0', 'off' ), true ) ) {
return false;
}
return $default;
}
/**
* @param array<string,mixed> $goal sg_goals row
* @param array<int,array<string,mixed>>|null $tiers raw sg_goal_tiers rows
* @return array{goal_id:int,title:string,currency:string,raised_minor:int,target_minor:int,percent:int,raised_display:string,target_display:string,reached:bool,tier:?array}
*/
public static function payload( array $goal, int $raised, ?array $tiers = null ): array {
$g = Payloads::goal( $goal, $raised, $tiers ?: null );
$tier = null;
$reached = GoalTargetPolicy::isClosedAtTarget( $goal );
if ( isset( $g['tiers'] ) ) {
$cur = $g['tiers']['current'];
if ( null !== $cur ) {
$tier = array(
'index' => (int) $g['tiers']['index'],
'count' => (int) $g['tiers']['count'],
'title' => (string) $cur['title'],
'target_minor' => (int) $cur['target_minor'],
'target_display' => (string) $cur['target_display'],
);
}
$reached = $reached || ! empty( $g['tiers']['all_reached'] );
} else {
$reached = $reached || ( $g['target_minor'] > 0 && $g['raised_minor'] >= $g['target_minor'] );
}
return array(
'goal_id' => (int) ( $goal['id'] ?? 0 ),
'title' => $g['title'],
'currency' => $g['currency'],
'raised_minor' => $g['raised_minor'],
'target_minor' => $g['target_minor'],
'percent' => $reached ? 100 : $g['percent'],
'raised_display' => $g['raised_display'],
'target_display' => $g['target_display'],
'reached' => $reached,
'tier' => $tier,
);
}
/**
* Server-rendered bar (the JS updates this same markup in place). Labels are already translated.
*
* @param array<string,string> $labels reached, of (%1$s raised, %2$s target), tier (%1$s name, %2$s target), label (%s title)
*/
public static function html( array $p, array $labels ): string {
$e = static fn( string $s ): string => htmlspecialchars( $s, ENT_QUOTES, 'UTF-8' );
$pct = max( 0, min( 100, (int) $p['percent'] ) );
$sub = static fn( string $tpl, array $v ): string => (string) preg_replace_callback( '/%(\d)\$s/', static fn( array $m ): string => $v[ (int) $m[1] - 1 ] ?? '', $tpl );
$of = $sub( $labels['of'] ?? '%1$s / %2$s', array( $p['raised_display'], $p['target_display'] ) );
$out = '<div class="sg-progress' . ( $p['reached'] ? ' sg-progress-reached' : '' ) . '" data-goal="' . (int) $p['goal_id'] . '">';
$out .= '<div class="sg-progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="' . $pct . '"'
. ' aria-valuetext="' . $e( $p['reached'] ? ( $labels['reached'] ?? '' ) : $of . ' (' . $pct . '%)' ) . '"'
. ' aria-label="' . $e( str_replace( '%s', (string) $p['title'], $labels['label'] ?? '%s' ) ) . '">'
. '<span class="sg-progress-fill" style="width:' . $pct . '%"></span></div>';
$out .= '<div class="sg-progress-text"><span class="sg-progress-amounts">' . $e( $of ) . '</span> <span class="sg-progress-pct">' . $pct . '%</span></div>';
$out .= '<p class="sg-progress-tier"' . ( $p['tier'] ? '' : ' hidden' ) . '>'
. ( $p['tier'] ? $e( $sub( $labels['tier'] ?? '%1$s (%2$s)', array( $p['tier']['title'], $p['tier']['target_display'] ) ) ) : '' ) . '</p>';
$out .= '<p class="sg-progress-done"' . ( $p['reached'] ? '' : ' hidden' ) . '>' . $e( $p['reached'] ? ( $labels['reached'] ?? '' ) : '' ) . '</p>';
return $out . '</div>';
}
}