diff --git a/assets/overlay/overlay.css b/assets/overlay/overlay.css index f96c25b..24e7f94 100644 --- a/assets/overlay/overlay.css +++ b/assets/overlay/overlay.css @@ -38,8 +38,8 @@ body.sg-offline #sg-root{opacity:.6} @media (prefers-reduced-motion:reduce){.sg-fill{transition:none}.sg-alert,.sg-alert.sg-out,.sg-alert.sg-bonus,.sg-alert.sg-bonus.sg-out{animation:none}.sg-alert.sg-bonus{box-shadow:0 0 .8em var(--sg-accent2)}} .sg-w-actions #sg-root{align-items:flex-start} .sg-w-actions #sg-root:empty{display:none} -.sg-menu{height:100%;display:flex;flex-direction:column;overflow:hidden} -.sg-menu-view{flex:1;min-height:0;overflow:hidden} +.sg-menu{height:auto;display:flex;flex-direction:column;overflow:hidden;font-size:calc(1em * var(--sg-menu-scale,1))} +.sg-menu-view{flex:none;min-height:0;overflow:hidden} .sg-menu-list{list-style:none;margin:0;padding:0} .sg-menu-item{padding:.3em 0;font-weight:600;overflow-wrap:anywhere} .sg-menu-item b{color:var(--sg-accent2);font-weight:800} diff --git a/assets/overlay/overlay.js b/assets/overlay/overlay.js index 72c93e4..bd87960 100644 --- a/assets/overlay/overlay.js +++ b/assets/overlay/overlay.js @@ -146,14 +146,50 @@ } // ---- actions (reward menu) ---- - // Renders nothing visible when there are no actions. Long lists scroll gently via CSS (--sg-rows sets the duration). - var actionsSig = null; + // Renders nothing visible when there are no actions. The card hugs its content and the text is scaled to the + // LARGEST size that still fits the browser-source window (shrinks for long lists, grows for short ones), and + // re-fits when the window is resized. Only if even the smallest size does not fit does it fall back to a gentle scroll. + var actionsSig = null, menuEls = null, fitTimer = 0; + var MENU_MIN = 0.5, MENU_MAX = 1.8; + function fitMenu() { + if (!menuEls) return; + var card = menuEls.card, view = menuEls.view, track = menuEls.track, ul = menuEls.ul; + // reset any previous scroll fallback + track.classList.remove('sg-scrolling'); + while (track.children.length > 1) track.removeChild(track.lastChild); + view.style.height = ''; + var cs = getComputedStyle(root); + var inner = root.clientHeight - parseFloat(cs.paddingTop) - parseFloat(cs.paddingBottom); + if (inner <= 0) return; + function fits(scale) { + card.style.setProperty('--sg-menu-scale', String(scale)); + return card.offsetHeight <= inner; + } + var lo = MENU_MIN, hi = MENU_MAX, best = null; + if (fits(hi)) { best = hi; } + else if (!fits(lo)) { best = null; } + else { for (var i = 0; i < 9; i++) { var mid = (lo + hi) / 2; if (fits(mid)) { lo = mid; } else { hi = mid; } } best = lo; } + if (best !== null) { card.style.setProperty('--sg-menu-scale', String(best)); return; } + // Even the smallest size overflows: use the smallest and scroll gently inside the window. + card.style.setProperty('--sg-menu-scale', String(MENU_MIN)); + var chrome = card.offsetHeight - view.offsetHeight; + view.style.height = Math.max(40, inner - chrome) + 'px'; + var dup = ul.cloneNode(true); + dup.setAttribute('aria-hidden', 'true'); + track.appendChild(dup); + track.style.setProperty('--sg-scroll-s', Math.max(8, menuEls.count * 3) + 's'); + track.classList.add('sg-scrolling'); + } + function scheduleFit() { clearTimeout(fitTimer); fitTimer = setTimeout(fitMenu, 120); } + window.addEventListener('resize', scheduleFit); + if (typeof ResizeObserver === 'function') { try { new ResizeObserver(scheduleFit).observe(document.documentElement); } catch (e) { /* resize event is the fallback */ } } function renderActions(d) { var list = Array.isArray(d.actions) ? d.actions : []; var sig = JSON.stringify(list); if (sig === actionsSig) return; actionsSig = sig; root.textContent = ''; + menuEls = null; if (!d.active || !list.length) return; var card = el('div', 'sg-card sg-menu'); var view = el('div', 'sg-menu-view'); @@ -171,16 +207,8 @@ view.appendChild(track); card.appendChild(view); root.appendChild(card); - // Overflow check after layout: duplicate the list for a seamless loop and start the CSS scroll. - requestAnimationFrame(function () { - if (ul.scrollHeight > view.clientHeight + 2) { - var dup = ul.cloneNode(true); - dup.setAttribute('aria-hidden', 'true'); - track.appendChild(dup); - track.style.setProperty('--sg-scroll-s', Math.max(8, list.length * 3) + 's'); - track.classList.add('sg-scrolling'); - } - }); + menuEls = { card: card, view: view, track: track, ul: ul, count: list.length }; + requestAnimationFrame(fitMenu); } // ---- polling ---- diff --git a/includes/Admin/GoalsPage.php b/includes/Admin/GoalsPage.php index fb415fd..f9ea020 100644 --- a/includes/Admin/GoalsPage.php +++ b/includes/Admin/GoalsPage.php @@ -43,7 +43,7 @@ final class GoalsPage { $pct = $target > 0 ? min( 100, intdiv( $raised * 100, $target ) ) : 0; echo '
[streamgoals_donate goal="' . $gid . '"]Off (recommended): only the amount the donor chose counts, so a $10 donation always adds $10 even if they also covered the fee.
When the raised total reaches or goes over the target, the goal is marked complete and the donation page stops taking donations to it. Donations already in checkout still complete. You can reopen it with "Reopen" (raise the target first if you want more donations: a reopened goal that is still at or over its target closes again after the next donation).
' . esc_html__( 'This goal has been reached - thank you for your support!', 'streamgoals-donate' ) . '
'; + } return '' . esc_html__( 'There is no active goal to donate to right now.', 'streamgoals-donate' ) . '
'; } diff --git a/tests/Unit/GoalTargetPolicyTest.php b/tests/Unit/GoalTargetPolicyTest.php new file mode 100644 index 0000000..e23bab5 --- /dev/null +++ b/tests/Unit/GoalTargetPolicyTest.php @@ -0,0 +1,86 @@ + 3, 'currency' => 'USD', 'status' => 'active', 'target_minor' => 10000, 'close_at_target' => 1, 'counts_fees' => 0, 'starts_at' => null, 'ends_at' => null ), $o ); + } + + public function test_below_equal_and_over_target(): void { + $this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal(), 9999 ) ); + $this->assertTrue( GoalTargetPolicy::shouldClose( $this->goal(), 10000 ) ); + $this->assertTrue( GoalTargetPolicy::shouldClose( $this->goal(), 25000 ) ); + } + + public function test_flag_off_or_missing_never_closes(): void { + $this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'close_at_target' => 0 ) ), 99999 ) ); + $g = $this->goal(); + unset( $g['close_at_target'] ); + $this->assertFalse( GoalTargetPolicy::shouldClose( $g, 99999 ) ); + } + + public function test_only_active_goals_are_closed(): void { + $this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'status' => 'complete' ) ), 20000 ) ); + $this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'status' => 'paused' ) ), 20000 ) ); + } + + public function test_target_zero_never_closes(): void { + $this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'target_minor' => 0 ) ), 0 ) ); + $this->assertFalse( GoalTargetPolicy::shouldClose( $this->goal( array( 'target_minor' => 0 ) ), 500 ) ); + } + + public function test_base_vs_total_basis(): void { + $this->assertSame( 'base_minor', GoalTargetPolicy::amountColumn( $this->goal() ) ); + $this->assertSame( 'total_minor', GoalTargetPolicy::amountColumn( $this->goal( array( 'counts_fees' => 1 ) ) ) ); + } + + public function test_closed_at_target_detection_and_pause_is_not_closed(): void { + $this->assertTrue( GoalTargetPolicy::isClosedAtTarget( $this->goal( array( 'status' => 'complete' ) ) ) ); + $this->assertFalse( GoalTargetPolicy::isClosedAtTarget( $this->goal( array( 'status' => 'paused' ) ) ) ); + $this->assertFalse( GoalTargetPolicy::isClosedAtTarget( $this->goal( array( 'status' => 'complete', 'close_at_target' => 0 ) ) ) ); + } + + public function test_checkbox_parsing(): void { + $this->assertSame( 1, GoalTargetPolicy::flagFromPost( '1' ) ); + $this->assertSame( 1, GoalTargetPolicy::flagFromPost( 'on' ) ); + $this->assertSame( 0, GoalTargetPolicy::flagFromPost( null ) ); + $this->assertSame( 0, GoalTargetPolicy::flagFromPost( '' ) ); + $this->assertSame( 0, GoalTargetPolicy::flagFromPost( '0' ) ); + $this->assertSame( 0, GoalTargetPolicy::flagFromPost( array( '1' ) ) ); + } + + public function test_new_orders_for_a_closed_goal_get_a_friendly_409(): void { + try { + OrderValidator::validate( array( 'amount' => '5.00' ), $this->goal( array( 'status' => 'complete' ) ), Settings::defaults(), new FeeCalculator( 349, 49 ), 1800000000 ); + $this->fail( 'expected refusal' ); + } catch ( ValidationException $e ) { + $this->assertSame( 'goal_inactive', $e->errorCode ); + $this->assertSame( 409, $e->status ); + $this->assertStringContainsString( 'Thank you', $e->getMessage() ); + } + } + + public function test_paused_goal_keeps_generic_message(): void { + try { + OrderValidator::quote( array( 'amount' => '5.00' ), $this->goal( array( 'status' => 'paused' ) ), Settings::defaults(), new FeeCalculator( 349, 49 ), 1800000000 ); + $this->fail( 'expected refusal' ); + } catch ( ValidationException $e ) { + $this->assertSame( 'This goal is not accepting donations.', $e->getMessage() ); + } + } + + public function test_goal_open_for_active_flagged_goal(): void { + $this->assertTrue( OrderValidator::goalIsOpen( $this->goal(), 1800000000 ) ); + $this->assertFalse( OrderValidator::goalIsOpen( $this->goal( array( 'status' => 'complete' ) ), 1800000000 ) ); + } +}