From 537cf9db07b629f5464214bf092d2620c4092189 Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Wed, 25 Nov 2026 12:01:14 +0100 Subject: [PATCH 1/4] Redesign the monthly badges popover The popover now opens with three cards: how long the site has used Progress Planner, the monthly badges earned and the tasks completed. Each year follows, newest first, with its badges in a four-column grid and its own badges-earned and tasks-completed numbers. Hovering (or tabbing to) a badge that can still be earned shows how many points are left; the current month's badge shows "In progress" with a short wobble, which is off for reduced motion. The widget link now reads "Show all my monthly badges!". The content and streak badges, and the hook pp-hosts uses to add its onboarding badges, stay below the monthly section until they get their own popovers. Co-Authored-By: Claude Opus 5.5 --- assets/css/page-widgets/monthly-badges.css | 135 ++++++++++++++- assets/css/web-components/prpl-badge.css | 126 +++++++++++++- .../admin/widgets/class-monthly-badges.php | 99 +++++++++++ .../test-class-monthly-badges-popover.php | 151 +++++++++++++++++ views/page-widgets/monthly-badges.php | 2 +- views/popovers/monthly-badges.php | 154 ++++++++++++++---- 6 files changed, 632 insertions(+), 35 deletions(-) create mode 100644 tests/phpunit/test-class-monthly-badges-popover.php diff --git a/assets/css/page-widgets/monthly-badges.css b/assets/css/page-widgets/monthly-badges.css index 16a14b8c8..d364ac56e 100644 --- a/assets/css/page-widgets/monthly-badges.css +++ b/assets/css/page-widgets/monthly-badges.css @@ -65,11 +65,114 @@ \*------------------------------------*/ #prpl-popover-monthly-badges { - .prpl-widgets-container { + .prpl-monthly-badges-popover { + container-type: inline-size; + } + + /* Number cards: the totals at the top and the per-year cards. */ + .prpl-badges-stat { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + gap: 0.25rem; + padding: var(--prpl-padding); + border-radius: var(--prpl-border-radius-big); + background: var(--prpl-background-monthly); + text-align: center; + } + + .prpl-badges-stat-number { + font-size: var(--prpl-font-size-3xl); + font-weight: 600; + line-height: 1; + color: var(--prpl-color-text); + } + + .prpl-badges-stat-label { + font-size: var(--prpl-font-size-base); + } + + /* Top row: intro text and the two totals. */ + .prpl-badges-summary { + display: grid; + grid-template-columns: repeat(3, 1fr); + gap: var(--prpl-padding); + padding-bottom: var(--prpl-gap); + border-bottom: 1px solid var(--prpl-color-divider); + + .prpl-badges-stat, + .prpl-badges-summary-intro { + background: var(--prpl-background-content); + } + } + + .prpl-badges-summary-intro { + padding: var(--prpl-padding); + border-radius: var(--prpl-border-radius-big); + + p { + margin: 0; + + & + p { + margin-top: 0.5rem; + } + } + + strong { + color: var(--prpl-color-text); + } + } + + /* One section per year. */ + .prpl-badges-year { + padding: var(--prpl-padding) 0 var(--prpl-gap); + border-bottom: 1px solid var(--prpl-color-divider); + } + + .prpl-badges-year-title { + margin: 0 0 var(--prpl-padding); + font-size: var(--prpl-font-size-xl); + color: var(--prpl-color-headings); + } + + .prpl-badges-year-content { + display: grid; + grid-template-columns: 2fr 1fr; + gap: var(--prpl-padding); + align-items: start; + } + + .prpl-badges-year-grid { + display: grid; + grid-template-columns: repeat(4, 1fr); + gap: var(--prpl-padding); + } + + .prpl-badges-year-stats { + display: grid; + gap: var(--prpl-padding); + } + + /* The wrapper is not a grid item, so the badge needs to be a block to size it. */ + .prpl-badge-image prpl-badge { + display: block; + } + + .prpl-badge-name { + margin: 0; + font-size: var(--prpl-font-size-small); + text-align: center; + line-height: 1.2; + } + + /* The content and streak badges (until they get their own popovers). */ + .prpl-widgets-container.in-popover { display: grid; grid-template-columns: repeat(auto-fit, minmax(var(--prpl-column-min-width), 1fr)); grid-gap: var(--prpl-gap); grid-auto-rows: auto; + margin-top: var(--prpl-gap); } .prpl-widget-title { @@ -78,10 +181,6 @@ background: var(--prpl-background-monthly); } - .badge-group-monthly { - background-color: transparent; - } - .prpl-widget-wrapper-content { .prpl-widget-title { @@ -103,6 +202,32 @@ background-color: transparent; } } + + @container (max-width: 900px) { + + .prpl-badges-summary { + grid-template-columns: 1fr 1fr; + } + + .prpl-badges-summary-intro { + grid-column: 1 / -1; + } + + .prpl-badges-year-content { + grid-template-columns: 1fr; + } + + .prpl-badges-year-stats { + grid-template-columns: 1fr 1fr; + } + } + + @container (max-width: 520px) { + + .prpl-badges-year-grid { + grid-template-columns: repeat(2, 1fr); + } + } } /* This is the badge streak widget. */ diff --git a/assets/css/web-components/prpl-badge.css b/assets/css/web-components/prpl-badge.css index 4807362f0..e26729f12 100644 --- a/assets/css/web-components/prpl-badge.css +++ b/assets/css/web-components/prpl-badge.css @@ -77,7 +77,7 @@ prpl-badge { width: 20px; height: 20px; background-color: var(--prpl-color-icon-missed-badge); - border: 2px solid #fff; + border: 2px solid var(--prpl-background-paper); border-radius: 50%; position: absolute; top: 10%; @@ -87,3 +87,127 @@ prpl-badge { } } } + +/*------------------------------------*\ + Monthly badges popover: hover states. + + A missed badge (still earnable) shows how many points are left; + the current month's badge shows "In progress" with a small wobble. +\*------------------------------------*/ +.prpl-badge[data-state] { + position: relative; + cursor: default; + border-radius: var(--prpl-radius-paper); + + &:focus-visible { + outline: 2px solid var(--prpl-color-link); + outline-offset: 4px; + } + + .prpl-badge-image { + position: relative; + display: block; + } + + &:is(:hover, :focus-visible) prpl-badge[complete="false"] img { + opacity: 1; + filter: none; + } +} + +.prpl-badge-points-to-go { + position: absolute; + top: 0; + left: 50%; + z-index: 1; + display: inline-flex; + align-items: center; + gap: 0.25rem; + padding: 2px 0.5rem 2px 2px; + border-radius: 999px; + background: var(--prpl-background-alert-error); + color: var(--prpl-color-text); + font-size: var(--prpl-font-size-small); + line-height: 1.2; + white-space: nowrap; + pointer-events: none; + opacity: 0; + transform: translate(-50%, 4px); + transition: opacity 0.2s ease-in-out, transform 0.2s ease-in-out; +} + +.prpl-badge-points-to-go-icon { + display: inline-flex; + align-items: center; + justify-content: center; + width: 20px; + height: 20px; + border-radius: 50%; + background: var(--prpl-color-icon-missed-badge); + color: #fff; + font-weight: 600; +} + +.prpl-badge[data-state="missed"]:is(:hover, :focus-visible) { + + .prpl-badge-points-to-go { + opacity: 1; + transform: translate(-50%, 0); + } + + /* The pill carries the "!" now. */ + prpl-badge::after { + opacity: 0; + } +} + +.prpl-badge-name-hover { + display: none; + font-weight: 600; +} + +.prpl-badge[data-state="in-progress"]:is(:hover, :focus-visible) { + + .prpl-badge-name-default { + display: none; + } + + .prpl-badge-name-hover { + display: inline; + } + + prpl-badge img { + animation: prpl-badge-wobble 0.8s ease-in-out; + } +} + +@keyframes prpl-badge-wobble { + + 0%, + 100% { + transform: rotate(0); + } + + 20% { + transform: rotate(-8deg); + } + + 40% { + transform: rotate(6deg); + } + + 60% { + transform: rotate(-4deg); + } + + 80% { + transform: rotate(2deg); + } +} + +@media (prefers-reduced-motion: reduce) { + + .prpl-badge[data-state="in-progress"]:is(:hover, :focus-visible) prpl-badge img { + animation: none; + } +} diff --git a/classes/admin/widgets/class-monthly-badges.php b/classes/admin/widgets/class-monthly-badges.php index b50c2050f..86a7843be 100644 --- a/classes/admin/widgets/class-monthly-badges.php +++ b/classes/admin/widgets/class-monthly-badges.php @@ -22,6 +22,13 @@ final class Monthly_Badges extends Widget { */ protected $id = 'monthly-badges'; + /** + * The popover numbers, computed once per request. + * + * @var array{months: int, badges: int, tasks: int, years: array}|null + */ + private $popover_stats = null; + /** * Get the score. * @@ -71,6 +78,98 @@ public function get_previous_incomplete_months_badges() { return $previous_incomplete_month_badges; } + /** + * Get the numbers shown in the monthly badges popover. + * + * Badges are the monthly badges earned; tasks are the completed tasks + * (recommendations and golden to-do tasks) that earn monthly badge points. + * + * @return array{months: int, badges: int, tasks: int, years: array} + */ + public function get_popover_stats() { + if ( null !== $this->popover_stats ) { + return $this->popover_stats; + } + + $stats = [ + 'months' => $this->get_months_since_activation(), + 'badges' => 0, + 'tasks' => 0, + 'years' => [], + ]; + + foreach ( \array_keys( Monthly::get_instances() ) as $year ) { + $badges = 0; + foreach ( Monthly::get_instances_for_year( $year ) as $badge ) { + if ( 100 === (int) $badge->progress_callback()['progress'] ) { + ++$badges; + } + } + + $tasks = \count( + \progress_planner()->get_activities__query()->query_activities( + [ + 'category' => 'suggested_task', + 'type' => 'completed', + 'start_date' => new DateTime( "{$year}-01-01" ), + 'end_date' => new DateTime( "{$year}-12-31" ), + ] + ) + ); + + $stats['years'][ (int) $year ] = [ + 'badges' => $badges, + 'tasks' => $tasks, + ]; + $stats['badges'] += $badges; + $stats['tasks'] += $tasks; + } + + $this->popover_stats = $stats; + return $stats; + } + + /** + * Get the hover state of a badge in the monthly badges popover. + * + * - "missed": one of the previous two months, not complete, still earnable. + * - "in-progress": the current month, not complete yet. + * - "": no hover state (complete, future, or too old to earn). + * + * @param \Progress_Planner\Badges\Monthly $badge The badge. + * + * @return string + */ + public function get_popover_badge_state( $badge ) { + if ( 100 === (int) $badge->progress_callback()['progress'] ) { + return ''; + } + + foreach ( $this->get_previous_incomplete_months_badges() as $missed_badge ) { + if ( $missed_badge->get_id() === $badge->get_id() ) { + return 'missed'; + } + } + + return Monthly::get_badge_id_from_date( new DateTime() ) === $badge->get_id() ? 'in-progress' : ''; + } + + /** + * Get the number of months since the plugin was activated. + * + * Counts started months, so a site activated this month shows 1. + * + * @return int + */ + public function get_months_since_activation() { + $activation = \progress_planner()->get_activation_date(); + $now = new DateTime(); + $months = ( (int) $now->format( 'Y' ) - (int) $activation->format( 'Y' ) ) * 12 + + (int) $now->format( 'n' ) - (int) $activation->format( 'n' ); + + return \max( 1, $months + 1 ); + } + /** * Get the stylesheet dependencies. * diff --git a/tests/phpunit/test-class-monthly-badges-popover.php b/tests/phpunit/test-class-monthly-badges-popover.php new file mode 100644 index 000000000..76f848374 --- /dev/null +++ b/tests/phpunit/test-class-monthly-badges-popover.php @@ -0,0 +1,151 @@ +set_activation_date( ( new \DateTime( 'first day of this month' ) )->modify( '-6 months' ) ); + \progress_planner()->get_settings()->set( 'badges', [] ); + } + + /** + * Set the activation date and drop the cached badge instances built from it. + * + * @param \DateTime $date The activation date. + * + * @return void + */ + private function set_activation_date( $date ) { + \progress_planner()->get_settings()->set( 'activation_date', $date->format( 'Y-m-d' ) ); + + $instances = new \ReflectionProperty( Monthly::class, 'instances' ); + $instances->setAccessible( true ); + $instances->setValue( null, [] ); + } + + /** + * Insert task activities on a date. + * + * @param string $date The activity date (Y-m-d). + * @param int $count How many activities. + * @param string $type The activity type. + * + * @return void + */ + private function insert_activities( $date, $count, $type = 'completed' ) { + static $data_id = 7000; + for ( $i = 0; $i < $count; $i++ ) { + $activity = new Activity(); + $activity->category = 'suggested_task'; + $activity->type = $type; + $activity->date = new \DateTime( $date ); + $activity->data_id = (string) ++$data_id; + $activity->user_id = 1; + $activity->save(); + } + } + + /** + * Get the badge for a month relative to now. + * + * @param string $modify A DateTime modifier, e.g. "-1 month". + * + * @return Monthly + */ + private function badge_for( $modify ) { + $date = ( new \DateTime( 'first day of this month' ) )->modify( $modify ); + return Monthly::get_instance_from_id( Monthly::get_badge_id_from_date( $date ) ); + } + + /** + * Months are counted as started months, so this month counts as one. + * + * @return void + */ + public function test_months_since_activation() { + $widget = new Monthly_Badges(); + + $this->set_activation_date( new \DateTime( 'first day of this month' ) ); + $this->assertSame( 1, $widget->get_months_since_activation() ); + + $this->set_activation_date( ( new \DateTime( 'first day of this month' ) )->modify( '-3 months' ) ); + $this->assertSame( 4, $widget->get_months_since_activation() ); + } + + /** + * The current month's badge is "in progress" until it is complete. + * + * @return void + */ + public function test_current_month_badge_is_in_progress() { + $this->assertSame( 'in-progress', ( new Monthly_Badges() )->get_popover_badge_state( $this->badge_for( '+0 months' ) ) ); + } + + /** + * The previous two months are "missed" while incomplete; older months have no state. + * + * @return void + */ + public function test_only_the_previous_two_months_are_missed() { + $widget = new Monthly_Badges(); + + $this->assertSame( 'missed', $widget->get_popover_badge_state( $this->badge_for( '-1 month' ) ) ); + $this->assertSame( 'missed', $widget->get_popover_badge_state( $this->badge_for( '-2 months' ) ) ); + $this->assertSame( '', $widget->get_popover_badge_state( $this->badge_for( '-3 months' ) ) ); + } + + /** + * A complete badge has no hover state. + * + * @return void + */ + public function test_complete_badge_has_no_state() { + $this->insert_activities( ( new \DateTime( 'first day of this month' ) )->format( 'Y-m-d' ), Monthly::TARGET_POINTS ); + + $this->assertSame( '', ( new Monthly_Badges() )->get_popover_badge_state( $this->badge_for( '+0 months' ) ) ); + } + + /** + * Earned badges and completed tasks are counted per year and in total. + * + * @return void + */ + public function test_popover_stats_count_badges_and_completed_tasks() { + $this_month = new \DateTime( 'first day of this month' ); + $year = (int) $this_month->format( 'Y' ); + + // Enough completed tasks to earn this month's badge. + $this->insert_activities( $this_month->format( 'Y-m-d' ), Monthly::TARGET_POINTS ); + + // A task activity that is not a completion is not a completed task. + $this->insert_activities( $this_month->format( 'Y-m-d' ), 1, 'test_activity' ); + + $stats = ( new Monthly_Badges() )->get_popover_stats(); + + $this->assertSame( 1, $stats['years'][ $year ]['badges'] ); + $this->assertSame( Monthly::TARGET_POINTS, $stats['years'][ $year ]['tasks'] ); + $this->assertSame( 1, $stats['badges'] ); + $this->assertSame( Monthly::TARGET_POINTS, $stats['tasks'] ); + $this->assertSame( 7, $stats['months'] ); + } +} diff --git a/views/page-widgets/monthly-badges.php b/views/page-widgets/monthly-badges.php index ac962716e..4f1e793e7 100644 --- a/views/page-widgets/monthly-badges.php +++ b/views/page-widgets/monthly-badges.php @@ -60,7 +60,7 @@ \progress_planner()->the_view( 'page-widgets/parts/monthly-badges.php', [ 'title_year' => 2025 ] ); } - \progress_planner()->get_ui__popover()->the_popover( 'monthly-badges' )->render_button( '', \esc_html__( 'Show all badges', 'progress-planner' ) ); + \progress_planner()->get_ui__popover()->the_popover( 'monthly-badges' )->render_button( '', \esc_html__( 'Show all my monthly badges!', 'progress-planner' ) ); \progress_planner()->get_ui__popover()->the_popover( 'monthly-badges' )->render(); ?> diff --git a/views/popovers/monthly-badges.php b/views/popovers/monthly-badges.php index 2f17c264d..840ceed58 100644 --- a/views/popovers/monthly-badges.php +++ b/views/popovers/monthly-badges.php @@ -5,44 +5,142 @@ * @package Progress_Planner */ +use Progress_Planner\Badges\Monthly; + // Exit if accessed directly. if ( ! \defined( 'ABSPATH' ) ) { exit; } -?> -

-
-
- - * - * This is an associative array where the key is the year and the elements are arrays of \Progress_Planner\Badges\Badge objects. - */ - $prpl_badges = \progress_planner()->get_badges()->get_badges( 'monthly' ); +$prpl_widget = \progress_planner()->get_admin__widgets__monthly_badges(); +$prpl_stats = $prpl_widget->get_popover_stats(); +$prpl_branding_id = (int) \progress_planner()->get_ui__branding()->get_branding_id(); - foreach ( $prpl_badges as $prpl_badges_year => $prpl_monthly_badges ) { - \progress_planner()->the_view( - 'page-widgets/parts/monthly-badges.php', - [ - 'css_class' => 'in-popover', - 'badges_year' => $prpl_badges_year, - ] - ); - } - ?> +// Newest year first. +$prpl_badges_years = \array_keys( Monthly::get_instances() ); +\rsort( $prpl_badges_years ); +?> +

+ +
+
+
+

+

+ ' . \sprintf( + /* translators: %s: The number of months. */ + \esc_html( \_n( '%s month', '%s months', $prpl_stats['months'], 'progress-planner' ) ), + \number_format_i18n( $prpl_stats['months'] ) + ) . '' + ), + [ 'strong' => [] ] + ); + ?> +

+
+
+ + +
+
+ + +
-
+ \__( 'Content badges', 'progress-planner' ), - 'maintenance' => \__( 'Streak badges', 'progress-planner' ), + $prpl_year_stats = $prpl_stats['years'][ (int) $prpl_badges_year ] ?? [ + 'badges' => 0, + 'tasks' => 0, ]; ?> +
+

+ +

+
+
+ + progress_callback(); + $prpl_is_complete = 100 === (int) $prpl_badge_progress['progress']; + $prpl_badge_state = $prpl_widget->get_popover_badge_state( $prpl_badge ); + ?> + + data-state="" + tabindex="0" + + > + + + + + + + + + +

+ get_name() ); ?> + + + +

+
+ +
+
+
+ + +
+
+ + +
+
+
+
+ + + \__( 'Content badges', 'progress-planner' ), + 'maintenance' => \__( 'Streak badges', 'progress-planner' ), + ]; + ?> +
$prpl_widget_title ) : ?>

@@ -61,7 +159,7 @@ class="prpl-badge" complete="" badge-id="get_id() ); ?>" badge-name="get_name() ); ?>" - branding-id="get_ui__branding()->get_branding_id(); ?>" + branding-id="" >

get_name() ); ?>

From 0894ec47cc2d453989c13c1ccd82da788bfd0962 Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Wed, 25 Nov 2026 12:35:54 +0100 Subject: [PATCH 2/4] Match the popover spacing to the design 32px inside the popover (--prpl-gap) and 6px between a badge and its name. No spacing variable gives 6px, so it is set directly. Co-Authored-By: Claude Opus 5.5 --- assets/css/page-widgets/monthly-badges.css | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/assets/css/page-widgets/monthly-badges.css b/assets/css/page-widgets/monthly-badges.css index d364ac56e..d0be8e0b9 100644 --- a/assets/css/page-widgets/monthly-badges.css +++ b/assets/css/page-widgets/monthly-badges.css @@ -64,6 +64,7 @@ Popover styles. \*------------------------------------*/ #prpl-popover-monthly-badges { + padding: var(--prpl-gap); .prpl-monthly-badges-popover { container-type: inline-size; @@ -147,6 +148,11 @@ display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--prpl-padding); + + /* The badge's own 6px margin is the only space above the name. */ + .prpl-badge { + gap: 0; + } } .prpl-badges-year-stats { @@ -157,6 +163,7 @@ /* The wrapper is not a grid item, so the badge needs to be a block to size it. */ .prpl-badge-image prpl-badge { display: block; + margin-bottom: 6px; /* Design: 6px between badge and name; no spacing variable gives 6px. */ } .prpl-badge-name { From 3a5d7bf78921d346f4fae013e2701ea8947e3e63 Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Wed, 25 Nov 2026 12:40:55 +0100 Subject: [PATCH 3/4] Work out each monthly badge once, and fix the yearly task count The popover asked each badge for its progress up to three times (for the yearly numbers, the template and the hover state), and the hover state also re-checked the previous two months for every badge. Each call on an unfinished badge saves its progress, so one render tried to write the settings 23 times on a site with four badges. Progress and hover state are now worked out once per badge in get_popover_stats(), and the template reads them from there: 5 writes on the same site. The yearly "tasks completed" counted from 1 January, but the first year's badges start later (November 2024, or the month of activation), so earlier tasks were counted too. It now counts from the first badge month of that year. The hover details ("N points to go", "In progress") also show on focus, so a tap on a touch screen reveals them; the outline stays for keyboard focus only. Co-Authored-By: Claude Opus 5.5 --- assets/css/web-components/prpl-badge.css | 14 ++- .../admin/widgets/class-monthly-badges.php | 87 +++++++++++++------ .../test-class-monthly-badges-popover.php | 40 +++++++++ views/popovers/monthly-badges.php | 24 ++--- 4 files changed, 118 insertions(+), 47 deletions(-) diff --git a/assets/css/web-components/prpl-badge.css b/assets/css/web-components/prpl-badge.css index e26729f12..0e1a08d39 100644 --- a/assets/css/web-components/prpl-badge.css +++ b/assets/css/web-components/prpl-badge.css @@ -99,6 +99,12 @@ prpl-badge { cursor: default; border-radius: var(--prpl-radius-paper); + /* Reveal on :focus too, so a tap on a touch screen shows the details; + the outline is only for keyboard focus. */ + &:focus { + outline: none; + } + &:focus-visible { outline: 2px solid var(--prpl-color-link); outline-offset: 4px; @@ -109,7 +115,7 @@ prpl-badge { display: block; } - &:is(:hover, :focus-visible) prpl-badge[complete="false"] img { + &:is(:hover, :focus) prpl-badge[complete="false"] img { opacity: 1; filter: none; } @@ -148,7 +154,7 @@ prpl-badge { font-weight: 600; } -.prpl-badge[data-state="missed"]:is(:hover, :focus-visible) { +.prpl-badge[data-state="missed"]:is(:hover, :focus) { .prpl-badge-points-to-go { opacity: 1; @@ -166,7 +172,7 @@ prpl-badge { font-weight: 600; } -.prpl-badge[data-state="in-progress"]:is(:hover, :focus-visible) { +.prpl-badge[data-state="in-progress"]:is(:hover, :focus) { .prpl-badge-name-default { display: none; @@ -207,7 +213,7 @@ prpl-badge { @media (prefers-reduced-motion: reduce) { - .prpl-badge[data-state="in-progress"]:is(:hover, :focus-visible) prpl-badge img { + .prpl-badge[data-state="in-progress"]:is(:hover, :focus) prpl-badge img { animation: none; } } diff --git a/classes/admin/widgets/class-monthly-badges.php b/classes/admin/widgets/class-monthly-badges.php index 86a7843be..38a8b887b 100644 --- a/classes/admin/widgets/class-monthly-badges.php +++ b/classes/admin/widgets/class-monthly-badges.php @@ -23,9 +23,9 @@ final class Monthly_Badges extends Widget { protected $id = 'monthly-badges'; /** - * The popover numbers, computed once per request. + * The popover data, computed once per request. * - * @var array{months: int, badges: int, tasks: int, years: array}|null + * @var array{months: int, badges: int, tasks: int, years: array}>}|null */ private $popover_stats = null; @@ -79,12 +79,14 @@ public function get_previous_incomplete_months_badges() { } /** - * Get the numbers shown in the monthly badges popover. + * Get everything the monthly badges popover shows. * - * Badges are the monthly badges earned; tasks are the completed tasks - * (recommendations and golden to-do tasks) that earn monthly badge points. + * Each badge's progress is worked out once here; the template reads it + * from the result. Badges are the monthly badges earned; tasks are the + * completed tasks (recommendations and golden to-do tasks), counted from + * the first month that has a badge. * - * @return array{months: int, badges: int, tasks: int, years: array} + * @return array{months: int, badges: int, tasks: int, years: array}>} */ public function get_popover_stats() { if ( null !== $this->popover_stats ) { @@ -98,31 +100,43 @@ public function get_popover_stats() { 'years' => [], ]; - foreach ( \array_keys( Monthly::get_instances() ) as $year ) { + foreach ( Monthly::get_instances() as $year => $year_badges ) { + $year = (int) $year; $badges = 0; - foreach ( Monthly::get_instances_for_year( $year ) as $badge ) { - if ( 100 === (int) $badge->progress_callback()['progress'] ) { + $items = []; + foreach ( $year_badges as $badge ) { + $progress = $badge->progress_callback(); + $items[] = [ + 'badge' => $badge, + 'progress' => $progress, + 'state' => $this->get_badge_state( $badge->get_id(), (int) $progress['progress'] ), + ]; + if ( 100 === (int) $progress['progress'] ) { ++$badges; } } - $tasks = \count( - \progress_planner()->get_activities__query()->query_activities( - [ - 'category' => 'suggested_task', - 'type' => 'completed', - 'start_date' => new DateTime( "{$year}-01-01" ), - 'end_date' => new DateTime( "{$year}-12-31" ), - ] - ) - ); - - $stats['years'][ (int) $year ] = [ + $tasks = 0; + if ( ! empty( $year_badges ) ) { + $tasks = \count( + \progress_planner()->get_activities__query()->query_activities( + [ + 'category' => 'suggested_task', + 'type' => 'completed', + 'start_date' => new DateTime( \sprintf( '%d-%02d-01', $year, (int) $year_badges[0]->get_month() ) ), + 'end_date' => new DateTime( "{$year}-12-31" ), + ] + ) + ); + } + + $stats['years'][ $year ] = [ 'badges' => $badges, 'tasks' => $tasks, + 'items' => $items, ]; - $stats['badges'] += $badges; - $stats['tasks'] += $tasks; + $stats['badges'] += $badges; + $stats['tasks'] += $tasks; } $this->popover_stats = $stats; @@ -141,17 +155,36 @@ public function get_popover_stats() { * @return string */ public function get_popover_badge_state( $badge ) { - if ( 100 === (int) $badge->progress_callback()['progress'] ) { + return $this->get_badge_state( $badge->get_id(), (int) $badge->progress_callback()['progress'] ); + } + + /** + * Get a badge's hover state from its ID and progress. + * + * @param string $badge_id The badge ID. + * @param int $progress The badge progress (0-100). + * + * @return string + */ + private function get_badge_state( $badge_id, $progress ) { + if ( 100 <= $progress ) { return ''; } - foreach ( $this->get_previous_incomplete_months_badges() as $missed_badge ) { - if ( $missed_badge->get_id() === $badge->get_id() ) { + $month = new DateTime( 'first day of this month' ); + if ( Monthly::get_badge_id_from_date( $month ) === $badge_id ) { + return 'in-progress'; + } + + // The previous two months can still be earned. + for ( $i = 0; $i < 2; $i++ ) { + $month->modify( 'first day of previous month' ); + if ( Monthly::get_badge_id_from_date( $month ) === $badge_id ) { return 'missed'; } } - return Monthly::get_badge_id_from_date( new DateTime() ) === $badge->get_id() ? 'in-progress' : ''; + return ''; } /** diff --git a/tests/phpunit/test-class-monthly-badges-popover.php b/tests/phpunit/test-class-monthly-badges-popover.php index 76f848374..48e37f433 100644 --- a/tests/phpunit/test-class-monthly-badges-popover.php +++ b/tests/phpunit/test-class-monthly-badges-popover.php @@ -148,4 +148,44 @@ public function test_popover_stats_count_badges_and_completed_tasks() { $this->assertSame( Monthly::TARGET_POINTS, $stats['tasks'] ); $this->assertSame( 7, $stats['months'] ); } + + /** + * Tasks completed before the first badge month are not counted for that year. + * + * @return void + */ + public function test_tasks_before_the_first_badge_month_are_not_counted() { + $this_month = new \DateTime( 'first day of this month' ); + if ( 1 === (int) $this_month->format( 'n' ) ) { + $this->markTestSkipped( 'Needs an earlier month in the same year.' ); + } + + // Activated this month, so this year's first badge is this month's. + $this->set_activation_date( clone $this_month ); + + $this->insert_activities( ( clone $this_month )->modify( '-1 month' )->format( 'Y-m-d' ), 2 ); + $this->insert_activities( $this_month->format( 'Y-m-d' ), 3 ); + + $stats = ( new Monthly_Badges() )->get_popover_stats(); + + $this->assertSame( 3, $stats['years'][ (int) $this_month->format( 'Y' ) ]['tasks'] ); + } + + /** + * Every badge in the popover data carries its progress and state. + * + * @return void + */ + public function test_popover_items_carry_progress_and_state() { + $year = (int) ( new \DateTime() )->format( 'Y' ); + $items = ( new Monthly_Badges() )->get_popover_stats()['years'][ $year ]['items']; + + $states = []; + foreach ( $items as $item ) { + $this->assertArrayHasKey( 'progress', $item['progress'] ); + $states[ $item['badge']->get_id() ] = $item['state']; + } + + $this->assertSame( 'in-progress', $states[ Monthly::get_badge_id_from_date( new \DateTime() ) ] ); + } } diff --git a/views/popovers/monthly-badges.php b/views/popovers/monthly-badges.php index 840ceed58..48c56b9ab 100644 --- a/views/popovers/monthly-badges.php +++ b/views/popovers/monthly-badges.php @@ -5,20 +5,17 @@ * @package Progress_Planner */ -use Progress_Planner\Badges\Monthly; - // Exit if accessed directly. if ( ! \defined( 'ABSPATH' ) ) { exit; } -$prpl_widget = \progress_planner()->get_admin__widgets__monthly_badges(); -$prpl_stats = $prpl_widget->get_popover_stats(); +$prpl_stats = \progress_planner()->get_admin__widgets__monthly_badges()->get_popover_stats(); $prpl_branding_id = (int) \progress_planner()->get_ui__branding()->get_branding_id(); // Newest year first. -$prpl_badges_years = \array_keys( Monthly::get_instances() ); -\rsort( $prpl_badges_years ); +$prpl_years = $prpl_stats['years']; +\krsort( $prpl_years ); ?>

@@ -53,13 +50,7 @@

- - 0, - 'tasks' => 0, - ]; - ?> + $prpl_year_stats ) : ?>

- + progress_callback(); + $prpl_badge = $prpl_item['badge']; + $prpl_badge_progress = $prpl_item['progress']; + $prpl_badge_state = $prpl_item['state']; $prpl_is_complete = 100 === (int) $prpl_badge_progress['progress']; - $prpl_badge_state = $prpl_widget->get_popover_badge_state( $prpl_badge ); ?> Date: Wed, 25 Nov 2026 12:50:35 +0100 Subject: [PATCH 4/4] Leave room for the tour box when the tour opens the badges popover The tour opens the monthly badges popover on one step. A popover sits above everything else, including the tour box, and the redesigned popover is taller: on a 1280x720 screen it covered the tour's Next button (the e2e tour test failed on that). While the tour shows it, the popover now stays at the top with a lower maximum height, so the tour box fits below. Co-Authored-By: Claude Opus 5.5 --- assets/css/admin.css | 6 ++++++ assets/js/tour.js | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/assets/css/admin.css b/assets/css/admin.css index ad58f000d..669a4114e 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -542,6 +542,12 @@ button.prpl-info-icon { } } +/* While the tour points at a popover, keep it at the top so the tour box fits below. */ +.prpl-popover.prpl-popover-in-tour { + margin-top: var(--prpl-padding); + max-height: calc(100vh - 280px); +} + .driver-popover.prpl-driverjs-theme { background-color: var(--prpl-background-monthly); color: var(--prpl-color-text); diff --git a/assets/js/tour.js b/assets/js/tour.js index c57ccb41f..e17eb95dd 100644 --- a/assets/js/tour.js +++ b/assets/js/tour.js @@ -43,11 +43,15 @@ const prplDriverObj = prplDriver( { } ); function prplTourShowPopover( popover ) { + // The popover sits above the tour box (top layer); this class keeps it at + // the top of the screen and leaves room for the tour box below it. + popover.classList.add( 'prpl-popover-in-tour' ); popover.showPopover(); prplMakePopoverBackdropTransparent( popover ); } function prplTourHidePopover( popover ) { + popover.classList.remove( 'prpl-popover-in-tour' ); popover.hidePopover(); document.getElementById( popover.id + '-backdrop-transparency' ).remove(); }