Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.root {
flex-shrink: 0;
width: calc(var(--wpds-dimension-base) * 17);
height: calc(var(--wpds-dimension-base) * 17);
}

.root.success {
--site-health-stroke: var(--wpds-color-stroke-surface-success);
--site-health-fg: var(--wpds-color-fg-content-success);
}

.root.warning {
--site-health-stroke: var(--wpds-color-stroke-surface-warning);
--site-health-fg: var(--wpds-color-fg-content-warning);
}

.root.error {
--site-health-stroke: var(--wpds-color-stroke-surface-error);
--site-health-fg: var(--wpds-color-fg-content-error);
}

.root circle:first-child {
stroke: var(--wpds-color-bg-track-neutral);
stroke-width: 12;
}

.root circle:last-of-type {
stroke: var(--site-health-stroke);
stroke-width: 12;
stroke-linecap: round;
transform: rotate(-90deg);
transform-origin: 50% 50%;
transition: stroke-dashoffset 0.5s ease;
}

.percentage {
fill: var(--site-health-fg);
font-size: 36px;
font-weight: var(--wpds-typography-font-weight-medium);
}
60 changes: 60 additions & 0 deletions widgets/site-health/components/circle-progress/circle-progress.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* WordPress dependencies
*/
import { SVG, Circle } from '@wordpress/primitives';

/**
* Internal dependencies
*/
import styles from './circle-progress.module.css';

export type HealthTone = 'success' | 'warning' | 'error';

// SVG circle geometry (matches WP core's site-health progress ring).
const RADIUS = 90;
const CIRCUMFERENCE = 2 * Math.PI * RADIUS;

interface CircleProgressProps {
percentage: number;
tone: HealthTone;
}

/* Progress ring; the health tone selects its stroke/fill via a CSS-module class. */
export function CircleProgress( { percentage, tone }: CircleProgressProps ) {
const offset = CIRCUMFERENCE * ( 1 - percentage / 100 );

return (
<SVG
className={ `${ styles.root } ${ styles[ tone ] }` }
viewBox="0 0 200 200"
>
<Circle
r={ RADIUS }
cx="100"
cy="100"
fill="transparent"
strokeDasharray={ CIRCUMFERENCE }
strokeDashoffset={ 0 }
/>

<Circle
r={ RADIUS }
cx="100"
cy="100"
fill="transparent"
strokeDasharray={ CIRCUMFERENCE }
strokeDashoffset={ offset }
/>

<text
x="100"
y="100"
textAnchor="middle"
dominantBaseline="middle"
className={ styles.percentage }
>
{ percentage }%
</text>
</SVG>
);
}
2 changes: 2 additions & 0 deletions widgets/site-health/components/circle-progress/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { CircleProgress } from './circle-progress';
export type { HealthTone } from './circle-progress';
2 changes: 2 additions & 0 deletions widgets/site-health/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { CircleProgress } from './circle-progress';
export type { HealthTone } from './circle-progress';
14 changes: 14 additions & 0 deletions widgets/site-health/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@wordpress/site-health-widget",
"version": "1.0.0",
"private": true,
"dependencies": {
"@wordpress/api-fetch": "file:../../packages/api-fetch",
"@wordpress/components": "file:../../packages/components",
"@wordpress/element": "file:../../packages/element",
"@wordpress/i18n": "file:../../packages/i18n",
"@wordpress/icons": "file:../../packages/icons",
"@wordpress/primitives": "file:../../packages/primitives",
"@wordpress/ui": "file:../../packages/ui"
}
}
170 changes: 170 additions & 0 deletions widgets/site-health/render.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { __, _n, sprintf } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { Spinner } from '@wordpress/components';
import { Link, Stack, Text } from '@wordpress/ui';

/**
* Internal dependencies
*/
import { CircleProgress, type HealthTone } from './components';
import styles from './style.module.css';

type TestStatus = 'good' | 'recommended' | 'critical';

type TestResult = {
status: TestStatus;
};

type IssueCounts = {
good: number;
recommended: number;
critical: number;
};

// Async site health tests exposed via the REST API.
const ASYNC_TEST_PATHS = [
'/wp-site-health/v1/tests/background-updates',
'/wp-site-health/v1/tests/loopback-requests',
'/wp-site-health/v1/tests/https-status',
'/wp-site-health/v1/tests/dotorg-communication',
'/wp-site-health/v1/tests/authorization-header',
] as const;

/**
* Maps a percentage to a semantic tone used for CSS class selection.
*
* @param {number} pct Percentage of passing tests (0-100).
*/
function toneForPercentage( pct: number ): HealthTone {
if ( pct === 100 ) {
return 'success';
}
if ( pct >= 75 ) {
return 'warning';
}
return 'error';
}

/**
* Returns the human-readable status message matching the PHP widget logic.
*
* @param {IssueCounts} counts Aggregated issue counts.
*/
function statusMessage( counts: IssueCounts ): string {
const total = counts.recommended + counts.critical;

if ( total <= 0 ) {
return __(
'Great job! Your site currently passes all site health checks.'
);
}

if ( counts.critical === 1 ) {
return __(
'Your site has a critical issue that should be addressed as soon as possible to improve its performance and security.'
);
}

if ( counts.critical > 1 ) {
return __(
'Your site has critical issues that should be addressed as soon as possible to improve its performance and security.'
);
}

if ( counts.recommended === 1 ) {
return __(
'Your site\u2019s health is looking good, but there is still one thing you can do to improve its performance and security.'
);
}

return __(
'Your site\u2019s health is looking good, but there are still some things you can do to improve its performance and security.'
);
}

export default function SiteHealth() {
const [ counts, setCounts ] = useState< IssueCounts | null >( null );
const [ isLoading, setIsLoading ] = useState( true );

useEffect( () => {
let ignore = false;

const requests = ASYNC_TEST_PATHS.map( ( path ) =>
apiFetch< TestResult >( { path } ).catch( () => null )
);

Promise.all( requests ).then( ( results ) => {
if ( ignore ) {
return;
}

const totals: IssueCounts = {
good: 0,
recommended: 0,
critical: 0,
};

for ( const result of results ) {
if ( result?.status && result.status in totals ) {
totals[ result.status ]++;
}
}

setCounts( totals );
setIsLoading( false );
} );

return () => {
ignore = true;
};
}, [] );

if ( isLoading ) {
return (
<Stack
className={ styles.loading }
direction="column"
align="center"
justify="center"
>
<Spinner />
</Stack>
);
}

if ( ! counts ) {
return null;
}

const total = counts.good + counts.recommended + counts.critical;
const percentage =
total > 0 ? Math.round( ( counts.good / total ) * 100 ) : 0;
const issuesTotal = counts.recommended + counts.critical;
const tone = toneForPercentage( percentage );

return (
<Stack
className={ styles.root }
align="center"
direction="column"
justify="center"
gap="lg"
>
<CircleProgress percentage={ percentage } tone={ tone } />
<Text variant="body-lg">{ statusMessage( counts ) }</Text>
{ issuesTotal > 0 && (
<Link href="site-health.php">
{ sprintf(
/* translators: %d: Number of issues to address. */
_n( 'Review %d item', 'Review %d items', issuesTotal ),
issuesTotal
) }
</Link>
) }
</Stack>
);
}
11 changes: 11 additions & 0 deletions widgets/site-health/style.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.root {
margin: 0 auto;
max-width: calc(var(--wpds-dimension-base) * 80);
text-align: center;
height: 100%;
}

.loading {
min-height: calc(var(--wpds-dimension-base) * 55);
height: 100%;
}
7 changes: 7 additions & 0 deletions widgets/site-health/widget.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "core/site-health",
"title": "Site Health Status",
"description": "Shows an overview of your site health and any issues that need attention.",
"category": "site",
"presentation": "framed"
}
11 changes: 11 additions & 0 deletions widgets/site-health/widget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { shield } from '@wordpress/icons';

export default {
name: 'core/site-health',
title: __( 'Site Health Status' ),
icon: shield,
};
Loading