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/css/page-widgets/monthly-badges.css b/assets/css/page-widgets/monthly-badges.css index 16a14b8c8..d0be8e0b9 100644 --- a/assets/css/page-widgets/monthly-badges.css +++ b/assets/css/page-widgets/monthly-badges.css @@ -64,12 +64,122 @@ Popover styles. \*------------------------------------*/ #prpl-popover-monthly-badges { + padding: var(--prpl-gap); - .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); + + /* The badge's own 6px margin is the only space above the name. */ + .prpl-badge { + gap: 0; + } + } + + .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; + margin-bottom: 6px; /* Design: 6px between badge and name; no spacing variable gives 6px. */ + } + + .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 +188,6 @@ background: var(--prpl-background-monthly); } - .badge-group-monthly { - background-color: transparent; - } - .prpl-widget-wrapper-content { .prpl-widget-title { @@ -103,6 +209,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..0e1a08d39 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,133 @@ 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); + + /* 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; + } + + .prpl-badge-image { + position: relative; + display: block; + } + + &:is(:hover, :focus) 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) { + + .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) { + + .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) prpl-badge img { + animation: none; + } +} 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(); } diff --git a/classes/admin/widgets/class-monthly-badges.php b/classes/admin/widgets/class-monthly-badges.php index b50c2050f..38a8b887b 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 data, 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,131 @@ public function get_previous_incomplete_months_badges() { return $previous_incomplete_month_badges; } + /** + * Get everything the monthly badges popover shows. + * + * 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}>} + */ + 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 ( Monthly::get_instances() as $year => $year_badges ) { + $year = (int) $year; + $badges = 0; + $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 = 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; + } + + $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 ) { + 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 ''; + } + + $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 ''; + } + + /** + * 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..48e37f433 --- /dev/null +++ b/tests/phpunit/test-class-monthly-badges-popover.php @@ -0,0 +1,191 @@ +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'] ); + } + + /** + * 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/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..48c56b9ab 100644 --- a/views/popovers/monthly-badges.php +++ b/views/popovers/monthly-badges.php @@ -9,40 +9,130 @@ 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_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_years = $prpl_stats['years']; +\krsort( $prpl_years ); +?> +

- 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, - ] - ); - } - ?> +
+
+
+

+

+ ' . \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 ) : ?> +
+

+ +

+
+
+ + + + data-state="" + tabindex="0" + + > + + + + + + + + + +

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

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

@@ -61,7 +151,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() ); ?>